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