clang-tidy: modernize-macro-to-enum
[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 <clocale>
22 #include <memory>
23
24 #include "main.h"
25 #include "misc.h"
26
27 #include "filedata.h"
28 #include "ui-fileops.h"
29
30 #include <langinfo.h>
31
32 gdouble get_zoom_increment()
33 {
34         return ((options->image.zoom_increment != 0) ? static_cast<gdouble>(options->image.zoom_increment) / 100.0 : 1.0);
35 }
36
37 gchar *utf8_validate_or_convert(const gchar *text)
38 {
39         gint len;
40
41         if (!text) return nullptr;
42
43         len = strlen(text);
44         if (!g_utf8_validate(text, len, nullptr))
45                 return g_convert(text, len, "UTF-8", "ISO-8859-1", nullptr, nullptr, nullptr);
46
47         return g_strdup(text);
48 }
49
50 gint utf8_compare(const gchar *s1, const gchar *s2, gboolean case_sensitive)
51 {
52         gchar *s1_key, *s2_key;
53         gchar *s1_t, *s2_t;
54         gint ret;
55
56         g_assert(g_utf8_validate(s1, -1, nullptr));
57         g_assert(g_utf8_validate(s2, -1, nullptr));
58
59         if (!case_sensitive)
60                 {
61                 s1_t = g_utf8_casefold(s1, -1);
62                 s2_t = g_utf8_casefold(s2, -1);
63                 }
64         else
65                 {
66                 s1_t = const_cast<gchar *>(s1);
67                 s2_t = const_cast<gchar *>(s2);
68                 }
69
70         s1_key = g_utf8_collate_key(s1_t, -1);
71         s2_key = g_utf8_collate_key(s2_t, -1);
72
73         ret = strcmp(s1_key, s2_key);
74
75         g_free(s1_key);
76         g_free(s2_key);
77
78         if (!case_sensitive)
79                 {
80                 g_free(s1_t);
81                 g_free(s2_t);
82                 }
83
84         return ret;
85 }
86
87 /* Borrowed from gtkfilesystemunix.c */
88 gchar *expand_tilde(const gchar *filename)
89 {
90 #ifndef G_OS_UNIX
91         return g_strdup(filename);
92 #else
93         const gchar *notilde;
94         const gchar *slash;
95         const gchar *home;
96
97         if (filename[0] != '~')
98                 return g_strdup(filename);
99
100         notilde = filename + 1;
101         slash = strchr(notilde, G_DIR_SEPARATOR);
102         if (slash == notilde || !*notilde)
103                 {
104                 home = g_get_home_dir();
105                 if (!home)
106                         return g_strdup(filename);
107                 }
108         else
109                 {
110                 gchar *username;
111                 struct passwd *passwd;
112
113                 if (slash)
114                         username = g_strndup(notilde, slash - notilde);
115                 else
116                         username = g_strdup(notilde);
117
118                 passwd = getpwnam(username);
119                 g_free(username);
120
121                 if (!passwd)
122                         return g_strdup(filename);
123
124                 home = passwd->pw_dir;
125                 }
126
127         if (slash)
128                 return g_build_filename(home, G_DIR_SEPARATOR_S, slash + 1, NULL);
129
130         return g_build_filename(home, G_DIR_SEPARATOR_S, NULL);
131 #endif
132 }
133
134 /* Search for latitude/longitude parameters in a string
135  */
136
137 #define GEOCODE_NAME "geocode-parameters.awk"
138 enum {
139         BUFSIZE = 128
140 };
141
142 gchar *decode_geo_script(const gchar *path_dir, const gchar *input_text)
143 {
144         std::unique_ptr<gchar, decltype(&g_free)> message{nullptr, g_free};
145         gchar *path = g_build_filename(path_dir, GEOCODE_NAME, NULL);
146         gchar *cmd = g_strconcat("echo \'", input_text, "\'  | awk -f ", path, NULL);
147
148         if (g_file_test(path, G_FILE_TEST_EXISTS))
149                 {
150                 gchar buf[BUFSIZE];
151                 FILE *fp;
152
153                 if ((fp = popen(cmd, "r")) == nullptr)
154                         {
155                         message.reset(g_strconcat("Error: opening pipe\n", input_text, NULL));
156                         }
157                 else
158                         {
159                         while (fgets(buf, BUFSIZE, fp))
160                                 {
161                                 DEBUG_1("Output: %s", buf);
162                                 }
163
164                         message.reset(g_strconcat(buf, NULL));
165
166                         if(pclose(fp))
167                                 {
168                                 message.reset(g_strconcat("Error: Command not found or exited with error status\n", input_text, NULL));
169                                 }
170                         }
171                 }
172         else
173                 {
174                 message.reset(g_strconcat(input_text, NULL));
175                 }
176
177         g_free(path);
178         g_free(cmd);
179         return message.release();
180 }
181
182 gchar *decode_geo_parameters(const gchar *input_text)
183 {
184         gchar *message;
185         gchar *dir;
186
187         message = decode_geo_script(gq_bindir, input_text);
188         if (strstr(message, "Error"))
189                 {
190                 g_free(message);
191                 dir = g_build_filename(get_rc_dir(), "applications", NULL);
192                 message = decode_geo_script(dir, input_text);
193                 g_free(dir);
194                 }
195
196         return message;
197 }
198
199 /* Run a command like system() but may output debug messages. */
200 int runcmd(const gchar *cmd)
201 {
202 #if 1
203         return system(cmd);
204         return 0;
205 #else
206         /* For debugging purposes */
207         int retval = -1;
208         FILE *in;
209
210         DEBUG_1("Running command: %s", cmd);
211
212         in = popen(cmd, "r");
213         if (in)
214                 {
215                 int status;
216                 const gchar *msg;
217                 gchar buf[2048];
218
219                 while (fgets(buf, sizeof(buf), in) != NULL )
220                         {
221                         DEBUG_1("Output: %s", buf);
222                         }
223
224                 status = pclose(in);
225
226                 if (WIFEXITED(status))
227                         {
228                         msg = "Command terminated with exit code";
229                         retval = WEXITSTATUS(status);
230                         }
231                 else if (WIFSIGNALED(status))
232                         {
233                         msg = "Command was killed by signal";
234                         retval = WTERMSIG(status);
235                         }
236                 else
237                         {
238                         msg = "pclose() returned";
239                         retval = status;
240                         }
241
242                 DEBUG_1("%s : %d\n", msg, retval);
243         }
244
245         return retval;
246 #endif
247 }
248
249 /**
250  * @brief Returns integer representing first_day_of_week
251  * @returns Integer in range 1 to 7
252  * 
253  * Uses current locale to get first day of week.
254  * If _NL_TIME_FIRST_WEEKDAY is not available, ISO 8601
255  * states first day of week is Monday.
256  * USA, Mexico and Canada (and others) use Sunday as first day of week.
257  * 
258  * Sunday == 1
259  */
260 gint date_get_first_day_of_week()
261 {
262 #ifdef HAVE__NL_TIME_FIRST_WEEKDAY
263         return nl_langinfo(_NL_TIME_FIRST_WEEKDAY)[0];
264 #else
265         gchar *dot;
266         gchar *current_locale;
267
268         current_locale = setlocale(LC_ALL, NULL);
269         dot = strstr(current_locale, ".");
270         if ((strncmp(dot - 2, "US", 2) == 0) || (strncmp(dot - 2, "MX", 2) == 0) || (strncmp(dot - 2, "CA", 2) == 0))
271                 {
272                 return 1;
273                 }
274         else
275                 {
276                 return 2;
277                 }
278 #endif
279 }
280
281 /**
282  * @brief Get an abbreviated day name from locale
283  * @param day Integer in range 1 to 7, representing day of week
284  * @returns String containing abbreviated day name
285  * 
286  *  Uses current locale to get day name
287  * 
288  * Sunday == 1
289  * Result must be freed
290  */
291 gchar *date_get_abbreviated_day_name(gint day)
292 {
293         gchar *abday = nullptr;
294
295         switch (day)
296                 {
297                 case 1:
298                 abday = g_strdup(nl_langinfo(ABDAY_1));
299                 break;
300                 case 2:
301                 abday = g_strdup(nl_langinfo(ABDAY_2));
302                 break;
303                 case 3:
304                 abday = g_strdup(nl_langinfo(ABDAY_3));
305                 break;
306                 case 4:
307                 abday = g_strdup(nl_langinfo(ABDAY_4));
308                 break;
309                 case 5:
310                 abday = g_strdup(nl_langinfo(ABDAY_5));
311                 break;
312                 case 6:
313                 abday = g_strdup(nl_langinfo(ABDAY_6));
314                 break;
315                 case 7:
316                 abday = g_strdup(nl_langinfo(ABDAY_7));
317                 break;
318                 }
319
320         return abday;
321 }
322
323 gchar *convert_rating_to_stars(gint rating)
324 {
325         GString *str = g_string_new(nullptr);
326
327         if (rating == -1)
328                 {
329                 str = g_string_append_unichar(str, options->star_rating.rejected);
330                 return g_string_free(str, FALSE);
331                 }
332
333         if (rating > 0 && rating < 6)
334                 {
335                 for (; rating > 0; --rating)
336                         {
337                         str = g_string_append_unichar(str, options->star_rating.star);
338                         }
339                 return g_string_free(str, FALSE);
340                 }
341
342         return g_strdup("");
343 }
344
345 gchar *get_symbolic_link(const gchar *path_utf8)
346 {
347         gchar *sl;
348         struct stat st;
349         gchar *ret = g_strdup("");
350
351         sl = path_from_utf8(path_utf8);
352
353         if (lstat(sl, &st) == 0 && S_ISLNK(st.st_mode))
354                 {
355                 gchar *buf;
356                 gint l;
357
358                 buf = static_cast<gchar *>(g_malloc(st.st_size + 1));
359                 l = readlink(sl, buf, st.st_size);
360
361                 if (l == st.st_size)
362                         {
363                         buf[l] = '\0';
364
365                         ret = buf;
366                         }
367                 else
368                         {
369                         g_free(buf);
370                         }
371                 }
372
373         g_free(sl);
374
375         return ret;
376 }
377
378 gint get_cpu_cores()
379 {
380     return sysconf(_SC_NPROCESSORS_ONLN);
381 }
382
383 #ifdef HAVE_GTK4
384 void convert_gdkcolor_to_gdkrgba(gpointer data, GdkRGBA *gdk_rgba)
385 {
386 /* @FIXME GTK4 stub */
387 }
388 #else
389 void convert_gdkcolor_to_gdkrgba(gpointer data, GdkRGBA *gdk_rgba)
390 {
391         auto gdk_color = static_cast<GdkColor *>(data);
392
393         gdk_rgba->red = CLAMP((double)gdk_color->red / 65535.0, 0.0, 1.0);
394         gdk_rgba->green = CLAMP((double)gdk_color->green / 65535.0, 0.0, 1.0);
395         gdk_rgba->blue = CLAMP((double)gdk_color->blue / 65535.0, 0.0, 1.0);
396         gdk_rgba->alpha = 1.0;
397 }
398 #endif
399
400 void gq_gtk_entry_set_text(GtkEntry *entry, const gchar *text)
401 {
402         GtkEntryBuffer *buffer;
403
404         buffer = gtk_entry_get_buffer(entry);
405         gtk_entry_buffer_set_text(buffer, text, static_cast<gint>(g_utf8_strlen(text, -1)));
406 }
407
408 const gchar *gq_gtk_entry_get_text(GtkEntry *entry)
409 {
410         GtkEntryBuffer *buffer;
411
412         buffer = gtk_entry_get_buffer(entry);
413         return gtk_entry_buffer_get_text(buffer);
414 }
415
416 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)
417 {
418         gtk_grid_attach(grid, child, left_attach, top_attach, right_attach - left_attach, bottom_attach - top_attach);
419 }
420
421 void gq_gtk_grid_attach_default(GtkGrid *grid, GtkWidget *child, guint left_attach, guint right_attach, guint top_attach, guint bottom_attach )
422 {
423         gtk_grid_attach(grid, child, left_attach, top_attach, right_attach - left_attach, bottom_attach - top_attach);
424 }
425
426 /* Copied from the libarchive .repo. examples */
427
428 #ifndef HAVE_ARCHIVE
429 gchar *open_archive(FileData *)
430 {
431         log_printf("%s", _("Warning: libarchive not installed"));
432         return NULL;
433 }
434
435 #else
436
437 #include <archive.h>
438 #include <archive_entry.h>
439
440 static void errmsg(const char *);
441 static gboolean extract(const char *filename, int do_extract, int flags);
442 static int copy_data(struct archive *, struct archive *);
443 static void msg(const char *);
444 static int verbose = 0;
445
446 gchar *open_archive(FileData *fd)
447 {
448         int flags;
449         gchar *current_dir;
450         gchar *destination_dir;
451         gboolean success;
452         gint error;
453
454         destination_dir = g_build_filename(g_get_tmp_dir(), GQ_ARCHIVE_DIR, instance_identifier, fd->path, NULL);
455
456         if (!recursive_mkdir_if_not_exists(destination_dir, 0755))
457                 {
458                 log_printf("%s%s%s", _("Open Archive - Cannot create directory: "), destination_dir, "\n");
459                 g_free(destination_dir);
460                 return nullptr;
461                 }
462
463         current_dir = g_get_current_dir();
464         error = chdir(destination_dir);
465         if (error)
466                 {
467                 log_printf("%s%s%s%s%s", _("Open Archive - Cannot change directory to: "), destination_dir, _("\n  Error code: "), strerror(errno), "\n");
468                 g_free(destination_dir);
469                 g_free(current_dir);
470                 return nullptr;
471                 }
472
473         flags = ARCHIVE_EXTRACT_TIME;
474         success = extract(fd->path, 1, flags);
475
476         error = chdir(current_dir);
477         if (error)
478                 {
479                 log_printf("%s%s%s%s%s", _("Open Archive - Cannot change directory to: "), current_dir, _("\n  Error code: "), strerror(errno), "\n");
480                 g_free(destination_dir);
481                 g_free(current_dir);
482                 return nullptr;
483                 }
484         g_free(current_dir);
485
486         if (!success)
487                 {
488                 g_free(destination_dir);
489                 destination_dir = nullptr;
490                 }
491
492         return destination_dir;
493 }
494
495 static gboolean extract(const char *filename, int do_extract, int flags)
496 {
497         struct archive *a;
498         struct archive *ext;
499         struct archive_entry *entry;
500         int r;
501
502         a = archive_read_new();
503         ext = archive_write_disk_new();
504         archive_write_disk_set_options(ext, flags);
505         archive_write_disk_set_standard_lookup(ext);
506         archive_read_support_filter_all(a);
507         archive_read_support_format_all(a);
508
509         if (filename != nullptr && strcmp(filename, "-") == 0)
510                 {
511                 filename = nullptr;
512                 }
513         if ((r = archive_read_open_filename(a, filename, 10240)))
514                 {
515                 errmsg(archive_error_string(a));
516                 errmsg("\n");
517                 return(FALSE);
518                 }
519         for (;;)
520                 {
521                 int needcr = 0;
522
523                 r = archive_read_next_header(a, &entry);
524                 if (r == ARCHIVE_EOF)
525                         {
526                         break;
527                         }
528                 if (r != ARCHIVE_OK)
529                         {
530                         errmsg(archive_error_string(a));
531                         errmsg("\n");
532                         return(FALSE);
533                         }
534                 if (verbose && do_extract)
535                         {
536                         msg("x ");
537                         }
538                 if (verbose || !do_extract)
539                         {
540                         msg(archive_entry_pathname(entry));
541                         msg(" ");
542                         needcr = 1;
543                         }
544                 if (do_extract)
545                         {
546                         r = archive_write_header(ext, entry);
547                         if (r != ARCHIVE_OK)
548                                 {
549                                 errmsg(archive_error_string(a));
550                                 needcr = 1;
551                                 }
552                         else
553                                 {
554                                 r = copy_data(a, ext);
555                                 if (r != ARCHIVE_OK)
556                                         {
557                                         needcr = 1;
558                                         }
559                                 }
560                         }
561                 if (needcr)
562                         {
563                         msg("\n");
564                         }
565                 }
566         archive_read_close(a);
567         archive_read_free(a);
568
569         archive_write_close(ext);
570         archive_write_free(ext);
571         return(TRUE);
572 }
573
574 static int copy_data(struct archive *ar, struct archive *aw)
575 {
576         int r;
577         const void *buff;
578         size_t size;
579         int64_t offset;
580
581         for (;;)
582                 {
583                 r = archive_read_data_block(ar, &buff, &size, &offset);
584                 if (r == ARCHIVE_EOF)
585                         return (ARCHIVE_OK);
586                 if (r != ARCHIVE_OK)
587                         {
588                         errmsg(archive_error_string(ar));
589                         return (r);
590                         }
591                 r = archive_write_data_block(aw, buff, size, offset);
592                 if (r != ARCHIVE_OK)
593                         {
594                         errmsg(archive_error_string(ar));
595                         return (r);
596                         }
597                 }
598 }
599
600 static void msg(const char *m)
601 {
602         log_printf("Open Archive - libarchive error: %s \n", m);
603 }
604
605 static void errmsg(const char *m)
606 {
607         if (m == nullptr)
608                 {
609                 m = "Error: No error description provided.\n";
610                 }
611         log_printf("Open Archive - libarchive error: %s \n", m);
612 }
613 #endif
614 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */