[folks] e-d-s: Implement read/write support for gender property



commit 21abb433ae11eb38305627ee1778e703c281a6b5
Author: Raul Gutierrez Segales <rgs collabora co uk>
Date:   Tue Aug 2 11:45:34 2011 +0100

    e-d-s: Implement read/write support for gender property
    
    Fixes: https://bugzilla.gnome.org/show_bug.cgi?id=655745

 NEWS                                     |    1 +
 backends/eds/lib/edsf-persona-store.vala |   49 ++++++++++++++++++
 backends/eds/lib/edsf-persona.vala       |   80 ++++++++++++++++++++++++++++--
 3 files changed, 126 insertions(+), 4 deletions(-)
---
diff --git a/NEWS b/NEWS
index 6ca3444..95084db 100644
--- a/NEWS
+++ b/NEWS
@@ -25,6 +25,7 @@ Bugs fixed:
 * Bug 652643 â Add PersonaStore cache
 * Bug 655510 â Make truly-writeable *Details property setters public
 * Bug 654907 â The writable store shouldn't be set by type_id
+* Bug 655745 â Implement read/write support for gender property
 
 API changes:
 * Swf.Persona retains and exposes its libsocialweb Contact
diff --git a/backends/eds/lib/edsf-persona-store.vala b/backends/eds/lib/edsf-persona-store.vala
index f995637..a25e1e5 100644
--- a/backends/eds/lib/edsf-persona-store.vala
+++ b/backends/eds/lib/edsf-persona-store.vala
@@ -206,6 +206,7 @@ public class Edsf.PersonaStore : Folks.PersonaStore
    * - PersonaStore.detail_key (PersonaDetail.AVATAR)
    * - PersonaStore.detail_key (PersonaDetail.EMAIL_ADDRESSES)
    * - PersonaStore.detail_key (PersonaDetail.FULL_NAME)
+   * - PersonaStore.detail_key (PersonaDetail.GENDER)
    * - PersonaStore.detail_key (PersonaDetail.IM_ADDRESSES)
    * - PersonaStore.detail_key (PersonaDetail.PHONE_NUMBERS)
    * - PersonaStore.detail_key (PersonaDetail.POSTAL_ADDRESSES)
@@ -292,6 +293,11 @@ public class Edsf.PersonaStore : Folks.PersonaStore
               var notes = (Gee.HashSet<Note>) v.get_object ();
               yield this._set_contact_notes (contact, notes);
             }
+          else if (k == Folks.PersonaStore.detail_key (PersonaDetail.GENDER))
+            {
+              var gender = (Gender) v.get_enum ();
+              yield this._set_contact_gender (contact, gender);
+            }
         }
 
       Edsf.Persona? persona = null;
@@ -903,6 +909,49 @@ public class Edsf.PersonaStore : Folks.PersonaStore
       contact.set (ContactField.CATEGORY_LIST, categories);
     }
 
