diff options
author | Charles Kerr <charles.kerr@canonical.com> | 2013-09-09 19:03:48 +0000 |
---|---|---|
committer | Tarmac <Unknown> | 2013-09-09 19:03:48 +0000 |
commit | 2da9d7c1bb979d47cca34de8875d3e9e90a2644a (patch) | |
tree | 836ee7b77bd1fc5ea4ce77f2989049e7783e1339 | |
parent | 03b3c04755082cf45dccf944b412d4b79aeabb72 (diff) | |
parent | 0b5af4aca455466f64f855e4328cd29ee15e7548 (diff) | |
download | ayatana-indicator-datetime-2da9d7c1bb979d47cca34de8875d3e9e90a2644a.tar.gz ayatana-indicator-datetime-2da9d7c1bb979d47cca34de8875d3e9e90a2644a.tar.bz2 ayatana-indicator-datetime-2da9d7c1bb979d47cca34de8875d3e9e90a2644a.zip |
Remove code duplication between timezone-file and timezone-geoclue.
Approved by Ted Gould, PS Jenkins bot.
-rw-r--r-- | src/timezone-file.c | 44 | ||||
-rw-r--r-- | src/timezone-geoclue.c | 39 | ||||
-rw-r--r-- | src/timezone.c | 46 | ||||
-rw-r--r-- | src/timezone.h | 8 |
4 files changed, 51 insertions, 86 deletions
diff --git a/src/timezone-file.c b/src/timezone-file.c index 2adf2ca..698ce3e 100644 --- a/src/timezone-file.c +++ b/src/timezone-file.c @@ -35,9 +35,7 @@ static GParamSpec * properties[PROP_LAST] = { 0 }; struct _IndicatorDatetimeTimezoneFilePriv { gchar * filename; - GFile * file; GFileMonitor * monitor; - gchar * timezone; }; typedef IndicatorDatetimeTimezoneFilePriv priv_t; @@ -56,26 +54,18 @@ reload (IndicatorDatetimeTimezoneFile * self) priv_t * p = self->priv; GError * err = NULL; - gchar * new_timezone = NULL; + gchar * timezone = NULL; - if (!g_file_get_contents (p->filename, &new_timezone, NULL, &err)) + if (!g_file_get_contents (p->filename, &timezone, NULL, &err)) { g_warning ("%s Unable to read timezone file '%s': %s", G_STRLOC, p->filename, err->message); g_error_free (err); } else { - g_strstrip (new_timezone); - - if (g_strcmp0 (p->timezone, new_timezone)) - { - g_free (p->timezone); - p->timezone = g_strdup (new_timezone); - g_debug ("%s new timezone set: '%s'", G_STRLOC, p->timezone); - indicator_datetime_timezone_notify_timezone (INDICATOR_DATETIME_TIMEZONE(self)); - } - - g_free (new_timezone); + g_strstrip (timezone); + indicator_datetime_timezone_set_timezone (INDICATOR_DATETIME_TIMEZONE(self), timezone); + g_free (timezone); } } @@ -83,17 +73,17 @@ static void set_filename (IndicatorDatetimeTimezoneFile * self, const char * filename) { GError * err; + GFile * file; priv_t * p = self->priv; g_clear_object (&p->monitor); - g_clear_object (&p->file); g_free (p->filename); p->filename = g_strdup (filename); - p->file = g_file_new_for_path (p->filename); - err = NULL; - p->monitor = g_file_monitor_file (p->file, G_FILE_MONITOR_NONE, NULL, &err); + file = g_file_new_for_path (p->filename); + p->monitor = g_file_monitor_file (file, G_FILE_MONITOR_NONE, NULL, &err); + g_object_unref (file); if (err != NULL) { g_warning ("%s Unable to monitor timezone file '%s': %s", G_STRLOC, TIMEZONE_FILE, err->message); @@ -109,16 +99,6 @@ set_filename (IndicatorDatetimeTimezoneFile * self, const char * filename) } /*** -**** IndicatorDatetimeTimezoneClass funcs -***/ - -static const char * -my_get_timezone (IndicatorDatetimeTimezone * self) -{ - return INDICATOR_DATETIME_TIMEZONE_FILE(self)->priv->timezone; -} - -/*** **** GObjectClass funcs ***/ @@ -167,7 +147,6 @@ my_dispose (GObject * o) priv_t * p = self->priv; g_clear_object (&p->monitor); - g_clear_object (&p->file); G_OBJECT_CLASS (indicator_datetime_timezone_file_parent_class)->dispose (o); } @@ -179,7 +158,6 @@ my_finalize (GObject * o) priv_t * p = self->priv; g_free (p->filename); - g_free (p->timezone); G_OBJECT_CLASS (indicator_datetime_timezone_file_parent_class)->finalize (o); } @@ -192,7 +170,6 @@ static void indicator_datetime_timezone_file_class_init (IndicatorDatetimeTimezoneFileClass * klass) { GObjectClass * object_class; - IndicatorDatetimeTimezoneClass * location_class; const GParamFlags flags = G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS; object_class = G_OBJECT_CLASS (klass); @@ -201,9 +178,6 @@ indicator_datetime_timezone_file_class_init (IndicatorDatetimeTimezoneFileClass object_class->set_property = my_set_property; object_class->get_property = my_get_property; - location_class = INDICATOR_DATETIME_TIMEZONE_CLASS (klass); - location_class->get_timezone = my_get_timezone; - g_type_class_add_private (klass, sizeof (IndicatorDatetimeTimezoneFilePriv)); /* install properties */ diff --git a/src/timezone-geoclue.c b/src/timezone-geoclue.c index f7dcf5c..c89558d 100644 --- a/src/timezone-geoclue.c +++ b/src/timezone-geoclue.c @@ -29,7 +29,6 @@ struct _IndicatorDatetimeTimezoneGeocluePriv GeoclueMaster * master; GeoclueMasterClient * client; GeoclueAddress * address; - gchar * timezone; }; typedef IndicatorDatetimeTimezoneGeocluePriv priv_t; @@ -45,19 +44,6 @@ static void geo_restart (IndicatorDatetimeTimezoneGeoclue * self); ***/ static void -set_timezone (IndicatorDatetimeTimezoneGeoclue * self, const gchar * timezone) -{ - priv_t * p = self->priv; - - if (g_strcmp0 (p->timezone, timezone)) - { - g_free (p->timezone); - p->timezone = g_strdup (timezone); - indicator_datetime_timezone_notify_timezone (INDICATOR_DATETIME_TIMEZONE(self)); - } -} - -static void on_address_changed (GeoclueAddress * address G_GNUC_UNUSED, int timestamp G_GNUC_UNUSED, GHashTable * addy_data, @@ -73,7 +59,7 @@ on_address_changed (GeoclueAddress * address G_GNUC_UNUSED, { IndicatorDatetimeTimezoneGeoclue * self = INDICATOR_DATETIME_TIMEZONE_GEOCLUE (gself); const char * timezone = g_hash_table_lookup (addy_data, "timezone"); - set_timezone (self, timezone); + indicator_datetime_timezone_set_timezone (INDICATOR_DATETIME_TIMEZONE(self), timezone); } } @@ -197,12 +183,6 @@ geo_restart (IndicatorDatetimeTimezoneGeoclue * self) **** ***/ -static const char * -my_get_timezone (IndicatorDatetimeTimezone * self) -{ - return INDICATOR_DATETIME_TIMEZONE_GEOCLUE(self)->priv->timezone; -} - static void my_dispose (GObject * o) { @@ -212,28 +192,12 @@ my_dispose (GObject * o) } static void -my_finalize (GObject * o) -{ - IndicatorDatetimeTimezoneGeoclue * self = INDICATOR_DATETIME_TIMEZONE_GEOCLUE (o); - priv_t * p = self->priv; - - g_free (p->timezone); - - G_OBJECT_CLASS (indicator_datetime_timezone_geoclue_parent_class)->finalize (o); -} - -static void indicator_datetime_timezone_geoclue_class_init (IndicatorDatetimeTimezoneGeoclueClass * klass) { GObjectClass * object_class; - IndicatorDatetimeTimezoneClass * location_class; object_class = G_OBJECT_CLASS (klass); object_class->dispose = my_dispose; - object_class->finalize = my_finalize; - - location_class = INDICATOR_DATETIME_TIMEZONE_CLASS (klass); - location_class->get_timezone = my_get_timezone; g_type_class_add_private (klass, sizeof (IndicatorDatetimeTimezoneGeocluePriv)); } @@ -246,6 +210,7 @@ indicator_datetime_timezone_geoclue_init (IndicatorDatetimeTimezoneGeoclue * sel p = G_TYPE_INSTANCE_GET_PRIVATE (self, INDICATOR_TYPE_DATETIME_TIMEZONE_GEOCLUE, IndicatorDatetimeTimezoneGeocluePriv); + self->priv = p; geo_start (self); diff --git a/src/timezone.c b/src/timezone.c index a543155..ca207f2 100644 --- a/src/timezone.c +++ b/src/timezone.c @@ -32,6 +32,13 @@ enum static GParamSpec * properties[PROP_LAST] = { 0, }; +struct _IndicatorDatetimeTimezonePriv +{ + GString * timezone; +}; + +typedef struct _IndicatorDatetimeTimezonePriv priv_t; + static void my_get_property (GObject * o, guint property_id, @@ -52,9 +59,13 @@ my_get_property (GObject * o, } static void -my_dispose (GObject * object) +my_finalize (GObject * o) { - G_OBJECT_CLASS (indicator_datetime_timezone_parent_class)->dispose (object); + priv_t * p = INDICATOR_DATETIME_TIMEZONE(o)->priv; + + g_string_free (p->timezone, TRUE); + + G_OBJECT_CLASS (indicator_datetime_timezone_parent_class)->finalize (o); } static void @@ -64,11 +75,11 @@ indicator_datetime_timezone_class_init (IndicatorDatetimeTimezoneClass * klass) GObjectClass * object_class; const GParamFlags flags = G_PARAM_READABLE | G_PARAM_STATIC_STRINGS; + g_type_class_add_private (klass, sizeof (IndicatorDatetimeTimezonePriv)); + object_class = G_OBJECT_CLASS (klass); object_class->get_property = my_get_property; - object_class->dispose = my_dispose; - - klass->get_timezone = NULL; + object_class->finalize = my_finalize; properties[PROP_TIMEZONE] = g_param_spec_string ("timezone", "Timezone", @@ -80,8 +91,17 @@ indicator_datetime_timezone_class_init (IndicatorDatetimeTimezoneClass * klass) } static void -indicator_datetime_timezone_init (IndicatorDatetimeTimezone * self G_GNUC_UNUSED) +indicator_datetime_timezone_init (IndicatorDatetimeTimezone * self) { + priv_t * p; + + p = G_TYPE_INSTANCE_GET_PRIVATE (self, + INDICATOR_TYPE_DATETIME_TIMEZONE, + IndicatorDatetimeTimezonePriv); + + p->timezone = g_string_new (NULL); + + self->priv = p; } /*** @@ -93,13 +113,19 @@ indicator_datetime_timezone_get_timezone (IndicatorDatetimeTimezone * self) { g_return_val_if_fail (INDICATOR_IS_DATETIME_TIMEZONE (self), NULL); - return INDICATOR_DATETIME_TIMEZONE_GET_CLASS (self)->get_timezone (self); + return self->priv->timezone->str; } void -indicator_datetime_timezone_notify_timezone (IndicatorDatetimeTimezone * self) +indicator_datetime_timezone_set_timezone (IndicatorDatetimeTimezone * self, + const char * timezone) { - g_return_if_fail (INDICATOR_IS_DATETIME_TIMEZONE (self)); + priv_t * p = self->priv; - g_object_notify_by_pspec (G_OBJECT(self), properties[PROP_TIMEZONE]); + if (g_strcmp0 (p->timezone->str, timezone)) + { + g_string_assign (p->timezone, timezone); + g_debug ("%s new timezone set: '%s'", G_STRLOC, timezone); + g_object_notify_by_pspec (G_OBJECT(self), properties[PROP_TIMEZONE]); + } } diff --git a/src/timezone.h b/src/timezone.h index 076bedc..aaaa296 100644 --- a/src/timezone.h +++ b/src/timezone.h @@ -32,6 +32,7 @@ G_BEGIN_DECLS #define INDICATOR_IS_DATETIME_TIMEZONE(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), INDICATOR_TYPE_DATETIME_TIMEZONE)) typedef struct _IndicatorDatetimeTimezone IndicatorDatetimeTimezone; +typedef struct _IndicatorDatetimeTimezonePriv IndicatorDatetimeTimezonePriv; typedef struct _IndicatorDatetimeTimezoneClass IndicatorDatetimeTimezoneClass; GType indicator_datetime_timezone_get_type (void); @@ -51,14 +52,12 @@ struct _IndicatorDatetimeTimezone { /*< private >*/ GObject parent; + IndicatorDatetimeTimezonePriv * priv; }; struct _IndicatorDatetimeTimezoneClass { GObjectClass parent_class; - - /* virtual functions */ - const char * (*get_timezone) (IndicatorDatetimeTimezone * self); }; /*** @@ -67,7 +66,8 @@ struct _IndicatorDatetimeTimezoneClass const char * indicator_datetime_timezone_get_timezone (IndicatorDatetimeTimezone *); -void indicator_datetime_timezone_notify_timezone (IndicatorDatetimeTimezone *); +void indicator_datetime_timezone_set_timezone (IndicatorDatetimeTimezone *, + const char * new_timezone); G_END_DECLS |