key-file: Add anti-linking support
authorPhilip Withnall <philip@tecnocode.co.uk>
Sun, 6 May 2012 21:59:45 +0000 (22:59 +0100)
committerPhilip Withnall <philip@tecnocode.co.uk>
Sat, 7 Jul 2012 16:06:54 +0000 (17:06 +0100)
Helps: https://bugzilla.gnome.org/show_bug.cgi?id=629537

backends/key-file/kf-persona-store.vala
backends/key-file/kf-persona.vala

index 2e3c268..a5905a0 100644 (file)
@@ -44,9 +44,12 @@ public class Folks.Backends.Kf.PersonaStore : Folks.PersonaStore
     {
       "alias",
       "im-addresses",
-      "web-service-addresses"
+      "web-service-addresses",
+      "anti-links"
     };
 
+  internal static const string anti_links_key_name = "__anti-links";
+
   /**
    * {@inheritDoc}
    */
index b87aa45..05b471d 100644 (file)
@@ -30,6 +30,7 @@ using Folks.Backends.Kf;
  */
 public class Folks.Backends.Kf.Persona : Folks.Persona,
     AliasDetails,
+    AntiLinkable,
     ImDetails,
     WebServiceDetails
 {
@@ -45,7 +46,8 @@ public class Folks.Backends.Kf.Persona : Folks.Persona,
     {
       "alias",
       "im-addresses",
-      "web-service-addresses"
+      "web-service-addresses",
+      "anti-links"
     };
 
   /**
@@ -256,6 +258,51 @@ public class Folks.Backends.Kf.Persona : Folks.Persona,
       this.notify_property ("web-service-addresses");
     }
 
+  private HashSet<string> _anti_links;
+  private Set<string> _anti_links_ro;
+
+  /**
+   * {@inheritDoc}
+   *
+   * @since UNRELEASED
+   */
+  [CCode (notify = false)]
+  public Set<string> anti_links
+    {
+      get { return this._anti_links_ro; }
+      set { this.change_anti_links.begin (value); }
+    }
+
+  /**
+   * {@inheritDoc}
+   *
+   * @since UNRELEASED
+   */
+  public async void change_anti_links (Set<string> anti_links)
+      throws PropertyError
+    {
+      if (Folks.Internal.equal_sets<string> (anti_links, this.anti_links))
+        {
+          return;
+        }
+
+      unowned KeyFile key_file = ((Kf.PersonaStore) this.store).get_key_file ();
+
+      /* Skip the persona's UID; don't allow reflexive anti-links. */
+      anti_links.remove (this.uid);
+
+      key_file.set_string_list (this.display_id,
+          Kf.PersonaStore.anti_links_key_name, anti_links.to_array ());
+
+      /* Get the PersonaStore to save the key file */
+      yield ((Kf.PersonaStore) this.store).save_key_file ();
+
+      /* Update the stored anti-links. */
+      this._anti_links.clear ();
+      this._anti_links.add_all (anti_links);
+      this.notify_property ("anti-links");
+    }
+
   /**
    * Create a new persona.
    *
@@ -287,6 +334,8 @@ public class Folks.Backends.Kf.Persona : Folks.Persona,
             null, null,
              AbstractFieldDetails<string>.hash_static,
              AbstractFieldDetails<string>.equal_static);
+      this._anti_links = new HashSet<string> ();
+      this._anti_links_ro = this._anti_links.read_only_view;
 
       /* Load the IM addresses from the key file */
       unowned KeyFile key_file = ((Kf.PersonaStore) this.store).get_key_file ();
@@ -310,6 +359,25 @@ public class Folks.Backends.Kf.Persona : Folks.Persona,
                   continue;
                 }
 
+              /* Anti-links. */
+              if (key == Kf.PersonaStore.anti_links_key_name)
+                {
+                  var anti_link_array =
+                      key_file.get_string_list (this.display_id, key);
+
+                  if (anti_link_array != null)
+                    {
+                      foreach (var anti_link in anti_link_array)
+                        {
+                          this._anti_links.add (anti_link);
+                        }
+
+                      debug ("    Loaded %u anti-links.",
+                          anti_link_array.length);
+                      continue;
+                    }
+                }
+
               /* Web service addresses */
               var decomposed_key = key.split(".", 2);
               if (decomposed_key.length == 2 &&