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.
39 #include "ui-fileops.h"
41 gdouble get_zoom_increment()
43 return ((options->image.zoom_increment != 0) ? static_cast<gdouble>(options->image.zoom_increment) / 100.0 : 1.0);
46 gchar *utf8_validate_or_convert(const gchar *text)
50 if (!text) return nullptr;
53 if (!g_utf8_validate(text, len, nullptr))
54 return g_convert(text, len, "UTF-8", "ISO-8859-1", nullptr, nullptr, nullptr);
56 return g_strdup(text);
59 gint utf8_compare(const gchar *s1, const gchar *s2, gboolean case_sensitive)
67 g_assert(g_utf8_validate(s1, -1, nullptr));
68 g_assert(g_utf8_validate(s2, -1, nullptr));
72 s1_t = g_utf8_casefold(s1, -1);
73 s2_t = g_utf8_casefold(s2, -1);
77 s1_t = const_cast<gchar *>(s1);
78 s2_t = const_cast<gchar *>(s2);
81 s1_key = g_utf8_collate_key(s1_t, -1);
82 s2_key = g_utf8_collate_key(s2_t, -1);
84 ret = strcmp(s1_key, s2_key);
98 /* Borrowed from gtkfilesystemunix.c */
99 gchar *expand_tilde(const gchar *filename)
102 return g_strdup(filename);
104 const gchar *notilde;
108 if (filename[0] != '~')
109 return g_strdup(filename);
111 notilde = filename + 1;
112 slash = strchr(notilde, G_DIR_SEPARATOR);
113 if (slash == notilde || !*notilde)
115 home = g_get_home_dir();
117 return g_strdup(filename);
122 struct passwd *passwd;
125 username = g_strndup(notilde, slash - notilde);
127 username = g_strdup(notilde);
129 passwd = getpwnam(username);
133 return g_strdup(filename);
135 home = passwd->pw_dir;
139 return g_build_filename(home, G_DIR_SEPARATOR_S, slash + 1, NULL);
141 return g_build_filename(home, G_DIR_SEPARATOR_S, NULL);
145 /* Search for latitude/longitude parameters in a string
148 #define GEOCODE_NAME "geocode-parameters.awk"
153 gchar *decode_geo_script(const gchar *path_dir, const gchar *input_text)
155 std::unique_ptr<gchar, decltype(&g_free)> message{nullptr, g_free};
156 gchar *path = g_build_filename(path_dir, GEOCODE_NAME, NULL);
157 gchar *cmd = g_strconcat("echo \'", input_text, "\' | awk -f ", path, NULL);
159 if (g_file_test(path, G_FILE_TEST_EXISTS))
164 if ((fp = popen(cmd, "r")) == nullptr)
166 message.reset(g_strconcat("Error: opening pipe\n", input_text, NULL));
170 while (fgets(buf, BUFSIZE, fp))
172 DEBUG_1("Output: %s", buf);
175 message.reset(g_strconcat(buf, NULL));
179 message.reset(g_strconcat("Error: Command not found or exited with error status\n", input_text, NULL));
185 message.reset(g_strconcat(input_text, NULL));
190 return message.release();
193 gchar *decode_geo_parameters(const gchar *input_text)
198 message = decode_geo_script(gq_bindir, input_text);
199 if (strstr(message, "Error"))
202 dir = g_build_filename(get_rc_dir(), "applications", NULL);
203 message = decode_geo_script(dir, input_text);
210 /* Run a command like system() but may output debug messages. */
211 int runcmd(const gchar *cmd)
217 /* For debugging purposes */
221 DEBUG_1("Running command: %s", cmd);
223 in = popen(cmd, "r");
230 while (fgets(buf, sizeof(buf), in) != NULL )
232 DEBUG_1("Output: %s", buf);
237 if (WIFEXITED(status))
239 msg = "Command terminated with exit code";
240 retval = WEXITSTATUS(status);
242 else if (WIFSIGNALED(status))
244 msg = "Command was killed by signal";
245 retval = WTERMSIG(status);
249 msg = "pclose() returned";
253 DEBUG_1("%s : %d\n", msg, retval);
261 * @brief Returns integer representing first_day_of_week
262 * @returns Integer in range 1 to 7
264 * Uses current locale to get first day of week.
265 * If _NL_TIME_FIRST_WEEKDAY is not available, ISO 8601
266 * states first day of week is Monday.
267 * USA, Mexico and Canada (and others) use Sunday as first day of week.
271 gint date_get_first_day_of_week()
273 #if HAVE__NL_TIME_FIRST_WEEKDAY
274 return nl_langinfo(_NL_TIME_FIRST_WEEKDAY)[0];
277 gchar *current_locale;
279 current_locale = setlocale(LC_ALL, NULL);
280 dot = strstr(current_locale, ".");
281 if ((strncmp(dot - 2, "US", 2) == 0) || (strncmp(dot - 2, "MX", 2) == 0) || (strncmp(dot - 2, "CA", 2) == 0))
293 * @brief Get an abbreviated day name from locale
294 * @param day Integer in range 1 to 7, representing day of week
295 * @returns String containing abbreviated day name
297 * Uses current locale to get day name
300 * Result must be freed
302 gchar *date_get_abbreviated_day_name(gint day)
304 gchar *abday = nullptr;
309 abday = g_strdup(nl_langinfo(ABDAY_1));
312 abday = g_strdup(nl_langinfo(ABDAY_2));
315 abday = g_strdup(nl_langinfo(ABDAY_3));
318 abday = g_strdup(nl_langinfo(ABDAY_4));
321 abday = g_strdup(nl_langinfo(ABDAY_5));
324 abday = g_strdup(nl_langinfo(ABDAY_6));
327 abday = g_strdup(nl_langinfo(ABDAY_7));
334 gchar *convert_rating_to_stars(gint rating)
336 GString *str = g_string_new(nullptr);
340 str = g_string_append_unichar(str, options->star_rating.rejected);
341 return g_string_free(str, FALSE);
344 if (rating > 0 && rating < 6)
346 for (; rating > 0; --rating)
348 str = g_string_append_unichar(str, options->star_rating.star);
350 return g_string_free(str, FALSE);
356 gchar *get_symbolic_link(const gchar *path_utf8)
360 gchar *ret = g_strdup("");
362 sl = path_from_utf8(path_utf8);
364 if (lstat(sl, &st) == 0 && S_ISLNK(st.st_mode))
369 buf = static_cast<gchar *>(g_malloc(st.st_size + 1));
370 l = readlink(sl, buf, st.st_size);
391 return sysconf(_SC_NPROCESSORS_ONLN);
395 void convert_gdkcolor_to_gdkrgba(gpointer data, GdkRGBA *gdk_rgba)
397 /* @FIXME GTK4 stub */
400 void convert_gdkcolor_to_gdkrgba(gpointer data, GdkRGBA *gdk_rgba)
402 auto gdk_color = static_cast<GdkColor *>(data);
404 gdk_rgba->red = CLAMP((double)gdk_color->red / 65535.0, 0.0, 1.0);
405 gdk_rgba->green = CLAMP((double)gdk_color->green / 65535.0, 0.0, 1.0);
406 gdk_rgba->blue = CLAMP((double)gdk_color->blue / 65535.0, 0.0, 1.0);
407 gdk_rgba->alpha = 1.0;
411 void gq_gtk_entry_set_text(GtkEntry *entry, const gchar *text)
413 GtkEntryBuffer *buffer;
415 buffer = gtk_entry_get_buffer(entry);
416 gtk_entry_buffer_set_text(buffer, text, static_cast<gint>(g_utf8_strlen(text, -1)));
419 const gchar *gq_gtk_entry_get_text(GtkEntry *entry)
421 GtkEntryBuffer *buffer;
423 buffer = gtk_entry_get_buffer(entry);
424 return gtk_entry_buffer_get_text(buffer);
427 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)
429 gtk_grid_attach(grid, child, left_attach, top_attach, right_attach - left_attach, bottom_attach - top_attach);
432 void gq_gtk_grid_attach_default(GtkGrid *grid, GtkWidget *child, guint left_attach, guint right_attach, guint top_attach, guint bottom_attach )
434 gtk_grid_attach(grid, child, left_attach, top_attach, right_attach - left_attach, bottom_attach - top_attach);
437 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */