[gnome-calendar/wip/flb/weather-forecast: 47/50] view: Move weather API to interface GcalView



commit 600710177fff2d4800f304ab67245855c73e3f7f
Author: Florian Brosch <flo brosch gmail com>
Date:   Sun Oct 29 16:38:01 2017 +0100

    view: Move weather API to interface GcalView
    
    This allows us to get rid of similar code.

 src/views/gcal-month-view.c  |   71 +++-------------------------
 src/views/gcal-month-view.h  |    5 --
 src/views/gcal-view.c        |   93 +++++++++++++++++++++++++++++++++++++
 src/views/gcal-view.h        |   15 ++++++
 src/views/gcal-week-header.c |  104 ++++++++++++++++++++++++------------------
 src/views/gcal-week-view.c   |   13 +-----
 src/views/gcal-year-view.c   |   71 +++-------------------------
 src/views/gcal-year-view.h   |    6 --
 8 files changed, 184 insertions(+), 194 deletions(-)
---
diff --git a/src/views/gcal-month-view.c b/src/views/gcal-month-view.c
index bf2fe5d..0afc4b1 100644
--- a/src/views/gcal-month-view.c
+++ b/src/views/gcal-month-view.c
@@ -1346,8 +1346,12 @@ gcal_month_view_set_property (GObject       *object,
       break;
 
     case PROP_WEATHER_SERVICE:
-        gcal_month_view_set_weather_service (self, g_value_get_object (value));
-        break;
+      gcal_view_set_weather_service_impl_helper (&self->weather_service,
+                                                 GCAL_WEATHER_SERVICE (g_value_get_object (value)),
+                                                 (GcalWeatherUpdateFunc) update_weather,
+                                                 (GCallback) weather_changed,
+                                                 GTK_WIDGET (self));
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
       break;
@@ -1373,7 +1377,7 @@ gcal_month_view_get_property (GObject       *object,
       break;
 
     case PROP_WEATHER_SERVICE:
-      g_value_set_boxed (value, gcal_month_view_get_weather_service (self));
+      g_value_set_boxed (value, self->weather_service);
       break;
 
     default:
@@ -2078,18 +2082,7 @@ gcal_month_view_class_init (GcalMonthViewClass *klass)
 
   g_object_class_override_property (object_class, PROP_DATE, "active-date");
   g_object_class_override_property (object_class, PROP_MANAGER, "manager");
-
-  /**
-   * GcalWeekView:weather-service:
-   *
-   * Sets the weather service to use.
-   */
-  g_object_class_install_property
-      (object_class,
-       PROP_WEATHER_SERVICE,
-       g_param_spec_object ("weather-service", "weather-service", "weather-service",
-                            GCAL_TYPE_WEATHER_SERVICE,
-                            G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE));
+  g_object_class_override_property (object_class, PROP_WEATHER_SERVICE, "weather-service");
 
   signals[EVENT_ACTIVATED] = g_signal_new ("event-activated",
                                            GCAL_TYPE_MONTH_VIEW,
@@ -2201,51 +2194,3 @@ gcal_month_view_set_use_24h_format (GcalMonthView *self,
 {
   self->use_24h_format = use_24h;
 }
-
-/**
- * gcal_month_view_set_weather_service:
- * @self: The #GcalWeatherView instance.
- * @service: (nullable): The weather service to query.
- *
- * Sets the service to query for weather reports.
- */
-void
-gcal_month_view_set_weather_service (GcalMonthView      *self,
-                                     GcalWeatherService *service)
-{
-  g_return_if_fail (GCAL_IS_MONTH_VIEW (self));
-  g_return_if_fail (service == NULL || GCAL_IS_WEATHER_SERVICE (service));
-
-  if (self->weather_service != service)
-    {
-      if (self->weather_service != NULL)
-        g_signal_handlers_disconnect_by_func (self->weather_service,
-                                              (GCallback) weather_changed,
-                                              self);
-
-      g_set_object (&self->weather_service, service);
-
-      if (self->weather_service != NULL)
-        g_signal_connect (self->weather_service,
-                          "weather-changed",
-                          (GCallback) weather_changed,
-                          self);
-
-      update_weather (self, TRUE);
-      g_object_notify (G_OBJECT (self), "weather-service");
-    }
-}
-
-/**
- * gcal_month_view_get_weather_service:
- * @self: The #GcalWeatherView instance.
- *
- * Returns: (transfer none): The internal weather service object.
- */
-GcalWeatherService*
-gcal_month_view_get_weather_service (GcalMonthView *self)
-{
-  g_return_val_if_fail (GCAL_IS_MONTH_VIEW (self), NULL);
-
-  return self->weather_service;
-}
diff --git a/src/views/gcal-month-view.h b/src/views/gcal-month-view.h
index 1670aa7..86d1d2c 100644
--- a/src/views/gcal-month-view.h
+++ b/src/views/gcal-month-view.h
@@ -35,11 +35,6 @@ void                 gcal_month_view_set_first_weekday           (GcalMonthView
 void                 gcal_month_view_set_use_24h_format          (GcalMonthView      *self,
                                                                   gboolean            use_24h);
 
-void                 gcal_month_view_set_weather_service         (GcalMonthView      *self,
-                                                                  GcalWeatherService *service);
-
-GcalWeatherService*  gcal_month_view_get_weather_service         (GcalMonthView      *self);
-
 G_END_DECLS
 
 #endif /* __GCAL_MONTH_VIEW_H__ */
diff --git a/src/views/gcal-view.c b/src/views/gcal-view.c
index d9ce8b7..93af9af 100644
--- a/src/views/gcal-view.c
+++ b/src/views/gcal-view.c
@@ -57,6 +57,18 @@ gcal_view_default_init (GcalViewInterface *iface)
                                                             G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
   /**
+   * GcalView::weather-service:
+   *
+   * The #GcalWeatherService of the view.
+   */
+  g_object_interface_install_property (iface,
+                                       g_param_spec_object ("weather-service",
+                                                            "The weather service",
+                                                            "The weather service of the view",
+                                                            GCAL_TYPE_WEATHER_SERVICE,
+                                                            G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+  /**
    * GcalView::create-event:
    *
    * Emitted when the view wants to create an event.
@@ -126,6 +138,87 @@ gcal_view_get_manager (GcalView *self)
 }
 
 /**
+ * gcal_view_get_weather_service:
+ * @self: The #GcalView instance.
+ * @service: (nullable): The weather service to query.
+ *
+ * Sets the service to query for weather reports.
+ */
+GcalWeatherService*
+gcal_view_get_weather_service (GcalView *self)
+{
+  GcalWeatherService *service; /* unowned */
+
+  g_return_val_if_fail (GCAL_IS_VIEW (self), NULL);
+
+  g_object_get (self, "weather-service", &service, NULL);
+  g_object_unref (service);
+
+  return service;
+}
+
+/**
+ * gcal_view_set_weather_service:
+ * @self: The #GcalView instance.
+ *
+ * Returns: (transfer none): The internal weather service object.
+ */
+void
+gcal_view_set_weather_service (GcalView           *self,
+                               GcalWeatherService *service)
+{
+  g_return_if_fail (GCAL_IS_VIEW (self));
+  g_return_if_fail (service == NULL || GCAL_IS_WEATHER_SERVICE (service));
+
+  g_object_set (self, "weather-service", &service, NULL);
+}
+
+/**
+ * gcal_view_set_weather_service_impl_helper:
+ * @old_service: The views weather service field.
+ * @new_service: (nullable): The new weather service to use.
+ * @update_func: The function to call after updating weather.
+ * @weather_changed_cb: The views "weather-changed" handler
+ * @data: The data to pass to @update_func and @weather_changed_cb
+ *
+ * Internal implementation helper for
+ * gcal_view_set_weather_service().
+ */
+void
+gcal_view_set_weather_service_impl_helper (GcalWeatherService  **old_service,
+                                           GcalWeatherService   *new_service,
+                                           GcalWeatherUpdateFunc update_func,
+                                           GCallback             weather_changed_cb,
+                                           GtkWidget            *data)
+{
+  g_return_if_fail (old_service != NULL);
+  g_return_if_fail (*old_service == NULL || GCAL_IS_WEATHER_SERVICE (*old_service));
+  g_return_if_fail (new_service == NULL || GCAL_IS_WEATHER_SERVICE (new_service));
+  g_return_if_fail (update_func != NULL);
+  g_return_if_fail (weather_changed_cb != NULL);
+  g_return_if_fail (GTK_IS_WIDGET (data));
+
+  if (*old_service != new_service)
+    {
+      if (*old_service != NULL)
+        g_signal_handlers_disconnect_by_func (*old_service,
+                                              (GCallback) weather_changed_cb,
+                                              data);
+
+      g_set_object (old_service, new_service);
+
+      if (*old_service != NULL)
+        g_signal_connect (*old_service,
+                          "weather-changed",
+                          (GCallback) weather_changed_cb,
+                          data);
+
+      update_func (data);
+      g_object_notify (G_OBJECT (data), "weather-service");
+    }
+}
+
+/**
  * gcal_view_get_date:
  * @view: a #GcalView
  *
diff --git a/src/views/gcal-view.h b/src/views/gcal-view.h
index ce416a3..20586ba 100644
--- a/src/views/gcal-view.h
+++ b/src/views/gcal-view.h
@@ -21,6 +21,7 @@
 #define __GCAL_VIEW_H__
 
 #include "gcal-event-widget.h"
+#include "gcal-weather-service.h"
 
 G_BEGIN_DECLS
 
@@ -28,6 +29,8 @@ G_BEGIN_DECLS
 
 G_DECLARE_INTERFACE (GcalView, gcal_view, GCAL, VIEW, GtkWidget)
 
+typedef void (*GcalWeatherUpdateFunc) (GtkWidget *widget);
+
 struct _GcalViewInterface
 {
   GTypeInterface      parent;
@@ -63,12 +66,24 @@ icaltimetype*        gcal_view_get_date                          (GcalView
 
 GcalManager*         gcal_view_get_manager                       (GcalView           *self);
 
+GcalWeatherService*  gcal_view_get_weather_service               (GcalView           *view);
+
+void                 gcal_view_set_weather_service               (GcalView           *view,
+                                                                  GcalWeatherService *service);
+
+void                 gcal_view_set_weather_service_impl_helper   (GcalWeatherService **old_service,
+                                                                  GcalWeatherService *new_service,
+                                                                  GcalWeatherUpdateFunc update_func,
+                                                                  GCallback            weather_changed_cb,
+                                                                  GtkWidget          *data);
+
 void                 gcal_view_clear_marks                       (GcalView           *view);
 
 GList*               gcal_view_get_children_by_uuid              (GcalView              *view,
                                                                   GcalRecurrenceModType  mod,
                                                                   const gchar           *uuid);
 
+
 G_END_DECLS
 
 #endif /* __GCAL_MONTH_VIEW_H__ */
diff --git a/src/views/gcal-week-header.c b/src/views/gcal-week-header.c
index cc48036..8d48c23 100644
--- a/src/views/gcal-week-header.c
+++ b/src/views/gcal-week-header.c
@@ -95,7 +95,6 @@ struct _GcalWeekHeader
   gint              dnd_cell;
 
   GcalWeatherService *weather_service; /* unowned, nullable */
-  gulong              on_weather_changed_hid;
   /* Array of nullable weather infos for each day, starting with Sunday. */
   WeatherInfoDay      weather_infos[7];
 
@@ -110,10 +109,14 @@ typedef enum
 
 enum
 {
+  PROP_0,
+  PROP_WEATHER_SERVICE,
+  NUM_PROPS
+};
+
+enum
+{
   EVENT_ACTIVATED,
-  /*
-  EVENT_WEATHER_MOUSEOVER,
-  */
   LAST_SIGNAL
 };
 
@@ -1091,26 +1094,56 @@ gcal_week_header_finalize (GObject *object)
   for (i = 0; i < 7; i++)
     g_list_free (self->events[i]);
 
+  if (self->weather_service != NULL)
+    {
+      g_signal_handlers_disconnect_by_func (self->weather_service,
+                                            (GCallback) on_weather_update,
+                                            self);
+      g_clear_object (&self->weather_service);
+    }
+
   for (i = 0; i < G_N_ELEMENTS (self->weather_infos); i++)
     wid_clear (&self->weather_infos[i]);
 }
 
 static void
 gcal_week_header_get_property (GObject    *object,
-                               guint       prop_id,
+                               guint       property_id,
                                GValue     *value,
                                GParamSpec *pspec)
 {
-  G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+  GcalWeekHeader *self = GCAL_WEEK_HEADER (object);
+
+  switch (property_id)
+    {
+    case PROP_WEATHER_SERVICE:
+      g_value_set_boxed (value, self->weather_service);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+      break;
+    }
 }
 
 static void
 gcal_week_header_set_property (GObject      *object,
-                               guint         prop_id,
+                               guint         property_id,
                                const GValue *value,
                                GParamSpec   *pspec)
 {
-  G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+  GcalWeekHeader *self = GCAL_WEEK_HEADER (object);
+
+  switch (property_id)
+    {
+    case PROP_WEATHER_SERVICE:
+        gcal_week_header_set_weather_service (self, GCAL_WEATHER_SERVICE (g_value_get_object (value)));
+        break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+      break;
+    }
 }
 
 static void
@@ -1591,6 +1624,19 @@ gcal_week_header_class_init (GcalWeekHeaderClass *kclass)
   widget_class->drag_leave = gcal_week_header_drag_leave;
   widget_class->drag_drop = gcal_week_header_drag_drop;
 
+  /**
+   * GcalWeekHeader::weather-service:
+   *
+   * The #GcalWeatherService of the view.
+   */
+  g_object_class_install_property (G_OBJECT_CLASS (kclass),
+                                   PROP_WEATHER_SERVICE,
+                                   g_param_spec_object ("weather-service",
+                                                        "The weather service",
+                                                        "The weather service of the view",
+                                                        GCAL_TYPE_WEATHER_SERVICE,
+                                                        G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
   signals[EVENT_ACTIVATED] = g_signal_new ("event-activated",
                                            GCAL_TYPE_WEEK_HEADER,
                                            G_SIGNAL_RUN_FIRST,
@@ -1599,22 +1645,6 @@ gcal_week_header_class_init (GcalWeekHeaderClass *kclass)
                                            1,
                                            GCAL_TYPE_EVENT_WIDGET);
 
-  /* *
-   * GCalWeekHeader::mouseover-weather:
-   * @info: Hovered GcalWeatherInfo
-   *
-   * Triggered when cursor points to a weather indicator.
-   */
-  /*
-  signals[EVENT_WEATHER_MOUSEOVER] = g_signal_new ("weather-mouseover",
-                                                   GCAL_TYPE_WEEK_HEADER,
-                                                   G_SIGNAL_RUN_FIRST,
-                                                   0, NULL, NULL, NULL,
-                                                   G_TYPE_NONE,
-                                                   1,
-                                                   GCAL_TYPE_WEATHER_INFO);
-  */
-
   gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/calendar/week-header.ui");
 
   gtk_widget_class_bind_template_child (widget_class, GcalWeekHeader, expand_button);
@@ -1644,7 +1674,6 @@ gcal_week_header_init (GcalWeekHeader *self)
   self->selection_end = -1;
   self->dnd_cell = -1;
   self->weather_service = NULL;
-  self->on_weather_changed_hid = 0;
   memset (self->weather_infos, 0, sizeof(self->weather_infos));
   gtk_widget_init_template (GTK_WIDGET (self));
 
@@ -1750,26 +1779,11 @@ gcal_week_header_set_weather_service (GcalWeekHeader     *self,
   g_return_if_fail (GCAL_IS_WEEK_HEADER (self));
   g_return_if_fail (service == NULL || GCAL_IS_WEATHER_SERVICE (service));
 
-  if (self->weather_service == service)
-    return;
-
-  if (self->on_weather_changed_hid > 0)
-    {
-      g_signal_handler_disconnect (self->weather_service, self->on_weather_changed_hid);
-      self->on_weather_changed_hid = 0;
-    }
-
-  self->weather_service = service;
-
-  if (self->weather_service != NULL)
-    self->on_weather_changed_hid = g_signal_connect_object (
-                                         self->weather_service,
-                                         "weather-changed",
-                                         G_CALLBACK (on_weather_update),
-                                         self,
-                                         0);
-
-  gcal_week_header_update_weather_infos (self);
+  gcal_view_set_weather_service_impl_helper (&self->weather_service,
+                                             service,
+                                             (GcalWeatherUpdateFunc) gcal_week_header_update_weather_infos,
+                                             (GCallback) on_weather_update,
+                                             GTK_WIDGET (self));
 }
 
 /**
diff --git a/src/views/gcal-week-view.c b/src/views/gcal-week-view.c
index 5e65d47..7290b95 100644
--- a/src/views/gcal-week-view.c
+++ b/src/views/gcal-week-view.c
@@ -625,18 +625,7 @@ gcal_week_view_class_init (GcalWeekViewClass *klass)
 
   g_object_class_override_property (object_class, PROP_DATE, "active-date");
   g_object_class_override_property (object_class, PROP_MANAGER, "manager");
-
-  /**
-   * GcalWeekView:weather-service:
-   *
-   * Sets the weather service to use.
-   */
-  g_object_class_install_property
-      (object_class,
-       PROP_WEATHER_SERVICE,
-       g_param_spec_object ("weather-service", "weather-service", "weather-service",
-                            GCAL_TYPE_WEATHER_SERVICE,
-                            G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE));
+  g_object_class_override_property (object_class, PROP_WEATHER_SERVICE, "weather-service");
 
   signals[EVENT_ACTIVATED] = g_signal_new ("event-activated",
                                            GCAL_TYPE_WEEK_VIEW,
diff --git a/src/views/gcal-year-view.c b/src/views/gcal-year-view.c
index 75597e3..f18d1cc 100644
--- a/src/views/gcal-year-view.c
+++ b/src/views/gcal-year-view.c
@@ -66,7 +66,7 @@ struct _GcalYearView
 
   /* manager singleton */
   GcalManager  *manager;
-  GcalWeatherService *weather_service;
+  GcalWeatherService *weather_service; /* owned, nullable */
 
   /* range shown on the sidebar */
   icaltimetype *start_selected_date;
@@ -1678,7 +1678,7 @@ gcal_year_view_get_property (GObject    *object,
       break;
 
     case PROP_WEATHER_SERVICE:
-      g_value_set_object (value, gcal_year_view_get_weather_service (self));
+      g_value_set_object (value, self->weather_service);
       break;
 
     case PROP_SHOW_WEEK_NUMBERS:
@@ -1716,7 +1716,11 @@ gcal_year_view_set_property (GObject      *object,
       break;
 
     case PROP_WEATHER_SERVICE:
-      gcal_year_view_set_weather_service (self, g_value_get_object (value));
+      gcal_view_set_weather_service_impl_helper (&self->weather_service,
+                                                 GCAL_WEATHER_SERVICE (g_value_get_object (value)),
+                                                 (GcalWeatherUpdateFunc) update_weather,
+                                                 (GCallback) weather_changed,
+                                                 GTK_WIDGET (self));
       break;
 
     case PROP_SHOW_WEEK_NUMBERS:
@@ -1994,18 +1998,7 @@ gcal_year_view_class_init (GcalYearViewClass *klass)
 
   g_object_class_override_property (object_class, PROP_DATE, "active-date");
   g_object_class_override_property (object_class, PROP_MANAGER, "manager");
-
-  /**
-   * GcalWeekView:weather-service:
-   *
-   * Sets the weather service to use.
-   */
-  g_object_class_install_property
-      (object_class,
-       PROP_WEATHER_SERVICE,
-       g_param_spec_object ("weather-service", "weather-service", "weather-service",
-                            GCAL_TYPE_WEATHER_SERVICE,
-                            G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE));
+  g_object_class_override_property (object_class, PROP_WEATHER_SERVICE, "weather-service");
 
   g_object_class_install_property (object_class,
                                    PROP_SHOW_WEEK_NUMBERS,
@@ -2171,51 +2164,3 @@ gcal_year_view_set_use_24h_format (GcalYearView *year_view,
 {
   year_view->use_24h_format = use_24h_format;
 }
-
-/**
- * gcal_year_view_set_weather_service:
- * @self: The #GcalYearView instance.
- * @service: (nullable): The weather service to query.
- *
- * Sets the service to query for weather reports.
- */
-void
-gcal_year_view_set_weather_service (GcalYearView       *self,
-                                    GcalWeatherService *service)
-{
-  g_return_if_fail (GCAL_IS_YEAR_VIEW (self));
-  g_return_if_fail (service == NULL || GCAL_IS_WEATHER_SERVICE (service));
-
-  if (self->weather_service != service)
-    {
-      if (self->weather_service != NULL)
-        g_signal_handlers_disconnect_by_func (self->weather_service,
-                                              (GCallback) weather_changed,
-                                              self);
-
-      g_set_object (&self->weather_service, service);
-
-      if (self->weather_service != NULL)
-        g_signal_connect (self->weather_service,
-                          "weather-changed",
-                          (GCallback) weather_changed,
-                          self);
-
-      update_weather (self);
-      g_object_notify (G_OBJECT (self), "weather-service");
-    }
-}
-
-/**
- * gcal_year_view_get_weather_service:
- * @self: The #GcalYearView instance.
- *
- * Returns: (transfer none): The internal weather service object.
- */
-GcalWeatherService*
-gcal_year_view_get_weather_service (GcalYearView *self)
-{
-  g_return_val_if_fail (GCAL_IS_YEAR_VIEW (self), NULL);
-
-  return self->weather_service;
-}
diff --git a/src/views/gcal-year-view.h b/src/views/gcal-year-view.h
index 15f548a..da0d18e 100644
--- a/src/views/gcal-year-view.h
+++ b/src/views/gcal-year-view.h
@@ -38,12 +38,6 @@ void              gcal_year_view_set_use_24h_format (GcalYearView *year_view,
 void              gcal_year_view_set_current_date   (GcalYearView *year_view,
                                                      icaltimetype *current_date);
 
-void              gcal_year_view_set_weather_service (GcalYearView *self,
-                                                      GcalWeatherService *service);
-
-GcalWeatherService* gcal_year_view_get_weather_service (GcalYearView *self);
-
-
 G_END_DECLS
 
 #endif /* GCAL_YEAR_VIEW_H */


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