Move open_archive() to separate module
[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 <sys/stat.h>
24 #include <unistd.h>
25
26 #include <cstdio>
27 #include <cstdlib>
28 #include <cstring>
29 #include <memory>
30
31 #include <langinfo.h>
32 #include <pwd.h>
33
34 #include <config.h>
35
36 #include "debug.h"
37 #include "main.h"
38 #include "options.h"
39 #include "ui-fileops.h"
40
41 gdouble get_zoom_increment()
42 {
43         return ((options->image.zoom_increment != 0) ? static_cast<gdouble>(options->image.zoom_increment) / 100.0 : 1.0);
44 }
45
46 gchar *utf8_validate_or_convert(const gchar *text)
47 {
48         gint len;
49
50         if (!text) return nullptr;
51
52         len = strlen(text);
53         if (!g_utf8_validate(text, len, nullptr))
54                 return g_convert(text, len, "UTF-8", "ISO-8859-1", nullptr, nullptr, nullptr);
55
56         return g_strdup(text);
57 }
58
59 gint utf8_compare(const gchar *s1, const gchar *s2, gboolean case_sensitive)
60 {
61         gchar *s1_key;
62         gchar *s2_key;
63         gchar *s1_t;
64         gchar *s2_t;
65         gint ret;
66
67         g_assert(g_utf8_validate(s1, -1, nullptr));
68         g_assert(g_utf8_validate(s2, -1, nullptr));
69
70         if (!case_sensitive)
71                 {
72                 s1_t = g_utf8_casefold(s1, -1);
73                 s2_t = g_utf8_casefold(s2, -1);
74                 }
75         else
76                 {
77                 s1_t = const_cast<gchar *>(s1);
78                 s2_t = const_cast<gchar *>(s2);
79                 }
80
81         s1_key = g_utf8_collate_key(s1_t, -1);
82         s2_key = g_utf8_collate_key(s2_t, -1);
83
84         ret = strcmp(s1_key, s2_key);
85
86         g_free(s1_key);
87         g_free(s2_key);
88
89         if (!case_sensitive)
90                 {
91                 g_free(s1_t);
92                 g_free(s2_t);
93                 }
94
95         return ret;
96 }
97
98 /* Borrowed from gtkfilesystemunix.c */
99 gchar *expand_tilde(const gchar *filename)
100 {
101 #ifndef G_OS_UNIX
102         return g_strdup(filename);
103 #else
104         const gchar *notilde;
105         const gchar *slash;
106         const gchar *home;
107
108         if (filename[0] != '~')
109                 return g_strdup(filename);
110
111         notilde = filename + 1;
112         slash = strchr(notilde, G_DIR_SEPARATOR);
113         if (slash == notilde || !*notilde)
114                 {
115                 home = g_get_home_dir();
116                 if (!home)
117                         return g_strdup(filename);
118                 }
119         else
120                 {
121                 gchar *username;
122                 struct passwd *passwd;
123
124                 if (slash)
125                         username = g_strndup(notilde, slash - notilde);
126                 else
127                         username = g_strdup(notilde);
128
129                 passwd = getpwnam(username);
130                 g_free(username);
131
132                 if (!passwd)
133                         return g_strdup(filename);
134
135                 home = passwd->pw_dir;
136                 }
137
138         if (slash)
139                 return g_build_filename(home, G_DIR_SEPARATOR_S, slash + 1, NULL);
140
141         return g_build_filename(home, G_DIR_SEPARATOR_S, NULL);
142 #endif
143 }
144
145 /* Search for latitude/longitude parameters in a string
146  */
147
148 #define GEOCODE_NAME "geocode-parameters.awk"
149 enum {
150         BUFSIZE = 128
151 };
152
153 gchar *decode_geo_script(const gchar *path_dir, const gchar *input_text)
154 {
155         std::unique_ptr<gchar, decltype(&g_free)> message{nullptr, g_free};
156         gchar *path = g_build_filename(path_dir, GEOCODE_NAME, NULL);
157         gchar *cmd = g_strconcat("echo \'", input_text, "\'  | awk -f ", path, NULL);
158
159         if (g_file_test(path, G_FILE_TEST_EXISTS))
160                 {
161                 gchar buf[BUFSIZE];
162                 FILE *fp;
163
164                 if ((fp = popen(cmd, "r")) == nullptr)
165                         {
166                         message.reset(g_strconcat("Error: opening pipe\n", input_text, NULL));
167                         }
168                 else
169                         {
170                         while (fgets(buf, BUFSIZE, fp))
171                                 {
172                                 DEBUG_1("Output: %s", buf);
173                                 }
174
175                         message.reset(g_strconcat(buf, NULL));
176
177                         if(pclose(fp))
178                                 {
179                                 message.reset(g_strconcat("Error: Command not found or exited with error status\n", input_text, NULL));
180                                 }
181                         }
182                 }
183         else
184                 {
185                 message.reset(g_strconcat(input_text, NULL));
186                 }
187
188         g_free(path);
189         g_free(cmd);
190         return message.release();
191 }
192
193 gchar *decode_geo_parameters(const gchar *input_text)
194 {
195         gchar *message;
196         gchar *dir;
197
198         message = decode_geo_script(gq_bindir, input_text);
199         if (strstr(message, "Error"))
200                 {
201                 g_free(message);
202                 dir = g_build_filename(get_rc_dir(), "applications", NULL);
203                 message = decode_geo_script(dir, input_text);
204                 g_free(dir);
205                 }
206
207         return message;
208 }
209
210 /* Run a command like system() but may output debug messages. */
211 int runcmd(const gchar *cmd)
212 {
213 #if 1
214         return system(cmd);
215         return 0;
216 #else
217         /* For debugging purposes */
218         int retval = -1;
219         FILE *in;
220
221         DEBUG_1("Running command: %s", cmd);
222
223         in = popen(cmd, "r");
224         if (in)
225                 {
226                 int status;
227                 const gchar *msg;
228                 gchar buf[2048];
229
230                 while (fgets(buf, sizeof(buf), in) != NULL )
231                         {
232                         DEBUG_1("Output: %s", buf);
233                         }
234
235                 status = pclose(in);
236
237                 if (WIFEXITED(status))
238                         {
239                         msg = "Command terminated with exit code";
240                         retval = WEXITSTATUS(status);
241                         }
242                 else if (WIFSIGNALED(status))
243                         {
244                         msg = "Command was killed by signal";
245                         retval = WTERMSIG(status);
246                         }
247                 else
248                         {
249                         msg = "pclose() returned";
250                         retval = status;
251                         }
252
253                 DEBUG_1("%s : %d\n", msg, retval);
254         }
255
256         return retval;
257 #endif
258 }
259
260 /**
261  * @brief Returns integer representing first_day_of_week
262  * @returns Integer in range 1 to 7
263  * 
264  * Uses current locale to get first day of week.
265  * If _NL_TIME_FIRST_WEEKDAY is not available, ISO 8601
266  * states first day of week is Monday.
267  * USA, Mexico and Canada (and others) use Sunday as first day of week.
268  * 
269  * Sunday == 1
270  */
271 gint date_get_first_day_of_week()
272 {
273 #if HAVE__NL_TIME_FIRST_WEEKDAY
274         return nl_langinfo(_NL_TIME_FIRST_WEEKDAY)[0];
275 #else
276         gchar *dot;
277         gchar *current_locale;
278
279         current_locale = setlocale(LC_ALL, NULL);
280         dot = strstr(current_locale, ".");
281         if ((strncmp(dot - 2, "US", 2) == 0) || (strncmp(dot - 2, "MX", 2) == 0) || (strncmp(dot - 2, "CA", 2) == 0))
282                 {
283                 return 1;
284                 }
285         else
286                 {
287                 return 2;
288                 }
289 #endif
290 }
291
292 /**
293  * @brief Get an abbreviated day name from locale
294  * @param day Integer in range 1 to 7, representing day of week
295  * @returns String containing abbreviated day name
296  * 
297  *  Uses current locale to get day name
298  * 
299  * Sunday == 1
300  * Result must be freed
301  */
302 gchar *date_get_abbreviated_day_name(gint day)
303 {
304         gchar *abday = nullptr;
305
306         switch (day)
307                 {
308                 case 1:
309                 abday = g_strdup(nl_langinfo(ABDAY_1));
310                 break;
311                 case 2:
312                 abday = g_strdup(nl_langinfo(ABDAY_2));
313                 break;
314                 case 3:
315                 abday = g_strdup(nl_langinfo(ABDAY_3));
316                 break;
317                 case 4:
318                 abday = g_strdup(nl_langinfo(ABDAY_4));
319                 break;
320                 case 5:
321                 abday = g_strdup(nl_langinfo(ABDAY_5));
322                 break;
323                 case 6:
324                 abday = g_strdup(nl_langinfo(ABDAY_6));
325                 break;
326                 case 7:
327                 abday = g_strdup(nl_langinfo(ABDAY_7));
328                 break;
329                 }
330
331         return abday;
332 }
333
334 gchar *convert_rating_to_stars(gint rating)
335 {
336         GString *str = g_string_new(nullptr);
337
338         if (rating == -1)
339                 {
340                 str = g_string_append_unichar(str, options->star_rating.rejected);
341                 return g_string_free(str, FALSE);
342                 }
343
344         if (rating > 0 && rating < 6)
345                 {
346                 for (; rating > 0; --rating)
347                         {
348                         str = g_string_append_unichar(str, options->star_rating.star);
349                         }
350                 return g_string_free(str, FALSE);
351                 }
352
353         return g_strdup("");
354 }
355
356 gchar *get_symbolic_link(const gchar *path_utf8)
357 {
358         gchar *sl;
359         struct stat st;
360         gchar *ret = g_strdup("");
361
362         sl = path_from_utf8(path_utf8);
363
364         if (lstat(sl, &st) == 0 && S_ISLNK(st.st_mode))
365                 {
366                 gchar *buf;
367                 gint l;
368
369                 buf = static_cast<gchar *>(g_malloc(st.st_size + 1));
370                 l = readlink(sl, buf, st.st_size);
371
372                 if (l == st.st_size)
373                         {
374                         buf[l] = '\0';
375
376                         ret = buf;
377                         }
378                 else
379                         {
380                         g_free(buf);
381                         }
382                 }
383
384         g_free(sl);
385
386         return ret;
387 }
388
389 gint get_cpu_cores()
390 {
391     return sysconf(_SC_NPROCESSORS_ONLN);
392 }
393
394 #if HAVE_GTK4
395 void convert_gdkcolor_to_gdkrgba(gpointer data, GdkRGBA *gdk_rgba)
396 {
397 /* @FIXME GTK4 stub */
398 }
399 #else
400 void convert_gdkcolor_to_gdkrgba(gpointer data, GdkRGBA *gdk_rgba)
401 {
402         auto gdk_color = static_cast<GdkColor *>(data);
403
404         gdk_rgba->red = CLAMP((double)gdk_color->red / 65535.0, 0.0, 1.0);
405         gdk_rgba->green = CLAMP((double)gdk_color->green / 65535.0, 0.0, 1.0);
406         gdk_rgba->blue = CLAMP((double)gdk_color->blue / 65535.0, 0.0, 1.0);
407         gdk_rgba->alpha = 1.0;
408 }
409 #endif
410
411 void gq_gtk_entry_set_text(GtkEntry *entry, const gchar *text)
412 {
413         GtkEntryBuffer *buffer;
414
415         buffer = gtk_entry_get_buffer(entry);
416         gtk_entry_buffer_set_text(buffer, text, static_cast<gint>(g_utf8_strlen(text, -1)));
417 }
418
419 const gchar *gq_gtk_entry_get_text(GtkEntry *entry)
420 {
421         GtkEntryBuffer *buffer;
422
423         buffer = gtk_entry_get_buffer(entry);
424         return gtk_entry_buffer_get_text(buffer);
425 }
426
427 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)
428 {
429         gtk_grid_attach(grid, child, left_attach, top_attach, right_attach - left_attach, bottom_attach - top_attach);
430 }
431
432 void gq_gtk_grid_attach_default(GtkGrid *grid, GtkWidget *child, guint left_attach, guint right_attach, guint top_attach, guint bottom_attach )
433 {
434         gtk_grid_attach(grid, child, left_attach, top_attach, right_attach - left_attach, bottom_attach - top_attach);
435 }
436
437 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */