[Xfce4-commits] <xfce4-weather-plugin:master> Astrological data: Download and parse data.
Harald Judt
noreply at xfce.org
Tue Jul 31 13:26:02 CEST 2012
Updating branch refs/heads/master
to 1294931a98cb360cddd92891247c32f71ec26512 (commit)
from f93f9b9c6df05e9a21c14a9a9e08f56606fa205c (commit)
commit 1294931a98cb360cddd92891247c32f71ec26512
Author: Harald Judt <h.judt at gmx.at>
Date: Tue Jul 31 11:06:21 2012 +0200
Astrological data: Download and parse data.
Download astrological data when not available or on day change.
panel-plugin/weather.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++
panel-plugin/weather.h | 6 ++-
2 files changed, 85 insertions(+), 2 deletions(-)
diff --git a/panel-plugin/weather.c b/panel-plugin/weather.c
index 41f540e..7146bb8 100644
--- a/panel-plugin/weather.c
+++ b/panel-plugin/weather.c
@@ -319,6 +319,45 @@ set_icon_current(xfceweather_data *data)
static void
+cb_astro_update(gboolean succeed,
+ gchar *result,
+ size_t len,
+ gpointer user_data)
+{
+ xfceweather_data *data = user_data;
+ xmlDoc *doc;
+ xmlNode *cur_node;
+ xml_astro *astro = NULL;
+
+ if (G_LIKELY(succeed && result)) {
+ if (g_utf8_validate(result, -1, NULL)) {
+ /* force parsing as UTF-8, the XML encoding header may lie */
+ doc = xmlReadMemory(result, strlen(result), NULL, "UTF-8", 0);
+ } else
+ doc = xmlParseMemory(result, strlen(result));
+
+ g_free(result);
+
+ if (G_LIKELY(doc)) {
+ cur_node = xmlDocGetRootElement(doc);
+
+ if (G_LIKELY(cur_node))
+ astro = parse_astro(cur_node);
+
+ xmlFreeDoc(doc);
+ }
+ }
+
+ if (astro) {
+ if (data->astrodata)
+ xml_astro_free(data->astrodata);
+ data->astrodata = astro;
+ data->last_astro_update = time(NULL);
+ }
+}
+
+
+static void
cb_update(gboolean succeed,
gchar *result,
size_t len,
@@ -365,6 +404,25 @@ cb_update(gboolean succeed,
static gboolean
+need_astro_update(xfceweather_data *data)
+{
+ time_t now_t;
+ struct tm now_tm, last_tm;
+
+ if (!data->updatetimeout || !data->last_astro_update)
+ return TRUE;
+
+ time(&now_t);
+ now_tm = *localtime(&now_t);
+ last_tm = *localtime(&(data->last_astro_update));
+ if (now_tm.tm_mday != last_tm.tm_mday)
+ return TRUE;
+
+ return FALSE;
+}
+
+
+static gboolean
need_data_update(xfceweather_data *data)
{
time_t now_t;
@@ -414,6 +472,26 @@ update_weatherdata(xfceweather_data *data)
return TRUE;
}
+ /* fetch astrological data */
+ if (need_astro_update(data)) {
+ time_t now_t = time(NULL);
+ struct tm now_tm = *localtime(&now_t);
+
+ /* build url */
+ url = g_strdup_printf("/weatherapi/sunrise/1.0/?"
+ "lat=%s;lon=%s;date=%04d-%02d-%02d",
+ data->lat, data->lon,
+ now_tm.tm_year + 1900,
+ now_tm.tm_mon + 1,
+ now_tm.tm_mday);
+
+ /* start receive thread */
+ g_warning("getting http://api.yr.no/%s", url);
+ weather_http_receive_data("api.yr.no", url, data->proxy_host,
+ data->proxy_port, cb_astro_update, data);
+
+ }
+
/* fetch newest XML data */
if (need_data_update(data)) {
/* build url */
@@ -941,6 +1019,9 @@ xfceweather_free(XfcePanelPlugin *plugin,
if (data->weatherdata)
xml_weather_free(data->weatherdata);
+ if (data->astrodata)
+ xml_astro_free(data->astrodata);
+
if (data->updatetimeout) {
g_source_remove(data->updatetimeout);
data->updatetimeout = 0;
diff --git a/panel-plugin/weather.h b/panel-plugin/weather.h
index 6ef86c9..b58132c 100644
--- a/panel-plugin/weather.h
+++ b/panel-plugin/weather.h
@@ -1,5 +1,5 @@
/* Copyright (c) 2003-2007 Xfce Development Team
- *
+ *
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
@@ -49,6 +49,7 @@ typedef struct {
GtkOrientation orientation;
GtkOrientation panel_orientation;
gint updatetimeout;
+ time_t last_astro_update;
time_t last_data_update;
time_t last_conditions_update;
@@ -58,6 +59,7 @@ typedef struct {
unit_systems unit_system;
xml_weather *weatherdata;
+ xml_astro *astrodata;
gchar *proxy_host;
gint proxy_port;
@@ -66,7 +68,7 @@ typedef struct {
/* used for storing the configured but not active proxy settings */
gchar *saved_proxy_host;
gint saved_proxy_port;
-
+
gboolean animation_transitions;
gint forecast_days;
} xfceweather_data;
More information about the Xfce4-commits
mailing list