[folks: 3/4] Move Backend preparation to BackendStore. Fixes bgo#628970.



commit c6e5052b366db020357696f90176c814c82f70b7
Author: Travis Reitter <travis reitter collabora co uk>
Date:   Thu Sep 9 16:15:49 2010 -0700

    Move Backend preparation to BackendStore. Fixes bgo#628970.
    
    An effect of this is that all backends will be prepared by the time
    IndividualAggregator.prepare() completes.

 folks/backend-store.vala         |   38 +++++++++++++++++++++++++---------
 folks/individual-aggregator.vala |   42 ++++++++++++++++++++++---------------
 2 files changed, 53 insertions(+), 27 deletions(-)
---
diff --git a/folks/backend-store.vala b/folks/backend-store.vala
index 4035dbb..ee2dda8 100644
--- a/folks/backend-store.vala
+++ b/folks/backend-store.vala
@@ -38,6 +38,7 @@ public class Folks.BackendStore : Object {
   private delegate void ModuleFinalizeFunc (BackendStore store);
 
   private HashMap<string,Backend> backend_hash;
+  private HashMap<string,Backend> _prepared_backends;
   private GLib.List<ModuleFinalizeFunc> finalize_funcs = null;
   private static weak BackendStore instance;
 
@@ -61,8 +62,7 @@ public class Folks.BackendStore : Object {
    * This list will be empty before { link BackendStore.load_backends} has been
    * called.
    *
-   * The backends in this list have not necessarily been prepared (see { link
-   * Backend.prepare}).
+   * The backends in this list have been prepared and are ready to use.
    *
    * @since 0.2.0
    */
@@ -71,11 +71,8 @@ public class Folks.BackendStore : Object {
       owned get
         {
           var backends = new GLib.List<Backend> ();
-          foreach (var entry in this.backend_hash)
-            {
-              backends.prepend (entry.value);
-
-            }
+          foreach (var entry in this._prepared_backends)
+            backends.prepend (entry.value);
 
           return backends;
         }
@@ -103,6 +100,8 @@ public class Folks.BackendStore : Object {
   private BackendStore ()
     {
       this.backend_hash = new HashMap<string,Backend> (str_hash, str_equal);
+      this._prepared_backends = new HashMap<string,Backend> (str_hash,
+          str_equal);
     }
 
   ~BackendStore ()
@@ -116,7 +115,7 @@ public class Folks.BackendStore : Object {
     }
 
   /**
-   * Find and load all available backends.
+   * Find, load, and prepare all backends which are not disabled.
    *
    * Backends will be searched for in the path given by the `FOLKS_BACKEND_DIR`
    * environment variable, if it's set. If it's not set, backends will be
@@ -143,6 +142,27 @@ public class Folks.BackendStore : Object {
       assert (dir != null && yield is_dir (dir));
 
       yield this.load_modules_from_dir (dir);
+
+      /* all the found modules should call add_backend() for their backends
+       * (adding them to the backend hash) as a consequence of
+       * load_modules_from_dir(). */
+
+      foreach (var backend in this.backend_hash.values)
+        {
+          try
+            {
+              yield backend.prepare ();
+
+              debug ("New backend '%s' prepared", backend.name);
+              this._prepared_backends.set (backend.name, backend);
+              this.backend_available (backend);
+            }
+          catch (GLib.Error e)
+            {
+              warning ("Error preparing Backend '%s': %s", backend.name,
+                  e.message);
+            }
+        }
   }
 
   /**
@@ -152,9 +172,7 @@ public class Folks.BackendStore : Object {
    */
   public void add_backend (Backend backend)
     {
-      debug ("New backend '%s' available", backend.name);
       this.backend_hash.set (backend.name, backend);
-      this.backend_available (backend);
     }
 
   /**
diff --git a/folks/individual-aggregator.vala b/folks/individual-aggregator.vala
index 7b658ea..beaf2be 100644
--- a/folks/individual-aggregator.vala
+++ b/folks/individual-aggregator.vala
@@ -138,28 +138,36 @@ public class Folks.IndividualAggregator : Object
    */
   public async void prepare () throws GLib.Error
     {
-      this.backend_store.load_backends ();
+      /* Once this async function returns, all the { link Backend}s will have
+       * been prepared (though no { link PersonaStore}s are guaranteed to be
+       * available yet). This last guarantee is new as of version 0.2.0. */
+      yield this.backend_store.load_backends ();
+    }
+
+  private async void add_backend (Backend backend)
+    {
+      if (!this.backends.contains (backend))
+        {
+          this.backends.add (backend);
+
+          backend.persona_store_added.connect (
+              this.backend_persona_store_added_cb);
+          backend.persona_store_removed.connect (
+              this.backend_persona_store_removed_cb);
+
+          /* handle the stores that have already been signaled */
+          backend.persona_stores.foreach ((k, v) =>
+              {
+                this.backend_persona_store_added_cb (backend,
+                  (PersonaStore) v);
+              });
+        }
     }
 
   private void backend_available_cb (BackendStore backend_store,
       Backend backend)
     {
-      backend.persona_store_added.connect (this.backend_persona_store_added_cb);
-      backend.persona_store_removed.connect (
-          this.backend_persona_store_removed_cb);
-
-      backend.prepare.begin ((obj, result) =>
-        {
-          try
-            {
-              backend.prepare.end (result);
-            }
-          catch (GLib.Error e)
-            {
-              warning ("Error preparing Backend '%s': %s", backend.name,
-                  e.message);
-            }
-        });
+      this.add_backend.begin (backend);
     }
 
   private void backend_persona_store_added_cb (Backend backend,



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