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 * Travis Reitter <travis.reitter@collabora.co.uk>
26 * A physical person, aggregated from the various {@link Persona}s the person
27 * might have, such as their different IM addresses or vCard entries.
29 public class Folks.Individual : Object, Alias, Avatar, Capabilities, Groups,
32 private HashTable<string, bool> _groups;
33 private GLib.List<Persona> _personas;
34 private HashTable<PersonaStore, HashSet<Persona>> stores;
36 /* XXX: should setting this push it down into the Persona (to foward along to
37 * the actual store if possible?) */
41 public string alias { get; set; }
46 public File avatar { get; set; }
51 public CapabilitiesFlags capabilities { get; private set; }
56 public Folks.PresenceType presence_type { get; private set; }
61 public string presence_message { get; private set; }
64 * A unique identifier for the Individual.
66 * This uniquely identifies the Individual, and persists across
67 * {@link IndividualAggregator} instances.
69 * FIXME: Will this.id actually be the persistent ID for storage?
71 public string id { get; private set; }
74 * Emitted when the last of the Individual's {@link Persona}s has been
77 * At this point, the Individual is invalid, so any client referencing it
78 * should unreference it and remove it from their UI.
80 public signal void removed ();
85 public HashTable<string, bool> groups
87 get { return this._groups; }
89 /* Propagate the list of new groups to every Persona in the individual
90 * which implements the Groups interface */
93 this._personas.foreach ((p) =>
96 ((Groups) p).groups = value;
102 * The set of {@link Persona}s encapsulated by this Individual.
104 * Changing the set of personas may cause updates to the aggregated properties
105 * provided by the Individual, resulting in property notifications for them.
107 public GLib.List<Persona> personas
109 get { return this._personas; }
113 /* Disconnect from all our previous personas */
114 this._personas.foreach ((p) =>
116 var persona = (Persona) p;
117 var groups = (p is Groups) ? (Groups) p : null;
119 persona.notify["avatar"].disconnect (this.notify_avatar_cb);
120 persona.notify["presence-message"].disconnect (
121 this.notify_presence_cb);
122 persona.notify["presence-type"].disconnect (
123 this.notify_presence_cb);
124 groups.group_changed.disconnect (this.persona_group_changed_cb);
127 this._personas = value.copy ();
129 /* If all the personas have been removed, remove the individual */
130 if (this._personas.length () < 1)
136 /* TODO: base this upon our ID in permanent storage, once we have that
138 if (this.id == null && this._personas.data != null)
139 this.id = this._personas.data.iid;
141 /* Connect to all the new personas */
142 this._personas.foreach ((p) =>
144 var persona = (Persona) p;
145 var groups = (p is Groups) ? (Groups) p : null;
147 persona.notify["avatar"].connect (this.notify_avatar_cb);
148 persona.notify["presence-message"].connect (
149 this.notify_presence_cb);
150 persona.notify["presence-type"].connect (this.notify_presence_cb);
151 groups.group_changed.connect (this.persona_group_changed_cb);
154 /* Update our aggregated fields and notify the changes */
155 this.update_fields ();
159 private void notify_avatar_cb (Object obj, ParamSpec ps)
161 this.update_avatar ();
164 private void persona_group_changed_cb (string group, bool is_member)
166 this.change_group (group, is_member);
167 this.update_groups ();
171 * Add or remove the Individual from the specified group.
173 * If `is_member` is `true`, the Individual will be added to the `group`. If
174 * it is `false`, they will be removed from the `group`.
176 * The group membership change will propagate to every {@link Persona} in
179 * @param group a freeform group identifier
180 * @param is_member whether the Individual should be a member of the group
182 public void change_group (string group, bool is_member)
184 this._personas.foreach ((p) =>
187 ((Groups) p).change_group (group, is_member);
190 /* don't notify, since it hasn't happened in the persona backing stores
191 * yet; react to that directly */
194 private void notify_presence_cb (Object obj, ParamSpec ps)
196 this.update_presence ();
200 * Create a new Individual.
202 * The Individual can optionally be seeded with the {@link Persona}s in
203 * `personas`. Otherwise, it will have to have personas added using the
204 * {@link Folks.Individual.personas} property after construction.
206 * @return a new Individual
208 public Individual (GLib.List<Persona>? personas)
210 Object (personas: personas);
212 this.stores = new HashTable<PersonaStore, HashSet<Persona>> (direct_hash,
214 this.stores_update ();
217 private void stores_update ()
219 this._personas.foreach ((p) =>
221 var persona = (Persona) p;
222 var store_is_new = false;
223 var persona_set = this.stores.lookup (persona.store);
224 if (persona_set == null)
226 persona_set = new HashSet<Persona> (direct_hash, direct_equal);
230 persona_set.add (persona);
234 this.stores.insert (persona.store, persona_set);
236 persona.store.removed.connect (this.store_removed_cb);
237 persona.store.personas_removed.connect (
238 this.store_personas_removed_cb);
243 private void store_removed_cb (PersonaStore store)
245 var persona_set = this.stores.lookup (store);
246 if (persona_set != null)
248 foreach (var persona in persona_set)
250 this._personas.remove (persona);
254 this.stores.remove (store);
256 if (this._personas.length () < 1 || this.stores.size () < 1)
262 this.update_fields ();
265 private void store_personas_removed_cb (PersonaStore store,
266 GLib.List<Persona> personas)
268 personas.foreach ((data) =>
270 this._personas.remove ((Persona) data);
273 if (this._personas.length () < 1)
279 this.update_fields ();
282 private void update_fields ()
284 /* Gather the first occurrence of each field. We assume that there is
285 * at least one persona in the list, since the Individual should've been
286 * destroyed before now otherwise. */
288 var caps = CapabilitiesFlags.NONE;
289 this._personas.foreach ((persona) =>
291 var p = (Persona) persona;
293 /* FIXME: also check to see if alias is just whitespace */
297 caps |= p.capabilities;
302 /* We have to pick a UID, since none of the personas have an alias
303 * available. Pick the UID from the first persona in the list. */
304 alias = this._personas.data.uid;
305 warning ("No aliases available for individual; using UID instead: %s",
309 /* only notify if the value has changed */
310 if (this.alias != alias)
313 if (this.capabilities != caps)
314 this.capabilities = caps;
316 this.update_groups ();
317 this.update_presence ();
318 this.update_avatar ();
321 private void update_groups ()
323 var new_groups = new HashTable<string, bool> (str_hash, str_equal);
325 /* this._groups is null during initial construction */
326 if (this._groups == null)
327 this._groups = new HashTable<string, bool> (str_hash, str_equal);
329 /* FIXME: this should partition the personas by store (maybe we should
330 * keep that mapping in general in this class), and execute
331 * "groups-changed" on the store (with the set of personas), to allow the
332 * back-end to optimize it (like Telepathy will for MembersChanged for the
333 * groups channel list) */
334 this._personas.foreach ((p) =>
338 var persona = (Groups) p;
340 persona.groups.foreach ((k, v) =>
342 new_groups.insert ((string) k, true);
347 new_groups.foreach ((k, v) =>
349 var group = (string) k;
350 if (this._groups.lookup (group) != true)
352 this._groups.insert (group, true);
353 this._groups.foreach ((k, v) =>
359 this.group_changed (group, true);
363 /* buffer the removals, so we don't remove while iterating */
364 var removes = new GLib.List<string> ();
365 this._groups.foreach ((k, v) =>
367 var group = (string) k;
368 if (new_groups.lookup (group) != true)
369 removes.prepend (group);
372 removes.foreach ((l) =>
374 var group = (string) l;
375 this._groups.remove (group);
376 this.group_changed (group, false);
380 private void update_presence ()
382 var presence_message = "";
383 var presence_type = Folks.PresenceType.UNSET;
385 /* Choose the most available presence from our personas */
386 this._personas.foreach ((p) =>
388 var persona = (Persona) p;
390 if (Presence.typecmp (persona.presence_type, presence_type) > 0)
392 presence_type = persona.presence_type;
393 presence_message = persona.presence_message;
397 if (presence_message == null)
398 presence_message = "";
400 /* only notify if the value has changed */
401 if (this.presence_message != presence_message)
402 this.presence_message = presence_message;
404 if (this.presence_type != presence_type)
405 this.presence_type = presence_type;
408 private void update_avatar ()
412 this._personas.foreach ((p) =>
414 var persona = (Persona) p;
418 avatar = persona.avatar;
423 /* only notify if the value has changed */
424 if (this.avatar != avatar)
425 this.avatar = avatar;
429 * Get a bitmask of the capabilities of this Individual.
431 * The capabilities is the union of the sets of capabilities of all the
432 * {@link Persona}s in the Individual.
434 * @return bitmask of the Individual's capabilities
436 public CapabilitiesFlags get_capabilities ()
438 return this.capabilities;
442 * GLib/C convenience functions (for built-in casting, etc.)
446 * Get the Individual's alias.
448 * The alias is a user-chosen name for the Individual; how the user wants that
449 * Individual to be represented in UIs.
451 * @return the Individual's alias
453 public unowned string get_alias ()
459 * Get a mapping of group ID to whether the Individual is a member.
461 * Freeform group IDs are mapped to a boolean which is `true` if the
462 * Individual is a member of the group, and `false` otherwise.
464 * @return a mapping of group ID to membership status
466 public HashTable<string, bool> get_groups ()
473 * Get the Individual's current presence message.
475 * The presence message returned is from the same {@link Persona} which
476 * provided the presence type returned by
477 * {@link Individual.get_presence_type}.
479 * If none of the {@link Persona}s in the Individual have a presence message
480 * set, an empty string is returned.
482 * @return the Individual's presence message
484 public unowned string get_presence_message ()
486 return this.presence_message;
490 * Get the Individual's current presence type.
492 * The presence type returned is from the same {@link Persona} which provided
493 * the presence message returned by {@link Individual.get_presence_message}.
495 * If none of the {@link Persona}s in the Individual have a presence type set,
496 * {@link PresenceType.UNSET} is returned.
498 * @return the Individual's presence type
500 public Folks.PresenceType get_presence_type ()
502 return this.presence_type;
506 * Whether the Individual is online.
508 * This will be `true` if any of the Individual's {@link Persona}s have a
509 * presence type higher than {@link PresenceType.OFFLINE}, as determined by
510 * {@link Presence.typecmp}.
512 * @return `true` if the Individual is online, `false` otherwise
514 public bool is_online ()
517 return p.is_online ();