]> git.0d.be Git - empathy.git/blob - libempathy/empathy-individual-manager.c
Updated kn translations
[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       /* priv->top_individuals borrows its reference from
293        * priv->individuals_pop so we take a reference on the individual while
294        * removing it to make sure it stays alive while calling
295        * check_top_individuals(). */
296       g_object_ref (individual);
297       g_sequence_remove (iter);
298       check_top_individuals (self);
299       g_object_unref (individual);
300     }
301
302   g_signal_handlers_disconnect_by_func (individual,
303       individual_group_changed_cb, self);
304   g_signal_handlers_disconnect_by_func (individual,
305       individual_notify_is_favourite_cb, self);
306   g_signal_handlers_disconnect_by_func (individual,
307       individual_notify_im_interaction_count, self);
308
309   g_hash_table_remove (priv->individuals, folks_individual_get_id (individual));
310 }
311
312 /* This is emitted for *all* individuals in the individual aggregator (not
313  * just the ones we keep a reference to), to allow for the case where a new
314  * individual doesn't contain an EmpathyContact, but later has a persona added
315  * which does. */
316 static void
317 individual_notify_personas_cb (FolksIndividual *individual,
318     GParamSpec *pspec,
319     EmpathyIndividualManager *self)
320 {
321   EmpathyIndividualManagerPriv *priv = GET_PRIV (self);
322
323   const gchar *id = folks_individual_get_id (individual);
324   gboolean has_contact = empathy_folks_individual_contains_contact (individual);
325   gboolean had_contact = (g_hash_table_lookup (priv->individuals,
326       id) != NULL) ? TRUE : FALSE;
327
328   if (had_contact == TRUE && has_contact == FALSE)
329     {
330       GList *removed = NULL;
331
332       /* The Individual has lost its EmpathyContact */
333       removed = g_list_prepend (removed, individual);
334       g_signal_emit (self, signals[MEMBERS_CHANGED], 0, NULL, NULL, removed,
335           TP_CHANNEL_GROUP_CHANGE_REASON_NONE /* FIXME */);
336       g_list_free (removed);
337
338       remove_individual (self, individual);
339     }
340   else if (had_contact == FALSE && has_contact == TRUE)
341     {
342       GList *added = NULL;
343
344       /* The Individual has gained its first EmpathyContact */
345       add_individual (self, individual);
346
347       added = g_list_prepend (added, individual);
348       g_signal_emit (self, signals[MEMBERS_CHANGED], 0, NULL, added, NULL,
349           TP_CHANNEL_GROUP_CHANGE_REASON_NONE /* FIXME */);
350       g_list_free (added);
351     }
352 }
353
354 static void
355 aggregator_individuals_changed_cb (FolksIndividualAggregator *aggregator,
356     GeeMultiMap *changes,
357     EmpathyIndividualManager *self)
358 {
359   EmpathyIndividualManagerPriv *priv = GET_PRIV (self);
360   GeeIterator *iter;
361   GeeSet *removed;
362   GeeCollection *added;
363   GList *added_set = NULL, *added_filtered = NULL, *removed_list = NULL;
364
365   /* We're not interested in the relationships between the added and removed
366    * individuals, so just extract collections of them. Note that the added
367    * collection may contain duplicates, while the removed set won't. */
368   removed = gee_multi_map_get_keys (changes);
369   added = gee_multi_map_get_values (changes);
370
371   /* Handle the removals first, as one of the added Individuals might have the
372    * same ID as one of the removed Individuals (due to linking). */
373   iter = gee_iterable_iterator (GEE_ITERABLE (removed));
374   while (gee_iterator_next (iter))
375     {
376       FolksIndividual *ind = gee_iterator_get (iter);
377
378       if (ind == NULL)
379         continue;
380
381       g_signal_handlers_disconnect_by_func (ind,
382           individual_notify_personas_cb, self);
383
384       if (g_hash_table_lookup (priv->individuals,
385           folks_individual_get_id (ind)) != NULL)
386         {
387           remove_individual (self, ind);
388           removed_list = g_list_prepend (removed_list, ind);
389         }
390
391       g_clear_object (&ind);
392     }
393   g_clear_object (&iter);
394
395   /* Filter the individuals for ones which contain EmpathyContacts */
396   iter = gee_iterable_iterator (GEE_ITERABLE (added));
397   while (gee_iterator_next (iter))
398     {
399       FolksIndividual *ind = gee_iterator_get (iter);
400
401       /* Make sure we handle each added individual only once. */
402       if (ind == NULL || g_list_find (added_set, ind) != NULL)
403         continue;
404       added_set = g_list_prepend (added_set, ind);
405
406       g_signal_connect (ind, "notify::personas",
407           G_CALLBACK (individual_notify_personas_cb), self);
408
409       if (empathy_folks_individual_contains_contact (ind) == TRUE)
410         {
411           add_individual (self, ind);
412           added_filtered = g_list_prepend (added_filtered, ind);
413         }
414
415       g_clear_object (&ind);
416     }
417   g_clear_object (&iter);
418
419   g_list_free (added_set);
420
421   g_object_unref (added);
422   g_object_unref (removed);
423
424   /* Bail if we have no individuals left */
425   if (added_filtered == NULL && removed == NULL)
426     return;
427
428   added_filtered = g_list_reverse (added_filtered);
429
430   g_signal_emit (self, signals[MEMBERS_CHANGED], 0, NULL,
431       added_filtered, removed_list,
432       TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
433       TRUE);
434
435   g_list_free (added_filtered);
436   g_list_free (removed_list);
437 }
438
439 static void
440 individual_manager_dispose (GObject *object)
441 {
442   EmpathyIndividualManagerPriv *priv = GET_PRIV (object);
443
444   g_hash_table_unref (priv->individuals);
445
446   tp_clear_object (&priv->aggregator);
447
448   G_OBJECT_CLASS (empathy_individual_manager_parent_class)->dispose (object);
449 }
450
451 static void
452 individual_manager_finalize (GObject *object)
453 {
454   EmpathyIndividualManagerPriv *priv = GET_PRIV (object);
455
456   g_sequence_free (priv->individuals_pop);
457
458   G_OBJECT_CLASS (empathy_individual_manager_parent_class)->finalize (object);
459 }
460
461 static GObject *
462 individual_manager_constructor (GType type,
463     guint n_props,
464     GObjectConstructParam *props)
465 {
466   GObject *retval;
467
468   if (manager_singleton)
469     {
470       retval = g_object_ref (manager_singleton);
471     }
472   else
473     {
474       retval =
475           G_OBJECT_CLASS (empathy_individual_manager_parent_class)->
476           constructor (type, n_props, props);
477
478       manager_singleton = EMPATHY_INDIVIDUAL_MANAGER (retval);
479       g_object_add_weak_pointer (retval, (gpointer) & manager_singleton);
480     }
481
482   return retval;
483 }
484
485 /**
486  * empathy_individual_manager_initialized:
487  *
488  * Reports whether or not the singleton has already been created.
489  *
490  * There can be instances where you want to access the #EmpathyIndividualManager
491  * only if it has been set up for this process.
492  *
493  * Returns: %TRUE if the #EmpathyIndividualManager singleton has previously
494  * been initialized.
495  */
496 gboolean
497 empathy_individual_manager_initialized (void)
498 {
499   return (manager_singleton != NULL);
500 }
501
502 static void
503 empathy_individual_manager_class_init (EmpathyIndividualManagerClass *klass)
504 {
505   GObjectClass *object_class = G_OBJECT_CLASS (klass);
506   GParamSpec *spec;
507
508   object_class->get_property = individual_manager_get_property;
509   object_class->dispose = individual_manager_dispose;
510   object_class->finalize = individual_manager_finalize;
511   object_class->constructor = individual_manager_constructor;
512
513   spec = g_param_spec_pointer ("top-individuals", "top individuals",
514       "Top Individuals",
515       G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
516   g_object_class_install_property (object_class, PROP_TOP_INDIVIDUALS, spec);
517
518   signals[GROUPS_CHANGED] =
519       g_signal_new ("groups-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, 3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_STRING, G_TYPE_BOOLEAN);
526
527   signals[FAVOURITES_CHANGED] =
528       g_signal_new ("favourites-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, 2, FOLKS_TYPE_INDIVIDUAL, G_TYPE_BOOLEAN);
535
536   signals[MEMBERS_CHANGED] =
537       g_signal_new ("members-changed",
538           G_TYPE_FROM_CLASS (klass),
539           G_SIGNAL_RUN_LAST,
540           0,
541           NULL, NULL,
542           g_cclosure_marshal_generic,
543           G_TYPE_NONE,
544           4, G_TYPE_STRING, G_TYPE_POINTER, G_TYPE_POINTER, G_TYPE_UINT);
545
546   signals[CONTACTS_LOADED] =
547       g_signal_new ("contacts-loaded",
548           G_TYPE_FROM_CLASS (klass),
549           G_SIGNAL_RUN_LAST,
550           0,
551           NULL, NULL,
552           g_cclosure_marshal_generic,
553           G_TYPE_NONE,
554           0);
555
556   g_type_class_add_private (object_class,
557       sizeof (EmpathyIndividualManagerPriv));
558 }
559
560 static void
561 aggregator_is_quiescent_notify_cb (FolksIndividualAggregator *aggregator,
562     GParamSpec *spec,
563     EmpathyIndividualManager *self)
564 {
565   EmpathyIndividualManagerPriv *priv = GET_PRIV (self);
566   gboolean is_quiescent;
567
568   if (priv->contacts_loaded)
569     return;
570
571   g_object_get (aggregator, "is-quiescent", &is_quiescent, NULL);
572
573   if (!is_quiescent)
574     return;
575
576   priv->contacts_loaded = TRUE;
577
578   g_signal_emit (self, signals[CONTACTS_LOADED], 0);
579 }
580
581 static void
582 empathy_individual_manager_init (EmpathyIndividualManager *self)
583 {
584   EmpathyIndividualManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
585       EMPATHY_TYPE_INDIVIDUAL_MANAGER, EmpathyIndividualManagerPriv);
586
587   self->priv = priv;
588   priv->individuals = g_hash_table_new_full (g_str_hash, g_str_equal,
589       g_free, g_object_unref);
590
591   priv->individuals_pop = g_sequence_new (g_object_unref);
592
593   priv->aggregator = folks_individual_aggregator_new ();
594   tp_g_signal_connect_object (priv->aggregator, "individuals-changed-detailed",
595       G_CALLBACK (aggregator_individuals_changed_cb), self, 0);
596   tp_g_signal_connect_object (priv->aggregator, "notify::is-quiescent",
597       G_CALLBACK (aggregator_is_quiescent_notify_cb), self, 0);
598   folks_individual_aggregator_prepare (priv->aggregator, NULL, NULL);
599 }
600
601 EmpathyIndividualManager *
602 empathy_individual_manager_dup_singleton (void)
603 {
604   return g_object_new (EMPATHY_TYPE_INDIVIDUAL_MANAGER, NULL);
605 }
606
607 GList *
608 empathy_individual_manager_get_members (EmpathyIndividualManager *self)
609 {
610   EmpathyIndividualManagerPriv *priv = GET_PRIV (self);
611
612   g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (self), NULL);
613
614   return g_hash_table_get_values (priv->individuals);
615 }
616
617 FolksIndividual *
618 empathy_individual_manager_lookup_member (EmpathyIndividualManager *self,
619     const gchar *id)
620 {
621   EmpathyIndividualManagerPriv *priv = GET_PRIV (self);
622
623   g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (self), NULL);
624
625   return g_hash_table_lookup (priv->individuals, id);
626 }
627
628 static void
629 aggregator_add_persona_from_details_cb (GObject *source,
630     GAsyncResult *result,
631     gpointer user_data)
632 {
633   FolksIndividualAggregator *aggregator = FOLKS_INDIVIDUAL_AGGREGATOR (source);
634   EmpathyContact *contact = EMPATHY_CONTACT (user_data);
635   FolksPersona *persona;
636   GError *error = NULL;
637
638   persona = folks_individual_aggregator_add_persona_from_details_finish (
639       aggregator, result, &error);
640   if (error != NULL)
641     {
642       g_warning ("failed to add individual from contact: %s", error->message);
643       g_clear_error (&error);
644     }
645
646   /* The persona can be NULL even if there wasn't an error, if the persona was
647    * already in the contact list */
648   if (persona != NULL)
649     {
650       /* Set the contact's persona */
651       empathy_contact_set_persona (contact, persona);
652       g_object_unref (persona);
653     }
654
655   g_object_unref (contact);
656 }
657
658 void
659 empathy_individual_manager_add_from_contact (EmpathyIndividualManager *self,
660     EmpathyContact *contact)
661 {
662   EmpathyIndividualManagerPriv *priv;
663   FolksBackendStore *backend_store;
664   FolksBackend *backend;
665   FolksPersonaStore *persona_store;
666   GHashTable* details;
667   GeeMap *persona_stores;
668   TpAccount *account;
669   const gchar *store_id;
670
671   g_return_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (self));
672   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
673
674   priv = GET_PRIV (self);
675
676   /* We need to ref the contact since otherwise its linked TpHandle will be
677    * destroyed. */
678   g_object_ref (contact);
679
680   DEBUG ("adding individual from contact %s (%s)",
681       empathy_contact_get_id (contact), empathy_contact_get_alias (contact));
682
683   account = empathy_contact_get_account (contact);
684   store_id = tp_proxy_get_object_path (TP_PROXY (account));
685
686   /* Get the persona store to use */
687   backend_store = folks_backend_store_dup ();
688   backend =
689       folks_backend_store_dup_backend_by_name (backend_store, "telepathy");
690
691   if (backend == NULL)
692     {
693       g_warning ("Failed to add individual from contact: couldn't get "
694           "'telepathy' backend");
695       goto finish;
696     }
697
698   persona_stores = folks_backend_get_persona_stores (backend);
699   persona_store = gee_map_get (persona_stores, store_id);
700
701   if (persona_store == NULL)
702     {
703       g_warning ("Failed to add individual from contact: couldn't get persona "
704           "store '%s'", store_id);
705       goto finish;
706     }
707
708   details = tp_asv_new (
709       "contact", G_TYPE_STRING, empathy_contact_get_id (contact),
710       NULL);
711
712   folks_individual_aggregator_add_persona_from_details (
713       priv->aggregator, NULL, persona_store, details,
714       aggregator_add_persona_from_details_cb, contact);
715
716   g_hash_table_unref (details);
717   g_object_unref (persona_store);
718
719 finish:
720   tp_clear_object (&backend);
721   tp_clear_object (&backend_store);
722 }
723
724 static void
725 aggregator_remove_individual_cb (GObject *source,
726     GAsyncResult *result,
727     gpointer user_data)
728 {
729   FolksIndividualAggregator *aggregator = FOLKS_INDIVIDUAL_AGGREGATOR (source);
730   GError *error = NULL;
731
732   folks_individual_aggregator_remove_individual_finish (
733       aggregator, result, &error);
734   if (error != NULL)
735     {
736       g_warning ("failed to remove individual: %s", error->message);
737       g_clear_error (&error);
738     }
739 }
740
741 /**
742  * Removes the inner contact from the server (and thus the Individual). Not
743  * meant for de-shelling inner personas from an Individual.
744  */
745 void
746 empathy_individual_manager_remove (EmpathyIndividualManager *self,
747     FolksIndividual *individual,
748     const gchar *message)
749 {
750   EmpathyIndividualManagerPriv *priv;
751
752   g_return_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (self));
753   g_return_if_fail (FOLKS_IS_INDIVIDUAL (individual));
754
755   priv = GET_PRIV (self);
756
757   DEBUG ("removing individual %s (%s)",
758       folks_individual_get_id (individual),
759       folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual)));
760
761   folks_individual_aggregator_remove_individual (priv->aggregator, individual,
762       aggregator_remove_individual_cb, self);
763 }
764
765 /* FIXME: The parameter @self is not required and the method can be placed in
766  * utilities. I left it as it is to stay coherent with empathy-2.34 */
767 /**
768  * empathy_individual_manager_supports_blocking
769  * @self: the #EmpathyIndividualManager
770  * @individual: an individual to check
771  *
772  * Indicates whether any personas of an @individual can be blocked.
773  *
774  * Returns: %TRUE if any persona supports contact blocking
775  */
776 gboolean
777 empathy_individual_manager_supports_blocking (EmpathyIndividualManager *self,
778     FolksIndividual *individual)
779 {
780   GeeSet *personas;
781   GeeIterator *iter;
782   gboolean retval = FALSE;
783
784   g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (self), FALSE);
785
786   personas = folks_individual_get_personas (individual);
787   iter = gee_iterable_iterator (GEE_ITERABLE (personas));
788   while (!retval && gee_iterator_next (iter))
789     {
790       TpfPersona *persona = gee_iterator_get (iter);
791       TpConnection *conn;
792
793       if (TPF_IS_PERSONA (persona))
794         {
795           TpContact *tp_contact;
796
797           tp_contact = tpf_persona_get_contact (persona);
798           if (tp_contact != NULL)
799             {
800               conn = tp_contact_get_connection (tp_contact);
801
802               if (tp_proxy_has_interface_by_id (conn,
803                     TP_IFACE_QUARK_CONNECTION_INTERFACE_CONTACT_BLOCKING))
804                 retval = TRUE;
805             }
806         }
807       g_clear_object (&persona);
808     }
809   g_clear_object (&iter);
810
811   return retval;
812 }
813
814 void
815 empathy_individual_manager_set_blocked (EmpathyIndividualManager *self,
816     FolksIndividual *individual,
817     gboolean blocked,
818     gboolean abusive)
819 {
820   GeeSet *personas;
821   GeeIterator *iter;
822
823   g_return_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (self));
824
825   personas = folks_individual_get_personas (individual);
826   iter = gee_iterable_iterator (GEE_ITERABLE (personas));
827   while (gee_iterator_next (iter))
828     {
829       TpfPersona *persona = gee_iterator_get (iter);
830
831       if (TPF_IS_PERSONA (persona))
832         {
833           TpContact *tp_contact;
834           TpConnection *conn;
835
836           tp_contact = tpf_persona_get_contact (persona);
837           if (tp_contact == NULL)
838             continue;
839
840           conn = tp_contact_get_connection (tp_contact);
841
842           if (!tp_proxy_has_interface_by_id (conn,
843                 TP_IFACE_QUARK_CONNECTION_INTERFACE_CONTACT_BLOCKING))
844             continue;
845
846           if (blocked)
847             tp_contact_block_async (tp_contact, abusive, NULL, NULL);
848           else
849             tp_contact_unblock_async (tp_contact, NULL, NULL);
850         }
851       g_clear_object (&persona);
852     }
853   g_clear_object (&iter);
854 }
855
856 static void
857 groups_change_group_cb (GObject *source,
858     GAsyncResult *result,
859     gpointer user_data)
860 {
861   FolksGroupDetails *group_details = FOLKS_GROUP_DETAILS (source);
862   GError *error = NULL;
863
864   folks_group_details_change_group_finish (group_details, result, &error);
865   if (error != NULL)
866     {
867       g_warning ("failed to change group: %s", error->message);
868       g_clear_error (&error);
869     }
870 }
871
872 static void
873 remove_group_cb (const gchar *id,
874     FolksIndividual *individual,
875     const gchar *group)
876 {
877   folks_group_details_change_group (FOLKS_GROUP_DETAILS (individual), group,
878       FALSE, groups_change_group_cb, NULL);
879 }
880
881 void
882 empathy_individual_manager_remove_group (EmpathyIndividualManager *manager,
883     const gchar *group)
884 {
885   EmpathyIndividualManagerPriv *priv;
886
887   g_return_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (manager));
888   g_return_if_fail (group != NULL);
889
890   priv = GET_PRIV (manager);
891
892   DEBUG ("removing group %s", group);
893
894   /* Remove every individual from the group */
895   g_hash_table_foreach (priv->individuals, (GHFunc) remove_group_cb,
896       (gpointer) group);
897 }
898
899 gboolean
900 empathy_individual_manager_get_contacts_loaded (EmpathyIndividualManager *self)
901 {
902   EmpathyIndividualManagerPriv *priv = GET_PRIV (self);
903
904   return priv->contacts_loaded;
905 }
906
907 GList *
908 empathy_individual_manager_get_top_individuals (EmpathyIndividualManager *self)
909 {
910   EmpathyIndividualManagerPriv *priv = GET_PRIV (self);
911
912   return priv->top_individuals;
913 }
914
915 static void
916 unprepare_cb (GObject *source,
917     GAsyncResult *result,
918     gpointer user_data)
919 {
920   GError *error = NULL;
921   GSimpleAsyncResult *my_result = user_data;
922
923   folks_individual_aggregator_unprepare_finish (
924       FOLKS_INDIVIDUAL_AGGREGATOR (source), result, &error);
925
926   if (error != NULL)
927     {
928       DEBUG ("Failed to unprepare the aggregator: %s", error->message);
929       g_simple_async_result_take_error (my_result, error);
930     }
931
932   g_simple_async_result_complete (my_result);
933   g_object_unref (my_result);
934 }
935
936 void
937 empathy_individual_manager_unprepare_async (
938     EmpathyIndividualManager *self,
939     GAsyncReadyCallback callback,
940     gpointer user_data)
941 {
942   EmpathyIndividualManagerPriv *priv = GET_PRIV (self);
943   GSimpleAsyncResult *result;
944
945   result = g_simple_async_result_new (G_OBJECT (self), callback, user_data,
946       empathy_individual_manager_unprepare_async);
947
948   folks_individual_aggregator_unprepare (priv->aggregator, unprepare_cb,
949       result);
950 }
951
952 gboolean
953 empathy_individual_manager_unprepare_finish (
954     EmpathyIndividualManager *self,
955     GAsyncResult *result,
956     GError **error)
957 {
958   empathy_implement_finish_void (self,
959       empathy_individual_manager_unprepare_async)
960 }