datetime: Move backward TZ DB to tz.[ch]

So we can add hacks in one location
This commit is contained in:
Bastien Nocera 2011-04-13 17:07:07 +01:00
parent f8c3b0c59e
commit 8d15af48c8
4 changed files with 96 additions and 87 deletions

View file

@ -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);
}
@ -540,57 +533,6 @@ button_press_event (GtkWidget *widget,
return TRUE;
}
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)
{
@ -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;

View file

@ -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) {
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;
}

View file

@ -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 *
@ -125,6 +125,9 @@ tz_load_db (void)
g_free (tz_data_file);
/* Load up the hashtable of backward links */
load_backward_tz (tz_db);
return tz_db;
}
@ -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);
}

View file

@ -42,6 +42,7 @@ typedef struct _TzInfo TzInfo;
struct _TzDB
{
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);