Deduplicate cell_renderer_height_override()
[geeqie.git] / src / misc.cc
1 /*
2  * Copyright (C) 2008 - 2016 The Geeqie Team
3  *
4  * Authors: Vladimir Nadvornik, Laurent Monin
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 #include "misc.h"
22
23 #include <sys/stat.h>
24 #include <unistd.h>
25
26 #include <cstdio>
27 #include <cstdlib>
28 #include <cstring>
29 #include <memory>
30
31 #include <langinfo.h>
32 #include <pwd.h>
33
34 #include <config.h>
35
36 #include "debug.h"
37 #include "main.h"
38 #include "options.h"
39 #include "ui-fileops.h"
40
41 namespace
42 {
43
44 constexpr int BUFSIZE = 128;
45
46 constexpr gint CELL_HEIGHT_OVERRIDE = 512;
47
48 } // namespace
49
50 gdouble get_zoom_increment()
51 {
52         return ((options->image.zoom_increment != 0) ? static_cast<gdouble>(options->image.zoom_increment) / 100.0 : 1.0);
53 }
54
55 gchar *utf8_validate_or_convert(const gchar *text)
56 {
57         gint len;
58
59         if (!text) return nullptr;
60
61         len = strlen(text);
62         if (!g_utf8_validate(text, len, nullptr))
63                 return g_convert(text, len, "UTF-8", "ISO-8859-1", nullptr, nullptr, nullptr);
64
65         return g_strdup(text);
66 }
67
68 gint utf8_compare(const gchar *s1, const gchar *s2, gboolean case_sensitive)
69 {
70         gchar *s1_key;
71         gchar *s2_key;
72         gchar *s1_t;
73         gchar *s2_t;
74         gint ret;
75
76         g_assert(g_utf8_validate(s1, -1, nullptr));
77         g_assert(g_utf8_validate(s2, -1, nullptr));
78
79         if (!case_sensitive)
80                 {
81                 s1_t = g_utf8_casefold(s1, -1);
82                 s2_t = g_utf8_casefold(s2, -1);
83                 }
84         else
85                 {
86                 s1_t = const_cast<gchar *>(s1);
87                 s2_t = const_cast<gchar *>(s2);
88                 }
89
90         s1_key = g_utf8_collate_key(s1_t, -1);
91         s2_key = g_utf8_collate_key(s2_t, -1);
92
93         ret = strcmp(s1_key, s2_key);
94
95         g_free(s1_key);
96         g_free(s2_key);
97
98         if (!case_sensitive)
99                 {
100                 g_free(s1_t);
101                 g_free(s2_t);
102                 }
103
104         return ret;
105 }
106
107 /* Borrowed from gtkfilesystemunix.c */
108 gchar *expand_tilde(const gchar *filename)
109 {
110 #ifndef G_OS_UNIX
111         return g_strdup(filename);
112 #else
113         const gchar *notilde;
114         const gchar *slash;
115         const gchar *home;
116
117         if (filename[0] != '~')
118                 return g_strdup(filename);
119
120         notilde = filename + 1;
121         slash = strchr(notilde, G_DIR_SEPARATOR);
122         if (slash == notilde || !*notilde)
123                 {
124                 home = g_get_home_dir();
125                 if (!home)
126                         return g_strdup(filename);
127                 }
128         else
129                 {
130                 gchar *username;
131                 struct passwd *passwd;
132
133                 if (slash)
134                         username = g_strndup(notilde, slash - notilde);
135                 else
136                         username = g_strdup(notilde);
137
138                 passwd = getpwnam(username);
139                 g_free(username);
140
141                 if (!passwd)
142                         return g_strdup(filename);
143
144                 home = passwd->pw_dir;
145                 }
146
147         if (slash)
148                 return g_build_filename(home, G_DIR_SEPARATOR_S, slash + 1, NULL);
149
150         return g_build_filename(home, G_DIR_SEPARATOR_S, NULL);
151 #endif
152 }
153
154 /* Search for latitude/longitude parameters in a string
155  */
156
157 #define GEOCODE_NAME "geocode-parameters.awk"
158
159 gchar *decode_geo_script(const gchar *path_dir, const gchar *input_text)
160 {
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);
164
165         if (g_file_test(path, G_FILE_TEST_EXISTS))
166                 {
167                 gchar buf[BUFSIZE];
168                 FILE *fp;
169
170                 if ((fp = popen(cmd, "r")) == nullptr)
171                         {
172                         message.reset(g_strconcat("Error: opening pipe\n", input_text, NULL));
173                         }
174                 else
175                         {
176                         while (fgets(buf, BUFSIZE, fp))
177                                 {
178                                 DEBUG_1("Output: %s", buf);
179                                 }
180
181                         message.reset(g_strconcat(buf, NULL));
182
183                         if(pclose(fp))
184                                 {
185                                 message.reset(g_strconcat("Error: Command not found or exited with error status\n", input_text, NULL));
186                                 }
187                         }
188                 }
189         else
190                 {
191                 message.reset(g_strconcat(input_text, NULL));
192                 }
193
194         g_free(path);
195         g_free(cmd);
196         return message.release();
197 }
198
199 gchar *decode_geo_parameters(const gchar *input_text)
200 {
201         gchar *message;
202         gchar *dir;
203
204         message = decode_geo_script(gq_bindir, input_text);
205         if (strstr(message, "Error"))
206                 {
207                 g_free(message);
208                 dir = g_build_filename(get_rc_dir(), "applications", NULL);
209                 message = decode_geo_script(dir, input_text);
210                 g_free(dir);
211                 }
212
213         return message;
214 }
215
216 /* Run a command like system() but may output debug messages. */
217 int runcmd(const gchar *cmd)
218 {
219 #if 1
220         return system(cmd);
221         return 0;
222 #else
223         /* For debugging purposes */
224         int retval = -1;
225         FILE *in;
226
227         DEBUG_1("Running command: %s", cmd);
228
229         in = popen(cmd, "r");
230         if (in)
231                 {
232                 int status;
233                 const gchar *msg;
234                 gchar buf[2048];
235
236                 while (fgets(buf, sizeof(buf), in) != NULL )
237                         {
238                         DEBUG_1("Output: %s", buf);
239                         }
240
241                 status = pclose(in);
242
243                 if (WIFEXITED(status))
244                         {
245                         msg = "Command terminated with exit code";
246                         retval = WEXITSTATUS(status);
247                         }
248                 else if (WIFSIGNALED(status))
249                         {
250                         msg = "Command was killed by signal";
251                         retval = WTERMSIG(status);
252                         }
253                 else
254                         {
255                         msg = "pclose() returned";
256                         retval = status;
257                         }
258
259                 DEBUG_1("%s : %d\n", msg, retval);
260         }
261
262         return retval;
263 #endif
264 }
265
266 /**
267  * @brief Returns integer representing first_day_of_week
268  * @returns Integer in range 1 to 7
269  * 
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.
274  * 
275  * Sunday == 1
276  */
277 gint date_get_first_day_of_week()
278 {
279 #if HAVE__NL_TIME_FIRST_WEEKDAY
280         return nl_langinfo(_NL_TIME_FIRST_WEEKDAY)[0];
281 #else
282         gchar *dot;
283         gchar *current_locale;
284
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))
288                 {
289                 return 1;
290                 }
291         else
292                 {
293                 return 2;
294                 }
295 #endif
296 }
297
298 /**
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
302  * 
303  *  Uses current locale to get day name
304  * 
305  * Sunday == 1
306  * Result must be freed
307  */
308 gchar *date_get_abbreviated_day_name(gint day)
309 {
310         gchar *abday = nullptr;
311
312         switch (day)
313                 {
314                 case 1:
315                 abday = g_strdup(nl_langinfo(ABDAY_1));
316                 break;
317                 case 2:
318                 abday = g_strdup(nl_langinfo(ABDAY_2));
319                 break;
320                 case 3:
321                 abday = g_strdup(nl_langinfo(ABDAY_3));
322                 break;
323                 case 4:
324                 abday = g_strdup(nl_langinfo(ABDAY_4));
325                 break;
326                 case 5:
327                 abday = g_strdup(nl_langinfo(ABDAY_5));
328                 break;
329                 case 6:
330                 abday = g_strdup(nl_langinfo(ABDAY_6));
331                 break;
332                 case 7:
333                 abday = g_strdup(nl_langinfo(ABDAY_7));
334                 break;
335                 }
336
337         return abday;
338 }
339
340 gchar *convert_rating_to_stars(gint rating)
341 {
342         GString *str = g_string_new(nullptr);
343
344         if (rating == -1)
345                 {
346                 str = g_string_append_unichar(str, options->star_rating.rejected);
347                 return g_string_free(str, FALSE);
348                 }
349
350         if (rating > 0 && rating < 6)
351                 {
352                 for (; rating > 0; --rating)
353                         {
354                         str = g_string_append_unichar(str, options->star_rating.star);
355                         }
356                 return g_string_free(str, FALSE);
357                 }
358
359         return g_strdup("");
360 }
361
362 gchar *get_symbolic_link(const gchar *path_utf8)
363 {
364         gchar *sl;
365         struct stat st;
366         gchar *ret = g_strdup("");
367
368         sl = path_from_utf8(path_utf8);
369
370         if (lstat(sl, &st) == 0 && S_ISLNK(st.st_mode))
371                 {
372                 gchar *buf;
373                 gint l;
374
375                 buf = static_cast<gchar *>(g_malloc(st.st_size + 1));
376                 l = readlink(sl, buf, st.st_size);
377
378                 if (l == st.st_size)
379                         {
380                         buf[l] = '\0';
381
382                         ret = buf;
383                         }
384                 else
385                         {
386                         g_free(buf);
387                         }
388                 }
389
390         g_free(sl);
391
392         return ret;
393 }
394
395 gint get_cpu_cores()
396 {
397     return sysconf(_SC_NPROCESSORS_ONLN);
398 }
399
400 #if HAVE_GTK4
401 void convert_gdkcolor_to_gdkrgba(gpointer data, GdkRGBA *gdk_rgba)
402 {
403 /* @FIXME GTK4 stub */
404 }
405 #else
406 void convert_gdkcolor_to_gdkrgba(gpointer data, GdkRGBA *gdk_rgba)
407 {
408         auto gdk_color = static_cast<GdkColor *>(data);
409
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;
414 }
415 #endif
416
417 void gq_gtk_entry_set_text(GtkEntry *entry, const gchar *text)
418 {
419         GtkEntryBuffer *buffer;
420
421         buffer = gtk_entry_get_buffer(entry);
422         gtk_entry_buffer_set_text(buffer, text, static_cast<gint>(g_utf8_strlen(text, -1)));
423 }
424
425 const gchar *gq_gtk_entry_get_text(GtkEntry *entry)
426 {
427         GtkEntryBuffer *buffer;
428
429         buffer = gtk_entry_get_buffer(entry);
430         return gtk_entry_buffer_get_text(buffer);
431 }
432
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)
434 {
435         gtk_grid_attach(grid, child, left_attach, top_attach, right_attach - left_attach, bottom_attach - top_attach);
436 }
437
438 void gq_gtk_grid_attach_default(GtkGrid *grid, GtkWidget *child, guint left_attach, guint right_attach, guint top_attach, guint bottom_attach )
439 {
440         gtk_grid_attach(grid, child, left_attach, top_attach, right_attach - left_attach, bottom_attach - top_attach);
441 }
442
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)
445 {
446         GParamSpec *spec;
447
448         spec = g_object_class_find_property(G_OBJECT_GET_CLASS(G_OBJECT(renderer)), "height");
449         if (spec && G_IS_PARAM_SPEC_INT(spec))
450                 {
451                 GParamSpecInt *spec_int;
452
453                 spec_int = G_PARAM_SPEC_INT(spec);
454                 if (spec_int->maximum < CELL_HEIGHT_OVERRIDE) spec_int->maximum = CELL_HEIGHT_OVERRIDE;
455                 }
456 }
457
458 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */