]> git.street.me.uk Git - andy/viking.git/blob - src/util.c
Add a startup option to load a default map (as defined by layer default for the map...
[andy/viking.git] / src / util.c
1 /*
2  *    Viking - GPS data editor
3  *    Copyright (C) 2007, Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
4  *    Based on:
5  *    Copyright (C) 2003-2007, Leandro A. F. Pereira <leandro@linuxmag.com.br>
6  *
7  *    This program is free software; you can redistribute it and/or modify
8  *    it under the terms of the GNU General Public License as published by
9  *    the Free Software Foundation, version 2.
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
17  *    along with this program; if not, write to the Free Software
18  *    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <glib/gstdio.h>
25 #include <glib/gi18n.h>
26 #include <glib/gprintf.h>
27
28 #include "util.h"
29 #include "dialog.h"
30 #include "globals.h"
31 #include "download.h"
32
33 /*
34 #ifdef WINDOWS
35 #include <windows.h>
36 #endif
37
38 #ifndef WINDOWS
39 static gboolean spawn_command_line_async(const gchar * cmd,
40                                          const gchar * arg)
41 {
42   gchar *cmdline = NULL;
43   gboolean status;
44
45   cmdline = g_strdup_printf("%s '%s'", cmd, arg);
46   g_debug("Running: %s", cmdline);
47     
48   status = g_spawn_command_line_async(cmdline, NULL);
49
50   g_free(cmdline);
51  
52   return status;
53 }
54 #endif
55 */
56
57 void open_url(GtkWindow *parent, const gchar * url)
58 {
59   GError *error = NULL;
60   gtk_show_uri ( gtk_widget_get_screen (GTK_WIDGET(parent)), url, GDK_CURRENT_TIME, &error );
61   if ( error ) {
62     a_dialog_error_msg_extra ( parent, _("Could not launch web browser. %s"), error->message );
63     g_error_free ( error );
64   }
65 }
66
67 void new_email(GtkWindow *parent, const gchar * address)
68 {
69   gchar *uri = g_strdup_printf("mailto:%s", address);
70   GError *error = NULL;
71   gtk_show_uri ( gtk_widget_get_screen (GTK_WIDGET(parent)), uri, GDK_CURRENT_TIME, &error );
72   if ( error ) {
73     a_dialog_error_msg_extra ( parent, _("Could not create new email. %s"), error->message );
74     g_error_free ( error );
75   }
76   /*
77 #ifdef WINDOWS
78   ShellExecute(NULL, NULL, (char *) uri, NULL, ".\\", 0);
79 #else
80   if (!spawn_command_line_async("xdg-email", uri))
81     a_dialog_error_msg ( parent, _("Could not create new email.") );
82 #endif
83   */
84   g_free(uri);
85   uri = NULL;
86 }
87 gchar *uri_escape(gchar *str)
88 {
89   gchar *esc_str = g_malloc(3*strlen(str));
90   gchar *dst = esc_str;
91   gchar *src;
92
93   for (src = str; *src; src++) {
94     if (*src == ' ')
95      *dst++ = '+';
96     else if (g_ascii_isalnum(*src))
97      *dst++ = *src;
98     else {
99       g_sprintf(dst, "%%%02hhX", *src);
100       dst += 3;
101     }
102   }
103   *dst = '\0';
104
105   return(esc_str);
106 }
107
108
109 GList * str_array_to_glist(gchar* data[])
110 {
111   GList *gl = NULL;
112   gpointer * p;
113   for (p = (gpointer)data; *p; p++)
114     gl = g_list_prepend(gl, *p);
115   return g_list_reverse(gl);
116 }
117
118 /**
119  * split_string_from_file_on_equals:
120  *
121  * @buf: the input string
122  * @key: newly allocated string that is before the '='
123  * @val: newly allocated string after the '='
124  *
125  * Designed for file line processing, so it ignores strings beginning with special
126  *  characters, such as '#'; returns false in such situations.
127  * Also returns false if no equals character is found
128  *
129  * e.g. if buf = "GPS.parameter=42"
130  *   key = "GPS.parameter"
131  *   val = "42"
132  */
133 gboolean split_string_from_file_on_equals ( const gchar *buf, gchar **key, gchar **val )
134 {
135   // comments, special characters in viking file format
136   if ( buf == NULL || buf[0] == '\0' || buf[0] == '~' || buf[0] == '=' || buf[0] == '#' )
137     return FALSE;
138
139   if ( ! strchr ( buf, '=' ) )
140     return FALSE;
141
142   gchar **strv = g_strsplit ( buf, "=", 2 );
143
144   gint gi = 0;
145   gchar *str = strv[gi];
146   while ( str ) {
147         if ( gi == 0 )
148           *key = g_strdup ( str );
149         else
150           *val = g_strdup ( str );
151     gi++;
152     str = strv[gi];
153   }
154
155   g_strfreev ( strv );
156
157   // Remove newline from val and also any other whitespace
158   *key = g_strstrip ( *key );
159   *val = g_strstrip ( *val );
160
161   return TRUE;
162 }
163
164 /* 1 << (x) is like a 2**(x) */
165 #define GZ(x) (1<<(x))
166
167 static const gdouble scale_mpps[] = { 0.125, 0.25, 0.5, GZ(0), GZ(1), GZ(2), GZ(3), GZ(4), GZ(5), GZ(6), GZ(7), GZ(8), GZ(9),
168                                       GZ(10), GZ(11), GZ(12), GZ(13), GZ(14), GZ(15), GZ(16), GZ(17) };
169
170 static const gint num_scales = (sizeof(scale_mpps) / sizeof(scale_mpps[0]));
171
172 #define ERROR_MARGIN 0.01
173 /**
174  * mpp_to_zoom:
175  *
176  * Returns: a zoom level. see : http://wiki.openstreetmap.org/wiki/Zoom_levels
177  */
178 guint8 mpp_to_zoom ( gdouble mpp )
179 {
180   gint i;
181   for ( i = 0; i < num_scales; i++ ) {
182     if ( ABS(scale_mpps[i] - mpp) < ERROR_MARGIN ) {
183       g_debug ( "mpp_to_zoom: %f -> %d", mpp, i );
184       return 20-i;
185     }
186   }
187   return 17; // a safe zoomed in default
188 }
189
190 typedef struct {
191   GtkWindow *window; // Layer needed for redrawing
192   gchar *version;     // Image list
193 } new_version_thread_data;
194
195 static gboolean new_version_available_message ( new_version_thread_data *nvtd )
196 {
197   // Only a simple goto website option is offered
198   // Trying to do an installation update is platform specific
199   if ( a_dialog_yes_or_no ( nvtd->window,
200                             _("There is a newer version of Viking available: %s\n\nDo you wish to go to Viking's website now?"), nvtd->version ) )
201     // NB 'VIKING_URL' redirects to the Wiki, here we want to go the main site.
202     open_url ( nvtd->window, "http://sourceforge.net/projects/viking/" );
203   //else
204   //  increase amount of time between performing version checks
205   g_free ( nvtd->version );
206   g_free ( nvtd );
207   return FALSE;
208 }
209
210 static void latest_version_thread ( GtkWindow *window )
211 {
212   // Need to allow a few of redirects, as SF file is often served from different server
213   DownloadMapOptions options = { FALSE, FALSE, NULL, 5, NULL, NULL };
214   gchar *filename = a_download_uri_to_tmp_file ( "http://sourceforge.net/projects/viking/files/VERSION", &options );
215   //gchar *filename = g_strdup ( "VERSION" );
216   if ( !filename ) {
217     return;
218   }
219
220   GMappedFile *mf = g_mapped_file_new ( filename, FALSE, NULL );
221   if ( !mf )
222     return;
223
224   gchar *text = g_mapped_file_get_contents ( mf );
225
226   gint latest_version = viking_version_to_number ( text );
227   gint my_version = viking_version_to_number ( VIKING_VERSION );
228
229   g_debug ( "The lastest version is: %s", text );
230
231   if ( my_version < latest_version ) {
232     new_version_thread_data *nvtd = g_malloc ( sizeof(new_version_thread_data) );
233     nvtd->window = window;
234     nvtd->version = g_strdup ( text );
235     gdk_threads_add_idle ( (GSourceFunc) new_version_available_message, nvtd );
236   }
237   else
238     g_debug ( "Running the lastest version: %s", VIKING_VERSION );
239
240   g_mapped_file_unref ( mf );
241   if ( filename ) {
242     g_remove ( filename );
243     g_free ( filename );
244   }
245 }
246
247 /*
248  * check_latest_version:
249  * @window: Somewhere where we may need use the display to inform the user about the version status
250  *
251  * Periodically checks the released latest VERSION file on the website to compare with the running version
252  *
253  * ATM the plan is for a 1.4.2 release to be always on *just* for Windows platforms
254  * Then in 1.5.X it will made entirely optional (default on for Windows)
255  *  with a longer periodic check (enabled via state saving using the soon to be released 'settings' code)
256  *
257  */
258 void check_latest_version ( GtkWindow *window )
259 {
260 #ifdef WINDOWS
261 #if GLIB_CHECK_VERSION (2, 32, 0)
262   g_thread_try_new ( "latest_version_thread", (GThreadFunc)latest_version_thread, window, NULL );
263 #else
264   g_thread_create ( (GThreadFunc)latest_version_thread, window, FALSE, NULL );
265 #endif
266 #endif
267 }