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