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