1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
3 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
5 * Copyright (C) 2013, Rob Norris <rw_norris@hotmail.com>
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; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Dependencies in this file can be on anything.
24 * For functions with simple system dependencies put it in util.c
27 #include <glib/gstdio.h>
28 #include <glib/gi18n.h>
35 #include "preferences.h"
36 #include "vikmapslayer.h"
40 #define FMT_MAX_NUMBER_CODES 9
43 * vu_trackpoint_formatted_message:
44 * @format_code: String describing the message to generate
45 * @trkpt: The trackpoint for which the message is generated about
46 * @trkpt_prev: A trackpoint (presumed previous) for interpolating values with the other trackpoint (such as speed)
47 * @trk: The track in which the trackpoints reside
49 * TODO: One day replace this cryptic format code with some kind of tokenizer parsing
50 * thus would make it more user friendly and maybe even GUI controlable.
51 * However for now at least there is some semblance of user control
53 gchar* vu_trackpoint_formatted_message ( gchar *format_code, VikTrackpoint *trkpt, VikTrackpoint *trkpt_prev, VikTrack *trk )
60 len = strlen ( format_code );
61 if ( len > FMT_MAX_NUMBER_CODES )
62 len = FMT_MAX_NUMBER_CODES;
64 gchar* values[FMT_MAX_NUMBER_CODES];
66 for ( i = 0; i < FMT_MAX_NUMBER_CODES; i++ ) {
70 gchar *speed_units_str = NULL;
71 vik_units_speed_t speed_units = a_vik_get_units_speed ();
72 switch (speed_units) {
73 case VIK_UNITS_SPEED_MILES_PER_HOUR:
74 speed_units_str = g_strdup ( _("mph") );
76 case VIK_UNITS_SPEED_METRES_PER_SECOND:
77 speed_units_str = g_strdup ( _("m/s") );
79 case VIK_UNITS_SPEED_KNOTS:
80 speed_units_str = g_strdup ( _("knots") );
83 // VIK_UNITS_SPEED_KILOMETRES_PER_HOUR:
84 speed_units_str = g_strdup ( _("km/h") );
88 gchar *separator = g_strdup ( " | " );
90 for ( i = 0; i < len; i++ ) {
91 switch ( g_ascii_toupper ( format_code[i] ) ) {
92 case 'G': values[i] = g_strdup ( _("GPSD") ); break; // GPS Preamble
93 case 'K': values[i] = g_strdup ( _("Trkpt") ); break; // Trkpt Preamble
97 gchar *speedtype = NULL;
98 if ( isnan(trkpt->speed) && trkpt_prev ) {
99 if ( trkpt->has_timestamp && trkpt_prev->has_timestamp ) {
100 if ( trkpt->timestamp == trkpt_prev->timestamp ) {
102 // Work out from previous trackpoint location and time difference
103 speed = vik_coord_diff(&(trkpt->coord), &(trkpt_prev->coord)) / ABS(trkpt->timestamp - trkpt_prev->timestamp);
105 switch (speed_units) {
106 case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR:
107 speed = VIK_MPS_TO_KPH(speed);
109 case VIK_UNITS_SPEED_MILES_PER_HOUR:
110 speed = VIK_MPS_TO_MPH(speed);
112 case VIK_UNITS_SPEED_KNOTS:
113 speed = VIK_MPS_TO_KNOTS(speed);
116 // VIK_UNITS_SPEED_METRES_PER_SECOND:
117 // Already in m/s so nothing to do
120 speedtype = g_strdup ( "*" ); // Interpolated
123 speedtype = g_strdup ( "**" );
126 speedtype = g_strdup ( "**" );
129 speed = trkpt->speed;
130 speedtype = g_strdup ( "" );
133 values[i] = g_strdup_printf ( _("%sSpeed%s %.1f%s"), separator, speedtype, speed, speed_units_str );
134 g_free ( speedtype );
139 vik_units_height_t height_units = a_vik_get_units_height ();
140 switch (height_units) {
141 case VIK_UNITS_HEIGHT_FEET:
142 values[i] = g_strdup_printf ( _("%sAlt %dfeet"), separator, (int)round(VIK_METERS_TO_FEET(trkpt->altitude)) );
145 //VIK_UNITS_HEIGHT_METRES:
146 values[i] = g_strdup_printf ( _("%sAlt %dm"), separator, (int)round(trkpt->altitude) );
153 gint heading = isnan(trkpt->course) ? 0 : (gint)round(trkpt->course);
154 values[i] = g_strdup_printf ( _("%sCourse %03d\302\260" ), separator, heading );
160 gint diff = (gint) round ( vik_coord_diff ( &(trkpt->coord), &(trkpt_prev->coord) ) );
162 gchar *dist_units_str = NULL;
163 vik_units_distance_t dist_units = a_vik_get_units_distance ();
164 // expect the difference between track points to be small hence use metres or yards
165 switch (dist_units) {
166 case VIK_UNITS_DISTANCE_MILES:
167 dist_units_str = g_strdup ( _("yards") );
170 // VIK_UNITS_DISTANCE_KILOMETRES:
171 dist_units_str = g_strdup ( _("m") );
175 values[i] = g_strdup_printf ( _("%sDistance diff %d%s"), separator, diff, dist_units_str );
177 g_free ( dist_units_str );
185 if ( trkpt->has_timestamp ) {
186 // Compact date time format
187 strftime (tmp, sizeof(tmp), "%x %X", localtime(&(trkpt->timestamp)));
190 g_snprintf (tmp, sizeof(tmp), "--");
191 values[i] = g_strdup_printf ( _("%sTime %s"), separator, tmp );
197 if ( trkpt->has_timestamp && trkpt_prev->has_timestamp ) {
198 time_t t_diff = trkpt->timestamp - trkpt_prev->timestamp;
199 values[i] = g_strdup_printf ( _("%sTime diff %lds"), separator, t_diff );
205 case 'X': values[i] = g_strdup_printf ( _("%sNo. of Sats %d"), separator, trkpt->nsats ); break;
209 // Distance from start (along the track)
210 gdouble distd = vik_track_get_length_to_trackpoint (trk, trkpt);
211 gchar *dist_units_str = NULL;
212 vik_units_distance_t dist_units = a_vik_get_units_distance ();
213 // expect the difference between track points to be small hence use metres or yards
214 switch (dist_units) {
215 case VIK_UNITS_DISTANCE_MILES:
216 dist_units_str = g_strdup ( _("miles") );
217 distd = VIK_METERS_TO_MILES(distd);
220 // VIK_UNITS_DISTANCE_KILOMETRES:
221 dist_units_str = g_strdup ( _("km") );
222 distd = distd / 1000.0;
225 values[i] = g_strdup_printf ( _("%sDistance along %.2f%s"), separator, distd, dist_units_str );
226 g_free ( dist_units_str );
232 // Location (Lat/Long)
233 gchar *lat = NULL, *lon = NULL;
235 vik_coord_to_latlon (&(trkpt->coord), &ll);
236 a_coords_latlon_to_string ( &ll, &lat, &lon );
237 values[i] = g_strdup_printf ( "%s%s %s", separator, lat, lon );
243 case 'N': // Name of track
244 values[i] = g_strdup_printf ( _("%sTrack: %s"), separator, trk->name );
247 case 'E': // Name of trackpoint if available
249 values[i] = g_strdup_printf ( "%s%s", separator, trkpt->name );
251 values[i] = g_strdup ( "" );
259 g_free ( separator );
260 g_free ( speed_units_str );
262 gchar *msg = g_strconcat ( values[0], values[1], values[2], values[3], values[4], values[5], values[6], values[7], values[8], NULL );
264 for ( i = 0; i < FMT_MAX_NUMBER_CODES; i++ ) {
265 if ( values[i] != '\0' )
266 g_free ( values[i] );
273 GtkWindow *window; // Layer needed for redrawing
274 gchar *version; // Image list
275 } new_version_thread_data;
277 static gboolean new_version_available_message ( new_version_thread_data *nvtd )
279 // Only a simple goto website option is offered
280 // Trying to do an installation update is platform specific
281 if ( a_dialog_yes_or_no ( nvtd->window,
282 _("There is a newer version of Viking available: %s\n\nDo you wish to go to Viking's website now?"), nvtd->version ) )
283 // NB 'VIKING_URL' redirects to the Wiki, here we want to go the main site.
284 open_url ( nvtd->window, "http://sourceforge.net/projects/viking/" );
286 g_free ( nvtd->version );
291 #define VIK_SETTINGS_VERSION_CHECKED_DATE "version_checked_date"
293 static void latest_version_thread ( GtkWindow *window )
295 // Need to allow a few redirects, as SF file is often served from different server
296 DownloadMapOptions options = { FALSE, FALSE, NULL, 5, NULL, NULL, NULL };
297 gchar *filename = a_download_uri_to_tmp_file ( "http://sourceforge.net/projects/viking/files/VERSION", &options );
298 //gchar *filename = g_strdup ( "VERSION" );
303 GMappedFile *mf = g_mapped_file_new ( filename, FALSE, NULL );
307 gchar *text = g_mapped_file_get_contents ( mf );
309 gint latest_version = viking_version_to_number ( text );
310 gint my_version = viking_version_to_number ( VIKING_VERSION );
312 g_debug ( "The lastest version is: %s", text );
314 if ( my_version < latest_version ) {
315 new_version_thread_data *nvtd = g_malloc ( sizeof(new_version_thread_data) );
316 nvtd->window = window;
317 nvtd->version = g_strdup ( text );
318 gdk_threads_add_idle ( (GSourceFunc) new_version_available_message, nvtd );
321 g_debug ( "Running the lastest version: %s", VIKING_VERSION );
323 g_mapped_file_unref ( mf );
325 g_remove ( filename );
329 // Update last checked time
331 g_get_current_time ( &time );
332 a_settings_set_string ( VIK_SETTINGS_VERSION_CHECKED_DATE, g_time_val_to_iso8601(&time) );
335 #define VIK_SETTINGS_VERSION_CHECK_PERIOD "version_check_period_days"
338 * vu_check_latest_version:
339 * @window: Somewhere where we may need use the display to inform the user about the version status
341 * Periodically checks the released latest VERSION file on the website to compare with the running version
344 void vu_check_latest_version ( GtkWindow *window )
346 if ( ! a_vik_get_check_version () )
349 gboolean do_check = FALSE;
352 if ( ! a_settings_get_integer ( VIK_SETTINGS_VERSION_CHECK_PERIOD, &check_period ) ) {
356 // Get last checked date...
357 GDate *gdate_last = g_date_new();
358 GDate *gdate_now = g_date_new();
360 gchar *last_checked_date = NULL;
362 // When no previous date available - set to do the version check
363 if ( a_settings_get_string ( VIK_SETTINGS_VERSION_CHECKED_DATE, &last_checked_date) ) {
364 if ( g_time_val_from_iso8601 ( last_checked_date, &time_last ) ) {
365 g_date_set_time_val ( gdate_last, &time_last );
374 g_get_current_time ( &time_now );
375 g_date_set_time_val ( gdate_now, &time_now );
378 // Dates available so do the comparison
379 g_date_add_days ( gdate_last, check_period );
380 if ( g_date_compare ( gdate_last, gdate_now ) < 0 )
384 g_date_free ( gdate_last );
385 g_date_free ( gdate_now );
388 #if GLIB_CHECK_VERSION (2, 32, 0)
389 g_thread_try_new ( "latest_version_thread", (GThreadFunc)latest_version_thread, window, NULL );
391 g_thread_create ( (GThreadFunc)latest_version_thread, window, FALSE, NULL );
397 * vu_set_auto_features_on_first_run:
399 * Ask the user's opinion to set some of Viking's default behaviour
401 void vu_set_auto_features_on_first_run ( void )
403 gboolean auto_features = FALSE;
404 if ( a_vik_very_first_run () ) {
406 GtkWidget *win = gtk_window_new ( GTK_WINDOW_TOPLEVEL );
408 if ( a_dialog_yes_or_no ( GTK_WINDOW(win),
409 _("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 ) )
410 auto_features = TRUE;
413 if ( auto_features ) {
414 // Set Maps to autodownload
415 // Ensure the default is true
416 maps_layer_set_autodownload_default ( TRUE );
418 // Simplistic repeat of preference settings
419 // Only the name & type are important for setting a preference via this 'external' way
421 // Enable auto add map +
423 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, NULL, NULL, }, };
424 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, NULL, NULL}, };
426 VikLayerParamData vlp_data;
428 a_preferences_run_setparam ( vlp_data, pref_add_map );
430 vlp_data.u = VIK_STARTUP_METHOD_AUTO_LOCATION;
431 a_preferences_run_setparam ( vlp_data, pref_startup_method );
433 // Only on Windows make checking for the latest version on by default
434 // For other systems it's expected a Package manager or similar controls the installation, so leave it off
436 VikLayerParam pref_startup_version_check[] = { { VIK_LAYER_NUM_TYPES, VIKING_PREFERENCES_STARTUP_NAMESPACE "check_version", VIK_LAYER_PARAM_BOOLEAN, VIK_LAYER_GROUP_NONE, NULL, VIK_LAYER_WIDGET_CHECKBUTTON, NULL, NULL, NULL, NULL, }, };
438 a_preferences_run_setparam ( vlp_data, pref_startup_version_check );
441 // Ensure settings are saved for next time
442 a_preferences_save_to_file ();
447 * vu_get_canonical_filename:
449 * Returns: Canonical absolute filename
451 * Any time a path may contain a relative component, so need to prepend that directory it is relative to
452 * Then resolve the full path to get the normal canonical filename
454 gchar *vu_get_canonical_filename ( VikLayer *vl, const gchar *filename )
456 gchar *canonical = NULL;
460 if ( g_path_is_absolute ( filename ) )
461 canonical = g_strdup ( filename );
463 const gchar *vw_filename = vik_window_get_filename ( VIK_WINDOW_FROM_WIDGET (vl->vvp) );
464 gchar *dirpath = NULL;
466 dirpath = g_path_get_dirname ( vw_filename );
468 dirpath = g_get_current_dir(); // Fallback - if here then probably can't create the correct path
471 if ( g_path_is_absolute ( dirpath ) )
472 full = g_strconcat ( dirpath, G_DIR_SEPARATOR_S, filename, NULL );
474 full = g_strconcat ( g_get_current_dir(), G_DIR_SEPARATOR_S, dirpath, G_DIR_SEPARATOR_S, filename, NULL );
476 canonical = file_realpath_dup ( full ); // resolved