[folks] Handle setting existing emails/phones in a Trf.Persona



commit 80ad989ec0f785561986ab417917a46554fc9636
Author: Raul Gutierrez Segales <raul gutierrez segales collabora co uk>
Date:   Sun Apr 10 15:17:30 2011 +0100

    Handle setting existing emails/phones in a Trf.Persona

 NEWS                                        |    2 +
 backends/tracker/lib/trf-persona-store.vala |  191 ++++++++++++++++-----------
 2 files changed, 117 insertions(+), 76 deletions(-)
---
diff --git a/NEWS b/NEWS
index d01dd7f..3ac1310 100644
--- a/NEWS
+++ b/NEWS
@@ -27,6 +27,8 @@ Bugs fixed:
 * Bug 646944 â?? Fix libsocialweb updates and removals of contacts
 * Bug 644867 â?? add interface for linkable web service contact UIDs
 * Bug 647168 â?? print the "web-service-addresses" property correctly
+* Bug 646831 â?? Setting an existing phone number or e-mail address on a
+  Trf.Persona fails
 
 API changes:
 * Add equal () to StructuredName
diff --git a/backends/tracker/lib/trf-persona-store.vala b/backends/tracker/lib/trf-persona-store.vala
index 622ad52..c5d395e 100644
--- a/backends/tracker/lib/trf-persona-store.vala
+++ b/backends/tracker/lib/trf-persona-store.vala
@@ -486,36 +486,14 @@ public class Trf.PersonaStore : Folks.PersonaStore
             {
               unowned GLib.List<FieldDetails> email_addresses =
                 (GLib.List<FieldDetails>) v.get_pointer ();
-              int email_cnt = 0;
-              foreach (var e in email_addresses)
+              var email_addresses_l = email_addresses.copy ();
+              foreach (var email_fd_obj in email_addresses_l)
                 {
-                  var email_affl = "_:email_affl%d".printf (email_cnt);
-                  var email = yield this._urn_from_property (
-                      Trf.OntologyDefs.NCO_EMAIL,
-                      Trf.OntologyDefs.NCO_EMAIL_PROP, e.value);
-
-                  if (email == "")
-                    {
-                      email = "_:email%d".printf (email_cnt);
-                      builder.subject (email);
-                      builder.predicate ("a");
-                      builder.object (Trf.OntologyDefs.NCO_EMAIL);
-                      builder.predicate (Trf.OntologyDefs.NCO_EMAIL_PROP);
-                      builder.object_string (e.value);
-                    }
-
-                  builder.subject (email_affl);
-                  builder.predicate ("a");
-                  builder.object (Trf.OntologyDefs.NCO_AFFILIATION);
-                  builder.predicate (Trf.OntologyDefs.NCO_HAS_EMAIL);
-                  builder.object (email);
-
-                  builder.subject ("_:p");
-                  builder.predicate (Trf.OntologyDefs.NCO_HAS_AFFILIATION);
-                  builder.object (email_affl);
-
-                  email_cnt++;
+                  email_fd_obj.ref ();
                 }
+              yield this._build_update_query (builder,
+                  (owned) email_addresses_l,
+                  "_:p", Trf.Attrib.EMAILS);
             }
           else if (k == Folks.PersonaStore.detail_key (
                 PersonaDetail.IM_ADDRESSES))
@@ -570,37 +548,14 @@ public class Trf.PersonaStore : Folks.PersonaStore
             {
               unowned GLib.List<FieldDetails> phone_numbers =
                 (GLib.List<FieldDetails>) v.get_pointer ();
-
-              int phone_cnt = 0;
-              foreach (var p in phone_numbers)
+              var phone_numbers_l = phone_numbers.copy ();
+              foreach (var phone_fd_obj in phone_numbers_l)
                 {
-                  var phone_affl = "_:phone_affl%d".printf (phone_cnt);
-                  var phone = yield this._urn_from_property (
-                      Trf.OntologyDefs.NCO_PHONE,
-                      Trf.OntologyDefs.NCO_PHONE_PROP, p.value);
-
-                  if (phone == "")
-                      {
-                        phone = "_:phone%d".printf (phone_cnt);
-                        builder.subject (phone);
-                        builder.predicate ("a");
-                        builder.object (Trf.OntologyDefs.NCO_PHONE);
-                        builder.predicate (Trf.OntologyDefs.NCO_PHONE_PROP);
-                        builder.object_string (p.value);
-                      }
-
-                  builder.subject (phone_affl);
-                  builder.predicate ("a");
-                  builder.object (Trf.OntologyDefs.NCO_AFFILIATION);
-                  builder.predicate (Trf.OntologyDefs.NCO_HAS_PHONE);
-                  builder.object (phone);
-
-                  builder.subject ("_:p");
-                  builder.predicate (Trf.OntologyDefs.NCO_HAS_AFFILIATION);
-                  builder.object (phone_affl);
-
-                  phone_cnt++;
+                  phone_fd_obj.ref ();
                 }
+              yield this._build_update_query (builder,
+                  (owned) phone_numbers_l,
+                "_:p", Trf.Attrib.PHONES);
             }
           else if (k == Folks.PersonaStore.detail_key (PersonaDetail.ROLES))
             {
@@ -902,6 +857,67 @@ public class Trf.PersonaStore : Folks.PersonaStore
       return urn;
     }
 
