From 2c50be3ea56fc6503ccf962c3faca2f09b7e3c20 Mon Sep 17 00:00:00 2001 From: Andrzej Date: Sun, 11 Mar 2012 18:28:52 +0900 Subject: [PATCH] Added support for imperial units (currently temperature only). --- panel-plugin/weather-data.c | 24 +++++++++++++++++------- panel-plugin/weather-data.h | 2 +- panel-plugin/weather-summary.c | 32 +++++++++++++++++++++++--------- panel-plugin/weather.c | 20 +++++++++++++++----- 4 files changed, 56 insertions(+), 22 deletions(-) diff --git a/panel-plugin/weather-data.c b/panel-plugin/weather-data.c index 5c17a8d..55346cd 100644 --- a/panel-plugin/weather-data.c +++ b/panel-plugin/weather-data.c @@ -25,7 +25,7 @@ #include "weather-data.h" #include "weather.h" -#define CHK_NULL(s) ((s) ? (s):"") +#define CHK_NULL(s) ((s) ? g_strdup(s):g_strdup("")) static gboolean need_interval(datas type) { @@ -33,23 +33,33 @@ static gboolean need_interval(datas type) } const gchar * -get_data (xml_weather *data, datas type) +get_data (xml_weather *data, units unit, datas type) { const xml_time *timeslice = NULL; const xml_location *loc = NULL; + gdouble val; if (data == NULL) - return ""; + return g_strdup(""); timeslice = get_current_timeslice(data, need_interval(type)); if (timeslice == NULL) - return ""; + return g_strdup(""); loc = timeslice->location; switch(type) { case TEMPERATURE: - return CHK_NULL(loc->temperature_value); + { + if (loc->temperature_value == NULL) + return g_strdup(""); + val = atof (loc->temperature_value); + if (unit == IMPERIAL && strcmp(loc->temperature_unit, "celcius") == 0) + val = val * 9.0 / 5.0 + 32.0; + if (unit == METRIC && strcmp(loc->temperature_unit, "fahrenheit") == 0) + val = (val - 32.0) * 5.0 / 9.0; + return g_strdup_printf ("%.1f", val); + } case PRESSURE: return CHK_NULL(loc->pressure_value); case WIND_SPEED: @@ -75,7 +85,7 @@ get_data (xml_weather *data, datas type) case SYMBOL: return CHK_NULL(loc->symbol); } - return ""; + return g_strdup(""); } const gchar * @@ -95,7 +105,7 @@ get_unit (xml_weather *data, units unit, datas type) switch(type) { case TEMPERATURE: - return strcmp(loc->temperature_unit, "celcius") ? "°F":"°C"; + return (unit == IMPERIAL) ? "°F":"°C"; case PRESSURE: return CHK_NULL(loc->pressure_unit); case WIND_SPEED: diff --git a/panel-plugin/weather-data.h b/panel-plugin/weather-data.h index 5e80763..db6ae8b 100644 --- a/panel-plugin/weather-data.h +++ b/panel-plugin/weather-data.h @@ -42,7 +42,7 @@ typedef enum { } units; const gchar * -get_data (xml_weather *data, datas type); +get_data (xml_weather *data, units unit, datas type); const gchar * get_unit (xml_weather *data, units unit, datas type); G_END_DECLS diff --git a/panel-plugin/weather-summary.c b/panel-plugin/weather-summary.c index b0a4aae..7741fdf 100644 --- a/panel-plugin/weather-summary.c +++ b/panel-plugin/weather-summary.c @@ -41,10 +41,12 @@ static gboolean lnk_clicked (GtkTextTag *tag, GObject *obj, #define APPEND_TEXT_ITEM_REAL(text) gtk_text_buffer_insert(GTK_TEXT_BUFFER(buffer), \ &iter, text, -1);\ g_free (value); -#define APPEND_TEXT_ITEM(text, item) value = g_strdup_printf("\t%s%s%s %s\n",\ +#define APPEND_TEXT_ITEM(text, item) str = get_data(data->weatherdata, data->unit, item);\ + value = g_strdup_printf("\t%s%s%s %s\n",\ text, text?": ":"", \ - get_data(data->weatherdata, item),\ + str, \ get_unit(data->weatherdata, data->unit, item));\ + g_free (str);\ APPEND_TEXT_ITEM_REAL(value); #define APPEND_LINK_ITEM(prefix, text, url, lnk_tag) \ gtk_text_buffer_insert(GTK_TEXT_BUFFER(buffer), \ @@ -238,6 +240,7 @@ create_summary_tab (xfceweather_data *data) GtkWidget *weather_channel_icon; gchar *start; xml_time *timeslice; + gchar *str; view = gtk_text_view_new (); gtk_text_view_set_editable (GTK_TEXT_VIEW (view), FALSE); @@ -278,17 +281,25 @@ create_summary_tab (xfceweather_data *data) /* Wind */ APPEND_BTEXT (_("\nWind\n")); - wind = translate_wind_speed (data->weatherdata, get_data (data->weatherdata, WIND_SPEED), data->unit); + str = get_data (data->weatherdata, data->unit, WIND_SPEED); + wind = translate_wind_speed (data->weatherdata, str, data->unit); + g_free (str); + str = get_data (data->weatherdata, data->unit, WIND_BEAUFORT); value = g_strdup_printf ("\t%s: %s (%s on the Beaufort scale)\n", _("Speed"), wind, - get_data (data->weatherdata, WIND_BEAUFORT)); + str); + g_free (str); g_free (wind); APPEND_TEXT_ITEM_REAL (value); - wind = translate_wind_direction (get_data (data->weatherdata, WIND_DIRECTION)); - value = g_strdup_printf ("\t%s: %s (%s%s)\n", _("Direction"), - wind ? wind : get_data (data->weatherdata, WIND_DIRECTION), - get_data (data->weatherdata, WIND_DIRECTION_DEG), + str = get_data (data->weatherdata, data->unit, WIND_DIRECTION); + wind = translate_wind_direction (str); + g_free (str); + if (wind == NULL) + wind = get_data (data->weatherdata, data->unit, WIND_DIRECTION); + str = get_data (data->weatherdata, data->unit, WIND_DIRECTION_DEG); + value = g_strdup_printf ("\t%s: %s (%s%s)\n", _("Direction"), wind, str, get_unit (data->weatherdata, data->unit, WIND_DIRECTION_DEG)); + g_free (str); g_free (wind); APPEND_TEXT_ITEM_REAL (value); @@ -537,6 +548,7 @@ create_summary_window (xfceweather_data *data) GtkWidget *window, *notebook, *vbox; gchar *title; GdkPixbuf *icon; + gchar *str; window = xfce_titled_dialog_new_with_buttons (_("Weather Update"), NULL, @@ -555,7 +567,9 @@ create_summary_window (xfceweather_data *data) gtk_box_pack_start (GTK_BOX (GTK_DIALOG (window)->vbox), vbox, TRUE, TRUE, 0); - icon = get_icon (get_data (data->weatherdata, SYMBOL), 48); + str = get_data (data->weatherdata, data->unit, SYMBOL); + icon = get_icon (str, 48); + g_free (str); if (!icon) icon = get_icon ("99", 48); diff --git a/panel-plugin/weather.c b/panel-plugin/weather.c index 7b75c4b..96ca383 100644 --- a/panel-plugin/weather.c +++ b/panel-plugin/weather.c @@ -153,7 +153,7 @@ make_label (xml_weather *weatherdata, else txtsize = "xx-small"; - rawvalue = get_data (weatherdata, opt); + rawvalue = get_data (weatherdata, unit, opt); switch (opt) { @@ -194,6 +194,7 @@ make_label (xml_weather *weatherdata, txtsize, rawvalue, get_unit (weatherdata, unit, opt)); } } + g_free (rawvalue); return str; } @@ -286,7 +287,9 @@ set_icon_current (xfceweather_data *data) size = data->size; } - icon = get_icon (get_data (data->weatherdata, SYMBOL), size); + str = get_data (data->weatherdata, data->unit, SYMBOL); + icon = get_icon (str, size); + g_free (str); gtk_image_set_from_pixbuf (GTK_IMAGE (data->iconimage), icon); @@ -294,9 +297,11 @@ set_icon_current (xfceweather_data *data) g_object_unref (G_OBJECT (icon)); #if !GTK_CHECK_VERSION(2,12,0) + str = get_data (data->weatherdata, data->unit, SYMBOL); gtk_tooltips_set_tip (data->tooltips, data->tooltipbox, - translate_desc (get_data (data->weatherdata, SYMBOL)), + translate_desc (str), NULL); + g_free (str); #endif } @@ -757,20 +762,25 @@ static gboolean weather_get_tooltip_cb (GtkWidget *widget, { GdkPixbuf *icon; gchar *markup_text; + gchar *str; if (data->weatherdata == NULL) { gtk_tooltip_set_text (tooltip, _("Cannot update weather data")); } else { + str = get_data (data->weatherdata, data->unit, SYMBOL); markup_text = g_markup_printf_escaped( "%s\n" "%s", data->location_name, - translate_desc (get_data (data->weatherdata, SYMBOL)) + translate_desc (str) ); + g_free (str); gtk_tooltip_set_markup (tooltip, markup_text); g_free(markup_text); } - icon = get_icon (get_data (data->weatherdata, SYMBOL), 32); + str = get_data (data->weatherdata, data->unit, SYMBOL); + icon = get_icon (str, 32); + g_free (str); gtk_tooltip_set_icon (tooltip, icon); g_object_unref (G_OBJECT(icon)); -- 1.7.5.4