[gnome-control-center] datetime: Move backward TZ DB to tz.[ch]



commit 8d15af48c88f97024ad4bd6c2e66c63d2beb67f7
Author: Bastien Nocera <hadess hadess net>
Date:   Wed Apr 13 17:07:07 2011 +0100

    datetime: Move backward TZ DB to tz.[ch]
    
    So we can add hacks in one location

 panels/datetime/cc-timezone-map.c |   82 +------------------------------------
 panels/datetime/test-timezone.c   |   14 +++++-
 panels/datetime/tz.c              |   82 +++++++++++++++++++++++++++++++++++-
 panels/datetime/tz.h              |    5 ++-
 4 files changed, 96 insertions(+), 87 deletions(-)
---
diff --git a/panels/datetime/cc-timezone-map.c b/panels/datetime/cc-timezone-map.c
index 755c48c..ba7ba40 100644
--- a/panels/datetime/cc-timezone-map.c
+++ b/panels/datetime/cc-timezone-map.c
@@ -57,7 +57,6 @@ struct _CcTimezoneMapPrivate
 
   TzDB *tzdb;
   TzLocation *location;
-  GHashTable *alias_db;
 };
 
 enum
@@ -171,12 +170,6 @@ cc_timezone_map_dispose (GObject *object)
       priv->visible_map_rowstride = 0;
     }
 
-  if (priv->alias_db)
-    {
-      g_hash_table_destroy (priv->alias_db);
-      priv->alias_db = NULL;
-    }
-
   G_OBJECT_CLASS (cc_timezone_map_parent_class)->dispose (object);
 }
 
@@ -541,57 +534,6 @@ button_press_event (GtkWidget      *widget,
 }
 
 static void
-load_backward_tz (CcTimezoneMap *self)
-{
-  GError *error = NULL;
-  char **lines, *contents;
-  guint i;
-
-  self->priv->alias_db = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
-
-  if (g_file_get_contents (GNOMECC_DATA_DIR "/datetime/backward", &contents, NULL, &error) == FALSE)
-    {
-      g_warning ("Failed to load 'backward' file: %s", error->message);
-      return;
-    }
-  lines = g_strsplit (contents, "\n", -1);
-  g_free (contents);
-  for (i = 0; lines[i] != NULL; i++)
-    {
-      char **items;
-      guint j;
-      char *real, *alias;
-
-      if (g_ascii_strncasecmp (lines[i], "Link\t", 5) != 0)
-        continue;
-
-      items = g_strsplit (lines[i], "\t", -1);
-      real = NULL;
-      alias = NULL;
-      /* Skip the "Link<tab>" part */
-      for (j = 1; items[j] != NULL; j++)
-        {
-          if (items[j][0] == '\0')
-            continue;
-          if (real == NULL)
-            {
-              real = items[j];
-              continue;
-            }
-          alias = items[j];
-          break;
-        }
-
-      if (real == NULL || alias == NULL)
-        g_warning ("Could not parse line: %s", lines[i]);
-
-      g_hash_table_insert (self->priv->alias_db, g_strdup (alias), g_strdup (real));
-      g_strfreev (items);
-    }
-  g_strfreev (lines);
-}
-
-static void
 cc_timezone_map_init (CcTimezoneMap *self)
 {
   CcTimezoneMapPrivate *priv;
@@ -622,8 +564,6 @@ cc_timezone_map_init (CcTimezoneMap *self)
 
   g_signal_connect (self, "button-press-event", G_CALLBACK (button_press_event),
                     NULL);
-
-  load_backward_tz (self);
 }
 
 CcTimezoneMap *
@@ -632,26 +572,6 @@ cc_timezone_map_new (void)
   return g_object_new (CC_TYPE_TIMEZONE_MAP, NULL);
 }
 
-static char *
-get_clean_tz (CcTimezoneMap *map,
-	      const char    *tz)
-{
-	char *ret;
-	const char *timezone;
-
-	if (g_str_has_prefix (tz, "right/"))
-		timezone = tz + strlen ("right/");
-	else if (g_str_has_prefix (tz, "posix/"))
-		timezone = tz + strlen ("posix/");
-	else
-		timezone = tz;
-
-	ret = g_hash_table_lookup (map->priv->alias_db, timezone);
-	if (ret == NULL)
-		return g_strdup (timezone);
-	return g_strdup (ret);
-}
-
 gboolean
 cc_timezone_map_set_timezone (CcTimezoneMap *map,
                               const gchar   *timezone)
