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)
52 gchar *s1_key, *s2_key;
56 g_assert(g_utf8_validate(s1, -1, nullptr));
57 g_assert(g_utf8_validate(s2, -1, nullptr));
61 s1_t = g_utf8_casefold(s1, -1);
62 s2_t = g_utf8_casefold(s2, -1);
66 s1_t = const_cast<gchar *>(s1);
67 s2_t = const_cast<gchar *>(s2);
70 s1_key = g_utf8_collate_key(s1_t, -1);
71 s2_key = g_utf8_collate_key(s2_t, -1);
73 ret = strcmp(s1_key, s2_key);
87 /* Borrowed from gtkfilesystemunix.c */
88 gchar *expand_tilde(const gchar *filename)
91 return g_strdup(filename);
97 if (filename[0] != '~')
98 return g_strdup(filename);
100 notilde = filename + 1;
101 slash = strchr(notilde, G_DIR_SEPARATOR);
102 if (slash == notilde || !*notilde)
104 home = g_get_home_dir();
106 return g_strdup(filename);
111 struct passwd *passwd;
114 username = g_strndup(notilde, slash - notilde);
116 username = g_strdup(notilde);
118 passwd = getpwnam(username);
122 return g_strdup(filename);
124 home = passwd->pw_dir;
128 return g_build_filename(home, G_DIR_SEPARATOR_S, slash + 1, NULL);
130 return g_build_filename(home, G_DIR_SEPARATOR_S, NULL);
134 /* Search for latitude/longitude parameters in a string
137 #define GEOCODE_NAME "geocode-parameters.awk"
140 gchar *decode_geo_script(const gchar *path_dir, const gchar *input_text)
142 std::unique_ptr<gchar, decltype(&g_free)> message{nullptr, g_free};
143 gchar *path = g_build_filename(path_dir, GEOCODE_NAME, NULL);
144 gchar *cmd = g_strconcat("echo \'", input_text, "\' | awk -f ", path, NULL);
146 if (g_file_test(path, G_FILE_TEST_EXISTS))
151 if ((fp = popen(cmd, "r")) == nullptr)
153 message.reset(g_strconcat("Error: opening pipe\n", input_text, NULL));
157 while (fgets(buf, BUFSIZE, fp))
159 DEBUG_1("Output: %s", buf);
162 message.reset(g_strconcat(buf, NULL));
166 message.reset(g_strconcat("Error: Command not found or exited with error status\n", input_text, NULL));
172 message.reset(g_strconcat(input_text, NULL));
177 return message.release();
180 gchar *decode_geo_parameters(const gchar *input_text)
185 message = decode_geo_script(gq_bindir, input_text);
186 if (strstr(message, "Error"))
189 dir = g_build_filename(get_rc_dir(), "applications", NULL);
190 message = decode_geo_script(dir, input_text);
197 /* Run a command like system() but may output debug messages. */
198 int runcmd(const gchar *cmd)
204 /* For debugging purposes */
208 DEBUG_1("Running command: %s", cmd);
210 in = popen(cmd, "r");
217 while (fgets(buf, sizeof(buf), in) != NULL )
219 DEBUG_1("Output: %s", buf);
224 if (WIFEXITED(status))
226 msg = "Command terminated with exit code";
227 retval = WEXITSTATUS(status);
229 else if (WIFSIGNALED(status))
231 msg = "Command was killed by signal";
232 retval = WTERMSIG(status);
236 msg = "pclose() returned";
240 DEBUG_1("%s : %d\n", msg, retval);
248 * @brief Returns integer representing first_day_of_week
249 * @returns Integer in range 1 to 7
251 * Uses current locale to get first day of week.
252 * If _NL_TIME_FIRST_WEEKDAY is not available, ISO 8601
253 * states first day of week is Monday.
254 * USA, Mexico and Canada (and others) use Sunday as first day of week.
258 gint date_get_first_day_of_week()
260 #ifdef HAVE__NL_TIME_FIRST_WEEKDAY
261 return nl_langinfo(_NL_TIME_FIRST_WEEKDAY)[0];
264 gchar *current_locale;
266 current_locale = setlocale(LC_ALL, NULL);
267 dot = strstr(current_locale, ".");
268 if ((strncmp(dot - 2, "US", 2) == 0) || (strncmp(dot - 2, "MX", 2) == 0) || (strncmp(dot - 2, "CA", 2) == 0))
280 * @brief Get an abbreviated day name from locale
281 * @param day Integer in range 1 to 7, representing day of week
282 * @returns String containing abbreviated day name
284 * Uses current locale to get day name
287 * Result must be freed
289 gchar *date_get_abbreviated_day_name(gint day)
291 gchar *abday = nullptr;
296 abday = g_strdup(nl_langinfo(ABDAY_1));
299 abday = g_strdup(nl_langinfo(ABDAY_2));
302 abday = g_strdup(nl_langinfo(ABDAY_3));
305 abday = g_strdup(nl_langinfo(ABDAY_4));
308 abday = g_strdup(nl_langinfo(ABDAY_5));
311 abday = g_strdup(nl_langinfo(ABDAY_6));
314 abday = g_strdup(nl_langinfo(ABDAY_7));
321 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 return g_string_free(str, FALSE);
331 if (rating > 0 && rating < 6)
333 for (; rating > 0; --rating)
335 str = g_string_append_unichar(str, options->star_rating.star);
337 return g_string_free(str, FALSE);
343 gchar *get_symbolic_link(const gchar *path_utf8)
347 gchar *ret = g_strdup("");
349 sl = path_from_utf8(path_utf8);
351 if (lstat(sl, &st) == 0 && S_ISLNK(st.st_mode))
356 buf = static_cast<gchar *>(g_malloc(st.st_size + 1));
357 l = readlink(sl, buf, st.st_size);
378 return sysconf(_SC_NPROCESSORS_ONLN);
382 void convert_gdkcolor_to_gdkrgba(gpointer data, GdkRGBA *gdk_rgba)
384 /* @FIXME GTK4 stub */
387 void convert_gdkcolor_to_gdkrgba(gpointer data, GdkRGBA *gdk_rgba)
389 auto gdk_color = static_cast<GdkColor *>(data);
391 gdk_rgba->red = CLAMP((double)gdk_color->red / 65535.0, 0.0, 1.0);
392 gdk_rgba->green = CLAMP((double)gdk_color->green / 65535.0, 0.0, 1.0);
393 gdk_rgba->blue = CLAMP((double)gdk_color->blue / 65535.0, 0.0, 1.0);
394 gdk_rgba->alpha = 1.0;
398 void gq_gtk_entry_set_text(GtkEntry *entry, const gchar *text)
400 GtkEntryBuffer *buffer;
402 buffer = gtk_entry_get_buffer(entry);
403 gtk_entry_buffer_set_text(buffer, text, static_cast<gint>(g_utf8_strlen(text, -1)));
406 const gchar *gq_gtk_entry_get_text(GtkEntry *entry)
408 GtkEntryBuffer *buffer;
410 buffer = gtk_entry_get_buffer(entry);
411 return gtk_entry_buffer_get_text(buffer);
414 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)
416 gtk_grid_attach(grid, child, left_attach, top_attach, right_attach - left_attach, bottom_attach - top_attach);
419 void gq_gtk_grid_attach_default(GtkGrid *grid, GtkWidget *child, guint left_attach, guint right_attach, guint top_attach, guint bottom_attach )
421 gtk_grid_attach(grid, child, left_attach, top_attach, right_attach - left_attach, bottom_attach - top_attach);
424 /* Copied from the libarchive .repo. examples */
427 gchar *open_archive(FileData *)
429 log_printf("%s", _("Warning: libarchive not installed"));
436 #include <archive_entry.h>
438 static void errmsg(const char *);
439 static gboolean extract(const char *filename, int do_extract, int flags);
440 static int copy_data(struct archive *, struct archive *);
441 static void msg(const char *);
442 static int verbose = 0;
444 gchar *open_archive(FileData *fd)
448 gchar *destination_dir;
452 destination_dir = g_build_filename(g_get_tmp_dir(), GQ_ARCHIVE_DIR, instance_identifier, fd->path, NULL);
454 if (!recursive_mkdir_if_not_exists(destination_dir, 0755))
456 log_printf("%s%s%s", _("Open Archive - Cannot create directory: "), destination_dir, "\n");
457 g_free(destination_dir);
461 current_dir = g_get_current_dir();
462 error = chdir(destination_dir);
465 log_printf("%s%s%s%s%s", _("Open Archive - Cannot change directory to: "), destination_dir, _("\n Error code: "), strerror(errno), "\n");
466 g_free(destination_dir);
471 flags = ARCHIVE_EXTRACT_TIME;
472 success = extract(fd->path, 1, flags);
474 error = chdir(current_dir);
477 log_printf("%s%s%s%s%s", _("Open Archive - Cannot change directory to: "), current_dir, _("\n Error code: "), strerror(errno), "\n");
478 g_free(destination_dir);
486 g_free(destination_dir);
487 destination_dir = nullptr;
490 return destination_dir;
493 static gboolean extract(const char *filename, int do_extract, int flags)
497 struct archive_entry *entry;
500 a = archive_read_new();
501 ext = archive_write_disk_new();
502 archive_write_disk_set_options(ext, flags);
503 archive_write_disk_set_standard_lookup(ext);
504 archive_read_support_filter_all(a);
505 archive_read_support_format_all(a);
507 if (filename != nullptr && strcmp(filename, "-") == 0)
511 if ((r = archive_read_open_filename(a, filename, 10240)))
513 errmsg(archive_error_string(a));
521 r = archive_read_next_header(a, &entry);
522 if (r == ARCHIVE_EOF)
528 errmsg(archive_error_string(a));
532 if (verbose && do_extract)
536 if (verbose || !do_extract)
538 msg(archive_entry_pathname(entry));
544 r = archive_write_header(ext, entry);
547 errmsg(archive_error_string(a));
552 r = copy_data(a, ext);
564 archive_read_close(a);
565 archive_read_free(a);
567 archive_write_close(ext);
568 archive_write_free(ext);
572 static int copy_data(struct archive *ar, struct archive *aw)
581 r = archive_read_data_block(ar, &buff, &size, &offset);
582 if (r == ARCHIVE_EOF)
586 errmsg(archive_error_string(ar));
589 r = archive_write_data_block(aw, buff, size, offset);
592 errmsg(archive_error_string(ar));
598 static void msg(const char *m)
600 log_printf("Open Archive - libarchive error: %s \n", m);
603 static void errmsg(const char *m)
607 m = "Error: No error description provided.\n";
609 log_printf("Open Archive - libarchive error: %s \n", m);
612 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */