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