[folks] Change PhoneDetails.phone_numbers to be a Set<FieldDetails>



commit 6510a5cee80afafeefd507387455c6ebcb66a880
Author: Philip Withnall <philip withnall collabora co uk>
Date:   Tue Apr 19 22:13:01 2011 +0100

    Change PhoneDetails.phone_numbers to be a Set<FieldDetails>
    
    Helps: bgo#640092

 NEWS                                        |    1 +
 backends/tracker/lib/trf-persona-store.vala |  104 +++------------------------
 backends/tracker/lib/trf-persona.vala       |   23 ++----
 folks/individual.vala                       |   35 +++++-----
 folks/phone-details.vala                    |    5 +-
 folks/potential-match.vala                  |   10 +--
 tests/tracker/add-persona.vala              |   13 ++--
 tests/tracker/duplicated-phones.vala        |   18 ++---
 tests/tracker/match-phone-number.vala       |   16 ++--
 tests/tracker/phones-updates.vala           |    4 +-
 tests/tracker/set-phones.vala               |   10 ++--
 tools/inspect/utils.vala                    |    6 +-
 12 files changed, 74 insertions(+), 171 deletions(-)
---
diff --git a/NEWS b/NEWS
index b29363c..718c3a8 100644
--- a/NEWS
+++ b/NEWS
@@ -23,6 +23,7 @@ API changes:
 * FieldDetails.extend_parameters() now takes type MultiMap<string, string>
 * PostalAddressDetails.postal_addresses is now of type Set<PostalAddress>
 * EmailDetails.email_addresses is now of type Set<FieldDetails>
+* PhoneDetails.phone_numbers is now of type Set<FieldDetails>
 
 Overview of changes from libfolks 0.4.0 to libfolks 0.5.0
 =========================================================
diff --git a/backends/tracker/lib/trf-persona-store.vala b/backends/tracker/lib/trf-persona-store.vala
index 9daa6c3..86f9f10 100644
--- a/backends/tracker/lib/trf-persona-store.vala
+++ b/backends/tracker/lib/trf-persona-store.vala
@@ -539,15 +539,9 @@ public class Trf.PersonaStore : Folks.PersonaStore
           else if (k == Folks.PersonaStore.detail_key (
                 PersonaDetail.PHONE_NUMBERS))
             {
-              unowned GLib.List<FieldDetails> phone_numbers =
-                (GLib.List<FieldDetails>) v.get_pointer ();
-              var phone_numbers_l = phone_numbers.copy ();
-              foreach (var phone_fd_obj in phone_numbers_l)
-                {
-                  phone_fd_obj.ref ();
-                }
-              yield this._build_update_query (builder,
-                  (owned) phone_numbers_l,
+              Set<FieldDetails> phone_numbers =
+                (Set<FieldDetails>) v.get_object ();
+              yield this._build_update_query_set (builder, phone_numbers,
                 "_:p", Trf.Attrib.PHONES);
             }
           else if (k == Folks.PersonaStore.detail_key (PersonaDetail.ROLES))
@@ -846,9 +840,9 @@ public class Trf.PersonaStore : Folks.PersonaStore
       return urn;
     }
 
