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"
44 constexpr int BUFSIZE = 128;
46 constexpr gint CELL_HEIGHT_OVERRIDE = 512;
50 gdouble get_zoom_increment()
52 return ((options->image.zoom_increment != 0) ? static_cast<gdouble>(options->image.zoom_increment) / 100.0 : 1.0);
55 gchar *utf8_validate_or_convert(const gchar *text)
59 if (!text) return nullptr;
62 if (!g_utf8_validate(text, len, nullptr))
63 return g_convert(text, len, "UTF-8", "ISO-8859-1", nullptr, nullptr, nullptr);
65 return g_strdup(text);
68 gint utf8_compare(const gchar *s1, const gchar *s2, gboolean case_sensitive)
76 g_assert(g_utf8_validate(s1, -1, nullptr));
77 g_assert(g_utf8_validate(s2, -1, nullptr));
81 s1_t = g_utf8_casefold(s1, -1);
82 s2_t = g_utf8_casefold(s2, -1);
86 s1_t = const_cast<gchar *>(s1);
87 s2_t = const_cast<gchar *>(s2);
90 s1_key = g_utf8_collate_key(s1_t, -1);
91 s2_key = g_utf8_collate_key(s2_t, -1);
93 ret = strcmp(s1_key, s2_key);
107 /* Borrowed from gtkfilesystemunix.c */
108 gchar *expand_tilde(const gchar *filename)
111 return g_strdup(filename);
113 const gchar *notilde;
117 if (filename[0] != '~')
118 return g_strdup(filename);
120 notilde = filename + 1;
121 slash = strchr(notilde, G_DIR_SEPARATOR);
122 if (slash == notilde || !*notilde)
124 home = g_get_home_dir();
126 return g_strdup(filename);
131 struct passwd *passwd;
134 username = g_strndup(notilde, slash - notilde);
136 username = g_strdup(notilde);
138 passwd = getpwnam(username);
142 return g_strdup(filename);
144 home = passwd->pw_dir;
148 return g_build_filename(home, G_DIR_SEPARATOR_S, slash + 1, NULL);
150 return g_build_filename(home, G_DIR_SEPARATOR_S, NULL);
154 /* Search for latitude/longitude parameters in a string
157 #define GEOCODE_NAME "geocode-parameters.awk"
159 gchar *decode_geo_script(const gchar *path_dir, const gchar *input_text)
161 std::unique_ptr<gchar, decltype(&g_free)> message{nullptr, g_free};
162 gchar *path = g_build_filename(path_dir, GEOCODE_NAME, NULL);
163 gchar *cmd = g_strconcat("echo \'", input_text, "\' | awk -f ", path, NULL);
165 if (g_file_test(path, G_FILE_TEST_EXISTS))
170 if ((fp = popen(cmd, "r")) == nullptr)
172 message.reset(g_strconcat("Error: opening pipe\n", input_text, NULL));
176 while (fgets(buf, BUFSIZE, fp))
178 DEBUG_1("Output: %s", buf);
181 message.reset(g_strconcat(buf, NULL));
185 message.reset(g_strconcat("Error: Command not found or exited with error status\n", input_text, NULL));
191 message.reset(g_strconcat(input_text, NULL));
196 return message.release();
199 gchar *decode_geo_parameters(const gchar *input_text)
204 message = decode_geo_script(gq_bindir, input_text);
205 if (strstr(message, "Error"))
208 dir = g_build_filename(get_rc_dir(), "applications", NULL);
209 message = decode_geo_script(dir, input_text);
216 /* Run a command like system() but may output debug messages. */
217 int runcmd(const gchar *cmd)
223 /* For debugging purposes */
227 DEBUG_1("Running command: %s", cmd);
229 in = popen(cmd, "r");
236 while (fgets(buf, sizeof(buf), in) != NULL )
238 DEBUG_1("Output: %s", buf);
243 if (WIFEXITED(status))
245 msg = "Command terminated with exit code";
246 retval = WEXITSTATUS(status);
248 else if (WIFSIGNALED(status))
250 msg = "Command was killed by signal";
251 retval = WTERMSIG(status);
255 msg = "pclose() returned";
259 DEBUG_1("%s : %d\n", msg, retval);
267 * @brief Returns integer representing first_day_of_week
268 * @returns Integer in range 1 to 7
270 * Uses current locale to get first day of week.
271 * If _NL_TIME_FIRST_WEEKDAY is not available, ISO 8601
272 * states first day of week is Monday.
273 * USA, Mexico and Canada (and others) use Sunday as first day of week.
277 gint date_get_first_day_of_week()
279 #if HAVE__NL_TIME_FIRST_WEEKDAY
280 return nl_langinfo(_NL_TIME_FIRST_WEEKDAY)[0];
283 gchar *current_locale;
285 current_locale = setlocale(LC_ALL, NULL);
286 dot = strstr(current_locale, ".");
287 if ((strncmp(dot - 2, "US", 2) == 0) || (strncmp(dot - 2, "MX", 2) == 0) || (strncmp(dot - 2, "CA", 2) == 0))
299 * @brief Get an abbreviated day name from locale
300 * @param day Integer in range 1 to 7, representing day of week
301 * @returns String containing abbreviated day name
303 * Uses current locale to get day name
306 * Result must be freed
308 gchar *date_get_abbreviated_day_name(gint day)
310 gchar *abday = nullptr;
315 abday = g_strdup(nl_langinfo(ABDAY_1));
318 abday = g_strdup(nl_langinfo(ABDAY_2));
321 abday = g_strdup(nl_langinfo(ABDAY_3));
324 abday = g_strdup(nl_langinfo(ABDAY_4));
327 abday = g_strdup(nl_langinfo(ABDAY_5));
330 abday = g_strdup(nl_langinfo(ABDAY_6));
333 abday = g_strdup(nl_langinfo(ABDAY_7));
340 gchar *convert_rating_to_stars(gint rating)
342 GString *str = g_string_new(nullptr);
346 str = g_string_append_unichar(str, options->star_rating.rejected);
347 return g_string_free(str, FALSE);
350 if (rating > 0 && rating < 6)
352 for (; rating > 0; --rating)
354 str = g_string_append_unichar(str, options->star_rating.star);
356 return g_string_free(str, FALSE);
362 gchar *get_symbolic_link(const gchar *path_utf8)
366 gchar *ret = g_strdup("");
368 sl = path_from_utf8(path_utf8);
370 if (lstat(sl, &st) == 0 && S_ISLNK(st.st_mode))
375 buf = static_cast<gchar *>(g_malloc(st.st_size + 1));
376 l = readlink(sl, buf, st.st_size);
397 return sysconf(_SC_NPROCESSORS_ONLN);
401 void convert_gdkcolor_to_gdkrgba(gpointer data, GdkRGBA *gdk_rgba)
403 /* @FIXME GTK4 stub */
406 void convert_gdkcolor_to_gdkrgba(gpointer data, GdkRGBA *gdk_rgba)
408 auto gdk_color = static_cast<GdkColor *>(data);
410 gdk_rgba->red = CLAMP((double)gdk_color->red / 65535.0, 0.0, 1.0);
411 gdk_rgba->green = CLAMP((double)gdk_color->green / 65535.0, 0.0, 1.0);
412 gdk_rgba->blue = CLAMP((double)gdk_color->blue / 65535.0, 0.0, 1.0);
413 gdk_rgba->alpha = 1.0;
417 void gq_gtk_entry_set_text(GtkEntry *entry, const gchar *text)
419 GtkEntryBuffer *buffer;
421 buffer = gtk_entry_get_buffer(entry);
422 gtk_entry_buffer_set_text(buffer, text, static_cast<gint>(g_utf8_strlen(text, -1)));
425 const gchar *gq_gtk_entry_get_text(GtkEntry *entry)
427 GtkEntryBuffer *buffer;
429 buffer = gtk_entry_get_buffer(entry);
430 return gtk_entry_buffer_get_text(buffer);
433 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)
435 gtk_grid_attach(grid, child, left_attach, top_attach, right_attach - left_attach, bottom_attach - top_attach);
438 void gq_gtk_grid_attach_default(GtkGrid *grid, GtkWidget *child, guint left_attach, guint right_attach, guint top_attach, guint bottom_attach )
440 gtk_grid_attach(grid, child, left_attach, top_attach, right_attach - left_attach, bottom_attach - top_attach);
443 /* this overrides the low default of a GtkCellRenderer from 100 to CELL_HEIGHT_OVERRIDE, something sane for our purposes */
444 void cell_renderer_height_override(GtkCellRenderer *renderer)
448 spec = g_object_class_find_property(G_OBJECT_GET_CLASS(G_OBJECT(renderer)), "height");
449 if (spec && G_IS_PARAM_SPEC_INT(spec))
451 GParamSpecInt *spec_int;
453 spec_int = G_PARAM_SPEC_INT(spec);
454 if (spec_int->maximum < CELL_HEIGHT_OVERRIDE) spec_int->maximum = CELL_HEIGHT_OVERRIDE;
458 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */