2 * Copyright (C) 2012 Collabora Ltd.
4 * This library is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation, either version 2.1 of the License, or
7 * (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library. If not, see <http://www.gnu.org/licenses/>.
18 * Jeremy Whiting <jeremy.whiting@collabora.co.uk>
20 * Based on kf-persona.vala by:
21 * Philip Withnall <philip.withnall@collabora.co.uk>
27 using Folks.Backends.Ofono;
30 * A persona subclass which represents a single persona from a simple key file.
34 public class Folks.Backends.Ofono.Persona : Folks.Persona,
39 private StructuredName? _structured_name = null;
40 private string _full_name = "";
41 private string _nickname = "";
42 private HashSet<PhoneFieldDetails> _phone_numbers;
43 private Set<PhoneFieldDetails> _phone_numbers_ro;
44 private HashSet<EmailFieldDetails> _email_addresses;
45 private Set<EmailFieldDetails> _email_addresses_ro;
47 private const string[] _linkable_properties =
52 private static string[] _writeable_properties = {};
57 public override string[] linkable_properties
59 get { return Ofono.Persona._linkable_properties; }
65 public override string[] writeable_properties
67 get { return Ofono.Persona._writeable_properties; }
73 [CCode (notify = false)]
74 public Set<PhoneFieldDetails> phone_numbers
76 get { return this._phone_numbers_ro; }
77 set { this.change_phone_numbers.begin (value); } /* not writeable */
83 [CCode (notify = false)]
84 public StructuredName? structured_name
86 get { return this._structured_name; }
87 set { this.change_structured_name.begin (value); } /* not writeable */
93 [CCode (notify = false)]
94 public string full_name
96 get { return this._full_name; }
97 set { this.change_full_name.begin (value); } /* not writeable */
103 [CCode (notify = false)]
104 public string nickname
106 get { return this._nickname; }
107 set { this.change_nickname.begin (value); } /* not writeable */
113 [CCode (notify = false)]
114 public Set<EmailFieldDetails> email_addresses
116 get { return this._email_addresses_ro; }
117 set { this.change_email_addresses.begin (value); } /* not writeable */
121 * Create a new persona.
123 * Create a new persona for the given vCard contents.
125 * @param vcard the vCard data to use for this {@link Persona}.
126 * @param store the {@link PersonaStore} this {@link Persona} belongs to.
130 public Persona (string vcard, Folks.PersonaStore store)
132 var iid = Checksum.compute_for_string (ChecksumType.SHA1, vcard);
133 var uid = Folks.Persona.build_uid ("ofono", store.id, iid);
135 Object (display_id: uid,
140 this._set_vcard (vcard);
145 debug ("Adding Ofono Persona '%s' (IID '%s', group '%s')", this.uid,
146 this.iid, this.display_id);
148 this._phone_numbers = new HashSet<PhoneFieldDetails> ();
149 this._phone_numbers_ro = this._phone_numbers.read_only_view;
151 this._email_addresses = new HashSet<EmailFieldDetails> ();
152 this._email_addresses_ro = this._email_addresses.read_only_view;
155 private void _set_vcard (string vcard)
157 E.VCard card = new E.VCard.from_string (vcard);
159 E.VCardAttribute? attribute = card.get_attribute ("TEL");
160 if (attribute != null)
162 this._phone_numbers.add (new PhoneFieldDetails (attribute.get_value_decoded ().str) );
165 attribute = card.get_attribute ("FN");
166 if (attribute != null)
168 this._full_name = attribute.get_value_decoded ().str;
171 attribute = card.get_attribute ("NICKNAME");
172 if (attribute != null)
174 this._nickname = attribute.get_value_decoded ().str;
177 attribute = card.get_attribute ("N");
178 if (attribute != null)
180 unowned GLib.List<StringBuilder> values = attribute.get_values_decoded ();
181 if (values.length () >= 5)
183 this._structured_name = new StructuredName (values.nth_data (0).str,
184 values.nth_data (1).str,
185 values.nth_data (2).str,
186 values.nth_data (3).str,
187 values.nth_data (4).str);
191 warning ("Expected 5 components to N value of vcard, got %u", values.length ());
195 attribute = card.get_attribute ("EMAIL");
196 if (attribute != null)
198 this._email_addresses.add (new EmailFieldDetails (attribute.get_value_decoded ().str) );
205 public override void linkable_property_to_links (string prop_name,
206 Folks.Persona.LinkablePropertyCallback callback)
208 if (prop_name == "phone-numbers")
210 foreach (var phone_number in this._phone_numbers)
212 if (phone_number.value != null)
213 callback (phone_number.value);
216 else if (prop_name == "email-addresses")
218 foreach (var email_address in this._email_addresses)
220 if (email_address.value != null)
221 callback (email_address.value);
227 base.linkable_property_to_links (prop_name, callback);