Geocode image by drag-drop
[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 gdouble get_zoom_increment(void)
26 {
27         return ((options->image.zoom_increment != 0) ? (gdouble)options->image.zoom_increment / 100.0 : 1.0);
28 }
29
30 gchar *utf8_validate_or_convert(const gchar *text)
31 {
32         gint len;
33
34         if (!text) return NULL;
35
36         len = strlen(text);
37         if (!g_utf8_validate(text, len, NULL))
38                 return g_convert(text, len, "UTF-8", "ISO-8859-1", NULL, NULL, NULL);
39
40         return g_strdup(text);
41 }
42
43 gint utf8_compare(const gchar *s1, const gchar *s2, gboolean case_sensitive)
44 {
45         gchar *s1_key, *s2_key;
46         gchar *s1_t, *s2_t;
47         gint ret;
48
49         g_assert(g_utf8_validate(s1, -1, NULL));
50         g_assert(g_utf8_validate(s2, -1, NULL));
51
52         if (!case_sensitive)
53                 {
54                 s1_t = g_utf8_casefold(s1, -1);
55                 s2_t = g_utf8_casefold(s2, -1);
56                 }
57         else
58                 {
59                 s1_t = (gchar *) s1;
60                 s2_t = (gchar *) s2;
61                 }
62
63         s1_key = g_utf8_collate_key(s1_t, -1);
64         s2_key = g_utf8_collate_key(s2_t, -1);
65
66         ret = strcmp(s1_key, s2_key);
67
68         g_free(s1_key);
69         g_free(s2_key);
70
71         if (!case_sensitive)
72                 {
73                 g_free(s1_t);
74                 g_free(s2_t);
75                 }
76
77         return ret;
78 }
79
80 /* Borrowed from gtkfilesystemunix.c */
81 gchar *expand_tilde(const gchar *filename)
82 {
83 #ifndef G_OS_UNIX
84         return g_strdup(filename);
85 #else
86         const gchar *notilde;
87         const gchar *slash;
88         const gchar *home;
89
90         if (filename[0] != '~')
91                 return g_strdup(filename);
92
93         notilde = filename + 1;
94         slash = strchr(notilde, G_DIR_SEPARATOR);
95         if (slash == notilde || !*notilde)
96                 {
97                 home = g_get_home_dir();
98                 if (!home)
99                         return g_strdup(filename);
100                 }
101         else
102                 {
103                 gchar *username;
104                 struct passwd *passwd;
105
106                 if (slash)
107                         username = g_strndup(notilde, slash - notilde);
108                 else
109                         username = g_strdup(notilde);
110
111                 passwd = getpwnam(username);
112                 g_free(username);
113
114                 if (!passwd)
115                         return g_strdup(filename);
116
117                 home = passwd->pw_dir;
118                 }
119
120         if (slash)
121                 return g_build_filename(home, G_DIR_SEPARATOR_S, slash + 1, NULL);
122         else
123                 return g_build_filename(home, G_DIR_SEPARATOR_S, NULL);
124 #endif
125 }
126
127 /* Search for latitude/longitude parameters in a string
128  */
129
130 #define GEOCODE_NAME "geocode-parameters.awk"
131 #define BUFSIZE 128
132
133 gchar *decode_geo_parameters(const gchar *input_text)
134 {
135         gchar *message;
136         gchar *path = g_build_filename(get_rc_dir(), GEOCODE_NAME, NULL);
137         gchar *cmd = g_strconcat("echo \'", input_text, "\'  | awk -f ", path, NULL);
138
139         if (g_file_test(path, G_FILE_TEST_EXISTS))
140                 {
141                 gchar buf[BUFSIZE];
142                 FILE *fp;
143
144                 if ((fp = popen(cmd, "r")) == NULL)
145                         {
146                         message = g_strconcat("Error: opening pipe\n", input_text, NULL);
147                         }
148                 else
149                         {
150                         fgets(buf, BUFSIZE, fp);
151                         message = g_strconcat(buf, NULL);
152
153                         if(pclose(fp))
154                                 {
155                                 message = g_strconcat("Error: Command not found or exited with error status\n", input_text, NULL);
156                                 }
157                         }
158                 }
159         else
160                 {
161                 message = g_strconcat(input_text, NULL);
162                 }
163
164         g_free(path);
165         g_free(cmd);
166         return message;
167 }
168
169 /* Run a command like system() but may output debug messages. */
170 int runcmd(gchar *cmd)
171 {
172 #if 1
173         return system(cmd);
174         return 0;
175 #else
176         /* For debugging purposes */
177         int retval = -1;
178         FILE *in;
179
180         DEBUG_1("Running command: %s", cmd);
181
182         in = popen(cmd, "r");
183         if (in)
184                 {
185                 int status;
186                 const gchar *msg;
187                 gchar buf[2048];
188
189                 while (fgets(buf, sizeof(buf), in) != NULL )
190                         {
191                         DEBUG_1("Output: %s", buf);
192                         }
193
194                 status = pclose(in);
195
196                 if (WIFEXITED(status))
197                         {
198                         msg = "Command terminated with exit code";
199                         retval = WEXITSTATUS(status);
200                         }
201                 else if (WIFSIGNALED(status))
202                         {
203                         msg = "Command was killed by signal";
204                         retval = WTERMSIG(status);
205                         }
206                 else
207                         {
208                         msg = "pclose() returned";
209                         retval = status;
210                         }
211
212                 DEBUG_1("%s : %d\n", msg, retval);
213         }
214
215         return retval;
216 #endif
217 }
218
219
220 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */