]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-contact-list-store.c
Updated Oriya Translation
[empathy.git] / libempathy-gtk / empathy-contact-list-store.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2005-2007 Imendio AB
4  * Copyright (C) 2007-2008 Collabora Ltd.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public
17  * License along with this program; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA  02110-1301  USA
20  *
21  * Authors: Mikael Hallendal <micke@imendio.com>
22  *          Martyn Russell <martyn@imendio.com>
23  *          Xavier Claessens <xclaesse@gmail.com>
24  */
25
26 #include "config.h"
27
28 #include <string.h>
29
30 #include <glib.h>
31 #include <gtk/gtk.h>
32
33 #include <telepathy-glib/util.h>
34
35 #include <libempathy/empathy-utils.h>
36 #include <libempathy/empathy-enum-types.h>
37 #include <libempathy/empathy-contact-manager.h>
38
39 #include "empathy-contact-list-store.h"
40 #include "empathy-ui-utils.h"
41 #include "empathy-gtk-enum-types.h"
42
43 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
44 #include <libempathy/empathy-debug.h>
45
46 /* Active users are those which have recently changed state
47  * (e.g. online, offline or from normal to a busy state).
48  */
49
50 /* Time in seconds user is shown as active */
51 #define ACTIVE_USER_SHOW_TIME 7
52
53 /* Time in seconds after connecting which we wait before active users are enabled */
54 #define ACTIVE_USER_WAIT_TO_ENABLE_TIME 5
55
56 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyContactListStore)
57 typedef struct {
58         EmpathyContactList         *list;
59         gboolean                    show_offline;
60         gboolean                    show_avatars;
61         gboolean                    show_groups;
62         gboolean                    is_compact;
63         gboolean                    show_active;
64         EmpathyContactListStoreSort sort_criterium;
65         guint                       inhibit_active;
66 } EmpathyContactListStorePriv;
67
68 typedef struct {
69         GtkTreeIter  iter;
70         const gchar *name;
71         gboolean     found;
72 } FindGroup;
73
74 typedef struct {
75         EmpathyContact *contact;
76         gboolean       found;
77         GList         *iters;
78 } FindContact;
79
80 typedef struct {
81         EmpathyContactListStore *store;
82         EmpathyContact          *contact;
83         gboolean                remove;
84 } ShowActiveData;
85
86 static void             contact_list_store_finalize                  (GObject                       *object);
87 static void             contact_list_store_get_property              (GObject                       *object,
88                                                                       guint                          param_id,
89                                                                       GValue                        *value,
90                                                                       GParamSpec                    *pspec);
91 static void             contact_list_store_set_property              (GObject                       *object,
92                                                                       guint                          param_id,
93                                                                       const GValue                  *value,
94                                                                       GParamSpec                    *pspec);
95 static void             contact_list_store_setup                     (EmpathyContactListStore       *store);
96 static gboolean         contact_list_store_inibit_active_cb          (EmpathyContactListStore       *store);
97 static void             contact_list_store_members_changed_cb        (EmpathyContactList            *list_iface,
98                                                                       EmpathyContact                *contact,
99                                                                       EmpathyContact                *actor,
100                                                                       guint                          reason,
101                                                                       gchar                         *message,
102                                                                       gboolean                       is_member,
103                                                                       EmpathyContactListStore       *store);
104 static void             contact_list_store_groups_changed_cb         (EmpathyContactList            *list_iface,
105                                                                       EmpathyContact                *contact,
106                                                                       gchar                         *group,
107                                                                       gboolean                       is_member,
108                                                                       EmpathyContactListStore       *store);
109 static void             contact_list_store_add_contact               (EmpathyContactListStore       *store,
110                                                                       EmpathyContact                *contact);
111 static void             contact_list_store_remove_contact            (EmpathyContactListStore       *store,
112                                                                       EmpathyContact                *contact);
113 static void             contact_list_store_contact_update            (EmpathyContactListStore       *store,
114                                                                       EmpathyContact                *contact);
115 static void             contact_list_store_contact_updated_cb        (EmpathyContact                *contact,
116                                                                       GParamSpec                    *param,
117                                                                       EmpathyContactListStore       *store);
118 static void             contact_list_store_contact_set_active        (EmpathyContactListStore       *store,
119                                                                       EmpathyContact                *contact,
120                                                                       gboolean                       active,
121                                                                       gboolean                       set_changed);
122 static ShowActiveData * contact_list_store_contact_active_new        (EmpathyContactListStore       *store,
123                                                                       EmpathyContact                *contact,
124                                                                       gboolean                       remove);
125 static void             contact_list_store_contact_active_free       (ShowActiveData                *data);
126 static gboolean         contact_list_store_contact_active_cb         (ShowActiveData                *data);
127 static gboolean         contact_list_store_get_group_foreach         (GtkTreeModel                  *model,
128                                                                       GtkTreePath                   *path,
129                                                                       GtkTreeIter                   *iter,
130                                                                       FindGroup                     *fg);
131 static void             contact_list_store_get_group                 (EmpathyContactListStore       *store,
132                                                                       const gchar                   *name,
133                                                                       GtkTreeIter                   *iter_group_to_set,
134                                                                       GtkTreeIter                   *iter_separator_to_set,
135                                                                       gboolean                      *created);
136 static gint             contact_list_store_state_sort_func           (GtkTreeModel                  *model,
137                                                                       GtkTreeIter                   *iter_a,
138                                                                       GtkTreeIter                   *iter_b,
139                                                                       gpointer                       user_data);
140 static gint             contact_list_store_name_sort_func            (GtkTreeModel                  *model,
141                                                                       GtkTreeIter                   *iter_a,
142                                                                       GtkTreeIter                   *iter_b,
143                                                                       gpointer                       user_data);
144 static gboolean         contact_list_store_find_contact_foreach      (GtkTreeModel                  *model,
145                                                                       GtkTreePath                   *path,
146                                                                       GtkTreeIter                   *iter,
147                                                                       FindContact                   *fc);
148 static GList *          contact_list_store_find_contact              (EmpathyContactListStore       *store,
149                                                                       EmpathyContact                *contact);
150 static gboolean         contact_list_store_update_list_mode_foreach  (GtkTreeModel                  *model,
151                                                                       GtkTreePath                   *path,
152                                                                       GtkTreeIter                   *iter,
153                                                                       EmpathyContactListStore       *store);
154
155 enum {
156         PROP_0,
157         PROP_CONTACT_LIST,
158         PROP_SHOW_OFFLINE,
159         PROP_SHOW_AVATARS,
160         PROP_SHOW_GROUPS,
161         PROP_IS_COMPACT,
162         PROP_SORT_CRITERIUM
163 };
164
165 G_DEFINE_TYPE (EmpathyContactListStore, empathy_contact_list_store, GTK_TYPE_TREE_STORE);
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 = G_TYPE_INSTANCE_GET_PRIVATE (store,
277                 EMPATHY_TYPE_CONTACT_LIST_STORE, EmpathyContactListStorePriv);
278
279         store->priv = priv;
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[] = {
732                 G_TYPE_STRING,        /* Status icon-name */
733                 GDK_TYPE_PIXBUF,      /* Avatar pixbuf */
734                 G_TYPE_BOOLEAN,       /* Avatar pixbuf visible */
735                 G_TYPE_STRING,        /* Name */
736                 G_TYPE_STRING,        /* Status string */
737                 G_TYPE_BOOLEAN,       /* Show status */
738                 EMPATHY_TYPE_CONTACT, /* Contact type */
739                 G_TYPE_BOOLEAN,       /* Is group */
740                 G_TYPE_BOOLEAN,       /* Is active */
741                 G_TYPE_BOOLEAN,       /* Is online */
742                 G_TYPE_BOOLEAN,       /* Is separator */
743                 G_TYPE_BOOLEAN,       /* Can make audio calls */
744                 G_TYPE_BOOLEAN,       /* Can make video calls */
745                 EMPATHY_TYPE_CONTACT_LIST_FLAGS, /* Flags */
746         };
747
748         priv = GET_PRIV (store);
749
750         gtk_tree_store_set_column_types (GTK_TREE_STORE (store),
751                                          EMPATHY_CONTACT_LIST_STORE_COL_COUNT,
752                                          types);
753
754         /* Set up sorting */
755         gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store),
756                                          EMPATHY_CONTACT_LIST_STORE_COL_NAME,
757                                          contact_list_store_name_sort_func,
758                                          store, NULL);
759         gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store),
760                                          EMPATHY_CONTACT_LIST_STORE_COL_STATUS,
761                                          contact_list_store_state_sort_func,
762                                          store, NULL);
763
764         priv->sort_criterium = EMPATHY_CONTACT_LIST_STORE_SORT_NAME;
765         empathy_contact_list_store_set_sort_criterium (store, priv->sort_criterium);
766 }
767
768 static gboolean
769 contact_list_store_inibit_active_cb (EmpathyContactListStore *store)
770 {
771         EmpathyContactListStorePriv *priv;
772
773         priv = GET_PRIV (store);
774
775         priv->show_active = TRUE;
776         priv->inhibit_active = 0;
777
778         return FALSE;
779 }
780
781 static void
782 contact_list_store_members_changed_cb (EmpathyContactList      *list_iface,
783                                        EmpathyContact          *contact,
784                                        EmpathyContact          *actor,
785                                        guint                    reason,
786                                        gchar                   *message,
787                                        gboolean                 is_member,
788                                        EmpathyContactListStore *store)
789 {
790         EmpathyContactListStorePriv *priv;
791
792         priv = GET_PRIV (store);
793
794         DEBUG ("Contact %s (%d) %s",
795                 empathy_contact_get_id (contact),
796                 empathy_contact_get_handle (contact),
797                 is_member ? "added" : "removed");
798
799         if (is_member) {
800                 g_signal_connect (contact, "notify::presence",
801                                   G_CALLBACK (contact_list_store_contact_updated_cb),
802                                   store);
803                 g_signal_connect (contact, "notify::presence-message",
804                                   G_CALLBACK (contact_list_store_contact_updated_cb),
805                                   store);
806                 g_signal_connect (contact, "notify::name",
807                                   G_CALLBACK (contact_list_store_contact_updated_cb),
808                                   store);
809                 g_signal_connect (contact, "notify::avatar",
810                                   G_CALLBACK (contact_list_store_contact_updated_cb),
811                                   store);
812                 g_signal_connect (contact, "notify::capabilities",
813                                   G_CALLBACK (contact_list_store_contact_updated_cb),
814                                   store);
815
816                 contact_list_store_add_contact (store, contact);
817         } else {
818                 g_signal_handlers_disconnect_by_func (contact,
819                                                       G_CALLBACK (contact_list_store_contact_updated_cb),
820                                                       store);
821
822                 contact_list_store_remove_contact (store, contact);
823         }
824 }
825
826 static void
827 contact_list_store_groups_changed_cb (EmpathyContactList      *list_iface,
828                                       EmpathyContact          *contact,
829                                       gchar                   *group,
830                                       gboolean                 is_member,
831                                       EmpathyContactListStore *store)
832 {
833         EmpathyContactListStorePriv *priv;
834         gboolean                     show_active;
835
836         priv = GET_PRIV (store);
837
838         DEBUG ("Updating groups for contact %s (%d)",
839                 empathy_contact_get_id (contact),
840                 empathy_contact_get_handle (contact));
841
842         /* We do this to make sure the groups are correct, if not, we
843          * would have to check the groups already set up for each
844          * contact and then see what has been updated.
845          */
846         show_active = priv->show_active;
847         priv->show_active = FALSE;
848         contact_list_store_remove_contact (store, contact);
849         contact_list_store_add_contact (store, contact);
850         priv->show_active = show_active;
851 }
852
853 static void
854 contact_list_store_add_contact (EmpathyContactListStore *store,
855                                 EmpathyContact          *contact)
856 {
857         EmpathyContactListStorePriv *priv;
858         GtkTreeIter                 iter;
859         GList                      *groups = NULL, *l;
860         TpConnection               *connection;
861         EmpathyContactListFlags     flags = 0;
862
863         priv = GET_PRIV (store);
864
865         if (EMP_STR_EMPTY (empathy_contact_get_name (contact)) ||
866             (!priv->show_offline && !empathy_contact_is_online (contact))) {
867                 return;
868         }
869
870         if (priv->show_groups) {
871                 groups = empathy_contact_list_get_groups (priv->list, contact);
872         }
873
874         connection = empathy_contact_get_connection (contact);
875         if (EMPATHY_IS_CONTACT_MANAGER (priv->list)) {
876                 flags = empathy_contact_manager_get_flags_for_connection (
877                         EMPATHY_CONTACT_MANAGER (priv->list), connection);
878         }
879         /* If no groups just add it at the top level. */
880         if (!groups) {
881                 gtk_tree_store_append (GTK_TREE_STORE (store), &iter, NULL);
882                 gtk_tree_store_set (GTK_TREE_STORE (store), &iter,
883                                     EMPATHY_CONTACT_LIST_STORE_COL_NAME, empathy_contact_get_name (contact),
884                                     EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, contact,
885                                     EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, FALSE,
886                                     EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, FALSE,
887                                     EMPATHY_CONTACT_LIST_STORE_COL_CAN_AUDIO_CALL,
888                                       empathy_contact_get_capabilities (contact) &
889                                         EMPATHY_CAPABILITIES_AUDIO,
890                                     EMPATHY_CONTACT_LIST_STORE_COL_CAN_VIDEO_CALL,
891                                       empathy_contact_get_capabilities (contact) &
892                                         EMPATHY_CAPABILITIES_VIDEO,
893                                     EMPATHY_CONTACT_LIST_STORE_COL_FLAGS, flags,
894                                     -1);
895         }
896
897         /* Else add to each group. */
898         for (l = groups; l; l = l->next) {
899                 GtkTreeIter iter_group;
900
901                 contact_list_store_get_group (store, l->data, &iter_group, NULL, NULL);
902
903                 gtk_tree_store_insert_after (GTK_TREE_STORE (store), &iter,
904                                              &iter_group, NULL);
905                 gtk_tree_store_set (GTK_TREE_STORE (store), &iter,
906                                     EMPATHY_CONTACT_LIST_STORE_COL_NAME, empathy_contact_get_name (contact),
907                                     EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, contact,
908                                     EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, FALSE,
909                                     EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, FALSE,
910                                     EMPATHY_CONTACT_LIST_STORE_COL_CAN_AUDIO_CALL,
911                                       empathy_contact_get_capabilities (contact) &
912                                         EMPATHY_CAPABILITIES_AUDIO,
913                                     EMPATHY_CONTACT_LIST_STORE_COL_CAN_VIDEO_CALL,
914                                       empathy_contact_get_capabilities (contact) &
915                                         EMPATHY_CAPABILITIES_VIDEO,
916                                     EMPATHY_CONTACT_LIST_STORE_COL_FLAGS, flags,
917                                     -1);
918                 g_free (l->data);
919         }
920         g_list_free (groups);
921
922         contact_list_store_contact_update (store, contact);
923
924 }
925
926 static void
927 contact_list_store_remove_contact (EmpathyContactListStore *store,
928                                    EmpathyContact          *contact)
929 {
930         EmpathyContactListStorePriv *priv;
931         GtkTreeModel               *model;
932         GList                      *iters, *l;
933
934         priv = GET_PRIV (store);
935
936         iters = contact_list_store_find_contact (store, contact);
937         if (!iters) {
938                 return;
939         }
940
941         /* Clean up model */
942         model = GTK_TREE_MODEL (store);
943
944         for (l = iters; l; l = l->next) {
945                 GtkTreeIter parent;
946
947                 /* NOTE: it is only <= 2 here because we have
948                  * separators after the group name, otherwise it
949                  * should be 1.
950                  */
951                 if (gtk_tree_model_iter_parent (model, &parent, l->data) &&
952                     gtk_tree_model_iter_n_children (model, &parent) <= 2) {
953                         gtk_tree_store_remove (GTK_TREE_STORE (store), &parent);
954                 } else {
955                         gtk_tree_store_remove (GTK_TREE_STORE (store), l->data);
956                 }
957         }
958
959         g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
960         g_list_free (iters);
961 }
962
963 static void
964 contact_list_store_contact_update (EmpathyContactListStore *store,
965                                    EmpathyContact          *contact)
966 {
967         EmpathyContactListStorePriv *priv;
968         ShowActiveData             *data;
969         GtkTreeModel               *model;
970         GList                      *iters, *l;
971         gboolean                    in_list;
972         gboolean                    should_be_in_list;
973         gboolean                    was_online = TRUE;
974         gboolean                    now_online = FALSE;
975         gboolean                    set_model = FALSE;
976         gboolean                    do_remove = FALSE;
977         gboolean                    do_set_active = FALSE;
978         gboolean                    do_set_refresh = FALSE;
979         gboolean                    show_avatar = FALSE;
980         GdkPixbuf                  *pixbuf_avatar;
981
982         priv = GET_PRIV (store);
983
984         model = GTK_TREE_MODEL (store);
985
986         iters = contact_list_store_find_contact (store, contact);
987         if (!iters) {
988                 in_list = FALSE;
989         } else {
990                 in_list = TRUE;
991         }
992
993         /* Get online state now. */
994         now_online = empathy_contact_is_online (contact);
995
996         if (priv->show_offline || now_online) {
997                 should_be_in_list = TRUE;
998         } else {
999                 should_be_in_list = FALSE;
1000         }
1001
1002         if (!in_list && !should_be_in_list) {
1003                 /* Nothing to do. */
1004                 DEBUG ("Contact:'%s' in list:NO, should be:NO",
1005                         empathy_contact_get_name (contact));
1006
1007                 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
1008                 g_list_free (iters);
1009                 return;
1010         }
1011         else if (in_list && !should_be_in_list) {
1012                 DEBUG ("Contact:'%s' in list:YES, should be:NO",
1013                         empathy_contact_get_name (contact));
1014
1015                 if (priv->show_active) {
1016                         do_remove = TRUE;
1017                         do_set_active = TRUE;
1018                         do_set_refresh = TRUE;
1019
1020                         set_model = TRUE;
1021                         DEBUG ("Remove item (after timeout)");
1022                 } else {
1023                         DEBUG ("Remove item (now)!");
1024                         contact_list_store_remove_contact (store, contact);
1025                 }
1026         }
1027         else if (!in_list && should_be_in_list) {
1028                 DEBUG ("Contact:'%s' in list:NO, should be:YES",
1029                         empathy_contact_get_name (contact));
1030
1031                 contact_list_store_add_contact (store, contact);
1032
1033                 if (priv->show_active) {
1034                         do_set_active = TRUE;
1035
1036                         DEBUG ("Set active (contact added)");
1037                 }
1038         } else {
1039                 DEBUG ("Contact:'%s' in list:YES, should be:YES",
1040                         empathy_contact_get_name (contact));
1041
1042                 /* Get online state before. */
1043                 if (iters && g_list_length (iters) > 0) {
1044                         gtk_tree_model_get (model, iters->data,
1045                                             EMPATHY_CONTACT_LIST_STORE_COL_IS_ONLINE, &was_online,
1046                                             -1);
1047                 }
1048
1049                 /* Is this really an update or an online/offline. */
1050                 if (priv->show_active) {
1051                         if (was_online != now_online) {
1052                                 do_set_active = TRUE;
1053                                 do_set_refresh = TRUE;
1054
1055                                 DEBUG ("Set active (contact updated %s)",
1056                                         was_online ? "online  -> offline" :
1057                                         "offline -> online");
1058                         } else {
1059                                 /* Was TRUE for presence updates. */
1060                                 /* do_set_active = FALSE;  */
1061                                 do_set_refresh = TRUE;
1062
1063                                 DEBUG ("Set active (contact updated)");
1064                         }
1065                 }
1066
1067                 set_model = TRUE;
1068         }
1069
1070         if (priv->show_avatars && !priv->is_compact) {
1071                 show_avatar = TRUE;
1072         }
1073         pixbuf_avatar = empathy_pixbuf_avatar_from_contact_scaled (contact, 32, 32);
1074         for (l = iters; l && set_model; l = l->next) {
1075                 gtk_tree_store_set (GTK_TREE_STORE (store), l->data,
1076                                     EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, empathy_icon_name_for_contact (contact),
1077                                     EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR, pixbuf_avatar,
1078                                     EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
1079                                     EMPATHY_CONTACT_LIST_STORE_COL_NAME, empathy_contact_get_name (contact),
1080                                     EMPATHY_CONTACT_LIST_STORE_COL_STATUS, empathy_contact_get_status (contact),
1081                                     EMPATHY_CONTACT_LIST_STORE_COL_STATUS_VISIBLE, !priv->is_compact,
1082                                     EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, FALSE,
1083                                     EMPATHY_CONTACT_LIST_STORE_COL_IS_ONLINE, now_online,
1084                                     EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, FALSE,
1085                                     EMPATHY_CONTACT_LIST_STORE_COL_CAN_AUDIO_CALL,
1086                                       empathy_contact_get_capabilities (contact) &
1087                                         EMPATHY_CAPABILITIES_AUDIO,
1088                                     EMPATHY_CONTACT_LIST_STORE_COL_CAN_VIDEO_CALL,
1089                                       empathy_contact_get_capabilities (contact) &
1090                                         EMPATHY_CAPABILITIES_VIDEO,
1091                                     -1);
1092         }
1093
1094         if (pixbuf_avatar) {
1095                 g_object_unref (pixbuf_avatar);
1096         }
1097
1098         if (priv->show_active && do_set_active) {
1099                 contact_list_store_contact_set_active (store, contact, do_set_active, do_set_refresh);
1100
1101                 if (do_set_active) {
1102                         data = contact_list_store_contact_active_new (store, contact, do_remove);
1103                         g_timeout_add_seconds (ACTIVE_USER_SHOW_TIME,
1104                                                (GSourceFunc) contact_list_store_contact_active_cb,
1105                                                data);
1106                 }
1107         }
1108
1109         /* FIXME: when someone goes online then offline quickly, the
1110          * first timeout sets the user to be inactive and the second
1111          * timeout removes the user from the contact list, really we
1112          * should remove the first timeout.
1113          */
1114         g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
1115         g_list_free (iters);
1116 }
1117
1118 static void
1119 contact_list_store_contact_updated_cb (EmpathyContact          *contact,
1120                                        GParamSpec              *param,
1121                                        EmpathyContactListStore *store)
1122 {
1123         DEBUG ("Contact:'%s' updated, checking roster is in sync...",
1124                 empathy_contact_get_name (contact));
1125
1126         contact_list_store_contact_update (store, contact);
1127 }
1128
1129 static void
1130 contact_list_store_contact_set_active (EmpathyContactListStore *store,
1131                                        EmpathyContact          *contact,
1132                                        gboolean                active,
1133                                        gboolean                set_changed)
1134 {
1135         EmpathyContactListStorePriv *priv;
1136         GtkTreeModel               *model;
1137         GList                      *iters, *l;
1138
1139         priv = GET_PRIV (store);
1140         model = GTK_TREE_MODEL (store);
1141
1142         iters = contact_list_store_find_contact (store, contact);
1143         for (l = iters; l; l = l->next) {
1144                 GtkTreePath *path;
1145
1146                 gtk_tree_store_set (GTK_TREE_STORE (store), l->data,
1147                                     EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, active,
1148                                     -1);
1149
1150                 DEBUG ("Set item %s", active ? "active" : "inactive");
1151
1152                 if (set_changed) {
1153                         path = gtk_tree_model_get_path (model, l->data);
1154                         gtk_tree_model_row_changed (model, path, l->data);
1155                         gtk_tree_path_free (path);
1156                 }
1157         }
1158
1159         g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
1160         g_list_free (iters);
1161
1162 }
1163
1164 static ShowActiveData *
1165 contact_list_store_contact_active_new (EmpathyContactListStore *store,
1166                                        EmpathyContact          *contact,
1167                                        gboolean                remove)
1168 {
1169         ShowActiveData *data;
1170
1171         DEBUG ("Contact:'%s' now active, and %s be removed",
1172                 empathy_contact_get_name (contact),
1173                 remove ? "WILL" : "WILL NOT");
1174
1175         data = g_slice_new0 (ShowActiveData);
1176
1177         data->store = g_object_ref (store);
1178         data->contact = g_object_ref (contact);
1179         data->remove = remove;
1180
1181         return data;
1182 }
1183
1184 static void
1185 contact_list_store_contact_active_free (ShowActiveData *data)
1186 {
1187         g_object_unref (data->contact);
1188         g_object_unref (data->store);
1189
1190         g_slice_free (ShowActiveData, data);
1191 }
1192
1193 static gboolean
1194 contact_list_store_contact_active_cb (ShowActiveData *data)
1195 {
1196         EmpathyContactListStorePriv *priv;
1197
1198         priv = GET_PRIV (data->store);
1199
1200         if (data->remove &&
1201             !priv->show_offline &&
1202             !empathy_contact_is_online (data->contact)) {
1203                 DEBUG ("Contact:'%s' active timeout, removing item",
1204                         empathy_contact_get_name (data->contact));
1205                 contact_list_store_remove_contact (data->store, data->contact);
1206         }
1207
1208         DEBUG ("Contact:'%s' no longer active",
1209                 empathy_contact_get_name (data->contact));
1210
1211         contact_list_store_contact_set_active (data->store,
1212                                                data->contact,
1213                                                FALSE,
1214                                                TRUE);
1215
1216         contact_list_store_contact_active_free (data);
1217
1218         return FALSE;
1219 }
1220
1221 static gboolean
1222 contact_list_store_get_group_foreach (GtkTreeModel *model,
1223                                       GtkTreePath  *path,
1224                                       GtkTreeIter  *iter,
1225                                       FindGroup    *fg)
1226 {
1227         gchar    *str;
1228         gboolean  is_group;
1229
1230         /* Groups are only at the top level. */
1231         if (gtk_tree_path_get_depth (path) != 1) {
1232                 return FALSE;
1233         }
1234
1235         gtk_tree_model_get (model, iter,
1236                             EMPATHY_CONTACT_LIST_STORE_COL_NAME, &str,
1237                             EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
1238                             -1);
1239
1240         if (is_group && !tp_strdiff (str, fg->name)) {
1241                 fg->found = TRUE;
1242                 fg->iter = *iter;
1243         }
1244
1245         g_free (str);
1246
1247         return fg->found;
1248 }
1249
1250 static void
1251 contact_list_store_get_group (EmpathyContactListStore *store,
1252                               const gchar            *name,
1253                               GtkTreeIter            *iter_group_to_set,
1254                               GtkTreeIter            *iter_separator_to_set,
1255                               gboolean               *created)
1256 {
1257         EmpathyContactListStorePriv *priv;
1258         GtkTreeModel                *model;
1259         GtkTreeIter                  iter_group;
1260         GtkTreeIter                  iter_separator;
1261         FindGroup                    fg;
1262
1263         priv = GET_PRIV (store);
1264
1265         memset (&fg, 0, sizeof (fg));
1266
1267         fg.name = name;
1268
1269         model = GTK_TREE_MODEL (store);
1270         gtk_tree_model_foreach (model,
1271                                 (GtkTreeModelForeachFunc) contact_list_store_get_group_foreach,
1272                                 &fg);
1273
1274         if (!fg.found) {
1275                 if (created) {
1276                         *created = TRUE;
1277                 }
1278
1279                 gtk_tree_store_append (GTK_TREE_STORE (store), &iter_group, NULL);
1280                 gtk_tree_store_set (GTK_TREE_STORE (store), &iter_group,
1281                                     EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, NULL,
1282                                     EMPATHY_CONTACT_LIST_STORE_COL_NAME, name,
1283                                     EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, TRUE,
1284                                     EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, FALSE,
1285                                     EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, FALSE,
1286                                     -1);
1287
1288                 if (iter_group_to_set) {
1289                         *iter_group_to_set = iter_group;
1290                 }
1291
1292                 gtk_tree_store_append (GTK_TREE_STORE (store),
1293                                        &iter_separator,
1294                                        &iter_group);
1295                 gtk_tree_store_set (GTK_TREE_STORE (store), &iter_separator,
1296                                     EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, TRUE,
1297                                     -1);
1298
1299                 if (iter_separator_to_set) {
1300                         *iter_separator_to_set = iter_separator;
1301                 }
1302         } else {
1303                 if (created) {
1304                         *created = FALSE;
1305                 }
1306
1307                 if (iter_group_to_set) {
1308                         *iter_group_to_set = fg.iter;
1309                 }
1310
1311                 iter_separator = fg.iter;
1312
1313                 if (gtk_tree_model_iter_next (model, &iter_separator)) {
1314                         gboolean is_separator;
1315
1316                         gtk_tree_model_get (model, &iter_separator,
1317                                             EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator,
1318                                             -1);
1319
1320                         if (is_separator && iter_separator_to_set) {
1321                                 *iter_separator_to_set = iter_separator;
1322                         }
1323                 }
1324         }
1325 }
1326
1327 static gint
1328 contact_list_store_state_sort_func (GtkTreeModel *model,
1329                                     GtkTreeIter  *iter_a,
1330                                     GtkTreeIter  *iter_b,
1331                                     gpointer      user_data)
1332 {
1333         gint            ret_val = 0;
1334         gchar          *name_a, *name_b;
1335         gboolean        is_separator_a, is_separator_b;
1336         EmpathyContact *contact_a, *contact_b;
1337
1338         gtk_tree_model_get (model, iter_a,
1339                             EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name_a,
1340                             EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact_a,
1341                             EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator_a,
1342                             -1);
1343         gtk_tree_model_get (model, iter_b,
1344                             EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name_b,
1345                             EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact_b,
1346                             EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator_b,
1347                             -1);
1348
1349         /* Separator or group? */
1350         if (is_separator_a || is_separator_b) {
1351                 if (is_separator_a) {
1352                         ret_val = -1;
1353                 } else if (is_separator_b) {
1354                         ret_val = 1;
1355                 }
1356         } else if (!contact_a && contact_b) {
1357                 ret_val = 1;
1358         } else if (contact_a && !contact_b) {
1359                 ret_val = -1;
1360         } else if (!contact_a && !contact_b) {
1361                 /* Handle groups */
1362                 ret_val = g_utf8_collate (name_a, name_b);
1363         }
1364
1365         if (ret_val) {
1366                 goto free_and_out;
1367         }
1368
1369         /* If we managed to get this far, we can start looking at
1370          * the presences.
1371          */
1372         ret_val = -tp_connection_presence_type_cmp_availability (
1373                 empathy_contact_get_presence (EMPATHY_CONTACT (contact_a)),
1374                 empathy_contact_get_presence (EMPATHY_CONTACT (contact_b)));
1375
1376         if (ret_val == 0) {
1377                 /* Fallback: compare by name */
1378                 ret_val = g_utf8_collate (name_a, name_b);
1379         }
1380
1381 free_and_out:
1382         g_free (name_a);
1383         g_free (name_b);
1384
1385         if (contact_a) {
1386                 g_object_unref (contact_a);
1387         }
1388
1389         if (contact_b) {
1390                 g_object_unref (contact_b);
1391         }
1392
1393         return ret_val;
1394 }
1395
1396 static gint
1397 contact_list_store_name_sort_func (GtkTreeModel *model,
1398                                    GtkTreeIter  *iter_a,
1399                                    GtkTreeIter  *iter_b,
1400                                    gpointer      user_data)
1401 {
1402         gchar         *name_a, *name_b;
1403         EmpathyContact *contact_a, *contact_b;
1404         gboolean       is_separator_a, is_separator_b;
1405         gint           ret_val;
1406
1407         gtk_tree_model_get (model, iter_a,
1408                             EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name_a,
1409                             EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact_a,
1410                             EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator_a,
1411                             -1);
1412         gtk_tree_model_get (model, iter_b,
1413                             EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name_b,
1414                             EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact_b,
1415                             EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator_b,
1416                             -1);
1417
1418         /* If contact is NULL it means it's a group. */
1419
1420         if (is_separator_a || is_separator_b) {
1421                 if (is_separator_a) {
1422                         ret_val = -1;
1423                 } else if (is_separator_b) {
1424                         ret_val = 1;
1425                 }
1426         } else if (!contact_a && contact_b) {
1427                 ret_val = 1;
1428         } else if (contact_a && !contact_b) {
1429                 ret_val = -1;
1430         } else {
1431                 ret_val = g_utf8_collate (name_a, name_b);
1432         }
1433
1434         g_free (name_a);
1435         g_free (name_b);
1436
1437         if (contact_a) {
1438                 g_object_unref (contact_a);
1439         }
1440
1441         if (contact_b) {
1442                 g_object_unref (contact_b);
1443         }
1444
1445         return ret_val;
1446 }
1447
1448 static gboolean
1449 contact_list_store_find_contact_foreach (GtkTreeModel *model,
1450                                          GtkTreePath  *path,
1451                                          GtkTreeIter  *iter,
1452                                          FindContact  *fc)
1453 {
1454         EmpathyContact *contact;
1455
1456         gtk_tree_model_get (model, iter,
1457                             EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
1458                             -1);
1459
1460         if (contact == fc->contact) {
1461                 fc->found = TRUE;
1462                 fc->iters = g_list_append (fc->iters, gtk_tree_iter_copy (iter));
1463         }
1464
1465         if (contact) {
1466                 g_object_unref (contact);
1467         }
1468
1469         return FALSE;
1470 }
1471
1472 static GList *
1473 contact_list_store_find_contact (EmpathyContactListStore *store,
1474                                  EmpathyContact          *contact)
1475 {
1476         EmpathyContactListStorePriv *priv;
1477         GtkTreeModel              *model;
1478         GList                     *l = NULL;
1479         FindContact                fc;
1480
1481         priv = GET_PRIV (store);
1482
1483         memset (&fc, 0, sizeof (fc));
1484
1485         fc.contact = contact;
1486
1487         model = GTK_TREE_MODEL (store);
1488         gtk_tree_model_foreach (model,
1489                                 (GtkTreeModelForeachFunc) contact_list_store_find_contact_foreach,
1490                                 &fc);
1491
1492         if (fc.found) {
1493                 l = fc.iters;
1494         }
1495
1496         return l;
1497 }
1498
1499 static gboolean
1500 contact_list_store_update_list_mode_foreach (GtkTreeModel           *model,
1501                                              GtkTreePath            *path,
1502                                              GtkTreeIter            *iter,
1503                                              EmpathyContactListStore *store)
1504 {
1505         EmpathyContactListStorePriv *priv;
1506         gboolean                    show_avatar = FALSE;
1507
1508         priv = GET_PRIV (store);
1509
1510         if (priv->show_avatars && !priv->is_compact) {
1511                 show_avatar = TRUE;
1512         }
1513
1514         gtk_tree_store_set (GTK_TREE_STORE (store), iter,
1515                             EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
1516                             EMPATHY_CONTACT_LIST_STORE_COL_STATUS_VISIBLE, !priv->is_compact,
1517                             -1);
1518
1519         return FALSE;
1520 }
1521