Addl fix #299: File Compression and Archiving
[geeqie.git] / src / misc.c
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 #include "ui_fileops.h"
24
25 #include <langinfo.h>
26 #include <locale.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_bin_dir, 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(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 = 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         FILE *cpuinfo = fopen("/proc/cpuinfo", "rb");
382         char *arg = 0;
383         size_t size = 0;
384         int cores = 1;
385         gchar *siblings_line;
386         gchar *siblings_str;
387
388         while(getline(&arg, &size, cpuinfo) != -1)
389                 {
390                 siblings_line = g_strrstr(arg, "siblings");
391                 if (siblings_line)
392                         {
393                         siblings_str = g_strrstr(siblings_line, ":");
394                         if (siblings_str)
395                                 {
396                                 cores = g_ascii_strtoll(siblings_str + 1, NULL, 0);
397                                 }
398                         }
399                 }
400         free(arg);
401         fclose(cpuinfo);
402
403         return cores;
404 }
405
406 void tree_path_free_wrapper(void *data, void *useradata)
407 {
408         gtk_tree_path_free(data);
409 }
410
411 /* Copied from the libarchive .repo. examples */
412
413 #ifndef HAVE_ARCHIVE
414 gchar *open_archive(FileData *fd)
415 {
416         return NULL;
417 }
418
419 #else
420
421 #include <archive.h>
422 #include <archive_entry.h>
423
424 static void errmsg(const char *);
425 static gboolean extract(const char *filename, int do_extract, int flags);
426 static int copy_data(struct archive *, struct archive *);
427 static void msg(const char *);
428 static int verbose = 0;
429
430 gchar *open_archive(FileData *fd)
431 {
432         int flags;
433         gchar *current_dir;
434         gchar *destination_dir;
435         gboolean success;
436         gint error;
437
438         destination_dir = g_build_filename(g_get_tmp_dir(), GQ_ARCHIVE_DIR, instance_identifier, fd->path, NULL);
439
440         if (!recursive_mkdir_if_not_exists(destination_dir, 0755))
441                 {
442                 log_printf("%s%s%s", _("Open Archive - Cannot create directory: "), destination_dir, "\n");
443                 g_free(destination_dir);
444                 return NULL;
445                 }
446
447         current_dir = g_get_current_dir();
448         error = chdir(destination_dir);
449         if (error)
450                 {
451                 log_printf("%s%s%s%s%s", _("Open Archive - Cannot change directory to: "), destination_dir, _("\n  Error code: "), strerror(errno), "\n");
452                 g_free(destination_dir);
453                 g_free(current_dir);
454                 return NULL;
455                 }
456
457         flags = ARCHIVE_EXTRACT_TIME;
458         success = extract(fd->path, 1, flags);
459
460         error = chdir(current_dir);
461         if (error)
462                 {
463                 log_printf("%s%s%s%s%s", _("Open Archive - Cannot change directory to: "), current_dir, _("\n  Error code: "), strerror(errno), "\n");
464                 g_free(destination_dir);
465                 g_free(current_dir);
466                 return NULL;
467                 }
468         g_free(current_dir);
469
470         if (!success)
471                 {
472                 g_free(destination_dir);
473                 destination_dir = NULL;
474                 }
475
476         return destination_dir;
477 }
478
479 static gboolean extract(const char *filename, int do_extract, int flags)
480 {
481         struct archive *a;
482         struct archive *ext;
483         struct archive_entry *entry;
484         int r;
485
486         a = archive_read_new();
487         ext = archive_write_disk_new();
488         archive_write_disk_set_options(ext, flags);
489         archive_write_disk_set_standard_lookup(ext);
490         archive_read_support_filter_all(a);
491         archive_read_support_format_all(a);
492
493         if (filename != NULL && strcmp(filename, "-") == 0)
494                 {
495                 filename = NULL;
496                 }
497         if ((r = archive_read_open_filename(a, filename, 10240)))
498                 {
499                 errmsg(archive_error_string(a));
500                 errmsg("\n");
501                 return(FALSE);
502                 }
503         for (;;)
504                 {
505                 int needcr = 0;
506
507                 r = archive_read_next_header(a, &entry);
508                 if (r == ARCHIVE_EOF)
509                         {
510                         break;
511                         }
512                 if (r != ARCHIVE_OK)
513                         {
514                         errmsg(archive_error_string(a));
515                         errmsg("\n");
516                         return(FALSE);
517                         }
518                 if (verbose && do_extract)
519                         {
520                         msg("x ");
521                         }
522                 if (verbose || !do_extract)
523                         {
524                         msg(archive_entry_pathname(entry));
525                         msg(" ");
526                         needcr = 1;
527                         }
528                 if (do_extract)
529                         {
530                         r = archive_write_header(ext, entry);
531                         if (r != ARCHIVE_OK)
532                                 {
533                                 errmsg(archive_error_string(a));
534                                 needcr = 1;
535                                 }
536                         else
537                                 {
538                                 r = copy_data(a, ext);
539                                 if (r != ARCHIVE_OK)
540                                         {
541                                         needcr = 1;
542                                         }
543                                 }
544                         }
545                 if (needcr)
546                         {
547                         msg("\n");
548                         }
549                 }
550         archive_read_close(a);
551         archive_read_free(a);
552
553         archive_write_close(ext);
554         archive_write_free(ext);
555         return(TRUE);
556 }
557
558 static int copy_data(struct archive *ar, struct archive *aw)
559 {
560         int r;
561         const void *buff;
562         size_t size;
563         int64_t offset;
564
565         for (;;)
566                 {
567                 r = archive_read_data_block(ar, &buff, &size, &offset);
568                 if (r == ARCHIVE_EOF)
569                         return (ARCHIVE_OK);
570                 if (r != ARCHIVE_OK)
571                         {
572                         errmsg(archive_error_string(ar));
573                         return (r);
574                         }
575                 r = archive_write_data_block(aw, buff, size, offset);
576                 if (r != ARCHIVE_OK)
577                         {
578                         errmsg(archive_error_string(ar));
579                         return (r);
580                         }
581                 }
582 }
583
584 static void msg(const char *m)
585 {
586         log_printf("Open Archive - libarchive error: %s \n", m);
587 }
588
589 static void errmsg(const char *m)
590 {
591         if (m == NULL)
592                 {
593                 m = "Error: No error description provided.\n";
594                 }
595         log_printf("Open Archive - libarchive error: %s \n", m);
596 }
597 #endif
598 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */