From 91fc9c76dda105dff6b422639b0f1f4dc03fcacf Mon Sep 17 00:00:00 2001 From: Rob Norris Date: Tue, 11 Jun 2013 21:45:36 +0100 Subject: [PATCH] Add option to control whether a periodic version check should be performed. By default this only on for Windows systems. The period between checking is two weeks. --- src/globals.c | 10 +++++++ src/globals.h | 2 ++ src/util.c | 76 +++++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 77 insertions(+), 11 deletions(-) diff --git a/src/globals.c b/src/globals.c index 8abc2fe8..24328b90 100644 --- a/src/globals.c +++ b/src/globals.c @@ -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; +} diff --git a/src/globals.h b/src/globals.h index 6d06cba6..b22e9736 100644 --- a/src/globals.h +++ b/src/globals.h @@ -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." diff --git a/src/util.c b/src/util.c index 77fe4c31..b1e9894f 100644 --- a/src/util.c +++ b/src/util.c @@ -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 (); } -- 2.39.5