[Xfce4-commits] <xfce4-weather-plugin:master> Implement GeoNames altitude detection.

Harald Judt noreply at xfce.org
Tue Nov 27 16:46:04 CET 2012


Updating branch refs/heads/master
         to 4220403887e70748535e5c365d200fa34bc430ec (commit)
       from 19a5e323e8f0abedffde3070dbb4b1ef7de6199e (commit)

commit 4220403887e70748535e5c365d200fa34bc430ec
Author: Harald Judt <h.judt at gmx.at>
Date:   Wed Nov 21 14:48:38 2012 +0100

    Implement GeoNames altitude detection.

 panel-plugin/weather-config.c  |   56 +++++++++++++++++++++++++++++++++++++++-
 panel-plugin/weather-parsers.c |   35 +++++++++++++++++++++++++
 panel-plugin/weather-parsers.h |    9 ++++++
 3 files changed, 99 insertions(+), 1 deletions(-)

diff --git a/panel-plugin/weather-config.c b/panel-plugin/weather-config.c
index 82c2ee2..83b9c82 100644
--- a/panel-plugin/weather-config.c
+++ b/panel-plugin/weather-config.c
@@ -26,11 +26,12 @@
 #include "weather-parsers.h"
 #include "weather-data.h"
 #include "weather.h"
-
+#include "weather-debug.h"
 #include "weather-config.h"
 #include "weather-search.h"
 #include "weather-scrollbox.h"
 
+#define GEONAMES_USERNAME "xfce4weatherplugin"
 #define OPTIONS_N 13
 #define BORDER 4
 #define LOC_NAME_MAX_LEN 50
@@ -63,6 +64,8 @@ static const labeloption labeloptions[OPTIONS_N] = {
 
 typedef void (*cb_function) (xfceweather_data *);
 static cb_function cb = NULL;
+typedef void (*cb_conf_dialog_function) (xfceweather_dialog *);
+static cb_conf_dialog_function cb_dialog = NULL;
 
 
 static void
@@ -384,6 +387,55 @@ cb_findlocation(GtkButton *button,
 }
 
 
+static void
+cb_lookup_altitude(SoupSession *session,
+                   SoupMessage *msg,
+                   gpointer user_data)
+{
+    xfceweather_dialog *dialog = (xfceweather_dialog *) user_data;
+    xml_altitude *altitude = NULL;
+    gdouble alt;
+
+    altitude = (xml_altitude *)
+        parse_xml_document(msg, (XmlParseFunc) parse_altitude);
+
+    if (altitude) {
+        alt = string_to_double(altitude->altitude, -9999);
+        weather_debug("Altitude returned by GeoNames: %.0f meters", alt);
+        if (alt != -9999)
+            gtk_spin_button_set_value(GTK_SPIN_BUTTON(dialog->spin_alt),
+                                      alt);
+        xml_altitude_free(altitude);
+    }
+}
+
+
+static gboolean
+button_lookup_altitude_clicked(GtkButton *button,
+                               gpointer user_data)
+{
+    xfceweather_dialog *dialog = (xfceweather_dialog *) user_data;
+    gchar *url, latbuf[10], lonbuf[10];
+    gdouble lat, lon;
+
+    gtk_widget_set_sensitive(GTK_WIDGET(button), FALSE);
+    lat = gtk_spin_button_get_value(GTK_SPIN_BUTTON(dialog->spin_lat));
+    lon = gtk_spin_button_get_value(GTK_SPIN_BUTTON(dialog->spin_lon));
+
+    (void) g_ascii_formatd(latbuf, 10, "%.6f", lat);
+    (void) g_ascii_formatd(lonbuf, 10, "%.6f", lon);
+
+    url = g_strdup_printf("http://api.geonames.org"
+                          "/srtm3XML?lat=%s&lng=%s&username=%s",
+                          &latbuf[0], &lonbuf[0], GEONAMES_USERNAME);
+    weather_http_queue_request(dialog->wd->session, url,
+                               cb_lookup_altitude, user_data);
+    g_free(url);
+    gtk_widget_set_sensitive(GTK_WIDGET(button), TRUE);
+    return FALSE;
+}
+
+
 static GtkWidget *
 create_location_page(xfceweather_dialog *dialog)
 {
@@ -474,6 +526,8 @@ create_location_page(xfceweather_dialog *dialog)
     button_alt_lookup =
         gtk_button_new_with_mnemonic(_("Lookup altitu_de"));
     gtk_size_group_add_widget(sg_button, button_alt_lookup);
+    g_signal_connect(G_OBJECT(button_alt_lookup), "clicked",
+                     G_CALLBACK(button_lookup_altitude_clicked), dialog);
     gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 0, 1);
     gtk_table_attach_defaults(GTK_TABLE(table),
                               dialog->spin_alt, 1, 2, 0, 1);
diff --git a/panel-plugin/weather-parsers.c b/panel-plugin/weather-parsers.c
index 5a27a32..2e473b8 100644
--- a/panel-plugin/weather-parsers.c
+++ b/panel-plugin/weather-parsers.c
@@ -439,6 +439,30 @@ parse_place(xmlNode *cur_node)
 }
 
 
+xml_altitude *
+parse_altitude(xmlNode *cur_node)
+{
+    xml_altitude *alt;
+
+    g_assert(cur_node != NULL);
+    if (G_UNLIKELY(cur_node == NULL))
+        return NULL;
+
+    if (!NODE_IS_TYPE(cur_node, "geonames"))
+        return NULL;
+
+    alt = g_slice_new0(xml_altitude);
+    if (G_UNLIKELY(alt == NULL))
+        return NULL;
+    for (cur_node = cur_node->children; cur_node;
+         cur_node = cur_node->next) {
+        if (NODE_IS_TYPE(cur_node, "srtm3"))
+            alt->altitude = DATA(cur_node);
+    }
+    return alt;
+}
+
+
 xmlDoc *
 get_xml_document(SoupMessage *msg)
 {
@@ -573,3 +597,14 @@ xml_place_free(xml_place *place)
     g_free(place->display_name);
     g_slice_free(xml_place, place);
 }
+
+
+void
+xml_altitude_free(xml_altitude *alt)
+{
+    g_assert(alt != NULL);
+    if (G_UNLIKELY(alt == NULL))
+        return;
+    g_free(alt->altitude);
+    g_slice_free(xml_altitude, alt);
+}
diff --git a/panel-plugin/weather-parsers.h b/panel-plugin/weather-parsers.h
index b853eca..144926f 100644
--- a/panel-plugin/weather-parsers.h
+++ b/panel-plugin/weather-parsers.h
@@ -110,6 +110,11 @@ typedef struct {
 } xml_place;
 
 
+typedef struct {
+    gchar *altitude;
+} xml_altitude;
+
+
 xml_weather *parse_weather(xmlNode *cur_node);
 
 xml_astro *parse_astro(xmlNode *cur_node);
@@ -118,6 +123,8 @@ xml_geolocation *parse_geolocation(xmlNode *cur_node);
 
 xml_place *parse_place(xmlNode *cur_node);
 
+xml_altitude *parse_altitude(xmlNode *cur_node);
+
 xml_time *get_timeslice(xml_weather *data,
                         const time_t start_t,
                         const time_t end_t);
@@ -137,6 +144,8 @@ void xml_geolocation_free(xml_geolocation *geo);
 
 void xml_place_free(xml_place *place);
 
+void xml_altitude_free(xml_altitude *alt);
+
 G_END_DECLS
 
 #endif


More information about the Xfce4-commits mailing list