[gnome-calendar] time-zone-monitor: Introduce timezone monitor



commit ffb6eabe53d77198c1ad722d563636009014b9ab
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Wed Apr 24 18:56:23 2019 -0300

    time-zone-monitor: Introduce timezone monitor
    
    This will finally allow us monitor timezones and react
    to changes. And that's only possible because of the new
    GcalContext API!

 build-aux/flatpak/org.gnome.Calendar.json |   1 +
 src/gcal-context.c                        |  50 ++++++++
 src/gcal-context.h                        |   2 +
 src/gcal-time-zone-monitor.c              | 185 ++++++++++++++++++++++++++++++
 src/gcal-time-zone-monitor.h              |  35 ++++++
 src/meson.build                           |   1 +
 6 files changed, 274 insertions(+)
---
diff --git a/build-aux/flatpak/org.gnome.Calendar.json b/build-aux/flatpak/org.gnome.Calendar.json
index 397f70b5..f93cbba5 100644
--- a/build-aux/flatpak/org.gnome.Calendar.json
+++ b/build-aux/flatpak/org.gnome.Calendar.json
@@ -14,6 +14,7 @@
         "--socket=fallback-x11",
         "--socket=wayland",
         "--system-talk-name=org.freedesktop.login1",
+        "--system-talk-name=org.freedesktop.timedate1",
         "--system-talk-name=org.freedesktop.GeoClue2",
         "--talk-name=ca.desrt.dconf",
         "--talk-name=org.gnome.ControlCenter",
diff --git a/src/gcal-context.c b/src/gcal-context.c
index 4486c17e..2eacdde9 100644
--- a/src/gcal-context.c
+++ b/src/gcal-context.c
@@ -22,6 +22,7 @@
 
 #include "gcal-context.h"
 #include "gcal-night-light-monitor.h"
+#include "gcal-time-zone-monitor.h"
 
 struct _GcalContext
 {
@@ -37,6 +38,7 @@ struct _GcalContext
   GcalWeatherService *weather_service;
 
   GcalNightLightMonitor *night_light_monitor;
+  GcalTimeZoneMonitor   *timezone_monitor;
 };
 
 G_DEFINE_TYPE (GcalContext, gcal_context, G_TYPE_OBJECT)
@@ -49,6 +51,7 @@ enum
   PROP_MANAGER,
   PROP_SETTINGS,
   PROP_TIME_FORMAT,
+  PROP_TIMEZONE,
   PROP_WEATHER_SERVICE,
   N_PROPS
 };
@@ -80,6 +83,19 @@ load_time_format (GcalContext *self)
 }
 
 
+/*
+ * Callbacks
+ */
+
+static void
+on_timezone_changed_cb (GcalTimeZoneMonitor *timezone_monitor,
+                        GParamSpec          *pspec,
+                        GcalContext         *self)
+{
+  g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_TIMEZONE]);
+}
+
+
 /*
  * GObject overrides
  */
@@ -96,6 +112,7 @@ gcal_context_finalize (GObject *object)
   g_clear_object (&self->goa_client);
   g_clear_object (&self->manager);
   g_clear_object (&self->night_light_monitor);
+  g_clear_object (&self->timezone_monitor);
   g_clear_object (&self->weather_service);
 
   G_OBJECT_CLASS (gcal_context_parent_class)->finalize (object);
