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