gnome-panel r11013 - trunk/applets/clock



Author: vuntz
Date: Tue Apr  8 16:40:39 2008
New Revision: 11013
URL: http://svn.gnome.org/viewvc/gnome-panel?rev=11013&view=rev

Log:
2008-04-08  Vincent Untz  <vuntz gnome org>

	Lots of cleanup, some leak plugging.

	* clock-country.[ch]: add some const to function declarations
	* clock-zonetable.[ch]: lots of minor changes all over the place.
	Basically:
	 - make the ClockZonetable object a singleton
	 - be a bit more solid when parsing files
	 - plug some leaks (don't forget to unref ClockCountry and
	   ClockZoneinfo, eg)
	 - clean up the code to be more readable
	 - add some const to function declarations
	* clock.c: (create_cities_store): small FIXME for later


Modified:
   trunk/applets/clock/ChangeLog
   trunk/applets/clock/clock-country.c
   trunk/applets/clock/clock-country.h
   trunk/applets/clock/clock-zonetable.c
   trunk/applets/clock/clock-zonetable.h
   trunk/applets/clock/clock.c

Modified: trunk/applets/clock/clock-country.c
==============================================================================
--- trunk/applets/clock/clock-country.c	(original)
+++ trunk/applets/clock/clock-country.c	Tue Apr  8 16:40:39 2008
@@ -21,7 +21,7 @@
 #define PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), CLOCK_COUNTRY_TYPE, ClockCountryPrivate))
 
 ClockCountry *
-clock_country_new (gchar *code, gchar *name)
+clock_country_new (const gchar *code, const gchar *name)
 {
         ClockCountry *this;
         ClockCountryPrivate *priv;
@@ -72,7 +72,7 @@
         G_OBJECT_CLASS (clock_country_parent_class)->finalize (g_obj);
 }
 
-gchar *
+const gchar *
 clock_country_get_code (ClockCountry *this)
 {
         ClockCountryPrivate *priv = PRIVATE (this);
@@ -80,7 +80,7 @@
         return priv->code;
 }
 
-gchar *
+const gchar *
 clock_country_get_name (ClockCountry *this)
 {
         ClockCountryPrivate *priv = PRIVATE (this);
@@ -88,7 +88,7 @@
         return priv->name;
 }
 