@@ -131,6 +148,10 @@ gcal_context_get_property (GObject    *object,
       g_value_set_enum (value, self->time_format);
       break;
 
+    case PROP_TIMEZONE:
+      g_value_set_boxed (value, gcal_time_zone_monitor_get_timezone (self->timezone_monitor));
+      break;
+
     case PROP_WEATHER_SERVICE:
       g_value_set_object (value, self->weather_service);
       break;
@@ -153,6 +174,7 @@ gcal_context_set_property (GObject      *object,
     case PROP_MANAGER:
     case PROP_SETTINGS:
     case PROP_TIME_FORMAT:
+    case PROP_TIMEZONE:
     case PROP_WEATHER_SERVICE:
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@@ -199,6 +221,12 @@ gcal_context_class_init (GcalContextClass *klass)
                                                     GCAL_TIME_FORMAT_24H,
                                                     G_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY | 
G_PARAM_STATIC_STRINGS);
 
+  properties[PROP_TIMEZONE] = g_param_spec_boxed ("timezone",
+                                                  "Timezone",
+                                                  "System timezone",
+                                                  G_TYPE_TIME_ZONE,
+                                                  G_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY | 
G_PARAM_STATIC_STRINGS);
+
   properties[PROP_WEATHER_SERVICE] = g_param_spec_object ("weather-service",
                                                           "Weather service",
                                                           "Weather service",
@@ -219,6 +247,13 @@ gcal_context_init (GcalContext *self)
 
   self->night_light_monitor = gcal_night_light_monitor_new (self);
 
+  self->timezone_monitor = gcal_time_zone_monitor_new ();
+  g_signal_connect_object (self->timezone_monitor,
+                           "notify::timezone",
+                           G_CALLBACK (on_timezone_changed_cb),
+                           self,
+                           0);
+
   /* Time format */
   self->desktop_settings = g_settings_new ("org.gnome.desktop.interface");
   g_signal_connect_object (self->desktop_settings,
@@ -318,6 +353,21 @@ gcal_context_get_time_format (GcalContext *self)
   return self->time_format;
 }
 
+/**
+ * gcal_context_get_timezone:
+ *
+ * Retrieves the system timezone.
+ *
+ * Returns: (transfer none): a #GTimeZone
+ */
+GTimeZone*
+gcal_context_get_timezone (GcalContext *self)
+{
+  g_return_val_if_fail (GCAL_IS_CONTEXT (self), NULL);
+
+  return gcal_time_zone_monitor_get_timezone (self->timezone_monitor);
+}
+
 /**
  * gcal_context_get_weather_service:
  *
diff --git a/src/gcal-context.h b/src/gcal-context.h
index 1925d947..57b149be 100644
--- a/src/gcal-context.h
+++ b/src/gcal-context.h
@@ -45,6 +45,8 @@ GSettings*           gcal_context_get_settings                   (GcalContext
 
 GcalTimeFormat       gcal_context_get_time_format                (GcalContext        *self);
 
+GTimeZone*           gcal_context_get_timezone                   (GcalContext        *self);
+
 GcalWeatherService*  gcal_context_get_weather_service            (GcalContext        *self);
 
 G_END_DECLS
diff --git a/src/gcal-time-zone-monitor.c b/src/gcal-time-zone-monitor.c
new file mode 100644
index 00000000..f5bdaef9
--- /dev/null
+++ b/src/gcal-time-zone-monitor.c
@@ -0,0 +1,185 @@
+/* gcal-time-zone-monitor.c
+ *
+ * Copyright 2019 Georges Basile Stavracas Neto <georges stavracas gmail com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY 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 <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#define G_LOG_DOMAIN "GcalTimeZoneMonitor"
+
+#include "gcal-debug.h"
+#include "gcal-time-zone-monitor.h"
+
+#include <gio/gio.h>
+
+struct _GcalTimeZoneMonitor
+{
+  GObject             parent;
+
+  GTimeZone          *timezone;
+
+  GDBusProxy         *timedate1_proxy;
+};
+
+G_DEFINE_TYPE (GcalTimeZoneMonitor, gcal_time_zone_monitor, G_TYPE_OBJECT)
+
+enum
+{
+  PROP_0,
+  PROP_TIMEZONE,
+  N_PROPS
+};
+
+static GParamSpec *properties [N_PROPS];
+
+
+/*
+ * Callbacks
+ */
+
+static void
+on_timedate1_proxy_properties_changed_cb (GcalTimeZoneMonitor *self,
+                                          GVariant            *props,
+                                          const gchar * const *invalidated,
+                                          GDBusProxy          *proxy)
+{
+  g_autoptr (GVariant) timezone_variant = NULL;
+  const gchar *timezone_identifier = NULL;
+
+  g_assert (GCAL_IS_TIME_ZONE_MONITOR (self));
+  g_assert (G_IS_DBUS_PROXY (proxy));
+
+  timezone_variant = g_dbus_proxy_get_cached_property (proxy, "Timezone");
+
+  /*
+   * NULL timezone_variant means NULL timezone_identifier, which falls back
+   * to regularly checking /etc/localtime
+   */
+  if (timezone_variant)
+    timezone_identifier = g_variant_get_string (timezone_variant, NULL);
+
+  self->timezone = g_time_zone_new (timezone_identifier);
+
+  g_debug ("System timezone is %s", g_time_zone_get_identifier (self->timezone));
+
+  g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_TIMEZONE]);
+}
+
+
+/*
+ * GObject overrides
+ */
+
+static void
+gcal_time_zone_monitor_finalize (GObject *object)
+{
+  GcalTimeZoneMonitor *self = (GcalTimeZoneMonitor *)object;
+
+  g_clear_object (&self->timedate1_proxy);
+  g_clear_object (&self->timezone);
+
+  G_OBJECT_CLASS (gcal_time_zone_monitor_parent_class)->finalize (object);
+}
+
+static void
+gcal_time_zone_monitor_get_property (GObject    *object,
+                                     guint       prop_id,
+                                     GValue     *value,
+                                     GParamSpec *pspec)
+{
+  GcalTimeZoneMonitor *self = GCAL_TIME_ZONE_MONITOR (object);
+
+  switch (prop_id)
+    {
+    case PROP_TIMEZONE:
+      g_value_set_boxed (value, self->timezone);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+gcal_time_zone_monitor_class_init (GcalTimeZoneMonitorClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->finalize = gcal_time_zone_monitor_finalize;
+  object_class->get_property = gcal_time_zone_monitor_get_property;
+
+  properties[PROP_TIMEZONE] = g_param_spec_boxed ("timezone",
+                                                  "Timezone",
+                                                  "System timezone",
+                                                  G_TYPE_TIME_ZONE,
+                                                  G_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY | 
G_PARAM_STATIC_STRINGS);
+
+  g_object_class_install_properties (object_class, N_PROPS, properties);
+}
+
+static void
+gcal_time_zone_monitor_init (GcalTimeZoneMonitor *self)
+{
+  g_autoptr (GDBusConnection) connection = NULL;
+  g_autoptr (GDBusProxy) proxy = NULL;
+
+  GCAL_ENTRY;
+
+  connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL);
+
+  if (!connection)
+    GCAL_RETURN ();
+
+  proxy = g_dbus_proxy_new_sync (connection,
+                                 G_DBUS_PROXY_FLAGS_GET_INVALIDATED_PROPERTIES,
+                                 NULL,
+                                 "org.freedesktop.timedate1",
+                                 "/org/freedesktop/timedate1",
+                                 "org.freedesktop.timedate1",
+                                 NULL,
+                                 NULL);
+
+  if (!proxy)
+    GCAL_RETURN ();
+
+  g_debug ("Created Time Zone monitor");
+
+  g_signal_connect_object (proxy,
+                           "g-properties-changed",
+                           G_CALLBACK (on_timedate1_proxy_properties_changed_cb),
+                           self,
+                           G_CONNECT_SWAPPED);
+
+  self->timedate1_proxy = g_steal_pointer (&proxy);
+
+  on_timedate1_proxy_properties_changed_cb (self, NULL, NULL, self->timedate1_proxy);
+
+  GCAL_EXIT;
+}
+
+GcalTimeZoneMonitor*
+gcal_time_zone_monitor_new (void)
+{
+  return g_object_new (GCAL_TYPE_TIME_ZONE_MONITOR, NULL);
+}
+
+GTimeZone*
+gcal_time_zone_monitor_get_timezone (GcalTimeZoneMonitor *self)
+{
+  g_return_val_if_fail (GCAL_IS_TIME_ZONE_MONITOR (self), NULL);
+
+  return self->timezone;
+}
diff --git a/src/gcal-time-zone-monitor.h b/src/gcal-time-zone-monitor.h
new file mode 100644
index 00000000..97a25f63
--- /dev/null
+++ b/src/gcal-time-zone-monitor.h
@@ -0,0 +1,35 @@
+/* gcal-time-zone-monitor.h
+ *
+ * Copyright 2019 Georges Basile Stavracas Neto <georges stavracas gmail com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY 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 <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#pragma once
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define GCAL_TYPE_TIME_ZONE_MONITOR (gcal_time_zone_monitor_get_type())
+
+G_DECLARE_FINAL_TYPE (GcalTimeZoneMonitor, gcal_time_zone_monitor, GCAL, TIME_ZONE_MONITOR, GObject)
+
+GcalTimeZoneMonitor* gcal_time_zone_monitor_new                  (void);
+
+GTimeZone*           gcal_time_zone_monitor_get_timezone         (GcalTimeZoneMonitor *self);
+
+G_END_DECLS
diff --git a/src/meson.build b/src/meson.build
index ce5685a7..adef2634 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -51,6 +51,7 @@ sources = files(
   'gcal-shell-search-provider.c',
   'gcal-source-dialog.c',
   'gcal-time-selector.c',
+  'gcal-time-zone-monitor.c',
   'gcal-utils.c',
   'gcal-window.c',
   'gcal-timer.c'


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