[gnome-calendar/gnome-3-24] manager: use EDS selectable property to track disabled agendas



commit 1dba74d628b127723a600b8d6f860ae05ad3f2a0
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Fri Mar 17 21:25:23 2017 -0300

    manager: use EDS selectable property to track disabled agendas
    
    This way, we can keep GNOME Calendar and Evolution synchronized.
    And we can save some code too.

 data/org.gnome.calendar.gschema.xml.in |    5 --
 src/gcal-manager.c                     |  101 +++++++-------------------------
 src/gcal-manager.h                     |    3 -
 src/gcal-quick-add-popover.c           |    2 +-
 src/gcal-source-dialog.c               |   16 +++--
 src/gcal-utils.c                       |   12 ++++
 src/gcal-utils.h                       |    2 +
 src/gcal-window.c                      |    8 ++-
 8 files changed, 52 insertions(+), 97 deletions(-)
---
diff --git a/data/org.gnome.calendar.gschema.xml.in b/data/org.gnome.calendar.gschema.xml.in
index fb0aef8..99d2059 100644
--- a/data/org.gnome.calendar.gschema.xml.in
+++ b/data/org.gnome.calendar.gschema.xml.in
@@ -21,10 +21,5 @@
             <_summary>Type of the active view</_summary>
             <_description>Type of the active window view, default value is: monthly view</_description>
         </key>
-        <key name="disabled-sources" type="as">
-            <default>[]</default>
-            <_summary>List of the disabled sources</_summary>
-            <_description>Sources disabled last time Calendar ran</_description>
-        </key>
     </schema>
 </schemalist>
diff --git a/src/gcal-manager.c b/src/gcal-manager.c
index 0b570f4..f63235b 100644
--- a/src/gcal-manager.c
+++ b/src/gcal-manager.c
@@ -44,8 +44,6 @@ typedef struct
 typedef struct
 {
   ECalClient     *client;
-
-  gboolean        enabled;
   gboolean        connected;
 } GcalManagerUnit;
 
@@ -84,7 +82,6 @@ struct _GcalManager
 
   /* state flags */
   gboolean         goa_client_ready;
-  gchar          **disabled_sources;
   gint             sources_at_launch;
 
   /* timezone */
