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