2 * Copyright (C) 2008 - 2016 The Geeqie Team
4 * Authors: Vladimir Nadvornik, Laurent Monin
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "ui_fileops.h"
29 gdouble get_zoom_increment(void)
31 return ((options->image.zoom_increment != 0) ? (gdouble)options->image.zoom_increment / 100.0 : 1.0);
34 gchar *utf8_validate_or_convert(const gchar *text)
38 if (!text) return NULL;
41 if (!g_utf8_validate(text, len, NULL))
42 return g_convert(text, len, "UTF-8", "ISO-8859-1", NULL, NULL, NULL);
44 return g_strdup(text);
47 gint utf8_compare(const gchar *s1, const gchar *s2, gboolean case_sensitive)
49 gchar *s1_key, *s2_key;
53 g_assert(g_utf8_validate(s1, -1, NULL));
54 g_assert(g_utf8_validate(s2, -1, NULL));
58 s1_t = g_utf8_casefold(s1, -1);
59 s2_t = g_utf8_casefold(s2, -1);
67 s1_key = g_utf8_collate_key(s1_t, -1);
68 s2_key = g_utf8_collate_key(s2_t, -1);
70 ret = strcmp(s1_key, s2_key);
84 /* Borrowed from gtkfilesystemunix.c */
85 gchar *expand_tilde(const gchar *filename)
88 return g_strdup(filename);
94 if (filename[0] != '~')
95 return g_strdup(filename);
97 notilde = filename + 1;
98 slash = strchr(notilde, G_DIR_SEPARATOR);
99 if (slash == notilde || !*notilde)
101 home = g_get_home_dir();
103 return g_strdup(filename);
108 struct passwd *passwd;
111 username = g_strndup(notilde, slash - notilde);
113 username = g_strdup(notilde);
115 passwd = getpwnam(username);
119 return g_strdup(filename);
121 home = passwd->pw_dir;
125 return g_build_filename(home, G_DIR_SEPARATOR_S, slash + 1, NULL);
127 return g_build_filename(home, G_DIR_SEPARATOR_S, NULL);
131 /* Search for latitude/longitude parameters in a string
134 #define GEOCODE_NAME "geocode-parameters.awk"
137 gchar *decode_geo_script(const gchar *path_dir, const gchar *input_text)
140 gchar *path = g_build_filename(path_dir, GEOCODE_NAME, NULL);
141 gchar *cmd = g_strconcat("echo \'", input_text, "\' | awk -f ", path, NULL);
143 if (g_file_test(path, G_FILE_TEST_EXISTS))
148 if ((fp = popen(cmd, "r")) == NULL)
150 message = g_strconcat("Error: opening pipe\n", input_text, NULL);
154 while (fgets(buf, BUFSIZE, fp))
156 DEBUG_1("Output: %s", buf);
159 message = g_strconcat(buf, NULL);
163 message = g_strconcat("Error: Command not found or exited with error status\n", input_text, NULL);
169 message = g_strconcat(input_text, NULL);
177 gchar *decode_geo_parameters(const gchar *input_text)
182 message = decode_geo_script(gq_bindir, input_text);
183 if (strstr(message, "Error"))
186 dir = g_build_filename(get_rc_dir(), "applications", NULL);
187 message = decode_geo_script(dir, input_text);
194 /* Run a command like system() but may output debug messages. */
195 int runcmd(gchar *cmd)
201 /* For debugging purposes */
205 DEBUG_1("Running command: %s", cmd);
207 in = popen(cmd, "r");
214 while (fgets(buf, sizeof(buf), in) != NULL )
216 DEBUG_1("Output: %s", buf);
221 if (WIFEXITED(status))
223 msg = "Command terminated with exit code";
224 retval = WEXITSTATUS(status);
226 else if (WIFSIGNALED(status))
228 msg = "Command was killed by signal";
229 retval = WTERMSIG(status);
233 msg = "pclose() returned";
237 DEBUG_1("%s : %d\n", msg, retval);
245 * @brief Returns integer representing first_day_of_week
246 * @returns Integer in range 1 to 7
248 * Uses current locale to get first day of week.
249 * If _NL_TIME_FIRST_WEEKDAY is not available, ISO 8601
250 * states first day of week is Monday.
251 * USA, Mexico and Canada (and others) use Sunday as first day of week.
255 gint date_get_first_day_of_week()
257 #ifdef HAVE__NL_TIME_FIRST_WEEKDAY
258 return nl_langinfo(_NL_TIME_FIRST_WEEKDAY)[0];
261 gchar *current_locale;
263 current_locale = setlocale(LC_ALL, NULL);
264 dot = strstr(current_locale, ".");
265 if ((strncmp(dot - 2, "US", 2) == 0) || (strncmp(dot - 2, "MX", 2) == 0) || (strncmp(dot - 2, "CA", 2) == 0))
277 * @brief Get an abbreviated day name from locale
278 * @param day Integer in range 1 to 7, representing day of week
279 * @returns String containing abbreviated day name
281 * Uses current locale to get day name
284 * Result must be freed
286 gchar *date_get_abbreviated_day_name(gint day)
293 abday = g_strdup(nl_langinfo(ABDAY_1));
296 abday = g_strdup(nl_langinfo(ABDAY_2));
299 abday = g_strdup(nl_langinfo(ABDAY_3));
302 abday = g_strdup(nl_langinfo(ABDAY_4));
305 abday = g_strdup(nl_langinfo(ABDAY_5));
308 abday = g_strdup(nl_langinfo(ABDAY_6));
311 abday = g_strdup(nl_langinfo(ABDAY_7));
318 gchar *convert_rating_to_stars(gint rating)
321 GString *str = g_string_new(NULL);
325 str = g_string_append_unichar(str, options->star_rating.rejected);
326 ret = g_strdup(str->str);
327 g_string_free(str, TRUE);
329 else if (rating > 0 && rating < 6)
333 str = g_string_append_unichar(str, options->star_rating.star);
336 ret = g_strdup(str->str);
337 g_string_free(str, TRUE);
347 gchar *get_symbolic_link(const gchar *path_utf8)
351 gchar *ret = g_strdup("");
353 sl = path_from_utf8(path_utf8);
355 if (lstat(sl, &st) == 0 && S_ISLNK(st.st_mode))
360 buf = g_malloc(st.st_size + 1);
361 l = readlink(sl, buf, st.st_size);
380 gint get_cpu_cores(void)
382 return sysconf(_SC_NPROCESSORS_ONLN);
385 void tree_path_free_wrapper(void *data, void *useradata)
387 gtk_tree_path_free(data);
390 /* Copied from the libarchive .repo. examples */
393 gchar *open_archive(FileData *fd)
395 log_printf("%s", _("Warning: libarchive not installed"));
402 #include <archive_entry.h>
404 static void errmsg(const char *);
405 static gboolean extract(const char *filename, int do_extract, int flags);
406 static int copy_data(struct archive *, struct archive *);
407 static void msg(const char *);
408 static int verbose = 0;
410 gchar *open_archive(FileData *fd)
414 gchar *destination_dir;
418 destination_dir = g_build_filename(g_get_tmp_dir(), GQ_ARCHIVE_DIR, instance_identifier, fd->path, NULL);
420 if (!recursive_mkdir_if_not_exists(destination_dir, 0755))
422 log_printf("%s%s%s", _("Open Archive - Cannot create directory: "), destination_dir, "\n");
423 g_free(destination_dir);
427 current_dir = g_get_current_dir();
428 error = chdir(destination_dir);
431 log_printf("%s%s%s%s%s", _("Open Archive - Cannot change directory to: "), destination_dir, _("\n Error code: "), strerror(errno), "\n");
432 g_free(destination_dir);
437 flags = ARCHIVE_EXTRACT_TIME;
438 success = extract(fd->path, 1, flags);
440 error = chdir(current_dir);
443 log_printf("%s%s%s%s%s", _("Open Archive - Cannot change directory to: "), current_dir, _("\n Error code: "), strerror(errno), "\n");
444 g_free(destination_dir);
452 g_free(destination_dir);
453 destination_dir = NULL;
456 return destination_dir;
459 static gboolean extract(const char *filename, int do_extract, int flags)
463 struct archive_entry *entry;
466 a = archive_read_new();
467 ext = archive_write_disk_new();
468 archive_write_disk_set_options(ext, flags);
469 archive_write_disk_set_standard_lookup(ext);
470 archive_read_support_filter_all(a);
471 archive_read_support_format_all(a);
473 if (filename != NULL && strcmp(filename, "-") == 0)
477 if ((r = archive_read_open_filename(a, filename, 10240)))
479 errmsg(archive_error_string(a));
487 r = archive_read_next_header(a, &entry);
488 if (r == ARCHIVE_EOF)
494 errmsg(archive_error_string(a));
498 if (verbose && do_extract)
502 if (verbose || !do_extract)
504 msg(archive_entry_pathname(entry));
510 r = archive_write_header(ext, entry);
513 errmsg(archive_error_string(a));
518 r = copy_data(a, ext);
530 archive_read_close(a);
531 archive_read_free(a);
533 archive_write_close(ext);
534 archive_write_free(ext);
538 static int copy_data(struct archive *ar, struct archive *aw)
547 r = archive_read_data_block(ar, &buff, &size, &offset);
548 if (r == ARCHIVE_EOF)
552 errmsg(archive_error_string(ar));
555 r = archive_write_data_block(aw, buff, size, offset);
558 errmsg(archive_error_string(ar));
564 static void msg(const char *m)
566 log_printf("Open Archive - libarchive error: %s \n", m);
569 static void errmsg(const char *m)
573 m = "Error: No error description provided.\n";
575 log_printf("Open Archive - libarchive error: %s \n", m);
578 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */