]> git.street.me.uk Git - andy/viking.git/commitdiff
Add option to control whether a periodic version check should be performed.
authorRob Norris <rw_norris@hotmail.com>
Tue, 11 Jun 2013 20:45:36 +0000 (21:45 +0100)
committerRob Norris <rw_norris@hotmail.com>
Thu, 13 Jun 2013 18:14:43 +0000 (19:14 +0100)
By default this only on for Windows systems.
The period between checking is two weeks.

src/globals.c
src/globals.h
src/util.c

index 8abc2fe88ca2ee101298a24045404bc8532392c6..24328b9076fc4f7cff03b61cf756317076e90280 100644 (file)
@@ -122,6 +122,8 @@ static VikLayerParam startup_prefs[] = {
   { VIK_LAYER_NUM_TYPES, VIKING_PREFERENCES_STARTUP_NAMESPACE "startup_method", VIK_LAYER_PARAM_UINT, VIK_LAYER_GROUP_NONE, N_("Startup Method:"), VIK_LAYER_WIDGET_COMBOBOX, params_startup_methods, NULL, NULL, NULL },
   { VIK_LAYER_NUM_TYPES, VIKING_PREFERENCES_STARTUP_NAMESPACE "startup_file", VIK_LAYER_PARAM_STRING, VIK_LAYER_GROUP_NONE, N_("Startup File:"), VIK_LAYER_WIDGET_FILEENTRY, NULL, NULL,
     N_("The default file to load on startup. Only applies when the startup method is set to 'Specified File'"), NULL, },
+  { VIK_LAYER_NUM_TYPES, VIKING_PREFERENCES_STARTUP_NAMESPACE "check_version", VIK_LAYER_PARAM_BOOLEAN, VIK_LAYER_GROUP_NONE, N_("Check For New Version:"), VIK_LAYER_WIDGET_CHECKBUTTON, NULL, NULL,
+    N_("Periodically check to see if a new version of Viking is available"), NULL, },
 };
 /* End of Options static stuff */
 
@@ -197,6 +199,9 @@ void a_vik_preferences_init ()
   tmp.s = "";
   a_preferences_register(&startup_prefs[3], tmp, VIKING_PREFERENCES_STARTUP_GROUP_KEY);
 
+  tmp.b = FALSE;
+  a_preferences_register(&startup_prefs[4], tmp, VIKING_PREFERENCES_STARTUP_GROUP_KEY);
+
   // New Tab
   a_preferences_register_group ( VIKING_PREFERENCES_IO_GROUP_KEY, _("Export/External") );
 
@@ -358,3 +363,8 @@ const gchar *a_vik_get_startup_file ( )
 {
   return a_preferences_get(VIKING_PREFERENCES_STARTUP_NAMESPACE "startup_file")->s;
 }
+
+gboolean a_vik_get_check_version ( )
+{
+  return a_preferences_get(VIKING_PREFERENCES_STARTUP_NAMESPACE "check_version")->b;
+}
index 6d06cba660746c8b25d2766c728094a746ce867e..b22e9736bea7ecf3ef216dfb3b338f92c649cc92 100644 (file)
@@ -190,6 +190,8 @@ vik_startup_method_t a_vik_get_startup_method ( );
 
 const gchar *a_vik_get_startup_file ( );
 
+gboolean a_vik_get_check_version ( );
+
 /* Group for global preferences */
 #define VIKING_PREFERENCES_GROUP_KEY "viking.globals"
 #define VIKING_PREFERENCES_NAMESPACE "viking.globals."
index 77fe4c311f0bd0ca3871a3e65f4a07e4fe0325d4..b1e9894f361de674349d0e9252b00d706965f413 100644 (file)
@@ -31,6 +31,7 @@
 #include "download.h"
 #include "preferences.h"
 #include "vikmapslayer.h"
+#include "settings.h"
 
 /*
 #ifdef WINDOWS
@@ -202,16 +203,17 @@ static gboolean new_version_available_message ( new_version_thread_data *nvtd )
                             _("There is a newer version of Viking available: %s\n\nDo you wish to go to Viking's website now?"), nvtd->version ) )
     // NB 'VIKING_URL' redirects to the Wiki, here we want to go the main site.
     open_url ( nvtd->window, "http://sourceforge.net/projects/viking/" );
-  //else
-  //  increase amount of time between performing version checks
+
   g_free ( nvtd->version );
   g_free ( nvtd );
   return FALSE;
 }
 
+#define VIK_SETTINGS_VERSION_CHECKED_DATE "version_checked_date"
+
 static void latest_version_thread ( GtkWindow *window )
 {
-  // Need to allow a few of redirects, as SF file is often served from different server
+  // Need to allow a few redirects, as SF file is often served from different server
   DownloadMapOptions options = { FALSE, FALSE, NULL, 5, NULL, NULL };
   gchar *filename = a_download_uri_to_tmp_file ( "http://sourceforge.net/projects/viking/files/VERSION", &options );
   //gchar *filename = g_strdup ( "VERSION" );
@@ -244,28 +246,72 @@ static void latest_version_thread ( GtkWindow *window )
     g_remove ( filename );
     g_free ( filename );
   }
+
+  // Update last checked time
+  GTimeVal time;
+  g_get_current_time ( &time );
+  a_settings_set_string ( VIK_SETTINGS_VERSION_CHECKED_DATE, g_time_val_to_iso8601(&time) );
 }
 
+#define VIK_SETTINGS_VERSION_CHECK_PERIOD "version_check_period_days"
+
 /**
  * check_latest_version:
  * @window: Somewhere where we may need use the display to inform the user about the version status
  *
  * Periodically checks the released latest VERSION file on the website to compare with the running version
  *
- * ATM the plan is for a 1.4.2 release to be always on *just* for Windows platforms
- * Then in 1.5.X it will made entirely optional (default on for Windows)
- *  with a longer periodic check (enabled via state saving using the soon to be released 'settings' code)
- *
  */
 void check_latest_version ( GtkWindow *window )
 {
-#ifdef WINDOWS
+  if ( ! a_vik_get_check_version () )
+    return;
+
+  gboolean do_check = FALSE;
+
+  gint check_period;
+  if ( ! a_settings_get_integer ( VIK_SETTINGS_VERSION_CHECK_PERIOD, &check_period ) ) {
+    check_period = 14;
+  }
+
+  // Get last checked date...
+  GDate *gdate_last = g_date_new();
+  GDate *gdate_now = g_date_new();
+  GTimeVal time_last;
+  gchar *last_checked_date = NULL;
+
+  // When no previous date available - set to do the version check
+  if ( a_settings_get_string ( VIK_SETTINGS_VERSION_CHECKED_DATE, &last_checked_date) ) {
+    if ( g_time_val_from_iso8601 ( last_checked_date, &time_last ) ) {
+      g_date_set_time_val ( gdate_last, &time_last );
+    }
+    else
+      do_check = TRUE;
+  }
+  else
+    do_check = TRUE;
+
+  GTimeVal time_now;
+  g_get_current_time ( &time_now );
+  g_date_set_time_val ( gdate_now, &time_now );
+
+  if ( ! do_check ) {
+    // Dates available so do the comparison
+    g_date_add_days ( gdate_last, check_period );
+    if ( g_date_compare ( gdate_last, gdate_now ) < 0 )
+      do_check = TRUE;
+  }
+
+  g_date_free ( gdate_last );
+  g_date_free ( gdate_now );
+
+  if ( do_check ) {
 #if GLIB_CHECK_VERSION (2, 32, 0)
-  g_thread_try_new ( "latest_version_thread", (GThreadFunc)latest_version_thread, window, NULL );
+    g_thread_try_new ( "latest_version_thread", (GThreadFunc)latest_version_thread, window, NULL );
 #else
-  g_thread_create ( (GThreadFunc)latest_version_thread, window, FALSE, NULL );
-#endif
+    g_thread_create ( (GThreadFunc)latest_version_thread, window, FALSE, NULL );
 #endif
+  }
 }
 
 /**
@@ -305,6 +351,14 @@ void set_auto_features_on_first_run ( void )
     vlp_data.u = VIK_STARTUP_METHOD_AUTO_LOCATION;
     a_preferences_run_setparam ( vlp_data, pref_startup_method );
 
+    // Only on Windows make checking for the latest version on by default
+    // For other systems it's expected a Package manager or similar controls the installation, so leave it off
+#ifdef WINDOWS
+    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, }, };
+    vlp_data.b = TRUE;
+    a_preferences_run_setparam ( vlp_data, pref_startup_version_check );
+#endif
+
     // Ensure settings are saved for next time
     a_preferences_save_to_file ();
   }