1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007-2010 Collabora Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Authors: Xavier Claessens <xclaesse@gmail.com>
20 * Travis Reitter <travis.reitter@collabora.co.uk>
27 #include <telepathy-glib/account-manager.h>
28 #include <telepathy-glib/enums.h>
29 #include <telepathy-glib/proxy-subclass.h>
30 #include <telepathy-glib/util.h>
32 #include <folks/folks.h>
34 #include <extensions/extensions.h>
36 #include "empathy-individual-manager.h"
37 #include "empathy-marshal.h"
38 #include "empathy-utils.h"
40 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
41 #include "empathy-debug.h"
43 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyIndividualManager)
45 /* This class only stores and refs Individuals who contain an EmpathyContact.
47 * This class merely forwards along signals from the aggregator and individuals
48 * and wraps aggregator functions for other client code. */
51 FolksIndividualAggregator *aggregator;
52 GHashTable *individuals; /* Individual.id -> Individual */
53 } EmpathyIndividualManagerPriv;
63 static guint signals[LAST_SIGNAL] = { 0 };
65 G_DEFINE_TYPE (EmpathyIndividualManager, empathy_individual_manager,
68 static EmpathyIndividualManager *manager_singleton = NULL;
71 individual_group_changed_cb (FolksIndividual *individual,
74 EmpathyIndividualManager *self)
76 g_signal_emit (self, signals[GROUPS_CHANGED], 0, individual, group,
81 individual_notify_is_favourite_cb (FolksIndividual *individual,
83 EmpathyIndividualManager *self)
85 gboolean is_favourite = folks_favouritable_get_is_favourite (
86 FOLKS_FAVOURITABLE (individual));
87 g_signal_emit (self, signals[FAVOURITES_CHANGED], 0, individual,
92 add_individual (EmpathyIndividualManager *self, FolksIndividual *individual)
94 EmpathyIndividualManagerPriv *priv = GET_PRIV (self);
96 g_hash_table_insert (priv->individuals,
97 (gpointer) folks_individual_get_id (individual),
98 g_object_ref (individual));
100 g_signal_connect (individual, "group-changed",
101 G_CALLBACK (individual_group_changed_cb), self);
102 g_signal_connect (individual, "notify::is-favourite",
103 G_CALLBACK (individual_notify_is_favourite_cb), self);
107 remove_individual (EmpathyIndividualManager *self, FolksIndividual *individual)
109 EmpathyIndividualManagerPriv *priv = GET_PRIV (self);
111 g_signal_handlers_disconnect_by_func (individual,
112 individual_group_changed_cb, self);
113 g_signal_handlers_disconnect_by_func (individual,
114 individual_notify_is_favourite_cb, self);
116 g_hash_table_remove (priv->individuals, folks_individual_get_id (individual));
119 /* This is emitted for *all* individuals in the individual aggregator (not
120 * just the ones we keep a reference to), to allow for the case where a new
121 * individual doesn't contain an EmpathyContact, but later has a persona added
124 individual_notify_personas_cb (FolksIndividual *individual,
126 EmpathyIndividualManager *self)
128 EmpathyIndividualManagerPriv *priv = GET_PRIV (self);
130 const gchar *id = folks_individual_get_id (individual);
131 gboolean has_contact = empathy_folks_individual_contains_contact (individual);
132 gboolean had_contact = (g_hash_table_lookup (priv->individuals,
133 id) != NULL) ? TRUE : FALSE;
135 if (had_contact == TRUE && has_contact == FALSE)
137 GList *removed = NULL;
139 /* The Individual has lost its EmpathyContact */
140 removed = g_list_prepend (removed, individual);
141 g_signal_emit (self, signals[MEMBERS_CHANGED], 0, NULL, NULL, removed,
142 TP_CHANNEL_GROUP_CHANGE_REASON_NONE /* FIXME */);
143 g_list_free (removed);
145 remove_individual (self, individual);
147 else if (had_contact == FALSE && has_contact == TRUE)
151 /* The Individual has gained its first EmpathyContact */
152 add_individual (self, individual);
154 added = g_list_prepend (added, individual);
155 g_signal_emit (self, signals[MEMBERS_CHANGED], 0, NULL, added, NULL,
156 TP_CHANNEL_GROUP_CHANGE_REASON_NONE /* FIXME */);
162 aggregator_individuals_changed_cb (FolksIndividualAggregator *aggregator,
168 EmpathyIndividualManager *self)
170 EmpathyIndividualManagerPriv *priv = GET_PRIV (self);
171 GList *l, *added_filtered = NULL;
173 /* Handle the removals first, as one of the added Individuals might have the
174 * same ID as one of the removed Individuals (due to linking). */
175 for (l = removed; l; l = l->next)
177 FolksIndividual *ind = FOLKS_INDIVIDUAL (l->data);
179 g_signal_handlers_disconnect_by_func (ind,
180 individual_notify_personas_cb, self);
182 if (g_hash_table_lookup (priv->individuals,
183 folks_individual_get_id (ind)) != NULL)
184 remove_individual (self, ind);
187 /* Filter the individuals for ones which contain EmpathyContacts */
188 for (l = added; l; l = l->next)
190 FolksIndividual *ind = FOLKS_INDIVIDUAL (l->data);
192 g_signal_connect (ind, "notify::personas",
193 G_CALLBACK (individual_notify_personas_cb), self);
195 if (empathy_folks_individual_contains_contact (ind) == TRUE)
197 add_individual (self, ind);
198 added_filtered = g_list_prepend (added_filtered, ind);
202 /* Bail if we have no individuals left */
203 if (added_filtered == NULL && removed == NULL)
206 added_filtered = g_list_reverse (added_filtered);
208 g_signal_emit (self, signals[MEMBERS_CHANGED], 0, message,
209 added_filtered, removed,
210 tp_channel_group_change_reason_from_folks_groups_change_reason (reason),
213 g_list_free (added_filtered);
217 individual_manager_dispose (GObject *object)
219 EmpathyIndividualManagerPriv *priv = GET_PRIV (object);
221 g_hash_table_destroy (priv->individuals);
222 tp_clear_object (&priv->aggregator);
224 G_OBJECT_CLASS (empathy_individual_manager_parent_class)->dispose (object);
228 individual_manager_constructor (GType type,
230 GObjectConstructParam *props)
234 if (manager_singleton)
236 retval = g_object_ref (manager_singleton);
241 G_OBJECT_CLASS (empathy_individual_manager_parent_class)->
242 constructor (type, n_props, props);
244 manager_singleton = EMPATHY_INDIVIDUAL_MANAGER (retval);
245 g_object_add_weak_pointer (retval, (gpointer) & manager_singleton);
252 * empathy_individual_manager_initialized:
254 * Reports whether or not the singleton has already been created.
256 * There can be instances where you want to access the #EmpathyIndividualManager
257 * only if it has been set up for this process.
259 * Returns: %TRUE if the #EmpathyIndividualManager singleton has previously
263 empathy_individual_manager_initialized (void)
265 return (manager_singleton != NULL);
269 empathy_individual_manager_class_init (EmpathyIndividualManagerClass *klass)
271 GObjectClass *object_class = G_OBJECT_CLASS (klass);
273 object_class->dispose = individual_manager_dispose;
274 object_class->constructor = individual_manager_constructor;
276 signals[GROUPS_CHANGED] =
277 g_signal_new ("groups-changed",
278 G_TYPE_FROM_CLASS (klass),
282 _empathy_marshal_VOID__OBJECT_STRING_BOOLEAN,
283 G_TYPE_NONE, 3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_STRING, G_TYPE_BOOLEAN);
285 signals[FAVOURITES_CHANGED] =
286 g_signal_new ("favourites-changed",
287 G_TYPE_FROM_CLASS (klass),
291 _empathy_marshal_VOID__OBJECT_BOOLEAN,
292 G_TYPE_NONE, 2, FOLKS_TYPE_INDIVIDUAL, G_TYPE_BOOLEAN);
294 signals[MEMBERS_CHANGED] =
295 g_signal_new ("members-changed",
296 G_TYPE_FROM_CLASS (klass),
300 _empathy_marshal_VOID__STRING_OBJECT_OBJECT_UINT,
302 4, G_TYPE_STRING, G_TYPE_POINTER, G_TYPE_POINTER, G_TYPE_UINT);
304 g_type_class_add_private (object_class,
305 sizeof (EmpathyIndividualManagerPriv));
309 empathy_individual_manager_init (EmpathyIndividualManager *self)
311 EmpathyIndividualManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
312 EMPATHY_TYPE_INDIVIDUAL_MANAGER, EmpathyIndividualManagerPriv);
315 priv->individuals = g_hash_table_new_full (g_str_hash, g_str_equal,
316 NULL, g_object_unref);
318 priv->aggregator = folks_individual_aggregator_new ();
319 g_signal_connect (priv->aggregator, "individuals-changed",
320 G_CALLBACK (aggregator_individuals_changed_cb), self);
321 folks_individual_aggregator_prepare (priv->aggregator, NULL, NULL);
324 EmpathyIndividualManager *
325 empathy_individual_manager_dup_singleton (void)
327 return g_object_new (EMPATHY_TYPE_INDIVIDUAL_MANAGER, NULL);
331 empathy_individual_manager_get_members (EmpathyIndividualManager *self)
333 EmpathyIndividualManagerPriv *priv = GET_PRIV (self);
335 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (self), NULL);
337 return g_hash_table_get_values (priv->individuals);
341 empathy_individual_manager_lookup_member (EmpathyIndividualManager *self,
344 EmpathyIndividualManagerPriv *priv = GET_PRIV (self);
346 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (self), NULL);
348 return g_hash_table_lookup (priv->individuals, id);
352 aggregator_add_persona_from_details_cb (GObject *source,
353 GAsyncResult *result,
356 FolksIndividualAggregator *aggregator = FOLKS_INDIVIDUAL_AGGREGATOR (source);
357 EmpathyContact *contact = EMPATHY_CONTACT (user_data);
358 FolksPersona *persona;
359 GError *error = NULL;
361 persona = folks_individual_aggregator_add_persona_from_details_finish (
362 aggregator, result, &error);
365 g_warning ("failed to add individual from contact: %s", error->message);
366 g_clear_error (&error);
369 /* The persona can be NULL even if there wasn't an error, if the persona was
370 * already in the contact list */
373 /* Set the contact's persona */
374 empathy_contact_set_persona (contact, persona);
375 g_object_unref (persona);
378 g_object_unref (contact);
382 empathy_individual_manager_add_from_contact (EmpathyIndividualManager *self,
383 EmpathyContact *contact)
385 EmpathyIndividualManagerPriv *priv;
388 const gchar *store_id;
390 g_return_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (self));
391 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
393 priv = GET_PRIV (self);
395 /* We need to ref the contact since otherwise its linked TpHandle will be
397 g_object_ref (contact);
399 DEBUG ("adding individual from contact %s (%s)",
400 empathy_contact_get_id (contact), empathy_contact_get_alias (contact));
402 account = empathy_contact_get_account (contact);
403 store_id = tp_proxy_get_object_path (TP_PROXY (account));
405 details = tp_asv_new (
406 "contact", G_TYPE_STRING, empathy_contact_get_id (contact),
409 folks_individual_aggregator_add_persona_from_details (
410 priv->aggregator, NULL, "telepathy", store_id, details,
411 aggregator_add_persona_from_details_cb, contact);
413 g_hash_table_destroy (details);
417 aggregator_remove_individual_cb (GObject *source,
418 GAsyncResult *result,
421 FolksIndividualAggregator *aggregator = FOLKS_INDIVIDUAL_AGGREGATOR (source);
422 GError *error = NULL;
424 folks_individual_aggregator_remove_individual_finish (
425 aggregator, result, &error);
428 g_warning ("failed to remove individual: %s", error->message);
429 g_clear_error (&error);
434 * Removes the inner contact from the server (and thus the Individual). Not
435 * meant for de-shelling inner personas from an Individual.
438 empathy_individual_manager_remove (EmpathyIndividualManager *self,
439 FolksIndividual *individual,
440 const gchar *message)
442 EmpathyIndividualManagerPriv *priv;
444 g_return_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (self));
445 g_return_if_fail (FOLKS_IS_INDIVIDUAL (individual));
447 priv = GET_PRIV (self);
449 DEBUG ("removing individual %s (%s)",
450 folks_individual_get_id (individual),
451 folks_aliasable_get_alias (FOLKS_ALIASABLE (individual)));
453 folks_individual_aggregator_remove_individual (priv->aggregator, individual,
454 aggregator_remove_individual_cb, self);
458 groups_change_group_cb (GObject *source,
459 GAsyncResult *result,
462 FolksGroupable *groupable = FOLKS_GROUPABLE (source);
463 GError *error = NULL;
465 folks_groupable_change_group_finish (groupable, result, &error);
468 g_warning ("failed to change group: %s", error->message);
469 g_clear_error (&error);
474 remove_group_cb (const gchar *id,
475 FolksIndividual *individual,
478 folks_groupable_change_group (FOLKS_GROUPABLE (individual), group, FALSE,
479 groups_change_group_cb, NULL);
483 empathy_individual_manager_remove_group (EmpathyIndividualManager *manager,
486 EmpathyIndividualManagerPriv *priv;
488 g_return_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (manager));
489 g_return_if_fail (group != NULL);
491 priv = GET_PRIV (manager);
493 DEBUG ("removing group %s", group);
495 /* Remove every individual from the group */
496 g_hash_table_foreach (priv->individuals, (GHFunc) remove_group_cb,
501 link_personas_cb (FolksIndividualAggregator *aggregator,
502 GAsyncResult *async_result,
505 GError *error = NULL;
507 folks_individual_aggregator_link_personas_finish (aggregator, async_result,
512 g_warning ("Failed to link personas: %s", error->message);
513 g_clear_error (&error);
518 empathy_individual_manager_link_personas (EmpathyIndividualManager *self,
521 EmpathyIndividualManagerPriv *priv;
523 g_return_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (self));
524 g_return_if_fail (personas != NULL);
526 priv = GET_PRIV (self);
528 DEBUG ("Linking %u personas", g_list_length (personas));
530 folks_individual_aggregator_link_personas (priv->aggregator, personas,
531 (GAsyncReadyCallback) link_personas_cb, NULL);
535 unlink_individual_cb (FolksIndividualAggregator *aggregator,
536 GAsyncResult *async_result,
539 GError *error = NULL;
541 folks_individual_aggregator_unlink_individual_finish (aggregator,
542 async_result, &error);
546 g_warning ("Failed to unlink individual: %s", error->message);
547 g_clear_error (&error);
552 empathy_individual_manager_unlink_individual (EmpathyIndividualManager *self,
553 FolksIndividual *individual)
555 EmpathyIndividualManagerPriv *priv;
557 g_return_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (self));
558 g_return_if_fail (FOLKS_IS_INDIVIDUAL (individual));
560 priv = GET_PRIV (self);
562 DEBUG ("Unlinking individual '%s'", folks_individual_get_id (individual));
564 folks_individual_aggregator_unlink_individual (priv->aggregator, individual,
565 (GAsyncReadyCallback) unlink_individual_cb, NULL);