2 * Copyright (C) 2010 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 * Philip Withnall <philip.withnall@collabora.co.uk>
24 using Folks.Backends.Kf;
27 * A persona subclass which represents a single persona from a simple key file.
31 public class Folks.Backends.Kf.Persona : Folks.Persona,
36 private unowned GLib.KeyFile _key_file;
37 private HashMultiMap<string, string> _im_addresses;
38 private HashMultiMap<string, string> _web_service_addresses;
39 private string _alias;
40 private const string[] _linkable_properties =
43 "web-service-addresses"
49 public override string[] linkable_properties
51 get { return this._linkable_properties; }
61 get { return this._alias; }
65 if (this._alias == value)
68 debug ("Setting alias of Kf.Persona '%s' to '%s'.", this.uid, value);
71 this._key_file.set_string (this.display_id, "__alias", value);
73 ((Kf.PersonaStore) this.store).save_key_file.begin ();
80 public MultiMap<string, string> im_addresses
83 { return this._im_addresses; }
87 /* Remove the current IM addresses from the key file */
88 foreach (var protocol in this._im_addresses.get_keys ())
92 this._key_file.remove_key (this.display_id, protocol);
94 catch (KeyFileError e)
96 /* Ignore the error, since it's just a group or key not found
101 /* Add the new IM addresses to the key file and build a normalised
102 * table of them to set as the new property value */
103 var im_addresses = new HashMultiMap<string, string> ();
105 foreach (var protocol in value.get_keys ())
107 var addresses = value.get (protocol);
108 var normalised_addresses = new HashSet<string> ();
110 foreach (string address in addresses)
112 string normalised_address;
115 normalised_address = ImDetails.normalise_im_address (
118 catch (ImDetailsError e)
120 /* Somehow an error has crept into the user's
121 * relationships.ini. Warn of it and ignore the IM
127 normalised_addresses.add (normalised_address);
128 im_addresses.set (protocol, normalised_address);
131 string[] addrs = (string[]) normalised_addresses.to_array ();
132 addrs.length = normalised_addresses.size;
134 this._key_file.set_string_list (this.display_id, protocol, addrs);
137 this._im_addresses = im_addresses;
139 /* Get the PersonaStore to save the key file */
140 ((Kf.PersonaStore) this.store).save_key_file.begin ();
147 public MultiMap<string, string> web_service_addresses
150 { return this._web_service_addresses; }
154 /* Remove the current web service addresses from the key file */
155 foreach (var web_service in this._web_service_addresses.get_keys ())
159 this._key_file.remove_key (this.display_id, "web-service." + web_service);
161 catch (KeyFileError e)
163 /* Ignore the error, since it's just a group or key not found
168 /* Add the new web service addresses to the key file and build a
169 * table of them to set as the new property value */
170 var web_service_addresses = new HashMultiMap<string, string> ();
172 foreach (var web_service in value.get_keys ())
174 var addresses = value.get (web_service);
176 string[] addrs = (string[]) addresses.to_array ();
177 addrs.length = addresses.size;
179 this._key_file.set_string_list (this.display_id,
180 "web-service." + web_service, addrs);
182 foreach (var address in addresses)
184 web_service_addresses.set (web_service, address);
188 this._web_service_addresses = web_service_addresses;
190 /* Get the PersonaStore to save the key file */
191 ((Kf.PersonaStore) this.store).save_key_file.begin ();
196 * Create a new persona.
198 * Create a new persona for the {@link PersonaStore} `store`, representing
199 * the Persona given by the group `uid` in the key file `key_file`.
201 public Persona (KeyFile key_file, string id, Folks.PersonaStore store)
203 var iid = store.id + ":" + id;
204 var uid = this.build_uid ("key-file", store.id, id);
206 Object (display_id: id,
212 debug ("Adding key-file Persona '%s' (IID '%s', group '%s')", uid, iid,
215 this._key_file = key_file;
216 this._im_addresses = new HashMultiMap<string, string> ();
217 this._web_service_addresses = new HashMultiMap<string, string> ();
219 /* Load the IM addresses from the key file */
222 var keys = this._key_file.get_keys (this.display_id);
223 foreach (unowned string key in keys)
226 if (key == "__alias")
228 this._alias = this._key_file.get_string (this.display_id,
230 debug (" Loaded alias '%s'.", this._alias);
234 /* Web service addresses */
235 var decomposed_key = key.split(".", 2);
236 if (decomposed_key.length == 2 &&
237 decomposed_key[0] == "web-service")
239 unowned string web_service = decomposed_key[1];
240 var web_service_addresses = this._key_file.get_string_list (
241 this.display_id, web_service);
243 foreach (var web_service_address in web_service_addresses)
245 this._web_service_addresses.set (web_service,
246 web_service_address);
253 unowned string protocol = key;
254 var im_addresses = this._key_file.get_string_list (
255 this.display_id, protocol);
257 foreach (var im_address in im_addresses)
262 address = ImDetails.normalise_im_address (im_address,
265 catch (ImDetailsError e)
267 /* Warn of and ignore any invalid IM addresses */
272 this._im_addresses.set (protocol, address);
276 catch (KeyFileError e)
278 /* We get a GROUP_NOT_FOUND exception if we're creating a new
279 * Persona, since it doesn't yet exist in the key file. We shouldn't
280 * get any other exceptions, since we're iterating through a list of
281 * keys we've just retrieved. */
282 if (!(e is KeyFileError.GROUP_NOT_FOUND))
284 /* Translators: the parameter is an error message. */
285 warning (_("Couldn't load data from key file: %s"), e.message);
293 public override void linkable_property_to_links (string prop_name,
294 Folks.Persona.LinkablePropertyCallback callback)
296 if (prop_name == "im-addresses")
298 foreach (var protocol in this._im_addresses.get_keys ())
300 var im_addresses = this._im_addresses.get (protocol);
302 foreach (string address in im_addresses)
303 callback (protocol + ":" + address);
306 else if (prop_name == "web-service-addresses")
308 foreach (var web_service in this.web_service_addresses.get_keys ())
310 var web_service_addresses =
311 this._web_service_addresses.get (web_service);
313 foreach (string address in web_service_addresses)
314 callback (web_service + ":" + address);
320 base.linkable_property_to_links (prop_name, callback);