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