+  private async void _build_update_query (
+      Tracker.Sparql.Builder builder,
+      owned GLib.List<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:
+            related_attrib = Trf.OntologyDefs.NCO_PHONE;
+            related_prop = Trf.OntologyDefs.NCO_PHONE_PROP;
+            related_connection = Trf.OntologyDefs.NCO_HAS_PHONE;
+            affl_var = "_:phone_affl%d";
+            obj_var = "_:phone%d";
+            break;
+          case Trf.Attrib.EMAILS:
+            related_attrib = Trf.OntologyDefs.NCO_EMAIL;
+            related_prop = Trf.OntologyDefs.NCO_EMAIL_PROP;
+            related_connection = Trf.OntologyDefs.NCO_HAS_EMAIL;
+            affl_var = "_:email_affl%d";
+            obj_var = "_:email%d";
+            break;
+        }
+
+      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++;
+        }
+    }
+
   /*
    * Garbage collecting related resources:
    *  - for each related resource we (recursively)
@@ -1919,18 +1935,55 @@ public class Trf.PersonaStore : Folks.PersonaStore
       yield this._tracker_update (query, "change_is_favourite");
     }
 
+  internal async void _set_emails (Folks.Persona persona,
+      owned GLib.List<FieldDetails> emails)
+    {
+      yield this._set_unique_attrib (persona, (owned) emails,
+          Trf.Attrib.EMAILS);
+    }
+
   internal async void _set_phones (Folks.Persona persona,
       owned GLib.List<FieldDetails> phone_numbers)
     {
-      yield this._set_attrib (persona, (owned) phone_numbers,
+      yield this._set_unique_attrib (persona, (owned) phone_numbers,
           Trf.Attrib.PHONES);
     }
 
-  internal async void _set_emails (Folks.Persona persona,
-      owned GLib.List<FieldDetails> emails)
+  internal async void _set_unique_attrib (Folks.Persona persona,
+      owned GLib.List<FieldDetails> properties, Trf.Attrib attrib)
     {
-      yield this._set_attrib (persona, (owned) emails,
-          Trf.Attrib.EMAILS);
+      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:
+            query_name = "_set_phones";
+            yield this._remove_attributes_from_persona (persona,
+                _REMOVE_PHONES);
+            yield this._build_update_query (builder, (owned) properties,
+                "?contact", Trf.Attrib.PHONES);
+            break;
+          case Trf.Attrib.EMAILS:
+            query_name = "_set_emailss";
+            yield this._remove_attributes_from_persona (persona,
+                _REMOVE_EMAILS);
+            yield this._build_update_query (builder, (owned) properties,
+                "?contact", Trf.Attrib.EMAILS);
+            break;
+        }
+      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_urls (Folks.Persona persona,
@@ -2234,20 +2287,6 @@ public class Trf.PersonaStore : Folks.PersonaStore
 
       switch (what)
         {
-          case Trf.Attrib.PHONES:
-            related_attrib = Trf.OntologyDefs.NCO_PHONE;
-            related_prop = Trf.OntologyDefs.NCO_PHONE_PROP;
-            related_connection = Trf.OntologyDefs.NCO_HAS_PHONE;
-            yield this._remove_attributes_from_persona (persona,
-                _REMOVE_PHONES);
-            break;
-          case Trf.Attrib.EMAILS:
-            related_attrib = Trf.OntologyDefs.NCO_EMAIL;
-            related_prop = Trf.OntologyDefs.NCO_EMAIL_PROP;
-            related_connection = Trf.OntologyDefs.NCO_HAS_EMAIL;
-            yield this._remove_attributes_from_persona (persona,
-                _REMOVE_EMAILS);
-            break;
           case Trf.Attrib.URLS:
             related_attrib = Trf.OntologyDefs.NCO_URL;
             related_connection = Trf.OntologyDefs.NCO_URL;



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