]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-contact-list-store.c
Show avatars by default.
[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 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., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, 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 <gtk/gtk.h>
32
33 #include <libempathy/empathy-debug.h>
34
35 #include "empathy-contact-list-store.h"
36 #include "empathy-contact-groups.h"
37 #include "empathy-ui-utils.h"
38
39 #define DEBUG_DOMAIN "ContactListStore"
40
41 /* Active users are those which have recently changed state
42  * (e.g. online, offline or from normal to a busy state).
43  */
44
45 /* Time user is shown as active */
46 #define ACTIVE_USER_SHOW_TIME 7000
47
48 /* Time after connecting which we wait before active users are enabled */
49 #define ACTIVE_USER_WAIT_TO_ENABLE_TIME 5000
50
51 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_CONTACT_LIST_STORE, EmpathyContactListStorePriv))
52
53 struct _EmpathyContactListStorePriv {
54         EmpathyContactList         *list;
55         gboolean                    show_offline;
56         gboolean                    show_avatars;
57         gboolean                    is_compact;
58         gboolean                    show_active;
59         EmpathyContactListStoreSort  sort_criterium;
60         guint                       inhibit_active;
61
62         EmpathyContactGroupsFunc     get_contact_groups;
63         gpointer                    get_contact_groups_data;
64 };
65
66 typedef struct {
67         GtkTreeIter  iter;
68         const gchar *name;
69         gboolean     found;
70 } FindGroup;
71
72 typedef struct {
73         EmpathyContact *contact;
74         gboolean       found;
75         GList         *iters;
76 } FindContact;
77
78 typedef struct {
79         EmpathyContactListStore *store;
80         EmpathyContact          *contact;
81         gboolean                remove;
82 } ShowActiveData;
83
84 static void             empathy_contact_list_store_class_init         (EmpathyContactListStoreClass *klass);
85 static void             empathy_contact_list_store_init               (EmpathyContactListStore      *list);
86 static void             contact_list_store_finalize                  (GObject                     *object);
87 static void             contact_list_store_get_property              (GObject                     *object,
88                                                                       guint                        param_id,
89                                                                       GValue                      *value,
90                                                                       GParamSpec                  *pspec);
91 static void             contact_list_store_set_property              (GObject                     *object,
92                                                                       guint                        param_id,
93                                                                       const GValue                *value,
94                                                                       GParamSpec                  *pspec);
95 static void             contact_list_store_setup                     (EmpathyContactListStore      *store);
96 static gboolean         contact_list_store_inibit_active_cb          (EmpathyContactListStore      *store);
97 static void             contact_list_store_contact_added_cb          (EmpathyContactList          *list_iface,
98                                                                       EmpathyContact               *contact,
99                                                                       EmpathyContactListStore      *store);
100 static void             contact_list_store_add_contact               (EmpathyContactListStore      *store,
101                                                                       EmpathyContact               *contact);
102 static void             contact_list_store_contact_removed_cb        (EmpathyContactList          *list_iface,
103                                                                       EmpathyContact               *contact,
104                                                                       EmpathyContactListStore      *store);
105 static void             contact_list_store_remove_contact            (EmpathyContactListStore      *store,
106                                                                       EmpathyContact               *contact);
107 static void             contact_list_store_contact_update            (EmpathyContactListStore      *store,
108                                                                       EmpathyContact               *contact);
109 static void             contact_list_store_contact_groups_updated_cb (EmpathyContact               *contact,
110                                                                       GParamSpec                  *param,
111                                                                       EmpathyContactListStore      *store);
112 static void             contact_list_store_contact_updated_cb        (EmpathyContact               *contact,
113                                                                       GParamSpec                  *param,
114                                                                       EmpathyContactListStore      *store);
115 static void             contact_list_store_contact_set_active        (EmpathyContactListStore      *store,
116                                                                       EmpathyContact               *contact,
117                                                                       gboolean                     active,
118                                                                       gboolean                     set_changed);
119 static ShowActiveData * contact_list_store_contact_active_new        (EmpathyContactListStore      *store,
120                                                                       EmpathyContact               *contact,
121                                                                       gboolean                     remove);
122 static void             contact_list_store_contact_active_free       (ShowActiveData              *data);
123 static gboolean         contact_list_store_contact_active_cb         (ShowActiveData              *data);
124 static gboolean         contact_list_store_get_group_foreach         (GtkTreeModel                *model,
125                                                                       GtkTreePath                 *path,
126                                                                       GtkTreeIter                 *iter,
127                                                                       FindGroup                   *fg);
128 static void             contact_list_store_get_group                 (EmpathyContactListStore      *store,
129                                                                       const gchar                 *name,
130                                                                       GtkTreeIter                 *iter_group_to_set,
131                                                                       GtkTreeIter                 *iter_separator_to_set,
132                                                                       gboolean                    *created);
133 static gint             contact_list_store_state_sort_func           (GtkTreeModel                *model,
134                                                                       GtkTreeIter                 *iter_a,
135                                                                       GtkTreeIter                 *iter_b,
136                                                                       gpointer                     user_data);
137 static gint             contact_list_store_name_sort_func            (GtkTreeModel                *model,
138                                                                       GtkTreeIter                 *iter_a,
139                                                                       GtkTreeIter                 *iter_b,
140                                                                       gpointer                     user_data);
141 static gboolean         contact_list_store_find_contact_foreach      (GtkTreeModel                *model,
142                                                                       GtkTreePath                 *path,
143                                                                       GtkTreeIter                 *iter,
144                                                                       FindContact                 *fc);
145 static GList *          contact_list_store_find_contact              (EmpathyContactListStore      *store,
146                                                                       EmpathyContact               *contact);
147 static gboolean         contact_list_store_update_list_mode_foreach  (GtkTreeModel                *model,
148                                                                       GtkTreePath                 *path,
149                                                                       GtkTreeIter                 *iter,
150                                                                       EmpathyContactListStore      *store);
151
152 enum {
153         PROP_0,
154         PROP_SHOW_OFFLINE,
155         PROP_SHOW_AVATARS,
156         PROP_IS_COMPACT,
157         PROP_SORT_CRITERIUM
158 };
159
160 GType
161 empathy_contact_list_store_sort_get_type (void)
162 {
163         static GType etype = 0;
164
165         if (etype == 0) {
166                 static const GEnumValue values[] = {
167                         { EMPATHY_CONTACT_LIST_STORE_SORT_NAME, 
168                           "EMPATHY_CONTACT_LIST_STORE_SORT_NAME", 
169                           "name" },
170                         { EMPATHY_CONTACT_LIST_STORE_SORT_STATE, 
171                           "EMPATHY_CONTACT_LIST_STORE_SORT_STATE", 
172                           "state" },
173                         { 0, NULL, NULL }
174                 };
175
176                 etype = g_enum_register_static ("EmpathyContactListStoreSort", values);
177         }
178
179         return etype;
180 }
181
182 G_DEFINE_TYPE (EmpathyContactListStore, empathy_contact_list_store, GTK_TYPE_TREE_STORE);
183
184 static void
185 empathy_contact_list_store_class_init (EmpathyContactListStoreClass *klass)
186 {
187         GObjectClass *object_class = G_OBJECT_CLASS (klass);
188
189         object_class->finalize = contact_list_store_finalize;
190         object_class->get_property = contact_list_store_get_property;
191         object_class->set_property = contact_list_store_set_property;
192
193         g_object_class_install_property (object_class,
194                                          PROP_SHOW_OFFLINE,
195                                          g_param_spec_boolean ("show-offline",
196                                                                "Show Offline",
197                                                                "Whether contact list should display "
198                                                                "offline contacts",
199                                                                FALSE,
200                                                                G_PARAM_READWRITE));
201          g_object_class_install_property (object_class,
202                                           PROP_SHOW_AVATARS,
203                                           g_param_spec_boolean ("show-avatars",
204                                                                 "Show Avatars",
205                                                                 "Whether contact list should display "
206                                                                 "avatars for contacts",
207                                                                 TRUE,
208                                                                 G_PARAM_READWRITE));
209         g_object_class_install_property (object_class,
210                                          PROP_IS_COMPACT,
211                                          g_param_spec_boolean ("is-compact",
212                                                                "Is Compact",
213                                                                "Whether the contact list is in compact mode or not",
214                                                                FALSE,
215                                                                G_PARAM_READWRITE));
216
217         g_object_class_install_property (object_class,
218                                          PROP_SORT_CRITERIUM,
219                                          g_param_spec_enum ("sort-criterium",
220                                                             "Sort citerium",
221                                                             "The sort criterium to use for sorting the contact list",
222                                                             EMPATHY_TYPE_CONTACT_LIST_STORE_SORT,
223                                                             EMPATHY_CONTACT_LIST_STORE_SORT_NAME,
224                                                             G_PARAM_READWRITE));
225
226         g_type_class_add_private (object_class, sizeof (EmpathyContactListStorePriv));
227 }
228
229 static void
230 empathy_contact_list_store_init (EmpathyContactListStore *store)
231 {
232         EmpathyContactListStorePriv *priv;
233
234         priv = GET_PRIV (store);
235
236         priv->show_avatars = TRUE;
237         priv->inhibit_active = g_timeout_add (ACTIVE_USER_WAIT_TO_ENABLE_TIME,
238                                               (GSourceFunc) contact_list_store_inibit_active_cb,
239                                               store);
240 }
241
242 static void
243 contact_list_store_finalize (GObject *object)
244 {
245         EmpathyContactListStorePriv *priv;
246
247         priv = GET_PRIV (object);
248
249         /* FIXME: disconnect all signals on the list and contacts */
250
251         if (priv->list) {
252                 g_object_unref (priv->list);
253         }
254
255         if (priv->inhibit_active) {
256                 g_source_remove (priv->inhibit_active);
257         }
258
259         G_OBJECT_CLASS (empathy_contact_list_store_parent_class)->finalize (object);
260 }
261
262 static void
263 contact_list_store_get_property (GObject    *object,
264                                  guint       param_id,
265                                  GValue     *value,
266                                  GParamSpec *pspec)
267 {
268         EmpathyContactListStorePriv *priv;
269
270         priv = GET_PRIV (object);
271
272         switch (param_id) {
273         case PROP_SHOW_OFFLINE:
274                 g_value_set_boolean (value, priv->show_offline);
275                 break;
276         case PROP_SHOW_AVATARS:
277                 g_value_set_boolean (value, priv->show_avatars);
278                 break;
279         case PROP_IS_COMPACT:
280                 g_value_set_boolean (value, priv->is_compact);
281                 break;
282         case PROP_SORT_CRITERIUM:
283                 g_value_set_enum (value, priv->sort_criterium);
284                 break;
285         default:
286                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
287                 break;
288         };
289 }
290
291 static void
292 contact_list_store_set_property (GObject      *object,
293                                  guint         param_id,
294                                  const GValue *value,
295                                  GParamSpec   *pspec)
296 {
297         EmpathyContactListStorePriv *priv;
298
299         priv = GET_PRIV (object);
300
301         switch (param_id) {
302         case PROP_SHOW_OFFLINE:
303                 empathy_contact_list_store_set_show_offline (EMPATHY_CONTACT_LIST_STORE (object),
304                                                             g_value_get_boolean (value));
305                 break;
306         case PROP_SHOW_AVATARS:
307                 empathy_contact_list_store_set_show_avatars (EMPATHY_CONTACT_LIST_STORE (object),
308                                                             g_value_get_boolean (value));
309                 break;
310         case PROP_IS_COMPACT:
311                 empathy_contact_list_store_set_is_compact (EMPATHY_CONTACT_LIST_STORE (object),
312                                                           g_value_get_boolean (value));
313                 break;
314         case PROP_SORT_CRITERIUM:
315                 empathy_contact_list_store_set_sort_criterium (EMPATHY_CONTACT_LIST_STORE (object),
316                                                               g_value_get_enum (value));
317                 break;
318         default:
319                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
320                 break;
321         };
322 }
323
324 EmpathyContactListStore *
325 empathy_contact_list_store_new (EmpathyContactList *list_iface)
326 {
327         EmpathyContactListStore     *store;
328         EmpathyContactListStorePriv *priv;
329         GList                      *contacts, *l;
330
331         g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (list_iface), NULL);
332
333         store = g_object_new (EMPATHY_TYPE_CONTACT_LIST_STORE, NULL);
334         priv = GET_PRIV (store);
335
336         contact_list_store_setup (store);
337         priv->list = g_object_ref (list_iface);
338
339         /* Signal connection. */
340         g_signal_connect (priv->list,
341                           "contact-added",
342                           G_CALLBACK (contact_list_store_contact_added_cb),
343                           store);
344         g_signal_connect (priv->list,
345                           "contact-removed",
346                           G_CALLBACK (contact_list_store_contact_removed_cb),
347                           store);
348
349         /* Add contacts already created. */
350         contacts = empathy_contact_list_get_members (priv->list);
351         for (l = contacts; l; l = l->next) {
352                 EmpathyContact *contact;
353
354                 contact = l->data;
355
356                 contact_list_store_contact_added_cb (priv->list, contact, store);
357
358                 g_object_unref (contact);
359         }
360         g_list_free (contacts);
361
362         return store;
363 }
364
365 EmpathyContactList *
366 empathy_contact_list_store_get_list_iface (EmpathyContactListStore *store)
367 {
368         EmpathyContactListStorePriv *priv;
369
370         g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), FALSE);
371
372         priv = GET_PRIV (store);
373
374         return priv->list;
375 }
376
377 gboolean
378 empathy_contact_list_store_get_show_offline (EmpathyContactListStore *store)
379 {
380         EmpathyContactListStorePriv *priv;
381
382         g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), FALSE);
383
384         priv = GET_PRIV (store);
385
386         return priv->show_offline;
387 }
388
389 void
390 empathy_contact_list_store_set_show_offline (EmpathyContactListStore *store,
391                                             gboolean                show_offline)
392 {
393         EmpathyContactListStorePriv *priv;
394         GList                      *contacts, *l;
395         gboolean                    show_active;
396
397         g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
398
399         priv = GET_PRIV (store);
400
401         priv->show_offline = show_offline;
402         show_active = priv->show_active;
403
404         /* Disable temporarily. */
405         priv->show_active = FALSE;
406
407         contacts = empathy_contact_list_get_members (priv->list);
408         for (l = contacts; l; l = l->next) {
409                 EmpathyContact *contact;
410
411                 contact = EMPATHY_CONTACT (l->data);
412
413                 contact_list_store_contact_update (store, contact);
414
415                 g_object_unref (contact);
416         }
417         g_list_free (contacts);
418
419         /* Restore to original setting. */
420         priv->show_active = show_active;
421 }
422
423 gboolean
424 empathy_contact_list_store_get_show_avatars (EmpathyContactListStore *store)
425 {
426         EmpathyContactListStorePriv *priv;
427
428         g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), TRUE);
429
430         priv = GET_PRIV (store);
431
432         return priv->show_avatars;
433 }
434
435 void
436 empathy_contact_list_store_set_show_avatars (EmpathyContactListStore *store,
437                                             gboolean                show_avatars)
438 {
439         EmpathyContactListStorePriv *priv;
440         GtkTreeModel               *model;
441
442         g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
443
444         priv = GET_PRIV (store);
445
446         priv->show_avatars = show_avatars;
447
448         model = GTK_TREE_MODEL (store);
449
450         gtk_tree_model_foreach (model,
451                                 (GtkTreeModelForeachFunc)
452                                 contact_list_store_update_list_mode_foreach,
453                                 store);
454 }
455
456 gboolean
457 empathy_contact_list_store_get_is_compact (EmpathyContactListStore *store)
458 {
459         EmpathyContactListStorePriv *priv;
460
461         g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), TRUE);
462
463         priv = GET_PRIV (store);
464
465         return priv->is_compact;
466 }
467
468 void
469 empathy_contact_list_store_set_is_compact (EmpathyContactListStore *store,
470                                           gboolean                is_compact)
471 {
472         EmpathyContactListStorePriv *priv;
473         GtkTreeModel               *model;
474
475         g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
476
477         priv = GET_PRIV (store);
478
479         priv->is_compact = is_compact;
480
481         model = GTK_TREE_MODEL (store);
482
483         gtk_tree_model_foreach (model,
484                                 (GtkTreeModelForeachFunc)
485                                 contact_list_store_update_list_mode_foreach,
486                                 store);
487 }
488
489 EmpathyContactListStoreSort
490 empathy_contact_list_store_get_sort_criterium (EmpathyContactListStore *store)
491 {
492         EmpathyContactListStorePriv *priv;
493
494         g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), 0);
495
496         priv = GET_PRIV (store);
497
498         return priv->sort_criterium;
499 }
500
501 void
502 empathy_contact_list_store_set_sort_criterium (EmpathyContactListStore     *store,
503                                               EmpathyContactListStoreSort  sort_criterium)
504 {
505         EmpathyContactListStorePriv *priv;
506
507         g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
508
509         priv = GET_PRIV (store);
510
511         priv->sort_criterium = sort_criterium;
512
513         switch (sort_criterium) {
514         case EMPATHY_CONTACT_LIST_STORE_SORT_STATE:
515                 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
516                                                       COL_STATUS,
517                                                       GTK_SORT_ASCENDING);
518                 break;
519                 
520         case EMPATHY_CONTACT_LIST_STORE_SORT_NAME:
521                 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
522                                                       COL_NAME,
523                                                       GTK_SORT_ASCENDING);
524                 break;
525         }
526 }
527
528 gboolean
529 empathy_contact_list_store_row_separator_func (GtkTreeModel *model,
530                                               GtkTreeIter  *iter,
531                                               gpointer      data)
532 {
533         gboolean is_separator = FALSE;
534
535         g_return_val_if_fail (GTK_IS_TREE_MODEL (model), FALSE);
536
537         gtk_tree_model_get (model, iter,
538                             COL_IS_SEPARATOR, &is_separator,
539                             -1);
540
541         return is_separator;
542 }
543
544 gchar *
545 empathy_contact_list_store_get_parent_group (GtkTreeModel *model,
546                                             GtkTreePath  *path,
547                                             gboolean     *path_is_group)
548 {
549         GtkTreeIter  parent_iter, iter;
550         gchar       *name = NULL;
551         gboolean     is_group;
552
553         g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
554
555         if (path_is_group) {
556                 *path_is_group = FALSE;
557         }
558
559         if (!gtk_tree_model_get_iter (model, &iter, path)) {
560                 return NULL;
561         }
562
563         gtk_tree_model_get (model, &iter,
564                             COL_IS_GROUP, &is_group,
565                             COL_NAME, &name,
566                             -1);
567
568         if (!is_group) {
569                 g_free (name);
570                 name = NULL;
571
572                 if (!gtk_tree_model_iter_parent (model, &parent_iter, &iter)) {
573                         return NULL;
574                 }
575
576                 iter = parent_iter;
577
578                 gtk_tree_model_get (model, &iter,
579                                     COL_IS_GROUP, &is_group,
580                                     COL_NAME, &name,
581                                     -1);
582                 if (!is_group) {
583                         g_free (name);
584                         return NULL;
585                 }
586         }
587
588         if (path_is_group) {
589                 *path_is_group = TRUE;
590         }
591
592         return name;
593 }
594
595 gboolean
596 empathy_contact_list_store_search_equal_func (GtkTreeModel *model,
597                                              gint          column,
598                                              const gchar  *key,
599                                              GtkTreeIter  *iter,
600                                              gpointer      search_data)
601 {
602         gchar    *name, *name_folded;
603         gchar    *key_folded;
604         gboolean  ret;
605
606         g_return_val_if_fail (GTK_IS_TREE_MODEL (model), FALSE);
607
608         if (!key) {
609                 return FALSE;
610         }
611
612         gtk_tree_model_get (model, iter, COL_NAME, &name, -1);
613
614         if (!name) {
615                 return FALSE;
616         }
617
618         name_folded = g_utf8_casefold (name, -1);
619         key_folded = g_utf8_casefold (key, -1);
620
621         if (name_folded && key_folded && 
622             strstr (name_folded, key_folded)) {
623                 ret = FALSE;
624         } else {
625                 ret = TRUE;
626         }
627
628         g_free (name);
629         g_free (name_folded);
630         g_free (key_folded);
631
632         return ret;
633 }
634
635 void
636 empathy_contact_list_store_set_contact_groups_func (EmpathyContactListStore  *store,
637                                                    EmpathyContactGroupsFunc  func,
638                                                    gpointer                 user_data)
639 {
640         EmpathyContactListStorePriv *priv;
641         GList                      *contacts, *l;
642
643         g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
644
645         priv = GET_PRIV (store);
646
647         if (func) {
648                 priv->get_contact_groups = func;
649                 priv->get_contact_groups_data = user_data;
650         } else {
651                 priv->get_contact_groups = NULL;
652                 priv->get_contact_groups_data = NULL;
653         }
654
655         /* If we set a custom function to get contacts groups  we have to
656          * disconnect our default notify::groups signal and wait for the user
657          * to call himself empathy_contact_list_store_update_contact_groups ()
658          * when needed. If func is NULL we come back to default.
659          */
660         contacts = empathy_contact_list_get_members (priv->list);
661         for (l = contacts; l; l = l->next) {
662                 EmpathyContact *contact;
663
664                 contact = l->data;
665
666                 if (func) {
667                         g_signal_handlers_disconnect_by_func (contact, 
668                                                               G_CALLBACK (contact_list_store_contact_groups_updated_cb),
669                                                               store);
670                 } else {
671                         g_signal_connect (contact, "notify::groups",
672                                           G_CALLBACK (contact_list_store_contact_groups_updated_cb),
673                                           store);
674                 }
675
676                 empathy_contact_list_store_update_contact_groups (store, contact);
677
678                 g_object_unref (contact);
679         }
680         g_list_free (contacts);
681 }
682
683 void
684 empathy_contact_list_store_update_contact_groups (EmpathyContactListStore *store,
685                                                  EmpathyContact          *contact)
686 {
687         EmpathyContactListStorePriv *priv;
688         gboolean                    show_active;
689
690         g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
691         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
692
693         priv = GET_PRIV (store);
694
695         empathy_debug (DEBUG_DOMAIN, "Contact:'%s' updating groups",
696                       empathy_contact_get_name (contact));
697
698         /* We do this to make sure the groups are correct, if not, we
699          * would have to check the groups already set up for each
700          * contact and then see what has been updated.
701          */
702         show_active = priv->show_active;
703         priv->show_active = FALSE;
704         contact_list_store_remove_contact (store, contact);
705         contact_list_store_add_contact (store, contact);
706         priv->show_active = show_active;
707 }
708
709 static void
710 contact_list_store_setup (EmpathyContactListStore *store)
711 {
712         EmpathyContactListStorePriv *priv;
713         GType                       types[] = {G_TYPE_STRING,       /* Status icon-name */
714                                                GDK_TYPE_PIXBUF,     /* Avatar pixbuf */
715                                                G_TYPE_BOOLEAN,      /* Avatar pixbuf visible */
716                                                G_TYPE_STRING,       /* Name */
717                                                G_TYPE_STRING,       /* Status string */
718                                                G_TYPE_BOOLEAN,      /* Show status */
719                                                EMPATHY_TYPE_CONTACT, /* Contact type */
720                                                G_TYPE_BOOLEAN,      /* Is group */
721                                                G_TYPE_BOOLEAN,      /* Is active */
722                                                G_TYPE_BOOLEAN,      /* Is online */
723                                                G_TYPE_BOOLEAN};     /* Is separator */
724         
725         priv = GET_PRIV (store);
726
727         gtk_tree_store_set_column_types (GTK_TREE_STORE (store), COL_COUNT, types);
728
729         /* Set up sorting */
730         gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store),
731                                          COL_NAME,
732                                          contact_list_store_name_sort_func,
733                                          store, NULL);
734         gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store),
735                                          COL_STATUS,
736                                          contact_list_store_state_sort_func,
737                                          store, NULL);
738
739         priv->sort_criterium = EMPATHY_CONTACT_LIST_STORE_SORT_NAME;
740         empathy_contact_list_store_set_sort_criterium (store, priv->sort_criterium);
741 }
742
743 static gboolean
744 contact_list_store_inibit_active_cb (EmpathyContactListStore *store)
745 {
746         EmpathyContactListStorePriv *priv;
747
748         priv = GET_PRIV (store);
749
750         priv->show_active = TRUE;
751         priv->inhibit_active = 0;
752
753         return FALSE;
754 }
755
756 static void
757 contact_list_store_contact_added_cb (EmpathyContactList     *list_iface,
758                                      EmpathyContact          *contact,
759                                      EmpathyContactListStore *store)
760 {
761         EmpathyContactListStorePriv *priv;
762
763         priv = GET_PRIV (store);
764
765         empathy_debug (DEBUG_DOMAIN, 
766                       "Contact:'%s' added",
767                       empathy_contact_get_name (contact));
768
769         if (!priv->get_contact_groups) {
770                 g_signal_connect (contact, "notify::groups",
771                                   G_CALLBACK (contact_list_store_contact_groups_updated_cb),
772                                   store);
773         }
774         g_signal_connect (contact, "notify::presence",
775                           G_CALLBACK (contact_list_store_contact_updated_cb),
776                           store);
777         g_signal_connect (contact, "notify::name",
778                           G_CALLBACK (contact_list_store_contact_updated_cb),
779                           store);
780         g_signal_connect (contact, "notify::avatar",
781                           G_CALLBACK (contact_list_store_contact_updated_cb),
782                           store);
783         g_signal_connect (contact, "notify::type",
784                           G_CALLBACK (contact_list_store_contact_updated_cb),
785                           store);
786
787         contact_list_store_add_contact (store, contact);
788 }
789
790 static void
791 contact_list_store_add_contact (EmpathyContactListStore *store,
792                                 EmpathyContact          *contact)
793 {
794         EmpathyContactListStorePriv *priv;
795         GtkTreeIter                 iter;
796         GList                      *groups, *l;
797
798         priv = GET_PRIV (store);
799         
800         if (!priv->show_offline && !empathy_contact_is_online (contact)) {
801                 return;
802         }
803
804         /* If no groups just add it at the top level. */
805         if (priv->get_contact_groups) {
806                 groups = priv->get_contact_groups (contact,
807                                                    priv->get_contact_groups_data);
808         } else {
809                 groups = empathy_contact_get_groups (contact);
810         }
811
812         if (!groups) {
813                 gtk_tree_store_append (GTK_TREE_STORE (store), &iter, NULL);
814                 gtk_tree_store_set (GTK_TREE_STORE (store), &iter,
815                                     COL_NAME, empathy_contact_get_name (contact),
816                                     COL_CONTACT, contact,
817                                     COL_IS_GROUP, FALSE,
818                                     COL_IS_SEPARATOR, FALSE,
819                                     -1);
820         }
821
822         /* Else add to each group. */
823         for (l = groups; l; l = l->next) {
824                 GtkTreeIter  iter_group;
825                 const gchar *name;
826
827                 name = l->data;
828                 if (!name) {
829                         continue;
830                 }
831
832                 contact_list_store_get_group (store, name, &iter_group, NULL, NULL);
833
834                 gtk_tree_store_insert_after (GTK_TREE_STORE (store), &iter,
835                                              &iter_group, NULL);
836                 gtk_tree_store_set (GTK_TREE_STORE (store), &iter,
837                                     COL_NAME, empathy_contact_get_name (contact),
838                                     COL_CONTACT, contact,
839                                     COL_IS_GROUP, FALSE,
840                                     COL_IS_SEPARATOR, FALSE,
841                                     -1);
842         }
843
844         contact_list_store_contact_update (store, contact);
845 }
846
847 static void
848 contact_list_store_contact_removed_cb (EmpathyContactList     *list_iface,
849                                        EmpathyContact          *contact,
850                                        EmpathyContactListStore *store)
851 {
852         empathy_debug (DEBUG_DOMAIN, "Contact:'%s' removed",
853                       empathy_contact_get_name (contact));
854
855         /* Disconnect signals */
856         g_signal_handlers_disconnect_by_func (contact, 
857                                               G_CALLBACK (contact_list_store_contact_groups_updated_cb),
858                                               store);
859         g_signal_handlers_disconnect_by_func (contact,
860                                               G_CALLBACK (contact_list_store_contact_updated_cb),
861                                               store);
862
863         contact_list_store_remove_contact (store, contact);
864 }
865
866 static void
867 contact_list_store_remove_contact (EmpathyContactListStore *store,
868                                    EmpathyContact          *contact)
869 {
870         EmpathyContactListStorePriv *priv;
871         GtkTreeModel               *model;
872         GList                      *iters, *l;
873
874         priv = GET_PRIV (store);
875
876         iters = contact_list_store_find_contact (store, contact);
877         if (!iters) {
878                 return;
879         }
880         
881         /* Clean up model */
882         model = GTK_TREE_MODEL (store);
883
884         for (l = iters; l; l = l->next) {
885                 GtkTreeIter parent;
886
887                 /* NOTE: it is only <= 2 here because we have
888                  * separators after the group name, otherwise it
889                  * should be 1. 
890                  */
891                 if (gtk_tree_model_iter_parent (model, &parent, l->data) &&
892                     gtk_tree_model_iter_n_children (model, &parent) <= 2) {
893                         gtk_tree_store_remove (GTK_TREE_STORE (store), &parent);
894                 } else {
895                         gtk_tree_store_remove (GTK_TREE_STORE (store), l->data);
896                 }
897         }
898
899         g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
900         g_list_free (iters);
901 }
902
903 static void
904 contact_list_store_contact_update (EmpathyContactListStore *store,
905                                    EmpathyContact          *contact)
906 {
907         EmpathyContactListStorePriv *priv;
908         ShowActiveData             *data;
909         GtkTreeModel               *model;
910         GList                      *iters, *l;
911         gboolean                    in_list;
912         gboolean                    should_be_in_list;
913         gboolean                    was_online = TRUE;
914         gboolean                    now_online = FALSE;
915         gboolean                    set_model = FALSE;
916         gboolean                    do_remove = FALSE;
917         gboolean                    do_set_active = FALSE;
918         gboolean                    do_set_refresh = FALSE;
919         GdkPixbuf                  *pixbuf_avatar;
920
921         priv = GET_PRIV (store);
922
923         model = GTK_TREE_MODEL (store);
924
925         iters = contact_list_store_find_contact (store, contact);
926         if (!iters) {
927                 in_list = FALSE;
928         } else {
929                 in_list = TRUE;
930         }
931
932         /* Get online state now. */
933         now_online = empathy_contact_is_online (contact);
934
935         if (priv->show_offline || now_online) {
936                 should_be_in_list = TRUE;
937         } else {
938                 should_be_in_list = FALSE;
939         }
940
941         if (!in_list && !should_be_in_list) {
942                 /* Nothing to do. */
943                 empathy_debug (DEBUG_DOMAIN,
944                               "Contact:'%s' in list:NO, should be:NO",
945                               empathy_contact_get_name (contact));
946
947                 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
948                 g_list_free (iters);
949                 return;
950         }
951         else if (in_list && !should_be_in_list) {
952                 empathy_debug (DEBUG_DOMAIN,
953                               "Contact:'%s' in list:YES, should be:NO",
954                               empathy_contact_get_name (contact));
955
956                 if (priv->show_active) {
957                         do_remove = TRUE;
958                         do_set_active = TRUE;
959                         do_set_refresh = TRUE;
960
961                         set_model = TRUE;
962                         empathy_debug (DEBUG_DOMAIN, "Remove item (after timeout)");
963                 } else {
964                         empathy_debug (DEBUG_DOMAIN, "Remove item (now)!");
965                         contact_list_store_remove_contact (store, contact);
966                 }
967         }
968         else if (!in_list && should_be_in_list) {
969                 empathy_debug (DEBUG_DOMAIN,
970                               "Contact:'%s' in list:NO, should be:YES",
971                               empathy_contact_get_name (contact));
972
973                 contact_list_store_add_contact (store, contact);
974
975                 if (priv->show_active) {
976                         do_set_active = TRUE;
977
978                         empathy_debug (DEBUG_DOMAIN, "Set active (contact added)");
979                 }
980         } else {
981                 empathy_debug (DEBUG_DOMAIN,
982                               "Contact:'%s' in list:YES, should be:YES",
983                               empathy_contact_get_name (contact));
984
985                 /* Get online state before. */
986                 if (iters && g_list_length (iters) > 0) {
987                         gtk_tree_model_get (model, iters->data,
988                                             COL_IS_ONLINE, &was_online,
989                                             -1);
990                 }
991
992                 /* Is this really an update or an online/offline. */
993                 if (priv->show_active) {
994                         if (was_online != now_online) {
995                                 do_set_active = TRUE;
996                                 do_set_refresh = TRUE;
997
998                                 empathy_debug (DEBUG_DOMAIN, "Set active (contact updated %s)",
999                                               was_online ? "online  -> offline" :
1000                                                            "offline -> online");
1001                         } else {
1002                                 /* Was TRUE for presence updates. */
1003                                 /* do_set_active = FALSE;  */
1004                                 do_set_refresh = TRUE;
1005
1006                                 empathy_debug (DEBUG_DOMAIN, "Set active (contact updated)");
1007                         }
1008                 }
1009
1010                 set_model = TRUE;
1011         }
1012
1013         pixbuf_avatar = empathy_pixbuf_avatar_from_contact_scaled (contact, 32, 32);
1014         for (l = iters; l && set_model; l = l->next) {
1015                 gtk_tree_store_set (GTK_TREE_STORE (store), l->data,
1016                                     COL_ICON_STATUS, empathy_icon_name_for_contact (contact),
1017                                     COL_PIXBUF_AVATAR, pixbuf_avatar,
1018                                     COL_PIXBUF_AVATAR_VISIBLE, priv->show_avatars,
1019                                     COL_NAME, empathy_contact_get_name (contact),
1020                                     COL_STATUS, empathy_contact_get_status (contact),
1021                                     COL_STATUS_VISIBLE, !priv->is_compact,
1022                                     COL_IS_GROUP, FALSE,
1023                                     COL_IS_ONLINE, now_online,
1024                                     COL_IS_SEPARATOR, FALSE,
1025                                     -1);
1026         }
1027
1028         if (pixbuf_avatar) {
1029                 g_object_unref (pixbuf_avatar);
1030         }
1031
1032         if (priv->show_active && do_set_active) {
1033                 contact_list_store_contact_set_active (store, contact, do_set_active, do_set_refresh);
1034
1035                 if (do_set_active) {
1036                         data = contact_list_store_contact_active_new (store, contact, do_remove);
1037                         g_timeout_add (ACTIVE_USER_SHOW_TIME,
1038                                        (GSourceFunc) contact_list_store_contact_active_cb,
1039                                        data);
1040                 }
1041         }
1042
1043         /* FIXME: when someone goes online then offline quickly, the
1044          * first timeout sets the user to be inactive and the second
1045          * timeout removes the user from the contact list, really we
1046          * should remove the first timeout.
1047          */
1048         g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
1049         g_list_free (iters);
1050 }
1051
1052 static void
1053 contact_list_store_contact_groups_updated_cb (EmpathyContact          *contact,
1054                                               GParamSpec             *param,
1055                                               EmpathyContactListStore *store)
1056 {
1057         empathy_contact_list_store_update_contact_groups (store, contact);
1058 }
1059
1060 static void
1061 contact_list_store_contact_updated_cb (EmpathyContact          *contact,
1062                                        GParamSpec             *param,
1063                                        EmpathyContactListStore *store)
1064 {
1065         empathy_debug (DEBUG_DOMAIN,
1066                       "Contact:'%s' updated, checking roster is in sync...",
1067                       empathy_contact_get_name (contact));
1068
1069         contact_list_store_contact_update (store, contact);
1070 }
1071
1072 static void
1073 contact_list_store_contact_set_active (EmpathyContactListStore *store,
1074                                        EmpathyContact          *contact,
1075                                        gboolean                active,
1076                                        gboolean                set_changed)
1077 {
1078         EmpathyContactListStorePriv *priv;
1079         GtkTreeModel               *model;
1080         GList                      *iters, *l;
1081
1082         priv = GET_PRIV (store);
1083         model = GTK_TREE_MODEL (store);
1084
1085         iters = contact_list_store_find_contact (store, contact);
1086         for (l = iters; l; l = l->next) {
1087                 GtkTreePath *path;
1088
1089                 gtk_tree_store_set (GTK_TREE_STORE (store), l->data,
1090                                     COL_IS_ACTIVE, active,
1091                                     -1);
1092
1093                 empathy_debug (DEBUG_DOMAIN, "Set item %s", active ? "active" : "inactive");
1094
1095                 if (set_changed) {
1096                         path = gtk_tree_model_get_path (model, l->data);
1097                         gtk_tree_model_row_changed (model, path, l->data);
1098                         gtk_tree_path_free (path);
1099                 }
1100         }
1101
1102         g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
1103         g_list_free (iters);
1104
1105 }
1106
1107 static ShowActiveData *
1108 contact_list_store_contact_active_new (EmpathyContactListStore *store,
1109                                        EmpathyContact          *contact,
1110                                        gboolean                remove)
1111 {
1112         ShowActiveData *data;
1113
1114         empathy_debug (DEBUG_DOMAIN, 
1115                       "Contact:'%s' now active, and %s be removed",
1116                       empathy_contact_get_name (contact), 
1117                       remove ? "WILL" : "WILL NOT");
1118         
1119         data = g_slice_new0 (ShowActiveData);
1120
1121         data->store = g_object_ref (store);
1122         data->contact = g_object_ref (contact);
1123         data->remove = remove;
1124
1125         return data;
1126 }
1127
1128 static void
1129 contact_list_store_contact_active_free (ShowActiveData *data)
1130 {
1131         g_object_unref (data->contact);
1132         g_object_unref (data->store);
1133
1134         g_slice_free (ShowActiveData, data);
1135 }
1136
1137 static gboolean
1138 contact_list_store_contact_active_cb (ShowActiveData *data)
1139 {
1140         EmpathyContactListStorePriv *priv;
1141
1142         priv = GET_PRIV (data->store);
1143
1144         if (data->remove &&
1145             !priv->show_offline &&
1146             !empathy_contact_is_online (data->contact)) {
1147                 empathy_debug (DEBUG_DOMAIN, 
1148                               "Contact:'%s' active timeout, removing item",
1149                               empathy_contact_get_name (data->contact));
1150                 contact_list_store_remove_contact (data->store, data->contact);
1151         }
1152
1153         empathy_debug (DEBUG_DOMAIN, 
1154                       "Contact:'%s' no longer active",
1155                       empathy_contact_get_name (data->contact));
1156
1157         contact_list_store_contact_set_active (data->store,
1158                                                data->contact,
1159                                                FALSE,
1160                                                TRUE);
1161
1162         contact_list_store_contact_active_free (data);
1163
1164         return FALSE;
1165 }
1166
1167 static gboolean
1168 contact_list_store_get_group_foreach (GtkTreeModel *model,
1169                                       GtkTreePath  *path,
1170                                       GtkTreeIter  *iter,
1171                                       FindGroup    *fg)
1172 {
1173         gchar    *str;
1174         gboolean  is_group;
1175
1176         /* Groups are only at the top level. */
1177         if (gtk_tree_path_get_depth (path) != 1) {
1178                 return FALSE;
1179         }
1180
1181         gtk_tree_model_get (model, iter,
1182                             COL_NAME, &str,
1183                             COL_IS_GROUP, &is_group,
1184                             -1);
1185
1186         if (is_group && strcmp (str, fg->name) == 0) {
1187                 fg->found = TRUE;
1188                 fg->iter = *iter;
1189         }
1190
1191         g_free (str);
1192
1193         return fg->found;
1194 }
1195
1196 static void
1197 contact_list_store_get_group (EmpathyContactListStore *store,
1198                               const gchar            *name,
1199                               GtkTreeIter            *iter_group_to_set,
1200                               GtkTreeIter            *iter_separator_to_set,
1201                               gboolean               *created)
1202 {
1203         EmpathyContactListStorePriv *priv;
1204         GtkTreeModel               *model;
1205         GtkTreeIter                 iter_group;
1206         GtkTreeIter                 iter_separator;
1207         FindGroup                   fg;
1208
1209         priv = GET_PRIV (store);
1210
1211         memset (&fg, 0, sizeof (fg));
1212
1213         fg.name = name;
1214
1215         model = GTK_TREE_MODEL (store);
1216         gtk_tree_model_foreach (model,
1217                                 (GtkTreeModelForeachFunc) contact_list_store_get_group_foreach,
1218                                 &fg);
1219
1220         if (!fg.found) {
1221                 if (created) {
1222                         *created = TRUE;
1223                 }
1224
1225                 gtk_tree_store_append (GTK_TREE_STORE (store), &iter_group, NULL);
1226                 gtk_tree_store_set (GTK_TREE_STORE (store), &iter_group,
1227                                     COL_ICON_STATUS, NULL,
1228                                     COL_NAME, name,
1229                                     COL_IS_GROUP, TRUE,
1230                                     COL_IS_ACTIVE, FALSE,
1231                                     COL_IS_SEPARATOR, FALSE,
1232                                     -1);
1233
1234                 if (iter_group_to_set) {
1235                         *iter_group_to_set = iter_group;
1236                 }
1237
1238                 gtk_tree_store_append (GTK_TREE_STORE (store),
1239                                        &iter_separator, 
1240                                        &iter_group);
1241                 gtk_tree_store_set (GTK_TREE_STORE (store), &iter_separator,
1242                                     COL_IS_SEPARATOR, TRUE,
1243                                     -1);
1244
1245                 if (iter_separator_to_set) {
1246                         *iter_separator_to_set = iter_separator;
1247                 }
1248         } else {
1249                 if (created) {
1250                         *created = FALSE;
1251                 }
1252
1253                 if (iter_group_to_set) {
1254                         *iter_group_to_set = fg.iter;
1255                 }
1256
1257                 iter_separator = fg.iter;
1258
1259                 if (gtk_tree_model_iter_next (model, &iter_separator)) {
1260                         gboolean is_separator;
1261
1262                         gtk_tree_model_get (model, &iter_separator,
1263                                             COL_IS_SEPARATOR, &is_separator,
1264                                             -1);
1265
1266                         if (is_separator && iter_separator_to_set) {
1267                                 *iter_separator_to_set = iter_separator;
1268                         }
1269                 }
1270         }
1271 }
1272
1273 static gint
1274 contact_list_store_state_sort_func (GtkTreeModel *model,
1275                                     GtkTreeIter  *iter_a,
1276                                     GtkTreeIter  *iter_b,
1277                                     gpointer      user_data)
1278 {
1279         gint            ret_val = 0;
1280         gchar          *name_a, *name_b;
1281         gboolean        is_separator_a, is_separator_b;
1282         EmpathyContact  *contact_a, *contact_b;
1283         EmpathyPresence *presence_a, *presence_b;
1284         McPresence      state_a, state_b;
1285
1286         gtk_tree_model_get (model, iter_a,
1287                             COL_NAME, &name_a,
1288                             COL_CONTACT, &contact_a,
1289                             COL_IS_SEPARATOR, &is_separator_a,
1290                             -1);
1291         gtk_tree_model_get (model, iter_b,
1292                             COL_NAME, &name_b,
1293                             COL_CONTACT, &contact_b,
1294                             COL_IS_SEPARATOR, &is_separator_b,
1295                             -1);
1296
1297         /* Separator or group? */
1298         if (is_separator_a || is_separator_b) {
1299                 if (is_separator_a) {
1300                         ret_val = -1;
1301                 } else if (is_separator_b) {
1302                         ret_val = 1;
1303                 }
1304         } else if (!contact_a && contact_b) {
1305                 ret_val = 1;
1306         } else if (contact_a && !contact_b) {
1307                 ret_val = -1;
1308         } else if (!contact_a && !contact_b) {
1309                 /* Handle groups */
1310                 ret_val = g_utf8_collate (name_a, name_b);
1311         }
1312
1313         if (ret_val) {
1314                 goto free_and_out;
1315         }
1316
1317         /* If we managed to get this far, we can start looking at
1318          * the presences.
1319          */
1320         presence_a = empathy_contact_get_presence (EMPATHY_CONTACT (contact_a));
1321         presence_b = empathy_contact_get_presence (EMPATHY_CONTACT (contact_b));
1322
1323         if (!presence_a && presence_b) {
1324                 ret_val = 1;
1325         } else if (presence_a && !presence_b) {
1326                 ret_val = -1;
1327         } else if (!presence_a && !presence_b) {
1328                 /* Both offline, sort by name */
1329                 ret_val = g_utf8_collate (name_a, name_b);
1330         } else {
1331                 state_a = empathy_presence_get_state (presence_a);
1332                 state_b = empathy_presence_get_state (presence_b);
1333
1334                 if (state_a < state_b) {
1335                         ret_val = -1;
1336                 } else if (state_a > state_b) {
1337                         ret_val = 1;
1338                 } else {
1339                         /* Fallback: compare by name */
1340                         ret_val = g_utf8_collate (name_a, name_b);
1341                 }
1342         }
1343
1344 free_and_out:
1345         g_free (name_a);
1346         g_free (name_b);
1347
1348         if (contact_a) {
1349                 g_object_unref (contact_a);
1350         }
1351
1352         if (contact_b) {
1353                 g_object_unref (contact_b);
1354         }
1355
1356         return ret_val;
1357 }
1358
1359 static gint
1360 contact_list_store_name_sort_func (GtkTreeModel *model,
1361                                    GtkTreeIter  *iter_a,
1362                                    GtkTreeIter  *iter_b,
1363                                    gpointer      user_data)
1364 {
1365         gchar         *name_a, *name_b;
1366         EmpathyContact *contact_a, *contact_b;
1367         gboolean       is_separator_a, is_separator_b;
1368         gint           ret_val;
1369
1370         gtk_tree_model_get (model, iter_a,
1371                             COL_NAME, &name_a,
1372                             COL_CONTACT, &contact_a,
1373                             COL_IS_SEPARATOR, &is_separator_a,
1374                             -1);
1375         gtk_tree_model_get (model, iter_b,
1376                             COL_NAME, &name_b,
1377                             COL_CONTACT, &contact_b,
1378                             COL_IS_SEPARATOR, &is_separator_b,
1379                             -1);
1380
1381         /* If contact is NULL it means it's a group. */
1382
1383         if (is_separator_a || is_separator_b) {
1384                 if (is_separator_a) {
1385                         ret_val = -1;
1386                 } else if (is_separator_b) {
1387                         ret_val = 1;
1388                 }
1389         } else if (!contact_a && contact_b) {
1390                 ret_val = 1;
1391         } else if (contact_a && !contact_b) {
1392                 ret_val = -1;
1393         } else {
1394                 ret_val = g_utf8_collate (name_a, name_b);
1395         }
1396
1397         g_free (name_a);
1398         g_free (name_b);
1399
1400         if (contact_a) {
1401                 g_object_unref (contact_a);
1402         }
1403
1404         if (contact_b) {
1405                 g_object_unref (contact_b);
1406         }
1407
1408         return ret_val;
1409 }
1410
1411 static gboolean
1412 contact_list_store_find_contact_foreach (GtkTreeModel *model,
1413                                          GtkTreePath  *path,
1414                                          GtkTreeIter  *iter,
1415                                          FindContact  *fc)
1416 {
1417         EmpathyContact *contact;
1418
1419         gtk_tree_model_get (model, iter,
1420                             COL_CONTACT, &contact,
1421                             -1);
1422
1423         if (!contact) {
1424                 return FALSE;
1425         }
1426
1427         if (empathy_contact_equal (contact, fc->contact)) {
1428                 fc->found = TRUE;
1429                 fc->iters = g_list_append (fc->iters, gtk_tree_iter_copy (iter));
1430         }
1431         g_object_unref (contact);
1432
1433         return FALSE;
1434 }
1435
1436 static GList *
1437 contact_list_store_find_contact (EmpathyContactListStore *store,
1438                                  EmpathyContact          *contact)
1439 {
1440         EmpathyContactListStorePriv *priv;
1441         GtkTreeModel              *model;
1442         GList                     *l = NULL;
1443         FindContact                fc;
1444
1445         priv = GET_PRIV (store);
1446
1447         memset (&fc, 0, sizeof (fc));
1448
1449         fc.contact = contact;
1450
1451         model = GTK_TREE_MODEL (store);
1452         gtk_tree_model_foreach (model,
1453                                 (GtkTreeModelForeachFunc) contact_list_store_find_contact_foreach,
1454                                 &fc);
1455
1456         if (fc.found) {
1457                 l = fc.iters;
1458         }
1459
1460         return l;
1461 }
1462
1463 static gboolean
1464 contact_list_store_update_list_mode_foreach (GtkTreeModel           *model,
1465                                              GtkTreePath            *path,
1466                                              GtkTreeIter            *iter,
1467                                              EmpathyContactListStore *store)
1468 {
1469         EmpathyContactListStorePriv *priv;
1470         gboolean                    show_avatar = FALSE;
1471
1472         priv = GET_PRIV (store);
1473
1474         if (priv->show_avatars && !priv->is_compact) {
1475                 show_avatar = TRUE;
1476         }
1477
1478         gtk_tree_store_set (GTK_TREE_STORE (store), iter,
1479                             COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
1480                             COL_STATUS_VISIBLE, !priv->is_compact,
1481                             -1);
1482
1483         return FALSE;
1484 }
1485