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.
41 #include "main-defines.h"
44 #include "ui-fileops.h"
46 gdouble get_zoom_increment()
48 return ((options->image.zoom_increment != 0) ? static_cast<gdouble>(options->image.zoom_increment) / 100.0 : 1.0);
51 gchar *utf8_validate_or_convert(const gchar *text)
55 if (!text) return nullptr;
58 if (!g_utf8_validate(text, len, nullptr))
59 return g_convert(text, len, "UTF-8", "ISO-8859-1", nullptr, nullptr, nullptr);
61 return g_strdup(text);
64 gint utf8_compare(const gchar *s1, const gchar *s2, gboolean case_sensitive)
72 g_assert(g_utf8_validate(s1, -1, nullptr));
73 g_assert(g_utf8_validate(s2, -1, nullptr));
77 s1_t = g_utf8_casefold(s1, -1);
78 s2_t = g_utf8_casefold(s2, -1);
82 s1_t = const_cast<gchar *>(s1);
83 s2_t = const_cast<gchar *>(s2);
86 s1_key = g_utf8_collate_key(s1_t, -1);
87 s2_key = g_utf8_collate_key(s2_t, -1);
89 ret = strcmp(s1_key, s2_key);
103 /* Borrowed from gtkfilesystemunix.c */
104 gchar *expand_tilde(const gchar *filename)
107 return g_strdup(filename);
109 const gchar *notilde;
113 if (filename[0] != '~')
114 return g_strdup(filename);
116 notilde = filename + 1;
117 slash = strchr(notilde, G_DIR_SEPARATOR);
118 if (slash == notilde || !*notilde)
120 home = g_get_home_dir();
122 return g_strdup(filename);
127 struct passwd *passwd;
130 username = g_strndup(notilde, slash - notilde);
132 username = g_strdup(notilde);
134 passwd = getpwnam(username);
138 return g_strdup(filename);
140 home = passwd->pw_dir;
144 return g_build_filename(home, G_DIR_SEPARATOR_S, slash + 1, NULL);
146 return g_build_filename(home, G_DIR_SEPARATOR_S, NULL);
150 /* Search for latitude/longitude parameters in a string
153 #define GEOCODE_NAME "geocode-parameters.awk"
158 gchar *decode_geo_script(const gchar *path_dir, const gchar *input_text)
160 std::unique_ptr<gchar, decltype(&g_free)> message{nullptr, g_free};
161 gchar *path = g_build_filename(path_dir, GEOCODE_NAME, NULL);
162 gchar *cmd = g_strconcat("echo \'", input_text, "\' | awk -f ", path, NULL);
164 if (g_file_test(path, G_FILE_TEST_EXISTS))
169 if ((fp = popen(cmd, "r")) == nullptr)
171 message.reset(g_strconcat("Error: opening pipe\n", input_text, NULL));
175 while (fgets(buf, BUFSIZE, fp))
177 DEBUG_1("Output: %s", buf);
180 message.reset(g_strconcat(buf, NULL));
184 message.reset(g_strconcat("Error: Command not found or exited with error status\n", input_text, NULL));
190 message.reset(g_strconcat(input_text, NULL));
195 return message.release();
198 gchar *decode_geo_parameters(const gchar *input_text)
203 message = decode_geo_script(gq_bindir, input_text);
204 if (strstr(message, "Error"))
207 dir = g_build_filename(get_rc_dir(), "applications", NULL);
208 message = decode_geo_script(dir, input_text);
215 /* Run a command like system() but may output debug messages. */
216 int runcmd(const gchar *cmd)
222 /* For debugging purposes */
226 DEBUG_1("Running command: %s", cmd);
228 in = popen(cmd, "r");
235 while (fgets(buf, sizeof(buf), in) != NULL )
237 DEBUG_1("Output: %s", buf);
242 if (WIFEXITED(status))
244 msg = "Command terminated with exit code";
245 retval = WEXITSTATUS(status);
247 else if (WIFSIGNALED(status))
249 msg = "Command was killed by signal";
250 retval = WTERMSIG(status);
254 msg = "pclose() returned";
258 DEBUG_1("%s : %d\n", msg, retval);
266 * @brief Returns integer representing first_day_of_week
267 * @returns Integer in range 1 to 7
269 * Uses current locale to get first day of week.
270 * If _NL_TIME_FIRST_WEEKDAY is not available, ISO 8601
271 * states first day of week is Monday.
272 * USA, Mexico and Canada (and others) use Sunday as first day of week.
276 gint date_get_first_day_of_week()
278 #if HAVE__NL_TIME_FIRST_WEEKDAY
279 return nl_langinfo(_NL_TIME_FIRST_WEEKDAY)[0];
282 gchar *current_locale;
284 current_locale = setlocale(LC_ALL, NULL);
285 dot = strstr(current_locale, ".");
286 if ((strncmp(dot - 2, "US", 2) == 0) || (strncmp(dot - 2, "MX", 2) == 0) || (strncmp(dot - 2, "CA", 2) == 0))
298 * @brief Get an abbreviated day name from locale
299 * @param day Integer in range 1 to 7, representing day of week
300 * @returns String containing abbreviated day name
302 * Uses current locale to get day name
305 * Result must be freed
307 gchar *date_get_abbreviated_day_name(gint day)
309 gchar *abday = nullptr;
314 abday = g_strdup(nl_langinfo(ABDAY_1));
317 abday = g_strdup(nl_langinfo(ABDAY_2));
320 abday = g_strdup(nl_langinfo(ABDAY_3));
323 abday = g_strdup(nl_langinfo(ABDAY_4));
326 abday = g_strdup(nl_langinfo(ABDAY_5));
329 abday = g_strdup(nl_langinfo(ABDAY_6));
332 abday = g_strdup(nl_langinfo(ABDAY_7));
339 gchar *convert_rating_to_stars(gint rating)
341 GString *str = g_string_new(nullptr);
345 str = g_string_append_unichar(str, options->star_rating.rejected);
346 return g_string_free(str, FALSE);
349 if (rating > 0 && rating < 6)
351 for (; rating > 0; --rating)
353 str = g_string_append_unichar(str, options->star_rating.star);
355 return g_string_free(str, FALSE);
361 gchar *get_symbolic_link(const gchar *path_utf8)
365 gchar *ret = g_strdup("");
367 sl = path_from_utf8(path_utf8);
369 if (lstat(sl, &st) == 0 && S_ISLNK(st.st_mode))
374 buf = static_cast<gchar *>(g_malloc(st.st_size + 1));
375 l = readlink(sl, buf, st.st_size);
396 return sysconf(_SC_NPROCESSORS_ONLN);
400 void convert_gdkcolor_to_gdkrgba(gpointer data, GdkRGBA *gdk_rgba)
402 /* @FIXME GTK4 stub */
405 void convert_gdkcolor_to_gdkrgba(gpointer data, GdkRGBA *gdk_rgba)
407 auto gdk_color = static_cast<GdkColor *>(data);
409 gdk_rgba->red = CLAMP((double)gdk_color->red / 65535.0, 0.0, 1.0);
410 gdk_rgba->green = CLAMP((double)gdk_color->green / 65535.0, 0.0, 1.0);
411 gdk_rgba->blue = CLAMP((double)gdk_color->blue / 65535.0, 0.0, 1.0);
412 gdk_rgba->alpha = 1.0;
416 void gq_gtk_entry_set_text(GtkEntry *entry, const gchar *text)
418 GtkEntryBuffer *buffer;
420 buffer = gtk_entry_get_buffer(entry);
421 gtk_entry_buffer_set_text(buffer, text, static_cast<gint>(g_utf8_strlen(text, -1)));
424 const gchar *gq_gtk_entry_get_text(GtkEntry *entry)
426 GtkEntryBuffer *buffer;
428 buffer = gtk_entry_get_buffer(entry);
429 return gtk_entry_buffer_get_text(buffer);
432 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)
434 gtk_grid_attach(grid, child, left_attach, top_attach, right_attach - left_attach, bottom_attach - top_attach);
437 void gq_gtk_grid_attach_default(GtkGrid *grid, GtkWidget *child, guint left_attach, guint right_attach, guint top_attach, guint bottom_attach )
439 gtk_grid_attach(grid, child, left_attach, top_attach, right_attach - left_attach, bottom_attach - top_attach);
442 /* Copied from the libarchive .repo. examples */
445 gchar *open_archive(FileData *)
447 log_printf("%s", _("Warning: libarchive not installed"));
454 #include <archive_entry.h>
456 static void errmsg(const char *);
457 static gboolean extract(const char *filename, int do_extract, int flags);
458 static int copy_data(struct archive *, struct archive *);
459 static void msg(const char *);
460 static int verbose = 0;
462 gchar *open_archive(FileData *fd)
466 gchar *destination_dir;
470 destination_dir = g_build_filename(g_get_tmp_dir(), GQ_ARCHIVE_DIR, instance_identifier, fd->path, NULL);
472 if (!recursive_mkdir_if_not_exists(destination_dir, 0755))
474 log_printf("%s%s%s", _("Open Archive - Cannot create directory: "), destination_dir, "\n");
475 g_free(destination_dir);
479 current_dir = g_get_current_dir();
480 error = chdir(destination_dir);
483 log_printf("%s%s%s%s%s", _("Open Archive - Cannot change directory to: "), destination_dir, _("\n Error code: "), strerror(errno), "\n");
484 g_free(destination_dir);
489 flags = ARCHIVE_EXTRACT_TIME;
490 success = extract(fd->path, 1, flags);
492 error = chdir(current_dir);
495 log_printf("%s%s%s%s%s", _("Open Archive - Cannot change directory to: "), current_dir, _("\n Error code: "), strerror(errno), "\n");
496 g_free(destination_dir);
504 g_free(destination_dir);
505 destination_dir = nullptr;
508 return destination_dir;
511 static gboolean extract(const char *filename, int do_extract, int flags)
515 struct archive_entry *entry;
518 a = archive_read_new();
519 ext = archive_write_disk_new();
520 archive_write_disk_set_options(ext, flags);
521 archive_write_disk_set_standard_lookup(ext);
522 archive_read_support_filter_all(a);
523 archive_read_support_format_all(a);
525 if (filename != nullptr && strcmp(filename, "-") == 0)
529 if ((r = archive_read_open_filename(a, filename, 10240)))
531 errmsg(archive_error_string(a));
539 r = archive_read_next_header(a, &entry);
540 if (r == ARCHIVE_EOF)
546 errmsg(archive_error_string(a));
550 if (verbose && do_extract)
554 if (verbose || !do_extract)
556 msg(archive_entry_pathname(entry));
562 r = archive_write_header(ext, entry);
565 errmsg(archive_error_string(a));
570 r = copy_data(a, ext);
582 archive_read_close(a);
583 archive_read_free(a);
585 archive_write_close(ext);
586 archive_write_free(ext);
590 static int copy_data(struct archive *ar, struct archive *aw)
599 r = archive_read_data_block(ar, &buff, &size, &offset);
600 if (r == ARCHIVE_EOF)
604 errmsg(archive_error_string(ar));
607 r = archive_write_data_block(aw, buff, size, offset);
610 errmsg(archive_error_string(ar));
616 static void msg(const char *m)
618 log_printf("Open Archive - libarchive error: %s \n", m);
621 static void errmsg(const char *m)
625 m = "Error: No error description provided.\n";
627 log_printf("Open Archive - libarchive error: %s \n", m);
630 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */