[gnome-calendar/gbsneto/timeline: 6/18] search: Port to GcalTimeline



commit b3cefa885de943fc3b0f5c48471d0013ed3a7ad5
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Fri Mar 27 10:22:27 2020 -0300

    search: Port to GcalTimeline

 src/search/gcal-search-engine.c | 71 +++++++-------------------------
 src/search/gcal-search-model.c  | 91 +++++++++++++++++++----------------------
 src/search/gcal-search-model.h  |  4 +-
 3 files changed, 60 insertions(+), 106 deletions(-)
---
diff --git a/src/search/gcal-search-engine.c b/src/search/gcal-search-engine.c
index 8ad8cec7..d0818c77 100644
--- a/src/search/gcal-search-engine.c
+++ b/src/search/gcal-search-engine.c
@@ -24,7 +24,8 @@
 #include "gcal-date-time-utils.h"
 #include "gcal-search-engine.h"
 #include "gcal-search-model.h"
-#include "gcal-thread-utils.h"
+#include "gcal-timeline.h"
+#include "gcal-timeline-subscriber.h"
 
 #include <dazzle.h>
 
@@ -41,7 +42,7 @@ struct _GcalSearchEngine
 {
   GObject             parent;
 
-  ECalDataModel      *data_model;
+  GcalTimeline       *timeline;
 
   GcalContext        *context;
 };
@@ -91,21 +92,17 @@ search_func (GTask        *task,
   g_autoptr (GcalSearchModel) model = NULL;
   GcalSearchEngine *self;
   SearchData *data;
-  time_t start;
-  time_t end;
 
   self = GCAL_SEARCH_ENGINE (source_object);
   data = (SearchData*) task_data;
-  start = g_date_time_to_unix (data->range_start);
-  end = g_date_time_to_unix (data->range_end);
 
-  model = gcal_search_model_new (cancellable, data->max_results);
-  e_cal_data_model_set_filter (self->data_model, data->query);
+  model = gcal_search_model_new (cancellable,
+                                 data->max_results,
+                                 data->range_start,
+                                 data->range_end);
 
-  e_cal_data_model_subscribe (self->data_model,
-                              E_CAL_DATA_MODEL_SUBSCRIBER (model),
-                              start,
-                              end);
+  gcal_timeline_set_filter (self->timeline, data->query);
+  gcal_timeline_add_subscriber (self->timeline, GCAL_TIMELINE_SUBSCRIBER (model));
 
   gcal_search_model_wait_for_hits (model, cancellable);
 
@@ -120,27 +117,9 @@ on_manager_calendar_added_cb (GcalManager      *manager,
                               GcalCalendar     *calendar,
                               GcalSearchEngine *self)
 {
-  ECalClient *client;
+  g_debug ("Adding calendar %s to search results", gcal_calendar_get_id (calendar));
 
-  g_debug ("Adding source %s to search results", gcal_calendar_get_id (calendar));
-
-  client = gcal_calendar_get_client (calendar);
-
-  if (gcal_calendar_get_visible (calendar))
-    e_cal_data_model_add_client (self->data_model, client);
-}
-
-static void
-on_manager_calendar_changed_cb (GcalManager      *manager,
-                                GcalCalendar     *calendar,
-                                GcalSearchEngine *self)
-{
-  g_debug ("Changing source %s from search results", gcal_calendar_get_id (calendar));
-
-  if (gcal_calendar_get_visible (calendar))
-    e_cal_data_model_add_client (self->data_model, gcal_calendar_get_client (calendar));
-  else
-    e_cal_data_model_remove_client (self->data_model, gcal_calendar_get_id (calendar));
+  gcal_timeline_add_calendar (self->timeline, calendar);
 }
 
 static void
@@ -148,17 +127,9 @@ on_manager_calendar_removed_cb (GcalManager      *manager,
                                 GcalCalendar     *calendar,
                                 GcalSearchEngine *self)
 {
-  g_debug ("Removing source %s from search results", gcal_calendar_get_id (calendar));
-
-  e_cal_data_model_remove_client (self->data_model, gcal_calendar_get_id (calendar));
-}
+  g_debug ("Removing calendar %s from search results", gcal_calendar_get_id (calendar));
 
-static void
-on_timezone_changed_cb (GcalContext      *context,
-                        GParamSpec       *pspec,
-                        GcalSearchEngine *self)
-{
-  g_debug ("Timezone changed");
+  gcal_timeline_remove_calendar (self->timeline, calendar);
 }
 
 
@@ -172,7 +143,7 @@ gcal_search_engine_finalize (GObject *object)
   GcalSearchEngine *self = (GcalSearchEngine *)object;
 
   g_clear_object (&self->context);
-  g_clear_object (&self->data_model);
+  g_clear_object (&self->timeline);
 
   G_OBJECT_CLASS (gcal_search_engine_parent_class)->finalize (object);
 }
