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.
32 #include "main-defines.h"
34 #include "ui-fileops.h"
39 gdouble get_zoom_increment()
41 return ((options->image.zoom_increment != 0) ? static_cast<gdouble>(options->image.zoom_increment) / 100.0 : 1.0);
44 gchar *utf8_validate_or_convert(const gchar *text)
48 if (!text) return nullptr;
51 if (!g_utf8_validate(text, len, nullptr))
52 return g_convert(text, len, "UTF-8", "ISO-8859-1", nullptr, nullptr, nullptr);
54 return g_strdup(text);
57 gint utf8_compare(const gchar *s1, const gchar *s2, gboolean case_sensitive)
65 g_assert(g_utf8_validate(s1, -1, nullptr));
66 g_assert(g_utf8_validate(s2, -1, nullptr));
70 s1_t = g_utf8_casefold(s1, -1);
71 s2_t = g_utf8_casefold(s2, -1);
75 s1_t = const_cast<gchar *>(s1);
76 s2_t = const_cast<gchar *>(s2);
79 s1_key = g_utf8_collate_key(s1_t, -1);
80 s2_key = g_utf8_collate_key(s2_t, -1);
82 ret = strcmp(s1_key, s2_key);
96 /* Borrowed from gtkfilesystemunix.c */
97 gchar *expand_tilde(const gchar *filename)
100 return g_strdup(filename);
102 const gchar *notilde;
106 if (filename[0] != '~')
107 return g_strdup(filename);
109 notilde = filename + 1;
110 slash = strchr(notilde, G_DIR_SEPARATOR);
111 if (slash == notilde || !*notilde)
113 home = g_get_home_dir();
115 return g_strdup(filename);
120 struct passwd *passwd;
123 username = g_strndup(notilde, slash - notilde);
125 username = g_strdup(notilde);
127 passwd = getpwnam(username);
131 return g_strdup(filename);
133 home = passwd->pw_dir;
137 return g_build_filename(home, G_DIR_SEPARATOR_S, slash + 1, NULL);
139 return g_build_filename(home, G_DIR_SEPARATOR_S, NULL);
143 /* Search for latitude/longitude parameters in a string
146 #define GEOCODE_NAME "geocode-parameters.awk"
151 gchar *decode_geo_script(const gchar *path_dir, const gchar *input_text)
153 std::unique_ptr<gchar, decltype(&g_free)> message{nullptr, g_free};
154 gchar *path = g_build_filename(path_dir, GEOCODE_NAME, NULL);
155 gchar *cmd = g_strconcat("echo \'", input_text, "\' | awk -f ", path, NULL);
157 if (g_file_test(path, G_FILE_TEST_EXISTS))
162 if ((fp = popen(cmd, "r")) == nullptr)
164 message.reset(g_strconcat("Error: opening pipe\n", input_text, NULL));
168 while (fgets(buf, BUFSIZE, fp))
170 DEBUG_1("Output: %s", buf);
173 message.reset(g_strconcat(buf, NULL));
177 message.reset(g_strconcat("Error: Command not found or exited with error status\n", input_text, NULL));
183 message.reset(g_strconcat(input_text, NULL));
188 return message.release();
191 gchar *decode_geo_parameters(const gchar *input_text)
196 message = decode_geo_script(gq_bindir, input_text);
197 if (strstr(message, "Error"))
200 dir = g_build_filename(get_rc_dir(), "applications", NULL);
201 message = decode_geo_script(dir, input_text);
208 /* Run a command like system() but may output debug messages. */
209 int runcmd(const gchar *cmd)
215 /* For debugging purposes */
219 DEBUG_1("Running command: %s", cmd);
221 in = popen(cmd, "r");
228 while (fgets(buf, sizeof(buf), in) != NULL )
230 DEBUG_1("Output: %s", buf);
235 if (WIFEXITED(status))
237 msg = "Command terminated with exit code";
238 retval = WEXITSTATUS(status);
240 else if (WIFSIGNALED(status))
242 msg = "Command was killed by signal";
243 retval = WTERMSIG(status);
247 msg = "pclose() returned";
251 DEBUG_1("%s : %d\n", msg, retval);
259 * @brief Returns integer representing first_day_of_week
260 * @returns Integer in range 1 to 7
262 * Uses current locale to get first day of week.
263 * If _NL_TIME_FIRST_WEEKDAY is not available, ISO 8601
264 * states first day of week is Monday.
265 * USA, Mexico and Canada (and others) use Sunday as first day of week.
269 gint date_get_first_day_of_week()
271 #ifdef HAVE__NL_TIME_FIRST_WEEKDAY
272 return nl_langinfo(_NL_TIME_FIRST_WEEKDAY)[0];
275 gchar *current_locale;
277 current_locale = setlocale(LC_ALL, NULL);
278 dot = strstr(current_locale, ".");
279 if ((strncmp(dot - 2, "US", 2) == 0) || (strncmp(dot - 2, "MX", 2) == 0) || (strncmp(dot - 2, "CA", 2) == 0))
291 * @brief Get an abbreviated day name from locale
292 * @param day Integer in range 1 to 7, representing day of week
293 * @returns String containing abbreviated day name
295 * Uses current locale to get day name
298 * Result must be freed
300 gchar *date_get_abbreviated_day_name(gint day)
302 gchar *abday = nullptr;
307 abday = g_strdup(nl_langinfo(ABDAY_1));
310 abday = g_strdup(nl_langinfo(ABDAY_2));
313 abday = g_strdup(nl_langinfo(ABDAY_3));
316 abday = g_strdup(nl_langinfo(ABDAY_4));
319 abday = g_strdup(nl_langinfo(ABDAY_5));
322 abday = g_strdup(nl_langinfo(ABDAY_6));
325 abday = g_strdup(nl_langinfo(ABDAY_7));
332 gchar *convert_rating_to_stars(gint rating)
334 GString *str = g_string_new(nullptr);
338 str = g_string_append_unichar(str, options->star_rating.rejected);
339 return g_string_free(str, FALSE);
342 if (rating > 0 && rating < 6)
344 for (; rating > 0; --rating)
346 str = g_string_append_unichar(str, options->star_rating.star);
348 return g_string_free(str, FALSE);
354 gchar *get_symbolic_link(const gchar *path_utf8)
358 gchar *ret = g_strdup("");
360 sl = path_from_utf8(path_utf8);
362 if (lstat(sl, &st) == 0 && S_ISLNK(st.st_mode))
367 buf = static_cast<gchar *>(g_malloc(st.st_size + 1));
368 l = readlink(sl, buf, st.st_size);
389 return sysconf(_SC_NPROCESSORS_ONLN);
393 void convert_gdkcolor_to_gdkrgba(gpointer data, GdkRGBA *gdk_rgba)
395 /* @FIXME GTK4 stub */
398 void convert_gdkcolor_to_gdkrgba(gpointer data, GdkRGBA *gdk_rgba)
400 auto gdk_color = static_cast<GdkColor *>(data);
402 gdk_rgba->red = CLAMP((double)gdk_color->red / 65535.0, 0.0, 1.0);
403 gdk_rgba->green = CLAMP((double)gdk_color->green / 65535.0, 0.0, 1.0);
404 gdk_rgba->blue = CLAMP((double)gdk_color->blue / 65535.0, 0.0, 1.0);
405 gdk_rgba->alpha = 1.0;
409 void gq_gtk_entry_set_text(GtkEntry *entry, const gchar *text)
411 GtkEntryBuffer *buffer;
413 buffer = gtk_entry_get_buffer(entry);
414 gtk_entry_buffer_set_text(buffer, text, static_cast<gint>(g_utf8_strlen(text, -1)));
417 const gchar *gq_gtk_entry_get_text(GtkEntry *entry)
419 GtkEntryBuffer *buffer;
421 buffer = gtk_entry_get_buffer(entry);
422 return gtk_entry_buffer_get_text(buffer);
425 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)
427 gtk_grid_attach(grid, child, left_attach, top_attach, right_attach - left_attach, bottom_attach - top_attach);
430 void gq_gtk_grid_attach_default(GtkGrid *grid, GtkWidget *child, guint left_attach, guint right_attach, guint top_attach, guint bottom_attach )
432 gtk_grid_attach(grid, child, left_attach, top_attach, right_attach - left_attach, bottom_attach - top_attach);
435 /* Copied from the libarchive .repo. examples */
438 gchar *open_archive(FileData *)
440 log_printf("%s", _("Warning: libarchive not installed"));
447 #include <archive_entry.h>
449 static void errmsg(const char *);
450 static gboolean extract(const char *filename, int do_extract, int flags);
451 static int copy_data(struct archive *, struct archive *);
452 static void msg(const char *);
453 static int verbose = 0;
455 gchar *open_archive(FileData *fd)
459 gchar *destination_dir;
463 destination_dir = g_build_filename(g_get_tmp_dir(), GQ_ARCHIVE_DIR, instance_identifier, fd->path, NULL);
465 if (!recursive_mkdir_if_not_exists(destination_dir, 0755))
467 log_printf("%s%s%s", _("Open Archive - Cannot create directory: "), destination_dir, "\n");
468 g_free(destination_dir);
472 current_dir = g_get_current_dir();
473 error = chdir(destination_dir);
476 log_printf("%s%s%s%s%s", _("Open Archive - Cannot change directory to: "), destination_dir, _("\n Error code: "), strerror(errno), "\n");
477 g_free(destination_dir);
482 flags = ARCHIVE_EXTRACT_TIME;
483 success = extract(fd->path, 1, flags);
485 error = chdir(current_dir);
488 log_printf("%s%s%s%s%s", _("Open Archive - Cannot change directory to: "), current_dir, _("\n Error code: "), strerror(errno), "\n");
489 g_free(destination_dir);
497 g_free(destination_dir);
498 destination_dir = nullptr;
501 return destination_dir;
504 static gboolean extract(const char *filename, int do_extract, int flags)
508 struct archive_entry *entry;
511 a = archive_read_new();
512 ext = archive_write_disk_new();
513 archive_write_disk_set_options(ext, flags);
514 archive_write_disk_set_standard_lookup(ext);
515 archive_read_support_filter_all(a);
516 archive_read_support_format_all(a);
518 if (filename != nullptr && strcmp(filename, "-") == 0)
522 if ((r = archive_read_open_filename(a, filename, 10240)))
524 errmsg(archive_error_string(a));
532 r = archive_read_next_header(a, &entry);
533 if (r == ARCHIVE_EOF)
539 errmsg(archive_error_string(a));
543 if (verbose && do_extract)
547 if (verbose || !do_extract)
549 msg(archive_entry_pathname(entry));
555 r = archive_write_header(ext, entry);
558 errmsg(archive_error_string(a));
563 r = copy_data(a, ext);
575 archive_read_close(a);
576 archive_read_free(a);
578 archive_write_close(ext);
579 archive_write_free(ext);
583 static int copy_data(struct archive *ar, struct archive *aw)
592 r = archive_read_data_block(ar, &buff, &size, &offset);
593 if (r == ARCHIVE_EOF)
597 errmsg(archive_error_string(ar));
600 r = archive_write_data_block(aw, buff, size, offset);
603 errmsg(archive_error_string(ar));
609 static void msg(const char *m)
611 log_printf("Open Archive - libarchive error: %s \n", m);
614 static void errmsg(const char *m)
618 m = "Error: No error description provided.\n";
620 log_printf("Open Archive - libarchive error: %s \n", m);
623 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */