[folks] Add Emailable for contacts with emails



commit 89df5dc2da998801cebe0c4bfa79fc1b5671ce3d
Author: Marco Barisione <marco barisione org>
Date:   Tue Dec 28 17:42:18 2010 +0100

    Add Emailable for contacts with emails

 folks/Makefile.am     |    1 +
 folks/emailable.vala  |   37 ++++++++++++++++++++++++++++++++++
 folks/individual.vala |   53 +++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 91 insertions(+), 0 deletions(-)
---
diff --git a/folks/Makefile.am b/folks/Makefile.am
index 17460e2..6e533ed 100644
--- a/folks/Makefile.am
+++ b/folks/Makefile.am
@@ -15,6 +15,7 @@ libfolks_la_SOURCES = \
 	aliasable.vala \
 	backend.vala \
 	backend-store.vala \
+	emailable.vala \
 	favouritable.vala \
 	field-details.vala \
 	gender-owner.vala \
diff --git a/folks/emailable.vala b/folks/emailable.vala
new file mode 100644
index 0000000..09db2ad
--- /dev/null
+++ b/folks/emailable.vala
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2011 Collabora Ltd.
+ *
+ * This library is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors:
+ *       Marco Barisione <marco barisione collabora co uk>
+ */
+
+using GLib;
+
+/**
+ * Interface for classes that have email addresses, such as { link Persona}
+ * and { link Individual}.
+ *
+ * @since 0.3.UNRELEASED
+ */
+public interface Folks.Emailable : Object
+{
+  /**
+   * The email addresses of the contact.
+   *
+   * @since 0.3.UNRELEASED
+   */
+  public abstract List<FieldDetails> email_addresses { get; set; }
+}
diff --git a/folks/individual.vala b/folks/individual.vala
index 60b86e2..554ff8f 100644
--- a/folks/individual.vala
+++ b/folks/individual.vala
@@ -64,6 +64,7 @@ public enum Folks.TrustLevel
  */
 public class Folks.Individual : Object,
     Aliasable,
+    Emailable,
     Favouritable,
     GenderOwner,
     Groupable,
@@ -279,6 +280,22 @@ public class Folks.Individual : Object,
         }
     }
 
+  private GLib.List<FieldDetails> _email_addresses;
+  /**
+   * { inheritDoc}
+   */
+  public GLib.List<FieldDetails> email_addresses
+    {
+      get { return this._email_addresses; }
+      private set
+        {
+          this._email_addresses = new GLib.List<FieldDetails> ();
+          foreach (unowned FieldDetails fd in value)
+            this._email_addresses.prepend (fd);
+          this._email_addresses.reverse ();
+        }
+    }
+
   /**
    * Whether this Individual is a user-defined favourite.
    *
@@ -414,6 +431,11 @@ public class Folks.Individual : Object,
       this._update_phone_numbers ();
     }
 
+  private void _notify_email_addresses_cb ()
+    {
+      this._update_email_addresses ();
+    }
+
   /**
    * Add or remove the Individual from the specified group.
    *
@@ -549,6 +571,7 @@ public class Folks.Individual : Object,
       this._update_gender ();
       this._update_urls ();
       this._update_phone_numbers ();
+      this._update_email_addresses ();
     }
 
   private void _update_groups ()
@@ -828,6 +851,8 @@ public class Folks.Individual : Object,
       persona.notify["gender"].connect (this._notify_gender_cb);
       persona.notify["urls"].connect (this._notify_urls_cb);
       persona.notify["phone-numbers"].connect (this._notify_phone_numbers_cb);
+      persona.notify["email-addresses"].connect (
+          this._notify_email_addresses_cb);
 
       if (persona is Groupable)
         {
@@ -915,6 +940,8 @@ public class Folks.Individual : Object,
       persona.notify["urls"].disconnect (this._notify_urls_cb);
       persona.notify["phone-numbers"].disconnect (
           this._notify_phone_numbers_cb);
+      persona.notify["email-addresses"].disconnect (
+          this._notify_email_addresses_cb);
 
       if (persona is Groupable)
         {
@@ -997,6 +1024,32 @@ public class Folks.Individual : Object,
       this.notify_property ("phone-numbers");
     }
 
+  private void _update_email_addresses ()
+    {
+      /* Populate the email addresses as the union of our Personas' addresses.
+       * If the same URL exist multiple times we merge the parameters. */
+      var emails_set = new HashTable<unowned string, unowned FieldDetails> (
+          str_hash, str_equal);
+      foreach (var persona in this._persona_list)
+        {
+          var emailable = persona as Emailable;
+          if (emailable != null)
+            {
+              foreach (unowned FieldDetails fd in emailable.email_addresses)
+                {
+                  var existing = emails_set.lookup (fd.value);
+                  if (existing != null)
+                    existing.extend_parameters (fd.parameters);
+                  else
+                    emails_set.insert (fd.value, fd);
+                }
+            }
+        }
+      this._email_addresses = emails_set.get_values ();
+
+      this.notify_property ("email-addresses");
+    }
+
   private void _set_personas (GLib.List<Persona>? persona_list,
       Individual? replacement_individual)
     {



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