aboutsummaryrefslogtreecommitdiff
path: root/src/service.c
diff options
context:
space:
mode:
authorCharles Kerr <charles.kerr@canonical.com>2013-10-24 04:15:20 +0000
committerTarmac <Unknown>2013-10-24 04:15:20 +0000
commit9e2f24172c1fa388e2be2938998b20f5bf3a8241 (patch)
tree3cf94298d909590579d5171a7bc95b5a0608efd3 /src/service.c
parente38b6293bb37557d27efd052c82ee44d70996077 (diff)
parent6da162acd4a293a1cfe1933c54f6b06935deebbf (diff)
downloadayatana-indicator-datetime-9e2f24172c1fa388e2be2938998b20f5bf3a8241.tar.gz
ayatana-indicator-datetime-9e2f24172c1fa388e2be2938998b20f5bf3a8241.tar.bz2
ayatana-indicator-datetime-9e2f24172c1fa388e2be2938998b20f5bf3a8241.zip
Make timezone detection and localtime mockable. Fixes: https://bugs.launchpad.net/bugs/1237509.
Approved by PS Jenkins bot, Ted Gould.
Diffstat (limited to 'src/service.c')
-rw-r--r--src/service.c365
1 files changed, 183 insertions, 182 deletions
diff --git a/src/service.c b/src/service.c
index 08945bc..5fffc11 100644
--- a/src/service.c
+++ b/src/service.c
@@ -29,8 +29,6 @@
#include <url-dispatcher.h>
#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,16 +100,14 @@ 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;
+ 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;
@@ -150,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;
@@ -168,6 +172,12 @@ indicator_clear_timer (guint * tag)
}
}
+static inline GDateTime *
+indicator_datetime_service_get_localtime (IndicatorDatetimeService * self)
+{
+ return indicator_datetime_clock_get_localtime (self->priv->clock);
+}
+
/***
****
***/
@@ -178,6 +188,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);
}
@@ -360,7 +372,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)
@@ -374,11 +386,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)
@@ -589,23 +600,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 +618,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);
}
@@ -656,38 +649,46 @@ 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)
{
+ priv_t * p = self->priv;
GVariantBuilder b;
- gchar * fmt;
+ 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);
@@ -699,15 +700,15 @@ 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}", "title", g_variant_new_string (title));
- g_variant_builder_add (&b, "{sv}", "visible", g_variant_new_boolean (visible));
+ g_variant_builder_add (&b, "{sv}", "accessible-desc", label_variant);
+ g_variant_builder_add (&b, "{sv}", "label", label_variant);
+ 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);
- g_free (fmt);
return g_variant_builder_end (&b);
}
@@ -717,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);
}
@@ -1100,61 +1095,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 +1180,30 @@ create_locations_section (IndicatorDatetimeService * self)
GSList * l;
GSList * locations = NULL;
gchar ** user_locations;
- gboolean visible;
- IndicatorDatetimeTimezone * detected_timezones[2];
+ const 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; i<G_N_ELEMENTS(detected_timezones); i++)
+ detected_timezones = indicator_datetime_clock_get_timezones (p->clock);
+ 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_free (name);
}
/* 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 +1661,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 +2017,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 +2040,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 +2075,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,12 +2101,17 @@ my_dispose (GObject * o)
for (i=0; i<N_PROFILES; ++i)
g_clear_object (&p->menus[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);
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);
}
@@ -2181,6 +2122,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);
@@ -2194,9 +2136,45 @@ my_finalize (GObject * o)
static void
indicator_datetime_service_init (IndicatorDatetimeService * self)
{
- guint i, n;
+ GIcon * icon;
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);
+
+ 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
+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 +2211,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); i<n; i++)
{
g_string_printf (gstr, "changed::%s", header_settings[i]);
@@ -2327,6 +2295,7 @@ indicator_datetime_service_class_init (IndicatorDatetimeServiceClass * klass)
object_class->dispose = 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 +2314,12 @@ indicator_datetime_service_class_init (IndicatorDatetimeServiceClass * klass)
properties[PROP_0] = NULL;
+ properties[PROP_CLOCK] = g_param_spec_object ("clock",
+ "Clock",
+ "The clock",
+ INDICATOR_TYPE_DATETIME_CLOCK,
+ flags);
+
properties[PROP_PLANNER] = g_param_spec_object ("planner",
"Planner",
"The appointment provider",
@@ -2359,28 +2334,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 +2364,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 +2408,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;