[gnome-calendar/mcatanzaro/#419] Stop using e_cal_util_get_system_timezone()



commit 5570b75b8c9f1715415c70295bcc965b8ba45a8a
Author: Michael Catanzaro <mcatanzaro gnome org>
Date:   Thu Sep 19 11:07:56 2019 -0500

    Stop using e_cal_util_get_system_timezone()
    
    Because its return value is nullable, which is inconvenient for us.
    Instead, add and use gcal_context_get_icaltimezone() to supplement the
    existing gcal_context_get_timezone().
    
    We'll also need a function to convert from GTimeZone to ICalTimezone,
    gcal_timezone_to_icaltimezone().
    
    Finally, we have to initialize the GcalSearchEngine later when
    constructing GcalContext, because with this change GcalSearchEngine now
    needs GcalContext ready to initialize itself.

 src/core/gcal-context.c          | 13 ++++++++++-
 src/core/gcal-event.c            | 19 +++++++++------
 src/core/gcal-manager.c          | 50 +++++++++++++++++++++++++---------------
 src/search/gcal-search-engine.c  |  7 +++---
 src/utils/gcal-date-time-utils.c | 20 ++++++++++++++++
 src/utils/gcal-date-time-utils.h |  2 ++
 src/utils/gcal-utils.c           | 36 ++++++++++++++++++++---------
 7 files changed, 105 insertions(+), 42 deletions(-)
---
diff --git a/src/core/gcal-context.c b/src/core/gcal-context.c
index cd8b441e..afccdca9 100644
--- a/src/core/gcal-context.c
+++ b/src/core/gcal-context.c
@@ -21,6 +21,7 @@
 #define G_LOG_DOMAIN "GcalContext"
 
 #include "gcal-context.h"
+#include "gcal-date-time-utils.h"
 #include "gcal-night-light-monitor.h"
 #include "gcal-time-zone-monitor.h"
 
@@ -102,6 +103,16 @@ on_timezone_changed_cb (GcalTimeZoneMonitor *timezone_monitor,
  * GObject overrides
  */
 
+static void
+gcal_context_constructed (GObject *object)
+{
+  GcalContext *self = (GcalContext *)object;
+
+  G_OBJECT_CLASS (gcal_context_parent_class)->constructed (object);
+
+  self->search_engine = gcal_search_engine_new (self);
+}
+
 static void
 gcal_context_finalize (GObject *object)
 {
@@ -193,6 +204,7 @@ gcal_context_class_init (GcalContextClass *klass)
 {
   GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
+  object_class->constructed = gcal_context_constructed;
   object_class->finalize = gcal_context_finalize;
   object_class->get_property = gcal_context_get_property;
   object_class->set_property = gcal_context_set_property;
@@ -257,7 +269,6 @@ gcal_context_init (GcalContext *self)
   self->manager = gcal_manager_new ();
   self->settings = g_settings_new ("org.gnome.calendar");
   self->weather_service = gcal_weather_service_new ();
-  self->search_engine = gcal_search_engine_new (self);
 
   self->timezone_monitor = gcal_time_zone_monitor_new ();
   g_signal_connect_object (self->timezone_monitor,
diff --git a/src/core/gcal-event.c b/src/core/gcal-event.c
index 5507720e..d371cb14 100644
--- a/src/core/gcal-event.c
+++ b/src/core/gcal-event.c
@@ -20,6 +20,7 @@
 
 #include "gconstructor.h"
 #include "gcal-application.h"
+#include "gcal-context.h"
 #include "gcal-debug.h"
 #include "gcal-event.h"
 #include "gcal-utils.h"
@@ -223,12 +224,17 @@ static ECalComponentDateTime*
 build_component_from_datetime (GcalEvent *self,
                                GDateTime *dt)
 {
+  GcalApplication *application;
+  GcalContext *context;
   ICalTime *itt;
   gchar *tzid = NULL;
 
   if (!dt)
     return NULL;
 
+  application = GCAL_APPLICATION (g_application_get_default ());
+  context = gcal_application_get_context (application);
+
   itt = gcal_date_time_to_icaltime (dt);
 
   if (self->all_day)
@@ -238,14 +244,13 @@ build_component_from_datetime (GcalEvent *self,
     }
   else
     {
-      ICalTimezone *zone;
+      GTimeZone *zone;
+      ICalTimezone *tz;
 
-      zone = e_cal_util_get_system_timezone ();
-      if (zone != NULL)
-        {
-          i_cal_time_set_timezone (itt, zone);
-          tzid = g_strdup (i_cal_timezone_get_tzid (zone));
-         }
+      zone = gcal_context_get_timezone (context);
+      tz = gcal_timezone_to_icaltimezone (zone);
+      i_cal_time_set_timezone (itt, tz);
+      tzid = g_strdup (i_cal_timezone_get_tzid (tz));
     }
 
   /* Call it after setting the timezone, because the DATE values do not let set the timezone */
diff --git a/src/core/gcal-manager.c b/src/core/gcal-manager.c
index 319ce188..c87d7e32 100644
--- a/src/core/gcal-manager.c
+++ b/src/core/gcal-manager.c
@@ -18,6 +18,8 @@
 
 #define G_LOG_DOMAIN "GcalManager"
 
+#include "gcal-application.h"
+#include "gcal-context.h"
 #include "gcal-debug.h"
 #include "gcal-manager.h"
 #include "gcal-utils.h"
@@ -877,13 +879,18 @@ void
 gcal_manager_setup_shell_search (GcalManager             *self,
                                  ECalDataModelSubscriber *subscriber)
 {
-  ICalTimezone *tz;
+  GcalApplication *application;
+  GcalContext *context;
+  GTimeZone *zone;
 
   g_return_if_fail (GCAL_IS_MANAGER (self));
 
   if (self->shell_search_data_model)
     return;
 
+  application = GCAL_APPLICATION (g_application_get_default ());
+  context = gcal_application_get_context (application);
+
   self->shell_search_data_model = e_cal_data_model_new (gcal_thread_submit_job);
   g_signal_connect_object (self->shell_search_data_model,
                            "view-state-changed",
@@ -892,9 +899,9 @@ gcal_manager_setup_shell_search (GcalManager             *self,
                            G_CONNECT_SWAPPED);
 
   e_cal_data_model_set_expand_recurrences (self->shell_search_data_model, TRUE);
-  tz = e_cal_util_get_system_timezone ();
-  if (tz != NULL)
-    e_cal_data_model_set_timezone (self->shell_search_data_model, tz);
+
+  zone = gcal_context_get_timezone (context);
+  e_cal_data_model_set_timezone (self->shell_search_data_model, gcal_timezone_to_icaltimezone (zone));
 
   self->search_view_data = g_new0 (ViewStateData, 1);
   self->search_view_data->subscriber = subscriber;
@@ -1424,7 +1431,10 @@ gcal_manager_get_events (GcalManager *self,
                          ICalTime    *start_date,
                          ICalTime    *end_date)
 {
+  GcalApplication *application;
+  GcalContext *context;
   time_t range_start, range_end;
+  GTimeZone *zone;
   ICalTimezone *tz;
   GatherEventData data = {
     .manager = self,
@@ -1433,20 +1443,15 @@ gcal_manager_get_events (GcalManager *self,
 
   GCAL_ENTRY;
 
-  g_return_val_if_fail (GCAL_IS_MANAGER (self), NULL);
+  application = GCAL_APPLICATION (g_application_get_default ());
+  context = gcal_application_get_context (application);
 
-  tz = e_cal_util_get_system_timezone ();
+  g_return_val_if_fail (GCAL_IS_MANAGER (self), NULL);
 
-  if (tz)
-    {
-      range_start = i_cal_time_as_timet_with_zone (start_date, tz);
-      range_end = i_cal_time_as_timet_with_zone (end_date, tz);
-    }
-  else
-    {
-      range_start = i_cal_time_as_timet (start_date);
-      range_end = i_cal_time_as_timet (end_date);
-    }
+  zone = gcal_context_get_timezone (context);
+  tz = gcal_timezone_to_icaltimezone (zone);
+  range_start = i_cal_time_as_timet_with_zone (start_date, tz);
+  range_end = i_cal_time_as_timet_with_zone (end_date, tz);
 
   e_cal_data_model_foreach_component (self->e_data_model,
                                       range_start,
@@ -1536,10 +1541,16 @@ gcal_manager_startup (GcalManager *self)
   GList *sources, *l;
   GError *error = NULL;
   ESourceCredentialsProvider *credentials_provider;
+  GcalApplication *application;
+  GcalContext *context;
+  GTimeZone *zone;
   ICalTimezone *tz;
 
   GCAL_ENTRY;
 
+  application = GCAL_APPLICATION (g_application_get_default ());
+  context = gcal_application_get_context (application);
+
   self->clients = g_hash_table_new_full ((GHashFunc) e_source_hash,
                                          (GEqualFunc) e_source_equal,
                                          g_object_unref,
@@ -1637,9 +1648,10 @@ gcal_manager_startup (GcalManager *self)
   self->e_data_model = e_cal_data_model_new (gcal_thread_submit_job);
 
   e_cal_data_model_set_expand_recurrences (self->e_data_model, TRUE);
-  tz = e_cal_util_get_system_timezone ();
-  if (tz != NULL)
-    e_cal_data_model_set_timezone (self->e_data_model, tz);
+
+  zone = gcal_context_get_timezone (context);
+  tz = gcal_timezone_to_icaltimezone (zone);
+  e_cal_data_model_set_timezone (self->e_data_model, tz);
 
   sources = e_source_registry_list_enabled (self->source_registry, E_SOURCE_EXTENSION_CALENDAR);
 
diff --git a/src/search/gcal-search-engine.c b/src/search/gcal-search-engine.c
index afc241c5..7cf44699 100644
--- a/src/search/gcal-search-engine.c
+++ b/src/search/gcal-search-engine.c
@@ -177,17 +177,16 @@ gcal_search_engine_constructed (GObject *object)
 {
   GcalSearchEngine *self = (GcalSearchEngine *)object;
   GcalManager *manager;
-  ICalTimezone *tz;
+  GTimeZone *zone;
 
   G_OBJECT_CLASS (gcal_search_engine_parent_class)->constructed (object);
 
   /* Setup the data model */
   self->data_model = e_cal_data_model_new (gcal_thread_submit_job);
   e_cal_data_model_set_expand_recurrences (self->data_model, TRUE);
-  tz = e_cal_util_get_system_timezone ();
-  if (tz != NULL)
-    e_cal_data_model_set_timezone (self->data_model, tz);
 
+  zone = gcal_context_get_timezone (self->context);
+  e_cal_data_model_set_timezone (self->data_model, gcal_timezone_to_icaltimezone (zone));
 
   manager = gcal_context_get_manager (self->context);
   g_signal_connect_object (manager, "calendar-added", G_CALLBACK (on_manager_calendar_added_cb), self, 0);
diff --git a/src/utils/gcal-date-time-utils.c b/src/utils/gcal-date-time-utils.c
index 45ea57a6..607394a3 100644
--- a/src/utils/gcal-date-time-utils.c
+++ b/src/utils/gcal-date-time-utils.c
@@ -230,3 +230,23 @@ gcal_date_time_from_icaltime (const ICalTime *date)
   return g_steal_pointer (&dt);
 }
 
+/**
+ * @tz: a #GTimezone
+ *
+ * Returns an #ICalTimezone corresponding to @tz.
+ *
+ * Returns: (transfer none): an #ICalTimezone.
+ */
+ICalTimezone*
+gcal_timezone_to_icaltimezone (GTimeZone *tz)
+{
+  const gchar *tzid;
+  ICalTimezone *ical_tz;
+
+  tzid = g_time_zone_get_identifier (tz);
+  ical_tz = i_cal_timezone_get_builtin_timezone (tzid);
+
+  g_assert (ical_tz != NULL);
+  return ical_tz;
+}
+
diff --git a/src/utils/gcal-date-time-utils.h b/src/utils/gcal-date-time-utils.h
index 369082b0..50d9707e 100644
--- a/src/utils/gcal-date-time-utils.h
+++ b/src/utils/gcal-date-time-utils.h
@@ -51,4 +51,6 @@ gboolean             gcal_date_time_is_date                      (GDateTime
 
 GDateTime*           gcal_date_time_from_icaltime                (const ICalTime     *date);
 
+ICalTimezone*        gcal_timezone_to_icaltimezone               (GTimeZone          *tz);
+
 G_END_DECLS
diff --git a/src/utils/gcal-utils.c b/src/utils/gcal-utils.c
index 765c35f2..1701b1c2 100644
--- a/src/utils/gcal-utils.c
+++ b/src/utils/gcal-utils.c
@@ -23,6 +23,8 @@
 /* langinfo.h in glibc 2.27 defines ALTMON_* only if _GNU_SOURCE is defined.  */
 #define _GNU_SOURCE
 
+#include "gcal-application.h"
+#include "gcal-context.h"
 #include "gcal-enums.h"
 #include "gcal-utils.h"
 #include "gcal-event-widget.h"
@@ -369,13 +371,18 @@ build_component_from_details (const gchar *summary,
                               GDateTime   *initial_date,
                               GDateTime   *final_date)
 {
+  GcalApplication *application;
+  GcalContext *context;
   ECalComponent *event;
   ECalComponentDateTime *dt;
   ECalComponentText *summ;
-  ICalTimezone *zone = NULL;
+  ICalTimezone *zone;
   ICalTime *itt;
   gboolean all_day;
 
+  application = GCAL_APPLICATION (g_application_get_default ());
+  context = gcal_application_get_context (application);
+
   event = e_cal_component_new ();
   e_cal_component_set_new_vtype (event, E_CAL_COMPONENT_EVENT);
 
@@ -389,13 +396,16 @@ build_component_from_details (const gchar *summary,
    * When the event is all day, we consider UTC timezone by default. Otherwise,
    * we always use the system timezone to create new events
    */
-  if (!all_day)
+  if (all_day)
     {
-      zone = e_cal_util_get_system_timezone ();
+      zone = i_cal_timezone_get_utc_timezone ();
     }
-  if (zone == NULL)
+  else
     {
-      zone = i_cal_timezone_get_utc_timezone ();
+      GTimeZone *gzone;
+
+      gzone = gcal_context_get_timezone (context);
+      zone = gcal_timezone_to_icaltimezone (gzone);
     }
 
   /* Start date */
@@ -476,21 +486,25 @@ icaltime_compare_with_current (const ICalTime *date1,
                                const ICalTime *date2,
                                time_t         *current_time_t)
 {
+  GcalApplication *application;
+  GcalContext *context;
+  GTimeZone *zone;
   ICalTimezone *zone1, *zone2;
   gint result = 0;
   time_t start1, start2, diff1, diff2;
 
+  application = GCAL_APPLICATION (g_application_get_default ());
+  context = gcal_application_get_context (application);
+
+  zone = gcal_context_get_timezone (context);
+
   zone1 = i_cal_time_get_timezone (date1);
   if (!zone1)
-    zone1 = e_cal_util_get_system_timezone ();
-  if (!zone1)
-    zone1 = i_cal_timezone_get_utc_timezone ();
+    zone1 = gcal_timezone_to_icaltimezone (zone);
 
   zone2 = i_cal_time_get_timezone (date2);
   if (!zone2)
-    zone2 = e_cal_util_get_system_timezone ();
-  if (!zone2)
-    zone2 = i_cal_timezone_get_utc_timezone ();
+    zone2 = gcal_timezone_to_icaltimezone (zone);
 
   start1 = i_cal_time_as_timet_with_zone (date1, zone1);
   start2 = i_cal_time_as_timet_with_zone (date2, zone2);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]