From bcc04892148c7396e638f45e96fcba42d0034ec7 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 17 Oct 2013 16:32:44 -0500 Subject: refactor the timezone and current time provider into a gobject Interface 'IndicatorDatetimeClock' --- src/Makefile.am | 6 +- src/clock-live.c | 306 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/clock-live.h | 73 +++++++++++++ src/clock.c | 110 +++++++++++++++++++ src/clock.h | 76 +++++++++++++ src/main.c | 41 ++----- src/planner-mock.c | 178 ------------------------------- src/planner-mock.h | 58 ---------- src/service.c | 230 +++++++++++++++++----------------------- src/service.h | 10 +- 10 files changed, 680 insertions(+), 408 deletions(-) create mode 100644 src/clock-live.c create mode 100644 src/clock-live.h create mode 100644 src/clock.c create mode 100644 src/clock.h delete mode 100644 src/planner-mock.c delete mode 100644 src/planner-mock.h (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index 640650a..be7eb4d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -18,10 +18,12 @@ libindicator_datetime_service_a_CFLAGS = \ $(SHARED_CFLAGS) libindicator_datetime_service_a_SOURCES = \ + clock.c \ + clock.h \ + clock-live.c \ + clock-live.h \ planner.c \ planner.h \ - planner-mock.c \ - planner-mock.h \ planner-eds.c \ planner-eds.h \ service.c \ diff --git a/src/clock-live.c b/src/clock-live.c new file mode 100644 index 0000000..6e694ed --- /dev/null +++ b/src/clock-live.c @@ -0,0 +1,306 @@ +/* + * Copyright 2013 Canonical Ltd. + * + * Authors: + * Charles Kerr + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3, as published + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranties of + * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#include +#include + +#include "config.h" + +#include "clock-live.h" +#include "settings-shared.h" +#include "timezone-file.h" +#include "timezone-geoclue.h" + +/*** +**** private struct +***/ + +struct _IndicatorDatetimeClockLivePriv +{ + GSettings * settings; + + /* cached GTimeZone for use by indicator_datetime_service_get_localtime() */ + GTimeZone * internal_timezone; + + IndicatorDatetimeTimezone * tz_file; + IndicatorDatetimeTimezone * tz_geoclue; +}; + +typedef IndicatorDatetimeClockLivePriv priv_t; + +/*** +**** GObject boilerplate +***/ + +static void indicator_datetime_clock_interface_init ( + IndicatorDatetimeClockInterface * iface); + +G_DEFINE_TYPE_WITH_CODE ( + IndicatorDatetimeClockLive, + indicator_datetime_clock_live, + G_TYPE_OBJECT, + G_IMPLEMENT_INTERFACE (INDICATOR_TYPE_DATETIME_CLOCK, + indicator_datetime_clock_interface_init)); + +/*** +**** Convenience func +***/ + +#if 0 +static void +emit_changed (IndicatorDatetimeClockLive * self) +{ + indicator_datetime_clock_emit_changed (INDICATOR_DATETIME_CLOCK (self)); +} +#endif + +/*** +**** Timezones +***/ + +static void +update_internal_timezone (IndicatorDatetimeClockLive * self) +{ + priv_t * p = self->priv; + const char * id; + + /* find the id from tz_file or tz_geoclue if possible; NULL otherwise */ + id = NULL; + if (p->tz_file != NULL ) + id = indicator_datetime_timezone_get_timezone (p->tz_file); + if (!id && p->tz_geoclue) + id = indicator_datetime_timezone_get_timezone (p->tz_geoclue); + + g_clear_pointer (&p->internal_timezone, g_time_zone_unref); + p->internal_timezone = g_time_zone_new (id); +} + +static void +on_current_timezone_changed (IndicatorDatetimeClockLive * self) +{ + indicator_datetime_clock_emit_changed (INDICATOR_DATETIME_CLOCK (self)); +} + +static void +set_detect_location_enabled (IndicatorDatetimeClockLive * self, gboolean enabled) +{ + priv_t * p = self->priv; + gboolean changed = FALSE; + + /* geoclue */ + + if (!p->tz_geoclue && enabled) + { + p->tz_geoclue = indicator_datetime_timezone_geoclue_new (); + g_signal_connect_swapped (p->tz_geoclue, "notify::timezone", + G_CALLBACK(on_current_timezone_changed), + self); + changed = TRUE; + } + else if (p->tz_geoclue && !enabled) + { + g_signal_handlers_disconnect_by_func (p->tz_geoclue, + on_current_timezone_changed, + self); + g_clear_object (&p->tz_geoclue); + changed = TRUE; + } + + /* timezone file */ + + 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; + } + + if (changed) + on_current_timezone_changed (self); +} + +/* When the 'auto-detect timezone' boolean setting changes, + start or stop watching geoclue and /etc/timezone */ +static void +on_detect_location_changed (IndicatorDatetimeClockLive * self) +{ + const gboolean enabled = g_settings_get_boolean (self->priv->settings, SETTINGS_SHOW_DETECTED_S); + set_detect_location_enabled (self, enabled); +} + +/*** +**** IndicatorDatetimeClock virtual functions +***/ + +static gchar ** +my_get_timezones (IndicatorDatetimeClock * clock G_GNUC_UNUSED) +{ + IndicatorDatetimeClockLive * self; + priv_t * p; + GHashTable * hash; + gchar ** timezones; + int i; + GHashTableIter iter; + gpointer key; + + self = INDICATOR_DATETIME_CLOCK_LIVE (clock); + p = self->priv; + + hash = g_hash_table_new (g_str_hash, g_str_equal); + + if (p->tz_file != NULL) + { + const gchar * tz = indicator_datetime_timezone_get_timezone (p->tz_file); + 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); + } + + timezones = 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)) + timezones[i++] = g_strdup (key); + g_hash_table_unref (hash); + return timezones; +} + +static GDateTime * +my_get_current_time (IndicatorDatetimeClock * clock) +{ + IndicatorDatetimeClockLive * self; + priv_t * p; + + self = INDICATOR_DATETIME_CLOCK_LIVE (clock); + p = self->priv; + + if (G_UNLIKELY (p->internal_timezone == NULL)) + update_internal_timezone (self); + + return g_date_time_new_now (p->internal_timezone); +} + +/*** +**** GObject virtual functions +***/ + +static void +my_dispose (GObject * o) +{ + IndicatorDatetimeClockLive * self; + priv_t * p; + + self = INDICATOR_DATETIME_CLOCK_LIVE(o); + p = self->priv; + + g_clear_pointer (&p->internal_timezone, g_time_zone_unref); + + if (p->settings != NULL) + { + g_signal_handlers_disconnect_by_data (p->settings, self); + g_clear_object (&p->settings); + } + + set_detect_location_enabled (self, FALSE); + + G_OBJECT_CLASS (indicator_datetime_clock_live_parent_class)->dispose (o); +} + +static void +my_finalize (GObject * o) +{ +#if 0 + IndicatorDatetimeClockLive * self; + priv_t * p; + + self = INDICATOR_DATETIME_CLOCK_LIVE(o); + p = self->priv; +#endif + + G_OBJECT_CLASS (indicator_datetime_clock_live_parent_class)->dispose (o); +} + +/*** +**** Instantiation +***/ + +static void +indicator_datetime_clock_live_class_init (IndicatorDatetimeClockLiveClass * klass) +{ + GObjectClass * object_class = G_OBJECT_CLASS (klass); + + object_class->dispose = my_dispose; + object_class->finalize = my_finalize; + + g_type_class_add_private (klass, + sizeof (IndicatorDatetimeClockLivePriv)); +} + +static void +indicator_datetime_clock_interface_init (IndicatorDatetimeClockInterface * iface) +{ + iface->get_timezones = my_get_timezones; + iface->get_current_time = my_get_current_time; +} + +static void +indicator_datetime_clock_live_init (IndicatorDatetimeClockLive * self) +{ + IndicatorDatetimeClockLivePriv * p; + + p = G_TYPE_INSTANCE_GET_PRIVATE (self, + INDICATOR_TYPE_DATETIME_CLOCK_LIVE, + IndicatorDatetimeClockLivePriv); + self->priv = p; + + p->settings = g_settings_new (SETTINGS_INTERFACE); + g_signal_connect (p->settings, "changed::" SETTINGS_SHOW_DETECTED_S, + G_CALLBACK(on_detect_location_changed), self); + + + on_detect_location_changed (self); +} + +/*** +**** Public API +***/ + +IndicatorDatetimeClock * +indicator_datetime_clock_live_new (void) +{ + gpointer o = g_object_new (INDICATOR_TYPE_DATETIME_CLOCK_LIVE, NULL); + + return INDICATOR_DATETIME_CLOCK (o); +} diff --git a/src/clock-live.h b/src/clock-live.h new file mode 100644 index 0000000..4425f5b --- /dev/null +++ b/src/clock-live.h @@ -0,0 +1,73 @@ +/* + * Copyright 2013 Canonical Ltd. + * + * Authors: + * Charles Kerr + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3, as published + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranties of + * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef __INDICATOR_DATETIME_CLOCK_LIVE__H__ +#define __INDICATOR_DATETIME_CLOCK_LIVE__H__ + +#include /* parent class */ + +#include "clock.h" + +G_BEGIN_DECLS + +#define INDICATOR_TYPE_DATETIME_CLOCK_LIVE \ + (indicator_datetime_clock_live_get_type()) + +#define INDICATOR_DATETIME_CLOCK_LIVE(o) \ + (G_TYPE_CHECK_INSTANCE_CAST ((o), \ + INDICATOR_TYPE_DATETIME_CLOCK_LIVE, \ + IndicatorDatetimeClockLive)) + +#define INDICATOR_DATETIME_CLOCK_LIVE_GET_CLASS(o) \ + (G_TYPE_INSTANCE_GET_CLASS ((o), \ + INDICATOR_TYPE_DATETIME_CLOCK_LIVE, \ + IndicatorDatetimeClockLiveClass)) + +#define INDICATOR_IS_DATETIME_CLOCK_LIVE(o) \ + (G_TYPE_CHECK_INSTANCE_TYPE ((o), \ + INDICATOR_TYPE_DATETIME_CLOCK_LIVE)) + +typedef struct _IndicatorDatetimeClockLive + IndicatorDatetimeClockLive; +typedef struct _IndicatorDatetimeClockLivePriv + IndicatorDatetimeClockLivePriv; +typedef struct _IndicatorDatetimeClockLiveClass + IndicatorDatetimeClockLiveClass; + +/** + * An IndicatorDatetimeClock which gives live clock times + * from timezones determined by geoclue and /etc/timezone + */ +struct _IndicatorDatetimeClockLive +{ + GObject parent_instance; + + IndicatorDatetimeClockLivePriv * priv; +}; + +struct _IndicatorDatetimeClockLiveClass +{ + GObjectClass parent_class; +}; + +IndicatorDatetimeClock * indicator_datetime_clock_live_new (void); + +G_END_DECLS + +#endif /* __INDICATOR_DATETIME_CLOCK_LIVE__H__ */ diff --git a/src/clock.c b/src/clock.c new file mode 100644 index 0000000..1abdd21 --- /dev/null +++ b/src/clock.c @@ -0,0 +1,110 @@ +/* + * Copyright 2013 Canonical Ltd. + * + * Authors: + * Charles Kerr + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3, as published + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranties of + * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#include "clock.h" + +enum +{ + SIGNAL_CHANGED, + SIGNAL_LAST +}; + +static guint signals[SIGNAL_LAST] = { 0 }; + +G_DEFINE_INTERFACE (IndicatorDatetimeClock, + indicator_datetime_clock, + 0); + +static void +indicator_datetime_clock_default_init (IndicatorDatetimeClockInterface * klass) +{ + signals[SIGNAL_CHANGED] = g_signal_new ( + "changed", + G_TYPE_FROM_CLASS(klass), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (IndicatorDatetimeClockInterface, changed), + NULL, NULL, + g_cclosure_marshal_VOID__VOID, + G_TYPE_NONE, 0); +} + +/*** +**** PUBLIC API +***/ + +/** + * Get a strv of timezones. + * + * Return value: (element-type char*) + * (transfer full): + * array of timezone strings + */ +gchar ** +indicator_datetime_clock_get_timezones (IndicatorDatetimeClock * self) +{ + gchar ** timezones; + IndicatorDatetimeClockInterface * iface; + + g_return_val_if_fail (INDICATOR_IS_DATETIME_CLOCK(self), NULL); + iface = INDICATOR_DATETIME_CLOCK_GET_INTERFACE(self); + + if (iface->get_timezones != NULL) + timezones = iface->get_timezones (self); + else + timezones = NULL; + + return timezones; +} + +/** + * Get the current time. + * + * Return value: (element-type GDateTime*) + * (transfer full): + * the current time. + */ +GDateTime * +indicator_datetime_clock_get_current_time (IndicatorDatetimeClock * self) +{ + GDateTime * now; + IndicatorDatetimeClockInterface * iface; + + g_return_val_if_fail (INDICATOR_IS_DATETIME_CLOCK(self), NULL); + iface = INDICATOR_DATETIME_CLOCK_GET_INTERFACE(self); + + if (iface->get_current_time != NULL) + now = iface->get_current_time (self); + else + now = NULL; + + return now; +} + +/** + * Emits the "changed" signal. + * + * This should only be called by subclasses. + */ +void +indicator_datetime_clock_emit_changed (IndicatorDatetimeClock * self) +{ + g_return_if_fail (INDICATOR_IS_DATETIME_CLOCK (self)); + + g_signal_emit (self, signals[SIGNAL_CHANGED], 0, NULL); +} diff --git a/src/clock.h b/src/clock.h new file mode 100644 index 0000000..8932895 --- /dev/null +++ b/src/clock.h @@ -0,0 +1,76 @@ +/* + * Copyright 2013 Canonical Ltd. + * + * Authors: + * Charles Kerr + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3, as published + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranties of + * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef __INDICATOR_DATETIME_CLOCK__H__ +#define __INDICATOR_DATETIME_CLOCK__H__ + +#include + +G_BEGIN_DECLS + +#define INDICATOR_TYPE_DATETIME_CLOCK \ + (indicator_datetime_clock_get_type ()) + +#define INDICATOR_DATETIME_CLOCK(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST ((obj), \ + INDICATOR_TYPE_DATETIME_CLOCK, \ + IndicatorDatetimeClock)) + +#define INDICATOR_IS_DATETIME_CLOCK(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE ((obj), INDICATOR_TYPE_DATETIME_CLOCK)) + +#define INDICATOR_DATETIME_CLOCK_GET_INTERFACE(inst) \ + (G_TYPE_INSTANCE_GET_INTERFACE ((inst), \ + INDICATOR_TYPE_DATETIME_CLOCK, \ + IndicatorDatetimeClockInterface)) + +typedef struct _IndicatorDatetimeClock + IndicatorDatetimeClock; + +typedef struct _IndicatorDatetimeClockInterface + IndicatorDatetimeClockInterface; + +struct _IndicatorDatetimeClockInterface +{ + GTypeInterface parent_iface; + + /* signals */ + void (*changed) (IndicatorDatetimeClock * self); + + /* virtual functions */ + gchar** (*get_timezones) (IndicatorDatetimeClock * self); + GDateTime* (*get_current_time) (IndicatorDatetimeClock * self); +}; + +GType indicator_datetime_clock_get_type (void); + +/*** +**** +***/ + +gchar ** indicator_datetime_clock_get_timezones (IndicatorDatetimeClock * clock); + +GDateTime * indicator_datetime_clock_get_current_time (IndicatorDatetimeClock * clock); + +void indicator_datetime_clock_emit_changed (IndicatorDatetimeClock * clock); + + +G_END_DECLS + +#endif /* __INDICATOR_DATETIME_CLOCK__H__ */ diff --git a/src/main.c b/src/main.c index f791683..9305794 100644 --- a/src/main.c +++ b/src/main.c @@ -26,36 +26,26 @@ #include #include +#include "clock-live.h" #include "planner-eds.h" -#include "planner-mock.h" #include "service.h" /*** **** ***/ -/* When enabled, new alarms will show up every minute to test snap decisions */ -static gboolean test_alarms = FALSE; - -static GOptionEntry entries[] = { - { "test-alarms", '\0', 0, G_OPTION_ARG_NONE, &test_alarms, "Test Alarms", NULL }, - { NULL } -}; - static void on_name_lost (gpointer instance G_GNUC_UNUSED, gpointer loop) { g_message ("exiting: service couldn't acquire or lost ownership of busname"); - if (!test_alarms) - g_main_loop_quit ((GMainLoop*)loop); + g_main_loop_quit ((GMainLoop*)loop); } int main (int argc G_GNUC_UNUSED, char ** argv G_GNUC_UNUSED) { - GOptionContext * context; - GError * error; + IndicatorDatetimeClock * clock; IndicatorDatetimePlanner * planner; IndicatorDatetimeService * service; GMainLoop * loop; @@ -69,28 +59,10 @@ main (int argc G_GNUC_UNUSED, char ** argv G_GNUC_UNUSED) if (!notify_init ("indicator-datetime-service")) g_critical ("libnotify initialization failed"); - /* parse command-line options */ - context = g_option_context_new (NULL); - g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE); - if (!g_option_context_parse (context, &argc, &argv, &error)) - { - g_print("option parsing failed: %s\n", error->message); - return EXIT_FAILURE; - } - - /* set up the planner */ - if (test_alarms) - { - g_message ("Using fake appointment book for testing alarms."); - planner = indicator_datetime_planner_mock_new (); - } - else - { - planner = indicator_datetime_planner_eds_new (); - } - /* run */ - service = indicator_datetime_service_new (planner); + clock = indicator_datetime_clock_live_new (); + planner = indicator_datetime_planner_eds_new (); + service = indicator_datetime_service_new (clock, planner); loop = g_main_loop_new (NULL, FALSE); g_signal_connect (service, INDICATOR_DATETIME_SERVICE_SIGNAL_NAME_LOST, G_CALLBACK(on_name_lost), loop); @@ -100,5 +72,6 @@ main (int argc G_GNUC_UNUSED, char ** argv G_GNUC_UNUSED) g_main_loop_unref (loop); g_object_unref (service); g_object_unref (planner); + g_object_unref (clock); return 0; } diff --git a/src/planner-mock.c b/src/planner-mock.c deleted file mode 100644 index e67ad7e..0000000 --- a/src/planner-mock.c +++ /dev/null @@ -1,178 +0,0 @@ -/* - * Copyright 2013 Canonical Ltd. - * - * Authors: - * Charles Kerr - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 3, as published - * by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranties of - * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR - * PURPOSE. See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -#include "config.h" - -#include "planner-mock.h" - -struct _IndicatorDatetimePlannerMockPriv -{ - gboolean is_configured; -}; - -typedef IndicatorDatetimePlannerMockPriv priv_t; - -G_DEFINE_TYPE (IndicatorDatetimePlannerMock, - indicator_datetime_planner_mock, - INDICATOR_TYPE_DATETIME_PLANNER) - -/*** -**** IndicatorDatetimePlanner virtual funcs -***/ - -static void -my_get_appointments (IndicatorDatetimePlanner * planner, - GDateTime * begin_datetime, - GDateTime * end_datetime G_GNUC_UNUSED, - GAsyncReadyCallback callback, - gpointer user_data) -{ - GTask * task; - GSList * appointments; - struct IndicatorDatetimeAppt * appt; - struct IndicatorDatetimeAppt * prev; - - task = g_task_new (planner, NULL, callback, user_data); - - /** - *** Build the appointments list - **/ - - appointments = NULL; - - /* add a daily appointment that occurs at the beginning of the next minute */ - appt = g_slice_new0 (struct IndicatorDatetimeAppt); - appt->is_daily = TRUE; - appt->begin = g_date_time_add_seconds (begin_datetime, 60-g_date_time_get_seconds(begin_datetime)); - appt->end = g_date_time_add_minutes (appt->begin, 1); - appt->color = g_strdup ("#00FF00"); - appt->is_event = TRUE; - appt->summary = g_strdup ("Daily alarm"); - appt->uid = g_strdup ("this uid isn't very random."); - appt->has_alarms = TRUE; - appt->url = g_strdup ("alarm:///some-alarm-info-goes-here"); - appointments = g_slist_prepend (appointments, appt); - prev = appt; - - /* and add one for a minute later that has an alarm uri */ - appt = g_slice_new0 (struct IndicatorDatetimeAppt); - appt->is_daily = TRUE; - appt->begin = g_date_time_add_minutes (prev->end, 1); - appt->end = g_date_time_add_minutes (appt->begin, 1); - appt->color = g_strdup ("#0000FF"); - appt->is_event = TRUE; - appt->summary = g_strdup ("Second Daily alarm"); - appt->uid = g_strdup ("this uid isn't very random either."); - appt->has_alarms = FALSE; - appointments = g_slist_prepend (appointments, appt); - - /* done */ - g_task_return_pointer (task, appointments, NULL); - g_object_unref (task); -} - -static GSList * -my_get_appointments_finish (IndicatorDatetimePlanner * self G_GNUC_UNUSED, - GAsyncResult * res, - GError ** error) -{ - return g_task_propagate_pointer (G_TASK(res), error); -} - -static gboolean -my_is_configured (IndicatorDatetimePlanner * planner) -{ - IndicatorDatetimePlannerMock * self; - self = INDICATOR_DATETIME_PLANNER_MOCK (planner); - return self->priv->is_configured; -} - -static void -my_activate (IndicatorDatetimePlanner * self G_GNUC_UNUSED) -{ - g_message ("%s %s", G_STRLOC, G_STRFUNC); -} - -static void -my_activate_time (IndicatorDatetimePlanner * self G_GNUC_UNUSED, - GDateTime * activate_time) -{ - gchar * str = g_date_time_format (activate_time, "%F %T"); - g_message ("%s %s: %s", G_STRLOC, G_STRFUNC, str); - g_free (str); -} - -/*** -**** GObject virtual funcs -***/ - -static void -my_dispose (GObject * o) -{ - G_OBJECT_CLASS (indicator_datetime_planner_mock_parent_class)->dispose (o); -} - -/*** -**** Instantiation -***/ - -static void -indicator_datetime_planner_mock_class_init (IndicatorDatetimePlannerMockClass * klass) -{ - GObjectClass * object_class; - IndicatorDatetimePlannerClass * planner_class; - - object_class = G_OBJECT_CLASS (klass); - object_class->dispose = my_dispose; - - planner_class = INDICATOR_DATETIME_PLANNER_CLASS (klass); - planner_class->is_configured = my_is_configured; - planner_class->activate = my_activate; - planner_class->activate_time = my_activate_time; - planner_class->get_appointments = my_get_appointments; - planner_class->get_appointments_finish = my_get_appointments_finish; - - g_type_class_add_private (klass, sizeof (IndicatorDatetimePlannerMockPriv)); -} - -static void -indicator_datetime_planner_mock_init (IndicatorDatetimePlannerMock * self) -{ - priv_t * p; - - p = G_TYPE_INSTANCE_GET_PRIVATE (self, - INDICATOR_TYPE_DATETIME_PLANNER_MOCK, - IndicatorDatetimePlannerMockPriv); - - p->is_configured = TRUE; - - self->priv = p; -} - -/*** -**** Public -***/ - -IndicatorDatetimePlanner * -indicator_datetime_planner_mock_new (void) -{ - gpointer o = g_object_new (INDICATOR_TYPE_DATETIME_PLANNER_MOCK, NULL); - - return INDICATOR_DATETIME_PLANNER (o); -} diff --git a/src/planner-mock.h b/src/planner-mock.h deleted file mode 100644 index 8d7d7c2..0000000 --- a/src/planner-mock.h +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright 2013 Canonical Ltd. - * - * Authors: - * Charles Kerr - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 3, as published - * by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranties of - * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR - * PURPOSE. See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -#ifndef __INDICATOR_DATETIME_PLANNER_MOCK__H__ -#define __INDICATOR_DATETIME_PLANNER_MOCK__H__ - -#include "planner.h" /* parent class */ - -G_BEGIN_DECLS - -#define INDICATOR_TYPE_DATETIME_PLANNER_MOCK (indicator_datetime_planner_mock_get_type()) -#define INDICATOR_DATETIME_PLANNER_MOCK(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), INDICATOR_TYPE_DATETIME_PLANNER_MOCK, IndicatorDatetimePlannerMock)) -#define INDICATOR_DATETIME_PLANNER_MOCK_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), INDICATOR_TYPE_DATETIME_PLANNER_MOCK, IndicatorDatetimePlannerMockClass)) -#define INDICATOR_IS_DATETIME_PLANNER_MOCK(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), INDICATOR_TYPE_DATETIME_PLANNER_MOCK)) - -typedef struct _IndicatorDatetimePlannerMock IndicatorDatetimePlannerMock; -typedef struct _IndicatorDatetimePlannerMockPriv IndicatorDatetimePlannerMockPriv; -typedef struct _IndicatorDatetimePlannerMockClass IndicatorDatetimePlannerMockClass; - -GType indicator_datetime_planner_mock_get_type (void); - -/** - * An IndicatorDatetimePlanner which uses Evolution Data Server - * to get its list of appointments. - */ -struct _IndicatorDatetimePlannerMock -{ - /*< private >*/ - IndicatorDatetimePlanner parent; - IndicatorDatetimePlannerMockPriv * priv; -}; - -struct _IndicatorDatetimePlannerMockClass -{ - IndicatorDatetimePlannerClass parent_class; -}; - -IndicatorDatetimePlanner * indicator_datetime_planner_mock_new (void); - -G_END_DECLS - -#endif /* __INDICATOR_DATETIME_PLANNER_MOCK__H__ */ diff --git a/src/service.c b/src/service.c index 08945bc..b777b8c 100644 --- a/src/service.c +++ b/src/service.c @@ -29,8 +29,6 @@ #include #include "dbus-shared.h" -#include "timezone-file.h" -#include "timezone-geoclue.h" #include "service.h" #include "settings-shared.h" #include "utils.h" @@ -54,6 +52,7 @@ static guint signals[LAST_SIGNAL] = { 0 }; enum { PROP_0, + PROP_CLOCK, PROP_PLANNER, PROP_LAST }; @@ -101,13 +100,9 @@ struct _IndicatorDatetimeServicePrivate GSettings * settings; - IndicatorDatetimeTimezone * tz_file; - IndicatorDatetimeTimezone * tz_geoclue; + IndicatorDatetimeClock * clock; IndicatorDatetimePlanner * planner; - /* cached GTimeZone for use by indicator_datetime_service_get_localtime() */ - GTimeZone * internal_timezone; - /* the clock app's icon filename */ gchar * clock_app_icon_filename; @@ -168,6 +163,12 @@ indicator_clear_timer (guint * tag) } } +static inline GDateTime * +indicator_datetime_service_get_localtime (IndicatorDatetimeService * self) +{ + return indicator_datetime_clock_get_current_time (self->priv->clock); +} + /*** **** ***/ @@ -589,23 +590,6 @@ set_alarm_timer (IndicatorDatetimeService * self) **** ***/ -static void -update_internal_timezone (IndicatorDatetimeService * self) -{ - priv_t * p = self->priv; - const char * id; - - /* find the id from tz_file or tz_geoclue if possible; NULL otherwise */ - id = NULL; - if (!id && p->tz_file) - id = indicator_datetime_timezone_get_timezone (p->tz_file); - if (!id && p->tz_geoclue) - id = indicator_datetime_timezone_get_timezone (p->tz_geoclue); - - g_clear_pointer (&p->internal_timezone, g_time_zone_unref); - p->internal_timezone = g_time_zone_new (id); -} - /** * General purpose handler for rebuilding sections and restarting their timers * when time jumps for whatever reason: @@ -624,7 +608,6 @@ on_local_time_jumped (IndicatorDatetimeService * self) 1. rebuild the necessary states / menuitems when time jumps 2. restart the timers so their new wait interval is correct */ - update_internal_timezone (self); on_header_timer (self); on_timezone_timer (self); } @@ -1100,61 +1083,6 @@ create_desktop_appointments_section (IndicatorDatetimeService * self) **** ***/ -static void -on_current_timezone_changed (IndicatorDatetimeService * self) -{ - on_local_time_jumped (self); -} - -/* When the 'auto-detect timezone' boolean setting changes, - start or stop watching geoclue and /etc/timezone */ -static void -set_detect_location_enabled (IndicatorDatetimeService * self, gboolean enabled) -{ - gboolean changed = FALSE; - priv_t * p = self->priv; - - /* geoclue */ - - if (!p->tz_geoclue && enabled) - { - p->tz_geoclue = indicator_datetime_timezone_geoclue_new (); - g_signal_connect_swapped (p->tz_geoclue, "notify::timezone", - G_CALLBACK(on_current_timezone_changed), - self); - changed = TRUE; - } - else if (p->tz_geoclue && !enabled) - { - g_signal_handlers_disconnect_by_func (p->tz_geoclue, - on_current_timezone_changed, - self); - g_clear_object (&p->tz_geoclue); - changed = TRUE; - } - - /* timezone file */ - - 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; - } - - if (changed) - on_current_timezone_changed (self); -} /* A temp struct used by create_locations_section() for pruning duplicates and sorting. */ @@ -1240,44 +1168,30 @@ create_locations_section (IndicatorDatetimeService * self) GSList * l; GSList * locations = NULL; gchar ** user_locations; - gboolean visible; - IndicatorDatetimeTimezone * detected_timezones[2]; + gchar ** detected_timezones; priv_t * p = self->priv; GDateTime * now = indicator_datetime_service_get_localtime (self); - set_detect_location_enabled (self, - g_settings_get_boolean (p->settings, SETTINGS_SHOW_DETECTED_S)); - menu = g_menu_new (); /*** - **** Build a list of locations to add: use geo_timezone, - **** current_timezone, and SETTINGS_LOCATIONS_S, but omit duplicates. + **** Build a list of locations to add, omitting duplicates ***/ - /* maybe add the auto-detected timezones */ - detected_timezones[0] = p->tz_geoclue; - detected_timezones[1] = p->tz_file; - visible = g_settings_get_boolean (p->settings, SETTINGS_SHOW_DETECTED_S); - for (i=0; iclock); + for (i=0; detected_timezones && detected_timezones[i]; i++) { - if (detected_timezones[i] != NULL) - { - const char * tz = indicator_datetime_timezone_get_timezone (detected_timezones[i]); - if (tz && *tz) - { - gchar * name = get_current_zone_name (tz, p->settings); - locations = locations_add (locations, tz, name, visible); - g_free (name); - } - } + const char * tz = detected_timezones[i]; + gchar * name = get_current_zone_name (tz, p->settings); + locations = locations_add (locations, tz, name, TRUE); } + g_strfreev (detected_timezones); /* maybe add the user-specified locations */ user_locations = g_settings_get_strv (p->settings, SETTINGS_LOCATIONS_S); if (user_locations != NULL) { - visible = g_settings_get_boolean (p->settings, SETTINGS_SHOW_LOCATIONS_S); + const gboolean visible = g_settings_get_boolean (p->settings, SETTINGS_SHOW_LOCATIONS_S); for (i=0; user_locations[i] != NULL; i++) { @@ -1735,6 +1649,9 @@ rebuild_now (IndicatorDatetimeService * self, int sections) struct ProfileMenuInfo * desktop = &p->menus[PROFILE_DESKTOP]; struct ProfileMenuInfo * greeter = &p->menus[PROFILE_GREETER]; + if (p->actions == NULL) + return; + if (sections & SECTION_HEADER) { g_simple_action_set_state (p->desktop_header_action, @@ -2088,6 +2005,10 @@ my_get_property (GObject * o, switch (property_id) { + case PROP_CLOCK: + g_value_set_object (value, self->priv->clock); + break; + case PROP_PLANNER: g_value_set_object (value, self->priv->planner); break; @@ -2107,6 +2028,10 @@ my_set_property (GObject * o, switch (property_id) { + case PROP_CLOCK: + indicator_datetime_service_set_clock (self, g_value_get_object (value)); + break; + case PROP_PLANNER: indicator_datetime_service_set_planner (self, g_value_get_object (value)); break; @@ -2138,8 +2063,7 @@ my_dispose (GObject * o) g_clear_object (&p->cancellable); } - set_detect_location_enabled (self, FALSE); - + indicator_datetime_service_set_clock (self, NULL); indicator_datetime_service_set_planner (self, NULL); if (p->login1_manager != NULL) @@ -2165,7 +2089,6 @@ my_dispose (GObject * o) for (i=0; imenus[i].menu); - g_clear_pointer (&p->internal_timezone, g_time_zone_unref); g_clear_object (&p->calendar_action); g_clear_object (&p->desktop_header_action); g_clear_object (&p->phone_header_action); @@ -2194,9 +2117,27 @@ my_finalize (GObject * o) static void indicator_datetime_service_init (IndicatorDatetimeService * self) { - guint i, n; priv_t * p; + + /* init the priv pointer */ + + p = G_TYPE_INSTANCE_GET_PRIVATE (self, + INDICATOR_TYPE_DATETIME_SERVICE, + IndicatorDatetimeServicePrivate); + self->priv = p; + + p->cancellable = g_cancellable_new (); + + p->settings = g_settings_new (SETTINGS_INTERFACE); +} + +static void +my_constructed (GObject * gself) +{ + IndicatorDatetimeService * self = INDICATOR_DATETIME_SERVICE (gself); + priv_t * p = self->priv; GString * gstr = g_string_new (NULL); + guint i, n; /* these are the settings that affect the contents of the respective sections */ @@ -2233,20 +2174,10 @@ indicator_datetime_service_init (IndicatorDatetimeService * self) }; - /* init the priv pointer */ - - p = G_TYPE_INSTANCE_GET_PRIVATE (self, - INDICATOR_TYPE_DATETIME_SERVICE, - IndicatorDatetimeServicePrivate); - self->priv = p; - - p->cancellable = g_cancellable_new (); - /*** - **** Create the settings object and listen for changes + **** Listen for settings changes ***/ - p->settings = g_settings_new (SETTINGS_INTERFACE); for (i=0, n=G_N_ELEMENTS(header_settings); idispose = my_dispose; object_class->finalize = my_finalize; + object_class->constructed = my_constructed; object_class->get_property = my_get_property; object_class->set_property = my_set_property; @@ -2345,6 +2277,12 @@ indicator_datetime_service_class_init (IndicatorDatetimeServiceClass * klass) properties[PROP_0] = NULL; + properties[PROP_CLOCK] = g_param_spec_object ("clock", + "Clock", + "The clock", + G_TYPE_OBJECT, + flags); + properties[PROP_PLANNER] = g_param_spec_object ("planner", "Planner", "The appointment provider", @@ -2359,28 +2297,17 @@ indicator_datetime_service_class_init (IndicatorDatetimeServiceClass * klass) ***/ IndicatorDatetimeService * -indicator_datetime_service_new (IndicatorDatetimePlanner * planner) +indicator_datetime_service_new (IndicatorDatetimeClock * clock, + IndicatorDatetimePlanner * planner) { GObject * o = g_object_new (INDICATOR_TYPE_DATETIME_SERVICE, + "clock", clock, "planner", planner, NULL); return INDICATOR_DATETIME_SERVICE (o); } -/* This currently just returns the system time, - As we add test coverage, we'll need this to bypass the system time. */ -GDateTime * -indicator_datetime_service_get_localtime (IndicatorDatetimeService * self) -{ - priv_t * p = self->priv; - - if (G_UNLIKELY (p->internal_timezone == NULL)) - update_internal_timezone (self); - - return g_date_time_new_now (p->internal_timezone); -} - void indicator_datetime_service_set_calendar_date (IndicatorDatetimeService * self, GDateTime * date) @@ -2400,6 +2327,43 @@ indicator_datetime_service_set_calendar_date (IndicatorDatetimeService * self, update_appointment_lists (self); } +static void +on_clock_changed (IndicatorDatetimeService * self) +{ + on_local_time_jumped (self); +} + +void +indicator_datetime_service_set_clock (IndicatorDatetimeService * self, + IndicatorDatetimeClock * clock) +{ + priv_t * p; + + g_return_if_fail (INDICATOR_IS_DATETIME_SERVICE (self)); + g_return_if_fail ((clock == NULL) || INDICATOR_IS_DATETIME_CLOCK (clock)); + + p = self->priv; + + /* clear the old clock */ + + if (p->clock != NULL) + { + g_signal_handlers_disconnect_by_data (p->clock, self); + g_clear_object (&p->clock); + } + + /* set the new clock */ + + if (clock != NULL) + { + p->clock = g_object_ref (clock); + + g_signal_connect_swapped (p->clock, "changed", + G_CALLBACK(on_clock_changed), self); + on_clock_changed (self); + } +} + void indicator_datetime_service_set_planner (IndicatorDatetimeService * self, IndicatorDatetimePlanner * planner) @@ -2407,7 +2371,7 @@ indicator_datetime_service_set_planner (IndicatorDatetimeService * self, priv_t * p; g_return_if_fail (INDICATOR_IS_DATETIME_SERVICE (self)); - g_return_if_fail (INDICATOR_IS_DATETIME_PLANNER (planner)); + g_return_if_fail ((planner == NULL) || INDICATOR_IS_DATETIME_PLANNER (planner)); p = self->priv; diff --git a/src/service.h b/src/service.h index 25bb59a..d38db72 100644 --- a/src/service.h +++ b/src/service.h @@ -22,6 +22,8 @@ #include #include + +#include "clock.h" #include "planner.h" G_BEGIN_DECLS @@ -63,9 +65,8 @@ struct _IndicatorDatetimeServiceClass GType indicator_datetime_service_get_type (void); -IndicatorDatetimeService * indicator_datetime_service_new (IndicatorDatetimePlanner * planner); - -GDateTime * indicator_datetime_service_get_localtime (IndicatorDatetimeService * service); +IndicatorDatetimeService * indicator_datetime_service_new (IndicatorDatetimeClock * clock, + IndicatorDatetimePlanner * planner); void indicator_datetime_service_set_calendar_date (IndicatorDatetimeService * self, GDateTime * date); @@ -74,6 +75,9 @@ void indicator_datetime_service_set_planner (IndicatorDatetimeService * self, IndicatorDatetimePlanner * planner); +void indicator_datetime_service_set_clock (IndicatorDatetimeService * self, + IndicatorDatetimeClock * clock); + G_END_DECLS -- cgit v1.2.3 From 758a4880f645242f1c7753990dc46f880a7e9de8 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 17 Oct 2013 17:48:06 -0500 Subject: cleanup: dead code removal, fix comments, smaller implementation of update_internal_localtime() --- src/clock-live.c | 47 ++++++++++++++--------------------------------- src/clock.c | 6 +++--- src/clock.h | 4 ++-- src/main.c | 6 ++++-- src/service.c | 2 +- 5 files changed, 24 insertions(+), 41 deletions(-) (limited to 'src') diff --git a/src/clock-live.c b/src/clock-live.c index 6e694ed..564d990 100644 --- a/src/clock-live.c +++ b/src/clock-live.c @@ -35,7 +35,7 @@ struct _IndicatorDatetimeClockLivePriv { GSettings * settings; - /* cached GTimeZone for use by indicator_datetime_service_get_localtime() */ + /* cached GTimeZone for use by get_localtime() */ GTimeZone * internal_timezone; IndicatorDatetimeTimezone * tz_file; @@ -58,39 +58,10 @@ G_DEFINE_TYPE_WITH_CODE ( G_IMPLEMENT_INTERFACE (INDICATOR_TYPE_DATETIME_CLOCK, indicator_datetime_clock_interface_init)); -/*** -**** Convenience func -***/ - -#if 0 -static void -emit_changed (IndicatorDatetimeClockLive * self) -{ - indicator_datetime_clock_emit_changed (INDICATOR_DATETIME_CLOCK (self)); -} -#endif - /*** **** Timezones ***/ -static void -update_internal_timezone (IndicatorDatetimeClockLive * self) -{ - priv_t * p = self->priv; - const char * id; - - /* find the id from tz_file or tz_geoclue if possible; NULL otherwise */ - id = NULL; - if (p->tz_file != NULL ) - id = indicator_datetime_timezone_get_timezone (p->tz_file); - if (!id && p->tz_geoclue) - id = indicator_datetime_timezone_get_timezone (p->tz_geoclue); - - g_clear_pointer (&p->internal_timezone, g_time_zone_unref); - p->internal_timezone = g_time_zone_new (id); -} - static void on_current_timezone_changed (IndicatorDatetimeClockLive * self) { @@ -159,7 +130,7 @@ on_detect_location_changed (IndicatorDatetimeClockLive * self) ***/ static gchar ** -my_get_timezones (IndicatorDatetimeClock * clock G_GNUC_UNUSED) +my_get_timezones (IndicatorDatetimeClock * clock) { IndicatorDatetimeClockLive * self; priv_t * p; @@ -197,8 +168,18 @@ my_get_timezones (IndicatorDatetimeClock * clock G_GNUC_UNUSED) return timezones; } +static void +update_internal_timezone (IndicatorDatetimeClockLive * self) +{ + priv_t * p = self->priv; + gchar ** timezones = my_get_timezones (INDICATOR_DATETIME_CLOCK (self)); + g_clear_pointer (&p->internal_timezone, g_time_zone_unref); + p->internal_timezone = g_time_zone_new (timezones ? timezones[0] : NULL); + g_strfreev (timezones); +} + static GDateTime * -my_get_current_time (IndicatorDatetimeClock * clock) +my_get_localtime (IndicatorDatetimeClock * clock) { IndicatorDatetimeClockLive * self; priv_t * p; @@ -271,8 +252,8 @@ indicator_datetime_clock_live_class_init (IndicatorDatetimeClockLiveClass * klas static void indicator_datetime_clock_interface_init (IndicatorDatetimeClockInterface * iface) { + iface->get_localtime = my_get_localtime; iface->get_timezones = my_get_timezones; - iface->get_current_time = my_get_current_time; } static void diff --git a/src/clock.c b/src/clock.c index 1abdd21..a5cefee 100644 --- a/src/clock.c +++ b/src/clock.c @@ -80,7 +80,7 @@ indicator_datetime_clock_get_timezones (IndicatorDatetimeClock * self) * the current time. */ GDateTime * -indicator_datetime_clock_get_current_time (IndicatorDatetimeClock * self) +indicator_datetime_clock_get_localtime (IndicatorDatetimeClock * self) { GDateTime * now; IndicatorDatetimeClockInterface * iface; @@ -88,8 +88,8 @@ indicator_datetime_clock_get_current_time (IndicatorDatetimeClock * self) g_return_val_if_fail (INDICATOR_IS_DATETIME_CLOCK(self), NULL); iface = INDICATOR_DATETIME_CLOCK_GET_INTERFACE(self); - if (iface->get_current_time != NULL) - now = iface->get_current_time (self); + if (iface->get_localtime != NULL) + now = iface->get_localtime (self); else now = NULL; diff --git a/src/clock.h b/src/clock.h index 8932895..0c10dab 100644 --- a/src/clock.h +++ b/src/clock.h @@ -55,7 +55,7 @@ struct _IndicatorDatetimeClockInterface /* virtual functions */ gchar** (*get_timezones) (IndicatorDatetimeClock * self); - GDateTime* (*get_current_time) (IndicatorDatetimeClock * self); + GDateTime* (*get_localtime) (IndicatorDatetimeClock * self); }; GType indicator_datetime_clock_get_type (void); @@ -66,7 +66,7 @@ GType indicator_datetime_clock_get_type (void); gchar ** indicator_datetime_clock_get_timezones (IndicatorDatetimeClock * clock); -GDateTime * indicator_datetime_clock_get_current_time (IndicatorDatetimeClock * clock); +GDateTime * indicator_datetime_clock_get_localtime (IndicatorDatetimeClock * clock); void indicator_datetime_clock_emit_changed (IndicatorDatetimeClock * clock); diff --git a/src/main.c b/src/main.c index 9305794..dc08419 100644 --- a/src/main.c +++ b/src/main.c @@ -59,17 +59,19 @@ main (int argc G_GNUC_UNUSED, char ** argv G_GNUC_UNUSED) if (!notify_init ("indicator-datetime-service")) g_critical ("libnotify initialization failed"); - /* run */ + /* create the service */ clock = indicator_datetime_clock_live_new (); planner = indicator_datetime_planner_eds_new (); service = indicator_datetime_service_new (clock, planner); + + /* run */ loop = g_main_loop_new (NULL, FALSE); g_signal_connect (service, INDICATOR_DATETIME_SERVICE_SIGNAL_NAME_LOST, G_CALLBACK(on_name_lost), loop); g_main_loop_run (loop); + g_main_loop_unref (loop); /* cleanup */ - g_main_loop_unref (loop); g_object_unref (service); g_object_unref (planner); g_object_unref (clock); diff --git a/src/service.c b/src/service.c index b777b8c..f2b0b34 100644 --- a/src/service.c +++ b/src/service.c @@ -166,7 +166,7 @@ indicator_clear_timer (guint * tag) static inline GDateTime * indicator_datetime_service_get_localtime (IndicatorDatetimeService * self) { - return indicator_datetime_clock_get_current_time (self->priv->clock); + return indicator_datetime_clock_get_localtime (self->priv->clock); } /*** -- cgit v1.2.3 From 1a898e4eada6f50bd6f3e3b227d1ab0e143ec06d Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 17 Oct 2013 18:15:33 -0500 Subject: cache the timezone strv; lazy-rebuilding it when needed --- src/clock-live.c | 54 +++++++++++++++++++++++------------------------------- src/clock.c | 4 ++-- src/clock.h | 8 ++++---- src/service.c | 3 +-- 4 files changed, 30 insertions(+), 39 deletions(-) (limited to 'src') diff --git a/src/clock-live.c b/src/clock-live.c index 564d990..a5e42ec 100644 --- a/src/clock-live.c +++ b/src/clock-live.c @@ -35,11 +35,9 @@ struct _IndicatorDatetimeClockLivePriv { GSettings * settings; - /* cached GTimeZone for use by get_localtime() */ - GTimeZone * internal_timezone; - IndicatorDatetimeTimezone * tz_file; IndicatorDatetimeTimezone * tz_geoclue; + gchar ** timezones; }; typedef IndicatorDatetimeClockLivePriv priv_t; @@ -65,6 +63,8 @@ G_DEFINE_TYPE_WITH_CODE ( static void on_current_timezone_changed (IndicatorDatetimeClockLive * self) { + g_clear_pointer (&self->priv->timezones, g_strfreev); + indicator_datetime_clock_emit_changed (INDICATOR_DATETIME_CLOCK (self)); } @@ -129,18 +129,15 @@ on_detect_location_changed (IndicatorDatetimeClockLive * self) **** IndicatorDatetimeClock virtual functions ***/ -static gchar ** -my_get_timezones (IndicatorDatetimeClock * clock) +static void +rebuild_timezone_strv (IndicatorDatetimeClockLive * self) { - IndicatorDatetimeClockLive * self; priv_t * p; GHashTable * hash; - gchar ** timezones; int i; GHashTableIter iter; gpointer key; - self = INDICATOR_DATETIME_CLOCK_LIVE (clock); p = self->priv; hash = g_hash_table_new (g_str_hash, g_str_equal); @@ -159,38 +156,35 @@ my_get_timezones (IndicatorDatetimeClock * clock) g_hash_table_add (hash, (gpointer) tz); } - timezones = g_new0 (gchar*, g_hash_table_size(hash) + 1); + g_strfreev (p->timezones); + p->timezones = 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)) - timezones[i++] = g_strdup (key); + p->timezones[i++] = g_strdup (key); g_hash_table_unref (hash); - return timezones; } -static void -update_internal_timezone (IndicatorDatetimeClockLive * self) +static const gchar ** +my_get_timezones (IndicatorDatetimeClock * clock) { + IndicatorDatetimeClockLive * self = INDICATOR_DATETIME_CLOCK_LIVE (clock); priv_t * p = self->priv; - gchar ** timezones = my_get_timezones (INDICATOR_DATETIME_CLOCK (self)); - g_clear_pointer (&p->internal_timezone, g_time_zone_unref); - p->internal_timezone = g_time_zone_new (timezones ? timezones[0] : NULL); - g_strfreev (timezones); + + if (p->timezones == NULL) + rebuild_timezone_strv (self); + + return (const gchar **) p->timezones; } static GDateTime * my_get_localtime (IndicatorDatetimeClock * clock) { - IndicatorDatetimeClockLive * self; - priv_t * p; - - self = INDICATOR_DATETIME_CLOCK_LIVE (clock); - p = self->priv; - - if (G_UNLIKELY (p->internal_timezone == NULL)) - update_internal_timezone (self); - - return g_date_time_new_now (p->internal_timezone); + const gchar ** zones = my_get_timezones (clock); + GTimeZone * zone = g_time_zone_new (zones ? zones[0] : NULL); + GDateTime * time = g_date_time_new_now (zone); + g_time_zone_unref (zone); + return time; } /*** @@ -206,8 +200,6 @@ my_dispose (GObject * o) self = INDICATOR_DATETIME_CLOCK_LIVE(o); p = self->priv; - g_clear_pointer (&p->internal_timezone, g_time_zone_unref); - if (p->settings != NULL) { g_signal_handlers_disconnect_by_data (p->settings, self); @@ -222,13 +214,13 @@ my_dispose (GObject * o) static void my_finalize (GObject * o) { -#if 0 IndicatorDatetimeClockLive * self; priv_t * p; self = INDICATOR_DATETIME_CLOCK_LIVE(o); p = self->priv; -#endif + + g_strfreev (p->timezones); G_OBJECT_CLASS (indicator_datetime_clock_live_parent_class)->dispose (o); } diff --git a/src/clock.c b/src/clock.c index a5cefee..adfb0eb 100644 --- a/src/clock.c +++ b/src/clock.c @@ -55,10 +55,10 @@ indicator_datetime_clock_default_init (IndicatorDatetimeClockInterface * klass) * (transfer full): * array of timezone strings */ -gchar ** +const gchar ** indicator_datetime_clock_get_timezones (IndicatorDatetimeClock * self) { - gchar ** timezones; + const gchar ** timezones; IndicatorDatetimeClockInterface * iface; g_return_val_if_fail (INDICATOR_IS_DATETIME_CLOCK(self), NULL); diff --git a/src/clock.h b/src/clock.h index 0c10dab..40cdf1c 100644 --- a/src/clock.h +++ b/src/clock.h @@ -54,7 +54,7 @@ struct _IndicatorDatetimeClockInterface void (*changed) (IndicatorDatetimeClock * self); /* virtual functions */ - gchar** (*get_timezones) (IndicatorDatetimeClock * self); + const gchar** (*get_timezones) (IndicatorDatetimeClock * self); GDateTime* (*get_localtime) (IndicatorDatetimeClock * self); }; @@ -64,11 +64,11 @@ GType indicator_datetime_clock_get_type (void); **** ***/ -gchar ** indicator_datetime_clock_get_timezones (IndicatorDatetimeClock * clock); +const gchar ** indicator_datetime_clock_get_timezones (IndicatorDatetimeClock * clock); -GDateTime * indicator_datetime_clock_get_localtime (IndicatorDatetimeClock * clock); +GDateTime * indicator_datetime_clock_get_localtime (IndicatorDatetimeClock * clock); -void indicator_datetime_clock_emit_changed (IndicatorDatetimeClock * clock); +void indicator_datetime_clock_emit_changed (IndicatorDatetimeClock * clock); G_END_DECLS diff --git a/src/service.c b/src/service.c index f2b0b34..079456c 100644 --- a/src/service.c +++ b/src/service.c @@ -1168,7 +1168,7 @@ create_locations_section (IndicatorDatetimeService * self) GSList * l; GSList * locations = NULL; gchar ** user_locations; - gchar ** detected_timezones; + const gchar ** detected_timezones; priv_t * p = self->priv; GDateTime * now = indicator_datetime_service_get_localtime (self); @@ -1185,7 +1185,6 @@ create_locations_section (IndicatorDatetimeService * self) gchar * name = get_current_zone_name (tz, p->settings); locations = locations_add (locations, tz, name, TRUE); } - g_strfreev (detected_timezones); /* maybe add the user-specified locations */ user_locations = g_settings_get_strv (p->settings, SETTINGS_LOCATIONS_S); -- cgit v1.2.3 From 2c63a8cda5afbbf0747ec535b73bcc3280d6161b Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 17 Oct 2013 22:12:33 -0500 Subject: because it rarely changes, cache the header_label_format_string. --- src/service.c | 46 ++++++++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/service.c b/src/service.c index 079456c..b6e9fe8 100644 --- a/src/service.c +++ b/src/service.c @@ -106,6 +106,8 @@ struct _IndicatorDatetimeServicePrivate /* the clock app's icon filename */ gchar * clock_app_icon_filename; + gchar * header_label_format_string; + /* Whether or not we've tried to load the clock app's icon. This way we don't keep trying to reload it on the desktop */ gboolean clock_app_icon_initialized; @@ -179,6 +181,8 @@ static void rebuild_soon (IndicatorDatetimeService * self, int section); static inline void rebuild_header_soon (IndicatorDatetimeService * self) { + g_clear_pointer (&self->priv->header_label_format_string, g_free); + rebuild_soon (self, SECTION_HEADER); } @@ -361,7 +365,7 @@ on_header_timer (gpointer gself) return G_SOURCE_REMOVE; } -static char * get_header_label_format_string (IndicatorDatetimeService *); +static const char * get_header_label_format_string (IndicatorDatetimeService *); static void start_header_timer (IndicatorDatetimeService * self) @@ -375,11 +379,10 @@ start_header_timer (IndicatorDatetimeService * self) if (g_settings_get_boolean (self->priv->settings, SETTINGS_SHOW_CLOCK_S)) { - char * fmt = get_header_label_format_string (self); + const char * fmt = get_header_label_format_string (self); header_shows_seconds = fmt && (strstr(fmt,"%s") || strstr(fmt,"%S") || strstr(fmt,"%T") || strstr(fmt,"%X") || strstr(fmt,"%c")); - g_free (fmt); } if (header_shows_seconds) @@ -639,32 +642,39 @@ skew_timer_func (gpointer gself) **** ***/ -static gchar * +static const gchar * get_header_label_format_string (IndicatorDatetimeService * self) { - char * fmt; - GSettings * s = self->priv->settings; - const TimeFormatMode mode = g_settings_get_enum (s, SETTINGS_TIME_FORMAT_S); + priv_t * p = self->priv; - if (mode == TIME_FORMAT_MODE_CUSTOM) + if (p->header_label_format_string == NULL) { - fmt = g_settings_get_string (s, SETTINGS_CUSTOM_TIME_FORMAT_S); - } - else - { - gboolean show_day = g_settings_get_boolean (s, SETTINGS_SHOW_DAY_S); - gboolean show_date = g_settings_get_boolean (s, SETTINGS_SHOW_DATE_S); - fmt = generate_full_format_string (show_day, show_date, s); + char * fmt; + GSettings * s = p->settings; + const TimeFormatMode mode = g_settings_get_enum (s, SETTINGS_TIME_FORMAT_S); + + if (mode == TIME_FORMAT_MODE_CUSTOM) + { + fmt = g_settings_get_string (s, SETTINGS_CUSTOM_TIME_FORMAT_S); + } + else + { + gboolean show_day = g_settings_get_boolean (s, SETTINGS_SHOW_DAY_S); + gboolean show_date = g_settings_get_boolean (s, SETTINGS_SHOW_DATE_S); + fmt = generate_full_format_string (show_day, show_date, s); + } + + p->header_label_format_string = fmt; } - return fmt; + return p->header_label_format_string; } static GVariant * create_desktop_header_state (IndicatorDatetimeService * self) { GVariantBuilder b; - gchar * fmt; + const gchar * fmt; gchar * str; gboolean visible; GDateTime * now; @@ -690,7 +700,6 @@ create_desktop_header_state (IndicatorDatetimeService * self) /* cleanup */ g_date_time_unref (now); - g_free (fmt); return g_variant_builder_end (&b); } @@ -2103,6 +2112,7 @@ my_finalize (GObject * o) priv_t * p = self->priv; g_free (p->clock_app_icon_filename); + g_free (p->header_label_format_string); g_clear_pointer (&p->skew_time, g_date_time_unref); g_clear_pointer (&p->calendar_date, g_date_time_unref); -- cgit v1.2.3 From 0df37b0d0a4ae9ae14d896965fa37a9e61d0f7ab Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 17 Oct 2013 22:14:37 -0500 Subject: because is rarely changes, cache the GTimeZone used by indicator_datetime_clock_get_localtime() --- src/clock-live.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/clock-live.c b/src/clock-live.c index a5e42ec..de5ce19 100644 --- a/src/clock-live.c +++ b/src/clock-live.c @@ -38,6 +38,7 @@ struct _IndicatorDatetimeClockLivePriv IndicatorDatetimeTimezone * tz_file; IndicatorDatetimeTimezone * tz_geoclue; gchar ** timezones; + GTimeZone * localtime_zone; }; typedef IndicatorDatetimeClockLivePriv priv_t; @@ -63,7 +64,10 @@ G_DEFINE_TYPE_WITH_CODE ( static void on_current_timezone_changed (IndicatorDatetimeClockLive * self) { - g_clear_pointer (&self->priv->timezones, g_strfreev); + priv_t * p = self->priv; + + g_clear_pointer (&p->timezones, g_strfreev); + g_clear_pointer (&p->localtime_zone, g_time_zone_unref); indicator_datetime_clock_emit_changed (INDICATOR_DATETIME_CLOCK (self)); } @@ -130,7 +134,7 @@ on_detect_location_changed (IndicatorDatetimeClockLive * self) ***/ static void -rebuild_timezone_strv (IndicatorDatetimeClockLive * self) +rebuild_timezones (IndicatorDatetimeClockLive * self) { priv_t * p; GHashTable * hash; @@ -163,6 +167,9 @@ rebuild_timezone_strv (IndicatorDatetimeClockLive * self) while (g_hash_table_iter_next (&iter, &key, NULL)) p->timezones[i++] = g_strdup (key); g_hash_table_unref (hash); + + g_clear_pointer (&p->localtime_zone, g_time_zone_unref); + p->localtime_zone = g_time_zone_new (p->timezones ? p->timezones[0] : NULL); } static const gchar ** @@ -171,8 +178,8 @@ my_get_timezones (IndicatorDatetimeClock * clock) IndicatorDatetimeClockLive * self = INDICATOR_DATETIME_CLOCK_LIVE (clock); priv_t * p = self->priv; - if (p->timezones == NULL) - rebuild_timezone_strv (self); + if (G_UNLIKELY (p->timezones == NULL)) + rebuild_timezones (self); return (const gchar **) p->timezones; } @@ -180,11 +187,13 @@ my_get_timezones (IndicatorDatetimeClock * clock) static GDateTime * my_get_localtime (IndicatorDatetimeClock * clock) { - const gchar ** zones = my_get_timezones (clock); - GTimeZone * zone = g_time_zone_new (zones ? zones[0] : NULL); - GDateTime * time = g_date_time_new_now (zone); - g_time_zone_unref (zone); - return time; + IndicatorDatetimeClockLive * self = INDICATOR_DATETIME_CLOCK_LIVE (clock); + priv_t * p = self->priv; + + if (G_UNLIKELY (p->localtime_zone == NULL)) + rebuild_timezones (self); + + return g_date_time_new_now (p->localtime_zone); } /*** @@ -220,6 +229,7 @@ my_finalize (GObject * o) self = INDICATOR_DATETIME_CLOCK_LIVE(o); p = self->priv; + g_clear_pointer (&p->localtime_zone, g_time_zone_unref); g_strfreev (p->timezones); G_OBJECT_CLASS (indicator_datetime_clock_live_parent_class)->dispose (o); -- cgit v1.2.3 From 7fe0540b750cf88062427e8a1536f67bd2c5d8d3 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 17 Oct 2013 23:08:48 -0500 Subject: when building the desktop header state, reuse the label variant --- src/service.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/service.c b/src/service.c index b6e9fe8..4eb1542 100644 --- a/src/service.c +++ b/src/service.c @@ -679,6 +679,7 @@ create_desktop_header_state (IndicatorDatetimeService * self) gboolean visible; GDateTime * now; const gchar * title = _("Date and Time"); + GVariant * label_variant; visible = g_settings_get_boolean (self->priv->settings, SETTINGS_SHOW_CLOCK_S); @@ -692,9 +693,10 @@ create_desktop_header_state (IndicatorDatetimeService * self) g_warning ("%s", str); } + label_variant = g_variant_new_take_string (str); g_variant_builder_init (&b, G_VARIANT_TYPE_VARDICT); - g_variant_builder_add (&b, "{sv}", "accessible-desc", g_variant_new_string (str)); - g_variant_builder_add (&b, "{sv}", "label", g_variant_new_take_string (str)); + g_variant_builder_add (&b, "{sv}", "accessible-desc", label_variant); + g_variant_builder_add (&b, "{sv}", "label", label_variant); g_variant_builder_add (&b, "{sv}", "title", g_variant_new_string (title)); g_variant_builder_add (&b, "{sv}", "visible", g_variant_new_boolean (visible)); @@ -1193,6 +1195,7 @@ create_locations_section (IndicatorDatetimeService * self) const char * tz = detected_timezones[i]; gchar * name = get_current_zone_name (tz, p->settings); locations = locations_add (locations, tz, name, TRUE); + g_free (name); } /* maybe add the user-specified locations */ -- cgit v1.2.3 From be37c5fe747ed79a35d3903c9f549fc9c92fef93 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 18 Oct 2013 09:03:25 -0500 Subject: since we keep reusing the same GVariants again and again forever, cache them. --- src/service.c | 83 ++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 54 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/src/service.c b/src/service.c index 4eb1542..cb2ab4f 100644 --- a/src/service.c +++ b/src/service.c @@ -147,6 +147,13 @@ struct _IndicatorDatetimeServicePrivate /* appointments over the next few weeks. Used when building SECTION_APPOINTMENTS */ GSList * upcoming_appointments; + + /* variant cache */ + GVariant * desktop_title_variant; + GVariant * phone_title_variant; + GVariant * visible_true_variant; + GVariant * visible_false_variant; + GVariant * alarm_icon_variant; }; typedef IndicatorDatetimeServicePrivate priv_t; @@ -673,15 +680,15 @@ get_header_label_format_string (IndicatorDatetimeService * self) static GVariant * create_desktop_header_state (IndicatorDatetimeService * self) { + priv_t * p = self->priv; GVariantBuilder b; const gchar * fmt; gchar * str; gboolean visible; GDateTime * now; - const gchar * title = _("Date and Time"); GVariant * label_variant; - visible = g_settings_get_boolean (self->priv->settings, SETTINGS_SHOW_CLOCK_S); + visible = g_settings_get_boolean (p->settings, SETTINGS_SHOW_CLOCK_S); /* build the time string for the label & a11y */ fmt = get_header_label_format_string (self); @@ -697,8 +704,8 @@ create_desktop_header_state (IndicatorDatetimeService * self) g_variant_builder_init (&b, G_VARIANT_TYPE_VARDICT); g_variant_builder_add (&b, "{sv}", "accessible-desc", label_variant); g_variant_builder_add (&b, "{sv}", "label", label_variant); - g_variant_builder_add (&b, "{sv}", "title", g_variant_new_string (title)); - g_variant_builder_add (&b, "{sv}", "visible", g_variant_new_boolean (visible)); + g_variant_builder_add_value (&b, p->desktop_title_variant); + g_variant_builder_add_value (&b, visible ? p->visible_true_variant : p->visible_false_variant); /* cleanup */ g_date_time_unref (now); @@ -711,43 +718,37 @@ service_has_alarms (IndicatorDatetimeService * self); static GVariant * create_phone_header_state (IndicatorDatetimeService * self) { + priv_t * p = self->priv; + const gboolean has_alarms = service_has_alarms (self); GVariantBuilder b; GDateTime * now; const gchar * fmt; - gchar * label; - gboolean has_alarms; - gchar * a11y; - const gchar * title = _("Upcoming"); g_variant_builder_init (&b, G_VARIANT_TYPE_VARDICT); + g_variant_builder_add_value (&b, p->phone_title_variant); + g_variant_builder_add_value (&b, p->visible_true_variant); + + /* icon */ + if (has_alarms) + g_variant_builder_add_value (&b, p->alarm_icon_variant); - /* label */ + /* label, a11y */ now = indicator_datetime_service_get_localtime (self); fmt = get_terse_header_time_format_string (); - label = g_date_time_format (now, fmt); - - /* icon */ - if ((has_alarms = service_has_alarms (self))) + if (has_alarms) { - GIcon * icon; - icon = g_themed_icon_new_with_default_fallbacks (ALARM_CLOCK_ICON_NAME); - g_variant_builder_add (&b, "{sv}", "icon", g_icon_serialize (icon)); - g_object_unref (icon); + gchar * label = g_date_time_format (now, fmt); + gchar * a11y = g_strdup_printf (_("%s (has alarms)"), label); + g_variant_builder_add (&b, "{sv}", "label", g_variant_new_take_string (label)); + g_variant_builder_add (&b, "{sv}", "accessible-desc", g_variant_new_take_string (a11y)); } - - /* a11y */ - if (has_alarms) - a11y = g_strdup_printf (_("%s (has alarms)"), label); else - a11y = g_strdup (label); - g_variant_builder_add (&b, "{sv}", "accessible-desc", - g_variant_new_take_string (a11y)); - - g_variant_builder_add (&b, "{sv}", "visible", g_variant_new_boolean (TRUE)); - g_variant_builder_add (&b, "{sv}", "label", g_variant_new_take_string (label)); - g_variant_builder_add (&b, "{sv}", "title", g_variant_new_string (title)); + { + GVariant * v = g_variant_new_take_string (g_date_time_format (now, fmt)); + g_variant_builder_add (&b, "{sv}", "label", v); + g_variant_builder_add (&b, "{sv}", "accessible-desc", v); + } - /* cleanup */ g_date_time_unref (now); return g_variant_builder_end (&b); } @@ -2105,6 +2106,12 @@ my_dispose (GObject * o) g_clear_object (&p->phone_header_action); g_clear_object (&p->conn); + g_clear_pointer (&p->desktop_title_variant, g_variant_unref); + g_clear_pointer (&p->phone_title_variant, g_variant_unref); + g_clear_pointer (&p->visible_true_variant, g_variant_unref); + g_clear_pointer (&p->visible_false_variant, g_variant_unref); + g_clear_pointer (&p->alarm_icon_variant, g_variant_unref); + G_OBJECT_CLASS (indicator_datetime_service_parent_class)->dispose (o); } @@ -2129,6 +2136,7 @@ my_finalize (GObject * o) static void indicator_datetime_service_init (IndicatorDatetimeService * self) { + GIcon * icon; priv_t * p; /* init the priv pointer */ @@ -2141,6 +2149,23 @@ indicator_datetime_service_init (IndicatorDatetimeService * self) p->cancellable = g_cancellable_new (); p->settings = g_settings_new (SETTINGS_INTERFACE); + + p->desktop_title_variant = g_variant_new ("{sv}", "title", g_variant_new_string (_("Date and Time"))); + g_variant_ref_sink (p->desktop_title_variant); + + p->phone_title_variant = g_variant_new ("{sv}", "title", g_variant_new_string (_("Upcoming"))); + g_variant_ref_sink (p->phone_title_variant); + + p->visible_true_variant = g_variant_new ("{sv}", "visible", g_variant_new_boolean (TRUE)); + g_variant_ref_sink (p->visible_true_variant); + + p->visible_false_variant = g_variant_new ("{sv}", "visible", g_variant_new_boolean (FALSE)); + g_variant_ref_sink (p->visible_false_variant); + + icon = g_themed_icon_new_with_default_fallbacks (ALARM_CLOCK_ICON_NAME); + p->alarm_icon_variant = g_variant_new ("{sv}", "icon", g_icon_serialize (icon)); + g_variant_ref_sink (p->alarm_icon_variant); + g_object_unref (icon); } static void -- cgit v1.2.3 From 99f3bc0685f85744081eefa0c22d9f7c5b13f327 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 18 Oct 2013 09:32:53 -0500 Subject: copyediting: nobody uses this #define, so remove it --- src/timezone.h | 2 -- 1 file changed, 2 deletions(-) (limited to 'src') diff --git a/src/timezone.h b/src/timezone.h index aaaa296..fa6593d 100644 --- a/src/timezone.h +++ b/src/timezone.h @@ -37,8 +37,6 @@ typedef struct _IndicatorDatetimeTimezoneClass IndicatorDatetimeTimezoneClass; GType indicator_datetime_timezone_get_type (void); -#define INDICATOR_DATETIME_TIMEZONE_PROPERTY_TIMEZONE "timezone" - /** * Abstract Base Class for objects that provide a timezone. * -- cgit v1.2.3 From a2f21a1da1b1f467751020b56b04808f5dd00695 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 23 Oct 2013 09:42:49 -0500 Subject: make GObject a prerequisite of the IndicatorDatetimeClock interface. --- src/clock.c | 2 +- src/service.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/clock.c b/src/clock.c index adfb0eb..2c2fec2 100644 --- a/src/clock.c +++ b/src/clock.c @@ -29,7 +29,7 @@ static guint signals[SIGNAL_LAST] = { 0 }; G_DEFINE_INTERFACE (IndicatorDatetimeClock, indicator_datetime_clock, - 0); + G_TYPE_OBJECT); static void indicator_datetime_clock_default_init (IndicatorDatetimeClockInterface * klass) diff --git a/src/service.c b/src/service.c index cb2ab4f..5fffc11 100644 --- a/src/service.c +++ b/src/service.c @@ -2317,7 +2317,7 @@ indicator_datetime_service_class_init (IndicatorDatetimeServiceClass * klass) properties[PROP_CLOCK] = g_param_spec_object ("clock", "Clock", "The clock", - G_TYPE_OBJECT, + INDICATOR_TYPE_DATETIME_CLOCK, flags); properties[PROP_PLANNER] = g_param_spec_object ("planner", -- cgit v1.2.3 From 6da162acd4a293a1cfe1933c54f6b06935deebbf Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 23 Oct 2013 13:14:25 -0500 Subject: instead of keeping IndicatorDatetimeTimezone objects in separate fields, keep them in a list so they can be handled in a loop. --- src/clock-live.c | 94 ++++++++++++++++++++++++++------------------------------ 1 file changed, 43 insertions(+), 51 deletions(-) (limited to 'src') 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); } -- cgit v1.2.3