@@ -299,7 +296,7 @@ on_client_readonly_changed (EClient    *client,
   source = e_client_get_source (client);
 
   unit = g_hash_table_lookup (manager->clients, source);
-  if (unit && unit->enabled)
+  if (unit && is_source_enabled (source))
     source_changed (manager, source);
 
   GCAL_EXIT;
@@ -340,11 +337,13 @@ on_client_connected (GObject      *source_object,
   ECalClient *client;
   ESource *source;
   GError *error;
+  gboolean enabled;
 
   GCAL_ENTRY;
 
   manager = GCAL_MANAGER (user_data);
   source = e_client_get_source (E_CLIENT (source_object));
+  enabled = is_source_enabled (source);
 
   manager->sources_at_launch--;
 
@@ -380,14 +379,8 @@ on_client_connected (GObject      *source_object,
   /* notify the readonly property */
   g_signal_connect (client, "notify::readonly", G_CALLBACK (on_client_readonly_changed), user_data);
 
-  if (g_strv_contains ((const gchar * const *) manager->disabled_sources, e_source_get_uid (source)))
+  if (enabled)
     {
-      unit->enabled = FALSE;
-    }
-  else
-    {
-      unit->enabled = TRUE;
-
       e_cal_data_model_add_client (manager->e_data_model, client);
       e_cal_data_model_add_client (manager->search_data_model, client);
       if (manager->shell_search_data_model != NULL)
@@ -395,10 +388,8 @@ on_client_connected (GObject      *source_object,
     }
 
   /* refresh client when it's added */
-  if (unit->enabled && e_client_check_refresh_supported (E_CLIENT (client)))
-  {
+  if (enabled && e_client_check_refresh_supported (E_CLIENT (client)))
     e_client_refresh (E_CLIENT (client), NULL, on_client_refreshed, user_data);
-  }
 
   /* Cache all the online calendars, so the user can see them offline */
   offline_extension = e_source_get_extension (source, E_SOURCE_EXTENSION_OFFLINE);
@@ -410,7 +401,7 @@ on_client_connected (GObject      *source_object,
                                    NULL,
                                    NULL);
 
-  g_signal_emit (GCAL_MANAGER (user_data), signals[SOURCE_ADDED], 0, source, unit->enabled);
+  g_signal_emit (GCAL_MANAGER (user_data), signals[SOURCE_ADDED], 0, source, enabled);
 
   g_clear_object (&client);
 
@@ -776,8 +767,6 @@ gcal_manager_constructed (GObject *object)
   G_OBJECT_CLASS (gcal_manager_parent_class)->constructed (object);
 
   manager = GCAL_MANAGER (object);
-
-  manager->disabled_sources = g_settings_get_strv (manager->settings, "disabled-sources");
   manager->system_timezone = e_cal_util_get_system_timezone ();
 
   manager->clients = g_hash_table_new_full ((GHashFunc) e_source_hash, (GEqualFunc) e_source_equal,
@@ -905,8 +894,6 @@ gcal_manager_finalize (GObject *object)
   g_clear_object (&manager->search_data_model);
   g_clear_object (&manager->shell_search_data_model);
 
-  g_strfreev (manager->disabled_sources);
-
   if (manager->search_view_data != NULL)
     {
       g_free (manager->search_view_data->query);
@@ -1143,7 +1130,7 @@ gcal_manager_get_sources (GcalManager *manager)
     {
       GcalManagerUnit *unit = value;
 
-      if (!unit->enabled)
+      if (!is_source_enabled (key))
         continue;
 
       aux = g_list_append (aux, key);
@@ -1235,7 +1222,7 @@ gcal_manager_set_shell_search_query (GcalManager *manager,
 
   manager->search_view_data->passed_start = FALSE;
   manager->search_view_data->search_done = FALSE;
-  manager->search_view_data->sources_left = g_hash_table_size (manager->clients) - g_strv_length 
(manager->disabled_sources);
+  manager->search_view_data->sources_left = g_hash_table_size (manager->clients);
 
   if (manager->search_view_data->query != NULL)
     g_free (manager->search_view_data->query);
@@ -1435,48 +1422,32 @@ void
 gcal_manager_enable_source (GcalManager *manager,
                             ESource     *source)
 {
+  ESourceSelectable *selectable;
   GcalManagerUnit *unit;
-  gchar **new_disabled_sources;
-  guint i;
 
   GCAL_ENTRY;
 
   unit = g_hash_table_lookup (manager->clients, source);
+  selectable = e_source_get_extension (source, E_SOURCE_EXTENSION_CALENDAR);
 
-  if (unit->enabled)
+  if (is_source_enabled (source))
     {
       g_debug ("Source '%s' already enabled", e_source_get_uid (source));
       GCAL_EXIT;
       return;
     }
 
-  unit->enabled = TRUE;
   e_cal_data_model_add_client (manager->e_data_model, unit->client);
   e_cal_data_model_add_client (manager->search_data_model, unit->client);
-  if (manager->shell_search_data_model != NULL)
-    e_cal_data_model_add_client (manager->shell_search_data_model, unit->client);
 
-  /* remove source's uid from disabled_sources array */
-  new_disabled_sources = g_new0 (gchar*, g_strv_length (manager->disabled_sources));
-
-  for (i = 0; i < g_strv_length (manager->disabled_sources); i++)
-    {
-      if (g_strcmp0 (manager->disabled_sources[i], e_source_get_uid (source)) == 0)
-        continue;
-
-      new_disabled_sources[i] = g_strdup (manager->disabled_sources[i]);
-    }
-
-  g_strfreev (manager->disabled_sources);
-
-  manager->disabled_sources = new_disabled_sources;
+  if (manager->shell_search_data_model)
+    e_cal_data_model_add_client (manager->shell_search_data_model, unit->client);
 
   g_signal_emit (manager, signals[SOURCE_ENABLED], 0, source, TRUE);
 
-  /* sync settings value */
-  g_settings_set_strv (manager->settings,
-                       "disabled-sources",
-                       (const gchar * const *) manager->disabled_sources);
+  /* Save the source */
+  e_source_selectable_set_selected (selectable, TRUE);
+  gcal_manager_save_source (manager, source);
 
   GCAL_EXIT;
 }
@@ -1492,16 +1463,16 @@ void
 gcal_manager_disable_source (GcalManager *manager,
                              ESource     *source)
 {
+  ESourceSelectable *selectable;
   GcalManagerUnit *unit;
-  gchar **new_disabled_sources;
-  guint i;
   const gchar *source_uid;
 
   GCAL_ENTRY;
 
   unit = g_hash_table_lookup (manager->clients, source);
+  selectable = e_source_get_extension (source, E_SOURCE_EXTENSION_CALENDAR);
 
-  if (!unit->enabled)
+  if (!is_source_enabled (source))
     {
       g_debug ("Source '%s' already disabled", e_source_get_uid (source));
       GCAL_EXIT;
@@ -1510,29 +1481,17 @@ gcal_manager_disable_source (GcalManager *manager,
 
   source_uid = e_source_get_uid (source);
 
-  unit->enabled = FALSE;
-
   e_cal_data_model_remove_client (manager->e_data_model, source_uid);
   e_cal_data_model_remove_client (manager->search_data_model, source_uid);
 
   if (manager->shell_search_data_model != NULL)
     e_cal_data_model_remove_client (manager->shell_search_data_model, source_uid);
 
-  /* add source's uid from disabled_sources array */
-  new_disabled_sources = g_new0 (gchar*, g_strv_length (manager->disabled_sources) + 2);
-
-  for (i = 0; i < g_strv_length (manager->disabled_sources); i++)
-    new_disabled_sources[i] = g_strdup (manager->disabled_sources[i]);
-
-  new_disabled_sources[g_strv_length (manager->disabled_sources)] = g_strdup (source_uid);
-
-  g_strfreev (manager->disabled_sources);
-  manager->disabled_sources = new_disabled_sources;
-
   g_signal_emit (manager, signals[SOURCE_ENABLED], 0, source, FALSE);
 
-  /* sync settings value */
-  g_settings_set_strv (manager->settings, "disabled-sources", (const gchar * const *) 
manager->disabled_sources);
+  /* Save the source */
+  e_source_selectable_set_selected (selectable, FALSE);
+  gcal_manager_save_source (manager, source);
 
   GCAL_EXIT;
 }
@@ -1564,22 +1523,6 @@ gcal_manager_save_source (GcalManager *manager,
   GCAL_EXIT;
 }
 
-gboolean
-gcal_manager_source_enabled (GcalManager *manager,
-                             ESource     *source)
-{
-  GcalManagerUnit *unit;
-
-  GCAL_ENTRY;
-
-  unit = g_hash_table_lookup (manager->clients, source);
-
-  if (!unit)
-    GCAL_RETURN (FALSE);
-
-  GCAL_RETURN (unit->enabled);
-}
-
 void
 gcal_manager_refresh (GcalManager *manager)
 {
diff --git a/src/gcal-manager.h b/src/gcal-manager.h
index a368546..5633ef3 100644
--- a/src/gcal-manager.h
+++ b/src/gcal-manager.h
@@ -97,9 +97,6 @@ void                 gcal_manager_disable_source                 (GcalManager
 void                 gcal_manager_save_source                    (GcalManager        *manager,
                                                                   ESource            *source);
 
-gboolean             gcal_manager_source_enabled                 (GcalManager        *manager,
-                                                                  ESource            *source);
-
 GList*               gcal_manager_get_events                     (GcalManager        *manager,
                                                                   icaltimetype       *range_start,
                                                                   icaltimetype       *range_end);
diff --git a/src/gcal-quick-add-popover.c b/src/gcal-quick-add-popover.c
index 479420c..4129b50 100644
--- a/src/gcal-quick-add-popover.c
+++ b/src/gcal-quick-add-popover.c
@@ -364,7 +364,7 @@ on_source_changed (GcalManager         *manager,
     {
       on_source_added (self->manager,
                        source,
-                       gcal_manager_source_enabled (self->manager, source),
+                       is_source_enabled (source),
                        self);
 
       row = get_row_for_source (self, source);
diff --git a/src/gcal-source-dialog.c b/src/gcal-source-dialog.c
index b92f098..f89b25b 100644
--- a/src/gcal-source-dialog.c
+++ b/src/gcal-source-dialog.c
@@ -942,7 +942,7 @@ stack_visible_child_name_changed (GObject    *object,
 
       // enabled check
       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->calendar_visible_check),
-                                    gcal_manager_source_enabled (self->manager, self->source));
+                                    is_source_enabled (self->source));
 
       /* default source check button */
       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->default_check), (self->source == 
default_source));
@@ -1684,8 +1684,10 @@ notification_child_revealed_changed (GtkWidget  *notification,
         {
           g_warning ("[source-dialog] Error removing source: %s", error->message);
 
-          add_source (self->manager, self->removed_source,
-                      gcal_manager_source_enabled (self->manager, self->removed_source), user_data);
+          add_source (self->manager,
+                      self->removed_source,
+                      is_source_enabled (self->removed_source),
+                      user_data);
 
           gcal_manager_enable_source (self->manager, self->removed_source);
 
@@ -1713,8 +1715,10 @@ undo_remove_action (GtkButton *button,
       // Enable the source before adding it again
       gcal_manager_enable_source (self->manager, self->removed_source);
 
-      add_source (self->manager, self->removed_source,
-                  gcal_manager_source_enabled (self->manager, self->removed_source), user_data);
+      add_source (self->manager,
+                  self->removed_source,
+                  is_source_enabled (self->removed_source),
+                  user_data);
 
       /*
        * Don't clear the pointer, since we don't
@@ -2200,7 +2204,7 @@ gcal_source_dialog_set_manager (GcalSourceDialog *dialog,
       sources = gcal_manager_get_sources_connected (dialog->manager);
 
       for (l = sources; l != NULL; l = l->next)
-        add_source (dialog->manager, l->data, gcal_manager_source_enabled (dialog->manager, l->data), 
dialog);
+        add_source (dialog->manager, l->data, is_source_enabled (l->data), dialog);
     }
   else
     {
diff --git a/src/gcal-utils.c b/src/gcal-utils.c
index ddec389..5a9dfb3 100644
--- a/src/gcal-utils.c
+++ b/src/gcal-utils.c
@@ -924,3 +924,15 @@ should_change_date_for_scroll (gdouble        *scroll_value,
 
   return FALSE;
 }
+
+gboolean
+is_source_enabled (ESource *source)
+{
+  ESourceSelectable *selectable;
+
+  g_return_val_if_fail (E_IS_SOURCE (source), FALSE);
+
+  selectable = e_source_get_extension (source, E_SOURCE_EXTENSION_CALENDAR);
+
+  return e_source_selectable_get_selected (selectable);
+}
diff --git a/src/gcal-utils.h b/src/gcal-utils.h
index c39f355..2f65a8b 100644
--- a/src/gcal-utils.h
+++ b/src/gcal-utils.h
@@ -138,4 +138,6 @@ gint            get_alarm_trigger_minutes                       (GcalEvent
 gboolean        should_change_date_for_scroll                   (gdouble               *scroll_value,
                                                                  GdkEventScroll        *scroll_event);
 
+gboolean        is_source_enabled                               (ESource               *source);
+
 #endif // __GCAL_UTILS_H__
diff --git a/src/gcal-window.c b/src/gcal-window.c
index a0d841c..fd44536 100644
--- a/src/gcal-window.c
+++ b/src/gcal-window.c
@@ -917,7 +917,7 @@ make_row_from_source (GcalWindow *window,
 
   /* checkbox */
   checkbox = gtk_check_button_new ();
-  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (checkbox), gcal_manager_source_enabled (window->manager, 
source));
+  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (checkbox), is_source_enabled (source));
   g_signal_connect (checkbox, "notify::active", G_CALLBACK (on_calendar_toggled), window);
 
   gtk_container_add (GTK_CONTAINER (box), icon);
@@ -1021,7 +1021,7 @@ source_changed (GcalWindow *window,
       if (child_source != NULL && child_source == source)
         {
           gtk_widget_destroy (aux->data);
-          add_source (window->manager, source, gcal_manager_source_enabled (window->manager, source), 
window);
+          add_source (window->manager, source, is_source_enabled (source), window);
           break;
         }
     }
@@ -1507,9 +1507,11 @@ gcal_window_constructed (GObject *object)
   if (!gcal_manager_get_loading (window->manager))
     {
       GList *sources, *l;
+
       sources = gcal_manager_get_sources_connected (window->manager);
+
       for (l = sources; l != NULL; l = g_list_next (l))
-        add_source (window->manager, l->data, gcal_manager_source_enabled (window->manager, l->data), 
object);
+        add_source (window->manager, l->data, is_source_enabled (l->data), object);
 
       g_list_free (sources);
     }


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