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 #if !HAVE__NL_TIME_FIRST_WEEKDAY
42 #include "ui-fileops.h"
47 constexpr int BUFSIZE = 128;
49 constexpr gint CELL_HEIGHT_OVERRIDE = 512;
53 gdouble get_zoom_increment()
55 return ((options->image.zoom_increment != 0) ? static_cast<gdouble>(options->image.zoom_increment) / 100.0 : 1.0);
58 gchar *utf8_validate_or_convert(const gchar *text)
62 if (!text) return nullptr;
65 if (!g_utf8_validate(text, len, nullptr))
66 return g_convert(text, len, "UTF-8", "ISO-8859-1", nullptr, nullptr, nullptr);
68 return g_strdup(text);
71 gint utf8_compare(const gchar *s1, const gchar *s2, gboolean case_sensitive)
79 g_assert(g_utf8_validate(s1, -1, nullptr));
80 g_assert(g_utf8_validate(s2, -1, nullptr));
84 s1_t = g_utf8_casefold(s1, -1);
85 s2_t = g_utf8_casefold(s2, -1);
89 s1_t = const_cast<gchar *>(s1);
90 s2_t = const_cast<gchar *>(s2);
93 s1_key = g_utf8_collate_key(s1_t, -1);
94 s2_key = g_utf8_collate_key(s2_t, -1);
96 ret = strcmp(s1_key, s2_key);
110 /* Borrowed from gtkfilesystemunix.c */
111 gchar *expand_tilde(const gchar *filename)
114 return g_strdup(filename);
116 const gchar *notilde;
120 if (filename[0] != '~')
121 return g_strdup(filename);
123 notilde = filename + 1;
124 slash = strchr(notilde, G_DIR_SEPARATOR);
125 if (slash == notilde || !*notilde)
127 home = g_get_home_dir();
129 return g_strdup(filename);
134 struct passwd *passwd;
137 username = g_strndup(notilde, slash - notilde);
139 username = g_strdup(notilde);
141 passwd = getpwnam(username);
145 return g_strdup(filename);
147 home = passwd->pw_dir;
151 return g_build_filename(home, G_DIR_SEPARATOR_S, slash + 1, NULL);
153 return g_build_filename(home, G_DIR_SEPARATOR_S, NULL);
157 /* Search for latitude/longitude parameters in a string
160 #define GEOCODE_NAME "geocode-parameters.awk"
162 gchar *decode_geo_script(const gchar *path_dir, const gchar *input_text)
164 std::unique_ptr<gchar, decltype(&g_free)> message{nullptr, g_free};
165 gchar *path = g_build_filename(path_dir, GEOCODE_NAME, NULL);
166 gchar *cmd = g_strconcat("echo \'", input_text, "\' | awk -f ", path, NULL);
168 if (g_file_test(path, G_FILE_TEST_EXISTS))
173 if ((fp = popen(cmd, "r")) == nullptr)
175 message.reset(g_strconcat("Error: opening pipe\n", input_text, NULL));
179 while (fgets(buf, BUFSIZE, fp))
181 DEBUG_1("Output: %s", buf);
184 message.reset(g_strconcat(buf, NULL));
188 message.reset(g_strconcat("Error: Command not found or exited with error status\n", input_text, NULL));
194 message.reset(g_strconcat(input_text, NULL));
199 return message.release();
202 gchar *decode_geo_parameters(const gchar *input_text)
207 message = decode_geo_script(gq_bindir, input_text);
208 if (strstr(message, "Error"))
211 dir = g_build_filename(get_rc_dir(), "applications", NULL);
212 message = decode_geo_script(dir, input_text);
219 /* Run a command like system() but may output debug messages. */
220 int runcmd(const gchar *cmd)
226 /* For debugging purposes */
230 DEBUG_1("Running command: %s", cmd);
232 in = popen(cmd, "r");
239 while (fgets(buf, sizeof(buf), in) != NULL )
241 DEBUG_1("Output: %s", buf);
246 if (WIFEXITED(status))
248 msg = "Command terminated with exit code";
249 retval = WEXITSTATUS(status);
251 else if (WIFSIGNALED(status))
253 msg = "Command was killed by signal";
254 retval = WTERMSIG(status);
258 msg = "pclose() returned";
262 DEBUG_1("%s : %d\n", msg, retval);
270 * @brief Returns integer representing first_day_of_week
271 * @returns Integer in range 1 to 7
273 * Uses current locale to get first day of week.
274 * If _NL_TIME_FIRST_WEEKDAY is not available, ISO 8601
275 * states first day of week is Monday.
276 * USA, Mexico and Canada (and others) use Sunday as first day of week.
280 gint date_get_first_day_of_week()
282 #if HAVE__NL_TIME_FIRST_WEEKDAY
283 return nl_langinfo(_NL_TIME_FIRST_WEEKDAY)[0];
286 gchar *current_locale;
288 current_locale = setlocale(LC_ALL, NULL);
289 dot = strstr(current_locale, ".");
290 if ((strncmp(dot - 2, "US", 2) == 0) || (strncmp(dot - 2, "MX", 2) == 0) || (strncmp(dot - 2, "CA", 2) == 0))
302 * @brief Get an abbreviated day name from locale
303 * @param day Integer in range 1 to 7, representing day of week
304 * @returns String containing abbreviated day name
306 * Uses current locale to get day name
309 * Result must be freed
311 gchar *date_get_abbreviated_day_name(gint day)
313 gchar *abday = nullptr;
318 abday = g_strdup(nl_langinfo(ABDAY_1));
321 abday = g_strdup(nl_langinfo(ABDAY_2));
324 abday = g_strdup(nl_langinfo(ABDAY_3));
327 abday = g_strdup(nl_langinfo(ABDAY_4));
330 abday = g_strdup(nl_langinfo(ABDAY_5));
333 abday = g_strdup(nl_langinfo(ABDAY_6));
336 abday = g_strdup(nl_langinfo(ABDAY_7));
343 gchar *convert_rating_to_stars(gint rating)
345 GString *str = g_string_new(nullptr);
349 str = g_string_append_unichar(str, options->star_rating.rejected);
350 return g_string_free(str, FALSE);
353 if (rating > 0 && rating < 6)
355 for (; rating > 0; --rating)
357 str = g_string_append_unichar(str, options->star_rating.star);
359 return g_string_free(str, FALSE);
365 gchar *get_symbolic_link(const gchar *path_utf8)
369 gchar *ret = g_strdup("");
371 sl = path_from_utf8(path_utf8);
373 if (lstat(sl, &st) == 0 && S_ISLNK(st.st_mode))
378 buf = static_cast<gchar *>(g_malloc(st.st_size + 1));
379 l = readlink(sl, buf, st.st_size);
400 return sysconf(_SC_NPROCESSORS_ONLN);
404 void convert_gdkcolor_to_gdkrgba(gpointer data, GdkRGBA *gdk_rgba)
406 /* @FIXME GTK4 stub */
409 void convert_gdkcolor_to_gdkrgba(gpointer data, GdkRGBA *gdk_rgba)
411 auto gdk_color = static_cast<GdkColor *>(data);
413 gdk_rgba->red = CLAMP((double)gdk_color->red / 65535.0, 0.0, 1.0);
414 gdk_rgba->green = CLAMP((double)gdk_color->green / 65535.0, 0.0, 1.0);
415 gdk_rgba->blue = CLAMP((double)gdk_color->blue / 65535.0, 0.0, 1.0);
416 gdk_rgba->alpha = 1.0;
420 void gq_gtk_entry_set_text(GtkEntry *entry, const gchar *text)
422 GtkEntryBuffer *buffer;
424 buffer = gtk_entry_get_buffer(entry);
425 gtk_entry_buffer_set_text(buffer, text, static_cast<gint>(g_utf8_strlen(text, -1)));
428 const gchar *gq_gtk_entry_get_text(GtkEntry *entry)
430 GtkEntryBuffer *buffer;
432 buffer = gtk_entry_get_buffer(entry);
433 return gtk_entry_buffer_get_text(buffer);
436 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)
438 gtk_grid_attach(grid, child, left_attach, top_attach, right_attach - left_attach, bottom_attach - top_attach);
441 void gq_gtk_grid_attach_default(GtkGrid *grid, GtkWidget *child, guint left_attach, guint right_attach, guint top_attach, guint bottom_attach )
443 gtk_grid_attach(grid, child, left_attach, top_attach, right_attach - left_attach, bottom_attach - top_attach);
446 /* this overrides the low default of a GtkCellRenderer from 100 to CELL_HEIGHT_OVERRIDE, something sane for our purposes */
447 void cell_renderer_height_override(GtkCellRenderer *renderer)
451 spec = g_object_class_find_property(G_OBJECT_GET_CLASS(G_OBJECT(renderer)), "height");
452 if (spec && G_IS_PARAM_SPEC_INT(spec))
454 GParamSpecInt *spec_int;
456 spec_int = G_PARAM_SPEC_INT(spec);
457 if (spec_int->maximum < CELL_HEIGHT_OVERRIDE) spec_int->maximum = CELL_HEIGHT_OVERRIDE;
461 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */