From 5761e8d1d75838c8a097ab1e41c3b51b33037437 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 25 Jul 2013 18:57:29 -0500 Subject: implement IdoLocationMenuItem as a subclass of IdoTimeStampMenuItem --- src/idolocationmenuitem.c | 115 ++++++++++++---------------------------------- 1 file changed, 29 insertions(+), 86 deletions(-) (limited to 'src/idolocationmenuitem.c') diff --git a/src/idolocationmenuitem.c b/src/idolocationmenuitem.c index df7c210..335422a 100644 --- a/src/idolocationmenuitem.c +++ b/src/idolocationmenuitem.c @@ -32,7 +32,6 @@ enum { PROP_0, PROP_TIMEZONE, - PROP_FORMAT, PROP_LAST }; @@ -41,48 +40,34 @@ static GParamSpec *properties[PROP_LAST]; struct _IdoLocationMenuItemPrivate { char * timezone; - char * format; guint timestamp_timer; }; typedef IdoLocationMenuItemPrivate priv_t; -G_DEFINE_TYPE (IdoLocationMenuItem, ido_location_menu_item, IDO_TYPE_BASIC_MENU_ITEM); +G_DEFINE_TYPE (IdoLocationMenuItem, ido_location_menu_item, IDO_TYPE_TIME_STAMP_MENU_ITEM); /*** **** Timestamp Label ***/ static void -update_timestamp_label (IdoLocationMenuItem * self) +update_timestamp (IdoLocationMenuItem * self) { - char * str; - priv_t * p = self->priv; - - if (p->format && *p->format) - { - GTimeZone * tz; - GDateTime * now; - - tz = g_time_zone_new (p->timezone); - if (tz == NULL) - tz = g_time_zone_new_local (); - now = g_date_time_new_now (tz); - - str = g_date_time_format (now, p->format); + GTimeZone * tz; + GDateTime * date_time; - g_date_time_unref (now); - g_time_zone_unref (tz); - } - else - { - str = NULL; - } + tz = g_time_zone_new (self->priv->timezone); + if (tz == NULL) + tz = g_time_zone_new_local (); + date_time = g_date_time_new_now (tz); - ido_basic_menu_item_set_secondary_text (IDO_BASIC_MENU_ITEM(self), str); + ido_time_stamp_menu_item_set_date_time (IDO_TIME_STAMP_MENU_ITEM(self), + date_time); - g_free (str); + g_date_time_unref (date_time); + g_time_zone_unref (tz); } static void @@ -97,16 +82,16 @@ stop_timestamp_timer (IdoLocationMenuItem * self) } } -static void start_timestamp_timer (IdoLocationMenuItem * self); +static void restart_timestamp_timer (IdoLocationMenuItem * self); static gboolean on_timestamp_timer (gpointer gself) { IdoLocationMenuItem * self = IDO_LOCATION_MENU_ITEM (gself); - update_timestamp_label (self); + update_timestamp (self); - start_timestamp_timer (self); + restart_timestamp_timer (self); return G_SOURCE_REMOVE; } @@ -140,12 +125,11 @@ calculate_seconds_until_next_minute (void) } static void -start_timestamp_timer (IdoLocationMenuItem * self) +restart_timestamp_timer (IdoLocationMenuItem * self) { - int interval_sec; + const char * fmt = ido_time_stamp_menu_item_get_format (IDO_TIME_STAMP_MENU_ITEM (self)); gboolean timestamp_shows_seconds; - priv_t * p = self->priv; - const char * const fmt = p->format; + int interval_sec; stop_timestamp_timer (self); @@ -158,9 +142,9 @@ start_timestamp_timer (IdoLocationMenuItem * self) else interval_sec = calculate_seconds_until_next_minute(); - p->timestamp_timer = g_timeout_add_seconds (interval_sec, - on_timestamp_timer, - self); + self->priv->timestamp_timer = g_timeout_add_seconds (interval_sec, + on_timestamp_timer, + self); } /*** @@ -182,10 +166,6 @@ my_get_property (GObject * o, g_value_set_string (value, p->timezone); break; - case PROP_FORMAT: - g_value_set_string (value, p->format); - break; - default: G_OBJECT_WARN_INVALID_PROPERTY_ID (o, property_id, pspec); break; @@ -206,10 +186,6 @@ my_set_property (GObject * o, ido_location_menu_item_set_timezone (self, g_value_get_string (value)); break; - case PROP_FORMAT: - ido_location_menu_item_set_format (self, g_value_get_string (value)); - break; - default: G_OBJECT_WARN_INVALID_PROPERTY_ID (o, property_id, pspec); break; @@ -228,10 +204,8 @@ static void my_finalize (GObject * object) { IdoLocationMenuItem * self = IDO_LOCATION_MENU_ITEM (object); - priv_t * p = self->priv; - g_free (p->format); - g_free (p->timezone); + g_free (self->priv->timezone); G_OBJECT_CLASS (ido_location_menu_item_parent_class)->finalize (object); } @@ -243,7 +217,6 @@ my_finalize (GObject * object) static void ido_location_menu_item_class_init (IdoLocationMenuItemClass *klass) { - GParamFlags prop_flags; GObjectClass * gobject_class = G_OBJECT_CLASS (klass); g_type_class_add_private (klass, sizeof (IdoLocationMenuItemPrivate)); @@ -253,23 +226,12 @@ ido_location_menu_item_class_init (IdoLocationMenuItemClass *klass) gobject_class->dispose = my_dispose; gobject_class->finalize = my_finalize; - prop_flags = G_PARAM_CONSTRUCT - | G_PARAM_READWRITE - | G_PARAM_STATIC_STRINGS; - properties[PROP_TIMEZONE] = g_param_spec_string ( "timezone", "timezone identifier", "string used to identify a timezone; eg, 'America/Chicago'", NULL, - prop_flags); - - properties[PROP_FORMAT] = g_param_spec_string ( - "format", - "strftime format", - "strftime-style format string for the timestamp", - "%T", - prop_flags); + G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); g_object_class_install_properties (gobject_class, PROP_LAST, properties); } @@ -280,6 +242,11 @@ ido_location_menu_item_init (IdoLocationMenuItem *self) self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, IDO_LOCATION_MENU_ITEM_TYPE, IdoLocationMenuItemPrivate); + + /* Update the timer whenever the format string changes + because it determines whether we update once per second or per minute */ + g_signal_connect (self, "notify::format", + G_CALLBACK(restart_timestamp_timer), NULL); } /*** @@ -311,31 +278,7 @@ ido_location_menu_item_set_timezone (IdoLocationMenuItem * self, g_free (p->timezone); p->timezone = g_strdup (timezone); - update_timestamp_label (self); -} - -/** - * ido_location_menu_item_set_format: - * @format: the format string used when showing the location's time - * - * Set the format string for rendering the location's time - * in its right-justified secondary label. - * - * See strfrtime(3) for more information on the format string. - */ -void -ido_location_menu_item_set_format (IdoLocationMenuItem * self, - const char * format) -{ - priv_t * p; - - g_return_if_fail (IDO_IS_LOCATION_MENU_ITEM (self)); - p = self->priv; - - g_free (p->format); - p->format = g_strdup (format); - update_timestamp_label (self); - start_timestamp_timer (self); + update_timestamp (self); } /** -- cgit v1.2.3