@@ -182,27 +153,15 @@ gcal_search_engine_constructed (GObject *object)
 {
   GcalSearchEngine *self = (GcalSearchEngine *)object;
   GcalManager *manager;
-  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);
-
-  zone = gcal_context_get_timezone (self->context);
-  e_cal_data_model_set_timezone (self->data_model, gcal_timezone_to_icaltimezone (zone));
+  self->timeline = gcal_timeline_new (self->context);
 
   manager = gcal_context_get_manager (self->context);
   g_signal_connect_object (manager, "calendar-added", G_CALLBACK (on_manager_calendar_added_cb), self, 0);
-  g_signal_connect_object (manager, "calendar-changed", G_CALLBACK (on_manager_calendar_changed_cb), self, 
0);
   g_signal_connect_object (manager, "calendar-removed", G_CALLBACK (on_manager_calendar_removed_cb), self, 
0);
-
-  g_signal_connect_object (self->context,
-                           "notify::timezone",
-                           G_CALLBACK (on_timezone_changed_cb),
-                           self,
-                           0);
 }
 
 static void
diff --git a/src/search/gcal-search-model.c b/src/search/gcal-search-model.c
index e31102f5..bf4f0932 100644
--- a/src/search/gcal-search-model.c
+++ b/src/search/gcal-search-model.c
@@ -20,10 +20,10 @@
 
 #define G_LOG_DOMAIN "GcalSearchModel"
 
-#include "e-cal-data-model.h"
 #include "gcal-application.h"
 #include "gcal-context.h"
 #include "gcal-debug.h"
+#include "gcal-timeline-subscriber.h"
 #include "gcal-search-hit.h"
 #include "gcal-search-hit-event.h"
 #include "gcal-search-model.h"
@@ -40,17 +40,19 @@ struct _GcalSearchModel
 
   GCancellable       *cancellable;
   gint                max_results;
+  GDateTime          *range_start;
+  GDateTime          *range_end;
 
   GListModel         *model;
 };
 
-static void e_cal_data_model_subscriber_interface_init (ECalDataModelSubscriberInterface *iface);
+static void          gcal_timeline_subscriber_interface_init     (GcalTimelineSubscriberInterface *iface);
 
 static void g_list_model_interface_init                (GListModelInterface              *iface);
 
 G_DEFINE_TYPE_WITH_CODE (GcalSearchModel, gcal_search_model, G_TYPE_OBJECT,
-                         G_IMPLEMENT_INTERFACE (E_TYPE_CAL_DATA_MODEL_SUBSCRIBER,
-                                                e_cal_data_model_subscriber_interface_init)
+                         G_IMPLEMENT_INTERFACE (GCAL_TYPE_TIMELINE_SUBSCRIBER,
+                                                gcal_timeline_subscriber_interface_init)
                          G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL,
                                                 g_list_model_interface_init))
 
@@ -70,7 +72,7 @@ on_model_items_changed_cb (GListModel      *model,
 
 
 /*
- * ECalDataModelSubscriber interface
+ * GcalTimelineSubscriber interface
  */
 
 static gint
@@ -94,36 +96,34 @@ compare_search_hits_cb (gconstpointer a,
   return gcal_search_hit_compare (hit_a, hit_b);
 }
 
+static GDateTime*
+gcal_search_model_get_range_start (GcalTimelineSubscriber *subscriber)
+{
+  GcalSearchModel *self = GCAL_SEARCH_MODEL (subscriber);
+
+  return g_date_time_ref (self->range_start);
+}
+
+static GDateTime*
+gcal_search_model_get_range_end (GcalTimelineSubscriber *subscriber)
+{
+  GcalSearchModel *self = GCAL_SEARCH_MODEL (subscriber);
+
+  return g_date_time_ref (self->range_end);
+}
+
 static void
-gcal_search_model_component_added (ECalDataModelSubscriber *subscriber,
-                                   ECalClient              *client,
-                                   ECalComponent           *component)
+gcal_search_model_add_event (GcalTimelineSubscriber *subscriber,
+                             GcalEvent              *event)
 {
   g_autoptr (GcalSearchHitEvent) search_hit = NULL;
-  g_autoptr (GcalEvent) event = NULL;
-  g_autoptr (GError) error = NULL;
   GcalSearchModel *self;
-  GcalCalendar *calendar;
-  GcalContext *context;
-  ESource *source;
 
   self = GCAL_SEARCH_MODEL (subscriber);
 
   if (g_list_model_get_n_items (self->model) > self->max_results)
     return;
 
-  /* FIXME: propagate context to the model properly */
-  context = gcal_application_get_context (GCAL_APPLICATION (g_application_get_default ()));
-  source = e_client_get_source (E_CLIENT (client));
-  calendar = gcal_manager_get_calendar_from_source (gcal_context_get_manager (context), source);
-  event = gcal_event_new (calendar, component, &error);
-
-  if (error)
-    {
-      g_warning ("Error adding event to search results: %s", error->message);
-      return;
-    }
-
   GCAL_TRACE_MSG ("Adding search hit '%s'", gcal_event_get_summary (event));
 
   search_hit = gcal_search_hit_event_new (event);
@@ -135,38 +135,25 @@ gcal_search_model_component_added (ECalDataModelSubscriber *subscriber,
 }
 
 static void
-gcal_search_model_component_modified (ECalDataModelSubscriber *subscriber,
-                                      ECalClient              *client,
-                                      ECalComponent           *comp)
-{
-}
-
-static void
-gcal_search_model_component_removed (ECalDataModelSubscriber *subscriber,
-                                     ECalClient              *client,
-                                     const gchar             *uid,
-                                     const gchar             *rid)
-{
-}
-
-static void
-gcal_search_model_freeze (ECalDataModelSubscriber *subscriber)
+gcal_search_model_update_event (GcalTimelineSubscriber *subscriber,
+                                GcalEvent              *event)
 {
 }
 
 static void
-gcal_search_model_thaw (ECalDataModelSubscriber *subscriber)
+gcal_search_model_remove_event (GcalTimelineSubscriber *subscriber,
+                                GcalEvent              *event)
 {
 }
 
 static void
-e_cal_data_model_subscriber_interface_init (ECalDataModelSubscriberInterface *iface)
+gcal_timeline_subscriber_interface_init (GcalTimelineSubscriberInterface *iface)
 {
-  iface->component_added = gcal_search_model_component_added;
-  iface->component_modified = gcal_search_model_component_modified;
-  iface->component_removed = gcal_search_model_component_removed;
-  iface->freeze = gcal_search_model_freeze;
-  iface->thaw = gcal_search_model_thaw;
+  iface->get_range_start = gcal_search_model_get_range_start;
+  iface->get_range_end = gcal_search_model_get_range_end;
+  iface->add_event = gcal_search_model_add_event;
+  iface->update_event = gcal_search_model_update_event;
+  iface->remove_event = gcal_search_model_remove_event;
 }
 
 
@@ -215,6 +202,8 @@ gcal_search_model_finalize (GObject *object)
 
   g_cancellable_cancel (self->cancellable);
 
+  gcal_clear_date_time (&self->range_start);
+  gcal_clear_date_time (&self->range_end);
   g_clear_object (&self->cancellable);
   g_clear_object (&self->model);
 
@@ -238,13 +227,17 @@ gcal_search_model_init (GcalSearchModel *self)
 
 GcalSearchModel *
 gcal_search_model_new (GCancellable *cancellable,
-                       gint          max_results)
+                       gint          max_results,
+                       GDateTime    *range_start,
+                       GDateTime    *range_end)
 {
   GcalSearchModel *model;
 
   model = g_object_new (GCAL_TYPE_SEARCH_MODEL, NULL);
   model->max_results = max_results;
   model->cancellable = cancellable ? g_object_ref (cancellable) : NULL;
+  model->range_start = g_date_time_ref (range_start);
+  model->range_end = g_date_time_ref (range_end);
 
   return model;
 }
diff --git a/src/search/gcal-search-model.h b/src/search/gcal-search-model.h
index 3a0f7eb8..97775834 100644
--- a/src/search/gcal-search-model.h
+++ b/src/search/gcal-search-model.h
@@ -28,7 +28,9 @@ G_BEGIN_DECLS
 G_DECLARE_FINAL_TYPE (GcalSearchModel, gcal_search_model, GCAL, SEARCH_MODEL, GObject)
 
 GcalSearchModel*     gcal_search_model_new                       (GCancellable       *cancellable,
-                                                                  gint                max_results);
+                                                                  gint                max_results,
+                                                                  GDateTime          *range_start,
+                                                                  GDateTime          *range_end);
 
 void                 gcal_search_model_wait_for_hits             (GcalSearchModel    *self,
                                                                   GCancellable       *cancellable);


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