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