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