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>
39 static gboolean spawn_command_line_async(const gchar * cmd,
42 gchar *cmdline = NULL;
45 cmdline = g_strdup_printf("%s '%s'", cmd, arg);
46 g_debug("Running: %s", cmdline);
48 status = g_spawn_command_line_async(cmdline, NULL);
57 void open_url(GtkWindow *parent, const gchar * url)
60 gtk_show_uri ( gtk_widget_get_screen (GTK_WIDGET(parent)), url, GDK_CURRENT_TIME, &error );
62 a_dialog_error_msg_extra ( parent, _("Could not launch web browser. %s"), error->message );
63 g_error_free ( error );
67 void new_email(GtkWindow *parent, const gchar * address)
69 gchar *uri = g_strdup_printf("mailto:%s", address);
71 gtk_show_uri ( gtk_widget_get_screen (GTK_WIDGET(parent)), uri, GDK_CURRENT_TIME, &error );
73 a_dialog_error_msg_extra ( parent, _("Could not create new email. %s"), error->message );
74 g_error_free ( error );
78 ShellExecute(NULL, NULL, (char *) uri, NULL, ".\\", 0);
80 if (!spawn_command_line_async("xdg-email", uri))
81 a_dialog_error_msg ( parent, _("Could not create new email.") );
87 gchar *uri_escape(gchar *str)
89 gchar *esc_str = g_malloc(3*strlen(str));
93 for (src = str; *src; src++) {
96 else if (g_ascii_isalnum(*src))
99 g_sprintf(dst, "%%%02hhX", *src);
109 GList * str_array_to_glist(gchar* data[])
113 for (p = (gpointer)data; *p; p++)
114 gl = g_list_prepend(gl, *p);
115 return g_list_reverse(gl);
119 * split_string_from_file_on_equals:
121 * @buf: the input string
122 * @key: newly allocated string that is before the '='
123 * @val: newly allocated string after the '='
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
129 * e.g. if buf = "GPS.parameter=42"
130 * key = "GPS.parameter"
133 gboolean split_string_from_file_on_equals ( const gchar *buf, gchar **key, gchar **val )
135 // comments, special characters in viking file format
136 if ( buf == NULL || buf[0] == '\0' || buf[0] == '~' || buf[0] == '=' || buf[0] == '#' )
139 if ( ! strchr ( buf, '=' ) )
142 gchar **strv = g_strsplit ( buf, "=", 2 );
145 gchar *str = strv[gi];
148 *key = g_strdup ( str );
150 *val = g_strdup ( str );
157 // Remove newline from val and also any other whitespace
158 *key = g_strstrip ( *key );
159 *val = g_strstrip ( *val );
164 /* 1 << (x) is like a 2**(x) */
165 #define GZ(x) (1<<(x))
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) };
170 static const gint num_scales = (sizeof(scale_mpps) / sizeof(scale_mpps[0]));
172 #define ERROR_MARGIN 0.01
176 * Returns: a zoom level. see : http://wiki.openstreetmap.org/wiki/Zoom_levels
178 guint8 mpp_to_zoom ( gdouble mpp )
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 );
187 return 17; // a safe zoomed in default
191 GtkWindow *window; // Layer needed for redrawing
192 gchar *version; // Image list
193 } new_version_thread_data;
195 static gboolean new_version_available_message ( new_version_thread_data *nvtd )
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/" );
204 // increase amount of time between performing version checks
205 g_free ( nvtd->version );
210 static void latest_version_thread ( GtkWindow *window )
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" );
220 GMappedFile *mf = g_mapped_file_new ( filename, FALSE, NULL );
224 gchar *text = g_mapped_file_get_contents ( mf );
226 gint latest_version = viking_version_to_number ( text );
227 gint my_version = viking_version_to_number ( VIKING_VERSION );
229 g_debug ( "The lastest version is: %s", text );
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 );
238 g_debug ( "Running the lastest version: %s", VIKING_VERSION );
240 g_mapped_file_unref ( mf );
242 g_remove ( filename );
248 * check_latest_version:
249 * @window: Somewhere where we may need use the display to inform the user about the version status
251 * Periodically checks the released latest VERSION file on the website to compare with the running version
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)
258 void check_latest_version ( GtkWindow *window )
261 #if GLIB_CHECK_VERSION (2, 32, 0)
262 g_thread_try_new ( "latest_version_thread", (GThreadFunc)latest_version_thread, window, NULL );
264 g_thread_create ( (GThreadFunc)latest_version_thread, window, FALSE, NULL );