[Xfce4-commits] <xfce4-weather-plugin:master> Schedule data update after location parameter changes.

Harald Judt noreply at xfce.org
Wed Nov 28 00:00:01 CET 2012


Updating branch refs/heads/master
         to 2466b5e5044155a356aeaba573312e8a596a6b9a (commit)
       from f7365ac642c74cefa7dd946852196e6296a0e946 (commit)

commit 2466b5e5044155a356aeaba573312e8a596a6b9a
Author: Harald Judt <h.judt at gmx.at>
Date:   Tue Nov 27 22:27:38 2012 +0100

    Schedule data update after location parameter changes.
    
    When the user changes any of the lat/lon/alt settings, then new weather
    and astro data need to be downloaded. Of course, the user needs some time
    to make changes, so an update should not occur too often because that could
    send many requests to the webservices. To minimize these requests, install
    a timeout that gets reset whenever the user changes these settings.
    
    Note that the main update routine has an update timeout too that may add
    to this timeout, so the download will not happen immediately after this
    timeout expires.

 panel-plugin/weather-config.c |   43 +++++++++++++++++++++++++++++++++++++++-
 panel-plugin/weather-config.h |    4 +-
 2 files changed, 43 insertions(+), 4 deletions(-)

diff --git a/panel-plugin/weather-config.c b/panel-plugin/weather-config.c
index 3fdfc43..6ab95ec 100644
--- a/panel-plugin/weather-config.c
+++ b/panel-plugin/weather-config.c
@@ -32,6 +32,7 @@
 #include "weather-scrollbox.h"
 
 #define GEONAMES_USERNAME "xfce4weatherplugin"
+#define UPDATE_TIMER_DELAY 3
 #define OPTIONS_N 13
 #define BORDER 4
 #define LOC_NAME_MAX_LEN 50
@@ -109,6 +110,39 @@ typedef void (*cb_conf_dialog_function) (xfceweather_dialog *);
 static cb_conf_dialog_function cb_dialog = NULL;
 
 
+static gboolean
+schedule_data_update(gpointer user_data)
+{
+    xfceweather_dialog *dialog = (xfceweather_dialog *) user_data;
+    xfceweather_data *data;
+
+    if (dialog == NULL)
+        return FALSE;
+
+    weather_debug("Delayed update timer expired, now scheduling data update.");
+
+    /* force update of downloaded data, download will be performed by
+     * main routine */
+    data = dialog->wd;
+    memset(&data->last_data_update, 0, sizeof(data->last_data_update));
+    memset(&data->last_astro_update, 0, sizeof(data->last_astro_update));
+    return FALSE;
+}
+
+
+static void
+schedule_delayed_data_update(xfceweather_dialog *dialog)
+{
+    weather_debug("Starting delayed data update.");
+    if (dialog->timer_id)
+        g_source_remove(dialog->timer_id);
+
+    dialog->timer_id =
+        g_timeout_add_seconds(UPDATE_TIMER_DELAY,
+                              (GSourceFunc) schedule_data_update, dialog);
+}
+
+
 static gchar *
 sanitize_location_name(const gchar *location_name)
 {
@@ -334,13 +368,14 @@ static void
 spin_lat_value_changed(const GtkWidget *spin,
                        gpointer user_data)
 {
+    xfceweather_dialog *dialog = (xfceweather_dialog *) user_data;
     gchar latbuf[10];
     gdouble val;
 
-    xfceweather_dialog *dialog = (xfceweather_dialog *) user_data;
     val = gtk_spin_button_get_value(GTK_SPIN_BUTTON(spin));
     g_free(dialog->wd->lat);
     dialog->wd->lat = g_strdup(g_ascii_formatd(latbuf, 10, "%.6f", val));
+    schedule_delayed_data_update(dialog);
 }
 
 
@@ -348,13 +383,14 @@ static void
 spin_lon_value_changed(const GtkWidget *spin,
                        gpointer user_data)
 {
+    xfceweather_dialog *dialog = (xfceweather_dialog *) user_data;
     gchar lonbuf[10];
     gdouble val;
 
-    xfceweather_dialog *dialog = (xfceweather_dialog *) user_data;
     val = gtk_spin_button_get_value(GTK_SPIN_BUTTON(spin));
     g_free(dialog->wd->lon);
     dialog->wd->lon = g_strdup(g_ascii_formatd(lonbuf, 10, "%.6f", val));
+    schedule_delayed_data_update(dialog);
 }
 
 
@@ -364,10 +400,12 @@ spin_alt_value_changed(const GtkWidget *spin,
 {
     xfceweather_dialog *dialog = (xfceweather_dialog *) user_data;
     gdouble val;
+
     val = gtk_spin_button_get_value(GTK_SPIN_BUTTON(spin));
     if (dialog->wd->units->altitude == FEET)
         val *= 0.3048;
     dialog->wd->msl = (gint) val;
+    schedule_delayed_data_update(dialog);
 }
 
 
@@ -376,6 +414,7 @@ spin_timezone_value_changed(const GtkWidget *spin,
                             gpointer user_data)
 {
     xfceweather_dialog *dialog = (xfceweather_dialog *) user_data;
+
     dialog->wd->timezone =
         gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(spin));
 }
diff --git a/panel-plugin/weather-config.h b/panel-plugin/weather-config.h
index b54606f..4ad5cee 100644
--- a/panel-plugin/weather-config.h
+++ b/panel-plugin/weather-config.h
@@ -28,6 +28,8 @@ typedef struct {
 
 typedef struct {
     GtkWidget *dialog;
+    xfceweather_data *wd;
+    guint timer_id;
 
     /* location page */
     GtkWidget *text_loc_name;
@@ -63,8 +65,6 @@ typedef struct {
     GtkWidget *list_datatypes;           /* labels to show */
     GtkListStore *model_datatypes;       /* model for labels */
     GtkWidget *check_scrollbox_animate;
-
-    xfceweather_data *wd;
 } xfceweather_dialog;
 
 


More information about the Xfce4-commits mailing list