[Xfce4-commits] <xfce4-weather-plugin:master> Config dialog: Make timezone a string and use GeoNames to detect it.

Harald Judt noreply at xfce.org
Fri Feb 1 16:20:05 CET 2013


Updating branch refs/heads/master
         to 8f7c31f3de85c8a973be2ca3d93eb963de0ef3c3 (commit)
       from bdb0324514f225eba8d23961ed401af922f108a4 (commit)

commit 8f7c31f3de85c8a973be2ca3d93eb963de0ef3c3
Author: Harald Judt <h.judt at gmx.at>
Date:   Thu Jan 31 17:05:18 2013 +0100

    Config dialog: Make timezone a string and use GeoNames to detect it.
    
    GeoNames provides the timezoneId string that can be used to set the TZ
    environment variable. This makes it much easier to fix the timezone problems
    and saves us from all those tricky timezone calculation issues.

 panel-plugin/weather-config.c  |   65 +++++++++++++++++++++++++---------------
 panel-plugin/weather-config.h  |    2 +-
 panel-plugin/weather-debug.c   |   20 ++++--------
 panel-plugin/weather-parsers.c |   38 +++++++++++-----------
 panel-plugin/weather-parsers.h |    9 ++----
 panel-plugin/weather.c         |   16 ++++-----
 panel-plugin/weather.h         |    2 +-
 7 files changed, 79 insertions(+), 73 deletions(-)

diff --git a/panel-plugin/weather-config.c b/panel-plugin/weather-config.c
index e7097d6..eac0fd0 100644
--- a/panel-plugin/weather-config.c
+++ b/panel-plugin/weather-config.c
@@ -35,6 +35,7 @@
 #define OPTIONS_N 15
 #define BORDER 4
 #define LOC_NAME_MAX_LEN 50
+#define TIMEZONE_MAX_LEN 40
 
 #define ADD_PAGE(homogenous)                                        \
     palign = gtk_alignment_new(0.5, 0.5, 1, 1);                     \
@@ -77,8 +78,8 @@
     g_signal_connect(G_OBJECT(button), "clicked",                   \
                      G_CALLBACK(cb_func), dialog);
 
-#define SET_TOOLTIP(widget, text)                           \
-    gtk_widget_set_tooltip_text(GTK_WIDGET(widget), text);
+#define SET_TOOLTIP(widget, markup)                             \
+    gtk_widget_set_tooltip_markup(GTK_WIDGET(widget), markup);
 
 #define TEXT_UNKNOWN(text) (text ? text : "-")
 
@@ -255,19 +256,17 @@ cb_lookup_timezone(SoupSession *session,
 {
     xfceweather_dialog *dialog = (xfceweather_dialog *) user_data;
     xml_timezone *timezone;
-    gdouble tz;
 
     timezone = (xml_timezone *)
         parse_xml_document(msg, (XmlParseFunc) parse_timezone);
     weather_dump(weather_dump_timezone, timezone);
 
     if (timezone) {
-        tz = string_to_double(timezone->offset, -9999);
-        if (tz != -9999)
-            gtk_spin_button_set_value(GTK_SPIN_BUTTON(dialog->spin_timezone),
-                                      tz);
+        gtk_entry_set_text(GTK_ENTRY(dialog->text_timezone),
+                           timezone->timezone_id);
         xml_timezone_free(timezone);
-    }
+    } else
+        gtk_entry_set_text(GTK_ENTRY(dialog->text_timezone), "");
 }
 
 
@@ -293,8 +292,9 @@ lookup_altitude_timezone(const gpointer user_data)
     g_free(url);
 
     /* lookup timezone */
-    url = g_strdup_printf("http://www.earthtools.org/timezone/%s/%s",
-                          latstr, lonstr);
+    url = g_strdup_printf("http://api.geonames.org"
+                          "/timezone?lat=%s&lng=%s&username=%s",
+                          latstr, lonstr, GEONAMES_USERNAME);
     weather_http_queue_request(dialog->pd->session, url,
                                cb_lookup_timezone, user_data);
     g_free(url);
@@ -460,20 +460,21 @@ spin_alt_value_changed(const GtkWidget *spin,
 
 
 static void
-spin_timezone_value_changed(const GtkWidget *spin,
+text_timezone_changed(const GtkWidget *entry,
                             gpointer user_data)
 {
     xfceweather_dialog *dialog = (xfceweather_dialog *) user_data;
 
-    dialog->pd->timezone =
-        gtk_spin_button_get_value(GTK_SPIN_BUTTON(spin));
+    if (dialog->pd->timezone)
+        g_free(dialog->pd->timezone);
+    dialog->pd->timezone = g_strdup(gtk_entry_get_text(GTK_ENTRY(entry)));
 }
 
 
 static GtkWidget *
 create_location_page(xfceweather_dialog *dialog)
 {
-    GtkWidget *palign, *page, *hbox, *vbox, *label, *image;
+    GtkWidget *palign, *page, *hbox, *vbox, *label, *image, *sep;
     GtkWidget *button_loc_change;
     GtkSizeGroup *sg_label, *sg_spin;
 
@@ -568,16 +569,32 @@ create_location_page(xfceweather_dialog *dialog)
     /* timezone */
     hbox = gtk_hbox_new(FALSE, BORDER);
     ADD_LABEL(_("_Timezone:"), sg_label);
-    ADD_SPIN(dialog->spin_timezone, -25, 25, 0.5,
-             dialog->pd->timezone, 1, sg_spin);
+    dialog->text_timezone = gtk_entry_new();
+    gtk_entry_set_max_length(GTK_ENTRY(dialog->text_timezone),
+                             LOC_NAME_MAX_LEN);
+    gtk_label_set_mnemonic_widget(GTK_LABEL(label),
+                                  GTK_WIDGET(dialog->text_timezone));
     SET_TOOLTIP
-        (dialog->spin_timezone,
-         _("If the chosen location is not in your current timezone, this "
-           "value which is auto-detected using the EarthTools web service "
-           "will be used for correcting the time differences. Please adjust "
-           "it manually to accomodate for daylight summer time or if it "
-           "is not correct."));
+        (dialog->text_timezone,
+         _("If the chosen location is not in your current timezone, then "
+           "it is necessary to <i>put</i> the plugin into that other "
+           "timezone for the times to be shown correctly. The proper "
+           "timezone will be auto-detected via the GeoNames web service, "
+           "but you might want to correct it if necessary.\n"
+           "Leave this field empty to use the timezone set by your "
+           "system. Invalid entries will cause the use of UTC time, but "
+           "may also depend on your system."));
+    gtk_box_pack_start(GTK_BOX(hbox), dialog->text_timezone, TRUE, TRUE, 0);
     gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, BORDER);
+    if (dialog->pd->timezone)
+        gtk_entry_set_text(GTK_ENTRY(dialog->text_timezone),
+                           dialog->pd->timezone);
+    else
+        gtk_entry_set_text(GTK_ENTRY(dialog->text_timezone), "");
+
+    /* separator */
+    sep = gtk_hseparator_new();
+    gtk_box_pack_start(GTK_BOX(vbox), sep, FALSE, FALSE, BORDER * 2);
 
     /* instructions for correction of altitude and timezone */
     hbox = gtk_hbox_new(FALSE, BORDER);
@@ -1903,8 +1920,8 @@ setup_notebook_signals(xfceweather_dialog *dialog)
                      G_CALLBACK(spin_lon_value_changed), dialog);
     g_signal_connect(GTK_SPIN_BUTTON(dialog->spin_alt), "value-changed",
                      G_CALLBACK(spin_alt_value_changed), dialog);
-    g_signal_connect(GTK_SPIN_BUTTON(dialog->spin_timezone), "value-changed",
-                     G_CALLBACK(spin_timezone_value_changed), dialog);
+    g_signal_connect(GTK_EDITABLE(dialog->text_timezone), "changed",
+                     G_CALLBACK(text_timezone_changed), dialog);
 
     /* units page */
     g_signal_connect(dialog->combo_unit_temperature, "changed",
diff --git a/panel-plugin/weather-config.h b/panel-plugin/weather-config.h
index 51aed71..edfaeb8 100644
--- a/panel-plugin/weather-config.h
+++ b/panel-plugin/weather-config.h
@@ -38,7 +38,7 @@ typedef struct {
     GtkWidget *spin_lon;
     GtkWidget *spin_alt;
     GtkWidget *label_alt_unit;
-    GtkWidget *spin_timezone;
+    GtkWidget *text_timezone;
     GtkWidget *update_spinner;
 
     /* units page */
diff --git a/panel-plugin/weather-debug.c b/panel-plugin/weather-debug.c
index 4baf9bb..a075ca3 100644
--- a/panel-plugin/weather-debug.c
+++ b/panel-plugin/weather-debug.c
@@ -165,19 +165,13 @@ weather_dump_timezone(const xml_timezone *tz)
 
     out = g_strdup_printf("Timezone data:\n"
                           "  --------------------------------------------\n"
-                          "  offset: %s\n"
-                          "  suffix: %s\n"
-                          "  dst: %s\n"
-                          "  localtime: %s\n"
-                          "  isotime: %s\n"
-                          "  utctime: %s\n"
+                          "  country_code: %s\n"
+                          "  country_name: %s\n"
+                          "  timezone_id: %s\n"
                           "  --------------------------------------------",
-                          tz->offset,
-                          tz->suffix,
-                          tz->dst,
-                          tz->localtime,
-                          tz->isotime,
-                          tz->utctime);
+                          tz->country_code,
+                          tz->country_name,
+                          tz->timezone_id);
     return out;
 }
 
@@ -430,7 +424,7 @@ weather_dump_plugindata(const plugin_data *data)
                            "  latitude: %s\n"
                            "  longitude: %s\n"
                            "  msl: %d\n"
-                           "  timezone: %.1f\n"
+                           "  timezone: %s\n"
                            "  night time: %s\n"
                            "  --------------------------------------------\n"
                            "  icon theme dir: %s\n"
diff --git a/panel-plugin/weather-parsers.c b/panel-plugin/weather-parsers.c
index f51e7cd..c380130 100644
--- a/panel-plugin/weather-parsers.c
+++ b/panel-plugin/weather-parsers.c
@@ -515,26 +515,29 @@ parse_timezone(xmlNode *cur_node)
     xml_timezone *tz;
 
     g_assert(cur_node != NULL);
-    if (G_UNLIKELY(cur_node == NULL) || !NODE_IS_TYPE(cur_node, "timezone"))
+    if (G_UNLIKELY(cur_node == NULL) || !NODE_IS_TYPE(cur_node, "geonames"))
+        return NULL;
+
+    for (cur_node = cur_node->children; cur_node;
+         cur_node = cur_node->next)
+        if (NODE_IS_TYPE(cur_node, "timezone"))
+            break;
+
+    if (G_UNLIKELY(cur_node == NULL))
         return NULL;
 
     tz = g_slice_new0(xml_timezone);
     if (G_UNLIKELY(tz == NULL))
         return NULL;
+
     for (cur_node = cur_node->children; cur_node;
          cur_node = cur_node->next) {
-        if (NODE_IS_TYPE(cur_node, "offset"))
-            tz->offset = DATA(cur_node);
-        if (NODE_IS_TYPE(cur_node, "suffix"))
-            tz->suffix = DATA(cur_node);
-        if (NODE_IS_TYPE(cur_node, "dst"))
-            tz->dst = DATA(cur_node);
-        if (NODE_IS_TYPE(cur_node, "localtime"))
-            tz->localtime = DATA(cur_node);
-        if (NODE_IS_TYPE(cur_node, "isotime"))
-            tz->isotime = DATA(cur_node);
-        if (NODE_IS_TYPE(cur_node, "utctime"))
-            tz->utctime = DATA(cur_node);
+        if (NODE_IS_TYPE(cur_node, "countryCode"))
+            tz->country_code = DATA(cur_node);
+        if (NODE_IS_TYPE(cur_node, "countryName"))
+            tz->country_name = DATA(cur_node);
+        if (NODE_IS_TYPE(cur_node, "timezoneId"))
+            tz->timezone_id = DATA(cur_node);
     }
     return tz;
 }
