]> git.street.me.uk Git - andy/viking.git/blobdiff - src/vikutils.c
Enable a clear icon on entry boxes used for search like input.
[andy/viking.git] / src / vikutils.c
index e90efb4a1121f6da84a17bc2ee074b2f50674226..ac5ec59057a174c920f3e3f316a6901b73ad8f4c 100644 (file)
@@ -318,7 +318,8 @@ gchar* vu_trackpoint_formatted_message ( gchar *format_code, VikTrackpoint *trkp
                }
 
                case 'N': // Name of track
-                       values[i] = g_strdup_printf ( _("%sTrack: %s"), separator, trk->name );
+                       if ( trk )
+                               values[i] = g_strdup_printf ( _("%sTrack: %s"), separator, trk->name );
                        break;
 
                case 'E': // Name of trackpoint if available
@@ -370,7 +371,7 @@ static gboolean new_version_available_message ( new_version_thread_data *nvtd )
 static void latest_version_thread ( GtkWindow *window )
 {
        // Need to allow a few redirects, as SF file is often served from different server
-       DownloadMapOptions options = { FALSE, FALSE, NULL, 5, NULL, NULL, NULL };
+       DownloadFileOptions options = { FALSE, FALSE, NULL, 5, NULL, NULL, NULL };
        gchar *filename = a_download_uri_to_tmp_file ( "http://sourceforge.net/projects/viking/files/VERSION", &options );
        //gchar *filename = g_strdup ( "VERSION" );
        if ( !filename ) {
@@ -571,8 +572,15 @@ gchar *vu_get_canonical_filename ( VikLayer *vl, const gchar *filename )
 
 static struct kdtree *kd = NULL;
 
-static void load_ll_tz_dir ( const gchar *dir )
+/**
+ * load_ll_tz_dir
+ * @dir: The directory from which to load the latlontz.txt file
+ *
+ * Returns: The number of elements within the latlontz.txt loaded
+ */
+static gint load_ll_tz_dir ( const gchar *dir )
 {
+       gint inserted = 0;
        gchar *lltz = g_build_filename ( dir, "latlontz.txt", NULL );
        if ( g_access(lltz, R_OK) == 0 ) {
                gchar buffer[4096];
@@ -588,6 +596,8 @@ static void load_ll_tz_dir ( const gchar *dir )
                                        gchar *timezone = g_strchomp ( components[2] );
                                        if ( kd_insert ( kd, pt, timezone ) )
                                                g_critical ( "Insertion problem of %s for line %ld of latlontz.txt", timezone, line_num );
+                                       else
+                                               inserted++;
                                        // NB Don't free timezone as it's part of the kdtree data now
                                        g_free ( components[0] );
                                        g_free ( components[1] );
@@ -599,13 +609,12 @@ static void load_ll_tz_dir ( const gchar *dir )
                        fclose ( ff );
                }
                else {
-                       g_warning ( "Could not open %s", lltz);
+                       g_warning ( "%s: Could not open %s", __FUNCTION__, lltz);
                }
        }
-       else {
-               g_warning ( "Could not access %s", lltz);
-       }
        g_free ( lltz );
+
+       return inserted;
 }
 
 /**
@@ -623,12 +632,17 @@ void vu_setup_lat_lon_tz_lookup ()
 
        // Look in the directories of data path
        gchar **data_dirs = a_get_viking_data_path();
+       guint loaded = 0;
        // Process directories in reverse order for priority
        guint n_data_dirs = g_strv_length ( data_dirs );
        for (; n_data_dirs > 0; n_data_dirs--) {
-               load_ll_tz_dir(data_dirs[n_data_dirs-1]);
+               loaded += load_ll_tz_dir(data_dirs[n_data_dirs-1]);
        }
        g_strfreev ( data_dirs );
+
+       g_debug ( "%s: Loaded %d elements", __FUNCTION__, loaded );
+       if ( loaded == 0 )
+               g_critical ( "%s: No lat/lon/timezones loaded", __FUNCTION__ );
 }
 
 /**
@@ -667,9 +681,14 @@ static gchar* time_string_adjusted ( time_t *time, gint offset_s )
 static gchar* time_string_tz ( time_t *time, const gchar *format, GTimeZone *tz )
 {
        GDateTime *utc = g_date_time_new_from_unix_utc (*time);
+       if ( !utc ) {
+               g_warning ( "%s: result from g_date_time_new_from_unix_utc() is NULL", __FUNCTION__ );
+               return NULL;
+       }
        GDateTime *local = g_date_time_to_timezone ( utc, tz );
        if ( !local ) {
                g_date_time_unref ( utc );
+               g_warning ( "%s: result from g_date_time_to_timezone() is NULL", __FUNCTION__ );
                return NULL;
        }
        gchar *str = g_date_time_format ( local, format );
@@ -856,3 +875,55 @@ void vu_copy_label_menu ( GtkWidget *widget, guint button )
        gtk_widget_show ( item );
        gtk_menu_popup ( GTK_MENU(menu), NULL, NULL, NULL, NULL, button, gtk_get_current_event_time() );
 }
+
+/**
+ * Work out the best zoom level for the LatLon area and set the viewport to that zoom level
+ */
+void vu_zoom_to_show_latlons ( VikCoordMode mode, VikViewport *vvp, struct LatLon maxmin[2] )
+{
+       /* First set the center [in case previously viewing from elsewhere] */
+       /* Then loop through zoom levels until provided positions are in view */
+       /* This method is not particularly fast - but should work well enough */
+       struct LatLon average = { (maxmin[0].lat+maxmin[1].lat)/2, (maxmin[0].lon+maxmin[1].lon)/2 };
+       VikCoord coord;
+       vik_coord_load_from_latlon ( &coord, mode, &average );
+       vik_viewport_set_center_coord ( vvp, &coord, TRUE );
+
+       /* Convert into definite 'smallest' and 'largest' positions */
+       struct LatLon minmin;
+       if ( maxmin[0].lat < maxmin[1].lat )
+               minmin.lat = maxmin[0].lat;
+       else
+               minmin.lat = maxmin[1].lat;
+
+       struct LatLon maxmax;
+       if ( maxmin[0].lon > maxmin[1].lon )
+               maxmax.lon = maxmin[0].lon;
+       else
+               maxmax.lon = maxmin[1].lon;
+
+       /* Never zoom in too far - generally not that useful, as too close ! */
+       /* Always recalculate the 'best' zoom level */
+       gdouble zoom = 1.0;
+       vik_viewport_set_zoom ( vvp, zoom );
+
+       gdouble min_lat, max_lat, min_lon, max_lon;
+       /* Should only be a maximum of about 18 iterations from min to max zoom levels */
+       while ( zoom <= VIK_VIEWPORT_MAX_ZOOM ) {
+               vik_viewport_get_min_max_lat_lon ( vvp, &min_lat, &max_lat, &min_lon, &max_lon );
+               /* NB I think the logic used in this test to determine if the bounds is within view
+                  fails if track goes across 180 degrees longitude.
+                  Hopefully that situation is not too common...
+                  Mind you viking doesn't really do edge locations to well anyway */
+               if ( min_lat < minmin.lat &&
+                    max_lat > minmin.lat &&
+                    min_lon < maxmax.lon &&
+                    max_lon > maxmax.lon )
+                       /* Found within zoom level */
+                       break;
+
+               /* Try next */
+               zoom = zoom * 2;
+               vik_viewport_set_zoom ( vvp, zoom );
+       }
+}