[folks] telepathy: Make group changes truly asynchronous and report errors properly



commit 46ca881a18febf9848ea96379cfd3ee004c6ab6f
Author: Philip Withnall <philip tecnocode co uk>
Date:   Mon Mar 26 15:07:31 2012 +0100

    telepathy: Make group changes truly asynchronous and report errors properly
    
    Take advantage of GroupDetails.change_group() being async, and wait for the
    underlying Telepathy operation to complete before returning from it. This
    allows us to propagate errors properly, rather than just printing
    them as warnings on the terminal.
    
    This also includes changes to not notify of changes to Tpf.Persona.groups
    until Telepathy has notified us of the change. This should prevent groups
    changing in the UI if the underlying operation has actually failed.
    
    Helps: https://bugzilla.gnome.org/show_bug.cgi?id=671662

 backends/telepathy/lib/tpf-persona-store.vala |   24 -------------
 backends/telepathy/lib/tpf-persona.vala       |   45 +++++++++++++++++++-----
 2 files changed, 35 insertions(+), 34 deletions(-)
---
diff --git a/backends/telepathy/lib/tpf-persona-store.vala b/backends/telepathy/lib/tpf-persona-store.vala
index 7ed14cf..2263e07 100644
--- a/backends/telepathy/lib/tpf-persona-store.vala
+++ b/backends/telepathy/lib/tpf-persona-store.vala
@@ -1055,30 +1055,6 @@ public class Tpf.PersonaStore : Folks.PersonaStore
       this._emit_personas_changed (personas_added, personas_removed);
     }
 
-  internal async void _change_group_membership (Folks.Persona persona,
-      string group, bool is_member) throws Folks.PropertyError
-    {
-      var tp_persona = (Tpf.Persona) persona;
-
-      try
-        {
-          if (is_member)
-            {
-              yield tp_persona.contact.add_to_group_async (group);
-            }
-          else
-            {
-              yield tp_persona.contact.remove_from_group_async (group);
-            }
-        }
-      catch (GLib.Error e)
-        {
-          /* Translators: the parameter is an error message. */
-          throw new PropertyError.UNKNOWN_ERROR (
-              _("Failed to change group membership: %s"), e.message);
-        }
-    }
-
   /**
    * Remove a { link Persona} from the PersonaStore.
    *
diff --git a/backends/telepathy/lib/tpf-persona.vala b/backends/telepathy/lib/tpf-persona.vala
index 8f2ac4c..eae6551 100644
--- a/backends/telepathy/lib/tpf-persona.vala
+++ b/backends/telepathy/lib/tpf-persona.vala
@@ -416,15 +416,39 @@ public class Tpf.Persona : Folks.Persona,
    * See { link Folks.GroupDetails.change_group}.
    */
   public async void change_group (string group, bool is_member)
-      throws Folks.PropertyError
+      throws GLib.Error
     {
-      if (this._change_group (group, is_member))
+      if (this._groups.contains (group) != is_member)
         {
-          Tpf.PersonaStore store = (Tpf.PersonaStore) this.store;
-          yield store._change_group_membership (this, group, is_member);
+          yield this._change_group_membership (group, is_member);
         }
+
+      /* The change will be notified when we receive changes from the store. */
     }
 
+  private async void _change_group_membership (string group,
+      bool is_member) throws Folks.PropertyError
+    {
+      try
+        {
+          if (is_member)
+            {
+              yield this.contact.add_to_group_async (group);
+            }
+          else
+            {
+              yield this.contact.remove_from_group_async (group);
+            }
+        }
+      catch (GLib.Error e)
+        {
+          /* Translators: the parameter is an error message. */
+          throw new PropertyError.UNKNOWN_ERROR (
+              _("Failed to change group membership: %s"), e.message);
+        }
+    }
+
+  /* Note: Only ever called as a result of signals from Telepathy. */
   private bool _change_group (string group, bool is_member)
     {
       var changed = false;
@@ -439,7 +463,10 @@ public class Tpf.Persona : Folks.Persona,
         }
 
       if (changed == true)
-        this.group_changed (group, is_member);
+        {
+          this.group_changed (group, is_member);
+          this.notify_property ("groups");
+        }
 
       return changed;
     }
@@ -464,21 +491,19 @@ public class Tpf.Persona : Folks.Persona,
    */
   public async void change_groups (Set<string> groups) throws PropertyError
     {
-      Tpf.PersonaStore store = (Tpf.PersonaStore) this.store;
-
       foreach (var group1 in groups)
         {
           if (this._groups.contains (group1) == false)
-            yield store._change_group_membership (this, group1, true);
+            yield this._change_group_membership (group1, true);
         }
 
       foreach (var group2 in this._groups)
         {
           if (groups.contains (group2) == false)
-            yield store._change_group_membership (this, group2, false);
+            yield this._change_group_membership (group2, false);
         }
 
-      this.notify_property ("groups");
+      /* The change will be notified when we receive changes from the store. */
     }
 
   /* This has to be weak since, in general, we can't force any TpContacts to



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