]> git.0d.be Git - empathy.git/blob - libempathy/empathy-individual-manager.c
include telepathy-glib.h
[empathy.git] / libempathy / empathy-individual-manager.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2007-2010 Collabora Ltd.
4  *
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.
9  *
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.
14  *
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
18  *
19  * Authors: Xavier Claessens <xclaesse@gmail.com>
20  *          Travis Reitter <travis.reitter@collabora.co.uk>
21  */
22
23 #include <config.h>
24
25 #include <string.h>
26
27 #include <telepathy-glib/telepathy-glib.h>
28
29 #include <folks/folks.h>
30 #include <folks/folks-telepathy.h>
31
32 #include <extensions/extensions.h>
33
34 #include "empathy-individual-manager.h"
35 #include "empathy-utils.h"
36
37 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
38 #include "empathy-debug.h"
39
40 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyIndividualManager)
41
42 /* We just expose the $TOP_INDIVIDUALS_LEN more popular individuals as that's
43  * what the view actually care about. We just want to notify it when this list
44  * changes, not when the position of every single individual is updated. */
45 #define TOP_INDIVIDUALS_LEN 5
46
47 /* The constant INDIVIDUALS_COUNT_COMPRESS_FACTOR represents the number of
48  * interactions needed to be considered as 1 interaction */
49 #define INTERACTION_COUNT_COMPRESS_FACTOR 50
50
51 /* The constant DAY_IN_SECONDS represents the seconds in a day */
52 #define DAY_IN_SECONDS 86400
53
54 /* This class only stores and refs Individuals who contain an EmpathyContact.
55  *
56  * This class merely forwards along signals from the aggregator and individuals
57  * and wraps aggregator functions for other client code. */
58 typedef struct
59 {
60   FolksIndividualAggregator *aggregator;
61   GHashTable *individuals; /* Individual.id -> Individual */
62   gboolean contacts_loaded;
63
64   /* reffed FolksIndividual sorted by popularity (most popular first) */
65   GSequence *individuals_pop;
66   /* The TOP_INDIVIDUALS_LEN first FolksIndividual (borrowed) from
67    * individuals_pop */
68   GList *top_individuals;
69   guint global_interaction_counter;
70 } EmpathyIndividualManagerPriv;
71
72 enum
73 {
74   PROP_TOP_INDIVIDUALS = 1,
75   N_PROPS
76 };
77
78 enum
79 {
80   FAVOURITES_CHANGED,
81   GROUPS_CHANGED,
82   MEMBERS_CHANGED,
83   CONTACTS_LOADED,
84   LAST_SIGNAL
85 };
86
87 static guint signals[LAST_SIGNAL] = { 0 };
88
89 G_DEFINE_TYPE (EmpathyIndividualManager, empathy_individual_manager,
90     G_TYPE_OBJECT);
91
92 static EmpathyIndividualManager *manager_singleton = NULL;
93
94 static void
95 individual_manager_get_property (GObject *object,
96     guint property_id,
97     GValue *value,
98     GParamSpec *pspec)
99 {
100   EmpathyIndividualManager *self = EMPATHY_INDIVIDUAL_MANAGER (object);
101   EmpathyIndividualManagerPriv *priv = GET_PRIV (self);
102
103   switch (property_id)
104     {
105       case PROP_TOP_INDIVIDUALS:
106         g_value_set_pointer (value, priv->top_individuals);
107       default:
108         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
109         break;
110     }
111 }
112
113 static void
114 individual_group_changed_cb (FolksIndividual *individual,
115     gchar *group,
116     gboolean is_member,
117     EmpathyIndividualManager *self)
118 {
119   g_signal_emit (self, signals[GROUPS_CHANGED], 0, individual, group,
120       is_member);
121 }
122
123 static void
124 individual_notify_is_favourite_cb (FolksIndividual *individual,
125     GParamSpec *pspec,
126     EmpathyIndividualManager *self)
127 {
128   gboolean is_favourite = folks_favourite_details_get_is_favourite (
129       FOLKS_FAVOURITE_DETAILS (individual));
130   g_signal_emit (self, signals[FAVOURITES_CHANGED], 0, individual,
131       is_favourite);
132 }
133
134
135 /* Contacts that have been interacted with within the last 30 days and have
136  * have an interaction count > INTERACTION_COUNT_COMPRESS_FACTOR have a
137  * popularity value of the count/INTERACTION_COUNT_COMPRESS_FACTOR */
138 static guint
139 compute_popularity (FolksIndividual *individual)
140 {
141   FolksInteractionDetails *details = FOLKS_INTERACTION_DETAILS (individual);
142   GDateTime *last;
143   guint  current_timestamp, count;
144   float timediff;
145
146   last = folks_interaction_details_get_last_im_interaction_datetime (details);
147   if (last == NULL)
148     return 0;
149
150   /* Convert g_get_real_time () fro microseconds to seconds */
151   current_timestamp = g_get_real_time () / 1000000;
152   timediff = current_timestamp - g_date_time_to_unix (last);
153
154   if (timediff / DAY_IN_SECONDS > 30)
155     return 0;
156
157   count = folks_interaction_details_get_im_interaction_count (details);
158   count = count / INTERACTION_COUNT_COMPRESS_FACTOR;
159   if (count == 0)
160     return 0;
161
162   return count;
163 }
164
165 static void
166 check_top_individuals (EmpathyIndividualManager *self)
167 {
168   EmpathyIndividualManagerPriv *priv = GET_PRIV (self);
169   GSequenceIter *iter;
170   GList *l, *new_list = NULL;
171   gboolean modified = FALSE;
172   guint i;
173
174   iter = g_sequence_get_begin_iter (priv->individuals_pop);
175   l = priv->top_individuals;
176
177   /* Check if the TOP_INDIVIDUALS_LEN first individuals in individuals_pop are
178    * still the same as the ones in top_individuals */
179   for (i = 0; i < TOP_INDIVIDUALS_LEN && !g_sequence_iter_is_end (iter); i++)
180     {
181       FolksIndividual *individual = g_sequence_get (iter);
182       guint pop;
183
184       /* Don't include individual having 0 as pop */
185       pop = compute_popularity (individual);
186       if (pop <= 0)
187         break;
188
189       if (!modified)
190         {
191           if (l == NULL)
192             {
193               /* Old list is shorter than the new one */
194               modified = TRUE;
195             }
196           else
197             {
198               modified = (individual != l->data);
199
200               l = g_list_next (l);
201             }
202         }
203
204       new_list = g_list_prepend (new_list, individual);
205
206       iter = g_sequence_iter_next (iter);
207     }
208
209   g_list_free (priv->top_individuals);
210   priv->top_individuals = g_list_reverse (new_list);
211
212   if (modified)
213     {
214       DEBUG ("Top individuals changed:");
215
216       for (l = priv->top_individuals; l != NULL; l = g_list_next (l))
217         {
218           FolksIndividual *individual = l->data;
219
220           DEBUG ("  %s (%u)",
221               folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual)),
222               compute_popularity (individual));
223         }
224
225       g_object_notify (G_OBJECT (self), "top-individuals");
226     }
227 }
228
229 static gint
230 compare_individual_by_pop (gconstpointer a,
231     gconstpointer b,
232     gpointer user_data)
233 {
234   guint pop_a, pop_b;
235
236   pop_a = compute_popularity (FOLKS_INDIVIDUAL (a));
237   pop_b = compute_popularity (FOLKS_INDIVIDUAL (b));
238
239   return pop_b - pop_a;
240 }
241
242 static void
243 individual_notify_im_interaction_count (FolksIndividual *individual,
244     GParamSpec *pspec,
245     EmpathyIndividualManager *self)
246 {
247   EmpathyIndividualManagerPriv *priv = GET_PRIV (self);
248
249   /* We don't use g_sequence_sort_changed() because we'll first have to find
250    * the iter of @individual using g_sequence_lookup() but the lookup function
251    * won't work as it assumes that the sequence is sorted which is no longer
252    * the case at this point as @individual's popularity just changed. */
253   g_sequence_sort (priv->individuals_pop, compare_individual_by_pop, NULL);
254
255   /* Only check for top individuals after 10 interaction events happen */
256   if (priv->global_interaction_counter % 10 == 0)
257     check_top_individuals (self);
258   priv->global_interaction_counter++;
259 }
260
261 static void
262 add_individual (EmpathyIndividualManager *self, FolksIndividual *individual)
263 {
264   EmpathyIndividualManagerPriv *priv = GET_PRIV (self);
265
266   g_hash_table_insert (priv->individuals,
267       g_strdup (folks_individual_get_id (individual)),
268       g_object_ref (individual));
269
270   g_sequence_insert_sorted (priv->individuals_pop, g_object_ref (individual),
271       compare_individual_by_pop, NULL);
272   check_top_individuals (self);
273
274   g_signal_connect (individual, "group-changed",
275       G_CALLBACK (individual_group_changed_cb), self);
276   g_signal_connect (individual, "notify::is-favourite",
277       G_CALLBACK (individual_notify_is_favourite_cb), self);
278   g_signal_connect (individual, "notify::im-interaction-count",
279       G_CALLBACK (individual_notify_im_interaction_count), self);
280 }
281
282 static void
283 remove_individual (EmpathyIndividualManager *self, FolksIndividual *individual)
284 {
285   EmpathyIndividualManagerPriv *priv = GET_PRIV (self);
286   GSequenceIter *iter;
287
288   iter = g_sequence_lookup (priv->individuals_pop, individual,
289       compare_individual_by_pop, NULL);
290   if (iter != NULL)
291     {
292       g_sequence_remove (iter);
293       check_top_individuals (self);
294     }
295
296   g_signal_handlers_disconnect_by_func (individual,
297       individual_group_changed_cb, self);
298   g_signal_handlers_disconnect_by_func (individual,
299       individual_notify_is_favourite_cb, self);
300   g_signal_handlers_disconnect_by_func (individual,
301       individual_notify_im_interaction_count, self);
302
303   g_hash_table_remove (priv->individuals, folks_individual_get_id (individual));
304 }
305
306 /* This is emitted for *all* individuals in the individual aggregator (not
307  * just the ones we keep a reference to), to allow for the case where a new
308  * individual doesn't contain an EmpathyContact, but later has a persona added
309  * which does. */
310 static void
311 individual_notify_personas_cb (FolksIndividual *individual,
312     GParamSpec *pspec,
313     EmpathyIndividualManager *self)
314 {
315   EmpathyIndividualManagerPriv *priv = GET_PRIV (self);
316
317   const gchar *id = folks_individual_get_id (individual);
318   gboolean has_contact = empathy_folks_individual_contains_contact (individual);
319   gboolean had_contact = (g_hash_table_lookup (priv->individuals,
320       id) != NULL) ? TRUE : FALSE;
321
322   if (had_contact == TRUE && has_contact == FALSE)
323     {
324       GList *removed = NULL;
325
326       /* The Individual has lost its EmpathyContact */
327       removed = g_list_prepend (removed, individual);
328       g_signal_emit (self, signals[MEMBERS_CHANGED], 0, NULL, NULL, removed,
329           TP_CHANNEL_GROUP_CHANGE_REASON_NONE /* FIXME */);
330       g_list_free (removed);
331
332       remove_individual (self, individual);
333     }
334   else if (had_contact == FALSE && has_contact == TRUE)
335     {
336       GList *added = NULL;
337
338       /* The Individual has gained its first EmpathyContact */
339       add_individual (self, individual);
340
341       added = g_list_prepend (added, individual);
342       g_signal_emit (self, signals[MEMBERS_CHANGED], 0, NULL, added, NULL,
343           TP_CHANNEL_GROUP_CHANGE_REASON_NONE /* FIXME */);
344       g_list_free (added);
345     }
346 }
347
348 static void
349 aggregator_individuals_changed_cb (FolksIndividualAggregator *aggregator,
350     GeeMultiMap *changes,
351     EmpathyIndividualManager *self)
352 {
353   EmpathyIndividualManagerPriv *priv = GET_PRIV (self);
354   GeeIterator *iter;
355   GeeSet *removed;
356   GeeCollection *added;
357   GList *added_set = NULL, *added_filtered = NULL, *removed_list = NULL;
358
359   /* We're not interested in the relationships between the added and removed
360    * individuals, so just extract collections of them. Note that the added
361    * collection may contain duplicates, while the removed set won't. */
362   removed = gee_multi_map_get_keys (changes);
363   added = gee_multi_map_get_values (changes);
364
365   /* Handle the removals first, as one of the added Individuals might have the
366    * same ID as one of the removed Individuals (due to linking). */
367   iter = gee_iterable_iterator (GEE_ITERABLE (removed));
368   while (gee_iterator_next (iter))
369     {
370       FolksIndividual *ind = gee_iterator_get (iter);
371
372       if (ind == NULL)
373         continue;
374
375       g_signal_handlers_disconnect_by_func (ind,
376           individual_notify_personas_cb, self);
377
378       if (g_hash_table_lookup (priv->individuals,
379           folks_individual_get_id (ind)) != NULL)
380         {
381           remove_individual (self, ind);
382           removed_list = g_list_prepend (removed_list, ind);
383         }
384
385       g_clear_object (&ind);
386     }
387   g_clear_object (&iter);
388
389   /* Filter the individuals for ones which contain EmpathyContacts */
390   iter = gee_iterable_iterator (GEE_ITERABLE (added));
391   while (gee_iterator_next (iter))
392     {
393       FolksIndividual *ind = gee_iterator_get (iter);
394
395       /* Make sure we handle each added individual only once. */
396       if (ind == NULL || g_list_find (added_set, ind) != NULL)
397         continue;
398       added_set = g_list_prepend (added_set, ind);
399
400       g_signal_connect (ind, "notify::personas",
401           G_CALLBACK (individual_notify_personas_cb), self);
402
403       if (empathy_folks_individual_contains_contact (ind) == TRUE)
404         {
405           add_individual (self, ind);
406           added_filtered = g_list_prepend (added_filtered, ind);
407         }
408
409       g_clear_object (&ind);
410     }
411   g_clear_object (&iter);
412
413   g_list_free (added_set);
414
415   g_object_unref (added);
416   g_object_unref (removed);
417
418   /* Bail if we have no individuals left */
419   if (added_filtered == NULL && removed == NULL)
420     return;
421
422   added_filtered = g_list_reverse (added_filtered);
423
424   g_signal_emit (self, signals[MEMBERS_CHANGED], 0, NULL,
425       added_filtered, removed_list,
426       TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
427       TRUE);
428
429   g_list_free (added_filtered);
430   g_list_free (removed_list);
431 }
432
433 static void
434 individual_manager_dispose (GObject *object)
435 {
436   EmpathyIndividualManagerPriv *priv = GET_PRIV (object);
437
438   g_hash_table_unref (priv->individuals);
439
440   tp_clear_object (&priv->aggregator);
441
442   G_OBJECT_CLASS (empathy_individual_manager_parent_class)->dispose (object);
443 }
444
445 static void
446 individual_manager_finalize (GObject *object)
447 {
448   EmpathyIndividualManagerPriv *priv = GET_PRIV (object);
449
450   g_sequence_free (priv->individuals_pop);
451
452   G_OBJECT_CLASS (empathy_individual_manager_parent_class)->finalize (object);
453 }
454
455 static GObject *
456 individual_manager_constructor (GType type,
457     guint n_props,
458     GObjectConstructParam *props)
459 {
460   GObject *retval;
461
462   if (manager_singleton)
463     {
464       retval = g_object_ref (manager_singleton);
465     }
466   else
467     {
468       retval =
469           G_OBJECT_CLASS (empathy_individual_manager_parent_class)->
470           constructor (type, n_props, props);
471
472       manager_singleton = EMPATHY_INDIVIDUAL_MANAGER (retval);
473       g_object_add_weak_pointer (retval, (gpointer) & manager_singleton);
474     }
475
476   return retval;
477 }
478
479 /**
480  * empathy_individual_manager_initialized:
481  *
482  * Reports whether or not the singleton has already been created.
483  *
484  * There can be instances where you want to access the #EmpathyIndividualManager
485  * only if it has been set up for this process.
486  *
487  * Returns: %TRUE if the #EmpathyIndividualManager singleton has previously
488  * been initialized.
489  */
490 gboolean
491 empathy_individual_manager_initialized (void)
492 {
493   return (manager_singleton != NULL);
494 }
495
496 static void
497 empathy_individual_manager_class_init (EmpathyIndividualManagerClass *klass)
498 {
499   GObjectClass *object_class = G_OBJECT_CLASS (klass);
500   GParamSpec *spec;
501
502   object_class->get_property = individual_manager_get_property;
503   object_class->dispose = individual_manager_dispose;
504   object_class->finalize = individual_manager_finalize;
505   object_class->constructor = individual_manager_constructor;
506
507   spec = g_param_spec_pointer ("top-individuals", "top individuals",
508       "Top Individuals",
509       G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
510   g_object_class_install_property (object_class, PROP_TOP_INDIVIDUALS, spec);
511
512   signals[GROUPS_CHANGED] =
513       g_signal_new ("groups-changed",
514           G_TYPE_FROM_CLASS (klass),
515           G_SIGNAL_RUN_LAST,
516           0,
517           NULL, NULL,
518           g_cclosure_marshal_generic,
519           G_TYPE_NONE, 3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_STRING, G_TYPE_BOOLEAN);
520
521   signals[FAVOURITES_CHANGED] =
522       g_signal_new ("favourites-changed",
523           G_TYPE_FROM_CLASS (klass),
524           G_SIGNAL_RUN_LAST,
525           0,
526           NULL, NULL,
527           g_cclosure_marshal_generic,
528           G_TYPE_NONE, 2, FOLKS_TYPE_INDIVIDUAL, G_TYPE_BOOLEAN);
529
530   signals[MEMBERS_CHANGED] =
531       g_signal_new ("members-changed",
532           G_TYPE_FROM_CLASS (klass),
533           G_SIGNAL_RUN_LAST,
534           0,
535           NULL, NULL,
536           g_cclosure_marshal_generic,
537           G_TYPE_NONE,
538           4, G_TYPE_STRING, G_TYPE_POINTER, G_TYPE_POINTER, G_TYPE_UINT);
539
540   signals[CONTACTS_LOADED] =
541       g_signal_new ("contacts-loaded",
542           G_TYPE_FROM_CLASS (klass),
543           G_SIGNAL_RUN_LAST,
544           0,
545           NULL, NULL,
546           g_cclosure_marshal_generic,
547           G_TYPE_NONE,
548           0);
549
550   g_type_class_add_private (object_class,
551       sizeof (EmpathyIndividualManagerPriv));
552 }
553
554 static void
555 aggregator_is_quiescent_notify_cb (FolksIndividualAggregator *aggregator,
556     GParamSpec *spec,
557     EmpathyIndividualManager *self)
558 {
559   EmpathyIndividualManagerPriv *priv = GET_PRIV (self);
560   gboolean is_quiescent;
561
562   if (priv->contacts_loaded)
563     return;
564
565   g_object_get (aggregator, "is-quiescent", &is_quiescent, NULL);
566
567   if (!is_quiescent)
568     return;
569
570   priv->contacts_loaded = TRUE;
571
572   g_signal_emit (self, signals[CONTACTS_LOADED], 0);
573 }
574
575 static void
576 empathy_individual_manager_init (EmpathyIndividualManager *self)
577 {
578   EmpathyIndividualManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
579       EMPATHY_TYPE_INDIVIDUAL_MANAGER, EmpathyIndividualManagerPriv);
580
581   self->priv = priv;
582   priv->individuals = g_hash_table_new_full (g_str_hash, g_str_equal,
583       g_free, g_object_unref);
584
585   priv->individuals_pop = g_sequence_new (g_object_unref);
586
587   priv->aggregator = folks_individual_aggregator_new ();
588   tp_g_signal_connect_object (priv->aggregator, "individuals-changed-detailed",
589       G_CALLBACK (aggregator_individuals_changed_cb), self, 0);
590   tp_g_signal_connect_object (priv->aggregator, "notify::is-quiescent",
591       G_CALLBACK (aggregator_is_quiescent_notify_cb), self, 0);
592   folks_individual_aggregator_prepare (priv->aggregator, NULL, NULL);
593 }
594
595 EmpathyIndividualManager *
596 empathy_individual_manager_dup_singleton (void)
597 {
598   return g_object_new (EMPATHY_TYPE_INDIVIDUAL_MANAGER, NULL);
599 }
600
601 GList *
602 empathy_individual_manager_get_members (EmpathyIndividualManager *self)
603 {
604   EmpathyIndividualManagerPriv *priv = GET_PRIV (self);
605
606   g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (self), NULL);
607
608   return g_hash_table_get_values (priv->individuals);
609 }
610
611 FolksIndividual *
612 empathy_individual_manager_lookup_member (EmpathyIndividualManager *self,
613     const gchar *id)
614 {
615   EmpathyIndividualManagerPriv *priv = GET_PRIV (self);
616
617   g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (self), NULL);
618
619   return g_hash_table_lookup (priv->individuals, id);
620 }
621
622 static void
623 aggregator_add_persona_from_details_cb (GObject *source,
624     GAsyncResult *result,
625     gpointer user_data)
626 {
627   FolksIndividualAggregator *aggregator = FOLKS_INDIVIDUAL_AGGREGATOR (source);
628   EmpathyContact *contact = EMPATHY_CONTACT (user_data);
629   FolksPersona *persona;
630   GError *error = NULL;
631
632   persona = folks_individual_aggregator_add_persona_from_details_finish (
633       aggregator, result, &error);
634   if (error != NULL)
635     {
636       g_warning ("failed to add individual from contact: %s", error->message);
637       g_clear_error (&error);
638     }
639
640   /* The persona can be NULL even if there wasn't an error, if the persona was
641    * already in the contact list */
642   if (persona != NULL)
643     {
644       /* Set the contact's persona */
645       empathy_contact_set_persona (contact, persona);
646       g_object_unref (persona);
647     }
648
649   g_object_unref (contact);
650 }
651
652 void
653 empathy_individual_manager_add_from_contact (EmpathyIndividualManager *self,
654     EmpathyContact *contact)
655 {
656   EmpathyIndividualManagerPriv *priv;
657   FolksBackendStore *backend_store;
658   FolksBackend *backend;
659   FolksPersonaStore *persona_store;
660   GHashTable* details;
661   GeeMap *persona_stores;
662   TpAccount *account;
663   const gchar *store_id;
664
665   g_return_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (self));
666   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
667
668   priv = GET_PRIV (self);
669
670   /* We need to ref the contact since otherwise its linked TpHandle will be
671    * destroyed. */
672   g_object_ref (contact);
673
674   DEBUG ("adding individual from contact %s (%s)",
675       empathy_contact_get_id (contact), empathy_contact_get_alias (contact));
676
677   account = empathy_contact_get_account (contact);
678   store_id = tp_proxy_get_object_path (TP_PROXY (account));
679
680   /* Get the persona store to use */
681   backend_store = folks_backend_store_dup ();
682   backend =
683       folks_backend_store_dup_backend_by_name (backend_store, "telepathy");
684
685   if (backend == NULL)
686     {
687       g_warning ("Failed to add individual from contact: couldn't get "
688           "'telepathy' backend");
689       goto finish;
690     }
691
692   persona_stores = folks_backend_get_persona_stores (backend);
693   persona_store = gee_map_get (persona_stores, store_id);
694
695   if (persona_store == NULL)
696     {
697       g_warning ("Failed to add individual from contact: couldn't get persona "
698           "store '%s'", store_id);
699       goto finish;
700     }
701
702   details = tp_asv_new (
703       "contact", G_TYPE_STRING, empathy_contact_get_id (contact),
704       NULL);
705
706   folks_individual_aggregator_add_persona_from_details (
707       priv->aggregator, NULL, persona_store, details,
708       aggregator_add_persona_from_details_cb, contact);
709
710   g_hash_table_unref (details);
711   g_object_unref (persona_store);
712
713 finish:
714   tp_clear_object (&backend);
715   tp_clear_object (&backend_store);
716 }
717
718 static void
719 aggregator_remove_individual_cb (GObject *source,
720     GAsyncResult *result,
721     gpointer user_data)
722 {
723   FolksIndividualAggregator *aggregator = FOLKS_INDIVIDUAL_AGGREGATOR (source);
724   GError *error = NULL;
725
726   folks_individual_aggregator_remove_individual_finish (
727       aggregator, result, &error);
728   if (error != NULL)
729     {
730       g_warning ("failed to remove individual: %s", error->message);
731       g_clear_error (&error);
732     }
733 }
734
735 /**
736  * Removes the inner contact from the server (and thus the Individual). Not
737  * meant for de-shelling inner personas from an Individual.
738  */
739 void
740 empathy_individual_manager_remove (EmpathyIndividualManager *self,
741     FolksIndividual *individual,
742     const gchar *message)
743 {
744   EmpathyIndividualManagerPriv *priv;
745
746   g_return_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (self));
747   g_return_if_fail (FOLKS_IS_INDIVIDUAL (individual));
748
749   priv = GET_PRIV (self);
750
751   DEBUG ("removing individual %s (%s)",
752       folks_individual_get_id (individual),
753       folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual)));
754
755   folks_individual_aggregator_remove_individual (priv->aggregator, individual,
756       aggregator_remove_individual_cb, self);
757 }
758
759 /* FIXME: The parameter @self is not required and the method can be placed in
760  * utilities. I left it as it is to stay coherent with empathy-2.34 */
761 /**
762  * empathy_individual_manager_supports_blocking
763  * @self: the #EmpathyIndividualManager
764  * @individual: an individual to check
765  *
766  * Indicates whether any personas of an @individual can be blocked.
767  *
768  * Returns: %TRUE if any persona supports contact blocking
769  */
770 gboolean
771 empathy_individual_manager_supports_blocking (EmpathyIndividualManager *self,
772     FolksIndividual *individual)
773 {
774   GeeSet *personas;
775   GeeIterator *iter;
776   gboolean retval = FALSE;
777
778   g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (self), FALSE);
779
780   personas = folks_individual_get_personas (individual);
781   iter = gee_iterable_iterator (GEE_ITERABLE (personas));
782   while (!retval && gee_iterator_next (iter))
783     {
784       TpfPersona *persona = gee_iterator_get (iter);
785       TpConnection *conn;
786
787       if (TPF_IS_PERSONA (persona))
788         {
789           TpContact *tp_contact;
790
791           tp_contact = tpf_persona_get_contact (persona);
792           if (tp_contact != NULL)
793             {
794               conn = tp_contact_get_connection (tp_contact);
795
796               if (tp_proxy_has_interface_by_id (conn,
797                     TP_IFACE_QUARK_CONNECTION_INTERFACE_CONTACT_BLOCKING))
798                 retval = TRUE;
799             }
800         }
801       g_clear_object (&persona);
802     }
803   g_clear_object (&iter);
804
805   return retval;
806 }
807
808 void
809 empathy_individual_manager_set_blocked (EmpathyIndividualManager *self,
810     FolksIndividual *individual,
811     gboolean blocked,
812     gboolean abusive)
813 {
814   GeeSet *personas;
815   GeeIterator *iter;
816
817   g_return_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (self));
818
819   personas = folks_individual_get_personas (individual);
820   iter = gee_iterable_iterator (GEE_ITERABLE (personas));
821   while (gee_iterator_next (iter))
822     {
823       TpfPersona *persona = gee_iterator_get (iter);
824
825       if (TPF_IS_PERSONA (persona))
826         {
827           TpContact *tp_contact;
828           TpConnection *conn;
829
830           tp_contact = tpf_persona_get_contact (persona);
831           if (tp_contact == NULL)
832             continue;
833
834           conn = tp_contact_get_connection (tp_contact);
835
836           if (!tp_proxy_has_interface_by_id (conn,
837                 TP_IFACE_QUARK_CONNECTION_INTERFACE_CONTACT_BLOCKING))
838             continue;
839
840           if (blocked)
841             tp_contact_block_async (tp_contact, abusive, NULL, NULL);
842           else
843             tp_contact_unblock_async (tp_contact, NULL, NULL);
844         }
845       g_clear_object (&persona);
846     }
847   g_clear_object (&iter);
848 }
849
850 static void
851 groups_change_group_cb (GObject *source,
852     GAsyncResult *result,
853     gpointer user_data)
854 {
855   FolksGroupDetails *group_details = FOLKS_GROUP_DETAILS (source);
856   GError *error = NULL;
857
858   folks_group_details_change_group_finish (group_details, result, &error);
859   if (error != NULL)
860     {
861       g_warning ("failed to change group: %s", error->message);
862       g_clear_error (&error);
863     }
864 }
865
866 static void
867 remove_group_cb (const gchar *id,
868     FolksIndividual *individual,
869     const gchar *group)
870 {
871   folks_group_details_change_group (FOLKS_GROUP_DETAILS (individual), group,
872       FALSE, groups_change_group_cb, NULL);
873 }
874
875 void
876 empathy_individual_manager_remove_group (EmpathyIndividualManager *manager,
877     const gchar *group)
878 {
879   EmpathyIndividualManagerPriv *priv;
880
881   g_return_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (manager));
882   g_return_if_fail (group != NULL);
883
884   priv = GET_PRIV (manager);
885
886   DEBUG ("removing group %s", group);
887
888   /* Remove every individual from the group */
889   g_hash_table_foreach (priv->individuals, (GHFunc) remove_group_cb,
890       (gpointer) group);
891 }
892
893 gboolean
894 empathy_individual_manager_get_contacts_loaded (EmpathyIndividualManager *self)
895 {
896   EmpathyIndividualManagerPriv *priv = GET_PRIV (self);
897
898   return priv->contacts_loaded;
899 }
900
901 GList *
902 empathy_individual_manager_get_top_individuals (EmpathyIndividualManager *self)
903 {
904   EmpathyIndividualManagerPriv *priv = GET_PRIV (self);
905
906   return priv->top_individuals;
907 }
908
909 static void
910 unprepare_cb (GObject *source,
911     GAsyncResult *result,
912     gpointer user_data)
913 {
914   GError *error = NULL;
915   GSimpleAsyncResult *my_result = user_data;
916
917   folks_individual_aggregator_unprepare_finish (
918       FOLKS_INDIVIDUAL_AGGREGATOR (source), result, &error);
919
920   if (error != NULL)
921     {
922       DEBUG ("Failed to unprepare the aggregator: %s", error->message);
923       g_simple_async_result_take_error (my_result, error);
924     }
925
926   g_simple_async_result_complete (my_result);
927   g_object_unref (my_result);
928 }
929
930 void
931 empathy_individual_manager_unprepare_async (
932     EmpathyIndividualManager *self,
933     GAsyncReadyCallback callback,
934     gpointer user_data)
935 {
936   EmpathyIndividualManagerPriv *priv = GET_PRIV (self);
937   GSimpleAsyncResult *result;
938
939   result = g_simple_async_result_new (G_OBJECT (self), callback, user_data,
940       empathy_individual_manager_unprepare_async);
941
942   folks_individual_aggregator_unprepare (priv->aggregator, unprepare_cb,
943       result);
944 }
945
946 gboolean
947 empathy_individual_manager_unprepare_finish (
948     EmpathyIndividualManager *self,
949     GAsyncResult *result,
950     GError **error)
951 {
952   empathy_implement_finish_void (self,
953       empathy_individual_manager_unprepare_async)
954 }