[Xfce4-commits] <xfce4-weather-plugin:master> Locale support, part 1: Duplicate strings.

Harald Judt noreply at xfce.org
Wed Jul 4 18:30:01 CEST 2012


Updating branch refs/heads/master
         to bbedf98a21a4540a1a0bb7d5b1e5862cf7dd2878 (commit)
       from e278dfc3006b3f4bbba8c4d2c04a3269b358040d (commit)

commit bbedf98a21a4540a1a0bb7d5b1e5862cf7dd2878
Author: Harald Judt <h.judt at gmx.at>
Date:   Wed Jul 4 17:35:03 2012 +0200

    Locale support, part 1: Duplicate strings.
    
    Values like temperature, wind speed etc. are returned as immutable
    strings. That's bad because those values are not printed in the
    format defined by the user's locale.
    
    As a first step, duplicate the strings in get_data() and free them
    in the calling functions. Remove usage of CHK_NULL from get_unit(),
    as it would create a memory leak.

 panel-plugin/weather-data.c    |   14 ++++----
 panel-plugin/weather-data.h    |    2 +-
 panel-plugin/weather-summary.c |   79 ++++++++++++++++++++++++----------------
 panel-plugin/weather.c         |   26 +++++++++----
 4 files changed, 74 insertions(+), 47 deletions(-)

diff --git a/panel-plugin/weather-data.c b/panel-plugin/weather-data.c
index b9290cb..73a7929 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(""))
 
 gboolean has_timeslice(xml_weather *data, time_t start, time_t end)
 {
@@ -38,13 +38,13 @@ gboolean has_timeslice(xml_weather *data, time_t start, time_t end)
     return FALSE;
 }
 
-const gchar *
+gchar *
 get_data (xml_time *timeslice, datas type)
 {
 	const xml_location *loc = NULL;
 
 	if (timeslice == NULL)
-		return "";
+		return g_strdup("");
 
 	loc = timeslice->location;
 
@@ -60,9 +60,9 @@ get_data (xml_time *timeslice, datas type)
 	case PRESSURE:
 		return CHK_NULL(loc->pressure_value);
 	case WIND_SPEED:
-		return CHK_NULL( loc->wind_speed_mps);
+		return CHK_NULL(loc->wind_speed_mps);
 	case WIND_BEAUFORT:
-		return CHK_NULL( loc->wind_speed_beaufort);
+		return CHK_NULL(loc->wind_speed_beaufort);
 	case WIND_DIRECTION:
 		return CHK_NULL(loc->wind_dir_name);
 	case WIND_DIRECTION_DEG:
@@ -84,7 +84,7 @@ get_data (xml_time *timeslice, datas type)
 	case SYMBOL:
 		return CHK_NULL(loc->symbol);
 	}
-	return "";
+	return g_strdup("");
 }
 
 const gchar *
@@ -103,7 +103,7 @@ get_unit (xml_time *timeslice, units unit, datas type)
 	case TEMPERATURE:
 		return strcmp(loc->temperature_unit, "celcius") ? "°F":"°C";
 	case PRESSURE:
-		return CHK_NULL(loc->pressure_unit);
+		return (loc->pressure_unit) ? loc->pressure_unit : "";
 	case WIND_SPEED:
 		return "m/s";
 	case WIND_DIRECTION_DEG:
diff --git a/panel-plugin/weather-data.h b/panel-plugin/weather-data.h
index 190d726..b43a3fb 100644
--- a/panel-plugin/weather-data.h
+++ b/panel-plugin/weather-data.h
@@ -52,7 +52,7 @@ typedef enum {
 	NIGHT
 } daytime;
 
-const gchar *
+gchar *
 get_data (xml_time *timeslice, datas type);
 const gchar *
 get_unit (xml_time *timeslice, units unit, datas type);