-  private async void _build_update_query (
+  private async void _build_update_query_set (
       Tracker.Sparql.Builder builder,
-      owned GLib.List<FieldDetails> properties,
+      Set<FieldDetails> properties,
       string contact_var,
       Trf.Attrib attrib)
     {
@@ -868,57 +862,6 @@ public class Trf.PersonaStore : Folks.PersonaStore
             obj_var = "_:phone%d";
             break;
           case Trf.Attrib.EMAILS:
-            assert_not_reached ();
-        }
-
-      int cnt = 0;
-      foreach (var p in properties)
-        {
-          var affl = affl_var.printf (cnt);
-          var obj = yield this._urn_from_property (
-              related_attrib, related_prop, p.value);
-
-          if (obj == "")
-            {
-              obj = obj_var.printf (cnt);
-              builder.subject (obj);
-              builder.predicate ("a");
-              builder.object (related_attrib);
-              builder.predicate (related_prop);
-              builder.object_string (p.value);
-            }
-
-          builder.subject (affl);
-          builder.predicate ("a");
-          builder.object (Trf.OntologyDefs.NCO_AFFILIATION);
-          builder.predicate (related_connection);
-          builder.object (obj);
-
-          builder.subject (contact_var);
-          builder.predicate (Trf.OntologyDefs.NCO_HAS_AFFILIATION);
-          builder.object (affl);
-
-          cnt++;
-        }
-    }
-
-  private async void _build_update_query_set (
-      Tracker.Sparql.Builder builder,
-      Set<FieldDetails> properties,
-      string contact_var,
-      Trf.Attrib attrib)
-    {
-      string? affl_var = null;
-      string? obj_var = null;
-      unowned string? related_attrib = null;
-      unowned string? related_prop = null;
-      unowned string? related_connection = null;
-
-      switch (attrib)
-        {
-          case Trf.Attrib.PHONES:
-            assert_not_reached ();
-          case Trf.Attrib.EMAILS:
             related_attrib = Trf.OntologyDefs.NCO_EMAIL;
             related_prop = Trf.OntologyDefs.NCO_EMAIL_PROP;
             related_connection = Trf.OntologyDefs.NCO_HAS_EMAIL;
@@ -1983,14 +1926,14 @@ public class Trf.PersonaStore : Folks.PersonaStore
     }
 
   internal async void _set_phones (Folks.Persona persona,
-      owned GLib.List<FieldDetails> phone_numbers)
+      Set<FieldDetails> phone_numbers)
     {
-      yield this._set_unique_attrib (persona, (owned) phone_numbers,
+      yield this._set_unique_attrib_set (persona, phone_numbers,
           Trf.Attrib.PHONES);
     }
 
-  internal async void _set_unique_attrib (Folks.Persona persona,
-      owned GLib.List<FieldDetails> properties, Trf.Attrib attrib)
+  internal async void _set_unique_attrib_set (Folks.Persona persona,
+      Set<FieldDetails> properties, Trf.Attrib attrib)
     {
       string? query_name = null;
       var p_id = ((Trf.Persona) persona).tracker_id ();
@@ -2003,37 +1946,10 @@ public class Trf.PersonaStore : Folks.PersonaStore
             query_name = "_set_phones";
             yield this._remove_attributes_from_persona (persona,
                 _REMOVE_PHONES);
-            yield this._build_update_query (builder, (owned) properties,
+            yield this._build_update_query_set (builder, properties,
                 "?contact", Trf.Attrib.PHONES);
             break;
           case Trf.Attrib.EMAILS:
-            assert_not_reached ();
-        }
-      builder.insert_close ();
-      builder.where_open ();
-      builder.subject ("?contact");
-      builder.predicate ("a");
-      builder.object (Trf.OntologyDefs.NCO_PERSON);
-      string filter = " FILTER(tracker:id(?contact) = %s) ".printf (p_id);
-      builder.append (filter);
-      builder.where_close ();
-
-      yield this._tracker_update (builder.result, query_name);
-    }
-
-  internal async void _set_unique_attrib_set (Folks.Persona persona,
-      Set<FieldDetails> properties, Trf.Attrib attrib)
-    {
-      string? query_name = null;
-      var p_id = ((Trf.Persona) persona).tracker_id ();
-      var builder = new Tracker.Sparql.Builder.update ();
-      builder.insert_open (null);
-
-      switch (attrib)
-        {
-          case Trf.Attrib.PHONES:
-            assert_not_reached ();
-          case Trf.Attrib.EMAILS:
             query_name = "_set_emailss";
             yield this._remove_attributes_from_persona (persona,
                 _REMOVE_EMAILS);
diff --git a/backends/tracker/lib/trf-persona.vala b/backends/tracker/lib/trf-persona.vala
index 3e69dba..6675552 100644
--- a/backends/tracker/lib/trf-persona.vala
+++ b/backends/tracker/lib/trf-persona.vala
@@ -50,7 +50,7 @@ public class Trf.Persona : Folks.Persona,
   private bool _is_favourite;
   private const string[] _linkable_properties =
       {"im-addresses", "local-ids", "web-service-addresses"};
-  private GLib.List<FieldDetails> _phone_numbers;
+  private HashSet<FieldDetails> _phone_numbers = new HashSet<FieldDetails> ();
   private HashSet<FieldDetails> _email_addresses = new HashSet<FieldDetails> ();
   private weak Sparql.Cursor _cursor;
   private string _tracker_id;
@@ -77,20 +77,12 @@ public class Trf.Persona : Folks.Persona,
   /**
    * { inheritDoc}
    */
-  public GLib.List<FieldDetails> phone_numbers
+  public Set<FieldDetails> phone_numbers
     {
       get { return this._phone_numbers; }
       public set
         {
-          var _temp = new GLib.List<FieldDetails> ();
-          foreach (unowned FieldDetails e in value)
-            {
-              _temp.prepend (e);
-            }
-          _temp.reverse ();
-
-          ((Trf.PersonaStore) this.store)._set_phones (this,
-              (owned) _temp);
+          ((Trf.PersonaStore) this.store)._set_phones (this, value);
         }
     }
 
@@ -919,7 +911,7 @@ public class Trf.Persona : Folks.Persona,
           return;
         }
 
-      var phones = new GLib.List<FieldDetails> ();
+      var phones = new HashSet<FieldDetails> ();
       string[] phones_a = phones_field.split ("\n");
 
       foreach (var p in phones_a)
@@ -930,12 +922,11 @@ public class Trf.Persona : Folks.Persona,
               var fd = new FieldDetails (p_info[Trf.PhoneFields.PHONE]);
               fd.set_parameter ("tracker_id",
                   p_info[Trf.PhoneFields.TRACKER_ID]);
-              phones.prepend ((owned) fd);
+              phones.add (fd);
             }
         }
 