@@ -787,11 +790,8 @@ xml_timezone_free(xml_timezone *tz)
     g_assert(tz != NULL);
     if (G_UNLIKELY(tz == NULL))
         return;
-    g_free(tz->offset);
-    g_free(tz->suffix);
-    g_free(tz->dst);
-    g_free(tz->localtime);
-    g_free(tz->isotime);
-    g_free(tz->utctime);
+    g_free(tz->country_code);
+    g_free(tz->country_name);
+    g_free(tz->timezone_id);
     g_slice_free(xml_timezone, tz);
 }
diff --git a/panel-plugin/weather-parsers.h b/panel-plugin/weather-parsers.h
index e57f72a..4e00665 100644
--- a/panel-plugin/weather-parsers.h
+++ b/panel-plugin/weather-parsers.h
@@ -112,12 +112,9 @@ typedef struct {
 } xml_altitude;
 
 typedef struct {
-    gchar *offset;
-    gchar *suffix;
-    gchar *dst;
-    gchar *localtime;
-    gchar *isotime;
-    gchar *utctime;
+    gchar *country_code;
+    gchar *country_name;
+    gchar *timezone_id;
 } xml_timezone;
 
 
diff --git a/panel-plugin/weather.c b/panel-plugin/weather.c
index 6ffe5d6..694447a 100644
--- a/panel-plugin/weather.c
+++ b/panel-plugin/weather.c
@@ -773,12 +773,11 @@ xfceweather_read_config(XfcePanelPlugin *plugin,
     data->msl = xfce_rc_read_int_entry(rc, "msl", 0);
     constrain_to_limits(&data->msl, -420, 10000);
 
-    value = xfce_rc_read_entry(rc, "timezone", 0);
-    data->timezone = string_to_double(value, 0);
-    if (data->timezone < -25.0)
-        data->timezone = -25;
-    if (data->timezone > 25.0)
-        data->timezone = 25;
+    value = xfce_rc_read_entry(rc, "timezone", NULL);
+    if (value) {
+        g_free(data->timezone);
+        data->timezone = g_strdup(value);
+    }
 
     data->cache_file_max_age =
         xfce_rc_read_int_entry(rc, "cache_file_max_age", CACHE_FILE_MAX_AGE);
@@ -890,9 +889,7 @@ xfceweather_write_config(XfcePanelPlugin *plugin,
 
     xfce_rc_write_int_entry(rc, "msl", data->msl);
 
-    value = double_to_string(data->timezone, "%.1f");
-    xfce_rc_write_entry(rc, "timezone", value);
-    g_free(value);
+    xfce_rc_write_entry(rc, "timezone", data->timezone);
 
     xfce_rc_write_int_entry(rc, "cache_file_max_age",
                             data->cache_file_max_age);
@@ -1765,6 +1762,7 @@ xfceweather_free(XfcePanelPlugin *plugin,
     g_free(data->lon);
     g_free(data->location_name);
     g_free(data->scrollbox_font);
+    g_free(data->timezone);
 
     /* free update infos */
     g_slice_free(update_info, data->weather_update);
diff --git a/panel-plugin/weather.h b/panel-plugin/weather.h
index 58c2030..317960a 100644
--- a/panel-plugin/weather.h
+++ b/panel-plugin/weather.h
@@ -98,7 +98,7 @@ typedef struct {
     gchar *lat;
     gchar *lon;
     gint msl;
-    gdouble timezone;
+    gchar *timezone;
     gint cache_file_max_age;
     gboolean night_time;
 


More information about the Xfce4-commits mailing list