]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-contact-list-store.c
4f7398e22149bb6c1d93a278bc4cf4908932c593
[empathy.git] / libempathy-gtk / empathy-contact-list-store.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2005-2007 Imendio AB
4  * Copyright (C) 2007-2008 Collabora Ltd.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public
17  * License along with this program; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA  02110-1301  USA
20  *
21  * Authors: Mikael Hallendal <micke@imendio.com>
22  *          Martyn Russell <martyn@imendio.com>
23  *          Xavier Claessens <xclaesse@gmail.com>
24  */
25
26 #include "config.h"
27
28 #include <string.h>
29
30 #include <glib.h>
31 #include <glib/gi18n-lib.h>
32 #include <gtk/gtk.h>
33
34 #include <telepathy-glib/util.h>
35
36 #include <libempathy/empathy-utils.h>
37 #include <libempathy/empathy-tp-chat.h>
38 #include <libempathy/empathy-enum-types.h>
39 #include <libempathy/empathy-contact-manager.h>
40
41 #include "empathy-contact-list-store.h"
42 #include "empathy-ui-utils.h"
43 #include "empathy-gtk-enum-types.h"
44 #include "empathy-images.h"
45
46 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
47 #include <libempathy/empathy-debug.h>
48
49 /* Active users are those which have recently changed state
50  * (e.g. online, offline or from normal to a busy state).
51  */
52
53 /* Time in seconds user is shown as active */
54 #define ACTIVE_USER_SHOW_TIME 7
55
56 /* Time in seconds after connecting which we wait before active users are enabled */
57 #define ACTIVE_USER_WAIT_TO_ENABLE_TIME 5
58
59 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyContactListStore)
60 typedef struct {
61         EmpathyContactList         *list;
62         gboolean                    show_offline;
63         gboolean                    show_avatars;
64         gboolean                    show_groups;
65         gboolean                    is_compact;
66         gboolean                    show_protocols;
67         gboolean                    show_active;
68         EmpathyContactListStoreSort sort_criterium;
69         guint                       inhibit_active;
70         guint                       setup_idle_id;
71         gboolean                    dispose_has_run;
72         GHashTable                  *status_icons;
73         /* Hash: EmpathyContact* -> GQueue (GtkTreeIter *) */
74         GHashTable                  *empathy_contact_cache;
75         /* Hash: char *groupname -> GtkTreeIter * */
76         GHashTable                  *empathy_group_cache;
77 } EmpathyContactListStorePriv;
78
79 typedef struct {
80         EmpathyContactListStore *store;
81         EmpathyContact          *contact;
82         gboolean                remove;
83 } ShowActiveData;
84
85 static void             contact_list_store_dispose                  (GObject                       *object);
86 static void             contact_list_store_get_property              (GObject                       *object,
87                                                                       guint                          param_id,
88                                                                       GValue                        *value,
89                                                                       GParamSpec                    *pspec);
90 static void             contact_list_store_set_property              (GObject                       *object,
91                                                                       guint                          param_id,
92                                                                       const GValue                  *value,
93                                                                       GParamSpec                    *pspec);
94 static void             contact_list_store_setup                     (EmpathyContactListStore       *store);
95 static gboolean         contact_list_store_inibit_active_cb          (EmpathyContactListStore       *store);
96 static void             contact_list_store_members_changed_cb        (EmpathyContactList            *list_iface,
97                                                                       EmpathyContact                *contact,
98                                                                       EmpathyContact                *actor,
99                                                                       guint                          reason,
100                                                                       gchar                         *message,
101                                                                       gboolean                       is_member,
102                                                                       EmpathyContactListStore       *store);
103 static void             contact_list_store_member_renamed_cb         (EmpathyContactList            *list_iface,
104                                                                       EmpathyContact                *old_contact,
105                                                                       EmpathyContact                *new_contact,
106                                                                       guint                          reason,
107                                                                       gchar                         *message,
108                                                                       EmpathyContactListStore       *store);
109 static void             contact_list_store_groups_changed_cb         (EmpathyContactList            *list_iface,
110                                                                       EmpathyContact                *contact,
111                                                                       gchar                         *group,
112                                                                       gboolean                       is_member,
113                                                                       EmpathyContactListStore       *store);
114 static void             contact_list_store_add_contact               (EmpathyContactListStore       *store,
115                                                                       EmpathyContact                *contact);
116 static void             contact_list_store_remove_contact            (EmpathyContactListStore       *store,
117                                                                       EmpathyContact                *contact);
118 static void             contact_list_store_contact_update            (EmpathyContactListStore       *store,
119                                                                       EmpathyContact                *contact);
120 static void             contact_list_store_contact_updated_cb        (EmpathyContact                *contact,
121                                                                       GParamSpec                    *param,
122                                                                       EmpathyContactListStore       *store);
123 static void             contact_list_store_contact_set_active        (EmpathyContactListStore       *store,
124                                                                       EmpathyContact                *contact,
125                                                                       gboolean                       active,
126                                                                       gboolean                       set_changed);
127 static ShowActiveData * contact_list_store_contact_active_new        (EmpathyContactListStore       *store,
128                                                                       EmpathyContact                *contact,
129                                                                       gboolean                       remove);
130 static void             contact_list_store_contact_active_free       (ShowActiveData                *data);
131 static gboolean         contact_list_store_contact_active_cb         (ShowActiveData                *data);
132 static void             contact_list_store_get_group                 (EmpathyContactListStore       *store,
133                                                                       const gchar                   *name,
134                                                                       GtkTreeIter                   *iter_group_to_set,
135                                                                       GtkTreeIter                   *iter_separator_to_set,
136                                                                       gboolean                      *created,
137                                                                       gboolean                      is_fake_group);
138 static gint             contact_list_store_state_sort_func           (GtkTreeModel                  *model,
139                                                                       GtkTreeIter                   *iter_a,
140                                                                       GtkTreeIter                   *iter_b,
141                                                                       gpointer                       user_data);
142 static gint             contact_list_store_name_sort_func            (GtkTreeModel                  *model,
143                                                                       GtkTreeIter                   *iter_a,
144                                                                       GtkTreeIter                   *iter_b,
145                                                                       gpointer                       user_data);
146 static GList *          contact_list_store_find_contact              (EmpathyContactListStore       *store,
147                                                                       EmpathyContact                *contact);
148 static gboolean         contact_list_store_update_list_mode_foreach  (GtkTreeModel                  *model,
149                                                                       GtkTreePath                   *path,
150                                                                       GtkTreeIter                   *iter,
151                                                                       EmpathyContactListStore       *store);
152
153 enum {
154         PROP_0,
155         PROP_CONTACT_LIST,
156         PROP_SHOW_OFFLINE,
157         PROP_SHOW_AVATARS,
158         PROP_SHOW_PROTOCOLS,
159         PROP_SHOW_GROUPS,
160         PROP_IS_COMPACT,
161         PROP_SORT_CRITERIUM
162 };
163
164 G_DEFINE_TYPE (EmpathyContactListStore, empathy_contact_list_store, GTK_TYPE_TREE_STORE);
165
166 static void
167 contact_list_store_chat_state_changed_cb (TpChannel *self,
168                                      guint contact_handle,
169                                      guint state,
170                                      gpointer store)
171 {
172         EmpathyContactListStorePriv *priv = GET_PRIV (store);
173         GList *contacts, *l;
174
175         contacts = empathy_contact_list_get_members (priv->list);
176
177         for (l = contacts; l != NULL; l = l->next) {
178                 EmpathyContact *contact = EMPATHY_CONTACT (l->data);
179
180                 if (empathy_contact_get_handle (contact) == contact_handle) {
181                         contact_list_store_contact_update (store, contact);
182                         break;
183                 }
184         }
185
186         g_list_foreach (contacts, (GFunc) g_object_unref, NULL);
187         g_list_free (contacts);
188 }
189
190 static gboolean
191 contact_list_store_iface_setup (gpointer user_data)
192 {
193         EmpathyContactListStore     *store = user_data;
194         EmpathyContactListStorePriv *priv = GET_PRIV (store);
195         GList                       *contacts, *l;
196
197         /* Signal connection. */
198         g_signal_connect (priv->list,
199                           "member-renamed",
200                           G_CALLBACK (contact_list_store_member_renamed_cb),
201                           store);
202         g_signal_connect (priv->list,
203                           "members-changed",
204                           G_CALLBACK (contact_list_store_members_changed_cb),
205                           store);
206         g_signal_connect (priv->list,
207                           "groups-changed",
208                           G_CALLBACK (contact_list_store_groups_changed_cb),
209                           store);
210
211         if (EMPATHY_IS_TP_CHAT (priv->list)) {
212                         g_signal_connect (priv->list,
213                                           "chat-state-changed",
214                                           G_CALLBACK (contact_list_store_chat_state_changed_cb),
215                                           store);
216         }
217
218         /* Add contacts already created. */
219         contacts = empathy_contact_list_get_members (priv->list);
220         for (l = contacts; l; l = l->next) {
221                 contact_list_store_members_changed_cb (priv->list, l->data,
222                                                        NULL, 0, NULL,
223                                                        TRUE,
224                                                        store);
225
226                 g_object_unref (l->data);
227         }
228         g_list_free (contacts);
229
230         priv->setup_idle_id = 0;
231         return FALSE;
232 }
233
234
235 static void
236 contact_list_store_set_contact_list (EmpathyContactListStore *store,
237                                      EmpathyContactList      *list_iface)
238 {
239         EmpathyContactListStorePriv *priv = GET_PRIV (store);
240
241         priv->list = g_object_ref (list_iface);
242
243         /* Let a chance to have all properties set before populating */
244         priv->setup_idle_id = g_idle_add (contact_list_store_iface_setup, store);
245 }
246
247 static void
248 empathy_contact_list_store_class_init (EmpathyContactListStoreClass *klass)
249 {
250         GObjectClass *object_class = G_OBJECT_CLASS (klass);
251
252         object_class->dispose = contact_list_store_dispose;
253         object_class->get_property = contact_list_store_get_property;
254         object_class->set_property = contact_list_store_set_property;
255
256         g_object_class_install_property (object_class,
257                                          PROP_CONTACT_LIST,
258                                          g_param_spec_object ("contact-list",
259                                                               "The contact list iface",
260                                                               "The contact list iface",
261                                                               EMPATHY_TYPE_CONTACT_LIST,
262                                                               G_PARAM_CONSTRUCT_ONLY |
263                                                               G_PARAM_READWRITE));
264         g_object_class_install_property (object_class,
265                                          PROP_SHOW_OFFLINE,
266                                          g_param_spec_boolean ("show-offline",
267                                                                "Show Offline",
268                                                                "Whether contact list should display "
269                                                                "offline contacts",
270                                                                FALSE,
271                                                                G_PARAM_READWRITE));
272          g_object_class_install_property (object_class,
273                                           PROP_SHOW_AVATARS,
274                                           g_param_spec_boolean ("show-avatars",
275                                                                 "Show Avatars",
276                                                                 "Whether contact list should display "
277                                                                 "avatars for contacts",
278                                                                 TRUE,
279                                                                 G_PARAM_READWRITE));
280          g_object_class_install_property (object_class,
281                                           PROP_SHOW_PROTOCOLS,
282                                           g_param_spec_boolean ("show-protocols",
283                                                                 "Show Protocols",
284                                                                 "Whether contact list should display "
285                                                                 "protocols for contacts",
286                                                                 FALSE,
287                                                                 G_PARAM_READWRITE));
288          g_object_class_install_property (object_class,
289                                           PROP_SHOW_GROUPS,
290                                           g_param_spec_boolean ("show-groups",
291                                                                 "Show Groups",
292                                                                 "Whether contact list should display "
293                                                                 "contact groups",
294                                                                 TRUE,
295                                                                 G_PARAM_READWRITE));
296         g_object_class_install_property (object_class,
297                                          PROP_IS_COMPACT,
298                                          g_param_spec_boolean ("is-compact",
299                                                                "Is Compact",
300                                                                "Whether the contact list is in compact mode or not",
301                                                                FALSE,
302                                                                G_PARAM_READWRITE));
303
304         g_object_class_install_property (object_class,
305                                          PROP_SORT_CRITERIUM,
306                                          g_param_spec_enum ("sort-criterium",
307                                                             "Sort citerium",
308                                                             "The sort criterium to use for sorting the contact list",
309                                                             EMPATHY_TYPE_CONTACT_LIST_STORE_SORT,
310                                                             EMPATHY_CONTACT_LIST_STORE_SORT_NAME,
311                                                             G_PARAM_READWRITE));
312
313         g_type_class_add_private (object_class, sizeof (EmpathyContactListStorePriv));
314 }
315
316 static void
317 g_queue_free_full_iter (gpointer data)
318 {
319         GQueue *queue = (GQueue *) data;
320         g_queue_foreach (queue, (GFunc) gtk_tree_iter_free, NULL);
321         g_queue_free (queue);
322 }
323
324
325 static void
326 empathy_contact_list_store_init (EmpathyContactListStore *store)
327 {
328         EmpathyContactListStorePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (store,
329                 EMPATHY_TYPE_CONTACT_LIST_STORE, EmpathyContactListStorePriv);
330
331         store->priv = priv;
332         priv->show_avatars = TRUE;
333         priv->show_groups = TRUE;
334         priv->show_protocols = FALSE;
335         priv->inhibit_active = g_timeout_add_seconds (ACTIVE_USER_WAIT_TO_ENABLE_TIME,
336                                                       (GSourceFunc) contact_list_store_inibit_active_cb,
337                                                       store);
338         priv->status_icons = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
339         priv->empathy_contact_cache = g_hash_table_new_full (NULL, NULL, NULL,
340                 g_queue_free_full_iter);
341         priv->empathy_group_cache = g_hash_table_new_full (g_str_hash,
342                 g_str_equal, g_free,
343                 (GDestroyNotify) gtk_tree_iter_free);
344         contact_list_store_setup (store);
345 }
346
347 static void
348 contact_list_store_dispose (GObject *object)
349 {
350         EmpathyContactListStorePriv *priv = GET_PRIV (object);
351         GList                       *contacts, *l;
352
353         if (priv->dispose_has_run)
354                 return;
355         priv->dispose_has_run = TRUE;
356
357         contacts = empathy_contact_list_get_members (priv->list);
358         for (l = contacts; l; l = l->next) {
359                 g_signal_handlers_disconnect_by_func (l->data,
360                                                       G_CALLBACK (contact_list_store_contact_updated_cb),
361                                                       object);
362
363                 g_object_unref (l->data);
364         }
365         g_list_free (contacts);
366
367         g_signal_handlers_disconnect_by_func (priv->list,
368                                               G_CALLBACK (contact_list_store_member_renamed_cb),
369                                               object);
370         g_signal_handlers_disconnect_by_func (priv->list,
371                                               G_CALLBACK (contact_list_store_members_changed_cb),
372                                               object);
373         g_signal_handlers_disconnect_by_func (priv->list,
374                                               G_CALLBACK (contact_list_store_groups_changed_cb),
375                                               object);
376         g_object_unref (priv->list);
377
378         if (priv->inhibit_active) {
379                 g_source_remove (priv->inhibit_active);
380         }
381
382         if (priv->setup_idle_id != 0) {
383                 g_source_remove (priv->setup_idle_id);
384         }
385
386         g_hash_table_destroy (priv->status_icons);
387         g_hash_table_destroy (priv->empathy_contact_cache);
388         g_hash_table_destroy (priv->empathy_group_cache);
389         G_OBJECT_CLASS (empathy_contact_list_store_parent_class)->dispose (object);
390 }
391
392 static void
393 contact_list_store_get_property (GObject    *object,
394                                  guint       param_id,
395                                  GValue     *value,
396                                  GParamSpec *pspec)
397 {
398         EmpathyContactListStorePriv *priv;
399
400         priv = GET_PRIV (object);
401
402         switch (param_id) {
403         case PROP_CONTACT_LIST:
404                 g_value_set_object (value, priv->list);
405                 break;
406         case PROP_SHOW_OFFLINE:
407                 g_value_set_boolean (value, priv->show_offline);
408                 break;
409         case PROP_SHOW_AVATARS:
410                 g_value_set_boolean (value, priv->show_avatars);
411                 break;
412         case PROP_SHOW_PROTOCOLS:
413                 g_value_set_boolean (value, priv->show_protocols);
414                 break;
415         case PROP_SHOW_GROUPS:
416                 g_value_set_boolean (value, priv->show_groups);
417                 break;
418         case PROP_IS_COMPACT:
419                 g_value_set_boolean (value, priv->is_compact);
420                 break;
421         case PROP_SORT_CRITERIUM:
422                 g_value_set_enum (value, priv->sort_criterium);
423                 break;
424         default:
425                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
426                 break;
427         };
428 }
429
430 static void
431 contact_list_store_set_property (GObject      *object,
432                                  guint         param_id,
433                                  const GValue *value,
434                                  GParamSpec   *pspec)
435 {
436         switch (param_id) {
437         case PROP_CONTACT_LIST:
438                 contact_list_store_set_contact_list (EMPATHY_CONTACT_LIST_STORE (object),
439                                                      g_value_get_object (value));
440                 break;
441         case PROP_SHOW_OFFLINE:
442                 empathy_contact_list_store_set_show_offline (EMPATHY_CONTACT_LIST_STORE (object),
443                                                             g_value_get_boolean (value));
444                 break;
445         case PROP_SHOW_AVATARS:
446                 empathy_contact_list_store_set_show_avatars (EMPATHY_CONTACT_LIST_STORE (object),
447                                                             g_value_get_boolean (value));
448                 break;
449         case PROP_SHOW_PROTOCOLS:
450                 empathy_contact_list_store_set_show_protocols (EMPATHY_CONTACT_LIST_STORE (object),
451                                                             g_value_get_boolean (value));
452                 break;
453         case PROP_SHOW_GROUPS:
454                 empathy_contact_list_store_set_show_groups (EMPATHY_CONTACT_LIST_STORE (object),
455                                                             g_value_get_boolean (value));
456                 break;
457         case PROP_IS_COMPACT:
458                 empathy_contact_list_store_set_is_compact (EMPATHY_CONTACT_LIST_STORE (object),
459                                                           g_value_get_boolean (value));
460                 break;
461         case PROP_SORT_CRITERIUM:
462                 empathy_contact_list_store_set_sort_criterium (EMPATHY_CONTACT_LIST_STORE (object),
463                                                               g_value_get_enum (value));
464                 break;
465         default:
466                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
467                 break;
468         };
469 }
470
471 EmpathyContactListStore *
472 empathy_contact_list_store_new (EmpathyContactList *list_iface)
473 {
474         g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (list_iface), NULL);
475
476         return g_object_new (EMPATHY_TYPE_CONTACT_LIST_STORE,
477                              "contact-list", list_iface,
478                              NULL);
479 }
480
481 EmpathyContactList *
482 empathy_contact_list_store_get_list_iface (EmpathyContactListStore *store)
483 {
484         EmpathyContactListStorePriv *priv;
485
486         g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), FALSE);
487
488         priv = GET_PRIV (store);
489
490         return priv->list;
491 }
492
493 gboolean
494 empathy_contact_list_store_get_show_offline (EmpathyContactListStore *store)
495 {
496         EmpathyContactListStorePriv *priv;
497
498         g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), FALSE);
499
500         priv = GET_PRIV (store);
501
502         return priv->show_offline;
503 }
504
505 void
506 empathy_contact_list_store_set_show_offline (EmpathyContactListStore *store,
507                                             gboolean                show_offline)
508 {
509         EmpathyContactListStorePriv *priv;
510         GList                      *contacts, *l;
511         gboolean                    show_active;
512
513         g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
514
515         priv = GET_PRIV (store);
516
517         priv->show_offline = show_offline;
518         show_active = priv->show_active;
519
520         /* Disable temporarily. */
521         priv->show_active = FALSE;
522
523         contacts = empathy_contact_list_get_members (priv->list);
524         for (l = contacts; l; l = l->next) {
525                 contact_list_store_contact_update (store, l->data);
526
527                 g_object_unref (l->data);
528         }
529         g_list_free (contacts);
530
531         /* Restore to original setting. */
532         priv->show_active = show_active;
533
534         g_object_notify (G_OBJECT (store), "show-offline");
535 }
536
537 gboolean
538 empathy_contact_list_store_get_show_avatars (EmpathyContactListStore *store)
539 {
540         EmpathyContactListStorePriv *priv;
541
542         g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), TRUE);
543
544         priv = GET_PRIV (store);
545
546         return priv->show_avatars;
547 }
548
549 void
550 empathy_contact_list_store_set_show_avatars (EmpathyContactListStore *store,
551                                             gboolean                show_avatars)
552 {
553         EmpathyContactListStorePriv *priv;
554         GtkTreeModel               *model;
555
556         g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
557
558         priv = GET_PRIV (store);
559
560         priv->show_avatars = show_avatars;
561
562         model = GTK_TREE_MODEL (store);
563
564         gtk_tree_model_foreach (model,
565                                 (GtkTreeModelForeachFunc)
566                                 contact_list_store_update_list_mode_foreach,
567                                 store);
568
569         g_object_notify (G_OBJECT (store), "show-avatars");
570 }
571
572
573 gboolean
574 empathy_contact_list_store_get_show_protocols (EmpathyContactListStore *store)
575 {
576         EmpathyContactListStorePriv *priv;
577
578         g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), TRUE);
579
580         priv = GET_PRIV (store);
581
582         return priv->show_protocols;
583 }
584
585 void
586 empathy_contact_list_store_set_show_protocols (EmpathyContactListStore *store,
587                                             gboolean                show_protocols)
588 {
589         EmpathyContactListStorePriv *priv;
590         GtkTreeModel               *model;
591
592         g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
593
594         priv = GET_PRIV (store);
595
596         priv->show_protocols = show_protocols;
597
598         model = GTK_TREE_MODEL (store);
599
600         gtk_tree_model_foreach (model,
601                                 (GtkTreeModelForeachFunc)
602                                 contact_list_store_update_list_mode_foreach,
603                                 store);
604
605         g_object_notify (G_OBJECT (store), "show-protocols");
606 }
607
608 gboolean
609 empathy_contact_list_store_get_show_groups (EmpathyContactListStore *store)
610 {
611         EmpathyContactListStorePriv *priv;
612
613         g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), TRUE);
614
615         priv = GET_PRIV (store);
616
617         return priv->show_groups;
618 }
619
620 void
621 empathy_contact_list_store_set_show_groups (EmpathyContactListStore *store,
622                                             gboolean                 show_groups)
623 {
624         EmpathyContactListStorePriv *priv;
625
626         g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
627
628         priv = GET_PRIV (store);
629
630         if (priv->show_groups == show_groups) {
631                 return;
632         }
633
634         priv->show_groups = show_groups;
635
636         if (priv->setup_idle_id == 0) {
637                 /* Remove all contacts and add them back, not optimized but
638                  * that's the easy way :)
639                  *
640                  * This is only done if there's not a pending setup idle
641                  * callback, otherwise it will race and the contacts will get
642                  * added twice */
643                 GList *contacts, *l;
644
645                 gtk_tree_store_clear (GTK_TREE_STORE (store));
646
647                 /* Also clear the cache */
648                 g_hash_table_remove_all (priv->empathy_contact_cache);
649                 g_hash_table_remove_all (priv->empathy_group_cache);
650
651                 contacts = empathy_contact_list_get_members (priv->list);
652                 for (l = contacts; l; l = l->next) {
653                         contact_list_store_members_changed_cb (priv->list,
654                                                                l->data,
655                                                                NULL, 0, NULL,
656                                                                TRUE,
657                                                                store);
658
659                         g_object_unref (l->data);
660                 }
661                 g_list_free (contacts);
662         }
663
664         g_object_notify (G_OBJECT (store), "show-groups");
665 }
666
667 gboolean
668 empathy_contact_list_store_get_is_compact (EmpathyContactListStore *store)
669 {
670         EmpathyContactListStorePriv *priv;
671
672         g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), TRUE);
673
674         priv = GET_PRIV (store);
675
676         return priv->is_compact;
677 }
678
679 void
680 empathy_contact_list_store_set_is_compact (EmpathyContactListStore *store,
681                                           gboolean                is_compact)
682 {
683         EmpathyContactListStorePriv *priv;
684         GtkTreeModel               *model;
685
686         g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
687
688         priv = GET_PRIV (store);
689
690         priv->is_compact = is_compact;
691
692         model = GTK_TREE_MODEL (store);
693
694         gtk_tree_model_foreach (model,
695                                 (GtkTreeModelForeachFunc)
696                                 contact_list_store_update_list_mode_foreach,
697                                 store);
698
699         g_object_notify (G_OBJECT (store), "is-compact");
700 }
701
702 EmpathyContactListStoreSort
703 empathy_contact_list_store_get_sort_criterium (EmpathyContactListStore *store)
704 {
705         EmpathyContactListStorePriv *priv;
706
707         g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), 0);
708
709         priv = GET_PRIV (store);
710
711         return priv->sort_criterium;
712 }
713
714 void
715 empathy_contact_list_store_set_sort_criterium (EmpathyContactListStore     *store,
716                                               EmpathyContactListStoreSort  sort_criterium)
717 {
718         EmpathyContactListStorePriv *priv;
719
720         g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
721
722         priv = GET_PRIV (store);
723
724         priv->sort_criterium = sort_criterium;
725
726         switch (sort_criterium) {
727         case EMPATHY_CONTACT_LIST_STORE_SORT_STATE:
728                 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
729                                                       EMPATHY_CONTACT_LIST_STORE_COL_STATUS,
730                                                       GTK_SORT_ASCENDING);
731                 break;
732
733         case EMPATHY_CONTACT_LIST_STORE_SORT_NAME:
734                 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
735                                                       EMPATHY_CONTACT_LIST_STORE_COL_NAME,
736                                                       GTK_SORT_ASCENDING);
737                 break;
738
739         default:
740                 g_assert_not_reached ();
741         }
742
743         g_object_notify (G_OBJECT (store), "sort-criterium");
744 }
745
746 gboolean
747 empathy_contact_list_store_row_separator_func (GtkTreeModel *model,
748                                               GtkTreeIter  *iter,
749                                               gpointer      data)
750 {
751         gboolean is_separator = FALSE;
752
753         g_return_val_if_fail (GTK_IS_TREE_MODEL (model), FALSE);
754
755         gtk_tree_model_get (model, iter,
756                             EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator,
757                             -1);
758
759         return is_separator;
760 }
761
762 gchar *
763 empathy_contact_list_store_get_parent_group (GtkTreeModel *model,
764                                             GtkTreePath  *path,
765                                             gboolean     *path_is_group,
766                                             gboolean     *is_fake_group)
767 {
768         GtkTreeIter  parent_iter, iter;
769         gchar       *name = NULL;
770         gboolean     is_group;
771         gboolean     fake;
772
773         g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
774
775         if (path_is_group) {
776                 *path_is_group = FALSE;
777         }
778
779         if (!gtk_tree_model_get_iter (model, &iter, path)) {
780                 return NULL;
781         }
782
783         gtk_tree_model_get (model, &iter,
784                             EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
785                             EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
786                             -1);
787
788         if (!is_group) {
789                 g_free (name);
790                 name = NULL;
791
792                 if (!gtk_tree_model_iter_parent (model, &parent_iter, &iter)) {
793                         return NULL;
794                 }
795
796                 iter = parent_iter;
797
798                 gtk_tree_model_get (model, &iter,
799                                     EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
800                                     EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
801                                     EMPATHY_CONTACT_LIST_STORE_COL_IS_FAKE_GROUP, &fake,
802                                     -1);
803                 if (!is_group) {
804                         g_free (name);
805                         return NULL;
806                 }
807         }
808
809         if (path_is_group) {
810                 *path_is_group = TRUE;
811         }
812
813         if (is_fake_group != NULL)
814                 *is_fake_group = fake;
815
816         return name;
817 }
818
819 static void
820 contact_list_store_setup (EmpathyContactListStore *store)
821 {
822         EmpathyContactListStorePriv *priv;
823         GType types[] = {
824                 GDK_TYPE_PIXBUF,      /* Status pixbuf */
825                 GDK_TYPE_PIXBUF,      /* Avatar pixbuf */
826                 G_TYPE_BOOLEAN,       /* Avatar pixbuf visible */
827                 G_TYPE_STRING,        /* Name */
828                 G_TYPE_UINT,          /* Presence type */
829                 G_TYPE_STRING,        /* Status string */
830                 G_TYPE_BOOLEAN,       /* Compact view */
831                 EMPATHY_TYPE_CONTACT, /* Contact type */
832                 G_TYPE_BOOLEAN,       /* Is group */
833                 G_TYPE_BOOLEAN,       /* Is active */
834                 G_TYPE_BOOLEAN,       /* Is online */
835                 G_TYPE_BOOLEAN,       /* Is separator */
836                 G_TYPE_BOOLEAN,       /* Can make audio calls */
837                 G_TYPE_BOOLEAN,       /* Can make video calls */
838                 EMPATHY_TYPE_CONTACT_LIST_FLAGS, /* Flags */
839                 G_TYPE_BOOLEAN,       /* Is a fake group */
840         };
841
842         priv = GET_PRIV (store);
843
844         gtk_tree_store_set_column_types (GTK_TREE_STORE (store),
845                                          EMPATHY_CONTACT_LIST_STORE_COL_COUNT,
846                                          types);
847
848         /* Set up sorting */
849         gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store),
850                                          EMPATHY_CONTACT_LIST_STORE_COL_NAME,
851                                          contact_list_store_name_sort_func,
852                                          store, NULL);
853         gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store),
854                                          EMPATHY_CONTACT_LIST_STORE_COL_STATUS,
855                                          contact_list_store_state_sort_func,
856                                          store, NULL);
857
858         priv->sort_criterium = EMPATHY_CONTACT_LIST_STORE_SORT_NAME;
859         empathy_contact_list_store_set_sort_criterium (store, priv->sort_criterium);
860 }
861
862 static gboolean
863 contact_list_store_inibit_active_cb (EmpathyContactListStore *store)
864 {
865         EmpathyContactListStorePriv *priv;
866
867         priv = GET_PRIV (store);
868
869         priv->show_active = TRUE;
870         priv->inhibit_active = 0;
871
872         return FALSE;
873 }
874
875 static void
876 contact_list_store_add_contact_and_connect (EmpathyContactListStore *store, EmpathyContact *contact)
877 {
878         g_signal_connect (contact, "notify::presence",
879                           G_CALLBACK (contact_list_store_contact_updated_cb),
880                           store);
881         g_signal_connect (contact, "notify::presence-message",
882                           G_CALLBACK (contact_list_store_contact_updated_cb),
883                           store);
884         g_signal_connect (contact, "notify::name",
885                           G_CALLBACK (contact_list_store_contact_updated_cb),
886                           store);
887         g_signal_connect (contact, "notify::avatar",
888                           G_CALLBACK (contact_list_store_contact_updated_cb),
889                           store);
890         g_signal_connect (contact, "notify::capabilities",
891                           G_CALLBACK (contact_list_store_contact_updated_cb),
892                           store);
893
894         contact_list_store_add_contact (store, contact);
895 }
896
897 static void
898 contact_list_store_remove_contact_and_disconnect (EmpathyContactListStore *store, EmpathyContact *contact)
899 {
900         g_signal_handlers_disconnect_by_func (contact,
901                                               G_CALLBACK (contact_list_store_contact_updated_cb),
902                                               store);
903
904         contact_list_store_remove_contact (store, contact);
905 }
906
907 static void
908 contact_list_store_members_changed_cb (EmpathyContactList      *list_iface,
909                                        EmpathyContact          *contact,
910                                        EmpathyContact          *actor,
911                                        guint                    reason,
912                                        gchar                   *message,
913                                        gboolean                 is_member,
914                                        EmpathyContactListStore *store)
915 {
916         DEBUG ("Contact %s (%d) %s",
917                 empathy_contact_get_id (contact),
918                 empathy_contact_get_handle (contact),
919                 is_member ? "added" : "removed");
920
921         if (is_member) {
922                 contact_list_store_add_contact_and_connect (store, contact);
923         } else {
924                 contact_list_store_remove_contact_and_disconnect (store, contact);
925         }
926 }
927
928 static void
929 contact_list_store_member_renamed_cb (EmpathyContactList      *list_iface,
930                                       EmpathyContact          *old_contact,
931                                       EmpathyContact          *new_contact,
932                                       guint                    reason,
933                                       gchar                   *message,
934                                       EmpathyContactListStore *store)
935 {
936         DEBUG ("Contact %s (%d) renamed to %s (%d)",
937                 empathy_contact_get_id (old_contact),
938                 empathy_contact_get_handle (old_contact),
939                 empathy_contact_get_id (new_contact),
940                 empathy_contact_get_handle (new_contact));
941
942         /* add the new contact */
943         contact_list_store_add_contact_and_connect (store, new_contact);
944
945         /* remove old contact */
946         contact_list_store_remove_contact_and_disconnect (store, old_contact);
947 }
948
949 static void
950 contact_list_store_groups_changed_cb (EmpathyContactList      *list_iface,
951                                       EmpathyContact          *contact,
952                                       gchar                   *group,
953                                       gboolean                 is_member,
954                                       EmpathyContactListStore *store)
955 {
956         EmpathyContactListStorePriv *priv;
957         gboolean                     show_active;
958
959         priv = GET_PRIV (store);
960
961         DEBUG ("Updating groups for contact %s (%d)",
962                 empathy_contact_get_id (contact),
963                 empathy_contact_get_handle (contact));
964
965         /* We do this to make sure the groups are correct, if not, we
966          * would have to check the groups already set up for each
967          * contact and then see what has been updated.
968          */
969         show_active = priv->show_active;
970         priv->show_active = FALSE;
971         contact_list_store_remove_contact (store, contact);
972         contact_list_store_add_contact (store, contact);
973         priv->show_active = show_active;
974 }
975
976 static void
977 add_contact_to_store (GtkTreeStore *store,
978                       GtkTreeIter *iter,
979                       GtkTreeIter *parent,
980                       EmpathyContact *contact,
981                       EmpathyContactListFlags flags)
982 {
983         EmpathyContactListStorePriv *priv = GET_PRIV (store);
984         GQueue *queue;
985
986         gtk_tree_store_insert_with_values (store, iter, parent, 0,
987                             EMPATHY_CONTACT_LIST_STORE_COL_NAME, empathy_contact_get_alias (contact),
988                             EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, contact,
989                             EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, FALSE,
990                             EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, FALSE,
991                             EMPATHY_CONTACT_LIST_STORE_COL_CAN_AUDIO_CALL,
992                               empathy_contact_get_capabilities (contact) &
993                                 EMPATHY_CAPABILITIES_AUDIO,
994                             EMPATHY_CONTACT_LIST_STORE_COL_CAN_VIDEO_CALL,
995                               empathy_contact_get_capabilities (contact) &
996                                 EMPATHY_CAPABILITIES_VIDEO,
997                             EMPATHY_CONTACT_LIST_STORE_COL_FLAGS, flags,
998                             -1);
999
1000         queue = g_hash_table_lookup (priv->empathy_contact_cache, contact);
1001         if (queue) {
1002                 g_queue_push_tail (queue, gtk_tree_iter_copy (iter));
1003         } else {
1004                 queue = g_queue_new ();
1005                 g_queue_push_tail (queue, gtk_tree_iter_copy (iter));
1006                 g_hash_table_insert (priv->empathy_contact_cache, contact,
1007                         queue);
1008         }
1009 }
1010
1011 static void
1012 contact_list_store_add_contact (EmpathyContactListStore *store,
1013                                 EmpathyContact          *contact)
1014 {
1015         EmpathyContactListStorePriv *priv;
1016         GtkTreeIter                 iter;
1017         GList                      *groups = NULL, *l;
1018         TpConnection               *connection;
1019         EmpathyContactListFlags     flags = 0;
1020         char                       *protocol_name;
1021
1022         priv = GET_PRIV (store);
1023
1024         if (EMP_STR_EMPTY (empathy_contact_get_alias (contact)) ||
1025             (!priv->show_offline && !empathy_contact_is_online (contact))) {
1026                 return;
1027         }
1028
1029         if (priv->show_groups) {
1030                 groups = empathy_contact_list_get_groups (priv->list, contact);
1031         }
1032
1033         connection = empathy_contact_get_connection (contact);
1034         if (EMPATHY_IS_CONTACT_MANAGER (priv->list)) {
1035                 flags = empathy_contact_manager_get_flags_for_connection (
1036                         EMPATHY_CONTACT_MANAGER (priv->list), connection);
1037         }
1038
1039         tp_connection_parse_object_path (connection, &protocol_name, NULL);
1040
1041         if (!groups) {
1042                 GtkTreeIter iter_group, *parent;
1043
1044                 parent = &iter_group;
1045
1046                 if (!priv->show_groups) {
1047                         parent = NULL;
1048                 }
1049
1050                 add_contact_to_store (GTK_TREE_STORE (store), &iter, parent,
1051                                       contact, flags);
1052         }
1053
1054         g_free (protocol_name);
1055
1056         /* Else add to each group. */
1057         for (l = groups; l; l = l->next) {
1058                 GtkTreeIter iter_group;
1059
1060                 contact_list_store_get_group (store, l->data, &iter_group, NULL, NULL, FALSE);
1061
1062                 add_contact_to_store (GTK_TREE_STORE (store), &iter, &iter_group, contact, flags);
1063                 g_free (l->data);
1064         }
1065         g_list_free (groups);
1066
1067         contact_list_store_contact_update (store, contact);
1068 }
1069
1070 static void
1071 contact_list_store_remove_contact (EmpathyContactListStore *store,
1072                                    EmpathyContact          *contact)
1073 {
1074         EmpathyContactListStorePriv *priv = GET_PRIV (store);
1075         GtkTreeModel               *model;
1076         GList                      *l;
1077         GQueue                     *row_refs;
1078
1079         row_refs = g_hash_table_lookup (priv->empathy_contact_cache, contact);
1080         if (!row_refs) {
1081                 return;
1082         }
1083
1084         /* Clean up model */
1085         model = GTK_TREE_MODEL (store);
1086
1087         for (l = g_queue_peek_head_link (row_refs); l; l = l->next) {
1088                 GtkTreeIter *iter = l->data;
1089                 GtkTreeIter parent;
1090
1091                 /* NOTE: it is only <= 2 here because we have
1092                  * separators after the group name, otherwise it
1093                  * should be 1.
1094                  */
1095                 if (gtk_tree_model_iter_parent (model, &parent, iter) &&
1096                     gtk_tree_model_iter_n_children (model, &parent) <= 2) {
1097                         gchar *group_name;
1098                         gtk_tree_model_get (model, &parent,
1099                             EMPATHY_CONTACT_LIST_STORE_COL_NAME, &group_name,
1100                             -1);
1101                         g_hash_table_remove (priv->empathy_group_cache,
1102                                 group_name);
1103                         gtk_tree_store_remove (GTK_TREE_STORE (store), &parent);
1104                 } else {
1105                         gtk_tree_store_remove (GTK_TREE_STORE (store), iter);
1106                 }
1107         }
1108
1109         g_hash_table_remove (priv->empathy_contact_cache, contact);
1110 }
1111
1112 static void
1113 contact_list_store_contact_update (EmpathyContactListStore *store,
1114                                    EmpathyContact          *contact)
1115 {
1116         EmpathyContactListStorePriv *priv;
1117         ShowActiveData             *data;
1118         GtkTreeModel               *model;
1119         GList                      *iters, *l;
1120         gboolean                    in_list;
1121         gboolean                    should_be_in_list;
1122         gboolean                    was_online = TRUE;
1123         gboolean                    now_online = FALSE;
1124         gboolean                    set_model = FALSE;
1125         gboolean                    do_remove = FALSE;
1126         gboolean                    do_set_active = FALSE;
1127         gboolean                    do_set_refresh = FALSE;
1128         gboolean                    show_avatar = FALSE;
1129         GdkPixbuf                  *pixbuf_avatar;
1130         GdkPixbuf                  *pixbuf_status;
1131
1132         priv = GET_PRIV (store);
1133
1134         model = GTK_TREE_MODEL (store);
1135
1136         iters = contact_list_store_find_contact (store, contact);
1137         if (!iters) {
1138                 in_list = FALSE;
1139         } else {
1140                 in_list = TRUE;
1141         }
1142
1143         /* Get online state now. */
1144         now_online = empathy_contact_is_online (contact);
1145
1146         if (priv->show_offline || now_online) {
1147                 should_be_in_list = TRUE;
1148         } else {
1149                 should_be_in_list = FALSE;
1150         }
1151
1152         if (!in_list && !should_be_in_list) {
1153                 /* Nothing to do. */
1154                 DEBUG ("Contact:'%s' in list:NO, should be:NO",
1155                         empathy_contact_get_alias (contact));
1156
1157                 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
1158                 g_list_free (iters);
1159                 return;
1160         }
1161         else if (in_list && !should_be_in_list) {
1162                 DEBUG ("Contact:'%s' in list:YES, should be:NO",
1163                         empathy_contact_get_alias (contact));
1164
1165                 if (priv->show_active) {
1166                         do_remove = TRUE;
1167                         do_set_active = TRUE;
1168                         do_set_refresh = TRUE;
1169
1170                         set_model = TRUE;
1171                         DEBUG ("Remove item (after timeout)");
1172                 } else {
1173                         DEBUG ("Remove item (now)!");
1174                         contact_list_store_remove_contact (store, contact);
1175                 }
1176         }
1177         else if (!in_list && should_be_in_list) {
1178                 DEBUG ("Contact:'%s' in list:NO, should be:YES",
1179                         empathy_contact_get_alias (contact));
1180
1181                 contact_list_store_add_contact (store, contact);
1182
1183                 if (priv->show_active) {
1184                         do_set_active = TRUE;
1185
1186                         DEBUG ("Set active (contact added)");
1187                 }
1188         } else {
1189                 DEBUG ("Contact:'%s' in list:YES, should be:YES",
1190                         empathy_contact_get_alias (contact));
1191
1192                 /* Get online state before. */
1193                 if (iters && g_list_length (iters) > 0) {
1194                         gtk_tree_model_get (model, iters->data,
1195                                             EMPATHY_CONTACT_LIST_STORE_COL_IS_ONLINE, &was_online,
1196                                             -1);
1197                 }
1198
1199                 /* Is this really an update or an online/offline. */
1200                 if (priv->show_active) {
1201                         if (was_online != now_online) {
1202                                 do_set_active = TRUE;
1203                                 do_set_refresh = TRUE;
1204
1205                                 DEBUG ("Set active (contact updated %s)",
1206                                         was_online ? "online  -> offline" :
1207                                         "offline -> online");
1208                         } else {
1209                                 /* Was TRUE for presence updates. */
1210                                 /* do_set_active = FALSE;  */
1211                                 do_set_refresh = TRUE;
1212
1213                                 DEBUG ("Set active (contact updated)");
1214                         }
1215                 }
1216
1217                 set_model = TRUE;
1218         }
1219
1220         if (priv->show_avatars && !priv->is_compact) {
1221                 show_avatar = TRUE;
1222         }
1223         pixbuf_avatar = empathy_pixbuf_avatar_from_contact_scaled (contact, 32, 32);
1224         pixbuf_status = contact_list_store_get_contact_status_icon (store, contact);
1225         for (l = iters; l && set_model; l = l->next) {
1226                 gtk_tree_store_set (GTK_TREE_STORE (store), l->data,
1227                                     EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, pixbuf_status,
1228                                     EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR, pixbuf_avatar,
1229                                     EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
1230                                     EMPATHY_CONTACT_LIST_STORE_COL_NAME, empathy_contact_get_alias (contact),
1231                                     EMPATHY_CONTACT_LIST_STORE_COL_PRESENCE_TYPE,
1232                                       empathy_contact_get_presence (contact),
1233                                     EMPATHY_CONTACT_LIST_STORE_COL_STATUS,
1234                                       empathy_contact_get_presence_message (contact),
1235                                     EMPATHY_CONTACT_LIST_STORE_COL_COMPACT, priv->is_compact,
1236                                     EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, FALSE,
1237                                     EMPATHY_CONTACT_LIST_STORE_COL_IS_ONLINE, now_online,
1238                                     EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, FALSE,
1239                                     EMPATHY_CONTACT_LIST_STORE_COL_CAN_AUDIO_CALL,
1240                                       empathy_contact_get_capabilities (contact) &
1241                                         EMPATHY_CAPABILITIES_AUDIO,
1242                                     EMPATHY_CONTACT_LIST_STORE_COL_CAN_VIDEO_CALL,
1243                                       empathy_contact_get_capabilities (contact) &
1244                                         EMPATHY_CAPABILITIES_VIDEO,
1245                                     -1);
1246         }
1247
1248         if (pixbuf_avatar) {
1249                 g_object_unref (pixbuf_avatar);
1250         }
1251
1252         if (priv->show_active && do_set_active) {
1253                 contact_list_store_contact_set_active (store, contact, do_set_active, do_set_refresh);
1254
1255                 if (do_set_active) {
1256                         data = contact_list_store_contact_active_new (store, contact, do_remove);
1257                         g_timeout_add_seconds (ACTIVE_USER_SHOW_TIME,
1258                                                (GSourceFunc) contact_list_store_contact_active_cb,
1259                                                data);
1260                 }
1261         }
1262
1263         /* FIXME: when someone goes online then offline quickly, the
1264          * first timeout sets the user to be inactive and the second
1265          * timeout removes the user from the contact list, really we
1266          * should remove the first timeout.
1267          */
1268         g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
1269         g_list_free (iters);
1270 }
1271
1272 static void
1273 contact_list_store_contact_updated_cb (EmpathyContact          *contact,
1274                                        GParamSpec              *param,
1275                                        EmpathyContactListStore *store)
1276 {
1277         DEBUG ("Contact:'%s' updated, checking roster is in sync...",
1278                 empathy_contact_get_alias (contact));
1279
1280         contact_list_store_contact_update (store, contact);
1281 }
1282
1283 static void
1284 contact_list_store_contact_set_active (EmpathyContactListStore *store,
1285                                        EmpathyContact          *contact,
1286                                        gboolean                active,
1287                                        gboolean                set_changed)
1288 {
1289         GtkTreeModel               *model;
1290         GList                      *iters, *l;
1291
1292         model = GTK_TREE_MODEL (store);
1293
1294         iters = contact_list_store_find_contact (store, contact);
1295         for (l = iters; l; l = l->next) {
1296                 GtkTreePath *path;
1297
1298                 gtk_tree_store_set (GTK_TREE_STORE (store), l->data,
1299                                     EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, active,
1300                                     -1);
1301
1302                 DEBUG ("Set item %s", active ? "active" : "inactive");
1303
1304                 if (set_changed) {
1305                         path = gtk_tree_model_get_path (model, l->data);
1306                         gtk_tree_model_row_changed (model, path, l->data);
1307                         gtk_tree_path_free (path);
1308                 }
1309         }
1310
1311         g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
1312         g_list_free (iters);
1313
1314 }
1315
1316 static ShowActiveData *
1317 contact_list_store_contact_active_new (EmpathyContactListStore *store,
1318                                        EmpathyContact          *contact,
1319                                        gboolean                remove_)
1320 {
1321         ShowActiveData *data;
1322
1323         DEBUG ("Contact:'%s' now active, and %s be removed",
1324                 empathy_contact_get_alias (contact),
1325                 remove_ ? "WILL" : "WILL NOT");
1326
1327         data = g_slice_new0 (ShowActiveData);
1328
1329         data->store = g_object_ref (store);
1330         data->contact = g_object_ref (contact);
1331         data->remove = remove_;
1332
1333         return data;
1334 }
1335
1336 static void
1337 contact_list_store_contact_active_free (ShowActiveData *data)
1338 {
1339         g_object_unref (data->contact);
1340         g_object_unref (data->store);
1341
1342         g_slice_free (ShowActiveData, data);
1343 }
1344
1345 static gboolean
1346 contact_list_store_contact_active_cb (ShowActiveData *data)
1347 {
1348         EmpathyContactListStorePriv *priv;
1349
1350         priv = GET_PRIV (data->store);
1351
1352         if (data->remove &&
1353             !priv->show_offline &&
1354             !empathy_contact_is_online (data->contact)) {
1355                 DEBUG ("Contact:'%s' active timeout, removing item",
1356                         empathy_contact_get_alias (data->contact));
1357                 contact_list_store_remove_contact (data->store, data->contact);
1358         }
1359
1360         DEBUG ("Contact:'%s' no longer active",
1361                 empathy_contact_get_alias (data->contact));
1362
1363         contact_list_store_contact_set_active (data->store,
1364                                                data->contact,
1365                                                FALSE,
1366                                                TRUE);
1367
1368         contact_list_store_contact_active_free (data);
1369
1370         return FALSE;
1371 }
1372
1373 static void
1374 contact_list_store_get_group (EmpathyContactListStore *store,
1375                               const gchar            *name,
1376                               GtkTreeIter            *iter_group_to_set,
1377                               GtkTreeIter            *iter_separator_to_set,
1378                               gboolean               *created,
1379                               gboolean               is_fake_group)
1380 {
1381         EmpathyContactListStorePriv *priv = GET_PRIV (store);
1382         GtkTreeModel                *model;
1383         GtkTreeIter                  iter_group;
1384         GtkTreeIter                  iter_separator;
1385         GtkTreeIter                 *iter;
1386
1387         model = GTK_TREE_MODEL (store);
1388         iter = g_hash_table_lookup (priv->empathy_group_cache, name);
1389
1390         if (iter == NULL) {
1391                 if (created) {
1392                         *created = TRUE;
1393                 }
1394
1395                 gtk_tree_store_insert_with_values (GTK_TREE_STORE (store), &iter_group, NULL, 0,
1396                                     EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, NULL,
1397                                     EMPATHY_CONTACT_LIST_STORE_COL_NAME, name,
1398                                     EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, TRUE,
1399                                     EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, FALSE,
1400                                     EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, FALSE,
1401                                     EMPATHY_CONTACT_LIST_STORE_COL_IS_FAKE_GROUP, is_fake_group,
1402                                     -1);
1403
1404                 g_hash_table_insert (priv->empathy_group_cache,
1405                         g_strdup (name), gtk_tree_iter_copy (&iter_group));
1406
1407                 if (iter_group_to_set) {
1408                         *iter_group_to_set = iter_group;
1409                 }
1410
1411                 gtk_tree_store_insert_with_values (GTK_TREE_STORE (store), &iter_separator, &iter_group, 0,
1412                                     EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, TRUE,
1413                                     -1);
1414
1415                 if (iter_separator_to_set) {
1416                         *iter_separator_to_set = iter_separator;
1417                 }
1418         } else {
1419                 if (created) {
1420                         *created = FALSE;
1421                 }
1422
1423                 if (iter_group_to_set) {
1424                         *iter_group_to_set = *iter;
1425                 }
1426
1427                 iter_separator = *iter;
1428
1429                 if (gtk_tree_model_iter_next (model, &iter_separator)) {
1430                         gboolean is_separator;
1431
1432                         gtk_tree_model_get (model, &iter_separator,
1433                                             EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator,
1434                                             -1);
1435
1436                         if (is_separator && iter_separator_to_set) {
1437                                 *iter_separator_to_set = iter_separator;
1438                         }
1439                 }
1440         }
1441 }
1442
1443 static gint
1444 compare_separator_and_groups (gboolean is_separator_a,
1445                               gboolean is_separator_b,
1446                               const gchar *name_a,
1447                               const gchar *name_b,
1448                               EmpathyContact *contact_a,
1449                               EmpathyContact *contact_b,
1450                               gboolean fake_group_a,
1451                               gboolean fake_group_b)
1452 {
1453         if (is_separator_a || is_separator_b) {
1454                 /* We have at least one separator */
1455                 if (is_separator_a) {
1456                         return -1;
1457                 } else if (is_separator_b) {
1458                         return 1;
1459                 }
1460         }
1461
1462         /* One group and one contact */
1463         if (!contact_a && contact_b) {
1464                 return 1;
1465         } else if (contact_a && !contact_b) {
1466                 return -1;
1467         } else if (!contact_a && !contact_b) {
1468                 return g_utf8_collate (name_a, name_b);
1469         }
1470
1471         /* Two contacts, ordering depends of the sorting policy */
1472         return 0;
1473 }
1474
1475 static gint
1476 contact_list_store_contact_sort (EmpathyContact *contact_a,
1477                                  EmpathyContact *contact_b)
1478 {
1479         TpAccount *account_a, *account_b;
1480         gint ret_val;
1481
1482         g_return_val_if_fail (contact_a != NULL || contact_b != NULL, 0);
1483
1484         /* alias */
1485         ret_val = g_utf8_collate (empathy_contact_get_alias (contact_a),
1486                                   empathy_contact_get_alias (contact_b));
1487
1488         if (ret_val != 0)
1489                 goto out;
1490
1491         /* identifier */
1492         ret_val = g_utf8_collate (empathy_contact_get_id (contact_a),
1493                                   empathy_contact_get_id (contact_b));
1494
1495         if (ret_val != 0)
1496                 goto out;
1497
1498         account_a = empathy_contact_get_account (contact_a);
1499         account_b = empathy_contact_get_account (contact_b);
1500
1501         /* protocol */
1502         ret_val = strcmp (tp_account_get_protocol (account_a),
1503                           tp_account_get_protocol (account_b));
1504
1505         if (ret_val != 0)
1506                 goto out;
1507
1508         /* account ID */
1509         ret_val = strcmp (tp_proxy_get_object_path (account_a),
1510                           tp_proxy_get_object_path (account_b));
1511
1512 out:
1513         return ret_val;
1514 }
1515
1516 static gint
1517 contact_list_store_state_sort_func (GtkTreeModel *model,
1518                                     GtkTreeIter  *iter_a,
1519                                     GtkTreeIter  *iter_b,
1520                                     gpointer      user_data)
1521 {
1522         gint            ret_val;
1523         gchar          *name_a, *name_b;
1524         gboolean        is_separator_a, is_separator_b;
1525         EmpathyContact *contact_a, *contact_b;
1526         gboolean       fake_group_a, fake_group_b;
1527
1528         gtk_tree_model_get (model, iter_a,
1529                             EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name_a,
1530                             EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact_a,
1531                             EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator_a,
1532                             EMPATHY_CONTACT_LIST_STORE_COL_IS_FAKE_GROUP, &fake_group_a,
1533                             -1);
1534         gtk_tree_model_get (model, iter_b,
1535                             EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name_b,
1536                             EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact_b,
1537                             EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator_b,
1538                             EMPATHY_CONTACT_LIST_STORE_COL_IS_FAKE_GROUP, &fake_group_b,
1539                             -1);
1540
1541         if (contact_a == NULL || contact_b == NULL) {
1542                 ret_val = compare_separator_and_groups (is_separator_a, is_separator_b,
1543                         name_a, name_b, contact_a, contact_b, fake_group_a, fake_group_b);
1544                 goto free_and_out;
1545         }
1546
1547         /* If we managed to get this far, we can start looking at
1548          * the presences.
1549          */
1550         ret_val = -tp_connection_presence_type_cmp_availability (
1551                 empathy_contact_get_presence (EMPATHY_CONTACT (contact_a)),
1552                 empathy_contact_get_presence (EMPATHY_CONTACT (contact_b)));
1553
1554         if (ret_val == 0) {
1555                 /* Fallback: compare by name et al. */
1556                 ret_val = contact_list_store_contact_sort (contact_a, contact_b);
1557         }
1558
1559 free_and_out:
1560         g_free (name_a);
1561         g_free (name_b);
1562
1563         if (contact_a) {
1564                 g_object_unref (contact_a);
1565         }
1566
1567         if (contact_b) {
1568                 g_object_unref (contact_b);
1569         }
1570
1571         return ret_val;
1572 }
1573
1574 static gint
1575 contact_list_store_name_sort_func (GtkTreeModel *model,
1576                                    GtkTreeIter  *iter_a,
1577                                    GtkTreeIter  *iter_b,
1578                                    gpointer      user_data)
1579 {
1580         gchar         *name_a, *name_b;
1581         EmpathyContact *contact_a, *contact_b;
1582         gboolean       is_separator_a = FALSE, is_separator_b = FALSE;
1583         gint           ret_val;
1584         gboolean       fake_group_a, fake_group_b;
1585
1586         gtk_tree_model_get (model, iter_a,
1587                             EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name_a,
1588                             EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact_a,
1589                             EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator_a,
1590                             EMPATHY_CONTACT_LIST_STORE_COL_IS_FAKE_GROUP, &fake_group_a,
1591                             -1);
1592         gtk_tree_model_get (model, iter_b,
1593                             EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name_b,
1594                             EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact_b,
1595                             EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator_b,
1596                             EMPATHY_CONTACT_LIST_STORE_COL_IS_FAKE_GROUP, &fake_group_b,
1597                             -1);
1598
1599         if (contact_a == NULL || contact_b == NULL)
1600                 ret_val = compare_separator_and_groups (is_separator_a, is_separator_b,
1601                         name_a, name_b, contact_a, contact_b, fake_group_a, fake_group_b);
1602         else
1603                 ret_val = contact_list_store_contact_sort (contact_a, contact_b);
1604
1605         if (contact_a) {
1606                 g_object_unref (contact_a);
1607         }
1608
1609         if (contact_b) {
1610                 g_object_unref (contact_b);
1611         }
1612
1613         return ret_val;
1614 }
1615
1616 static GList *
1617 contact_list_store_find_contact (EmpathyContactListStore *store,
1618                                  EmpathyContact          *contact)
1619 {
1620         EmpathyContactListStorePriv *priv = GET_PRIV (store);
1621         GQueue                    *row_refs_queue;
1622         GList                     *i;
1623         GList                     *iters_list = NULL;
1624
1625         row_refs_queue = g_hash_table_lookup (priv->empathy_contact_cache, contact);
1626         if (!row_refs_queue)
1627                 return NULL;
1628
1629         for (i = g_queue_peek_head_link (row_refs_queue) ; i != NULL ;
1630              i = i->next) {
1631                 GtkTreeIter *iter = i->data;
1632
1633                 iters_list = g_list_prepend
1634                         (iters_list, gtk_tree_iter_copy (iter));
1635         }
1636
1637         return iters_list;
1638 }
1639
1640 static gboolean
1641 contact_list_store_update_list_mode_foreach (GtkTreeModel           *model,
1642                                              GtkTreePath            *path,
1643                                              GtkTreeIter            *iter,
1644                                              EmpathyContactListStore *store)
1645 {
1646         EmpathyContactListStorePriv *priv;
1647         gboolean                     show_avatar = FALSE;
1648         EmpathyContact              *contact;
1649         GdkPixbuf                   *pixbuf_status;
1650
1651         priv = GET_PRIV (store);
1652
1653         if (priv->show_avatars && !priv->is_compact) {
1654                 show_avatar = TRUE;
1655         }
1656
1657         gtk_tree_model_get (model, iter,
1658                             EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
1659                             -1);
1660
1661         if (contact == NULL){
1662                 return FALSE;
1663         }
1664         /* get icon from hash_table */
1665         pixbuf_status = contact_list_store_get_contact_status_icon (store, contact);
1666
1667         gtk_tree_store_set (GTK_TREE_STORE (store), iter,
1668                             EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, pixbuf_status,
1669                             EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
1670                             EMPATHY_CONTACT_LIST_STORE_COL_COMPACT, priv->is_compact,
1671                             -1);
1672
1673         return FALSE;
1674 }
1675
1676 static GdkPixbuf *
1677 contact_list_store_get_contact_status_icon_with_icon_name (
1678                                         EmpathyContactListStore *store,
1679                                         EmpathyContact *contact,
1680                                         const gchar *status_icon_name)
1681 {
1682         GdkPixbuf                   *pixbuf_status = NULL;
1683         EmpathyContactListStorePriv *priv;
1684         const gchar                 *protocol_name = NULL;
1685         gchar                       *icon_name = NULL;
1686
1687         priv = GET_PRIV (store);
1688
1689         if (priv->show_protocols) {
1690                 protocol_name = empathy_protocol_name_for_contact (contact);
1691                 icon_name = g_strdup_printf ("%s-%s", status_icon_name, protocol_name);
1692         } else {
1693                 icon_name = g_strdup_printf ("%s", status_icon_name);
1694         }
1695         pixbuf_status = g_hash_table_lookup (priv->status_icons, icon_name);
1696         if (pixbuf_status == NULL) {
1697                 pixbuf_status = empathy_pixbuf_contact_status_icon_with_icon_name (contact,
1698                                     status_icon_name,
1699                                     priv->show_protocols);
1700                 if (pixbuf_status != NULL) {
1701                         g_hash_table_insert (priv->status_icons,
1702                             g_strdup (icon_name),
1703                             pixbuf_status);
1704                 }
1705         }
1706
1707         g_free (icon_name);
1708         return pixbuf_status;
1709 }
1710
1711 GdkPixbuf *
1712 contact_list_store_get_contact_status_icon (EmpathyContactListStore *store,
1713                                             EmpathyContact *contact)
1714 {
1715         GdkPixbuf                   *pixbuf_status = NULL;
1716         EmpathyContactListStorePriv *priv;
1717         const gchar                 *status_icon_name = NULL;
1718         gboolean                     composing = FALSE;
1719
1720         priv = GET_PRIV (store);
1721
1722         if (EMPATHY_IS_TP_CHAT (priv->list)) {
1723                 if (empathy_tp_chat_get_chat_state (EMPATHY_TP_CHAT (priv->list),
1724                       contact) ==
1725                         TP_CHANNEL_CHAT_STATE_COMPOSING)
1726                 composing = TRUE;
1727         }
1728
1729         if (composing) {
1730                 status_icon_name = EMPATHY_IMAGE_TYPING;
1731         } else {
1732                 status_icon_name = empathy_icon_name_for_contact (contact);
1733         }
1734
1735         if (status_icon_name == NULL)
1736                 return NULL;
1737
1738         pixbuf_status = contact_list_store_get_contact_status_icon_with_icon_name (
1739                             store,
1740                             contact,
1741                             status_icon_name);
1742
1743         return pixbuf_status;
1744 }