diff options
author | Charles Kerr <charles.kerr@canonical.com> | 2013-06-17 09:06:02 -0500 |
---|---|---|
committer | Charles Kerr <charles.kerr@canonical.com> | 2013-06-17 09:06:02 -0500 |
commit | 72f23220db542cb8e41e9492528e79082c916804 (patch) | |
tree | 0ec9b3df80e55a3686d06bd9595c77a4a008f41f | |
parent | 49ee32fefe869a778f39f88a936f546ef2c68737 (diff) | |
download | ayatana-indicator-datetime-72f23220db542cb8e41e9492528e79082c916804.tar.gz ayatana-indicator-datetime-72f23220db542cb8e41e9492528e79082c916804.tar.bz2 ayatana-indicator-datetime-72f23220db542cb8e41e9492528e79082c916804.zip |
in service.c, replace all calls to time(NULL) and g_date_time_new_now_local() with a call to the new 'indicator_datetime_service_get_localtime()' func. This is currently a passthrough to _new_now_local(), but we'll need this control point to override the system time in unit testing
-rw-r--r-- | src/datetime-prefs-locations.c | 9 | ||||
-rw-r--r-- | src/service.c | 111 | ||||
-rw-r--r-- | src/service.h | 2 | ||||
-rw-r--r-- | src/utils.c | 6 | ||||
-rw-r--r-- | src/utils.h | 2 |
5 files changed, 63 insertions, 67 deletions
diff --git a/src/datetime-prefs-locations.c b/src/datetime-prefs-locations.c index 33fd660..4bbf053 100644 --- a/src/datetime-prefs-locations.c +++ b/src/datetime-prefs-locations.c @@ -25,6 +25,7 @@ with this program. If not, see <http://www.gnu.org/licenses/>. #endif #include <stdlib.h> +#include <time.h> /* time_t */ #include <glib/gi18n-lib.h> #include <gtk/gtk.h> #include <timezonemap/timezone-completion.h> @@ -416,10 +417,9 @@ update_times (GtkWidget * dlg) g_signal_handlers_block_by_func (store, save_when_idle, dlg); - GDateTime * now = g_date_time_new_now_local (); - GtkTreeIter iter; if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (store), &iter)) { + GDateTime * now = g_date_time_new_now_local (); do { gchar * strzone; @@ -428,7 +428,7 @@ update_times (GtkWidget * dlg) if (strzone && *strzone) { GTimeZone * tz = g_time_zone_new (strzone); GDateTime * now_tz = g_date_time_to_timezone (now, tz); - gchar * format = generate_format_string_at_time (now_tz); + gchar * format = generate_format_string_at_time (now, now_tz); gchar * time_str = g_date_time_format (now_tz, format); gchar * old_time_str; @@ -444,10 +444,9 @@ update_times (GtkWidget * dlg) } g_free (strzone); } while (gtk_tree_model_iter_next (GTK_TREE_MODEL (store), &iter)); + g_date_time_unref (now); } - g_date_time_unref (now); - g_signal_handlers_unblock_by_func (store, save_when_idle, dlg); return TRUE; diff --git a/src/service.c b/src/service.c index bb71a33..55905aa 100644 --- a/src/service.c +++ b/src/service.c @@ -211,18 +211,15 @@ rebuild_settings_section_soon (IndicatorDatetimeService * self) */ static guint -calculate_seconds_until_next_fifteen_minutes (void) +calculate_seconds_until_next_fifteen_minutes (GDateTime * now) { char * str; gint minute; guint seconds; GTimeSpan diff; - GDateTime * now; GDateTime * next; GDateTime * start_of_next; - now = g_date_time_new_now_local (); - minute = g_date_time_get_minute (now); minute = 15 - (minute % 15); next = g_date_time_add_minutes (now, minute); @@ -242,7 +239,6 @@ calculate_seconds_until_next_fifteen_minutes (void) g_date_time_unref (start_of_next); g_date_time_unref (next); - g_date_time_unref (now); return seconds; } @@ -267,13 +263,16 @@ on_timezone_timer (gpointer gself) static void start_timezone_timer (IndicatorDatetimeService * self) { + GDateTime * now; + guint seconds; priv_t * p = self->priv; indicator_clear_timer (&p->timezone_timer); - p->timezone_timer = g_timeout_add_seconds (calculate_seconds_until_next_fifteen_minutes(), - on_timezone_timer, - self); + now = indicator_datetime_service_get_localtime (self); + seconds = calculate_seconds_until_next_fifteen_minutes (now); + p->timezone_timer = g_timeout_add_seconds (seconds, on_timezone_timer, self); + g_date_time_unref (now); } /*** @@ -288,15 +287,13 @@ start_timezone_timer (IndicatorDatetimeService * self) */ static guint -calculate_milliseconds_until_next_minute (void) +calculate_milliseconds_until_next_minute (GDateTime * now) { - GDateTime * now; GDateTime * next; GDateTime * start_of_next; GTimeSpan interval_usec; guint interval_msec; - now = g_date_time_new_now_local (); next = g_date_time_add_minutes (now, 1); start_of_next = g_date_time_new_local (g_date_time_get_year (next), g_date_time_get_month (next), @@ -310,25 +307,19 @@ calculate_milliseconds_until_next_minute (void) g_date_time_unref (start_of_next); g_date_time_unref (next); - g_date_time_unref (now); return interval_msec; } static gint -calculate_milliseconds_until_next_second (void) +calculate_milliseconds_until_next_second (GDateTime * now) { - GDateTime * now; gint interval_usec; guint interval_msec; - now = g_date_time_new_now_local (); - interval_usec = G_USEC_PER_SEC - g_date_time_get_microsecond (now); interval_msec = (interval_usec + 999) / 1000; - g_date_time_unref (now); - return interval_msec; } @@ -355,6 +346,7 @@ start_header_timer (IndicatorDatetimeService * self) guint interval_msec; gboolean header_shows_seconds = FALSE; priv_t * p = self->priv; + GDateTime * now = indicator_datetime_service_get_localtime (self); indicator_clear_timer (&p->header_timer); @@ -368,9 +360,9 @@ start_header_timer (IndicatorDatetimeService * self) } if (header_shows_seconds) - interval_msec = calculate_milliseconds_until_next_second (); + interval_msec = calculate_milliseconds_until_next_second (now); else - interval_msec = calculate_milliseconds_until_next_minute (); + interval_msec = calculate_milliseconds_until_next_minute (now); interval_msec += 50; /* add a small margin to ensure the callback fires /after/ next is reached */ @@ -380,6 +372,8 @@ start_header_timer (IndicatorDatetimeService * self) on_header_timer, self, NULL); + + g_date_time_unref (now); } /** @@ -407,8 +401,8 @@ on_local_time_jumped (IndicatorDatetimeService * self) static gboolean skew_timer_func (gpointer gself) { - GDateTime * now = g_date_time_new_now_local (); IndicatorDatetimeService * self = INDICATOR_DATETIME_SERVICE (gself); + GDateTime * now = indicator_datetime_service_get_localtime (self); priv_t * p = self->priv; /* check for clock skew: has too much time passed since the last check? */ @@ -490,15 +484,15 @@ create_header_state (IndicatorDatetimeService * self) gchar * fmt; gchar * str; gboolean visible; - GDateTime * now_local; + GDateTime * now; priv_t * p = self->priv; 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); - now_local = g_date_time_new_now_local (); - str = g_date_time_format (now_local, fmt); + now = indicator_datetime_service_get_localtime (self); + str = g_date_time_format (now, fmt); if (str == NULL) { str = g_strdup (_("Unsupported date format")); @@ -511,7 +505,7 @@ create_header_state (IndicatorDatetimeService * self) g_variant_builder_add (&b, "{sv}", "visible", g_variant_new_boolean (visible)); /* cleanup */ - g_date_time_unref (now_local); + g_date_time_unref (now); g_free (str); g_free (fmt); return g_variant_builder_end (&b); @@ -530,10 +524,10 @@ get_calendar_date (IndicatorDatetimeService * self) GDateTime * date; priv_t * p = self->priv; - if (p->calendar_date == 0) - date = g_date_time_new_now_local (); - else + if (p->calendar_date) date = g_date_time_new_from_unix_local ((gint64)p->calendar_date); + else + date = indicator_datetime_service_get_localtime (self); return date; } @@ -626,26 +620,19 @@ create_calendar_section (IndicatorDatetimeService * self) { char * label; GMenuItem * menu_item; - GDateTime * date_time; + GDateTime * now; GMenu * menu = g_menu_new (); /* create the local date menuitem */ - date_time = g_date_time_new_now_local (); - if (date_time == NULL) - { - label = g_strdup (_("Error getting time")); - } - else - { - label = g_date_time_format (date_time, _("%A, %e %B %Y")); - g_date_time_unref (date_time); - } + now = indicator_datetime_service_get_localtime (self); + label = g_date_time_format (now, _("%A, %e %B %Y")); menu_item = g_menu_item_new (label, NULL); g_menu_item_set_action_and_target_value (menu_item, "indicator.activate-planner", g_variant_new_int64(0)); g_menu_append_item (menu, menu_item); g_object_unref (menu_item); g_free (label); + g_date_time_unref (now); /* create the calendar menuitem */ if (g_settings_get_boolean (self->priv->settings, SETTINGS_SHOW_CALENDAR_S)) @@ -706,7 +693,7 @@ get_upcoming_appointments (IndicatorDatetimeService * self) } static char * -get_appointment_time_format (struct IndicatorDatetimeAppt * appt) +get_appointment_time_format (struct IndicatorDatetimeAppt * appt, GDateTime * now) { char * fmt; gboolean full_day = g_date_time_difference (appt->end, appt->begin) == G_TIME_SPAN_DAY; @@ -721,7 +708,7 @@ get_appointment_time_format (struct IndicatorDatetimeAppt * appt) } else { - fmt = generate_format_string_at_time (appt->begin); + fmt = generate_format_string_at_time (now, appt->begin); } return fmt; @@ -738,13 +725,14 @@ create_appointments_section (IndicatorDatetimeService * self) GSList * l; GSList * appts; GMenuItem * menu_item; + GDateTime * now = indicator_datetime_service_get_localtime (self); /* build appointment menuitems */ appts = get_upcoming_appointments (self); for (l=appts; l!=NULL; l=l->next) { struct IndicatorDatetimeAppt * appt = l->data; - char * fmt = get_appointment_time_format (appt); + char * fmt = get_appointment_time_format (appt, now); const gint64 unix_time = g_date_time_to_unix (appt->begin); menu_item = g_menu_item_new (appt->summary, NULL); @@ -773,6 +761,7 @@ create_appointments_section (IndicatorDatetimeService * self) g_object_unref (menu_item); /* cleanup */ + g_date_time_unref (now); g_slist_free_full (appts, (GDestroyNotify)indicator_datetime_appt_free); } @@ -846,7 +835,7 @@ set_detect_location_enabled (IndicatorDatetimeService * self, gboolean enabled) for pruning duplicates and sorting. */ struct TimeLocation { - gint32 offset; + GTimeSpan offset; gchar * zone; gchar * name; gboolean visible; @@ -865,17 +854,15 @@ time_location_free (struct TimeLocation * loc) static struct TimeLocation* time_location_new (const char * zone, const char * name, - gboolean visible, - time_t now) + gboolean visible) { struct TimeLocation * loc = g_new (struct TimeLocation, 1); GTimeZone * tz = g_time_zone_new (zone); - gint interval = g_time_zone_find_interval (tz, G_TIME_TYPE_UNIVERSAL, now); - loc->offset = g_time_zone_get_offset (tz, interval); loc->zone = g_strdup (zone); loc->name = g_strdup (name); loc->visible = visible; loc->local_time = g_date_time_new_now (tz); + loc->offset = g_date_time_get_utc_offset (loc->local_time); g_time_zone_unref (tz); return loc; } @@ -884,7 +871,10 @@ static int time_location_compare (const struct TimeLocation * a, const struct TimeLocation * b) { - int ret = a->offset - b->offset; /* primary key */ + int ret = 0; + + if (!ret && (a->offset != b->offset)) /* primary key */ + ret = (a->offset < b->offset) ? -1 : 1; if (!ret) ret = g_strcmp0 (a->name, b->name); /* secondary key */ @@ -899,10 +889,9 @@ static GSList* locations_add (GSList * locations, const char * zone, const char * name, - gboolean visible, - time_t now) + gboolean visible) { - struct TimeLocation * loc = time_location_new (zone, name, visible, now); + struct TimeLocation * loc = time_location_new (zone, name, visible); if (g_slist_find_custom (locations, loc, (GCompareFunc)time_location_compare)) { @@ -927,9 +916,9 @@ create_locations_section (IndicatorDatetimeService * self) GSList * locations = NULL; gchar ** user_locations; gboolean visible; - const time_t now = time (NULL); - priv_t * p = self->priv; IndicatorDatetimeTimezone * detected_timezones[2]; + 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)); @@ -953,7 +942,7 @@ create_locations_section (IndicatorDatetimeService * self) if (tz && *tz) { gchar * name = get_current_zone_name (tz); - locations = locations_add (locations, tz, name, visible, now); + locations = locations_add (locations, tz, name, visible); g_free (name); } } @@ -970,7 +959,7 @@ create_locations_section (IndicatorDatetimeService * self) gchar * zone; gchar * name; split_settings_location (user_locations[i], &zone, &name); - locations = locations_add (locations, zone, name, visible, now); + locations = locations_add (locations, zone, name, visible); g_free (name); g_free (zone); } @@ -994,7 +983,7 @@ create_locations_section (IndicatorDatetimeService * self) detailed_action = g_strdup_printf ("indicator.set-location::%s %s", loc->zone, loc->name); - fmt = generate_format_string_at_time (loc->local_time); + fmt = generate_format_string_at_time (now, loc->local_time); menu_item = g_menu_item_new (label, detailed_action); g_menu_item_set_attribute (menu_item, "x-canonical-type", @@ -1012,6 +1001,7 @@ create_locations_section (IndicatorDatetimeService * self) } } + g_date_time_unref (now); g_slist_free_full (locations, (GDestroyNotify)time_location_free); return G_MENU_MODEL (menu); } @@ -1857,3 +1847,12 @@ indicator_datetime_service_new (gboolean replace) 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 G_GNUC_UNUSED) +{ + return g_date_time_new_now_local (); +} + diff --git a/src/service.h b/src/service.h index 47dd937..b8c9bcd 100644 --- a/src/service.h +++ b/src/service.h @@ -64,6 +64,8 @@ GType indicator_datetime_service_get_type (void); IndicatorDatetimeService * indicator_datetime_service_new (gboolean replace); +GDateTime * indicator_datetime_service_get_localtime (IndicatorDatetimeService * service); + G_END_DECLS #endif /* __INDICATOR_DATETIME_SERVICE_H__ */ diff --git a/src/utils.c b/src/utils.c index 07a2022..fa54008 100644 --- a/src/utils.c +++ b/src/utils.c @@ -226,7 +226,7 @@ generate_format_string_full (gboolean show_day, gboolean show_date) } gchar * -generate_format_string_at_time (GDateTime * time) +generate_format_string_at_time (GDateTime * now, GDateTime * time) { /* This is a bit less free-form than for the main "now" time label. */ /* If it is today, just the time should be shown (e.g. “3:55 PM”) @@ -236,8 +236,6 @@ generate_format_string_at_time (GDateTime * time) gboolean show_day = FALSE; gboolean show_date = FALSE; - GDateTime * now = g_date_time_new_now_local(); - /* First, are we same day? */ gint time_year, time_month, time_day; gint now_year, now_month, now_day; @@ -272,8 +270,6 @@ generate_format_string_at_time (GDateTime * time) g_date_time_unref(future_bound); } - g_date_time_unref (now); - return generate_format_string_full(show_day, show_date); } diff --git a/src/utils.h b/src/utils.h index c2bc0c5..8dc2964 100644 --- a/src/utils.h +++ b/src/utils.h @@ -31,7 +31,7 @@ gboolean is_locale_12h (void); void split_settings_location (const gchar * location, gchar ** zone, gchar ** name); gchar * get_current_zone_name (const gchar * location); gchar * generate_format_string_full (gboolean show_day, gboolean show_date); -gchar * generate_format_string_at_time (GDateTime * time); +gchar * generate_format_string_at_time (GDateTime * now, GDateTime * time); G_END_DECLS |