-      phones.reverse ();
-      this._phone_numbers = (owned) phones;
+      this._phone_numbers = phones;
     }
 
   internal bool _add_phone (string phone, string tracker_id)
@@ -955,7 +946,7 @@ public class Trf.Persona : Folks.Persona,
         {
           var fd = new FieldDetails (phone);
           fd.set_parameter ("tracker_id", tracker_id);
-          this._phone_numbers.prepend ((owned) fd);
+          this._phone_numbers.add (fd);
           this.notify_property ("phone-numbers");
         }
 
diff --git a/folks/individual.vala b/folks/individual.vala
index 0a2f7b3..94bad5d 100644
--- a/folks/individual.vala
+++ b/folks/individual.vala
@@ -272,19 +272,19 @@ public class Folks.Individual : Object,
         }
     }
 
-  private GLib.List<FieldDetails> _phone_numbers;
+  private HashSet<FieldDetails> _phone_numbers;
+
   /**
    * { inheritDoc}
    */
-  public GLib.List<FieldDetails> phone_numbers
+  public Set<FieldDetails> phone_numbers
     {
       get { return this._phone_numbers; }
       private set
         {
-          this._phone_numbers = new GLib.List<FieldDetails> ();
-          foreach (unowned FieldDetails fd in value)
-            this._phone_numbers.prepend (fd);
-          this._phone_numbers.reverse ();
+          this._phone_numbers = new HashSet<FieldDetails> ();
+          foreach (var fd in value)
+            this._phone_numbers.add (fd);
         }
     }
 
