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.
28 #include "ui-fileops.h"
32 gdouble get_zoom_increment()
34 return ((options->image.zoom_increment != 0) ? static_cast<gdouble>(options->image.zoom_increment) / 100.0 : 1.0);
37 gchar *utf8_validate_or_convert(const gchar *text)
41 if (!text) return nullptr;
44 if (!g_utf8_validate(text, len, nullptr))
45 return g_convert(text, len, "UTF-8", "ISO-8859-1", nullptr, nullptr, nullptr);
47 return g_strdup(text);
50 gint utf8_compare(const gchar *s1, const gchar *s2, gboolean case_sensitive)
58 g_assert(g_utf8_validate(s1, -1, nullptr));
59 g_assert(g_utf8_validate(s2, -1, nullptr));
63 s1_t = g_utf8_casefold(s1, -1);
64 s2_t = g_utf8_casefold(s2, -1);
68 s1_t = const_cast<gchar *>(s1);
69 s2_t = const_cast<gchar *>(s2);
72 s1_key = g_utf8_collate_key(s1_t, -1);
73 s2_key = g_utf8_collate_key(s2_t, -1);
75 ret = strcmp(s1_key, s2_key);
89 /* Borrowed from gtkfilesystemunix.c */
90 gchar *expand_tilde(const gchar *filename)
93 return g_strdup(filename);
99 if (filename[0] != '~')
100 return g_strdup(filename);
102 notilde = filename + 1;
103 slash = strchr(notilde, G_DIR_SEPARATOR);
104 if (slash == notilde || !*notilde)
106 home = g_get_home_dir();
108 return g_strdup(filename);
113 struct passwd *passwd;
116 username = g_strndup(notilde, slash - notilde);
118 username = g_strdup(notilde);
120 passwd = getpwnam(username);
124 return g_strdup(filename);
126 home = passwd->pw_dir;
130 return g_build_filename(home, G_DIR_SEPARATOR_S, slash + 1, NULL);
132 return g_build_filename(home, G_DIR_SEPARATOR_S, NULL);
136 /* Search for latitude/longitude parameters in a string
139 #define GEOCODE_NAME "geocode-parameters.awk"
144 gchar *decode_geo_script(const gchar *path_dir, const gchar *input_text)
146 std::unique_ptr<gchar, decltype(&g_free)> message{nullptr, g_free};
147 gchar *path = g_build_filename(path_dir, GEOCODE_NAME, NULL);
148 gchar *cmd = g_strconcat("echo \'", input_text, "\' | awk -f ", path, NULL);
150 if (g_file_test(path, G_FILE_TEST_EXISTS))
155 if ((fp = popen(cmd, "r")) == nullptr)
157 message.reset(g_strconcat("Error: opening pipe\n", input_text, NULL));
161 while (fgets(buf, BUFSIZE, fp))
163 DEBUG_1("Output: %s", buf);
166 message.reset(g_strconcat(buf, NULL));
170 message.reset(g_strconcat("Error: Command not found or exited with error status\n", input_text, NULL));
176 message.reset(g_strconcat(input_text, NULL));
181 return message.release();
184 gchar *decode_geo_parameters(const gchar *input_text)
189 message = decode_geo_script(gq_bindir, input_text);
190 if (strstr(message, "Error"))
193 dir = g_build_filename(get_rc_dir(), "applications", NULL);
194 message = decode_geo_script(dir, input_text);
201 /* Run a command like system() but may output debug messages. */
202 int runcmd(const gchar *cmd)
208 /* For debugging purposes */
212 DEBUG_1("Running command: %s", cmd);
214 in = popen(cmd, "r");
221 while (fgets(buf, sizeof(buf), in) != NULL )
223 DEBUG_1("Output: %s", buf);
228 if (WIFEXITED(status))
230 msg = "Command terminated with exit code";
231 retval = WEXITSTATUS(status);
233 else if (WIFSIGNALED(status))
235 msg = "Command was killed by signal";
236 retval = WTERMSIG(status);
240 msg = "pclose() returned";
244 DEBUG_1("%s : %d\n", msg, retval);
252 * @brief Returns integer representing first_day_of_week
253 * @returns Integer in range 1 to 7
255 * Uses current locale to get first day of week.
256 * If _NL_TIME_FIRST_WEEKDAY is not available, ISO 8601
257 * states first day of week is Monday.
258 * USA, Mexico and Canada (and others) use Sunday as first day of week.
262 gint date_get_first_day_of_week()
264 #ifdef HAVE__NL_TIME_FIRST_WEEKDAY
265 return nl_langinfo(_NL_TIME_FIRST_WEEKDAY)[0];
268 gchar *current_locale;
270 current_locale = setlocale(LC_ALL, NULL);
271 dot = strstr(current_locale, ".");
272 if ((strncmp(dot - 2, "US", 2) == 0) || (strncmp(dot - 2, "MX", 2) == 0) || (strncmp(dot - 2, "CA", 2) == 0))
284 * @brief Get an abbreviated day name from locale
285 * @param day Integer in range 1 to 7, representing day of week
286 * @returns String containing abbreviated day name
288 * Uses current locale to get day name
291 * Result must be freed
293 gchar *date_get_abbreviated_day_name(gint day)
295 gchar *abday = nullptr;
300 abday = g_strdup(nl_langinfo(ABDAY_1));
303 abday = g_strdup(nl_langinfo(ABDAY_2));
306 abday = g_strdup(nl_langinfo(ABDAY_3));
309 abday = g_strdup(nl_langinfo(ABDAY_4));
312 abday = g_strdup(nl_langinfo(ABDAY_5));
315 abday = g_strdup(nl_langinfo(ABDAY_6));
318 abday = g_strdup(nl_langinfo(ABDAY_7));
325 gchar *convert_rating_to_stars(gint rating)
327 GString *str = g_string_new(nullptr);
331 str = g_string_append_unichar(str, options->star_rating.rejected);
332 return g_string_free(str, FALSE);
335 if (rating > 0 && rating < 6)
337 for (; rating > 0; --rating)
339 str = g_string_append_unichar(str, options->star_rating.star);
341 return g_string_free(str, FALSE);
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 = static_cast<gchar *>(g_malloc(st.st_size + 1));
361 l = readlink(sl, buf, st.st_size);
382 return sysconf(_SC_NPROCESSORS_ONLN);
386 void convert_gdkcolor_to_gdkrgba(gpointer data, GdkRGBA *gdk_rgba)
388 /* @FIXME GTK4 stub */
391 void convert_gdkcolor_to_gdkrgba(gpointer data, GdkRGBA *gdk_rgba)
393 auto gdk_color = static_cast<GdkColor *>(data);
395 gdk_rgba->red = CLAMP((double)gdk_color->red / 65535.0, 0.0, 1.0);
396 gdk_rgba->green = CLAMP((double)gdk_color->green / 65535.0, 0.0, 1.0);
397 gdk_rgba->blue = CLAMP((double)gdk_color->blue / 65535.0, 0.0, 1.0);
398 gdk_rgba->alpha = 1.0;
402 void gq_gtk_entry_set_text(GtkEntry *entry, const gchar *text)
404 GtkEntryBuffer *buffer;
406 buffer = gtk_entry_get_buffer(entry);
407 gtk_entry_buffer_set_text(buffer, text, static_cast<gint>(g_utf8_strlen(text, -1)));
410 const gchar *gq_gtk_entry_get_text(GtkEntry *entry)
412 GtkEntryBuffer *buffer;
414 buffer = gtk_entry_get_buffer(entry);
415 return gtk_entry_buffer_get_text(buffer);
418 void gq_gtk_grid_attach(GtkGrid *grid, GtkWidget *child, guint left_attach, guint right_attach, guint top_attach, guint bottom_attach, GtkAttachOptions, GtkAttachOptions, guint, guint)
420 gtk_grid_attach(grid, child, left_attach, top_attach, right_attach - left_attach, bottom_attach - top_attach);
423 void gq_gtk_grid_attach_default(GtkGrid *grid, GtkWidget *child, guint left_attach, guint right_attach, guint top_attach, guint bottom_attach )
425 gtk_grid_attach(grid, child, left_attach, top_attach, right_attach - left_attach, bottom_attach - top_attach);
428 /* Copied from the libarchive .repo. examples */
431 gchar *open_archive(FileData *)
433 log_printf("%s", _("Warning: libarchive not installed"));
440 #include <archive_entry.h>
442 static void errmsg(const char *);
443 static gboolean extract(const char *filename, int do_extract, int flags);
444 static int copy_data(struct archive *, struct archive *);
445 static void msg(const char *);
446 static int verbose = 0;
448 gchar *open_archive(FileData *fd)
452 gchar *destination_dir;
456 destination_dir = g_build_filename(g_get_tmp_dir(), GQ_ARCHIVE_DIR, instance_identifier, fd->path, NULL);
458 if (!recursive_mkdir_if_not_exists(destination_dir, 0755))
460 log_printf("%s%s%s", _("Open Archive - Cannot create directory: "), destination_dir, "\n");
461 g_free(destination_dir);
465 current_dir = g_get_current_dir();
466 error = chdir(destination_dir);
469 log_printf("%s%s%s%s%s", _("Open Archive - Cannot change directory to: "), destination_dir, _("\n Error code: "), strerror(errno), "\n");
470 g_free(destination_dir);
475 flags = ARCHIVE_EXTRACT_TIME;
476 success = extract(fd->path, 1, flags);
478 error = chdir(current_dir);
481 log_printf("%s%s%s%s%s", _("Open Archive - Cannot change directory to: "), current_dir, _("\n Error code: "), strerror(errno), "\n");
482 g_free(destination_dir);
490 g_free(destination_dir);
491 destination_dir = nullptr;
494 return destination_dir;
497 static gboolean extract(const char *filename, int do_extract, int flags)
501 struct archive_entry *entry;
504 a = archive_read_new();
505 ext = archive_write_disk_new();
506 archive_write_disk_set_options(ext, flags);
507 archive_write_disk_set_standard_lookup(ext);
508 archive_read_support_filter_all(a);
509 archive_read_support_format_all(a);
511 if (filename != nullptr && strcmp(filename, "-") == 0)
515 if ((r = archive_read_open_filename(a, filename, 10240)))
517 errmsg(archive_error_string(a));
525 r = archive_read_next_header(a, &entry);
526 if (r == ARCHIVE_EOF)
532 errmsg(archive_error_string(a));
536 if (verbose && do_extract)
540 if (verbose || !do_extract)
542 msg(archive_entry_pathname(entry));
548 r = archive_write_header(ext, entry);
551 errmsg(archive_error_string(a));
556 r = copy_data(a, ext);
568 archive_read_close(a);
569 archive_read_free(a);
571 archive_write_close(ext);
572 archive_write_free(ext);
576 static int copy_data(struct archive *ar, struct archive *aw)
585 r = archive_read_data_block(ar, &buff, &size, &offset);
586 if (r == ARCHIVE_EOF)
590 errmsg(archive_error_string(ar));
593 r = archive_write_data_block(aw, buff, size, offset);
596 errmsg(archive_error_string(ar));
602 static void msg(const char *m)
604 log_printf("Open Archive - libarchive error: %s \n", m);
607 static void errmsg(const char *m)
611 m = "Error: No error description provided.\n";
613 log_printf("Open Archive - libarchive error: %s \n", m);
616 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */