2 * Viking - GPS data editor
3 * Copyright (C) 2007, Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
5 * Copyright (C) 2003-2007, Leandro A. F. Pereira <leandro@linuxmag.com.br>
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.
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.
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
24 #include <glib/gstdio.h>
25 #include <glib/gi18n.h>
26 #include <glib/gprintf.h>
32 #include "preferences.h"
33 #include "vikmapslayer.h"
41 static gboolean spawn_command_line_async(const gchar * cmd,
44 gchar *cmdline = NULL;
47 cmdline = g_strdup_printf("%s '%s'", cmd, arg);
48 g_debug("Running: %s", cmdline);
50 status = g_spawn_command_line_async(cmdline, NULL);
59 void open_url(GtkWindow *parent, const gchar * url)
62 gtk_show_uri ( gtk_widget_get_screen (GTK_WIDGET(parent)), url, GDK_CURRENT_TIME, &error );
64 a_dialog_error_msg_extra ( parent, _("Could not launch web browser. %s"), error->message );
65 g_error_free ( error );
69 void new_email(GtkWindow *parent, const gchar * address)
71 gchar *uri = g_strdup_printf("mailto:%s", address);
73 gtk_show_uri ( gtk_widget_get_screen (GTK_WIDGET(parent)), uri, GDK_CURRENT_TIME, &error );
75 a_dialog_error_msg_extra ( parent, _("Could not create new email. %s"), error->message );
76 g_error_free ( error );
80 ShellExecute(NULL, NULL, (char *) uri, NULL, ".\\", 0);
82 if (!spawn_command_line_async("xdg-email", uri))
83 a_dialog_error_msg ( parent, _("Could not create new email.") );
89 gchar *uri_escape(gchar *str)
91 gchar *esc_str = g_malloc(3*strlen(str));
95 for (src = str; *src; src++) {
98 else if (g_ascii_isalnum(*src))
101 g_sprintf(dst, "%%%02hhX", *src);
111 GList * str_array_to_glist(gchar* data[])
115 for (p = (gpointer)data; *p; p++)
116 gl = g_list_prepend(gl, *p);
117 return g_list_reverse(gl);
121 * split_string_from_file_on_equals:
123 * @buf: the input string
124 * @key: newly allocated string that is before the '='
125 * @val: newly allocated string after the '='
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
131 * e.g. if buf = "GPS.parameter=42"
132 * key = "GPS.parameter"
135 gboolean split_string_from_file_on_equals ( const gchar *buf, gchar **key, gchar **val )
137 // comments, special characters in viking file format
138 if ( buf == NULL || buf[0] == '\0' || buf[0] == '~' || buf[0] == '=' || buf[0] == '#' )
141 if ( ! strchr ( buf, '=' ) )
144 gchar **strv = g_strsplit ( buf, "=", 2 );
147 gchar *str = strv[gi];
150 *key = g_strdup ( str );
152 *val = g_strdup ( str );
159 // Remove newline from val and also any other whitespace
160 *key = g_strstrip ( *key );
161 *val = g_strstrip ( *val );
166 /* 1 << (x) is like a 2**(x) */
167 #define GZ(x) (1<<(x))
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) };
172 static const gint num_scales = (sizeof(scale_mpps) / sizeof(scale_mpps[0]));
174 #define ERROR_MARGIN 0.01
178 * Returns: a zoom level. see : http://wiki.openstreetmap.org/wiki/Zoom_levels
180 guint8 mpp_to_zoom ( gdouble mpp )
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 );
189 return 17; // a safe zoomed in default
193 GtkWindow *window; // Layer needed for redrawing
194 gchar *version; // Image list
195 } new_version_thread_data;
197 static gboolean new_version_available_message ( new_version_thread_data *nvtd )
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/" );
206 // increase amount of time between performing version checks
207 g_free ( nvtd->version );
212 static void latest_version_thread ( GtkWindow *window )
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" );
222 GMappedFile *mf = g_mapped_file_new ( filename, FALSE, NULL );
226 gchar *text = g_mapped_file_get_contents ( mf );
228 gint latest_version = viking_version_to_number ( text );
229 gint my_version = viking_version_to_number ( VIKING_VERSION );
231 g_debug ( "The lastest version is: %s", text );
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 );
240 g_debug ( "Running the lastest version: %s", VIKING_VERSION );
242 g_mapped_file_unref ( mf );
244 g_remove ( filename );
250 * check_latest_version:
251 * @window: Somewhere where we may need use the display to inform the user about the version status
253 * Periodically checks the released latest VERSION file on the website to compare with the running version
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)
260 void check_latest_version ( GtkWindow *window )
263 #if GLIB_CHECK_VERSION (2, 32, 0)
264 g_thread_try_new ( "latest_version_thread", (GThreadFunc)latest_version_thread, window, NULL );
266 g_thread_create ( (GThreadFunc)latest_version_thread, window, FALSE, NULL );
272 * set_auto_features_on_first_run:
274 * Ask the user's opinion to set some of Viking's default behaviour
276 void set_auto_features_on_first_run ( void )
278 gboolean auto_features = FALSE;
279 if ( a_vik_very_first_run () ) {
281 GtkWidget *win = gtk_window_new ( GTK_WINDOW_TOPLEVEL );
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;
288 if ( auto_features ) {
289 // Set Maps to autodownload
290 // Ensure the default is true
291 maps_layer_set_autodownload_default ( TRUE );
293 // Simplistic repeat of preference settings
294 // Only the name & type are important for setting a preference via this 'external' way
296 // Enable auto add map +
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, }, };
301 VikLayerParamData vlp_data;
303 a_preferences_run_setparam ( vlp_data, pref_add_map );
305 vlp_data.u = VIK_STARTUP_METHOD_AUTO_LOCATION;
306 a_preferences_run_setparam ( vlp_data, pref_startup_method );
308 // Ensure settings are saved for next time
309 a_preferences_save_to_file ();