]> git.street.me.uk Git - andy/viking.git/blame - src/vikutils.c
Fix waypoints may not be drawn when created by paste of a text location.
[andy/viking.git] / src / vikutils.c
CommitLineData
f93e0210
RN
1/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2/*
3 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 *
5 * Copyright (C) 2013, Rob Norris <rw_norris@hotmail.com>
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; either version 2 of the License, or
10 * (at your option) any later version.
11 *
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.
16 *
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
20 *
21 */
5ab2942c
RN
22/*
23 * Dependencies in this file can be on anything.
24 * For functions with simple system dependencies put it in util.c
25 */
f93e0210 26#include <math.h>
5ab2942c 27#include <glib/gstdio.h>
f93e0210 28#include <glib/gi18n.h>
5ab2942c 29#include <gtk/gtk.h>
f93e0210 30
3f31bec4 31#include "viking.h"
f93e0210 32#include "vikutils.h"
5ab2942c
RN
33#include "globals.h"
34#include "download.h"
35#include "preferences.h"
36#include "vikmapslayer.h"
37#include "settings.h"
38#include "util.h"
f93e0210
RN
39
40#define FMT_MAX_NUMBER_CODES 9
41
42/**
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
48 *
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
52 */
53gchar* vu_trackpoint_formatted_message ( gchar *format_code, VikTrackpoint *trkpt, VikTrackpoint *trkpt_prev, VikTrack *trk )
54{
55 if ( !trkpt )
56 return NULL;
57
58 gint len = 0;
59 if ( format_code )
60 len = strlen ( format_code );
61 if ( len > FMT_MAX_NUMBER_CODES )
62 len = FMT_MAX_NUMBER_CODES;
63
64 gchar* values[FMT_MAX_NUMBER_CODES];
65 int i;
66 for ( i = 0; i < FMT_MAX_NUMBER_CODES; i++ ) {
67 values[i] = '\0';
68 }
69
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") );
75 break;
76 case VIK_UNITS_SPEED_METRES_PER_SECOND:
77 speed_units_str = g_strdup ( _("m/s") );
78 break;
79 case VIK_UNITS_SPEED_KNOTS:
80 speed_units_str = g_strdup ( _("knots") );
81 break;
82 default:
83 // VIK_UNITS_SPEED_KILOMETRES_PER_HOUR:
84 speed_units_str = g_strdup ( _("km/h") );
85 break;
86 }
87
88 gchar *separator = g_strdup ( " | " );
89
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
94
95 case 'S': {
96 gdouble speed = 0.0;
97 gchar *speedtype = NULL;
b312b935 98 if ( isnan(trkpt->speed) && trkpt_prev ) {
f93e0210
RN
99 if ( trkpt->has_timestamp && trkpt_prev->has_timestamp ) {
100 if ( trkpt->timestamp == trkpt_prev->timestamp ) {
101
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);
104
105 switch (speed_units) {
106 case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR:
107 speed = VIK_MPS_TO_KPH(speed);
108 break;
109 case VIK_UNITS_SPEED_MILES_PER_HOUR:
110 speed = VIK_MPS_TO_MPH(speed);
111 break;
112 case VIK_UNITS_SPEED_KNOTS:
113 speed = VIK_MPS_TO_KNOTS(speed);
114 break;
115 default:
116 // VIK_UNITS_SPEED_METRES_PER_SECOND:
117 // Already in m/s so nothing to do
118 break;
119 }
120 speedtype = g_strdup ( "*" ); // Interpolated
121 }
122 else
123 speedtype = g_strdup ( "**" );
124 }
125 else
126 speedtype = g_strdup ( "**" );
127 }
128 else {
129 speed = trkpt->speed;
130 speedtype = g_strdup ( "" );
131 }
132
133 values[i] = g_strdup_printf ( _("%sSpeed%s %.1f%s"), separator, speedtype, speed, speed_units_str );
134 g_free ( speedtype );
135 break;
136 }
137
138 case 'A': {
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)) );
143 break;
144 default:
145 //VIK_UNITS_HEIGHT_METRES:
146 values[i] = g_strdup_printf ( _("%sAlt %dm"), separator, (int)round(trkpt->altitude) );
147 break;
148 }
149 break;
150 }
151
152 case 'C': {
153 gint heading = isnan(trkpt->course) ? 0 : (gint)round(trkpt->course);
154 values[i] = g_strdup_printf ( _("%sCourse %03d\302\260" ), separator, heading );
155 break;
156 }
157
158 case 'P': {
159 if ( trkpt_prev ) {
160 gint diff = (gint) round ( vik_coord_diff ( &(trkpt->coord), &(trkpt_prev->coord) ) );
161
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") );
168 break;
169 default:
170 // VIK_UNITS_DISTANCE_KILOMETRES:
171 dist_units_str = g_strdup ( _("m") );
172 break;
173 }
174
175 values[i] = g_strdup_printf ( _("%sDistance diff %d%s"), separator, diff, dist_units_str );
176
177 g_free ( dist_units_str );
178 }
179 break;
180 }
181
182 case 'T': {
183 gchar tmp[64];
184 tmp[0] = '\0';
185 if ( trkpt->has_timestamp ) {
186 // Compact date time format
187 strftime (tmp, sizeof(tmp), "%x %X", localtime(&(trkpt->timestamp)));
188 }
189 else
190 g_snprintf (tmp, sizeof(tmp), "--");
191 values[i] = g_strdup_printf ( _("%sTime %s"), separator, tmp );
192 break;
193 }
194
195 case 'M': {
196 if ( trkpt_prev ) {
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 );
200 }
201 }
202 break;
203 }
204
205 case 'X': values[i] = g_strdup_printf ( _("%sNo. of Sats %d"), separator, trkpt->nsats ); break;
206
207 case 'D': {
208 if ( trk ) {
bb77b487 209 // Distance from start (along the track)
f93e0210
RN
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);
218 break;
219 default:
220 // VIK_UNITS_DISTANCE_KILOMETRES:
221 dist_units_str = g_strdup ( _("km") );
222 distd = distd / 1000.0;
223 break;
224 }
225 values[i] = g_strdup_printf ( _("%sDistance along %.2f%s"), separator, distd, dist_units_str );
226 g_free ( dist_units_str );
227 }
228 break;
229 }
230
231 case 'L': {
232 // Location (Lat/Long)
233 gchar *lat = NULL, *lon = NULL;
234 struct LatLon ll;
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 );
238 g_free ( lat );
239 g_free ( lon );
240 break;
241 }
242
243 case 'N': // Name of track
244 values[i] = g_strdup_printf ( _("%sTrack: %s"), separator, trk->name );
245 break;
246
b45865b4
RN
247 case 'E': // Name of trackpoint if available
248 if ( trkpt->name )
249 values[i] = g_strdup_printf ( "%s%s", separator, trkpt->name );
250 else
251 values[i] = g_strdup ( "" );
252 break;
253
f93e0210
RN
254 default:
255 break;
256 }
257 }
258
259 g_free ( separator );
260 g_free ( speed_units_str );
261
bb77b487 262 gchar *msg = g_strconcat ( values[0], values[1], values[2], values[3], values[4], values[5], values[6], values[7], values[8], NULL );
f93e0210
RN
263
264 for ( i = 0; i < FMT_MAX_NUMBER_CODES; i++ ) {
265 if ( values[i] != '\0' )
266 g_free ( values[i] );
267 }
268
269 return msg;
270}
5ab2942c
RN
271
272typedef struct {
273 GtkWindow *window; // Layer needed for redrawing
274 gchar *version; // Image list
275} new_version_thread_data;
276
277static gboolean new_version_available_message ( new_version_thread_data *nvtd )
278{
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/" );
285
286 g_free ( nvtd->version );
287 g_free ( nvtd );
288 return FALSE;
289}
290
291#define VIK_SETTINGS_VERSION_CHECKED_DATE "version_checked_date"
292
293static void latest_version_thread ( GtkWindow *window )
294{
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" );
299 if ( !filename ) {
300 return;
301 }
302
303 GMappedFile *mf = g_mapped_file_new ( filename, FALSE, NULL );
304 if ( !mf )
305 return;
306
307 gchar *text = g_mapped_file_get_contents ( mf );
308
309 gint latest_version = viking_version_to_number ( text );
310 gint my_version = viking_version_to_number ( VIKING_VERSION );
311
312 g_debug ( "The lastest version is: %s", text );
313
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 );
319 }
320 else
321 g_debug ( "Running the lastest version: %s", VIKING_VERSION );
322
323 g_mapped_file_unref ( mf );
324 if ( filename ) {
325 g_remove ( filename );
326 g_free ( filename );
327 }
328
329 // Update last checked time
330 GTimeVal time;
331 g_get_current_time ( &time );
332 a_settings_set_string ( VIK_SETTINGS_VERSION_CHECKED_DATE, g_time_val_to_iso8601(&time) );
333}
334
335#define VIK_SETTINGS_VERSION_CHECK_PERIOD "version_check_period_days"
336
337/**
338 * vu_check_latest_version:
339 * @window: Somewhere where we may need use the display to inform the user about the version status
340 *
341 * Periodically checks the released latest VERSION file on the website to compare with the running version
342 *
343 */
344void vu_check_latest_version ( GtkWindow *window )
345{
346 if ( ! a_vik_get_check_version () )
347 return;
348
349 gboolean do_check = FALSE;
350
351 gint check_period;
352 if ( ! a_settings_get_integer ( VIK_SETTINGS_VERSION_CHECK_PERIOD, &check_period ) ) {
353 check_period = 14;
354 }
355
356 // Get last checked date...
357 GDate *gdate_last = g_date_new();
358 GDate *gdate_now = g_date_new();
359 GTimeVal time_last;
360 gchar *last_checked_date = NULL;
361
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 );
366 }
367 else
368 do_check = TRUE;
369 }
370 else
371 do_check = TRUE;
372
373 GTimeVal time_now;
374 g_get_current_time ( &time_now );
375 g_date_set_time_val ( gdate_now, &time_now );
376
377 if ( ! do_check ) {
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 )
381 do_check = TRUE;
382 }
383
384 g_date_free ( gdate_last );
385 g_date_free ( gdate_now );
386
387 if ( do_check ) {
388#if GLIB_CHECK_VERSION (2, 32, 0)
389 g_thread_try_new ( "latest_version_thread", (GThreadFunc)latest_version_thread, window, NULL );
390#else
391 g_thread_create ( (GThreadFunc)latest_version_thread, window, FALSE, NULL );
392#endif
393 }
394}
395
396/**
397 * vu_set_auto_features_on_first_run:
398 *
399 * Ask the user's opinion to set some of Viking's default behaviour
400 */
401void vu_set_auto_features_on_first_run ( void )
402{
403 gboolean auto_features = FALSE;
404 if ( a_vik_very_first_run () ) {
405
406 GtkWidget *win = gtk_window_new ( GTK_WINDOW_TOPLEVEL );
407
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;
411 }
412
413 if ( auto_features ) {
414 // Set Maps to autodownload
415 // Ensure the default is true
416 maps_layer_set_autodownload_default ( TRUE );
417
418 // Simplistic repeat of preference settings
419 // Only the name & type are important for setting a preference via this 'external' way
420
421 // Enable auto add map +
422 // Enable IP lookup
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}, };
425
426 VikLayerParamData vlp_data;
427 vlp_data.b = TRUE;
428 a_preferences_run_setparam ( vlp_data, pref_add_map );
429
430 vlp_data.u = VIK_STARTUP_METHOD_AUTO_LOCATION;
431 a_preferences_run_setparam ( vlp_data, pref_startup_method );
432
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
435#ifdef WINDOWS
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, }, };
437 vlp_data.b = TRUE;
438 a_preferences_run_setparam ( vlp_data, pref_startup_version_check );
439#endif
440
441 // Ensure settings are saved for next time
442 a_preferences_save_to_file ();
443 }
444}
1b14d0d2
RN
445
446/**
447 * vu_get_canonical_filename:
448 *
449 * Returns: Canonical absolute filename
450 *
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
453 */
454gchar *vu_get_canonical_filename ( VikLayer *vl, const gchar *filename )
455{
456 gchar *canonical = NULL;
457 if ( !filename )
458 return NULL;
459
460 if ( g_path_is_absolute ( filename ) )
461 canonical = g_strdup ( filename );
462 else {
463 const gchar *vw_filename = vik_window_get_filename ( VIK_WINDOW_FROM_WIDGET (vl->vvp) );
464 gchar *dirpath = NULL;
465 if ( vw_filename )
466 dirpath = g_path_get_dirname ( vw_filename );
467 else
468 dirpath = g_get_current_dir(); // Fallback - if here then probably can't create the correct path
469
470 gchar *full = NULL;
471 if ( g_path_is_absolute ( dirpath ) )
472 full = g_strconcat ( dirpath, G_DIR_SEPARATOR_S, filename, NULL );
473 else
474 full = g_strconcat ( g_get_current_dir(), G_DIR_SEPARATOR_S, dirpath, G_DIR_SEPARATOR_S, filename, NULL );
475
476 canonical = file_realpath_dup ( full ); // resolved
477 g_free ( full );
478 g_free ( dirpath );
479 }
480
481 return canonical;
482}