]> git.street.me.uk Git - andy/viking.git/blob - src/util.c
Map data license does not need to be shown every time for the default 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 #include "preferences.h"
33 #include "vikmapslayer.h"
34
35 /*
36 #ifdef WINDOWS
37 #include <windows.h>
38 #endif
39
40 #ifndef WINDOWS
41 static gboolean spawn_command_line_async(const gchar * cmd,
42                                          const gchar * arg)
43 {
44   gchar *cmdline = NULL;
45   gboolean status;
46
47   cmdline = g_strdup_printf("%s '%s'", cmd, arg);
48   g_debug("Running: %s", cmdline);
49     
50   status = g_spawn_command_line_async(cmdline, NULL);
51
52   g_free(cmdline);
53  
54   return status;
55 }
56 #endif
57 */
58
59 void open_url(GtkWindow *parent, const gchar * url)
60 {
61   GError *error = NULL;
62   gtk_show_uri ( gtk_widget_get_screen (GTK_WIDGET(parent)), url, GDK_CURRENT_TIME, &error );
63   if ( error ) {
64     a_dialog_error_msg_extra ( parent, _("Could not launch web browser. %s"), error->message );
65     g_error_free ( error );
66   }
67 }
68
69 void new_email(GtkWindow *parent, const gchar * address)
70 {
71   gchar *uri = g_strdup_printf("mailto:%s", address);
72   GError *error = NULL;
73   gtk_show_uri ( gtk_widget_get_screen (GTK_WIDGET(parent)), uri, GDK_CURRENT_TIME, &error );
74   if ( error ) {
75     a_dialog_error_msg_extra ( parent, _("Could not create new email. %s"), error->message );
76     g_error_free ( error );
77   }
78   /*
79 #ifdef WINDOWS
80   ShellExecute(NULL, NULL, (char *) uri, NULL, ".\\", 0);
81 #else
82   if (!spawn_command_line_async("xdg-email", uri))
83     a_dialog_error_msg ( parent, _("Could not create new email.") );
84 #endif
85   */
86   g_free(uri);
87   uri = NULL;
88 }
89 gchar *uri_escape(gchar *str)
90 {
91   gchar *esc_str = g_malloc(3*strlen(str));
92   gchar *dst = esc_str;
93   gchar *src;
94
95   for (src = str; *src; src++) {
96     if (*src == ' ')
97      *dst++ = '+';
98     else if (g_ascii_isalnum(*src))
99      *dst++ = *src;
100     else {
101       g_sprintf(dst, "%%%02hhX", *src);
102       dst += 3;
103     }
104   }
105   *dst = '\0';
106
107   return(esc_str);
108 }
109
110
111 GList * str_array_to_glist(gchar* data[])
112 {
113   GList *gl = NULL;
114   gpointer * p;
115   for (p = (gpointer)data; *p; p++)
116     gl = g_list_prepend(gl, *p);
117   return g_list_reverse(gl);
118 }
119
120 /**
121  * split_string_from_file_on_equals:
122  *
123  * @buf: the input string
124  * @key: newly allocated string that is before the '='
125  * @val: newly allocated string after the '='
126  *
127  * Designed for file line processing, so it ignores strings beginning with special
128  *  characters, such as '#'; returns false in such situations.
129  * Also returns false if no equals character is found
130  *
131  * e.g. if buf = "GPS.parameter=42"
132  *   key = "GPS.parameter"
133  *   val = "42"
134  */
135 gboolean split_string_from_file_on_equals ( const gchar *buf, gchar **key, gchar **val )
136 {
137   // comments, special characters in viking file format
138   if ( buf == NULL || buf[0] == '\0' || buf[0] == '~' || buf[0] == '=' || buf[0] == '#' )
139     return FALSE;
140
141   if ( ! strchr ( buf, '=' ) )
142     return FALSE;
143
144   gchar **strv = g_strsplit ( buf, "=", 2 );
145
146   gint gi = 0;
147   gchar *str = strv[gi];
148   while ( str ) {
149         if ( gi == 0 )
150           *key = g_strdup ( str );
151         else
152           *val = g_strdup ( str );
153     gi++;
154     str = strv[gi];
155   }
156
157   g_strfreev ( strv );
158
159   // Remove newline from val and also any other whitespace
160   *key = g_strstrip ( *key );
161   *val = g_strstrip ( *val );
162
163   return TRUE;
164 }
165
166 /* 1 << (x) is like a 2**(x) */
167 #define GZ(x) (1<<(x))
168
169 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),
170                                       GZ(10), GZ(11), GZ(12), GZ(13), GZ(14), GZ(15), GZ(16), GZ(17) };
171
172 static const gint num_scales = (sizeof(scale_mpps) / sizeof(scale_mpps[0]));
173
174 #define ERROR_MARGIN 0.01
175 /**
176  * mpp_to_zoom:
177  *
178  * Returns: a zoom level. see : http://wiki.openstreetmap.org/wiki/Zoom_levels
179  */
180 guint8 mpp_to_zoom ( gdouble mpp )
181 {
182   gint i;
183   for ( i = 0; i < num_scales; i++ ) {
184     if ( ABS(scale_mpps[i] - mpp) < ERROR_MARGIN ) {
185       g_debug ( "mpp_to_zoom: %f -> %d", mpp, i );
186       return 20-i;
187     }
188   }
189   return 17; // a safe zoomed in default
190 }
191
192 typedef struct {
193   GtkWindow *window; // Layer needed for redrawing
194   gchar *version;     // Image list
195 } new_version_thread_data;
196
197 static gboolean new_version_available_message ( new_version_thread_data *nvtd )
198 {
199   // Only a simple goto website option is offered
200   // Trying to do an installation update is platform specific
201   if ( a_dialog_yes_or_no ( nvtd->window,
202                             _("There is a newer version of Viking available: %s\n\nDo you wish to go to Viking's website now?"), nvtd->version ) )
203     // NB 'VIKING_URL' redirects to the Wiki, here we want to go the main site.
204     open_url ( nvtd->window, "http://sourceforge.net/projects/viking/" );
205   //else
206   //  increase amount of time between performing version checks
207   g_free ( nvtd->version );
208   g_free ( nvtd );
209   return FALSE;
210 }
211
212 static void latest_version_thread ( GtkWindow *window )
213 {
214   // Need to allow a few of redirects, as SF file is often served from different server
215   DownloadMapOptions options = { FALSE, FALSE, NULL, 5, NULL, NULL };
216   gchar *filename = a_download_uri_to_tmp_file ( "http://sourceforge.net/projects/viking/files/VERSION", &options );
217   //gchar *filename = g_strdup ( "VERSION" );
218   if ( !filename ) {
219     return;
220   }
221
222   GMappedFile *mf = g_mapped_file_new ( filename, FALSE, NULL );
223   if ( !mf )
224     return;
225
226   gchar *text = g_mapped_file_get_contents ( mf );
227
228   gint latest_version = viking_version_to_number ( text );
229   gint my_version = viking_version_to_number ( VIKING_VERSION );
230
231   g_debug ( "The lastest version is: %s", text );
232
233   if ( my_version < latest_version ) {
234     new_version_thread_data *nvtd = g_malloc ( sizeof(new_version_thread_data) );
235     nvtd->window = window;
236     nvtd->version = g_strdup ( text );
237     gdk_threads_add_idle ( (GSourceFunc) new_version_available_message, nvtd );
238   }
239   else
240     g_debug ( "Running the lastest version: %s", VIKING_VERSION );
241
242   g_mapped_file_unref ( mf );
243   if ( filename ) {
244     g_remove ( filename );
245     g_free ( filename );
246   }
247 }
248
249 /**
250  * check_latest_version:
251  * @window: Somewhere where we may need use the display to inform the user about the version status
252  *
253  * Periodically checks the released latest VERSION file on the website to compare with the running version
254  *
255  * ATM the plan is for a 1.4.2 release to be always on *just* for Windows platforms
256  * Then in 1.5.X it will made entirely optional (default on for Windows)
257  *  with a longer periodic check (enabled via state saving using the soon to be released 'settings' code)
258  *
259  */
260 void check_latest_version ( GtkWindow *window )
261 {
262 #ifdef WINDOWS
263 #if GLIB_CHECK_VERSION (2, 32, 0)
264   g_thread_try_new ( "latest_version_thread", (GThreadFunc)latest_version_thread, window, NULL );
265 #else
266   g_thread_create ( (GThreadFunc)latest_version_thread, window, FALSE, NULL );
267 #endif
268 #endif
269 }
270
271 /**
272  * set_auto_features_on_first_run:
273  *
274  *  Ask the user's opinion to set some of Viking's default behaviour
275  */
276 void set_auto_features_on_first_run ( void )
277 {
278   gboolean auto_features = FALSE;
279   if ( a_vik_very_first_run () ) {
280
281     GtkWidget *win = gtk_window_new ( GTK_WINDOW_TOPLEVEL );
282
283     if ( a_dialog_yes_or_no ( GTK_WINDOW(win),
284                               _("This appears to be Viking's very first run.\n\nDo you wish to enable automatic internet features?\n\nIndividual settings can be controlled in the Preferences."), NULL ) )
285       auto_features = TRUE;
286   }
287
288   if ( auto_features ) {
289     // Set Maps to autodownload
290     // Ensure the default is true
291     maps_layer_set_autodownload_default ( TRUE );
292
293     // Simplistic repeat of preference settings
294     //  Only the name & type are important for setting a preference via this 'external' way
295
296     // Enable auto add map +
297     // Enable IP lookup
298     VikLayerParam pref_add_map[] = { { VIK_LAYER_NUM_TYPES, VIKING_PREFERENCES_STARTUP_NAMESPACE "add_default_map_layer", VIK_LAYER_PARAM_BOOLEAN, VIK_LAYER_GROUP_NONE, NULL, VIK_LAYER_WIDGET_CHECKBUTTON, NULL, NULL, NULL, NULL, }, };
299     VikLayerParam pref_startup_method[] = { { VIK_LAYER_NUM_TYPES, VIKING_PREFERENCES_STARTUP_NAMESPACE "startup_method", VIK_LAYER_PARAM_UINT, VIK_LAYER_GROUP_NONE, NULL, VIK_LAYER_WIDGET_COMBOBOX, NULL, NULL, NULL, NULL, }, };
300
301     VikLayerParamData vlp_data;
302     vlp_data.b = TRUE;
303     a_preferences_run_setparam ( vlp_data, pref_add_map );
304
305     vlp_data.u = VIK_STARTUP_METHOD_AUTO_LOCATION;
306     a_preferences_run_setparam ( vlp_data, pref_startup_method );
307
308     // Ensure settings are saved for next time
309     a_preferences_save_to_file ();
310   }
311 }