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.
27 #include "ui-fileops.h"
31 gdouble get_zoom_increment()
33 return ((options->image.zoom_increment != 0) ? static_cast<gdouble>(options->image.zoom_increment) / 100.0 : 1.0);
36 gchar *utf8_validate_or_convert(const gchar *text)
40 if (!text) return nullptr;
43 if (!g_utf8_validate(text, len, nullptr))
44 return g_convert(text, len, "UTF-8", "ISO-8859-1", nullptr, nullptr, nullptr);
46 return g_strdup(text);
49 gint utf8_compare(const gchar *s1, const gchar *s2, gboolean case_sensitive)
51 gchar *s1_key, *s2_key;
55 g_assert(g_utf8_validate(s1, -1, nullptr));
56 g_assert(g_utf8_validate(s2, -1, nullptr));
60 s1_t = g_utf8_casefold(s1, -1);
61 s2_t = g_utf8_casefold(s2, -1);
65 s1_t = const_cast<gchar *>(s1);
66 s2_t = const_cast<gchar *>(s2);
69 s1_key = g_utf8_collate_key(s1_t, -1);
70 s2_key = g_utf8_collate_key(s2_t, -1);
72 ret = strcmp(s1_key, s2_key);
86 /* Borrowed from gtkfilesystemunix.c */
87 gchar *expand_tilde(const gchar *filename)
90 return g_strdup(filename);
96 if (filename[0] != '~')
97 return g_strdup(filename);
99 notilde = filename + 1;
100 slash = strchr(notilde, G_DIR_SEPARATOR);
101 if (slash == notilde || !*notilde)
103 home = g_get_home_dir();
105 return g_strdup(filename);
110 struct passwd *passwd;
113 username = g_strndup(notilde, slash - notilde);
115 username = g_strdup(notilde);
117 passwd = getpwnam(username);
121 return g_strdup(filename);
123 home = passwd->pw_dir;
127 return g_build_filename(home, G_DIR_SEPARATOR_S, slash + 1, NULL);
129 return g_build_filename(home, G_DIR_SEPARATOR_S, NULL);
133 /* Search for latitude/longitude parameters in a string
136 #define GEOCODE_NAME "geocode-parameters.awk"
139 gchar *decode_geo_script(const gchar *path_dir, const gchar *input_text)
141 std::unique_ptr<gchar, decltype(&g_free)> message{nullptr, g_free};
142 gchar *path = g_build_filename(path_dir, GEOCODE_NAME, NULL);
143 gchar *cmd = g_strconcat("echo \'", input_text, "\' | awk -f ", path, NULL);
145 if (g_file_test(path, G_FILE_TEST_EXISTS))
150 if ((fp = popen(cmd, "r")) == nullptr)
152 message.reset(g_strconcat("Error: opening pipe\n", input_text, NULL));
156 while (fgets(buf, BUFSIZE, fp))
158 DEBUG_1("Output: %s", buf);
161 message.reset(g_strconcat(buf, NULL));
165 message.reset(g_strconcat("Error: Command not found or exited with error status\n", input_text, NULL));
171 message.reset(g_strconcat(input_text, NULL));
176 return message.release();
179 gchar *decode_geo_parameters(const gchar *input_text)
184 message = decode_geo_script(gq_bindir, input_text);
185 if (strstr(message, "Error"))
188 dir = g_build_filename(get_rc_dir(), "applications", NULL);
189 message = decode_geo_script(dir, input_text);
196 /* Run a command like system() but may output debug messages. */
197 int runcmd(const gchar *cmd)
203 /* For debugging purposes */
207 DEBUG_1("Running command: %s", cmd);
209 in = popen(cmd, "r");
216 while (fgets(buf, sizeof(buf), in) != NULL )
218 DEBUG_1("Output: %s", buf);
223 if (WIFEXITED(status))
225 msg = "Command terminated with exit code";
226 retval = WEXITSTATUS(status);
228 else if (WIFSIGNALED(status))
230 msg = "Command was killed by signal";
231 retval = WTERMSIG(status);
235 msg = "pclose() returned";
239 DEBUG_1("%s : %d\n", msg, retval);
247 * @brief Returns integer representing first_day_of_week
248 * @returns Integer in range 1 to 7
250 * Uses current locale to get first day of week.
251 * If _NL_TIME_FIRST_WEEKDAY is not available, ISO 8601
252 * states first day of week is Monday.
253 * USA, Mexico and Canada (and others) use Sunday as first day of week.
257 gint date_get_first_day_of_week()
259 #ifdef HAVE__NL_TIME_FIRST_WEEKDAY
260 return nl_langinfo(_NL_TIME_FIRST_WEEKDAY)[0];
263 gchar *current_locale;
265 current_locale = setlocale(LC_ALL, NULL);
266 dot = strstr(current_locale, ".");
267 if ((strncmp(dot - 2, "US", 2) == 0) || (strncmp(dot - 2, "MX", 2) == 0) || (strncmp(dot - 2, "CA", 2) == 0))
279 * @brief Get an abbreviated day name from locale
280 * @param day Integer in range 1 to 7, representing day of week
281 * @returns String containing abbreviated day name
283 * Uses current locale to get day name
286 * Result must be freed
288 gchar *date_get_abbreviated_day_name(gint day)
290 gchar *abday = nullptr;
295 abday = g_strdup(nl_langinfo(ABDAY_1));
298 abday = g_strdup(nl_langinfo(ABDAY_2));
301 abday = g_strdup(nl_langinfo(ABDAY_3));
304 abday = g_strdup(nl_langinfo(ABDAY_4));
307 abday = g_strdup(nl_langinfo(ABDAY_5));
310 abday = g_strdup(nl_langinfo(ABDAY_6));
313 abday = g_strdup(nl_langinfo(ABDAY_7));
320 gchar *convert_rating_to_stars(gint rating)
323 GString *str = g_string_new(nullptr);
327 str = g_string_append_unichar(str, options->star_rating.rejected);
328 ret = g_strdup(str->str);
329 g_string_free(str, TRUE);
331 else if (rating > 0 && rating < 6)
335 str = g_string_append_unichar(str, options->star_rating.star);
338 ret = g_strdup(str->str);
339 g_string_free(str, TRUE);
349 gchar *get_symbolic_link(const gchar *path_utf8)
353 gchar *ret = g_strdup("");
355 sl = path_from_utf8(path_utf8);
357 if (lstat(sl, &st) == 0 && S_ISLNK(st.st_mode))
362 buf = static_cast<gchar *>(g_malloc(st.st_size + 1));
363 l = readlink(sl, buf, st.st_size);
384 return sysconf(_SC_NPROCESSORS_ONLN);
387 /* Copied from the libarchive .repo. examples */
390 gchar *open_archive(FileData *)
392 log_printf("%s", _("Warning: libarchive not installed"));
399 #include <archive_entry.h>
401 static void errmsg(const char *);
402 static gboolean extract(const char *filename, int do_extract, int flags);
403 static int copy_data(struct archive *, struct archive *);
404 static void msg(const char *);
405 static int verbose = 0;
407 gchar *open_archive(FileData *fd)
411 gchar *destination_dir;
415 destination_dir = g_build_filename(g_get_tmp_dir(), GQ_ARCHIVE_DIR, instance_identifier, fd->path, NULL);
417 if (!recursive_mkdir_if_not_exists(destination_dir, 0755))
419 log_printf("%s%s%s", _("Open Archive - Cannot create directory: "), destination_dir, "\n");
420 g_free(destination_dir);
424 current_dir = g_get_current_dir();
425 error = chdir(destination_dir);
428 log_printf("%s%s%s%s%s", _("Open Archive - Cannot change directory to: "), destination_dir, _("\n Error code: "), strerror(errno), "\n");
429 g_free(destination_dir);
434 flags = ARCHIVE_EXTRACT_TIME;
435 success = extract(fd->path, 1, flags);
437 error = chdir(current_dir);
440 log_printf("%s%s%s%s%s", _("Open Archive - Cannot change directory to: "), current_dir, _("\n Error code: "), strerror(errno), "\n");
441 g_free(destination_dir);
449 g_free(destination_dir);
450 destination_dir = nullptr;
453 return destination_dir;
456 static gboolean extract(const char *filename, int do_extract, int flags)
460 struct archive_entry *entry;
463 a = archive_read_new();
464 ext = archive_write_disk_new();
465 archive_write_disk_set_options(ext, flags);
466 archive_write_disk_set_standard_lookup(ext);
467 archive_read_support_filter_all(a);
468 archive_read_support_format_all(a);
470 if (filename != nullptr && strcmp(filename, "-") == 0)
474 if ((r = archive_read_open_filename(a, filename, 10240)))
476 errmsg(archive_error_string(a));
484 r = archive_read_next_header(a, &entry);
485 if (r == ARCHIVE_EOF)
491 errmsg(archive_error_string(a));
495 if (verbose && do_extract)
499 if (verbose || !do_extract)
501 msg(archive_entry_pathname(entry));
507 r = archive_write_header(ext, entry);
510 errmsg(archive_error_string(a));
515 r = copy_data(a, ext);
527 archive_read_close(a);
528 archive_read_free(a);
530 archive_write_close(ext);
531 archive_write_free(ext);
535 static int copy_data(struct archive *ar, struct archive *aw)
544 r = archive_read_data_block(ar, &buff, &size, &offset);
545 if (r == ARCHIVE_EOF)
549 errmsg(archive_error_string(ar));
552 r = archive_write_data_block(aw, buff, size, offset);
555 errmsg(archive_error_string(ar));
561 static void msg(const char *m)
563 log_printf("Open Archive - libarchive error: %s \n", m);
566 static void errmsg(const char *m)
570 m = "Error: No error description provided.\n";
572 log_printf("Open Archive - libarchive error: %s \n", m);
575 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */