]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-contact-list-store.c
contact-list-store: add a column saying if a group is a fake one or not
[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 <glib/gi18n-lib.h>
32 #include <gtk/gtk.h>
33
34 #include <telepathy-glib/util.h>
35
36 #include <libempathy/empathy-utils.h>
37 #include <libempathy/empathy-enum-types.h>
38 #include <libempathy/empathy-contact-manager.h>
39
40 #include "empathy-contact-list-store.h"
41 #include "empathy-ui-utils.h"
42 #include "empathy-gtk-enum-types.h"
43
44 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
45 #include <libempathy/empathy-debug.h>
46
47 /* Active users are those which have recently changed state
48  * (e.g. online, offline or from normal to a busy state).
49  */
50
51 /* Time in seconds user is shown as active */
52 #define ACTIVE_USER_SHOW_TIME 7
53
54 /* Time in seconds after connecting which we wait before active users are enabled */
55 #define ACTIVE_USER_WAIT_TO_ENABLE_TIME 5
56
57 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyContactListStore)
58 typedef struct {
59         EmpathyContactList         *list;
60         gboolean                    show_offline;
61         gboolean                    show_avatars;
62         gboolean                    show_groups;
63         gboolean                    is_compact;
64         gboolean                    show_protocols;
65         gboolean                    show_active;
66         EmpathyContactListStoreSort sort_criterium;
67         guint                       inhibit_active;
68         guint                       setup_idle_id;
69         gboolean                    dispose_has_run;
70         GHashTable                  *status_icons;
71 } EmpathyContactListStorePriv;
72
73 typedef struct {
74         GtkTreeIter  iter;
75         const gchar *name;
76         gboolean     found;
77 } FindGroup;
78
79 typedef struct {
80         EmpathyContact *contact;
81         gboolean       found;
82         GList         *iters;
83 } FindContact;
84
85 typedef struct {
86         EmpathyContactListStore *store;
87         EmpathyContact          *contact;
88         gboolean                remove;
89 } ShowActiveData;
90
91 static void             contact_list_store_dispose                  (GObject                       *object);
92 static void             contact_list_store_get_property              (GObject                       *object,
93                                                                       guint                          param_id,
94                                                                       GValue                        *value,
95                                                                       GParamSpec                    *pspec);
96 static void             contact_list_store_set_property              (GObject                       *object,
97                                                                       guint                          param_id,
98                                                                       const GValue                  *value,
99                                                                       GParamSpec                    *pspec);
100 static void             contact_list_store_setup                     (EmpathyContactListStore       *store);
101 static gboolean         contact_list_store_inibit_active_cb          (EmpathyContactListStore       *store);
102 static void             contact_list_store_members_changed_cb        (EmpathyContactList            *list_iface,
103                                                                       EmpathyContact                *contact,
104                                                                       EmpathyContact                *actor,
105                                                                       guint                          reason,
106                                                                       gchar                         *message,
107                                                                       gboolean                       is_member,
108                                                                       EmpathyContactListStore       *store);
109 static void             contact_list_store_favourites_changed_cb     (EmpathyContactList            *list_iface,
110                                                                       EmpathyContact                *contact,
111                                                                       gboolean                       is_favourite,
112                                                                       EmpathyContactListStore       *store);
113 static void             contact_list_store_member_renamed_cb         (EmpathyContactList            *list_iface,
114                                                                       EmpathyContact                *old_contact,
115                                                                       EmpathyContact                *new_contact,
116                                                                       guint                          reason,
117                                                                       gchar                         *message,
118                                                                       EmpathyContactListStore       *store);
119 static void             contact_list_store_groups_changed_cb         (EmpathyContactList            *list_iface,
120                                                                       EmpathyContact                *contact,
121                                                                       gchar                         *group,
122                                                                       gboolean                       is_member,
123                                                                       EmpathyContactListStore       *store);
124 static void             contact_list_store_add_contact               (EmpathyContactListStore       *store,
125                                                                       EmpathyContact                *contact);
126 static void             contact_list_store_remove_contact            (EmpathyContactListStore       *store,
127                                                                       EmpathyContact                *contact);
128 static void             contact_list_store_contact_update            (EmpathyContactListStore       *store,
129                                                                       EmpathyContact                *contact);
130 static void             contact_list_store_contact_updated_cb        (EmpathyContact                *contact,
131                                                                       GParamSpec                    *param,
132                                                                       EmpathyContactListStore       *store);
133 static void             contact_list_store_contact_set_active        (EmpathyContactListStore       *store,
134                                                                       EmpathyContact                *contact,
135                                                                       gboolean                       active,
136                                                                       gboolean                       set_changed);
137 static ShowActiveData * contact_list_store_contact_active_new        (EmpathyContactListStore       *store,
138                                                                       EmpathyContact                *contact,
139                                                                       gboolean                       remove);
140 static void             contact_list_store_contact_active_free       (ShowActiveData                *data);
141 static gboolean         contact_list_store_contact_active_cb         (ShowActiveData                *data);
142 static gboolean         contact_list_store_get_group_foreach         (GtkTreeModel                  *model,
143                                                                       GtkTreePath                   *path,
144                                                                       GtkTreeIter                   *iter,
145                                                                       FindGroup                     *fg);
146 static void             contact_list_store_get_group                 (EmpathyContactListStore       *store,
147                                                                       const gchar                   *name,
148                                                                       GtkTreeIter                   *iter_group_to_set,
149                                                                       GtkTreeIter                   *iter_separator_to_set,
150                                                                       gboolean                      *created,
151                                                                       gboolean                      is_fake_group);
152 static gint             contact_list_store_state_sort_func           (GtkTreeModel                  *model,
153                                                                       GtkTreeIter                   *iter_a,
154                                                                       GtkTreeIter                   *iter_b,
155                                                                       gpointer                       user_data);
156 static gint             contact_list_store_name_sort_func            (GtkTreeModel                  *model,
157                                                                       GtkTreeIter                   *iter_a,
158                                                                       GtkTreeIter                   *iter_b,
159                                                                       gpointer                       user_data);
160 static gboolean         contact_list_store_find_contact_foreach      (GtkTreeModel                  *model,
161                                                                       GtkTreePath                   *path,
162                                                                       GtkTreeIter                   *iter,
163                                                                       FindContact                   *fc);
164 static GList *          contact_list_store_find_contact              (EmpathyContactListStore       *store,
165                                                                       EmpathyContact                *contact);
166 static gboolean         contact_list_store_update_list_mode_foreach  (GtkTreeModel                  *model,
167                                                                       GtkTreePath                   *path,
168                                                                       GtkTreeIter                   *iter,
169                                                                       EmpathyContactListStore       *store);
170
171 enum {
172         PROP_0,
173         PROP_CONTACT_LIST,
174         PROP_SHOW_OFFLINE,
175         PROP_SHOW_AVATARS,
176         PROP_SHOW_PROTOCOLS,
177         PROP_SHOW_GROUPS,
178         PROP_IS_COMPACT,
179         PROP_SORT_CRITERIUM
180 };
181
182 G_DEFINE_TYPE (EmpathyContactListStore, empathy_contact_list_store, GTK_TYPE_TREE_STORE);
183
184 static gboolean
185 contact_list_store_iface_setup (gpointer user_data)
186 {
187         EmpathyContactListStore     *store = user_data;
188         EmpathyContactListStorePriv *priv = GET_PRIV (store);
189         GList                       *contacts, *l;
190
191         /* Signal connection. */
192         g_signal_connect (priv->list,
193                           "member-renamed",
194                           G_CALLBACK (contact_list_store_member_renamed_cb),
195                           store);
196         g_signal_connect (priv->list,
197                           "members-changed",
198                           G_CALLBACK (contact_list_store_members_changed_cb),
199                           store);
200         g_signal_connect (priv->list,
201                           "favourites-changed",
202                           G_CALLBACK (contact_list_store_favourites_changed_cb),
203                           store);
204         g_signal_connect (priv->list,
205                           "groups-changed",
206                           G_CALLBACK (contact_list_store_groups_changed_cb),
207                           store);
208
209         /* Add contacts already created. */
210         contacts = empathy_contact_list_get_members (priv->list);
211         for (l = contacts; l; l = l->next) {
212                 contact_list_store_members_changed_cb (priv->list, l->data,
213                                                        NULL, 0, NULL,
214                                                        TRUE,
215                                                        store);
216
217                 g_object_unref (l->data);
218         }
219         g_list_free (contacts);
220
221         priv->setup_idle_id = 0;
222         return FALSE;
223 }
224
225
226 static void
227 contact_list_store_set_contact_list (EmpathyContactListStore *store,
228                                      EmpathyContactList      *list_iface)
229 {
230         EmpathyContactListStorePriv *priv = GET_PRIV (store);
231
232         priv->list = g_object_ref (list_iface);
233
234         /* Let a chance to have all properties set before populating */
235         priv->setup_idle_id = g_idle_add (contact_list_store_iface_setup, store);
236 }
237
238 static void
239 empathy_contact_list_store_class_init (EmpathyContactListStoreClass *klass)
240 {
241         GObjectClass *object_class = G_OBJECT_CLASS (klass);
242
243         object_class->dispose = contact_list_store_dispose;
244         object_class->get_property = contact_list_store_get_property;
245         object_class->set_property = contact_list_store_set_property;
246
247         g_object_class_install_property (object_class,
248                                          PROP_CONTACT_LIST,
249                                          g_param_spec_object ("contact-list",
250                                                               "The contact list iface",
251                                                               "The contact list iface",
252                                                               EMPATHY_TYPE_CONTACT_LIST,
253                                                               G_PARAM_CONSTRUCT_ONLY |
254                                                               G_PARAM_READWRITE));
255         g_object_class_install_property (object_class,
256                                          PROP_SHOW_OFFLINE,
257                                          g_param_spec_boolean ("show-offline",
258                                                                "Show Offline",
259                                                                "Whether contact list should display "
260                                                                "offline contacts",
261                                                                FALSE,
262                                                                G_PARAM_READWRITE));
263          g_object_class_install_property (object_class,
264                                           PROP_SHOW_AVATARS,
265                                           g_param_spec_boolean ("show-avatars",
266                                                                 "Show Avatars",
267                                                                 "Whether contact list should display "
268                                                                 "avatars for contacts",
269                                                                 TRUE,
270                                                                 G_PARAM_READWRITE));
271          g_object_class_install_property (object_class,
272                                           PROP_SHOW_PROTOCOLS,
273                                           g_param_spec_boolean ("show-protocols",
274                                                                 "Show Protocols",
275                                                                 "Whether contact list should display "
276                                                                 "protocols for contacts",
277                                                                 FALSE,
278                                                                 G_PARAM_READWRITE));
279          g_object_class_install_property (object_class,
280                                           PROP_SHOW_GROUPS,
281                                           g_param_spec_boolean ("show-groups",
282                                                                 "Show Groups",
283                                                                 "Whether contact list should display "
284                                                                 "contact groups",
285                                                                 TRUE,
286                                                                 G_PARAM_READWRITE));
287         g_object_class_install_property (object_class,
288                                          PROP_IS_COMPACT,
289                                          g_param_spec_boolean ("is-compact",
290                                                                "Is Compact",
291                                                                "Whether the contact list is in compact mode or not",
292                                                                FALSE,
293                                                                G_PARAM_READWRITE));
294
295         g_object_class_install_property (object_class,
296                                          PROP_SORT_CRITERIUM,
297                                          g_param_spec_enum ("sort-criterium",
298                                                             "Sort citerium",
299                                                             "The sort criterium to use for sorting the contact list",
300                                                             EMPATHY_TYPE_CONTACT_LIST_STORE_SORT,
301                                                             EMPATHY_CONTACT_LIST_STORE_SORT_NAME,
302                                                             G_PARAM_READWRITE));
303
304         g_type_class_add_private (object_class, sizeof (EmpathyContactListStorePriv));
305 }
306
307 static void
308 empathy_contact_list_store_init (EmpathyContactListStore *store)
309 {
310         EmpathyContactListStorePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (store,
311                 EMPATHY_TYPE_CONTACT_LIST_STORE, EmpathyContactListStorePriv);
312
313         store->priv = priv;
314         priv->show_avatars = TRUE;
315         priv->show_groups = TRUE;
316         priv->show_protocols = FALSE;
317         priv->inhibit_active = g_timeout_add_seconds (ACTIVE_USER_WAIT_TO_ENABLE_TIME,
318                                                       (GSourceFunc) contact_list_store_inibit_active_cb,
319                                                       store);
320         priv->status_icons = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
321         contact_list_store_setup (store);
322 }
323
324 static void
325 contact_list_store_dispose (GObject *object)
326 {
327         EmpathyContactListStorePriv *priv = GET_PRIV (object);
328         GList                       *contacts, *l;
329
330         if (priv->dispose_has_run)
331                 return;
332         priv->dispose_has_run = TRUE;
333
334         contacts = empathy_contact_list_get_members (priv->list);
335         for (l = contacts; l; l = l->next) {
336                 g_signal_handlers_disconnect_by_func (l->data,
337                                                       G_CALLBACK (contact_list_store_contact_updated_cb),
338                                                       object);
339
340                 g_object_unref (l->data);
341         }
342         g_list_free (contacts);
343
344         g_signal_handlers_disconnect_by_func (priv->list,
345                                               G_CALLBACK (contact_list_store_member_renamed_cb),
346                                               object);
347         g_signal_handlers_disconnect_by_func (priv->list,
348                                               G_CALLBACK (contact_list_store_members_changed_cb),
349                                               object);
350         g_signal_handlers_disconnect_by_func (priv->list,
351                                               G_CALLBACK (contact_list_store_favourites_changed_cb),
352                                               object);
353         g_signal_handlers_disconnect_by_func (priv->list,
354                                               G_CALLBACK (contact_list_store_groups_changed_cb),
355                                               object);
356         g_object_unref (priv->list);
357
358         if (priv->inhibit_active) {
359                 g_source_remove (priv->inhibit_active);
360         }
361
362         if (priv->setup_idle_id != 0) {
363                 g_source_remove (priv->setup_idle_id);
364         }
365
366         g_hash_table_destroy (priv->status_icons);
367         G_OBJECT_CLASS (empathy_contact_list_store_parent_class)->dispose (object);
368 }
369
370 static void
371 contact_list_store_get_property (GObject    *object,
372                                  guint       param_id,
373                                  GValue     *value,
374                                  GParamSpec *pspec)
375 {
376         EmpathyContactListStorePriv *priv;
377
378         priv = GET_PRIV (object);
379
380         switch (param_id) {
381         case PROP_CONTACT_LIST:
382                 g_value_set_object (value, priv->list);
383                 break;
384         case PROP_SHOW_OFFLINE:
385                 g_value_set_boolean (value, priv->show_offline);
386                 break;
387         case PROP_SHOW_AVATARS:
388                 g_value_set_boolean (value, priv->show_avatars);
389                 break;
390         case PROP_SHOW_PROTOCOLS:
391                 g_value_set_boolean (value, priv->show_protocols);
392                 break;
393         case PROP_SHOW_GROUPS:
394                 g_value_set_boolean (value, priv->show_groups);
395                 break;
396         case PROP_IS_COMPACT:
397                 g_value_set_boolean (value, priv->is_compact);
398                 break;
399         case PROP_SORT_CRITERIUM:
400                 g_value_set_enum (value, priv->sort_criterium);
401                 break;
402         default:
403                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
404                 break;
405         };
406 }
407
408 static void
409 contact_list_store_set_property (GObject      *object,
410                                  guint         param_id,
411                                  const GValue *value,
412                                  GParamSpec   *pspec)
413 {
414         EmpathyContactListStorePriv *priv;
415
416         priv = GET_PRIV (object);
417
418         switch (param_id) {
419         case PROP_CONTACT_LIST:
420                 contact_list_store_set_contact_list (EMPATHY_CONTACT_LIST_STORE (object),
421                                                      g_value_get_object (value));
422                 break;
423         case PROP_SHOW_OFFLINE:
424                 empathy_contact_list_store_set_show_offline (EMPATHY_CONTACT_LIST_STORE (object),
425                                                             g_value_get_boolean (value));
426                 break;
427         case PROP_SHOW_AVATARS:
428                 empathy_contact_list_store_set_show_avatars (EMPATHY_CONTACT_LIST_STORE (object),
429                                                             g_value_get_boolean (value));
430                 break;
431         case PROP_SHOW_PROTOCOLS:
432                 empathy_contact_list_store_set_show_protocols (EMPATHY_CONTACT_LIST_STORE (object),
433                                                             g_value_get_boolean (value));
434                 break;
435         case PROP_SHOW_GROUPS:
436                 empathy_contact_list_store_set_show_groups (EMPATHY_CONTACT_LIST_STORE (object),
437                                                             g_value_get_boolean (value));
438                 break;
439         case PROP_IS_COMPACT:
440                 empathy_contact_list_store_set_is_compact (EMPATHY_CONTACT_LIST_STORE (object),
441                                                           g_value_get_boolean (value));
442                 break;
443         case PROP_SORT_CRITERIUM:
444                 empathy_contact_list_store_set_sort_criterium (EMPATHY_CONTACT_LIST_STORE (object),
445                                                               g_value_get_enum (value));
446                 break;
447         default:
448                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
449                 break;
450         };
451 }
452
453 EmpathyContactListStore *
454 empathy_contact_list_store_new (EmpathyContactList *list_iface)
455 {
456         g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (list_iface), NULL);
457
458         return g_object_new (EMPATHY_TYPE_CONTACT_LIST_STORE,
459                              "contact-list", list_iface,
460                              NULL);
461 }
462
463 EmpathyContactList *
464 empathy_contact_list_store_get_list_iface (EmpathyContactListStore *store)
465 {
466         EmpathyContactListStorePriv *priv;
467
468         g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), FALSE);
469
470         priv = GET_PRIV (store);
471
472         return priv->list;
473 }
474
475 gboolean
476 empathy_contact_list_store_get_show_offline (EmpathyContactListStore *store)
477 {
478         EmpathyContactListStorePriv *priv;
479
480         g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), FALSE);
481
482         priv = GET_PRIV (store);
483
484         return priv->show_offline;
485 }
486
487 void
488 empathy_contact_list_store_set_show_offline (EmpathyContactListStore *store,
489                                             gboolean                show_offline)
490 {
491         EmpathyContactListStorePriv *priv;
492         GList                      *contacts, *l;
493         gboolean                    show_active;
494
495         g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
496
497         priv = GET_PRIV (store);
498
499         priv->show_offline = show_offline;
500         show_active = priv->show_active;
501
502         /* Disable temporarily. */
503         priv->show_active = FALSE;
504
505         contacts = empathy_contact_list_get_members (priv->list);
506         for (l = contacts; l; l = l->next) {
507                 contact_list_store_contact_update (store, l->data);
508
509                 g_object_unref (l->data);
510         }
511         g_list_free (contacts);
512
513         /* Restore to original setting. */
514         priv->show_active = show_active;
515
516         g_object_notify (G_OBJECT (store), "show-offline");
517 }
518
519 gboolean
520 empathy_contact_list_store_get_show_avatars (EmpathyContactListStore *store)
521 {
522         EmpathyContactListStorePriv *priv;
523
524         g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), TRUE);
525
526         priv = GET_PRIV (store);
527
528         return priv->show_avatars;
529 }
530
531 void
532 empathy_contact_list_store_set_show_avatars (EmpathyContactListStore *store,
533                                             gboolean                show_avatars)
534 {
535         EmpathyContactListStorePriv *priv;
536         GtkTreeModel               *model;
537
538         g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
539
540         priv = GET_PRIV (store);
541
542         priv->show_avatars = show_avatars;
543
544         model = GTK_TREE_MODEL (store);
545
546         gtk_tree_model_foreach (model,
547                                 (GtkTreeModelForeachFunc)
548                                 contact_list_store_update_list_mode_foreach,
549                                 store);
550
551         g_object_notify (G_OBJECT (store), "show-avatars");
552 }
553
554
555 gboolean
556 empathy_contact_list_store_get_show_protocols (EmpathyContactListStore *store)
557 {
558         EmpathyContactListStorePriv *priv;
559
560         g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), TRUE);
561
562         priv = GET_PRIV (store);
563
564         return priv->show_protocols;
565 }
566
567 void
568 empathy_contact_list_store_set_show_protocols (EmpathyContactListStore *store,
569                                             gboolean                show_protocols)
570 {
571         EmpathyContactListStorePriv *priv;
572         GtkTreeModel               *model;
573
574         g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
575
576         priv = GET_PRIV (store);
577
578         priv->show_protocols = show_protocols;
579
580         model = GTK_TREE_MODEL (store);
581
582         gtk_tree_model_foreach (model,
583                                 (GtkTreeModelForeachFunc)
584                                 contact_list_store_update_list_mode_foreach,
585                                 store);
586
587         g_object_notify (G_OBJECT (store), "show-protocols");
588 }
589
590 gboolean
591 empathy_contact_list_store_get_show_groups (EmpathyContactListStore *store)
592 {
593         EmpathyContactListStorePriv *priv;
594
595         g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), TRUE);
596
597         priv = GET_PRIV (store);
598
599         return priv->show_groups;
600 }
601
602 void
603 empathy_contact_list_store_set_show_groups (EmpathyContactListStore *store,
604                                             gboolean                 show_groups)
605 {
606         EmpathyContactListStorePriv *priv;
607         GList                       *contacts, *l;
608
609         g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
610
611         priv = GET_PRIV (store);
612
613         if (priv->show_groups == show_groups) {
614                 return;
615         }
616
617         priv->show_groups = show_groups;
618
619         /* Remove all contacts and add them back, not optimized but that's the
620          * easy way :) */
621         gtk_tree_store_clear (GTK_TREE_STORE (store));
622         contacts = empathy_contact_list_get_members (priv->list);
623         for (l = contacts; l; l = l->next) {
624                 contact_list_store_members_changed_cb (priv->list, l->data,
625                                                        NULL, 0, NULL,
626                                                        TRUE,
627                                                        store);
628
629                 g_object_unref (l->data);
630         }
631         g_list_free (contacts);
632
633         g_object_notify (G_OBJECT (store), "show-groups");
634 }
635
636 gboolean
637 empathy_contact_list_store_get_is_compact (EmpathyContactListStore *store)
638 {
639         EmpathyContactListStorePriv *priv;
640
641         g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), TRUE);
642
643         priv = GET_PRIV (store);
644
645         return priv->is_compact;
646 }
647
648 void
649 empathy_contact_list_store_set_is_compact (EmpathyContactListStore *store,
650                                           gboolean                is_compact)
651 {
652         EmpathyContactListStorePriv *priv;
653         GtkTreeModel               *model;
654
655         g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
656
657         priv = GET_PRIV (store);
658
659         priv->is_compact = is_compact;
660
661         model = GTK_TREE_MODEL (store);
662
663         gtk_tree_model_foreach (model,
664                                 (GtkTreeModelForeachFunc)
665                                 contact_list_store_update_list_mode_foreach,
666                                 store);
667
668         g_object_notify (G_OBJECT (store), "is-compact");
669 }
670
671 EmpathyContactListStoreSort
672 empathy_contact_list_store_get_sort_criterium (EmpathyContactListStore *store)
673 {
674         EmpathyContactListStorePriv *priv;
675
676         g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), 0);
677
678         priv = GET_PRIV (store);
679
680         return priv->sort_criterium;
681 }
682
683 void
684 empathy_contact_list_store_set_sort_criterium (EmpathyContactListStore     *store,
685                                               EmpathyContactListStoreSort  sort_criterium)
686 {
687         EmpathyContactListStorePriv *priv;
688
689         g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
690
691         priv = GET_PRIV (store);
692
693         priv->sort_criterium = sort_criterium;
694
695         switch (sort_criterium) {
696         case EMPATHY_CONTACT_LIST_STORE_SORT_STATE:
697                 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
698                                                       EMPATHY_CONTACT_LIST_STORE_COL_STATUS,
699                                                       GTK_SORT_ASCENDING);
700                 break;
701
702         case EMPATHY_CONTACT_LIST_STORE_SORT_NAME:
703                 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
704                                                       EMPATHY_CONTACT_LIST_STORE_COL_NAME,
705                                                       GTK_SORT_ASCENDING);
706                 break;
707         }
708
709         g_object_notify (G_OBJECT (store), "sort-criterium");
710 }
711
712 gboolean
713 empathy_contact_list_store_row_separator_func (GtkTreeModel *model,
714                                               GtkTreeIter  *iter,
715                                               gpointer      data)
716 {
717         gboolean is_separator = FALSE;
718
719         g_return_val_if_fail (GTK_IS_TREE_MODEL (model), FALSE);
720
721         gtk_tree_model_get (model, iter,
722                             EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator,
723                             -1);
724
725         return is_separator;
726 }
727
728 gchar *
729 empathy_contact_list_store_get_parent_group (GtkTreeModel *model,
730                                             GtkTreePath  *path,
731                                             gboolean     *path_is_group)
732 {
733         GtkTreeIter  parent_iter, iter;
734         gchar       *name = NULL;
735         gboolean     is_group;
736
737         g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
738
739         if (path_is_group) {
740                 *path_is_group = FALSE;
741         }
742
743         if (!gtk_tree_model_get_iter (model, &iter, path)) {
744                 return NULL;
745         }
746
747         gtk_tree_model_get (model, &iter,
748                             EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
749                             EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
750                             -1);
751
752         if (!is_group) {
753                 g_free (name);
754                 name = NULL;
755
756                 if (!gtk_tree_model_iter_parent (model, &parent_iter, &iter)) {
757                         return NULL;
758                 }
759
760                 iter = parent_iter;
761
762                 gtk_tree_model_get (model, &iter,
763                                     EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
764                                     EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
765                                     -1);
766                 if (!is_group) {
767                         g_free (name);
768                         return NULL;
769                 }
770         }
771
772         if (path_is_group) {
773                 *path_is_group = TRUE;
774         }
775
776         return name;
777 }
778
779 gboolean
780 empathy_contact_list_store_search_equal_func (GtkTreeModel *model,
781                                               gint          column,
782                                               const gchar  *key,
783                                               GtkTreeIter  *iter,
784                                               gpointer      search_data)
785 {
786         gchar    *name, *name_folded;
787         gchar    *key_folded;
788         gboolean  ret;
789
790         g_return_val_if_fail (GTK_IS_TREE_MODEL (model), FALSE);
791
792         if (!key) {
793                 return TRUE;
794         }
795
796         gtk_tree_model_get (model, iter,
797                             EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
798                             -1);
799
800         if (!name) {
801                 return TRUE;
802         }
803
804         name_folded = g_utf8_casefold (name, -1);
805         key_folded = g_utf8_casefold (key, -1);
806
807         if (name_folded && key_folded &&
808             strstr (name_folded, key_folded)) {
809                 ret = FALSE;
810         } else {
811                 ret = TRUE;
812         }
813
814         g_free (name);
815         g_free (name_folded);
816         g_free (key_folded);
817
818         return ret;
819 }
820
821 static void
822 contact_list_store_setup (EmpathyContactListStore *store)
823 {
824         EmpathyContactListStorePriv *priv;
825         GType types[] = {
826                 GDK_TYPE_PIXBUF,      /* Status pixbuf */
827                 GDK_TYPE_PIXBUF,      /* Avatar pixbuf */
828                 G_TYPE_BOOLEAN,       /* Avatar pixbuf visible */
829                 G_TYPE_STRING,        /* Name */
830                 G_TYPE_STRING,        /* Status string */
831                 G_TYPE_BOOLEAN,       /* Show status */
832                 EMPATHY_TYPE_CONTACT, /* Contact type */
833                 G_TYPE_BOOLEAN,       /* Is group */
834                 G_TYPE_BOOLEAN,       /* Is active */
835                 G_TYPE_BOOLEAN,       /* Is online */
836                 G_TYPE_BOOLEAN,       /* Is separator */
837                 G_TYPE_BOOLEAN,       /* Can make audio calls */
838                 G_TYPE_BOOLEAN,       /* Can make video calls */
839                 EMPATHY_TYPE_CONTACT_LIST_FLAGS, /* Flags */
840                 G_TYPE_BOOLEAN,       /* Is a favourite */
841                 G_TYPE_BOOLEAN,       /* Is a fake group */
842         };
843
844         priv = GET_PRIV (store);
845
846         gtk_tree_store_set_column_types (GTK_TREE_STORE (store),
847                                          EMPATHY_CONTACT_LIST_STORE_COL_COUNT,
848                                          types);
849
850         /* Set up sorting */
851         gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store),
852                                          EMPATHY_CONTACT_LIST_STORE_COL_NAME,
853                                          contact_list_store_name_sort_func,
854                                          store, NULL);
855         gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store),
856                                          EMPATHY_CONTACT_LIST_STORE_COL_STATUS,
857                                          contact_list_store_state_sort_func,
858                                          store, NULL);
859
860         priv->sort_criterium = EMPATHY_CONTACT_LIST_STORE_SORT_NAME;
861         empathy_contact_list_store_set_sort_criterium (store, priv->sort_criterium);
862 }
863
864 static gboolean
865 contact_list_store_inibit_active_cb (EmpathyContactListStore *store)
866 {
867         EmpathyContactListStorePriv *priv;
868
869         priv = GET_PRIV (store);
870
871         priv->show_active = TRUE;
872         priv->inhibit_active = 0;
873
874         return FALSE;
875 }
876
877 static void
878 contact_list_store_add_contact_and_connect (EmpathyContactListStore *store, EmpathyContact *contact)
879 {
880         g_signal_connect (contact, "notify::presence",
881                           G_CALLBACK (contact_list_store_contact_updated_cb),
882                           store);
883         g_signal_connect (contact, "notify::presence-message",
884                           G_CALLBACK (contact_list_store_contact_updated_cb),
885                           store);
886         g_signal_connect (contact, "notify::name",
887                           G_CALLBACK (contact_list_store_contact_updated_cb),
888                           store);
889         g_signal_connect (contact, "notify::avatar",
890                           G_CALLBACK (contact_list_store_contact_updated_cb),
891                           store);
892         g_signal_connect (contact, "notify::capabilities",
893                           G_CALLBACK (contact_list_store_contact_updated_cb),
894                           store);
895
896         contact_list_store_add_contact (store, contact);
897 }
898
899 static void
900 contact_list_store_remove_contact_and_disconnect (EmpathyContactListStore *store, EmpathyContact *contact)
901 {
902         g_signal_handlers_disconnect_by_func (contact,
903                                               G_CALLBACK (contact_list_store_contact_updated_cb),
904                                               store);
905
906         contact_list_store_remove_contact (store, contact);
907 }
908
909 static void
910 contact_list_store_members_changed_cb (EmpathyContactList      *list_iface,
911                                        EmpathyContact          *contact,
912                                        EmpathyContact          *actor,
913                                        guint                    reason,
914                                        gchar                   *message,
915                                        gboolean                 is_member,
916                                        EmpathyContactListStore *store)
917 {
918         EmpathyContactListStorePriv *priv;
919
920         priv = GET_PRIV (store);
921
922         DEBUG ("Contact %s (%d) %s",
923                 empathy_contact_get_id (contact),
924                 empathy_contact_get_handle (contact),
925                 is_member ? "added" : "removed");
926
927         if (is_member) {
928                 contact_list_store_add_contact_and_connect (store, contact);
929         } else {
930                 contact_list_store_remove_contact_and_disconnect (store, contact);
931         }
932 }
933
934 static void
935 contact_list_store_change_contact_favourite_status (EmpathyContactListStore *store,
936                                                     EmpathyContact          *contact,
937                                                     gboolean                 is_favourite)
938 {
939         GList *iters, *l;
940
941         iters = contact_list_store_find_contact (store, contact);
942         for (l = iters; l; l = l->next) {
943                 gtk_tree_store_set (GTK_TREE_STORE (store), l->data,
944                         EMPATHY_CONTACT_LIST_STORE_COL_IS_FAVOURITE,
945                         is_favourite,
946                         -1);
947         }
948
949         g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
950         g_list_free (iters);
951 }
952
953 static void
954 contact_list_store_favourites_changed_cb (EmpathyContactList      *list_iface,
955                                           EmpathyContact          *contact,
956                                           gboolean                 is_favourite,
957                                           EmpathyContactListStore *store)
958 {
959         EmpathyContactListStorePriv *priv;
960
961         priv = GET_PRIV (store);
962
963         DEBUG ("Contact %s (%d) is %s a favourite",
964                 empathy_contact_get_id (contact),
965                 empathy_contact_get_handle (contact),
966                 is_favourite ? "now" : "no longer");
967
968         contact_list_store_change_contact_favourite_status (store, contact,
969                                                                                                                                                                                                       is_favourite);
970
971         contact_list_store_remove_contact (store, contact);
972         contact_list_store_add_contact (store, contact);
973 }
974
975 static void
976 contact_list_store_member_renamed_cb (EmpathyContactList      *list_iface,
977                                       EmpathyContact          *old_contact,
978                                       EmpathyContact          *new_contact,
979                                       guint                    reason,
980                                       gchar                   *message,
981                                       EmpathyContactListStore *store)
982 {
983         EmpathyContactListStorePriv *priv;
984
985         priv = GET_PRIV (store);
986
987         DEBUG ("Contact %s (%d) renamed to %s (%d)",
988                 empathy_contact_get_id (old_contact),
989                 empathy_contact_get_handle (old_contact),
990                 empathy_contact_get_id (new_contact),
991                 empathy_contact_get_handle (new_contact));
992
993         /* add the new contact */
994         contact_list_store_add_contact_and_connect (store, new_contact);
995
996         /* remove old contact */
997         contact_list_store_remove_contact_and_disconnect (store, old_contact);
998 }
999
1000 static void
1001 contact_list_store_groups_changed_cb (EmpathyContactList      *list_iface,
1002                                       EmpathyContact          *contact,
1003                                       gchar                   *group,
1004                                       gboolean                 is_member,
1005                                       EmpathyContactListStore *store)
1006 {
1007         EmpathyContactListStorePriv *priv;
1008         gboolean                     show_active;
1009
1010         priv = GET_PRIV (store);
1011
1012         DEBUG ("Updating groups for contact %s (%d)",
1013                 empathy_contact_get_id (contact),
1014                 empathy_contact_get_handle (contact));
1015
1016         /* We do this to make sure the groups are correct, if not, we
1017          * would have to check the groups already set up for each
1018          * contact and then see what has been updated.
1019          */
1020         show_active = priv->show_active;
1021         priv->show_active = FALSE;
1022         contact_list_store_remove_contact (store, contact);
1023         contact_list_store_add_contact (store, contact);
1024         priv->show_active = show_active;
1025 }
1026
1027 static void
1028 add_contact_to_store (GtkTreeStore *store,
1029                       GtkTreeIter *iter,
1030                       EmpathyContact *contact,
1031                       EmpathyContactListFlags flags)
1032 {
1033         gtk_tree_store_set (store, iter,
1034                             EMPATHY_CONTACT_LIST_STORE_COL_NAME, empathy_contact_get_name (contact),
1035                             EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, contact,
1036                             EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, FALSE,
1037                             EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, FALSE,
1038                             EMPATHY_CONTACT_LIST_STORE_COL_CAN_AUDIO_CALL,
1039                               empathy_contact_get_capabilities (contact) &
1040                                 EMPATHY_CAPABILITIES_AUDIO,
1041                             EMPATHY_CONTACT_LIST_STORE_COL_CAN_VIDEO_CALL,
1042                               empathy_contact_get_capabilities (contact) &
1043                                 EMPATHY_CAPABILITIES_VIDEO,
1044                             EMPATHY_CONTACT_LIST_STORE_COL_FLAGS, flags,
1045                             -1);
1046 }
1047
1048 static void
1049 contact_list_store_add_contact (EmpathyContactListStore *store,
1050                                 EmpathyContact          *contact)
1051 {
1052         EmpathyContactListStorePriv *priv;
1053         GtkTreeIter                 iter;
1054         GList                      *groups = NULL, *l;
1055         TpConnection               *connection;
1056         EmpathyContactListFlags     flags = 0;
1057
1058         priv = GET_PRIV (store);
1059
1060         if (EMP_STR_EMPTY (empathy_contact_get_name (contact)) ||
1061             (!priv->show_offline && !empathy_contact_is_online (contact))) {
1062                 return;
1063         }
1064
1065         if (priv->show_groups) {
1066                 groups = empathy_contact_list_get_groups (priv->list, contact);
1067         }
1068
1069         connection = empathy_contact_get_connection (contact);
1070         if (EMPATHY_IS_CONTACT_MANAGER (priv->list)) {
1071                 flags = empathy_contact_manager_get_flags_for_connection (
1072                         EMPATHY_CONTACT_MANAGER (priv->list), connection);
1073         }
1074         if (!groups) {
1075 #if HAVE_FAVOURITE_CONTACTS
1076                 GtkTreeIter iter_group;
1077
1078                 contact_list_store_get_group (store, EMPATHY_CONTACT_LIST_STORE_UNGROUPED,
1079                         &iter_group, NULL, NULL, TRUE);
1080
1081                 gtk_tree_store_insert_after (GTK_TREE_STORE (store), &iter,
1082                                              &iter_group, NULL);
1083 #else
1084                 /* FIXME: remove this in 2.31.x */
1085                 /* If no groups just add it at the top level. */
1086                 GtkTreeModel *model = GTK_TREE_MODEL (store);
1087
1088                 if (gtk_tree_model_get_iter_first (model, &iter)) do {
1089                         EmpathyContact *c;
1090
1091                         gtk_tree_model_get (model, &iter,
1092                                 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &c,
1093                                 -1);
1094
1095                         if (c == contact) {
1096                                 g_object_unref (c);
1097                                 return;
1098                         }
1099                         if (c != NULL)
1100                                 g_object_unref (c);
1101                 } while (gtk_tree_model_iter_next (model, &iter));
1102
1103                 gtk_tree_store_append (GTK_TREE_STORE (store), &iter, NULL);
1104 #endif
1105
1106                 add_contact_to_store (GTK_TREE_STORE (store), &iter, contact, flags);
1107         }
1108
1109         /* Else add to each group. */
1110         for (l = groups; l; l = l->next) {
1111                 GtkTreeIter iter_group;
1112
1113                 contact_list_store_get_group (store, l->data, &iter_group, NULL, NULL, FALSE);
1114
1115                 gtk_tree_store_insert_after (GTK_TREE_STORE (store), &iter,
1116                                              &iter_group, NULL);
1117
1118                 add_contact_to_store (GTK_TREE_STORE (store), &iter, contact, flags);
1119                 g_free (l->data);
1120         }
1121         g_list_free (groups);
1122
1123 #ifdef HAVE_FAVOURITE_CONTACTS
1124         if (empathy_contact_list_is_favourite (priv->list, contact)) {
1125         /* Add contact to the fake 'Favorites' group */
1126                 GtkTreeIter iter_group;
1127
1128                 contact_list_store_get_group (store, EMPATHY_CONTACT_LIST_STORE_FAVORITE,
1129                         &iter_group, NULL, NULL, TRUE);
1130
1131                 gtk_tree_store_insert_after (GTK_TREE_STORE (store), &iter,
1132                                              &iter_group, NULL);
1133
1134                 add_contact_to_store (GTK_TREE_STORE (store), &iter, contact, flags);
1135         }
1136 #endif
1137
1138         contact_list_store_contact_update (store, contact);
1139 }
1140
1141 static void
1142 contact_list_store_remove_contact (EmpathyContactListStore *store,
1143                                    EmpathyContact          *contact)
1144 {
1145         EmpathyContactListStorePriv *priv;
1146         GtkTreeModel               *model;
1147         GList                      *iters, *l;
1148
1149         priv = GET_PRIV (store);
1150
1151         iters = contact_list_store_find_contact (store, contact);
1152         if (!iters) {
1153                 return;
1154         }
1155
1156         /* Clean up model */
1157         model = GTK_TREE_MODEL (store);
1158
1159         for (l = iters; l; l = l->next) {
1160                 GtkTreeIter parent;
1161
1162                 /* NOTE: it is only <= 2 here because we have
1163                  * separators after the group name, otherwise it
1164                  * should be 1.
1165                  */
1166                 if (gtk_tree_model_iter_parent (model, &parent, l->data) &&
1167                     gtk_tree_model_iter_n_children (model, &parent) <= 2) {
1168                         gtk_tree_store_remove (GTK_TREE_STORE (store), &parent);
1169                 } else {
1170                         gtk_tree_store_remove (GTK_TREE_STORE (store), l->data);
1171                 }
1172         }
1173
1174         g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
1175         g_list_free (iters);
1176 }
1177
1178 static gboolean
1179 list_store_contact_is_favourite (EmpathyContactListStore *store,
1180                                  EmpathyContact          *contact)
1181 {
1182         EmpathyContactListStorePriv *priv;
1183
1184         priv = GET_PRIV (store);
1185
1186         return empathy_contact_list_is_favourite (priv->list, contact);
1187 }
1188
1189 static void
1190 contact_list_store_contact_update (EmpathyContactListStore *store,
1191                                    EmpathyContact          *contact)
1192 {
1193         EmpathyContactListStorePriv *priv;
1194         ShowActiveData             *data;
1195         GtkTreeModel               *model;
1196         GList                      *iters, *l;
1197         gboolean                    in_list;
1198         gboolean                    should_be_in_list;
1199         gboolean                    was_online = TRUE;
1200         gboolean                    now_online = FALSE;
1201         gboolean                    set_model = FALSE;
1202         gboolean                    do_remove = FALSE;
1203         gboolean                    do_set_active = FALSE;
1204         gboolean                    do_set_refresh = FALSE;
1205         gboolean                    show_avatar = FALSE;
1206         GdkPixbuf                  *pixbuf_avatar;
1207         GdkPixbuf                  *pixbuf_status;
1208
1209         priv = GET_PRIV (store);
1210
1211         model = GTK_TREE_MODEL (store);
1212
1213         iters = contact_list_store_find_contact (store, contact);
1214         if (!iters) {
1215                 in_list = FALSE;
1216         } else {
1217                 in_list = TRUE;
1218         }
1219
1220         /* Get online state now. */
1221         now_online = empathy_contact_is_online (contact);
1222
1223         if (priv->show_offline || now_online) {
1224                 should_be_in_list = TRUE;
1225         } else {
1226                 should_be_in_list = FALSE;
1227         }
1228
1229         if (!in_list && !should_be_in_list) {
1230                 /* Nothing to do. */
1231                 DEBUG ("Contact:'%s' in list:NO, should be:NO",
1232                         empathy_contact_get_name (contact));
1233
1234                 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
1235                 g_list_free (iters);
1236                 return;
1237         }
1238         else if (in_list && !should_be_in_list) {
1239                 DEBUG ("Contact:'%s' in list:YES, should be:NO",
1240                         empathy_contact_get_name (contact));
1241
1242                 if (priv->show_active) {
1243                         do_remove = TRUE;
1244                         do_set_active = TRUE;
1245                         do_set_refresh = TRUE;
1246
1247                         set_model = TRUE;
1248                         DEBUG ("Remove item (after timeout)");
1249                 } else {
1250                         DEBUG ("Remove item (now)!");
1251                         contact_list_store_remove_contact (store, contact);
1252                 }
1253         }
1254         else if (!in_list && should_be_in_list) {
1255                 DEBUG ("Contact:'%s' in list:NO, should be:YES",
1256                         empathy_contact_get_name (contact));
1257
1258                 contact_list_store_add_contact (store, contact);
1259
1260                 if (priv->show_active) {
1261                         do_set_active = TRUE;
1262
1263                         DEBUG ("Set active (contact added)");
1264                 }
1265         } else {
1266                 DEBUG ("Contact:'%s' in list:YES, should be:YES",
1267                         empathy_contact_get_name (contact));
1268
1269                 /* Get online state before. */
1270                 if (iters && g_list_length (iters) > 0) {
1271                         gtk_tree_model_get (model, iters->data,
1272                                             EMPATHY_CONTACT_LIST_STORE_COL_IS_ONLINE, &was_online,
1273                                             -1);
1274                 }
1275
1276                 /* Is this really an update or an online/offline. */
1277                 if (priv->show_active) {
1278                         if (was_online != now_online) {
1279                                 do_set_active = TRUE;
1280                                 do_set_refresh = TRUE;
1281
1282                                 DEBUG ("Set active (contact updated %s)",
1283                                         was_online ? "online  -> offline" :
1284                                         "offline -> online");
1285                         } else {
1286                                 /* Was TRUE for presence updates. */
1287                                 /* do_set_active = FALSE;  */
1288                                 do_set_refresh = TRUE;
1289
1290                                 DEBUG ("Set active (contact updated)");
1291                         }
1292                 }
1293
1294                 set_model = TRUE;
1295         }
1296
1297         if (priv->show_avatars && !priv->is_compact) {
1298                 show_avatar = TRUE;
1299         }
1300         pixbuf_avatar = empathy_pixbuf_avatar_from_contact_scaled (contact, 32, 32);
1301         pixbuf_status = contact_list_store_get_contact_status_icon (store, contact);
1302         for (l = iters; l && set_model; l = l->next) {
1303                 gtk_tree_store_set (GTK_TREE_STORE (store), l->data,
1304                                     EMPATHY_CONTACT_LIST_STORE_COL_IS_FAVOURITE, list_store_contact_is_favourite (store, contact),
1305                                     EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, pixbuf_status,
1306                                     EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR, pixbuf_avatar,
1307                                     EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
1308                                     EMPATHY_CONTACT_LIST_STORE_COL_NAME, empathy_contact_get_name (contact),
1309                                     EMPATHY_CONTACT_LIST_STORE_COL_STATUS, empathy_contact_get_status (contact),
1310                                     EMPATHY_CONTACT_LIST_STORE_COL_STATUS_VISIBLE, !priv->is_compact,
1311                                     EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, FALSE,
1312                                     EMPATHY_CONTACT_LIST_STORE_COL_IS_ONLINE, now_online,
1313                                     EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, FALSE,
1314                                     EMPATHY_CONTACT_LIST_STORE_COL_CAN_AUDIO_CALL,
1315                                       empathy_contact_get_capabilities (contact) &
1316                                         EMPATHY_CAPABILITIES_AUDIO,
1317                                     EMPATHY_CONTACT_LIST_STORE_COL_CAN_VIDEO_CALL,
1318                                       empathy_contact_get_capabilities (contact) &
1319                                         EMPATHY_CAPABILITIES_VIDEO,
1320                                     -1);
1321         }
1322
1323         if (pixbuf_avatar) {
1324                 g_object_unref (pixbuf_avatar);
1325         }
1326
1327         if (priv->show_active && do_set_active) {
1328                 contact_list_store_contact_set_active (store, contact, do_set_active, do_set_refresh);
1329
1330                 if (do_set_active) {
1331                         data = contact_list_store_contact_active_new (store, contact, do_remove);
1332                         g_timeout_add_seconds (ACTIVE_USER_SHOW_TIME,
1333                                                (GSourceFunc) contact_list_store_contact_active_cb,
1334                                                data);
1335                 }
1336         }
1337
1338         /* FIXME: when someone goes online then offline quickly, the
1339          * first timeout sets the user to be inactive and the second
1340          * timeout removes the user from the contact list, really we
1341          * should remove the first timeout.
1342          */
1343         g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
1344         g_list_free (iters);
1345 }
1346
1347 static void
1348 contact_list_store_contact_updated_cb (EmpathyContact          *contact,
1349                                        GParamSpec              *param,
1350                                        EmpathyContactListStore *store)
1351 {
1352         DEBUG ("Contact:'%s' updated, checking roster is in sync...",
1353                 empathy_contact_get_name (contact));
1354
1355         contact_list_store_contact_update (store, contact);
1356 }
1357
1358 static void
1359 contact_list_store_contact_set_active (EmpathyContactListStore *store,
1360                                        EmpathyContact          *contact,
1361                                        gboolean                active,
1362                                        gboolean                set_changed)
1363 {
1364         EmpathyContactListStorePriv *priv;
1365         GtkTreeModel               *model;
1366         GList                      *iters, *l;
1367
1368         priv = GET_PRIV (store);
1369         model = GTK_TREE_MODEL (store);
1370
1371         iters = contact_list_store_find_contact (store, contact);
1372         for (l = iters; l; l = l->next) {
1373                 GtkTreePath *path;
1374
1375                 gtk_tree_store_set (GTK_TREE_STORE (store), l->data,
1376                                     EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, active,
1377                                     -1);
1378
1379                 DEBUG ("Set item %s", active ? "active" : "inactive");
1380
1381                 if (set_changed) {
1382                         path = gtk_tree_model_get_path (model, l->data);
1383                         gtk_tree_model_row_changed (model, path, l->data);
1384                         gtk_tree_path_free (path);
1385                 }
1386         }
1387
1388         g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
1389         g_list_free (iters);
1390
1391 }
1392
1393 static ShowActiveData *
1394 contact_list_store_contact_active_new (EmpathyContactListStore *store,
1395                                        EmpathyContact          *contact,
1396                                        gboolean                remove_)
1397 {
1398         ShowActiveData *data;
1399
1400         DEBUG ("Contact:'%s' now active, and %s be removed",
1401                 empathy_contact_get_name (contact),
1402                 remove_ ? "WILL" : "WILL NOT");
1403
1404         data = g_slice_new0 (ShowActiveData);
1405
1406         data->store = g_object_ref (store);
1407         data->contact = g_object_ref (contact);
1408         data->remove = remove_;
1409
1410         return data;
1411 }
1412
1413 static void
1414 contact_list_store_contact_active_free (ShowActiveData *data)
1415 {
1416         g_object_unref (data->contact);
1417         g_object_unref (data->store);
1418
1419         g_slice_free (ShowActiveData, data);
1420 }
1421
1422 static gboolean
1423 contact_list_store_contact_active_cb (ShowActiveData *data)
1424 {
1425         EmpathyContactListStorePriv *priv;
1426
1427         priv = GET_PRIV (data->store);
1428
1429         if (data->remove &&
1430             !priv->show_offline &&
1431             !empathy_contact_is_online (data->contact)) {
1432                 DEBUG ("Contact:'%s' active timeout, removing item",
1433                         empathy_contact_get_name (data->contact));
1434                 contact_list_store_remove_contact (data->store, data->contact);
1435         }
1436
1437         DEBUG ("Contact:'%s' no longer active",
1438                 empathy_contact_get_name (data->contact));
1439
1440         contact_list_store_contact_set_active (data->store,
1441                                                data->contact,
1442                                                FALSE,
1443                                                TRUE);
1444
1445         contact_list_store_contact_active_free (data);
1446
1447         return FALSE;
1448 }
1449
1450 static gboolean
1451 contact_list_store_get_group_foreach (GtkTreeModel *model,
1452                                       GtkTreePath  *path,
1453                                       GtkTreeIter  *iter,
1454                                       FindGroup    *fg)
1455 {
1456         gchar    *str;
1457         gboolean  is_group;
1458
1459         /* Groups are only at the top level. */
1460         if (gtk_tree_path_get_depth (path) != 1) {
1461                 return FALSE;
1462         }
1463
1464         gtk_tree_model_get (model, iter,
1465                             EMPATHY_CONTACT_LIST_STORE_COL_NAME, &str,
1466                             EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
1467                             -1);
1468
1469         if (is_group && !tp_strdiff (str, fg->name)) {
1470                 fg->found = TRUE;
1471                 fg->iter = *iter;
1472         }
1473
1474         g_free (str);
1475
1476         return fg->found;
1477 }
1478
1479 static void
1480 contact_list_store_get_group (EmpathyContactListStore *store,
1481                               const gchar            *name,
1482                               GtkTreeIter            *iter_group_to_set,
1483                               GtkTreeIter            *iter_separator_to_set,
1484                               gboolean               *created,
1485                               gboolean               is_fake_group)
1486 {
1487         EmpathyContactListStorePriv *priv;
1488         GtkTreeModel                *model;
1489         GtkTreeIter                  iter_group;
1490         GtkTreeIter                  iter_separator;
1491         FindGroup                    fg;
1492
1493         priv = GET_PRIV (store);
1494
1495         memset (&fg, 0, sizeof (fg));
1496
1497         fg.name = name;
1498
1499         model = GTK_TREE_MODEL (store);
1500         gtk_tree_model_foreach (model,
1501                                 (GtkTreeModelForeachFunc) contact_list_store_get_group_foreach,
1502                                 &fg);
1503
1504         if (!fg.found) {
1505                 if (created) {
1506                         *created = TRUE;
1507                 }
1508
1509                 gtk_tree_store_append (GTK_TREE_STORE (store), &iter_group, NULL);
1510                 gtk_tree_store_set (GTK_TREE_STORE (store), &iter_group,
1511                                     EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, NULL,
1512                                     EMPATHY_CONTACT_LIST_STORE_COL_NAME, name,
1513                                     EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, TRUE,
1514                                     EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, FALSE,
1515                                     EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, FALSE,
1516                                     EMPATHY_CONTACT_LIST_STORE_COL_IS_FAKE_GROUP, is_fake_group,
1517                                     -1);
1518
1519                 if (iter_group_to_set) {
1520                         *iter_group_to_set = iter_group;
1521                 }
1522
1523                 gtk_tree_store_append (GTK_TREE_STORE (store),
1524                                        &iter_separator,
1525                                        &iter_group);
1526                 gtk_tree_store_set (GTK_TREE_STORE (store), &iter_separator,
1527                                     EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, TRUE,
1528                                     -1);
1529
1530                 if (iter_separator_to_set) {
1531                         *iter_separator_to_set = iter_separator;
1532                 }
1533         } else {
1534                 if (created) {
1535                         *created = FALSE;
1536                 }
1537
1538                 if (iter_group_to_set) {
1539                         *iter_group_to_set = fg.iter;
1540                 }
1541
1542                 iter_separator = fg.iter;
1543
1544                 if (gtk_tree_model_iter_next (model, &iter_separator)) {
1545                         gboolean is_separator;
1546
1547                         gtk_tree_model_get (model, &iter_separator,
1548                                             EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator,
1549                                             -1);
1550
1551                         if (is_separator && iter_separator_to_set) {
1552                                 *iter_separator_to_set = iter_separator;
1553                         }
1554                 }
1555         }
1556 }
1557
1558 static gint
1559 compare_separator_and_groups (gboolean is_separator_a,
1560                               gboolean is_separator_b,
1561                               const gchar *name_a,
1562                               const gchar *name_b,
1563                               EmpathyContact *contact_a,
1564                               EmpathyContact *contact_b)
1565 {
1566         if (is_separator_a || is_separator_b) {
1567                 /* We have at least one separator */
1568                 if (is_separator_a) {
1569                         return -1;
1570                 } else if (is_separator_b) {
1571                         return 1;
1572                 }
1573         }
1574
1575         /* One group and one contact */
1576         if (!contact_a && contact_b) {
1577                 return 1;
1578         } else if (contact_a && !contact_b) {
1579                 return -1;
1580         } else if (!contact_a && !contact_b) {
1581                 /* Two groups. The 'Ungrouped' fake group is display at the bottom of the
1582                  * contact list and the 'Favorites' at the top. */
1583                 if (!tp_strdiff (name_a, EMPATHY_CONTACT_LIST_STORE_UNGROUPED))
1584                         return 1;
1585                 else if (!tp_strdiff (name_b, EMPATHY_CONTACT_LIST_STORE_UNGROUPED))
1586                         return -1;
1587                 else if (!tp_strdiff (name_a, EMPATHY_CONTACT_LIST_STORE_FAVORITE))
1588                         return -1;
1589                 else if (!tp_strdiff (name_b, EMPATHY_CONTACT_LIST_STORE_FAVORITE))
1590                         return 1;
1591                 else
1592                         return g_utf8_collate (name_a, name_b);
1593         }
1594
1595         /* Two contacts, ordering depends of the sorting policy */
1596         return 0;
1597 }
1598
1599 static gint
1600 contact_list_store_state_sort_func (GtkTreeModel *model,
1601                                     GtkTreeIter  *iter_a,
1602                                     GtkTreeIter  *iter_b,
1603                                     gpointer      user_data)
1604 {
1605         gint            ret_val;
1606         gchar          *name_a, *name_b;
1607         gboolean        is_separator_a, is_separator_b;
1608         gboolean        is_favourite_a, is_favourite_b;
1609         EmpathyContact *contact_a, *contact_b;
1610
1611         gtk_tree_model_get (model, iter_a,
1612                             EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name_a,
1613                             EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact_a,
1614                             EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator_a,
1615                             EMPATHY_CONTACT_LIST_STORE_COL_IS_FAVOURITE, &is_favourite_a,
1616                             -1);
1617         gtk_tree_model_get (model, iter_b,
1618                             EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name_b,
1619                             EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact_b,
1620                             EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator_b,
1621                             EMPATHY_CONTACT_LIST_STORE_COL_IS_FAVOURITE, &is_favourite_b,
1622                             -1);
1623
1624         ret_val = compare_separator_and_groups (is_separator_a, is_separator_b,
1625                 name_a, name_b, contact_a, contact_b);
1626
1627         if (ret_val != 0) {
1628                 goto free_and_out;
1629         }
1630
1631         /* If we managed to get this far, we can start looking at
1632          * the presences.
1633          */
1634         ret_val = -tp_connection_presence_type_cmp_availability (
1635                 empathy_contact_get_presence (EMPATHY_CONTACT (contact_a)),
1636                 empathy_contact_get_presence (EMPATHY_CONTACT (contact_b)));
1637
1638         if (ret_val == 0) {
1639                 /* Fallback: compare by name */
1640                 ret_val = g_utf8_collate (name_a, name_b);
1641         }
1642
1643 free_and_out:
1644         g_free (name_a);
1645         g_free (name_b);
1646
1647         if (contact_a) {
1648                 g_object_unref (contact_a);
1649         }
1650
1651         if (contact_b) {
1652                 g_object_unref (contact_b);
1653         }
1654
1655         return ret_val;
1656 }
1657
1658 static gint
1659 contact_list_store_name_sort_func (GtkTreeModel *model,
1660                                    GtkTreeIter  *iter_a,
1661                                    GtkTreeIter  *iter_b,
1662                                    gpointer      user_data)
1663 {
1664         gchar         *name_a, *name_b;
1665         EmpathyContact *contact_a, *contact_b;
1666         gboolean       is_separator_a = FALSE, is_separator_b = FALSE;
1667         gboolean       is_favourite_a, is_favourite_b;
1668         gint           ret_val;
1669
1670         gtk_tree_model_get (model, iter_a,
1671                             EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name_a,
1672                             EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact_a,
1673                             EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator_a,
1674                             EMPATHY_CONTACT_LIST_STORE_COL_IS_FAVOURITE, &is_favourite_a,
1675                             -1);
1676         gtk_tree_model_get (model, iter_b,
1677                             EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name_b,
1678                             EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact_b,
1679                             EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator_b,
1680                             EMPATHY_CONTACT_LIST_STORE_COL_IS_FAVOURITE, &is_favourite_b,
1681                             -1);
1682
1683         ret_val = compare_separator_and_groups (is_separator_a, is_separator_b,
1684                 name_a, name_b, contact_a, contact_b);
1685
1686         if (ret_val == 0)
1687                 ret_val = g_utf8_collate (name_a, name_b);
1688
1689         if (contact_a) {
1690                 g_object_unref (contact_a);
1691         }
1692
1693         if (contact_b) {
1694                 g_object_unref (contact_b);
1695         }
1696
1697         return ret_val;
1698 }
1699
1700 static gboolean
1701 contact_list_store_find_contact_foreach (GtkTreeModel *model,
1702                                          GtkTreePath  *path,
1703                                          GtkTreeIter  *iter,
1704                                          FindContact  *fc)
1705 {
1706         EmpathyContact *contact;
1707
1708         gtk_tree_model_get (model, iter,
1709                             EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
1710                             -1);
1711
1712         if (contact == fc->contact) {
1713                 fc->found = TRUE;
1714                 fc->iters = g_list_append (fc->iters, gtk_tree_iter_copy (iter));
1715         }
1716
1717         if (contact) {
1718                 g_object_unref (contact);
1719         }
1720
1721         return FALSE;
1722 }
1723
1724 static GList *
1725 contact_list_store_find_contact (EmpathyContactListStore *store,
1726                                  EmpathyContact          *contact)
1727 {
1728         EmpathyContactListStorePriv *priv;
1729         GtkTreeModel              *model;
1730         GList                     *l = NULL;
1731         FindContact                fc;
1732
1733         priv = GET_PRIV (store);
1734
1735         memset (&fc, 0, sizeof (fc));
1736
1737         fc.contact = contact;
1738
1739         model = GTK_TREE_MODEL (store);
1740         gtk_tree_model_foreach (model,
1741                                 (GtkTreeModelForeachFunc) contact_list_store_find_contact_foreach,
1742                                 &fc);
1743
1744         if (fc.found) {
1745                 l = fc.iters;
1746         }
1747
1748         return l;
1749 }
1750
1751 static gboolean
1752 contact_list_store_update_list_mode_foreach (GtkTreeModel           *model,
1753                                              GtkTreePath            *path,
1754                                              GtkTreeIter            *iter,
1755                                              EmpathyContactListStore *store)
1756 {
1757         EmpathyContactListStorePriv *priv;
1758         gboolean                     show_avatar = FALSE;
1759         EmpathyContact              *contact;
1760         GdkPixbuf                   *pixbuf_status;
1761
1762         priv = GET_PRIV (store);
1763
1764         if (priv->show_avatars && !priv->is_compact) {
1765                 show_avatar = TRUE;
1766         }
1767
1768         gtk_tree_model_get (model, iter,
1769                             EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
1770                             -1);
1771
1772         if (contact == NULL){
1773                 return FALSE;
1774         }
1775         /* get icon from hash_table */
1776         pixbuf_status = contact_list_store_get_contact_status_icon (store, contact);
1777
1778         gtk_tree_store_set (GTK_TREE_STORE (store), iter,
1779                             EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, pixbuf_status,
1780                             EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
1781                             EMPATHY_CONTACT_LIST_STORE_COL_STATUS_VISIBLE, !priv->is_compact,
1782                             -1);
1783
1784         return FALSE;
1785 }
1786
1787 static GdkPixbuf *
1788 contact_list_store_get_contact_status_icon_with_icon_name (
1789                                         EmpathyContactListStore *store,
1790                                         EmpathyContact *contact,
1791                                         const gchar *status_icon_name)
1792 {
1793         GdkPixbuf                   *pixbuf_status = NULL;
1794         EmpathyContactListStorePriv *priv;
1795         const gchar                 *protocol_name = NULL;
1796         gchar                       *icon_name = NULL;
1797
1798         priv = GET_PRIV (store);
1799
1800         if (priv->show_protocols) {
1801                 protocol_name = empathy_protocol_name_for_contact (contact);
1802                 icon_name = g_strdup_printf ("%s-%s", status_icon_name, protocol_name);
1803         } else {
1804                 icon_name = g_strdup_printf ("%s", status_icon_name);
1805         }
1806         pixbuf_status = g_hash_table_lookup (priv->status_icons, icon_name);
1807         if (pixbuf_status == NULL) {
1808                 pixbuf_status = empathy_pixbuf_contact_status_icon_with_icon_name (contact,
1809                                     status_icon_name,
1810                                     priv->show_protocols);
1811                 if (pixbuf_status != NULL) {
1812                         g_hash_table_insert (priv->status_icons,
1813                             g_strdup (icon_name),
1814                             pixbuf_status);
1815                 }
1816         }
1817
1818         g_free (icon_name);
1819         return pixbuf_status;
1820 }
1821
1822 GdkPixbuf *
1823 contact_list_store_get_contact_status_icon (EmpathyContactListStore *store,
1824                                             EmpathyContact *contact)
1825 {
1826         GdkPixbuf                   *pixbuf_status = NULL;
1827         const gchar                 *status_icon_name = NULL;
1828
1829         status_icon_name = empathy_icon_name_for_contact (contact);
1830         if (status_icon_name == NULL)
1831                 return NULL;
1832
1833         pixbuf_status = contact_list_store_get_contact_status_icon_with_icon_name (
1834                             store,
1835                             contact,
1836                             status_icon_name);
1837
1838         return pixbuf_status;
1839 }