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