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>
33 #include <folks/folks-telepathy.h>
35 #include <extensions/extensions.h>
37 #include "empathy-individual-manager.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_favourite_details_get_is_favourite (
86 FOLKS_FAVOURITE_DETAILS (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 g_strdup (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,
163 GeeMultiMap *changes,
164 EmpathyIndividualManager *self)
166 EmpathyIndividualManagerPriv *priv = GET_PRIV (self);
169 GeeCollection *added;
170 GList *added_set = NULL, *added_filtered = NULL, *removed_list = NULL;
172 /* We're not interested in the relationships between the added and removed
173 * individuals, so just extract collections of them. Note that the added
174 * collection may contain duplicates, while the removed set won't. */
175 removed = gee_multi_map_get_keys (changes);
176 added = gee_multi_map_get_values (changes);
178 /* Handle the removals first, as one of the added Individuals might have the
179 * same ID as one of the removed Individuals (due to linking). */
180 iter = gee_iterable_iterator (GEE_ITERABLE (removed));
181 while (gee_iterator_next (iter))
183 FolksIndividual *ind = gee_iterator_get (iter);
188 g_signal_handlers_disconnect_by_func (ind,
189 individual_notify_personas_cb, self);
191 if (g_hash_table_lookup (priv->individuals,
192 folks_individual_get_id (ind)) != NULL)
194 remove_individual (self, ind);
195 removed_list = g_list_prepend (removed_list, ind);
198 g_clear_object (&ind);
200 g_clear_object (&iter);
202 /* Filter the individuals for ones which contain EmpathyContacts */
203 iter = gee_iterable_iterator (GEE_ITERABLE (added));
204 while (gee_iterator_next (iter))
206 FolksIndividual *ind = gee_iterator_get (iter);
208 /* Make sure we handle each added individual only once. */
209 if (ind == NULL || g_list_find (added_set, ind) != NULL)
211 added_set = g_list_prepend (added_set, ind);
213 g_signal_connect (ind, "notify::personas",
214 G_CALLBACK (individual_notify_personas_cb), self);
216 if (empathy_folks_individual_contains_contact (ind) == TRUE)
218 add_individual (self, ind);
219 added_filtered = g_list_prepend (added_filtered, ind);
222 g_clear_object (&ind);
224 g_clear_object (&iter);
226 g_list_free (added_set);
228 g_object_unref (added);
229 g_object_unref (removed);
231 /* Bail if we have no individuals left */
232 if (added_filtered == NULL && removed == NULL)
235 added_filtered = g_list_reverse (added_filtered);
237 g_signal_emit (self, signals[MEMBERS_CHANGED], 0, NULL,
238 added_filtered, removed_list,
239 TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
242 g_list_free (added_filtered);
243 g_list_free (removed_list);
247 individual_manager_dispose (GObject *object)
249 EmpathyIndividualManagerPriv *priv = GET_PRIV (object);
251 g_hash_table_unref (priv->individuals);
253 g_signal_handlers_disconnect_by_func (priv->aggregator,
254 aggregator_individuals_changed_cb, object);
255 tp_clear_object (&priv->aggregator);
257 G_OBJECT_CLASS (empathy_individual_manager_parent_class)->dispose (object);
261 individual_manager_constructor (GType type,
263 GObjectConstructParam *props)
267 if (manager_singleton)
269 retval = g_object_ref (manager_singleton);
274 G_OBJECT_CLASS (empathy_individual_manager_parent_class)->
275 constructor (type, n_props, props);
277 manager_singleton = EMPATHY_INDIVIDUAL_MANAGER (retval);
278 g_object_add_weak_pointer (retval, (gpointer) & manager_singleton);
285 * empathy_individual_manager_initialized:
287 * Reports whether or not the singleton has already been created.
289 * There can be instances where you want to access the #EmpathyIndividualManager
290 * only if it has been set up for this process.
292 * Returns: %TRUE if the #EmpathyIndividualManager singleton has previously
296 empathy_individual_manager_initialized (void)
298 return (manager_singleton != NULL);
302 empathy_individual_manager_class_init (EmpathyIndividualManagerClass *klass)
304 GObjectClass *object_class = G_OBJECT_CLASS (klass);
306 object_class->dispose = individual_manager_dispose;
307 object_class->constructor = individual_manager_constructor;
309 signals[GROUPS_CHANGED] =
310 g_signal_new ("groups-changed",
311 G_TYPE_FROM_CLASS (klass),
315 g_cclosure_marshal_generic,
316 G_TYPE_NONE, 3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_STRING, G_TYPE_BOOLEAN);
318 signals[FAVOURITES_CHANGED] =
319 g_signal_new ("favourites-changed",
320 G_TYPE_FROM_CLASS (klass),
324 g_cclosure_marshal_generic,
325 G_TYPE_NONE, 2, FOLKS_TYPE_INDIVIDUAL, G_TYPE_BOOLEAN);
327 signals[MEMBERS_CHANGED] =
328 g_signal_new ("members-changed",
329 G_TYPE_FROM_CLASS (klass),
333 g_cclosure_marshal_generic,
335 4, G_TYPE_STRING, G_TYPE_POINTER, G_TYPE_POINTER, G_TYPE_UINT);
337 g_type_class_add_private (object_class,
338 sizeof (EmpathyIndividualManagerPriv));
342 empathy_individual_manager_init (EmpathyIndividualManager *self)
344 EmpathyIndividualManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
345 EMPATHY_TYPE_INDIVIDUAL_MANAGER, EmpathyIndividualManagerPriv);
348 priv->individuals = g_hash_table_new_full (g_str_hash, g_str_equal,
349 g_free, g_object_unref);
351 priv->aggregator = folks_individual_aggregator_new ();
352 g_signal_connect (priv->aggregator, "individuals-changed-detailed",
353 G_CALLBACK (aggregator_individuals_changed_cb), self);
354 folks_individual_aggregator_prepare (priv->aggregator, NULL, NULL);
357 EmpathyIndividualManager *
358 empathy_individual_manager_dup_singleton (void)
360 return g_object_new (EMPATHY_TYPE_INDIVIDUAL_MANAGER, NULL);
364 empathy_individual_manager_get_members (EmpathyIndividualManager *self)
366 EmpathyIndividualManagerPriv *priv = GET_PRIV (self);
368 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (self), NULL);
370 return g_hash_table_get_values (priv->individuals);
374 empathy_individual_manager_lookup_member (EmpathyIndividualManager *self,
377 EmpathyIndividualManagerPriv *priv = GET_PRIV (self);
379 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (self), NULL);
381 return g_hash_table_lookup (priv->individuals, id);
385 aggregator_add_persona_from_details_cb (GObject *source,
386 GAsyncResult *result,
389 FolksIndividualAggregator *aggregator = FOLKS_INDIVIDUAL_AGGREGATOR (source);
390 EmpathyContact *contact = EMPATHY_CONTACT (user_data);
391 FolksPersona *persona;
392 GError *error = NULL;
394 persona = folks_individual_aggregator_add_persona_from_details_finish (
395 aggregator, result, &error);
398 g_warning ("failed to add individual from contact: %s", error->message);
399 g_clear_error (&error);
402 /* The persona can be NULL even if there wasn't an error, if the persona was
403 * already in the contact list */
406 /* Set the contact's persona */
407 empathy_contact_set_persona (contact, persona);
408 g_object_unref (persona);
411 g_object_unref (contact);
415 empathy_individual_manager_add_from_contact (EmpathyIndividualManager *self,
416 EmpathyContact *contact)
418 EmpathyIndividualManagerPriv *priv;
419 FolksBackendStore *backend_store;
420 FolksBackend *backend;
421 FolksPersonaStore *persona_store;
423 GeeMap *persona_stores;
425 const gchar *store_id;
427 g_return_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (self));
428 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
430 priv = GET_PRIV (self);
432 /* We need to ref the contact since otherwise its linked TpHandle will be
434 g_object_ref (contact);
436 DEBUG ("adding individual from contact %s (%s)",
437 empathy_contact_get_id (contact), empathy_contact_get_alias (contact));
439 account = empathy_contact_get_account (contact);
440 store_id = tp_proxy_get_object_path (TP_PROXY (account));
442 /* Get the persona store to use */
443 backend_store = folks_backend_store_dup ();
445 folks_backend_store_dup_backend_by_name (backend_store, "telepathy");
449 g_warning ("Failed to add individual from contact: couldn't get "
450 "'telepathy' backend");
454 persona_stores = folks_backend_get_persona_stores (backend);
455 persona_store = gee_map_get (persona_stores, store_id);
457 if (persona_store == NULL)
459 g_warning ("Failed to add individual from contact: couldn't get persona "
460 "store '%s'", store_id);
464 details = tp_asv_new (
465 "contact", G_TYPE_STRING, empathy_contact_get_id (contact),
468 folks_individual_aggregator_add_persona_from_details (
469 priv->aggregator, NULL, persona_store, details,
470 aggregator_add_persona_from_details_cb, contact);
472 g_hash_table_unref (details);
473 g_object_unref (persona_store);
476 tp_clear_object (&backend);
477 tp_clear_object (&backend_store);
481 aggregator_remove_individual_cb (GObject *source,
482 GAsyncResult *result,
485 FolksIndividualAggregator *aggregator = FOLKS_INDIVIDUAL_AGGREGATOR (source);
486 GError *error = NULL;
488 folks_individual_aggregator_remove_individual_finish (
489 aggregator, result, &error);
492 g_warning ("failed to remove individual: %s", error->message);
493 g_clear_error (&error);
498 * Removes the inner contact from the server (and thus the Individual). Not
499 * meant for de-shelling inner personas from an Individual.
502 empathy_individual_manager_remove (EmpathyIndividualManager *self,
503 FolksIndividual *individual,
504 const gchar *message)
506 EmpathyIndividualManagerPriv *priv;
508 g_return_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (self));
509 g_return_if_fail (FOLKS_IS_INDIVIDUAL (individual));
511 priv = GET_PRIV (self);
513 DEBUG ("removing individual %s (%s)",
514 folks_individual_get_id (individual),
515 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual)));
517 folks_individual_aggregator_remove_individual (priv->aggregator, individual,
518 aggregator_remove_individual_cb, self);
521 /* FIXME: The parameter @self is not required and the method can be placed in
522 * utilities. I left it as it is to stay coherent with empathy-2.34 */
524 * empathy_individual_manager_supports_blocking
525 * @self: the #EmpathyIndividualManager
526 * @individual: an individual to check
528 * Indicates whether any personas of an @individual can be blocked.
530 * Returns: %TRUE if any persona supports contact blocking
533 empathy_individual_manager_supports_blocking (EmpathyIndividualManager *self,
534 FolksIndividual *individual)
538 gboolean retval = FALSE;
540 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (self), FALSE);
542 personas = folks_individual_get_personas (individual);
543 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
544 while (!retval && gee_iterator_next (iter))
546 TpfPersona *persona = gee_iterator_get (iter);
549 if (TPF_IS_PERSONA (persona))
551 TpContact *tp_contact;
553 tp_contact = tpf_persona_get_contact (persona);
554 if (tp_contact != NULL)
556 conn = tp_contact_get_connection (tp_contact);
558 if (tp_proxy_has_interface_by_id (conn,
559 TP_IFACE_QUARK_CONNECTION_INTERFACE_CONTACT_BLOCKING))
563 g_clear_object (&persona);
565 g_clear_object (&iter);
571 empathy_individual_manager_set_blocked (EmpathyIndividualManager *self,
572 FolksIndividual *individual,
579 g_return_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (self));
581 personas = folks_individual_get_personas (individual);
582 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
583 while (gee_iterator_next (iter))
585 TpfPersona *persona = gee_iterator_get (iter);
587 if (TPF_IS_PERSONA (persona))
589 TpContact *tp_contact;
592 tp_contact = tpf_persona_get_contact (persona);
593 if (tp_contact == NULL)
596 conn = tp_contact_get_connection (tp_contact);
598 if (!tp_proxy_has_interface_by_id (conn,
599 TP_IFACE_QUARK_CONNECTION_INTERFACE_CONTACT_BLOCKING))
603 tp_contact_block_async (tp_contact, abusive, NULL, NULL);
605 tp_contact_unblock_async (tp_contact, NULL, NULL);
607 g_clear_object (&persona);
609 g_clear_object (&iter);
613 groups_change_group_cb (GObject *source,
614 GAsyncResult *result,
617 FolksGroupDetails *group_details = FOLKS_GROUP_DETAILS (source);
618 GError *error = NULL;
620 folks_group_details_change_group_finish (group_details, result, &error);
623 g_warning ("failed to change group: %s", error->message);
624 g_clear_error (&error);
629 remove_group_cb (const gchar *id,
630 FolksIndividual *individual,
633 folks_group_details_change_group (FOLKS_GROUP_DETAILS (individual), group,
634 FALSE, groups_change_group_cb, NULL);
638 empathy_individual_manager_remove_group (EmpathyIndividualManager *manager,
641 EmpathyIndividualManagerPriv *priv;
643 g_return_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (manager));
644 g_return_if_fail (group != NULL);
646 priv = GET_PRIV (manager);
648 DEBUG ("removing group %s", group);
650 /* Remove every individual from the group */
651 g_hash_table_foreach (priv->individuals, (GHFunc) remove_group_cb,
656 link_personas_cb (FolksIndividualAggregator *aggregator,
657 GAsyncResult *async_result,
660 GError *error = NULL;
662 folks_individual_aggregator_link_personas_finish (aggregator, async_result,
667 g_warning ("Failed to link personas: %s", error->message);
668 g_clear_error (&error);
673 empathy_individual_manager_link_personas (EmpathyIndividualManager *self,
676 EmpathyIndividualManagerPriv *priv;
678 g_return_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (self));
679 g_return_if_fail (personas != NULL);
681 priv = GET_PRIV (self);
683 DEBUG ("Linking %u personas",
684 gee_collection_get_size (GEE_COLLECTION (personas)));
686 folks_individual_aggregator_link_personas (priv->aggregator, personas,
687 (GAsyncReadyCallback) link_personas_cb, NULL);
691 unlink_individual_cb (FolksIndividualAggregator *aggregator,
692 GAsyncResult *async_result,
695 GError *error = NULL;
697 folks_individual_aggregator_unlink_individual_finish (aggregator,
698 async_result, &error);
702 g_warning ("Failed to unlink individual: %s", error->message);
703 g_clear_error (&error);
708 empathy_individual_manager_unlink_individual (EmpathyIndividualManager *self,
709 FolksIndividual *individual)
711 EmpathyIndividualManagerPriv *priv;
713 g_return_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (self));
714 g_return_if_fail (FOLKS_IS_INDIVIDUAL (individual));
716 priv = GET_PRIV (self);
718 DEBUG ("Unlinking individual '%s'", folks_individual_get_id (individual));
720 folks_individual_aggregator_unlink_individual (priv->aggregator, individual,
721 (GAsyncReadyCallback) unlink_individual_cb, NULL);