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