diff --git a/panel-plugin/weather-summary.c b/panel-plugin/weather-summary.c
index d250edd..0466775 100644
--- a/panel-plugin/weather-summary.c
+++ b/panel-plugin/weather-summary.c
@@ -41,10 +41,14 @@ 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",\
-                                                                 text, text?": ":"", \
-                                                                 get_data(timeslice, item), \
-                                                                 get_unit(timeslice, data->unit, item)); \
+#define APPEND_TEXT_ITEM(text, item)     rawvalue = get_data(timeslice, item); \
+                                         unit = get_unit(timeslice, data->unit, item); \
+                                         value = g_strdup_printf("\t%s%s%s%s%s\n", \
+                                                                 text, text ? ": " : "", \
+                                                                 rawvalue, \
+                                                                 strcmp(unit, "°") ? " " : "", \
+                                                                 unit); \
+                                         g_free (rawvalue);\
                                          APPEND_TEXT_ITEM_REAL(value);
 #define APPEND_LINK_ITEM(prefix, text, url, lnk_tag) \
 					 gtk_text_buffer_insert(GTK_TEXT_BUFFER(buffer), \
@@ -231,12 +235,12 @@ create_summary_tab (xfceweather_data *data)
   GtkTextBuffer *buffer;
   GtkTextIter    iter;
   GtkTextTag    *btag, *ltag1;
-  gchar         *value, *wind, *sun_val, *vis;
+  gchar         *value, *wind, *sun_val, *vis, *rawvalue;
+  const gchar   *unit;
   GtkWidget     *view, *frame, *scrolled;
   GdkColor       lnk_color;
   GtkAdjustment *adj;
   GtkWidget     *weather_channel_icon;
-  gchar         *start;
   xml_time      *timeslice;
 
   view = gtk_text_view_new ();
@@ -270,17 +274,11 @@ create_summary_tab (xfceweather_data *data)
 
   timeslice = get_current_timeslice(data->weatherdata, FALSE);
   APPEND_BTEXT(_("Coordinates and Time\n"));
-  value = g_strdup_printf (_("\tAltitude: %s %s\n"
-                             "\tLatitude: %s%s\n"
-                             "\tLongitude: %s%s\n\n"
-                             "\tData applies to time interval\n"
-                             "\tfrom %s\tto %s"),
-                           get_data(timeslice, ALTITUDE),
-                           get_unit(timeslice, data->unit, ALTITUDE),
-                           get_data(timeslice, LATITUDE),
-                           get_unit(timeslice, data->unit, LATITUDE),
-                           get_data(timeslice, LONGITUDE),
-                           get_unit(timeslice, data->unit, LONGITUDE),
+  APPEND_TEXT_ITEM (_("Altitude"), ALTITUDE);
+  APPEND_TEXT_ITEM (_("Latitude"), LATITUDE);
+  APPEND_TEXT_ITEM (_("Longitude"), LONGITUDE);
+
+  value = g_strdup_printf (_("\n\tData applies to time interval\n\tfrom %s\tto %s"),
                            ctime(&timeslice->start),
                            ctime(&timeslice->end));
   APPEND_TEXT_ITEM_REAL (value);
@@ -291,17 +289,23 @@ create_summary_tab (xfceweather_data *data)
 
   /* Wind */
   APPEND_BTEXT (_("\nWind\n"));
-  wind = translate_wind_speed (timeslice, get_data (timeslice, WIND_SPEED), data->unit);
-  value = g_strdup_printf (_("\t%s: %s (%s on the Beaufort scale)\n"), _("Speed"), wind,
-                           get_data (timeslice, WIND_BEAUFORT));
+  rawvalue = get_data (timeslice, WIND_SPEED);
+  wind = translate_wind_speed (timeslice, rawvalue, data->unit);
+  g_free (rawvalue);
+  rawvalue = get_data (timeslice, WIND_BEAUFORT);
+  value = g_strdup_printf (_("\t%s: %s (%s on the Beaufort scale)\n"), _("Speed"), wind, rawvalue);
+  g_free (rawvalue);
   g_free (wind);
   APPEND_TEXT_ITEM_REAL (value);
 
-  wind = translate_wind_direction (get_data (timeslice, WIND_DIRECTION));
+  rawvalue = get_data (timeslice, WIND_DIRECTION);
+  wind = translate_wind_direction (rawvalue);
+  g_free (rawvalue);
+  rawvalue = get_data (timeslice, WIND_DIRECTION_DEG);
   value = g_strdup_printf ("\t%s: %s (%s%s)\n", _("Direction"),
-                           wind ? wind : get_data (timeslice, WIND_DIRECTION),
-                           get_data (timeslice, WIND_DIRECTION_DEG),
+                           wind, rawvalue,
                            get_unit (timeslice, data->unit, WIND_DIRECTION_DEG));
+  g_free (rawvalue);
   g_free (wind);
   APPEND_TEXT_ITEM_REAL (value);
 
@@ -383,7 +387,7 @@ make_forecast (xfceweather_data *data,
     GdkColor lightbg = {0, 0xeaea, 0xeaea, 0xeaea};
     GdkColor darkbg = {0, 0x6666, 0x6666, 0x6666};
     gint num_days = 5, i, weekday, daytime;
-    gchar *dayname, *value;
+    gchar *dayname, *wind_speed, *value, *rawvalue;
     xml_time *fcdata;
     time_t now_t = time(NULL), fcday_t;
     struct tm tm_fcday;
@@ -445,16 +449,20 @@ make_forecast (xfceweather_data *data,
             fcdata = make_forecast_data(data->weatherdata, i, daytime);
             if (fcdata != NULL) {
                 if (fcdata->location != NULL) {
-                    icon = get_icon(get_data(fcdata, SYMBOL), 48, (daytime == NIGHT));
+                    rawvalue = get_data(fcdata, SYMBOL);
+                    icon = get_icon(rawvalue, 48, (daytime == NIGHT));
+                    g_free(rawvalue);
                     image = gtk_image_new_from_pixbuf(icon);
                     gtk_box_pack_start(GTK_BOX(forecast_box), GTK_WIDGET(image),
                                         TRUE, TRUE, 0);
                     if (G_LIKELY (icon))
                         g_object_unref (G_OBJECT (icon));
 
+                    rawvalue = get_data(fcdata, SYMBOL);
                     value = g_strdup_printf("%s",
-                                            translate_desc(get_data(fcdata, SYMBOL),
+                                            translate_desc(rawvalue,
                                                            (daytime == NIGHT)));
+                    g_free(rawvalue);
                     label = gtk_label_new(NULL);
                     gtk_label_set_markup(GTK_LABEL(label), value);
                     gtk_widget_show(GTK_WIDGET(label));
@@ -462,19 +470,25 @@ make_forecast (xfceweather_data *data,
                                         TRUE, TRUE, 0);
                     g_free(value);
 
+                    rawvalue = get_data(fcdata, TEMPERATURE);
                     value = g_strdup_printf("%s %s",
-                                            get_data(fcdata, TEMPERATURE),
+                                            rawvalue,
                                             get_unit(fcdata, data->unit, TEMPERATURE));
+                    g_free(rawvalue);
                     label = gtk_label_new(value);
                     gtk_widget_show(GTK_WIDGET(label));
                     gtk_box_pack_start(GTK_BOX(forecast_box), GTK_WIDGET(label),
                                         TRUE, TRUE, 0);
                     g_free(value);
 
+                    rawvalue = get_data(fcdata, WIND_DIRECTION);
+                    wind_speed = get_data(fcdata, WIND_SPEED);
                     value = g_strdup_printf("%s %s %s",
-                                            translate_wind_direction(get_data(fcdata, WIND_DIRECTION)),
-                                            get_data(fcdata, WIND_SPEED),
+                                            translate_wind_direction(rawvalue),
+                                            wind_speed,
                                             get_unit(fcdata, data->unit, WIND_SPEED));
+                    g_free(wind_speed);
+                    g_free(rawvalue);
                     label = gtk_label_new(value);
                     gtk_widget_show(GTK_WIDGET(label));
                     gtk_box_pack_start(GTK_BOX(forecast_box), label, TRUE, TRUE, 0);
@@ -521,7 +535,7 @@ GtkWidget *
 create_summary_window (xfceweather_data *data)
 {
   GtkWidget *window, *notebook, *vbox, *hbox, *label;
-  gchar     *title;
+  gchar     *title, *rawvalue;
   GdkPixbuf *icon;
   xml_time  *timeslice;
 
@@ -543,7 +557,10 @@ create_summary_window (xfceweather_data *data)
                       0);
 
   timeslice = get_current_timeslice(data->weatherdata, TRUE);
-  icon = get_icon (get_data (timeslice, SYMBOL), 48, is_night_time());
+
+  rawvalue = get_data (timeslice, SYMBOL);
+  icon = get_icon (rawvalue, 48, is_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 81ebfd7..833956a 100644
--- a/panel-plugin/weather.c
+++ b/panel-plugin/weather.c
@@ -100,10 +100,9 @@ make_label (xml_weather    *weatherdata,
 	    GtkOrientation orientation,
 	    gboolean       multiple_labels)
 {
-
-  gchar       *str, *value;
+  gchar       *str, *value, *rawvalue;
   xml_time    *timeslice;
-  const gchar *rawvalue, *lbl, *txtsize;
+  const gchar *lbl, *txtsize;
 
   switch (opt)
     {
@@ -201,6 +200,7 @@ make_label (xml_weather    *weatherdata,
                                txtsize, rawvalue, get_unit (timeslice, unit, opt));
       }
   }
+  g_free (rawvalue);
   return str;
 }
 
@@ -316,7 +316,10 @@ set_icon_current (xfceweather_data *data)
   /* get data from current timeslice */
   timeslice = get_current_timeslice(data->weatherdata, TRUE);
   nighttime = is_night_time();
-  icon = get_icon (get_data (timeslice, SYMBOL), size, nighttime);
+
+  str = get_data (timeslice, SYMBOL);
+  icon = get_icon (str, size, nighttime);
+  g_free (str);
  
   gtk_image_set_from_pixbuf (GTK_IMAGE (data->iconimage), icon);
 
@@ -324,9 +327,11 @@ set_icon_current (xfceweather_data *data)
     g_object_unref (G_OBJECT (icon));
 
 #if !GTK_CHECK_VERSION(2,12,0)
+  str = get_data (timeslice, SYMBOL);
   gtk_tooltips_set_tip (data->tooltips, data->tooltipbox,
-                        translate_desc (get_data (timeslice, SYMBOL), nighttime),
+                        translate_desc (str, nighttime),
                         NULL);
+  g_free (str);
 #endif
 }
 
@@ -796,7 +801,7 @@ static gboolean weather_get_tooltip_cb (GtkWidget        *widget,
 					xfceweather_data *data)
 {
   GdkPixbuf *icon;
-  gchar *markup_text;
+  gchar *markup_text, *rawvalue;
   xml_time *timeslice;
   gboolean nighttime;
 
@@ -805,16 +810,21 @@ static gboolean weather_get_tooltip_cb (GtkWidget        *widget,
   if (data->weatherdata == NULL) {
     gtk_tooltip_set_text (tooltip, _("Cannot update weather data"));
   } else {
+    rawvalue = get_data (timeslice, SYMBOL);
     markup_text = g_markup_printf_escaped(
   	  "<b>%s</b>\n"
 	  "%s",
 	  data->location_name,
-	  translate_desc (get_data (timeslice, SYMBOL), nighttime)
+	  translate_desc (rawvalue, nighttime)
 	  );
+    g_free(rawvalue);
     gtk_tooltip_set_markup (tooltip, markup_text);
     g_free(markup_text);
   }
-  icon = get_icon (get_data (timeslice, SYMBOL), 32, nighttime);
+
+  rawvalue = get_data (timeslice, SYMBOL);
+  icon = get_icon (rawvalue, 32, nighttime);
+  g_free (rawvalue);
   gtk_tooltip_set_icon (tooltip, icon);
   g_object_unref (G_OBJECT(icon));
 


More information about the Xfce4-commits mailing list