aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles Kerr <charles.kerr@canonical.com>2013-10-23 13:14:25 -0500
committerCharles Kerr <charles.kerr@canonical.com>2013-10-23 13:14:25 -0500
commit6da162acd4a293a1cfe1933c54f6b06935deebbf (patch)
tree3cf94298d909590579d5171a7bc95b5a0608efd3
parenta2f21a1da1b1f467751020b56b04808f5dd00695 (diff)
downloadayatana-indicator-datetime-6da162acd4a293a1cfe1933c54f6b06935deebbf.tar.gz
ayatana-indicator-datetime-6da162acd4a293a1cfe1933c54f6b06935deebbf.tar.bz2
ayatana-indicator-datetime-6da162acd4a293a1cfe1933c54f6b06935deebbf.zip
instead of keeping IndicatorDatetimeTimezone objects in separate fields, keep them in a list so they can be handled in a loop.
-rw-r--r--src/clock-live.c94
1 files changed, 43 insertions, 51 deletions
diff --git a/src/clock-live.c b/src/clock-live.c
index de5ce19..4153747 100644
--- a/src/clock-live.c
+++ b/src/clock-live.c
@@ -35,9 +35,8 @@ struct _IndicatorDatetimeClockLivePriv
{
GSettings * settings;
- IndicatorDatetimeTimezone * tz_file;
- IndicatorDatetimeTimezone * tz_geoclue;
- gchar ** timezones;
+ GSList * timezones; /* IndicatorDatetimeTimezone */
+ gchar ** timezones_strv;
GTimeZone * localtime_zone;
};
@@ -66,7 +65,9 @@ on_current_timezone_changed (IndicatorDatetimeClockLive * self)
{
priv_t * p = self->priv;
- g_clear_pointer (&p->timezones, g_strfreev);
+ /* Invalidate the timezone information.
+ These fields will be lazily regenerated by rebuild_timezones() */
+ g_clear_pointer (&p->timezones_strv, g_strfreev);
g_clear_pointer (&p->localtime_zone, g_time_zone_unref);
indicator_datetime_clock_emit_changed (INDICATOR_DATETIME_CLOCK (self));
@@ -75,44 +76,36 @@ on_current_timezone_changed (IndicatorDatetimeClockLive * self)
static void
set_detect_location_enabled (IndicatorDatetimeClockLive * self, gboolean enabled)
{
+ GSList * l;
priv_t * p = self->priv;
gboolean changed = FALSE;
- /* geoclue */
-
- if (!p->tz_geoclue && enabled)
+ /* clear out the old timezone objects */
+ if (p->timezones != NULL)
{
- p->tz_geoclue = indicator_datetime_timezone_geoclue_new ();
- g_signal_connect_swapped (p->tz_geoclue, "notify::timezone",
- G_CALLBACK(on_current_timezone_changed),
- self);
+ for (l=p->timezones; l!=NULL; l=l->next)
+ {
+ g_signal_handlers_disconnect_by_func (l->data, on_current_timezone_changed, self);
+ g_object_unref (l->data);
+ }
+
+ g_slist_free (p->timezones);
+ p->timezones = NULL;
changed = TRUE;
}
- else if (p->tz_geoclue && !enabled)
+
+ /* maybe add new timezone objects */
+ if (enabled)
{
- g_signal_handlers_disconnect_by_func (p->tz_geoclue,
- on_current_timezone_changed,
- self);
- g_clear_object (&p->tz_geoclue);
- changed = TRUE;
- }
+ p->timezones = g_slist_append (p->timezones, indicator_datetime_timezone_geoclue_new ());
+ p->timezones = g_slist_append (p->timezones, indicator_datetime_timezone_file_new (TIMEZONE_FILE));
- /* timezone file */
+ for (l=p->timezones; l!=NULL; l=l->next)
+ {
+ g_signal_connect_swapped (l->data, "notify::timezone",
+ G_CALLBACK(on_current_timezone_changed), self);
+ }
- if (!p->tz_file && enabled)
- {
- p->tz_file = indicator_datetime_timezone_file_new (TIMEZONE_FILE);
- g_signal_connect_swapped (p->tz_file, "notify::timezone",
- G_CALLBACK(on_current_timezone_changed),
- self);
- changed = TRUE;
- }
- else if (p->tz_file && !enabled)
- {
- g_signal_handlers_disconnect_by_func (p->tz_file,
- on_current_timezone_changed,
- self);
- g_clear_object (&p->tz_file);
changed = TRUE;
}
@@ -138,38 +131,37 @@ rebuild_timezones (IndicatorDatetimeClockLive * self)
{
priv_t * p;
GHashTable * hash;
+ GSList * l;
int i;
GHashTableIter iter;
gpointer key;
p = self->priv;
+ /* Build a hashtable of timezone strings.
+ This will weed out duplicates. */
hash = g_hash_table_new (g_str_hash, g_str_equal);
-
- if (p->tz_file != NULL)
+ for (l=p->timezones; l!=NULL; l=l->next)
{
- const gchar * tz = indicator_datetime_timezone_get_timezone (p->tz_file);
+ const gchar * tz = indicator_datetime_timezone_get_timezone (l->data);
if (tz && *tz)
g_hash_table_add (hash, (gpointer) tz);
}
- if (p->tz_geoclue != NULL)
- {
- const gchar * tz = indicator_datetime_timezone_get_timezone (p->tz_geoclue);
- if (tz && *tz)
- g_hash_table_add (hash, (gpointer) tz);
- }
-
- g_strfreev (p->timezones);
- p->timezones = g_new0 (gchar*, g_hash_table_size(hash) + 1);
+ /* rebuild p->timezone_strv */
+ g_strfreev (p->timezones_strv);
+ p->timezones_strv = g_new0 (gchar*, g_hash_table_size(hash) + 1);
i = 0;
g_hash_table_iter_init (&iter, hash);
while (g_hash_table_iter_next (&iter, &key, NULL))
- p->timezones[i++] = g_strdup (key);
- g_hash_table_unref (hash);
+ p->timezones_strv[i++] = g_strdup (key);
+ /* rebuild localtime_zone */
g_clear_pointer (&p->localtime_zone, g_time_zone_unref);
- p->localtime_zone = g_time_zone_new (p->timezones ? p->timezones[0] : NULL);
+ p->localtime_zone = g_time_zone_new (p->timezones_strv ? p->timezones_strv[0] : NULL);
+
+ /* cleanup */
+ g_hash_table_unref (hash);
}
static const gchar **
@@ -178,10 +170,10 @@ my_get_timezones (IndicatorDatetimeClock * clock)
IndicatorDatetimeClockLive * self = INDICATOR_DATETIME_CLOCK_LIVE (clock);
priv_t * p = self->priv;
- if (G_UNLIKELY (p->timezones == NULL))
+ if (G_UNLIKELY (p->timezones_strv == NULL))
rebuild_timezones (self);
- return (const gchar **) p->timezones;
+ return (const gchar **) p->timezones_strv;
}
static GDateTime *
@@ -230,7 +222,7 @@ my_finalize (GObject * o)
p = self->priv;
g_clear_pointer (&p->localtime_zone, g_time_zone_unref);
- g_strfreev (p->timezones);
+ g_strfreev (p->timezones_strv);
G_OBJECT_CLASS (indicator_datetime_clock_live_parent_class)->dispose (o);
}