+  internal async void _set_gender (Edsf.Persona persona,
+      Gender gender)
+    {
+      try
+        {
+          E.Contact contact = ((Edsf.Persona) persona).contact;
+          yield this._set_contact_gender (contact, gender);
+          yield this._addressbook.modify_contact (contact);
+        }
+      catch (GLib.Error e)
+        {
+          GLib.warning ("Can't set gender: %s", e.message);
+        }
+    }
+
+  private async void _set_contact_gender (E.Contact contact,
+      Gender gender)
+    {
+      var attr = contact.get_attribute (Edsf.Persona.gender_attribute_name);
+      if (attr != null)
+        {
+          contact.remove_attribute (attr);
+        }
+
+      switch (gender)
+        {
+          case Gender.UNSPECIFIED:
+            break;
+          case Gender.MALE:
+            attr = new VCardAttribute (null,
+                Edsf.Persona.gender_attribute_name);
+            attr.add_value (Edsf.Persona.gender_male);
+            contact.add_attribute (attr);
+            break;
+          case Gender.FEMALE:
+            attr = new VCardAttribute (null,
+                Edsf.Persona.gender_attribute_name);
+            attr.add_value (Edsf.Persona.gender_female);
+            contact.add_attribute (attr);
+            break;
+        }
+    }
+
   private void _contacts_added_cb (GLib.List<E.Contact> contacts)
     {
       var added_personas = new HashSet<Persona> ();
diff --git a/backends/eds/lib/edsf-persona.vala b/backends/eds/lib/edsf-persona.vala
index 6a321bb..52f1599 100644
--- a/backends/eds/lib/edsf-persona.vala
+++ b/backends/eds/lib/edsf-persona.vala
@@ -58,6 +58,43 @@ public class Edsf.Persona : Folks.Persona,
   public static const string[] url_properties = {
     "blog_url", "fburl", "homepage_url", "video_url"
   };
+
+  /**
+   * The vCard attribute used to specify a Contact's gender
+   *
+   * Based on:
+   * http://tools.ietf.org/html/draft-ietf-vcarddav-vcardrev-22
+   *
+   * Note that the above document is a draft and the gender property
+   * is still considered experimental, hence the "X-" prefix in the
+   * attribute name. So this might change.
+   *
+   * @since UNRELEASED
+   */
+  public static const string gender_attribute_name = "X-GENDER";
+
+  /**
+   * The value used to define the male gender for the
+   * X-GENDER vCard property.
+   *
+   * Based on:
+   * http://tools.ietf.org/html/draft-ietf-vcarddav-vcardrev-22
+   *
+   * @since UNRELEASED
+   */
+  public static const string gender_male = "M";
+
+  /**
+   * The value used to define the female gender for the
+   * X-GENDER vCard property.
+   *
+   * Based on:
+   * http://tools.ietf.org/html/draft-ietf-vcarddav-vcardrev-22
+   *
+   * @since UNRELEASED
+   */
+  public static const string gender_female = "F";
+
   private const string[] _linkable_properties = { "im-addresses",
                                                   "local-ids",
                                                   "web-service-addresses" };
@@ -285,16 +322,23 @@ public class Edsf.Persona : Folks.Persona,
         }
     }
 
+  private Gender _gender;
   /**
    * { inheritDoc}
    *
-   * @since 0.5.UNRELEASED
+   * @since UNRELEASED
    */
-  public Gender gender { get; private set; }
+  public Gender gender
+    {
+      get { return this._gender; }
+      public set
+        {
+          ((Edsf.PersonaStore) this.store)._set_gender (this, value);
+        }
+    }
 
   private HashSet<FieldDetails> _urls;
   private Set<FieldDetails> _urls_ro;
-
   /**
    * { inheritDoc}
    *
@@ -434,9 +478,9 @@ public class Edsf.Persona : Folks.Persona,
               uid: uid,
               iid: iid,
               store: store,
-              gender: Gender.UNSPECIFIED,
               is_user: is_user);
 
+      this._gender = Gender.UNSPECIFIED;
       this.contact_id = contact_id;
       this._phone_numbers = new HashSet<FieldDetails> ();
       this._phone_numbers_ro = this._phone_numbers.read_only_view;
@@ -523,6 +567,34 @@ public class Edsf.Persona : Folks.Persona,
       this._update_notes ();
       this._update_local_ids ();
       this._update_web_services_addresses ();
+      this._update_gender ();
+    }
+
+  private void _update_gender ()
+    {
+      var gender = Gender.UNSPECIFIED;
+      var gender_attr =
+          this.contact.get_attribute (Edsf.Persona.gender_attribute_name);
+
+      if (gender_attr != null)
+        {
+          var gender_str = gender_attr.get_value ().up ();
+
+          if (gender_str == Edsf.Persona.gender_male)
+            {
+              gender = Gender.MALE;
+            }
+          else if (gender_str == Edsf.Persona.gender_female)
+            {
+              gender = Gender.FEMALE;
+            }
+        }
+
+      if (this._gender != gender)
+        {
+          this._gender = gender;
+          this.notify_property ("gender");
+        }
     }
 
   private void _update_web_services_addresses ()



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