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