[folks] Key-file: implement LocalIdDetails



commit 5c039e613290d6131360b6627641169ee1474a8e
Author: Julian Sparber <julian sparber net>
Date:   Fri Oct 18 19:05:26 2019 +0200

    Key-file: implement LocalIdDetails
    
    The local ids can be used to link personas which don't have any common
    linking properties. This makes it possible to use the key file store to
    store linking personas.

 backends/key-file/kf-persona-store.vala | 12 +++++
 backends/key-file/kf-persona.vala       | 84 +++++++++++++++++++++++++++++++++
 2 files changed, 96 insertions(+)
---
diff --git a/backends/key-file/kf-persona-store.vala b/backends/key-file/kf-persona-store.vala
index c6292475..8592cf6a 100644
--- a/backends/key-file/kf-persona-store.vala
+++ b/backends/key-file/kf-persona-store.vala
@@ -45,6 +45,7 @@ public class Folks.Backends.Kf.PersonaStore : Folks.PersonaStore
       "alias",
       "im-addresses",
       "web-service-addresses",
+      "local-ids",
       "anti-links",
       null /* FIXME: https://bugzilla.gnome.org/show_bug.cgi?id=682698 */
     };
@@ -386,6 +387,13 @@ public class Folks.Backends.Kf.PersonaStore : Folks.PersonaStore
           ? (MultiMap<string, WebServiceFieldDetails>) val2.get_object ()
           : null;
 
+      unowned Value? val3 = details.lookup
+          (Folks.PersonaStore.detail_key (PersonaDetail.LOCAL_IDS));
+      Set<string> local_ids
+        = val3 != null
+        ? (Set<string>) val3.get_object ()
+        : null;
+
       debug ("Adding Persona from details.");
 
       /* Generate a new random number for the persona's ID, so as to try and
@@ -407,6 +415,10 @@ public class Folks.Backends.Kf.PersonaStore : Folks.PersonaStore
 
       try
         {
+          if (local_ids != null)
+            {
+              yield persona.change_local_ids (local_ids);
+            }
           if (im_addresses != null)
             {
               yield persona.change_im_addresses (im_addresses);
diff --git a/backends/key-file/kf-persona.vala b/backends/key-file/kf-persona.vala
index c6356bbb..9642c5c0 100644
--- a/backends/key-file/kf-persona.vala
+++ b/backends/key-file/kf-persona.vala
@@ -32,6 +32,7 @@ public class Folks.Backends.Kf.Persona : Folks.Persona,
     AliasDetails,
     AntiLinkable,
     ImDetails,
+    LocalIdDetails,
     WebServiceDetails
 {
   private HashMultiMap<string, ImFieldDetails> _im_addresses;
@@ -41,6 +42,7 @@ public class Folks.Backends.Kf.Persona : Folks.Persona,
     {
       "im-addresses",
       "web-service-addresses",
+      "local-ids",
       null /* FIXME: https://bugzilla.gnome.org/show_bug.cgi?id=682698 */
     };
   private const string[] _writeable_properties =
@@ -303,6 +305,59 @@ public class Folks.Backends.Kf.Persona : Folks.Persona,
       this.notify_property ("anti-links");
     }
 
+  private SmallSet<string> _local_ids;
+  private Set<string> _local_ids_ro;
+
+  /**
+   * {@inheritDoc}
+   *
+   * @since 0.14.0
+   */
+
+  [CCode (notify = false)]
+  public Set<string> local_ids
+    {
+      get
+        {
+          if (this._local_ids.contains (this.iid) == false)
+            {
+              this._local_ids.add (this.iid);
+            }
+          return this._local_ids_ro;
+        }
+      set { this.change_local_ids.begin (value); }
+    }
+
+  /**
+   * {@inheritDoc}
+   *
+   * @since 0.14.0
+   */
+  public async void change_local_ids (Set<string> local_ids)
+      throws PropertyError
+    {
+      if (Folks.Internal.equal_sets<string> (local_ids, this._local_ids))
+        {
+          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,
+          "__local-ids", local_ids.to_array ());
+
+      /* Get the PersonaStore to save the key file */
+      yield ((Kf.PersonaStore) this.store).save_key_file ();
+
+      /* Update the stored local_ids. */
+      this._local_ids.clear ();
+      this._local_ids.add_all (local_ids);
+      this.notify_property ("local-ids");
+    }
+
   /**
    * Create a new persona.
    *
@@ -335,6 +390,8 @@ public class Folks.Backends.Kf.Persona : Folks.Persona,
           AbstractFieldDetails<string>.equal_static);
       this._anti_links = new SmallSet<string> ();
       this._anti_links_ro = this._anti_links.read_only_view;
+      this._local_ids = new SmallSet<string> ();
+      this._local_ids_ro = this._local_ids.read_only_view;
 
       /* Load the IM addresses from the key file */
       unowned KeyFile key_file = ((Kf.PersonaStore) this.store).get_key_file ();
@@ -376,6 +433,25 @@ public class Folks.Backends.Kf.Persona : Folks.Persona,
                       continue;
                     }
                 }
+              /* Local-ids. */
+              if (key == "__local_ids")
+                {
+                  var local_ids_array =
+                      key_file.get_string_list (this.display_id, key);
+
+                  if (local_ids_array != null)
+                    {
+                      foreach (var local_id in local_ids_array)
+                        {
+                          this.local_ids.add (local_id);
+                        }
+
+                      debug ("    Loaded %u local_ids.",
+                          local_ids_array.length);
+                      continue;
+                    }
+                }
+
 
               /* Web service addresses */
               var decomposed_key = key.split(".", 2);
@@ -447,6 +523,14 @@ public class Folks.Backends.Kf.Persona : Folks.Persona,
           while (iter.next ())
             callback (iter.get_key () + ":" + iter.get_value ().value);
         }
+      else if (prop_name == "local-ids")
+        {
+          if (this._local_ids != null)
+          foreach (var id in this._local_ids)
+            {
+              callback (id);
+            }
+        }
       else if (prop_name == "web-service-addresses")
         {
           var iter = this.web_service_addresses.map_iterator ();


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