@@ -1172,41 +1172,40 @@ public class Folks.Individual : Object,
 
   private void _update_phone_numbers ()
     {
-      /* Populate the phone numberss as the union of our Personas' numbers
-       * If the same number exist multiple times we merge the parameters. */
+      /* Populate the phone numbers as the union of our Personas' numbers
+       * If the same number exists multiple times we merge the parameters. */
       /* FIXME: We should handle phone numbers better, just string comparison
          doesn't work. */
       var phone_numbers_set =
-          new HashTable<unowned string, unowned FieldDetails> (
-              str_hash, str_equal);
-      var phones = new GLib.List<FieldDetails> ();
+          new HashMap<unowned string, unowned FieldDetails> ();
+      var phones = new HashSet<FieldDetails> ();
 
       foreach (var persona in this._persona_list)
         {
           var phone_details = persona as PhoneDetails;
           if (phone_details != null)
             {
-              foreach (unowned FieldDetails fd in phone_details.phone_numbers)
+              foreach (var fd in phone_details.phone_numbers)
                 {
                   if (fd.value == null)
                     continue;
 
-                  var existing = phone_numbers_set.lookup (fd.value);
+                  var existing = phone_numbers_set.get (fd.value);
                   if (existing != null)
                     existing.extend_parameters (fd.parameters);
                   else
                     {
                       var new_fd = new FieldDetails (fd.value);
                       new_fd.extend_parameters (fd.parameters);
-                      phone_numbers_set.insert (fd.value, new_fd);
-                      phones.prepend ((owned) new_fd);
+                      phone_numbers_set.set (fd.value, new_fd);
+                      phones.add (new_fd);
                     }
                 }
             }
         }
-      /* Set the private member directly to avoid iterating this list again */
-      phones.reverse ();
-      this._phone_numbers = (owned) phones;
+
+      /* Set the private member directly to avoid iterating this set again */
+      this._phone_numbers = phones;
 
       this.notify_property ("phone-numbers");
     }
diff --git a/folks/phone-details.vala b/folks/phone-details.vala
index fb824b2..449d0be 100644
--- a/folks/phone-details.vala
+++ b/folks/phone-details.vala
@@ -20,6 +20,7 @@
  */
 
 using GLib;
+using Gee;
 
 /**
  * Interface for classes that can provide a phone number, such as
@@ -40,9 +41,9 @@ public interface Folks.PhoneDetails : Object
    *
    * A list of phone numbers associated to the contact.
    *
-   * @since 0.3.5
+   * @since UNRELEASED
    */