@@ -661,7 +581,7 @@ cc_timezone_map_set_timezone (CcTimezoneMap *map,
   char *real_tz;
   gboolean ret;
 
-  real_tz = get_clean_tz (map, timezone);
+  real_tz = tz_info_get_clean_name (map->priv->tzdb, timezone);
 
   locations = tz_get_locations (map->priv->tzdb);
   ret = FALSE;
diff --git a/panels/datetime/test-timezone.c b/panels/datetime/test-timezone.c
index 2dc744e..e749283 100644
--- a/panels/datetime/test-timezone.c
+++ b/panels/datetime/test-timezone.c
@@ -59,23 +59,33 @@ get_timezone_list (GList *tzs,
 int main (int argc, char **argv)
 {
 	CcTimezoneMap *map;
+	TzDB *tz_db;
 	GList *tzs, *l;
 	int ret = 0;
 
 	gtk_init (&argc, &argv);
 
 	map = cc_timezone_map_new ();
+	tz_db = tz_load_db ();
 	tzs = get_timezone_list (NULL, TZ_DIR, NULL);
 	for (l = tzs; l != NULL; l = l->next) {
 		char *timezone = l->data;
+		char *clean_tz;
 
-		if (cc_timezone_map_set_timezone (map, timezone) == FALSE) {
-			g_warning ("Failed to locate timezone '%s'", timezone);
+		clean_tz = tz_info_get_clean_name (tz_db, timezone);
+
+		if (cc_timezone_map_set_timezone (map, clean_tz) == FALSE) {
+			if (g_strcmp0 (clean_tz, timezone) == 0)
+				g_warning ("Failed to locate timezone '%s'", timezone);
+			else
+				g_warning ("Failed to locate timezone '%s' (alias for '%s')", timezone, clean_tz);
 			ret = 1;
 		}
 		g_free (timezone);
+		g_free (clean_tz);
 	}
 	g_list_free (tzs);
+	tz_db_free (tz_db);
 
 	return ret;
 }
diff --git a/panels/datetime/tz.c b/panels/datetime/tz.c
index 22b948b..61fd31c 100644
--- a/panels/datetime/tz.c
+++ b/panels/datetime/tz.c
@@ -39,7 +39,7 @@ static float convert_pos (gchar *pos, int digits);
 static int compare_country_names (const void *a, const void *b);
 static void sort_locations_by_country (GPtrArray *locations);
 static gchar * tz_data_file_get (void);
-
+static void load_backward_tz (TzDB *tz_db);
 
 /* ---------------- *
  * Public interface *
@@ -124,9 +124,12 @@ tz_load_db (void)
 	sort_locations_by_country (tz_db->locations);
 	
 	g_free (tz_data_file);
-	
+
+	/* Load up the hashtable of backward links */
+	load_backward_tz (tz_db);
+
 	return tz_db;
-}    
+}
 
 static void
 tz_location_free (TzLocation *loc)
@@ -143,6 +146,7 @@ tz_db_free (TzDB *db)
 {
 	g_ptr_array_foreach (db->locations, (GFunc) tz_location_free, NULL);
 	g_ptr_array_free (db->locations, TRUE);
+	g_hash_table_destroy (db->backward);
 	g_free (db);
 }
 
@@ -246,6 +250,26 @@ tz_info_free (TzInfo *tzinfo)
 	g_free (tzinfo);
 }
 
+char *
+tz_info_get_clean_name (TzDB *tz_db,
+			const char *tz)
+{
+	char *ret;
+	const char *timezone;
+
+	if (g_str_has_prefix (tz, "right/"))
+		timezone = tz + strlen ("right/");
+	else if (g_str_has_prefix (tz, "posix/"))
+		timezone = tz + strlen ("posix/");
+	else
+		timezone = tz;
+
+	ret = g_hash_table_lookup (tz_db->backward, timezone);
+	if (ret == NULL)
+		return g_strdup (timezone);
+	return g_strdup (ret);
+}
+
 /* ----------------- *
  * Private functions *
  * ----------------- */
@@ -317,3 +341,55 @@ sort_locations_by_country (GPtrArray *locations)
 	qsort (locations->pdata, locations->len, sizeof (gpointer),
 	       compare_country_names);
 }
+
+static void
+load_backward_tz (TzDB *tz_db)
+{
+  GError *error = NULL;
+  char **lines, *contents;
+  guint i;
+
+  tz_db->backward = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
+
+  if (g_file_get_contents (GNOMECC_DATA_DIR "/datetime/backward", &contents, NULL, &error) == FALSE)
+    {
+      g_warning ("Failed to load 'backward' file: %s", error->message);
+      return;
+    }
+  lines = g_strsplit (contents, "\n", -1);
+  g_free (contents);
+  for (i = 0; lines[i] != NULL; i++)
+    {
+      char **items;
+      guint j;
+      char *real, *alias;
+
+      if (g_ascii_strncasecmp (lines[i], "Link\t", 5) != 0)
+        continue;
+
+      items = g_strsplit (lines[i], "\t", -1);
+      real = NULL;
+      alias = NULL;
+      /* Skip the "Link<tab>" part */
+      for (j = 1; items[j] != NULL; j++)
+        {
+          if (items[j][0] == '\0')
+            continue;
+          if (real == NULL)
+            {
+              real = items[j];
+              continue;
+            }
+          alias = items[j];
+          break;
+        }
+
+      if (real == NULL || alias == NULL)
+        g_warning ("Could not parse line: %s", lines[i]);
+
+      g_hash_table_insert (tz_db->backward, g_strdup (alias), g_strdup (real));
+      g_strfreev (items);
+    }
+  g_strfreev (lines);
+}
+
diff --git a/panels/datetime/tz.h b/panels/datetime/tz.h
index 91281a5..71c1c23 100644
--- a/panels/datetime/tz.h
+++ b/panels/datetime/tz.h
@@ -41,7 +41,8 @@ typedef struct _TzInfo TzInfo;
 
 struct _TzDB
 {
-	GPtrArray *locations;
+	GPtrArray  *locations;
+	GHashTable *backward;
 };
 
 struct _TzLocation
@@ -72,6 +73,8 @@ struct _TzInfo
 
 TzDB      *tz_load_db                 (void);
 void       tz_db_free                 (TzDB *db);
+char *     tz_info_get_clean_name     (TzDB *tz_db,
+				       const char *tz);
 GPtrArray *tz_get_locations           (TzDB *db);
 void       tz_location_get_position   (TzLocation *loc,
 				       double *longitude, double *latitude);



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