[Xfce4-commits] <xfce4-weather-plugin:master> Merge new weather data with existing one.

Harald Judt noreply at xfce.org
Sun Dec 9 23:44:09 CET 2012


Updating branch refs/heads/master
         to b500f916eca04815f9eb0ac21da3bc1745375edb (commit)
       from cc9db7bb5fa11519433f5ab351a27c305ef1cdf3 (commit)

commit b500f916eca04815f9eb0ac21da3bc1745375edb
Author: Harald Judt <h.judt at gmx.at>
Date:   Sun Dec 9 19:24:21 2012 +0100

    Merge new weather data with existing one.
    
    Let's overwrite existing timeslice data and append new ones. That way,
    weather data gets updated without removing the existing data we already
    have and want to use.

 panel-plugin/weather-parsers.c |   28 ++++++++++++++++++----------
 panel-plugin/weather-parsers.h |    3 ++-
 panel-plugin/weather.c         |   21 ++++++++++-----------
 3 files changed, 30 insertions(+), 22 deletions(-)

diff --git a/panel-plugin/weather-parsers.c b/panel-plugin/weather-parsers.c
index 2b728d8..fab795d 100644
--- a/panel-plugin/weather-parsers.c
+++ b/panel-plugin/weather-parsers.c
@@ -243,20 +243,28 @@ parse_time(xmlNode *cur_node,
 }
 
 
-xml_weather *
-parse_weather(xmlNode *cur_node)
+/*
+ * Parse XML weather data and merge it with current data.
+ */
+void
+parse_weather(xmlNode *cur_node,
+              xml_weather *wd)
 {
-    xml_weather *wd;
     xmlNode *child_node;
 
-    if (G_UNLIKELY(!NODE_IS_TYPE(cur_node, "weatherdata"))) {
-        return NULL;
-    }
+    if (G_UNLIKELY(wd == NULL))
+        return;
 
-    if ((wd = g_slice_new0(xml_weather)) == NULL)
-        return NULL;
+    if (G_UNLIKELY(!NODE_IS_TYPE(cur_node, "weatherdata")))
+        return;
 
-    wd->timeslices = g_array_sized_new(FALSE, TRUE, sizeof(xml_time *), 200);
+    /* create new timeslices array if it doesn't exist yet, otherwise
+       overwrite existing data */
+    if (wd->timeslices == NULL)
+        wd->timeslices = g_array_sized_new(FALSE, TRUE,
+                                           sizeof(xml_time *), 200);
+    if (wd->timeslices == NULL)
+        return;
 
     for (cur_node = cur_node->children; cur_node; cur_node = cur_node->next) {
         if (cur_node->type != XML_ELEMENT_NODE)
@@ -275,7 +283,7 @@ parse_weather(xmlNode *cur_node)
                     parse_time(child_node, wd);
         }
     }
-    return wd;
+    return;
 }
 
 
diff --git a/panel-plugin/weather-parsers.h b/panel-plugin/weather-parsers.h
index d970632..327d43c 100644
--- a/panel-plugin/weather-parsers.h
+++ b/panel-plugin/weather-parsers.h
@@ -119,7 +119,8 @@ typedef struct {
 } xml_timezone;
 
 
-xml_weather *parse_weather(xmlNode *cur_node);
+void parse_weather(xmlNode *cur_node,
+                   xml_weather *wd);
 
 xml_astro *parse_astro(xmlNode *cur_node);
 
diff --git a/panel-plugin/weather.c b/panel-plugin/weather.c
index afe8796..c60634b 100644
--- a/panel-plugin/weather.c
+++ b/panel-plugin/weather.c
@@ -284,7 +284,6 @@ update_current_conditions(xfceweather_data *data)
     data->weatherdata->current_conditions =
         make_current_conditions(data->weatherdata,
                                 data->last_conditions_update);
-
     data->night_time = is_night_time(data->astrodata);
     update_icon(data);
     update_scrollbox(data);
@@ -317,20 +316,20 @@ cb_weather_update(SoupSession *session,
                   gpointer user_data)
 {
     xfceweather_data *data = user_data;
-    xml_weather *wd = NULL;
-
-    if ((wd = (xml_weather *)
-         parse_xml_document(msg, (XmlParseFunc) parse_weather))) {
-        if (G_LIKELY(data->weatherdata)) {
-            weather_debug("Freeing weather data.");
-            xml_weather_free(data->weatherdata);
+    xmlDoc *doc;
+    xmlNode *root_node;
+
+    doc = get_xml_document(msg);
+    if (G_LIKELY(doc)) {
+        root_node = xmlDocGetRootElement(doc);
+        if (G_LIKELY(root_node)) {
+            parse_weather(root_node, data->weatherdata);
+            data->last_data_update = time(NULL);
         }
-        data->weatherdata = wd;
-        data->last_data_update = time(NULL);
+        xmlFreeDoc(doc);
     }
     weather_debug("Updating current conditions.");
     update_current_conditions(data);
-
     weather_dump(weather_dump_weatherdata, data->weatherdata);
 }
 


More information about the Xfce4-commits mailing list