-  public abstract List<FieldDetails> phone_numbers { get; set; }
+  public abstract Set<FieldDetails> phone_numbers { get; set; }
 
   /**
    * Normalise and compare two phone numbers.
diff --git a/folks/potential-match.vala b/folks/potential-match.vala
index bceee13..2d91617 100644
--- a/folks/potential-match.vala
+++ b/folks/potential-match.vala
@@ -105,14 +105,12 @@ public class Folks.PotentialMatch : Object
 
   private void _inspect_phone_numbers ()
     {
-      unowned GLib.List<FieldDetails> list_a =
-        this._individual_a.phone_numbers;
-      unowned GLib.List<FieldDetails> list_b =
-        this._individual_b.phone_numbers;
+      var set_a = this._individual_a.phone_numbers;
+      var set_b = this._individual_b.phone_numbers;
 
-      foreach (var fd_a in list_a)
+      foreach (var fd_a in set_a)
         {
-          foreach (var fd_b in list_b)
+          foreach (var fd_b in set_b)
             {
               if (PhoneDetails.numbers_equal (fd_a.value, fd_b.value))
                 {
diff --git a/tests/tracker/add-persona.vala b/tests/tracker/add-persona.vala
index 1b7c7d5..c73eb7a 100644
--- a/tests/tracker/add-persona.vala
+++ b/tests/tracker/add-persona.vala
@@ -248,14 +248,13 @@ public class AddPersonaTests : Folks.TestCase
       details.insert (Folks.PersonaStore.detail_key (PersonaDetail.NOTES),
           (owned) v10);
 
-      Value? v11 = Value (typeof (GLib.List<FieldDetails>));
-      GLib.List<FieldDetails> phones =
-        new GLib.List<FieldDetails> ();
+      Value? v11 = Value (typeof (Set<FieldDetails>));
+      var phones = new HashSet<FieldDetails> ();
       var phone_1 = new FieldDetails (this._phone_1);
-      phones.prepend ((owned) phone_1);
+      phones.add (phone_1);
       var phone_2 = new FieldDetails (this._phone_2);
-      phones.prepend ((owned) phone_2);
-      v11.set_pointer (phones);
+      phones.add (phone_2);
+      v11.set_object (phones);
       details.insert (
           Folks.PersonaStore.detail_key (PersonaDetail.PHONE_NUMBERS),
           (owned) v11);
@@ -444,7 +443,7 @@ public class AddPersonaTests : Folks.TestCase
             }
         }
 
-      foreach (unowned FieldDetails e in i.phone_numbers)
+      foreach (var e in i.phone_numbers)
         {
           if (e.value == this._phone_1)
             {
diff --git a/tests/tracker/duplicated-phones.vala b/tests/tracker/duplicated-phones.vala
index 651c27b..24ff061 100644
--- a/tests/tracker/duplicated-phones.vala
+++ b/tests/tracker/duplicated-phones.vala
@@ -166,12 +166,11 @@ public class DuplicatedPhonesTests : Folks.TestCase
       details1.insert (Folks.PersonaStore.detail_key (PersonaDetail.FULL_NAME),
           (owned) val);
 
-      val = Value (typeof (GLib.List<FieldDetails>));
-      GLib.List<FieldDetails> phones1 =
-        new GLib.List<FieldDetails> ();
+      val = Value (typeof (Set<FieldDetails>));
+      var phones1 = new HashSet<FieldDetails> ();
       var phone_1 = new FieldDetails (this._phone_1);
-      phones1.prepend ((owned) phone_1);
-      val.set_pointer (phones1);
+      phones1.add (phone_1);
+      val.set_object (phones1);
       details1.insert (
           Folks.PersonaStore.detail_key (PersonaDetail.PHONE_NUMBERS),
           (owned) val);
@@ -181,12 +180,11 @@ public class DuplicatedPhonesTests : Folks.TestCase
       details2.insert (Folks.PersonaStore.detail_key (PersonaDetail.FULL_NAME),
           (owned) val);
 
-      val = Value (typeof (GLib.List<FieldDetails>));
-      GLib.List<FieldDetails> phones2 =
-        new GLib.List<FieldDetails> ();
+      val = Value (typeof (Set<FieldDetails>));
+      var phones2 = new HashSet<FieldDetails> ();
       var phone_2 = new FieldDetails (this._phone_1);
-      phones2.prepend ((owned) phone_2);
-      val.set_pointer (phones2);
+      phones2.add (phone_2);
+      val.set_object (phones2);
       details2.insert (
           Folks.PersonaStore.detail_key (PersonaDetail.PHONE_NUMBERS),
           (owned) val);
diff --git a/tests/tracker/match-phone-number.vala b/tests/tracker/match-phone-number.vala
index 9e92100..2b718ba 100644
--- a/tests/tracker/match-phone-number.vala
+++ b/tests/tracker/match-phone-number.vala
@@ -173,11 +173,11 @@ public class MatchPhoneNumberTests : Folks.TestCase
       details1.insert (Folks.PersonaStore.detail_key (PersonaDetail.FULL_NAME),
           (owned) val);
 
-      val = Value (typeof (GLib.List<FieldDetails>));
-      var phone_numbers1 = new GLib.List<FieldDetails> ();
+      val = Value (typeof (Set<FieldDetails>));
+      var phone_numbers1 = new HashSet<FieldDetails> ();
       var phone_number_1 = new FieldDetails (this._phone_1);
-      phone_numbers1.prepend ((owned) phone_number_1);
-      val.set_pointer (phone_numbers1);
+      phone_numbers1.add (phone_number_1);
+      val.set_object (phone_numbers1);
       details1.insert (
           Folks.PersonaStore.detail_key (PersonaDetail.PHONE_NUMBERS),
           (owned) val);
@@ -187,11 +187,11 @@ public class MatchPhoneNumberTests : Folks.TestCase
       details2.insert (Folks.PersonaStore.detail_key (PersonaDetail.FULL_NAME),
           (owned) val);
 
-      val = Value (typeof (GLib.List<FieldDetails>));
-      var phone_numbers2 = new GLib.List<FieldDetails> ();
+      val = Value (typeof (Set<FieldDetails>));
+      var phone_numbers2 = new HashSet<FieldDetails> ();
       var phone_number_2 = new FieldDetails (this._phone_2);
-      phone_numbers2.prepend ((owned) phone_number_2);
-      val.set_pointer (phone_numbers2);
+      phone_numbers2.add (phone_number_2);
+      val.set_object (phone_numbers2);
       details2.insert (
           Folks.PersonaStore.detail_key (PersonaDetail.PHONE_NUMBERS),
           (owned) val);
diff --git a/tests/tracker/phones-updates.vala b/tests/tracker/phones-updates.vala
index c2113d6..d1eae86 100644
--- a/tests/tracker/phones-updates.vala
+++ b/tests/tracker/phones-updates.vala
@@ -125,7 +125,7 @@ public class PhonesUpdatesTests : Folks.TestCase
 
               i.notify["phone-numbers"].connect (this._notify_phones_cb);
 
-              foreach (unowned FieldDetails fd in i.phone_numbers)
+              foreach (var fd in i.phone_numbers)
                 {
                   var phone = fd.value;
                   if (phone == this._phone_1)
@@ -142,7 +142,7 @@ public class PhonesUpdatesTests : Folks.TestCase
   private void _notify_phones_cb (Object individual_obj, ParamSpec ps)
     {
       Folks.Individual i = (Folks.Individual) individual_obj;
-      foreach (unowned FieldDetails fd in i.phone_numbers)
+      foreach (var fd in i.phone_numbers)
         {
           var phone = fd.value;
           if (phone == this._phone_1)
diff --git a/tests/tracker/set-phones.vala b/tests/tracker/set-phones.vala
index 00409d8..cbc7643 100644
--- a/tests/tracker/set-phones.vala
+++ b/tests/tracker/set-phones.vala
@@ -113,13 +113,13 @@ public class SetPhonesTests : Folks.TestCase
             {
               i.notify["phone-numbers"].connect (this._notify_phones_cb);
 
-              GLib.List<FieldDetails> phones = new GLib.List<FieldDetails> ();
+              var phones = new HashSet<FieldDetails> ();
               var p1 = new FieldDetails (this._phone_1);
-              phones.prepend ((owned) p1);
+              phones.add (p1);
               var p2 = new FieldDetails (this._phone_2);
-              phones.prepend ((owned) p2);
+              phones.add (p2);
               Trf.Persona p = (Trf.Persona)i.personas.nth_data (0);
-              p.phone_numbers = (owned) phones;
+              p.phone_numbers = phones;
             }
         }
 
@@ -131,7 +131,7 @@ public class SetPhonesTests : Folks.TestCase
       Folks.Individual i = (Folks.Individual) individual_obj;
       if (i.full_name == this._persona_fullname)
         {
-          foreach (unowned FieldDetails p in i.phone_numbers)
+          foreach (var p in i.phone_numbers)
             {
               if (p.value == this._phone_1)
                 this._phone_1_found = true;
diff --git a/tools/inspect/utils.vala b/tools/inspect/utils.vala
index fd20745..c25ab23 100644
--- a/tools/inspect/utils.vala
+++ b/tools/inspect/utils.vala
@@ -320,7 +320,8 @@ private class Folks.Inspect.Utils
           output_string += " }";
           return output_string;
         }
-      else if (prop_name == "email-addresses")
+      else if (prop_name == "email-addresses" ||
+               prop_name == "phone-numbers")
         {
           output_string = "{ ";
           bool first = true;
@@ -340,8 +341,7 @@ private class Folks.Inspect.Utils
 
             return output_string;
         }
-      else if (prop_name == "urls" ||
-               prop_name == "phone-numbers")
+      else if (prop_name == "urls")
         {
           output_string = "{ ";
           bool first = true;



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