-gchar *
+const gchar *
 clock_country_get_l10n_name (ClockCountry *this)
 {
         ClockCountryPrivate *priv = PRIVATE (this);

Modified: trunk/applets/clock/clock-country.h
==============================================================================
--- trunk/applets/clock/clock-country.h	(original)
+++ trunk/applets/clock/clock-country.h	Tue Apr  8 16:40:39 2008
@@ -25,11 +25,11 @@
 
 GType clock_country_get_type (void);
 
-ClockCountry *clock_country_new (gchar *code, gchar *name);
+ClockCountry *clock_country_new (const gchar *code, const gchar *name);
 
-gchar *clock_country_get_code (ClockCountry *this);
-gchar *clock_country_get_name (ClockCountry *this);
-gchar *clock_country_get_l10n_name (ClockCountry *this);
+const gchar *clock_country_get_code (ClockCountry *this);
+const gchar *clock_country_get_name (ClockCountry *this);
+const gchar *clock_country_get_l10n_name (ClockCountry *this);
 
 G_END_DECLS
 #endif /* __CLOCK_COUNTRY_H__ */

Modified: trunk/applets/clock/clock-zonetable.c
==============================================================================
--- trunk/applets/clock/clock-zonetable.c	(original)
+++ trunk/applets/clock/clock-zonetable.c	Tue Apr  8 16:40:39 2008
@@ -12,12 +12,17 @@
 #include "clock-country.h"
 #include "clock-zoneinfo.h"
 
+#ifdef HAVE_SOLARIS
+#define ZONETAB_FILE SYSTEM_ZONEINFODIR"/zone_sun.tab"
+#define ISO3166_FILE SYSTEM_ZONEINFODIR"/country.tab"
+#else
+#define ZONETAB_FILE SYSTEM_ZONEINFODIR"/zone.tab"
+#define ISO3166_FILE SYSTEM_ZONEINFODIR"/iso3166.tab"
+#endif
+
 G_DEFINE_TYPE (ClockZoneTable, clock_zonetable, G_TYPE_OBJECT)
 
 typedef struct {
-        gchar *zonetab;
-        gchar *iso3166;
-
         GList *list;
         GHashTable *table;
         GHashTable *l10n_table;
@@ -26,6 +31,9 @@
         GHashTable *country_table;
 } ClockZoneTablePrivate;
 
+static GObject *clock_zonetable_constructor (GType                  type,
+                                             guint                  n_construct_properties,
+                                             GObjectConstructParam *construct_properties);
 static void clock_zonetable_finalize (GObject *);
 static void clock_zonetable_load_zonetab (ClockZoneTable *this);
 static void clock_zonetable_load_iso3166 (ClockZoneTable *this);
@@ -36,35 +44,8 @@
 clock_zonetable_new (void)
 {
         ClockZoneTable *this;
-        ClockZoneTablePrivate *priv;
 
         this = g_object_new (CLOCK_ZONETABLE_TYPE, NULL);
-        priv = PRIVATE (this);
-
-#ifdef HAVE_SOLARIS
-        priv->zonetab = g_build_filename (SYSTEM_ZONEINFODIR,
-                                          "zone_sun.tab", NULL);
-        priv->iso3166 = g_build_filename (SYSTEM_ZONEINFODIR,
-                                          "country.tab", NULL);
-#else
-        priv->zonetab = g_build_filename (SYSTEM_ZONEINFODIR,
-                                          "zone.tab", NULL);
-        priv->iso3166 = g_build_filename (SYSTEM_ZONEINFODIR,
-                                          "iso3166.tab", NULL);
-#endif
-
-#ifdef CLOCK_TEXTDOMAIN
-        /* this is used when clock is embedded in the gnome-panel
-           package */
-        textdomain (CLOCK_TEXTDOMAIN);
-        bindtextdomain (CLOCK_TEXTDOMAIN, GNOMELOCALEDIR);
-#endif
-	/* FMQ: this sucks; we are using Evolution's gettext domain for our own purposes */
-        bindtextdomain (EVOLUTION_TEXTDOMAIN, GNOMELOCALEDIR);
-        bind_textdomain_codeset (EVOLUTION_TEXTDOMAIN, "UTF-8");
-
-        clock_zonetable_load_zonetab (this);
-        clock_zonetable_load_iso3166 (this);
 
         return this;
 }
@@ -74,9 +55,15 @@
 {
         GObjectClass *g_obj_class = G_OBJECT_CLASS (this_class);
 
+        g_obj_class->constructor = clock_zonetable_constructor;
         g_obj_class->finalize = clock_zonetable_finalize;
 
         g_type_class_add_private (this_class, sizeof (ClockZoneTablePrivate));
+
+        /* FIXME: find a good way to not use Evolution's gettext domain without
+         * duplicating all the strings */
+        bindtextdomain (EVOLUTION_TEXTDOMAIN, GNOMELOCALEDIR);
+        bind_textdomain_codeset (EVOLUTION_TEXTDOMAIN, "UTF-8");
 }
 
 static void
@@ -84,8 +71,6 @@
 {
         ClockZoneTablePrivate *priv = PRIVATE (this);
 
-        priv->zonetab = NULL;
-        priv->iso3166 = NULL;
         priv->list = NULL;
         priv->table = NULL;
         priv->l10n_table = NULL;
@@ -94,21 +79,35 @@
         priv->country_table = NULL;
 }
 
+static GObject *
+clock_zonetable_constructor (GType                  type,
+                             guint                  n_construct_properties,
+                             GObjectConstructParam *construct_properties)
+{
+        static GObject *obj = NULL;
+
+        /* This is a singleton, we don't need to have it per-applet */
+        if (obj)
+                return g_object_ref (obj);
+
+        obj = G_OBJECT_CLASS (clock_zonetable_parent_class)->constructor (
+                                                type,
+                                                n_construct_properties,
+                                                construct_properties);
+
+
+        clock_zonetable_load_zonetab (CLOCK_ZONETABLE (obj));
+        clock_zonetable_load_iso3166 (CLOCK_ZONETABLE (obj));
+        /* FIXME: add some file monitoring here to reload the files? */
+
+        return obj;
+}
+
 static void
 clock_zonetable_finalize (GObject *g_obj)
 {
         ClockZoneTablePrivate *priv = PRIVATE (g_obj);
 
-        if (priv->zonetab) {
-                g_free (priv->zonetab);
-                priv->zonetab = NULL;
-        }
-
-        if (priv->iso3166) {
-                g_free (priv->iso3166);
-                priv->iso3166 = NULL;
-        }
-
         if (priv->list) {
                 g_list_free (priv->list);
                 priv->list = NULL;
@@ -137,49 +136,57 @@
         G_OBJECT_CLASS (clock_zonetable_parent_class)->finalize (g_obj);
 }
 
-static gfloat
-clock_zonetable_parse_coord (gchar *coord)
+static gboolean
+clock_zonetable_parse_coord (const gchar *coord, gfloat *ret)
 {
-        gfloat ret = 0.0;
-
         gfloat deg = 0;
         gfloat min = 0;
         gfloat sec = 0;
 
-        gchar *num = coord + 1;
+        const gchar *num = coord + 1;
+        int read;
         int len = strlen (num);
 
         if (len == 4) {
                 /* DDMM */
-                sscanf (num, "%2f%2f", &deg, &min);
+                read = sscanf (num, "%2f%2f", &deg, &min);
+                if (read != 2)
+                        return FALSE;
         } else if (len == 5) {
                 /* DDDMM */
                 sscanf (num, "%3f%2f", &deg, &min);
+                if (read != 2)
+                        return FALSE;
         } else if (len == 6) {
                 /* DDMMSS */
                 sscanf (num, "%2f%2f%2f", &deg, &min, &sec);
+                if (read != 3)
+                        return FALSE;
         } else if (len == 7) {
                 /* DDDMMSS */
                 sscanf (num, "%3f%2f%2f", &deg, &min, &sec);
+                if (read != 3)
+                        return FALSE;
         }
 
-        ret = deg + min / 60 + sec / 3600;
+        *ret = deg + min / 60 + sec / 3600;
 
         if (coord[0] == '-') {
-                ret = -ret;
+                *ret = -*ret;
         }
 
-        return ret;
+        return TRUE;
 }
 
 
-static void
-clock_zonetable_parse_location (gchar *location, gfloat *lat, gfloat *lon)
+static gboolean
+clock_zonetable_parse_location (const gchar *location, gfloat *lat, gfloat *lon)
 {
         int i;
+        gboolean success;
 
         gchar *lat_str = NULL;
-        gchar *lon_str = NULL;
+        const gchar *lon_str = NULL;
 
         for (i = 1; location[i] != '\0'; i++) {
                 if (location[i] == '+' || location[i] == '-') {
@@ -189,17 +196,22 @@
                 }
         }
 
-        *lat = clock_zonetable_parse_coord (lat_str);
-        *lon = clock_zonetable_parse_coord (lon_str);
+        if (!lat_str || !lon_str)
+                return FALSE;
+
+        success = clock_zonetable_parse_coord (lat_str, lat) &&
+                  clock_zonetable_parse_coord (lon_str, lon);
 
         g_free (lat_str);
         /* lon_str wasn't copied */
+
+        return success;
 }
 
 static ClockZoneInfo *
-clock_zonetable_parse_info_line (gchar *line)
+clock_zonetable_parse_info_line (const gchar *line)
 {
-        ClockZoneInfo *ret;
+        ClockZoneInfo *ret = NULL;
 
         gchar *country = NULL;
         gchar *location = NULL;
@@ -211,38 +223,41 @@
 
         gchar **split = g_strsplit (line, "\t", 0);
 
-        country = split[0];
-        location = split[1];
-        zone = split[2];
-
-        if (split[3] != '\0') {
-                comment = split[3];
+        if (split[0] && split[1] && split[2] && (!split[3] || !split[4])) {
+                country = split[0];
+                location = split[1];
+                zone = split[2];
+
+                if (split[3] != NULL)
+                        comment = split[3];
+
+                if (clock_zonetable_parse_location (location, &lat, &lon))
+                        ret = clock_zoneinfo_new (zone, country,
+                                                  comment, lat, lon);
         }
 
-        clock_zonetable_parse_location (location, &lat, &lon);
-
-        ret = clock_zoneinfo_new (zone, country, comment, lat, lon);
-
         g_strfreev (split);
         return ret;
 }
 
 static ClockCountry *
-clock_zonetable_parse_iso3166_line (gchar *line)
+clock_zonetable_parse_iso3166_line (const gchar *line)
 {
-        ClockCountry *ret;
+        ClockCountry *ret = NULL;
 
         gchar *code = NULL;
         gchar *name = NULL;
 
         gchar **split = g_strsplit (line, "\t", 0);
 
-        code = split[0];
-        name = split[1];
+        if (split[0] && split[1] && !split[2]) {
+                code = split[0];
+                name = split[1];
 
-        ret = clock_country_new (code, name);
+                ret = clock_country_new (code, name);
+        }
 
-        g_free (split);
+        g_strfreev (split);
         return ret;
 }
 
@@ -252,28 +267,43 @@
         ClockZoneTablePrivate *priv = PRIVATE (this);
         GIOChannel *channel;
         gchar *line;
+        gchar *old_line;
+        ClockZoneInfo *info;
 
-        priv->table = g_hash_table_new (g_str_hash, g_str_equal);
-        priv->l10n_table = g_hash_table_new (g_str_hash, g_str_equal);
+        priv->table = g_hash_table_new_full (g_str_hash, g_str_equal,
+                                             NULL, g_object_unref);
+        priv->l10n_table = g_hash_table_new_full (g_str_hash, g_str_equal,
+                                                  NULL, g_object_unref);
 
-        channel = g_io_channel_new_file (priv->zonetab, "r", NULL);
+        channel = g_io_channel_new_file (ZONETAB_FILE, "r", NULL);
 
+        /* FIXME: be more solid than just crashing */
         g_assert (channel != NULL);
 
-        while (g_io_channel_read_line (channel, &line, NULL, NULL, NULL) == G_IO_STATUS_NORMAL) {
+        old_line = NULL;
+        while (g_io_channel_read_line (channel, &line, NULL,
+                                       NULL, NULL) == G_IO_STATUS_NORMAL) {
+                g_free (old_line);
+                old_line = line;
+
                 g_strstrip (line);
 
-                if (line[0] != '#') {
-                        ClockZoneInfo *info =
-                                clock_zonetable_parse_info_line (line);
-
-                        priv->list = g_list_prepend (priv->list, info);
-                        g_hash_table_replace (priv->table, clock_zoneinfo_get_name (info), info);
-                        g_hash_table_replace (priv->l10n_table, clock_zoneinfo_get_l10n_name (info), info);
-                }
+                if (line[0] == '#')
+                        continue;
 
-                g_free (line);
+                info = clock_zonetable_parse_info_line (line);
+                if (!info)
+                        continue;
+
+                priv->list = g_list_prepend (priv->list, info);
+                g_hash_table_replace (priv->table,
+                                      clock_zoneinfo_get_name (info),
+                                      g_object_ref_sink (info));
+                g_hash_table_replace (priv->l10n_table,
+                                      clock_zoneinfo_get_l10n_name (info),
+                                      g_object_ref_sink (info));
         }
+        g_free (old_line);
 
         g_io_channel_unref (channel);
 }
@@ -284,31 +314,45 @@
         ClockZoneTablePrivate *priv = PRIVATE (this);
         GIOChannel *channel;
         gchar *line;
+        gchar *old_line;
+        ClockCountry *info;
 
-        priv->country_table = g_hash_table_new (g_str_hash, g_str_equal);
+        priv->country_table = g_hash_table_new_full (g_str_hash, g_str_equal,
+                                                     NULL, g_object_unref);
 
-        channel = g_io_channel_new_file (priv->iso3166, "r", NULL);
+        channel = g_io_channel_new_file (ISO3166_FILE, "r", NULL);
 
+        /* FIXME: be more solid than just crashing */
         g_assert (channel != NULL);
 
-        while (g_io_channel_read_line (channel, &line, NULL, NULL, NULL) == G_IO_STATUS_NORMAL) {
+        old_line = NULL;
+        while (g_io_channel_read_line (channel, &line, NULL,
+                                       NULL, NULL) == G_IO_STATUS_NORMAL) {
+                g_free (old_line);
+                old_line = line;
+
                 g_strstrip (line);
 
-                if (line[0] != '#') {
-                        ClockCountry *info =
-                                clock_zonetable_parse_iso3166_line (line);
-                        priv->country_list = g_list_prepend (priv->country_list, info);
-                        g_hash_table_replace (priv->country_table, clock_country_get_code (info), info);
-                }
+                if (line[0] == '#')
+                        continue;
 
-                g_free (line);
+                info = clock_zonetable_parse_iso3166_line (line);
+                if (!info)
+                        continue;
+
+                priv->country_list = g_list_prepend (priv->country_list, info);
+                g_hash_table_replace (priv->country_table,
+                                      (char *) clock_country_get_code (info),
+                                      g_object_ref_sink (info));
         }
+        g_free (old_line);
 
         g_io_channel_unref (channel);
 }
 
 ClockZoneInfo *
-clock_zonetable_get_zone (ClockZoneTable *this, gchar *name)
+clock_zonetable_get_zone (ClockZoneTable *this,
+                          const gchar    *name)
 {
         ClockZoneTablePrivate *priv = PRIVATE (this);
 
@@ -316,7 +360,8 @@
 }
 
 ClockZoneInfo *
-clock_zonetable_get_l10n_zone (ClockZoneTable *this, gchar *l10n_name)
+clock_zonetable_get_l10n_zone (ClockZoneTable *this,
+                               const gchar    *l10n_name)
 {
         ClockZoneTablePrivate *priv = PRIVATE (this);
 
@@ -332,7 +377,8 @@
 }
 
 ClockCountry *
-clock_zonetable_get_country (ClockZoneTable *this, gchar *code)
+clock_zonetable_get_country (ClockZoneTable *this,
+                             const gchar    *code)
 {
         ClockZoneTablePrivate *priv = PRIVATE (this);
 

Modified: trunk/applets/clock/clock-zonetable.h
==============================================================================
--- trunk/applets/clock/clock-zonetable.h	(original)
+++ trunk/applets/clock/clock-zonetable.h	Tue Apr  8 16:40:39 2008
@@ -29,11 +29,15 @@
 GType clock_zonetable_get_type (void);
 
 ClockZoneTable *clock_zonetable_new (void);
-ClockZoneInfo *clock_zonetable_get_zone (ClockZoneTable *this, gchar *name);
-ClockZoneInfo *clock_zonetable_get_l10n_zone (ClockZoneTable *this, gchar *l10n_name);
-GList *clock_zonetable_get_zones (ClockZoneTable *this);
 
-ClockCountry *clock_zonetable_get_country (ClockZoneTable *this, gchar *code);
+GList *clock_zonetable_get_zones             (ClockZoneTable *this);
+ClockZoneInfo *clock_zonetable_get_zone      (ClockZoneTable *this,
+                                              const gchar    *name);
+ClockZoneInfo *clock_zonetable_get_l10n_zone (ClockZoneTable *this,
+                                              const gchar    *l10n_name);
+
+ClockCountry *clock_zonetable_get_country    (ClockZoneTable *this, 
+                                              const gchar    *code);
 
 G_END_DECLS
 #endif /* __CLOCK_ZONETABLE_H__ */

Modified: trunk/applets/clock/clock.c
==============================================================================
--- trunk/applets/clock/clock.c	(original)
+++ trunk/applets/clock/clock.c	Tue Apr  8 16:40:39 2008
@@ -997,6 +997,7 @@
 		gtk_list_store_append (cd->cities_store, &iter);
 		gtk_list_store_set (cd->cities_store, &iter,
 				    COL_CITY_NAME, clock_location_get_name (loc),
+				    /* FIXME: translate the timezone */
 				    COL_CITY_TZ, clock_location_get_timezone (loc),
                                     COL_CITY_LOC, loc,
 				    -1);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]