Release version 0.9.0
[platform/upstream/folks.git] / backends / ofono / ofono-persona.vala
1 /*
2  * Copyright (C) 2012 Collabora Ltd.
3  *
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.
8  *
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.
13  *
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/>.
16  *
17  * Authors:
18  *          Jeremy Whiting <jeremy.whiting@collabora.co.uk>
19  *
20  * Based on kf-persona.vala by:
21  *       Philip Withnall <philip.withnall@collabora.co.uk>
22  */
23
24 using GLib;
25 using Gee;
26 using Folks;
27 using Folks.Backends.Ofono;
28
29 /**
30  * A persona subclass which represents a single persona from a simple key file.
31  *
32  * @since 0.9.0
33  */
34 public class Folks.Backends.Ofono.Persona : Folks.Persona,
35     EmailDetails,
36     NameDetails,
37     PhoneDetails
38 {
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;
46   
47   private const string[] _linkable_properties =
48     {
49       "phone-numbers",
50       "email-addresses"
51     };
52   private static string[] _writeable_properties = {};
53
54   /**
55    * {@inheritDoc}
56    */
57   public override string[] linkable_properties
58     {
59       get { return Ofono.Persona._linkable_properties; }
60     }
61
62   /**
63    * {@inheritDoc}
64    */
65   public override string[] writeable_properties
66     {
67       get { return Ofono.Persona._writeable_properties; }
68     }
69
70   /**
71    * {@inheritDoc}
72    */
73   [CCode (notify = false)]
74   public Set<PhoneFieldDetails> phone_numbers
75     {
76       get { return this._phone_numbers_ro; }
77       set { this.change_phone_numbers.begin (value); } /* not writeable */
78     }
79
80   /**
81    * {@inheritDoc}
82    */
83   [CCode (notify = false)]
84   public StructuredName? structured_name
85     {
86       get { return this._structured_name; }
87       set { this.change_structured_name.begin (value); } /* not writeable */
88     }
89
90   /**
91    * {@inheritDoc}
92    */
93   [CCode (notify = false)]
94   public string full_name
95     {
96       get { return this._full_name; }
97       set { this.change_full_name.begin (value); } /* not writeable */
98     }
99
100   /**
101    * {@inheritDoc}
102    */
103   [CCode (notify = false)]
104   public string nickname
105     {
106       get { return this._nickname; }
107       set { this.change_nickname.begin (value); } /* not writeable */
108     }
109
110   /**
111    * {@inheritDoc}
112    */
113   [CCode (notify = false)]
114   public Set<EmailFieldDetails> email_addresses
115     {
116       get { return this._email_addresses_ro; }
117       set { this.change_email_addresses.begin (value); } /* not writeable */
118     }
119
120   /**
121    * Create a new persona.
122    *
123    * Create a new persona for the given vCard contents.
124    *
125    * @param vcard the vCard data to use for this {@link Persona}.
126    * @param store the {@link PersonaStore} this {@link Persona} belongs to.
127    *
128    * @since 0.9.0
129    */
130   public Persona (string vcard, Folks.PersonaStore store)
131     {
132       var iid = Checksum.compute_for_string (ChecksumType.SHA1, vcard);
133       var uid = Folks.Persona.build_uid ("ofono", store.id, iid);
134
135       Object (display_id: uid,
136               iid: iid,
137               uid: uid,
138               store: store,
139               is_user: false);
140       this._set_vcard (vcard);
141     }
142
143   construct
144     {
145       debug ("Adding Ofono Persona '%s' (IID '%s', group '%s')", this.uid,
146           this.iid, this.display_id);
147       
148       this._phone_numbers = new HashSet<PhoneFieldDetails> ();
149       this._phone_numbers_ro = this._phone_numbers.read_only_view;
150
151       this._email_addresses = new HashSet<EmailFieldDetails> ();
152       this._email_addresses_ro = this._email_addresses.read_only_view;
153     }
154
155   private void _set_vcard (string vcard)
156     {
157       E.VCard card = new E.VCard.from_string (vcard);
158       
159       E.VCardAttribute? attribute = card.get_attribute ("TEL");
160       if (attribute != null)
161         {
162           this._phone_numbers.add (new PhoneFieldDetails (attribute.get_value_decoded ().str) );
163         }
164
165       attribute = card.get_attribute ("FN");
166       if (attribute != null)
167         {
168           this._full_name = attribute.get_value_decoded ().str;
169         }
170       
171       attribute = card.get_attribute ("NICKNAME");
172       if (attribute != null)
173         {
174           this._nickname = attribute.get_value_decoded ().str;
175         }
176         
177       attribute = card.get_attribute ("N");
178       if (attribute != null)
179         {
180           unowned GLib.List<StringBuilder> values = attribute.get_values_decoded ();
181           if (values.length () >= 5)
182             {
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);
188             }
189           else
190             {
191               warning ("Expected 5 components to N value of vcard, got %u", values.length ());
192             }
193         }
194         
195       attribute = card.get_attribute ("EMAIL");
196       if (attribute != null)
197         {
198           this._email_addresses.add (new EmailFieldDetails (attribute.get_value_decoded ().str) );
199         }
200     }
201     
202   /**
203    * {@inheritDoc}
204    */
205   public override void linkable_property_to_links (string prop_name,
206       Folks.Persona.LinkablePropertyCallback callback)
207     {
208       if (prop_name == "phone-numbers")
209         {
210           foreach (var phone_number in this._phone_numbers)
211             {
212                 if (phone_number.value != null)
213                     callback (phone_number.value);
214             }
215         }
216       else if (prop_name == "email-addresses")
217         {
218           foreach (var email_address in this._email_addresses)
219             {
220                 if (email_address.value != null)
221                     callback (email_address.value);
222             }
223         }
224       else
225         {
226           /* Chain up */
227           base.linkable_property_to_links (prop_name, callback);
228         }
229     }
230 }