[Xfce4-commits] <xfce4-weather-plugin:master> Astrological data: Use data for day/night determination.
Harald Judt
noreply at xfce.org
Tue Jul 31 13:26:06 CEST 2012
Updating branch refs/heads/master
to d7786d1dc83e3d8805ebef133defa763edea9663 (commit)
from b862d54887cf7d28b043f72098471fe9eae408fa (commit)
commit d7786d1dc83e3d8805ebef133defa763edea9663
Author: Harald Judt <h.judt at gmx.at>
Date: Tue Jul 31 11:31:19 2012 +0200
Astrological data: Use data for day/night determination.
Finally we can use sunrise and sunset times for determining
whether it is (polar) day or (polar) night. For that, add a
new field to xfceweather_data which can be used in most parts
of the code.
This also brings along a rework of the several update functions,
some smaller fixes and reformatting.
panel-plugin/weather-data.c | 67 +++++++++++++++++++++---------
panel-plugin/weather-data.h | 50 +++++++++++-----------
panel-plugin/weather-summary.c | 2 +-
panel-plugin/weather.c | 87 +++++++++++++++++++++++++---------------
panel-plugin/weather.h | 1 +
5 files changed, 128 insertions(+), 79 deletions(-)
diff --git a/panel-plugin/weather-data.c b/panel-plugin/weather-data.c
index 7636efb..1de961c 100644
--- a/panel-plugin/weather-data.c
+++ b/panel-plugin/weather-data.c
@@ -25,9 +25,14 @@
#include "weather-data.h"
#include "weather.h"
+/* fallback values when astrodata is unavailable */
+#define NIGHT_TIME_START 21
+#define NIGHT_TIME_END 5
+
+
#define CHK_NULL(s) ((s) ? g_strdup(s) : g_strdup(""))
-#define LOCALE_DOUBLE(value, \
- format) (g_strdup_printf(format, \
+#define LOCALE_DOUBLE(value, \
+ format) (g_strdup_printf(format, \
g_ascii_strtod(value, NULL)))
@@ -162,6 +167,46 @@ get_unit(unit_systems unit_system,
/*
+ * Find out whether it's night or day.
+ *
+ * Either use the exact times for sunrise and sunset if
+ * available, or fallback to reasonable arbitrary values.
+ */
+gboolean
+is_night_time(xml_astro *astro)
+{
+ time_t now_t;
+ struct tm now_tm;
+
+ time(&now_t);
+
+ if (G_LIKELY(astro)) {
+ /* Polar night */
+ if (astro->sun_never_rises)
+ return TRUE;
+
+ /* Polar day */
+ if (astro->sun_never_sets)
+ return FALSE;
+
+ /* Sunrise and sunset are known */
+ if (difftime(astro->sunrise, now_t) >= 0)
+ return TRUE;
+
+ if (difftime(astro->sunset, now_t) <= 0)
+ return TRUE;
+
+ return FALSE;
+ }
+
+ /* no astrodata available, use fallback values */
+ now_tm = *localtime(&now_t);
+ return (now_tm.tm_hour >= NIGHT_TIME_START ||
+ now_tm.tm_hour < NIGHT_TIME_END);
+}
+
+
+/*
* Calculate start and end of a daytime interval using given dates.
* We ought to take one of the intervals supplied by the XML feed,
* which gives the most consistent data and does not force too many
@@ -212,24 +257,6 @@ get_current_conditions(xml_weather *data)
}
-/*
- * Check whether it is night or day.
- *
- * FIXME: Until we have a way to get the exact times for sunrise and
- * sunset, we'll have to use reasonable hardcoded values.
- */
-gboolean
-is_night_time()
-{
- time_t now_t;
- struct tm now_tm;
-
- time(&now_t);
- now_tm = *localtime(&now_t);
- return (now_tm.tm_hour >= 21 || now_tm.tm_hour < 5);
-}
-
-
time_t
time_calc(struct tm time_tm,
gint year,
diff --git a/panel-plugin/weather-data.h b/panel-plugin/weather-data.h
index d545c3f..a040f72 100644
--- a/panel-plugin/weather-data.h
+++ b/panel-plugin/weather-data.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
@@ -21,35 +21,35 @@
G_BEGIN_DECLS
typedef enum {
- ALTITUDE,
- LATITUDE,
- LONGITUDE,
- TEMPERATURE,
- PRESSURE,
- WIND_SPEED,
- WIND_BEAUFORT,
- WIND_DIRECTION,
- WIND_DIRECTION_DEG,
- HUMIDITY,
- CLOUDS_LOW,
- CLOUDS_MED,
- CLOUDS_HIGH,
- CLOUDINESS,
- FOG,
- PRECIPITATIONS,
- SYMBOL
+ ALTITUDE,
+ LATITUDE,
+ LONGITUDE,
+ TEMPERATURE,
+ PRESSURE,
+ WIND_SPEED,
+ WIND_BEAUFORT,
+ WIND_DIRECTION,
+ WIND_DIRECTION_DEG,
+ HUMIDITY,
+ CLOUDS_LOW,
+ CLOUDS_MED,
+ CLOUDS_HIGH,
+ CLOUDINESS,
+ FOG,
+ PRECIPITATIONS,
+ SYMBOL
} datas;
typedef enum {
- IMPERIAL,
- METRIC
+ IMPERIAL,
+ METRIC
} unit_systems;
typedef enum {
- MORNING,
- AFTERNOON,
- EVENING,
- NIGHT
+ MORNING,
+ AFTERNOON,
+ EVENING,
+ NIGHT
} daytime;
@@ -60,7 +60,7 @@ gchar *get_data(xml_time *timeslice,
const gchar *get_unit(unit_systems unit_system,
datas type);
-gboolean is_night_time();
+gboolean is_night_time(xml_astro *astro);
time_t time_calc(struct tm time_tm,
gint year,
diff --git a/panel-plugin/weather-summary.c b/panel-plugin/weather-summary.c
index aa40841..26953f9 100644
--- a/panel-plugin/weather-summary.c
+++ b/panel-plugin/weather-summary.c
@@ -767,7 +767,7 @@ create_summary_window (xfceweather_data *data)
conditions = get_current_conditions(data->weatherdata);
rawvalue = get_data(conditions, data->unit_system, SYMBOL);
- icon = get_icon(rawvalue, 48, is_night_time());
+ icon = get_icon(rawvalue, 48, data->night_time);
g_free(rawvalue);
gtk_window_set_icon(GTK_WINDOW(window), icon);
diff --git a/panel-plugin/weather.c b/panel-plugin/weather.c
index 7146bb8..958f03f 100644
--- a/panel-plugin/weather.c
+++ b/panel-plugin/weather.c
@@ -41,6 +41,7 @@
#define DATA_MAX_AGE (3 * 3600)
#define BORDER 8
+
#if !GLIB_CHECK_VERSION(2,14,0)
#define g_timeout_add_seconds(t, c, d) g_timeout_add((t) * 1000, (c), (d))
#endif
@@ -267,7 +268,6 @@ set_icon_current(xfceweather_data *data)
datas opt;
gchar *str;
gint size, height;
- gboolean nighttime;
for (i = 0; i < data->labels->len; i++) {
opt = g_array_index(data->labels, datas, i);
@@ -297,10 +297,9 @@ set_icon_current(xfceweather_data *data)
/* get current weather conditions */
conditions = get_current_conditions(data->weatherdata);
- nighttime = is_night_time();
str = get_data(conditions, data->unit_system, SYMBOL);
- icon = get_icon(str, size, nighttime);
+ icon = get_icon(str, size, data->night_time);
g_free(str);
gtk_image_set_from_pixbuf(GTK_IMAGE(data->iconimage), icon);
@@ -311,7 +310,7 @@ set_icon_current(xfceweather_data *data)
#if !GTK_CHECK_VERSION(2,12,0)
str = get_data(conditions, data->unit_system, SYMBOL);
gtk_tooltips_set_tip(data->tooltips, data->tooltipbox,
- translate_desc(str, nighttime),
+ translate_desc(str, data->night_time),
NULL);
g_free(str);
#endif
@@ -319,6 +318,26 @@ set_icon_current(xfceweather_data *data)
static void
+update_current_conditions(xfceweather_data *data)
+{
+ if (G_UNLIKELY(data == NULL) ||
+ G_UNLIKELY(data->weatherdata == NULL)) {
+ set_icon_error(data);
+ return;
+ }
+
+ if (data->weatherdata->current_conditions)
+ xml_time_free(data->weatherdata->current_conditions);
+
+ data->weatherdata->current_conditions =
+ make_current_conditions(data->weatherdata);
+ data->last_conditions_update = time(NULL);
+ data->night_time = is_night_time(data->astrodata);
+ set_icon_current(data);
+}
+
+
+static void
cb_astro_update(gboolean succeed,
gchar *result,
size_t len,
@@ -389,17 +408,12 @@ cb_update(gboolean succeed,
gtk_scrollbox_clear(GTK_SCROLLBOX(data->scrollbox));
- if (weather) {
- weather->current_conditions = make_current_conditions(weather);
- if (data->weatherdata)
- xml_weather_free(data->weatherdata);
+ if (weather) {
data->weatherdata = weather;
data->last_data_update = time(NULL);
- data->last_conditions_update = time(NULL);
- set_icon_current(data);
- } else
- set_icon_error(data);
+ }
+ update_current_conditions(data);
}
@@ -428,7 +442,7 @@ need_data_update(xfceweather_data *data)
time_t now_t;
gint diff;
- if (! data->updatetimeout || ! data->last_data_update)
+ if (!data->updatetimeout || !data->last_data_update)
return TRUE;
time(&now_t);
@@ -446,7 +460,7 @@ need_conditions_update(xfceweather_data *data)
time_t now_t;
struct tm now_tm, last_tm;
- if (! data->updatetimeout || ! data->last_conditions_update)
+ if (!data->updatetimeout || !data->last_conditions_update)
return TRUE;
time(&now_t);
@@ -464,6 +478,11 @@ static gboolean
update_weatherdata(xfceweather_data *data)
{
gchar *url;
+ gboolean night_time;
+
+ g_assert(data != NULL);
+ if (G_UNLIKELY(data == NULL))
+ return TRUE;
if ((!data->lat || !data->lon) ||
strlen(data->lat) == 0 || strlen(data->lon) == 0) {
@@ -490,9 +509,10 @@ update_weatherdata(xfceweather_data *data)
weather_http_receive_data("api.yr.no", url, data->proxy_host,
data->proxy_port, cb_astro_update, data);
+ g_free(url);
}
- /* fetch newest XML data */
+ /* fetch weather data */
if (need_data_update(data)) {
/* build url */
url =
@@ -506,17 +526,19 @@ update_weatherdata(xfceweather_data *data)
/* cleanup */
g_free(url);
- } else if (need_conditions_update(data)) {
- /* update current conditions, icon and labels */
- if (data->weatherdata) {
- if (data->weatherdata->current_conditions)
- xml_time_free(data->weatherdata->current_conditions);
-
- data->weatherdata->current_conditions =
- make_current_conditions(data->weatherdata);
- data->last_conditions_update = time(NULL);
+ }
+
+ /* update current conditions, icon and labels */
+ if (need_conditions_update(data))
+ update_current_conditions(data);
+
+ /* update night time status and icon */
+ night_time = is_night_time(data->astrodata);
+ if (data->night_time != night_time) {
+ data->night_time = night_time;
+ if (data->weatherdata)
set_icon_current(data);
- } else
+ else
set_icon_error(data);
}
@@ -860,27 +882,26 @@ static gboolean weather_get_tooltip_cb(GtkWidget *widget,
GdkPixbuf *icon;
gchar *markup_text, *rawvalue;
xml_time *conditions;
- gboolean nighttime;
conditions = get_current_conditions(data->weatherdata);
- nighttime = is_night_time();
if (data->weatherdata == NULL)
gtk_tooltip_set_text(tooltip, _("Cannot update weather data"));
else {
rawvalue = get_data(conditions, data->unit_system, SYMBOL);
- markup_text = g_markup_printf_escaped("<b>%s</b>\n"
- "%s",
- data->location_name,
- translate_desc(rawvalue,
- nighttime));
+ markup_text =
+ g_markup_printf_escaped("<b>%s</b>\n"
+ "%s",
+ data->location_name,
+ translate_desc(rawvalue,
+ data->night_time));
g_free(rawvalue);
gtk_tooltip_set_markup(tooltip, markup_text);
g_free(markup_text);
}
rawvalue = get_data(conditions, data->unit_system, SYMBOL);
- icon = get_icon(rawvalue, 32, nighttime);
+ icon = get_icon(rawvalue, 32, data->night_time);
g_free(rawvalue);
gtk_tooltip_set_icon(tooltip, icon);
g_object_unref(G_OBJECT(icon));
diff --git a/panel-plugin/weather.h b/panel-plugin/weather.h
index b58132c..872606c 100644
--- a/panel-plugin/weather.h
+++ b/panel-plugin/weather.h
@@ -60,6 +60,7 @@ typedef struct {
xml_weather *weatherdata;
xml_astro *astrodata;
+ gboolean night_time;
gchar *proxy_host;
gint proxy_port;
More information about the Xfce4-commits
mailing list