]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-contact-list-store.c
Merge branch 'vino-621998'
[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
608         g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
609
610         priv = GET_PRIV (store);
611
612         if (priv->show_groups == show_groups) {
613                 return;
614         }
615
616         priv->show_groups = show_groups;
617
618         if (priv->setup_idle_id == 0) {
619                 /* Remove all contacts and add them back, not optimized but
620                  * that's the easy way :)
621                  *
622                  * This is only done if there's not a pending setup idle
623                  * callback, otherwise it will race and the contacts will get
624                  * added twice */
625                 GList *contacts, *l;
626
627                 gtk_tree_store_clear (GTK_TREE_STORE (store));
628                 contacts = empathy_contact_list_get_members (priv->list);
629                 for (l = contacts; l; l = l->next) {
630                         contact_list_store_members_changed_cb (priv->list,
631                                                                l->data,
632                                                                NULL, 0, NULL,
633                                                                TRUE,
634                                                                store);
635
636                         g_object_unref (l->data);
637                 }
638                 g_list_free (contacts);
639         }
640
641         g_object_notify (G_OBJECT (store), "show-groups");
642 }
643
644 gboolean
645 empathy_contact_list_store_get_is_compact (EmpathyContactListStore *store)
646 {
647         EmpathyContactListStorePriv *priv;
648
649         g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), TRUE);
650
651         priv = GET_PRIV (store);
652
653         return priv->is_compact;
654 }
655
656 void
657 empathy_contact_list_store_set_is_compact (EmpathyContactListStore *store,
658                                           gboolean                is_compact)
659 {
660         EmpathyContactListStorePriv *priv;
661         GtkTreeModel               *model;
662
663         g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
664
665         priv = GET_PRIV (store);
666
667         priv->is_compact = is_compact;
668
669         model = GTK_TREE_MODEL (store);
670
671         gtk_tree_model_foreach (model,
672                                 (GtkTreeModelForeachFunc)
673                                 contact_list_store_update_list_mode_foreach,
674                                 store);
675
676         g_object_notify (G_OBJECT (store), "is-compact");
677 }
678
679 EmpathyContactListStoreSort
680 empathy_contact_list_store_get_sort_criterium (EmpathyContactListStore *store)
681 {
682         EmpathyContactListStorePriv *priv;
683
684         g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), 0);
685
686         priv = GET_PRIV (store);
687
688         return priv->sort_criterium;
689 }
690
691 void
692 empathy_contact_list_store_set_sort_criterium (EmpathyContactListStore     *store,
693                                               EmpathyContactListStoreSort  sort_criterium)
694 {
695         EmpathyContactListStorePriv *priv;
696
697         g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
698
699         priv = GET_PRIV (store);
700
701         priv->sort_criterium = sort_criterium;
702
703         switch (sort_criterium) {
704         case EMPATHY_CONTACT_LIST_STORE_SORT_STATE:
705                 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
706                                                       EMPATHY_CONTACT_LIST_STORE_COL_STATUS,
707                                                       GTK_SORT_ASCENDING);
708                 break;
709
710         case EMPATHY_CONTACT_LIST_STORE_SORT_NAME:
711                 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
712                                                       EMPATHY_CONTACT_LIST_STORE_COL_NAME,
713                                                       GTK_SORT_ASCENDING);
714                 break;
715         }
716
717         g_object_notify (G_OBJECT (store), "sort-criterium");
718 }
719
720 gboolean
721 empathy_contact_list_store_row_separator_func (GtkTreeModel *model,
722                                               GtkTreeIter  *iter,
723                                               gpointer      data)
724 {
725         gboolean is_separator = FALSE;
726
727         g_return_val_if_fail (GTK_IS_TREE_MODEL (model), FALSE);
728
729         gtk_tree_model_get (model, iter,
730                             EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator,
731                             -1);
732
733         return is_separator;
734 }
735
736 gchar *
737 empathy_contact_list_store_get_parent_group (GtkTreeModel *model,
738                                             GtkTreePath  *path,
739                                             gboolean     *path_is_group,
740                                             gboolean     *is_fake_group)
741 {
742         GtkTreeIter  parent_iter, iter;
743         gchar       *name = NULL;
744         gboolean     is_group;
745         gboolean     fake;
746
747         g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
748
749         if (path_is_group) {
750                 *path_is_group = FALSE;
751         }
752
753         if (!gtk_tree_model_get_iter (model, &iter, path)) {
754                 return NULL;
755         }
756
757         gtk_tree_model_get (model, &iter,
758                             EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
759                             EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
760                             -1);
761
762         if (!is_group) {
763                 g_free (name);
764                 name = NULL;
765
766                 if (!gtk_tree_model_iter_parent (model, &parent_iter, &iter)) {
767                         return NULL;
768                 }
769
770                 iter = parent_iter;
771
772                 gtk_tree_model_get (model, &iter,
773                                     EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
774                                     EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
775                                     EMPATHY_CONTACT_LIST_STORE_COL_IS_FAKE_GROUP, &fake,
776                                     -1);
777                 if (!is_group) {
778                         g_free (name);
779                         return NULL;
780                 }
781         }
782
783         if (path_is_group) {
784                 *path_is_group = TRUE;
785         }
786
787         if (is_fake_group != NULL)
788                 *is_fake_group = fake;
789
790         return name;
791 }
792
793 static void
794 contact_list_store_setup (EmpathyContactListStore *store)
795 {
796         EmpathyContactListStorePriv *priv;
797         GType types[] = {
798                 GDK_TYPE_PIXBUF,      /* Status pixbuf */
799                 GDK_TYPE_PIXBUF,      /* Avatar pixbuf */
800                 G_TYPE_BOOLEAN,       /* Avatar pixbuf visible */
801                 G_TYPE_STRING,        /* Name */
802                 G_TYPE_UINT,          /* Presence type */
803                 G_TYPE_STRING,        /* Status string */
804                 G_TYPE_BOOLEAN,       /* Compact view */
805                 EMPATHY_TYPE_CONTACT, /* Contact type */
806                 G_TYPE_BOOLEAN,       /* Is group */
807                 G_TYPE_BOOLEAN,       /* Is active */
808                 G_TYPE_BOOLEAN,       /* Is online */
809                 G_TYPE_BOOLEAN,       /* Is separator */
810                 G_TYPE_BOOLEAN,       /* Can make audio calls */
811                 G_TYPE_BOOLEAN,       /* Can make video calls */
812                 EMPATHY_TYPE_CONTACT_LIST_FLAGS, /* Flags */
813                 G_TYPE_BOOLEAN,       /* Is a fake group */
814         };
815
816         priv = GET_PRIV (store);
817
818         gtk_tree_store_set_column_types (GTK_TREE_STORE (store),
819                                          EMPATHY_CONTACT_LIST_STORE_COL_COUNT,
820                                          types);
821
822         /* Set up sorting */
823         gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store),
824                                          EMPATHY_CONTACT_LIST_STORE_COL_NAME,
825                                          contact_list_store_name_sort_func,
826                                          store, NULL);
827         gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store),
828                                          EMPATHY_CONTACT_LIST_STORE_COL_STATUS,
829                                          contact_list_store_state_sort_func,
830                                          store, NULL);
831
832         priv->sort_criterium = EMPATHY_CONTACT_LIST_STORE_SORT_NAME;
833         empathy_contact_list_store_set_sort_criterium (store, priv->sort_criterium);
834 }
835
836 static gboolean
837 contact_list_store_inibit_active_cb (EmpathyContactListStore *store)
838 {
839         EmpathyContactListStorePriv *priv;
840
841         priv = GET_PRIV (store);
842
843         priv->show_active = TRUE;
844         priv->inhibit_active = 0;
845
846         return FALSE;
847 }
848
849 static void
850 contact_list_store_add_contact_and_connect (EmpathyContactListStore *store, EmpathyContact *contact)
851 {
852         g_signal_connect (contact, "notify::presence",
853                           G_CALLBACK (contact_list_store_contact_updated_cb),
854                           store);
855         g_signal_connect (contact, "notify::presence-message",
856                           G_CALLBACK (contact_list_store_contact_updated_cb),
857                           store);
858         g_signal_connect (contact, "notify::name",
859                           G_CALLBACK (contact_list_store_contact_updated_cb),
860                           store);
861         g_signal_connect (contact, "notify::avatar",
862                           G_CALLBACK (contact_list_store_contact_updated_cb),
863                           store);
864         g_signal_connect (contact, "notify::capabilities",
865                           G_CALLBACK (contact_list_store_contact_updated_cb),
866                           store);
867
868         contact_list_store_add_contact (store, contact);
869 }
870
871 static void
872 contact_list_store_remove_contact_and_disconnect (EmpathyContactListStore *store, EmpathyContact *contact)
873 {
874         g_signal_handlers_disconnect_by_func (contact,
875                                               G_CALLBACK (contact_list_store_contact_updated_cb),
876                                               store);
877
878         contact_list_store_remove_contact (store, contact);
879 }
880
881 static void
882 contact_list_store_members_changed_cb (EmpathyContactList      *list_iface,
883                                        EmpathyContact          *contact,
884                                        EmpathyContact          *actor,
885                                        guint                    reason,
886                                        gchar                   *message,
887                                        gboolean                 is_member,
888                                        EmpathyContactListStore *store)
889 {
890         EmpathyContactListStorePriv *priv;
891
892         priv = GET_PRIV (store);
893
894         DEBUG ("Contact %s (%d) %s",
895                 empathy_contact_get_id (contact),
896                 empathy_contact_get_handle (contact),
897                 is_member ? "added" : "removed");
898
899         if (is_member) {
900                 contact_list_store_add_contact_and_connect (store, contact);
901         } else {
902                 contact_list_store_remove_contact_and_disconnect (store, contact);
903         }
904 }
905
906 static void
907 contact_list_store_favourites_changed_cb (EmpathyContactList      *list_iface,
908                                           EmpathyContact          *contact,
909                                           gboolean                 is_favourite,
910                                           EmpathyContactListStore *store)
911 {
912         EmpathyContactListStorePriv *priv;
913
914         priv = GET_PRIV (store);
915
916         DEBUG ("Contact %s (%d) is %s a favourite",
917                 empathy_contact_get_id (contact),
918                 empathy_contact_get_handle (contact),
919                 is_favourite ? "now" : "no longer");
920
921         contact_list_store_remove_contact (store, contact);
922         contact_list_store_add_contact (store, contact);
923 }
924
925 static void
926 contact_list_store_member_renamed_cb (EmpathyContactList      *list_iface,
927                                       EmpathyContact          *old_contact,
928                                       EmpathyContact          *new_contact,
929                                       guint                    reason,
930                                       gchar                   *message,
931                                       EmpathyContactListStore *store)
932 {
933         EmpathyContactListStorePriv *priv;
934
935         priv = GET_PRIV (store);
936
937         DEBUG ("Contact %s (%d) renamed to %s (%d)",
938                 empathy_contact_get_id (old_contact),
939                 empathy_contact_get_handle (old_contact),
940                 empathy_contact_get_id (new_contact),
941                 empathy_contact_get_handle (new_contact));
942
943         /* add the new contact */
944         contact_list_store_add_contact_and_connect (store, new_contact);
945
946         /* remove old contact */
947         contact_list_store_remove_contact_and_disconnect (store, old_contact);
948 }
949
950 static void
951 contact_list_store_groups_changed_cb (EmpathyContactList      *list_iface,
952                                       EmpathyContact          *contact,
953                                       gchar                   *group,
954                                       gboolean                 is_member,
955                                       EmpathyContactListStore *store)
956 {
957         EmpathyContactListStorePriv *priv;
958         gboolean                     show_active;
959
960         priv = GET_PRIV (store);
961
962         DEBUG ("Updating groups for contact %s (%d)",
963                 empathy_contact_get_id (contact),
964                 empathy_contact_get_handle (contact));
965
966         /* We do this to make sure the groups are correct, if not, we
967          * would have to check the groups already set up for each
968          * contact and then see what has been updated.
969          */
970         show_active = priv->show_active;
971         priv->show_active = FALSE;
972         contact_list_store_remove_contact (store, contact);
973         contact_list_store_add_contact (store, contact);
974         priv->show_active = show_active;
975 }
976
977 static void
978 add_contact_to_store (GtkTreeStore *store,
979                       GtkTreeIter *iter,
980                       GtkTreeIter *parent,
981                       EmpathyContact *contact,
982                       EmpathyContactListFlags flags)
983 {
984         gtk_tree_store_insert_with_values (store, iter, parent, 0,
985                             EMPATHY_CONTACT_LIST_STORE_COL_NAME, empathy_contact_get_name (contact),
986                             EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, contact,
987                             EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, FALSE,
988                             EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, FALSE,
989                             EMPATHY_CONTACT_LIST_STORE_COL_CAN_AUDIO_CALL,
990                               empathy_contact_get_capabilities (contact) &
991                                 EMPATHY_CAPABILITIES_AUDIO,
992                             EMPATHY_CONTACT_LIST_STORE_COL_CAN_VIDEO_CALL,
993                               empathy_contact_get_capabilities (contact) &
994                                 EMPATHY_CAPABILITIES_VIDEO,
995                             EMPATHY_CONTACT_LIST_STORE_COL_FLAGS, flags,
996                             -1);
997 }
998
999 static void
1000 contact_list_store_add_contact (EmpathyContactListStore *store,
1001                                 EmpathyContact          *contact)
1002 {
1003         EmpathyContactListStorePriv *priv;
1004         GtkTreeIter                 iter;
1005         GList                      *groups = NULL, *l;
1006         TpConnection               *connection;
1007         EmpathyContactListFlags     flags = 0;
1008         char                       *protocol_name;
1009
1010         priv = GET_PRIV (store);
1011
1012         if (EMP_STR_EMPTY (empathy_contact_get_name (contact)) ||
1013             (!priv->show_offline && !empathy_contact_is_online (contact))) {
1014                 return;
1015         }
1016
1017         if (priv->show_groups) {
1018                 groups = empathy_contact_list_get_groups (priv->list, contact);
1019         }
1020
1021         connection = empathy_contact_get_connection (contact);
1022         if (EMPATHY_IS_CONTACT_MANAGER (priv->list)) {
1023                 flags = empathy_contact_manager_get_flags_for_connection (
1024                         EMPATHY_CONTACT_MANAGER (priv->list), connection);
1025         }
1026
1027         tp_connection_parse_object_path (connection, &protocol_name, NULL);
1028
1029         if (!groups) {
1030                 GtkTreeIter iter_group, *parent;
1031
1032                 parent = &iter_group;
1033
1034                 if (!priv->show_groups) {
1035                         parent = NULL;
1036                 } else if (!tp_strdiff (protocol_name, "local-xmpp")) {
1037                         /* these are People Nearby */
1038                         contact_list_store_get_group (store,
1039                                 EMPATHY_CONTACT_LIST_STORE_PEOPLE_NEARBY,
1040                                 &iter_group, NULL, NULL, TRUE);
1041                 } else {
1042                         contact_list_store_get_group (store,
1043                                 EMPATHY_CONTACT_LIST_STORE_UNGROUPED,
1044                                 &iter_group, NULL, NULL, TRUE);
1045                 }
1046
1047                 add_contact_to_store (GTK_TREE_STORE (store), &iter, parent,
1048                                       contact, flags);
1049         }
1050
1051         g_free (protocol_name);
1052
1053         /* Else add to each group. */
1054         for (l = groups; l; l = l->next) {
1055                 GtkTreeIter iter_group;
1056
1057                 contact_list_store_get_group (store, l->data, &iter_group, NULL, NULL, FALSE);
1058
1059                 add_contact_to_store (GTK_TREE_STORE (store), &iter, &iter_group, contact, flags);
1060                 g_free (l->data);
1061         }
1062         g_list_free (groups);
1063
1064         if (priv->show_groups &&
1065             empathy_contact_list_is_favourite (priv->list, contact)) {
1066         /* Add contact to the fake 'Favorites' group */
1067                 GtkTreeIter iter_group;
1068
1069                 contact_list_store_get_group (store, EMPATHY_CONTACT_LIST_STORE_FAVORITE,
1070                         &iter_group, NULL, NULL, TRUE);
1071
1072                 add_contact_to_store (GTK_TREE_STORE (store), &iter, &iter_group, contact, flags);
1073         }
1074
1075         contact_list_store_contact_update (store, contact);
1076 }
1077
1078 static void
1079 contact_list_store_remove_contact (EmpathyContactListStore *store,
1080                                    EmpathyContact          *contact)
1081 {
1082         EmpathyContactListStorePriv *priv;
1083         GtkTreeModel               *model;
1084         GList                      *iters, *l;
1085
1086         priv = GET_PRIV (store);
1087
1088         iters = contact_list_store_find_contact (store, contact);
1089         if (!iters) {
1090                 return;
1091         }
1092
1093         /* Clean up model */
1094         model = GTK_TREE_MODEL (store);
1095
1096         for (l = iters; l; l = l->next) {
1097                 GtkTreeIter parent;
1098
1099                 /* NOTE: it is only <= 2 here because we have
1100                  * separators after the group name, otherwise it
1101                  * should be 1.
1102                  */
1103                 if (gtk_tree_model_iter_parent (model, &parent, l->data) &&
1104                     gtk_tree_model_iter_n_children (model, &parent) <= 2) {
1105                         gtk_tree_store_remove (GTK_TREE_STORE (store), &parent);
1106                 } else {
1107                         gtk_tree_store_remove (GTK_TREE_STORE (store), l->data);
1108                 }
1109         }
1110
1111         g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
1112         g_list_free (iters);
1113 }
1114
1115 static void
1116 contact_list_store_contact_update (EmpathyContactListStore *store,
1117                                    EmpathyContact          *contact)
1118 {
1119         EmpathyContactListStorePriv *priv;
1120         ShowActiveData             *data;
1121         GtkTreeModel               *model;
1122         GList                      *iters, *l;
1123         gboolean                    in_list;
1124         gboolean                    should_be_in_list;
1125         gboolean                    was_online = TRUE;
1126         gboolean                    now_online = FALSE;
1127         gboolean                    set_model = FALSE;
1128         gboolean                    do_remove = FALSE;
1129         gboolean                    do_set_active = FALSE;
1130         gboolean                    do_set_refresh = FALSE;
1131         gboolean                    show_avatar = FALSE;
1132         GdkPixbuf                  *pixbuf_avatar;
1133         GdkPixbuf                  *pixbuf_status;
1134
1135         priv = GET_PRIV (store);
1136
1137         model = GTK_TREE_MODEL (store);
1138
1139         iters = contact_list_store_find_contact (store, contact);
1140         if (!iters) {
1141                 in_list = FALSE;
1142         } else {
1143                 in_list = TRUE;
1144         }
1145
1146         /* Get online state now. */
1147         now_online = empathy_contact_is_online (contact);
1148
1149         if (priv->show_offline || now_online) {
1150                 should_be_in_list = TRUE;
1151         } else {
1152                 should_be_in_list = FALSE;
1153         }
1154
1155         if (!in_list && !should_be_in_list) {
1156                 /* Nothing to do. */
1157                 DEBUG ("Contact:'%s' in list:NO, should be:NO",
1158                         empathy_contact_get_name (contact));
1159
1160                 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
1161                 g_list_free (iters);
1162                 return;
1163         }
1164         else if (in_list && !should_be_in_list) {
1165                 DEBUG ("Contact:'%s' in list:YES, should be:NO",
1166                         empathy_contact_get_name (contact));
1167
1168                 if (priv->show_active) {
1169                         do_remove = TRUE;
1170                         do_set_active = TRUE;
1171                         do_set_refresh = TRUE;
1172
1173                         set_model = TRUE;
1174                         DEBUG ("Remove item (after timeout)");
1175                 } else {
1176                         DEBUG ("Remove item (now)!");
1177                         contact_list_store_remove_contact (store, contact);
1178                 }
1179         }
1180         else if (!in_list && should_be_in_list) {
1181                 DEBUG ("Contact:'%s' in list:NO, should be:YES",
1182                         empathy_contact_get_name (contact));
1183
1184                 contact_list_store_add_contact (store, contact);
1185
1186                 if (priv->show_active) {
1187                         do_set_active = TRUE;
1188
1189                         DEBUG ("Set active (contact added)");
1190                 }
1191         } else {
1192                 DEBUG ("Contact:'%s' in list:YES, should be:YES",
1193                         empathy_contact_get_name (contact));
1194
1195                 /* Get online state before. */
1196                 if (iters && g_list_length (iters) > 0) {
1197                         gtk_tree_model_get (model, iters->data,
1198                                             EMPATHY_CONTACT_LIST_STORE_COL_IS_ONLINE, &was_online,
1199                                             -1);
1200                 }
1201
1202                 /* Is this really an update or an online/offline. */
1203                 if (priv->show_active) {
1204                         if (was_online != now_online) {
1205                                 do_set_active = TRUE;
1206                                 do_set_refresh = TRUE;
1207
1208                                 DEBUG ("Set active (contact updated %s)",
1209                                         was_online ? "online  -> offline" :
1210                                         "offline -> online");
1211                         } else {
1212                                 /* Was TRUE for presence updates. */
1213                                 /* do_set_active = FALSE;  */
1214                                 do_set_refresh = TRUE;
1215
1216                                 DEBUG ("Set active (contact updated)");
1217                         }
1218                 }
1219
1220                 set_model = TRUE;
1221         }
1222
1223         if (priv->show_avatars && !priv->is_compact) {
1224                 show_avatar = TRUE;
1225         }
1226         pixbuf_avatar = empathy_pixbuf_avatar_from_contact_scaled (contact, 32, 32);
1227         pixbuf_status = contact_list_store_get_contact_status_icon (store, contact);
1228         for (l = iters; l && set_model; l = l->next) {
1229                 gtk_tree_store_set (GTK_TREE_STORE (store), l->data,
1230                                     EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, pixbuf_status,
1231                                     EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR, pixbuf_avatar,
1232                                     EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
1233                                     EMPATHY_CONTACT_LIST_STORE_COL_NAME, empathy_contact_get_name (contact),
1234                                     EMPATHY_CONTACT_LIST_STORE_COL_PRESENCE_TYPE,
1235                                       empathy_contact_get_presence (contact),
1236                                     EMPATHY_CONTACT_LIST_STORE_COL_STATUS,
1237                                       empathy_contact_get_presence_message (contact),
1238                                     EMPATHY_CONTACT_LIST_STORE_COL_COMPACT, priv->is_compact,
1239                                     EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, FALSE,
1240                                     EMPATHY_CONTACT_LIST_STORE_COL_IS_ONLINE, now_online,
1241                                     EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, FALSE,
1242                                     EMPATHY_CONTACT_LIST_STORE_COL_CAN_AUDIO_CALL,
1243                                       empathy_contact_get_capabilities (contact) &
1244                                         EMPATHY_CAPABILITIES_AUDIO,
1245                                     EMPATHY_CONTACT_LIST_STORE_COL_CAN_VIDEO_CALL,
1246                                       empathy_contact_get_capabilities (contact) &
1247                                         EMPATHY_CAPABILITIES_VIDEO,
1248                                     -1);
1249         }
1250
1251         if (pixbuf_avatar) {
1252                 g_object_unref (pixbuf_avatar);
1253         }
1254
1255         if (priv->show_active && do_set_active) {
1256                 contact_list_store_contact_set_active (store, contact, do_set_active, do_set_refresh);
1257
1258                 if (do_set_active) {
1259                         data = contact_list_store_contact_active_new (store, contact, do_remove);
1260                         g_timeout_add_seconds (ACTIVE_USER_SHOW_TIME,
1261                                                (GSourceFunc) contact_list_store_contact_active_cb,
1262                                                data);
1263                 }
1264         }
1265
1266         /* FIXME: when someone goes online then offline quickly, the
1267          * first timeout sets the user to be inactive and the second
1268          * timeout removes the user from the contact list, really we
1269          * should remove the first timeout.
1270          */
1271         g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
1272         g_list_free (iters);
1273 }
1274
1275 static void
1276 contact_list_store_contact_updated_cb (EmpathyContact          *contact,
1277                                        GParamSpec              *param,
1278                                        EmpathyContactListStore *store)
1279 {
1280         DEBUG ("Contact:'%s' updated, checking roster is in sync...",
1281                 empathy_contact_get_name (contact));
1282
1283         contact_list_store_contact_update (store, contact);
1284 }
1285
1286 static void
1287 contact_list_store_contact_set_active (EmpathyContactListStore *store,
1288                                        EmpathyContact          *contact,
1289                                        gboolean                active,
1290                                        gboolean                set_changed)
1291 {
1292         EmpathyContactListStorePriv *priv;
1293         GtkTreeModel               *model;
1294         GList                      *iters, *l;
1295
1296         priv = GET_PRIV (store);
1297         model = GTK_TREE_MODEL (store);
1298
1299         iters = contact_list_store_find_contact (store, contact);
1300         for (l = iters; l; l = l->next) {
1301                 GtkTreePath *path;
1302
1303                 gtk_tree_store_set (GTK_TREE_STORE (store), l->data,
1304                                     EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, active,
1305                                     -1);
1306
1307                 DEBUG ("Set item %s", active ? "active" : "inactive");
1308
1309                 if (set_changed) {
1310                         path = gtk_tree_model_get_path (model, l->data);
1311                         gtk_tree_model_row_changed (model, path, l->data);
1312                         gtk_tree_path_free (path);
1313                 }
1314         }
1315
1316         g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
1317         g_list_free (iters);
1318
1319 }
1320
1321 static ShowActiveData *
1322 contact_list_store_contact_active_new (EmpathyContactListStore *store,
1323                                        EmpathyContact          *contact,
1324                                        gboolean                remove_)
1325 {
1326         ShowActiveData *data;
1327
1328         DEBUG ("Contact:'%s' now active, and %s be removed",
1329                 empathy_contact_get_name (contact),
1330                 remove_ ? "WILL" : "WILL NOT");
1331
1332         data = g_slice_new0 (ShowActiveData);
1333
1334         data->store = g_object_ref (store);
1335         data->contact = g_object_ref (contact);
1336         data->remove = remove_;
1337
1338         return data;
1339 }
1340
1341 static void
1342 contact_list_store_contact_active_free (ShowActiveData *data)
1343 {
1344         g_object_unref (data->contact);
1345         g_object_unref (data->store);
1346
1347         g_slice_free (ShowActiveData, data);
1348 }
1349
1350 static gboolean
1351 contact_list_store_contact_active_cb (ShowActiveData *data)
1352 {
1353         EmpathyContactListStorePriv *priv;
1354
1355         priv = GET_PRIV (data->store);
1356
1357         if (data->remove &&
1358             !priv->show_offline &&
1359             !empathy_contact_is_online (data->contact)) {
1360                 DEBUG ("Contact:'%s' active timeout, removing item",
1361                         empathy_contact_get_name (data->contact));
1362                 contact_list_store_remove_contact (data->store, data->contact);
1363         }
1364
1365         DEBUG ("Contact:'%s' no longer active",
1366                 empathy_contact_get_name (data->contact));
1367
1368         contact_list_store_contact_set_active (data->store,
1369                                                data->contact,
1370                                                FALSE,
1371                                                TRUE);
1372
1373         contact_list_store_contact_active_free (data);
1374
1375         return FALSE;
1376 }
1377
1378 static gboolean
1379 contact_list_store_get_group_foreach (GtkTreeModel *model,
1380                                       GtkTreePath  *path,
1381                                       GtkTreeIter  *iter,
1382                                       FindGroup    *fg)
1383 {
1384         gchar    *str;
1385         gboolean  is_group;
1386
1387         /* Groups are only at the top level. */
1388         if (gtk_tree_path_get_depth (path) != 1) {
1389                 return FALSE;
1390         }
1391
1392         gtk_tree_model_get (model, iter,
1393                             EMPATHY_CONTACT_LIST_STORE_COL_NAME, &str,
1394                             EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
1395                             -1);
1396
1397         if (is_group && !tp_strdiff (str, fg->name)) {
1398                 fg->found = TRUE;
1399                 fg->iter = *iter;
1400         }
1401
1402         g_free (str);
1403
1404         return fg->found;
1405 }
1406
1407 static void
1408 contact_list_store_get_group (EmpathyContactListStore *store,
1409                               const gchar            *name,
1410                               GtkTreeIter            *iter_group_to_set,
1411                               GtkTreeIter            *iter_separator_to_set,
1412                               gboolean               *created,
1413                               gboolean               is_fake_group)
1414 {
1415         EmpathyContactListStorePriv *priv;
1416         GtkTreeModel                *model;
1417         GtkTreeIter                  iter_group;
1418         GtkTreeIter                  iter_separator;
1419         FindGroup                    fg;
1420
1421         priv = GET_PRIV (store);
1422
1423         memset (&fg, 0, sizeof (fg));
1424
1425         fg.name = name;
1426
1427         model = GTK_TREE_MODEL (store);
1428         gtk_tree_model_foreach (model,
1429                                 (GtkTreeModelForeachFunc) contact_list_store_get_group_foreach,
1430                                 &fg);
1431
1432         if (!fg.found) {
1433                 if (created) {
1434                         *created = TRUE;
1435                 }
1436
1437                 gtk_tree_store_insert_with_values (GTK_TREE_STORE (store), &iter_group, NULL, 0,
1438                                     EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, NULL,
1439                                     EMPATHY_CONTACT_LIST_STORE_COL_NAME, name,
1440                                     EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, TRUE,
1441                                     EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, FALSE,
1442                                     EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, FALSE,
1443                                     EMPATHY_CONTACT_LIST_STORE_COL_IS_FAKE_GROUP, is_fake_group,
1444                                     -1);
1445
1446                 if (iter_group_to_set) {
1447                         *iter_group_to_set = iter_group;
1448                 }
1449
1450                 gtk_tree_store_insert_with_values (GTK_TREE_STORE (store), &iter_separator, &iter_group, 0,
1451                                     EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, TRUE,
1452                                     -1);
1453
1454                 if (iter_separator_to_set) {
1455                         *iter_separator_to_set = iter_separator;
1456                 }
1457         } else {
1458                 if (created) {
1459                         *created = FALSE;
1460                 }
1461
1462                 if (iter_group_to_set) {
1463                         *iter_group_to_set = fg.iter;
1464                 }
1465
1466                 iter_separator = fg.iter;
1467
1468                 if (gtk_tree_model_iter_next (model, &iter_separator)) {
1469                         gboolean is_separator;
1470
1471                         gtk_tree_model_get (model, &iter_separator,
1472                                             EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator,
1473                                             -1);
1474
1475                         if (is_separator && iter_separator_to_set) {
1476                                 *iter_separator_to_set = iter_separator;
1477                         }
1478                 }
1479         }
1480 }
1481
1482 static gint
1483 get_position (const char **strv,
1484               const char *str)
1485 {
1486         int i;
1487
1488         for (i = 0; strv[i] != NULL; i++) {
1489                 if (!tp_strdiff (strv[i], str))
1490                         return i;
1491         }
1492
1493         return -1;
1494 }
1495
1496 static gint
1497 compare_separator_and_groups (gboolean is_separator_a,
1498                               gboolean is_separator_b,
1499                               const gchar *name_a,
1500                               const gchar *name_b,
1501                               EmpathyContact *contact_a,
1502                               EmpathyContact *contact_b,
1503                               gboolean fake_group_a,
1504                               gboolean fake_group_b)
1505 {
1506         /* these two lists are the sorted list of fake groups to include at the
1507          * top and bottom of the roster */
1508         const char *top_groups[] = {
1509                 EMPATHY_CONTACT_LIST_STORE_FAVORITE,
1510                 NULL
1511         };
1512
1513         const char *bottom_groups[] = {
1514                 EMPATHY_CONTACT_LIST_STORE_UNGROUPED,
1515                 NULL
1516         };
1517
1518         if (is_separator_a || is_separator_b) {
1519                 /* We have at least one separator */
1520                 if (is_separator_a) {
1521                         return -1;
1522                 } else if (is_separator_b) {
1523                         return 1;
1524                 }
1525         }
1526
1527         /* One group and one contact */
1528         if (!contact_a && contact_b) {
1529                 return 1;
1530         } else if (contact_a && !contact_b) {
1531                 return -1;
1532         } else if (!contact_a && !contact_b) {
1533                 gboolean a_in_top, b_in_top, a_in_bottom, b_in_bottom;
1534
1535                 a_in_top = fake_group_a &&
1536                         tp_strv_contains (top_groups, name_a);
1537                 b_in_top = fake_group_b &&
1538                         tp_strv_contains (top_groups, name_b);
1539                 a_in_bottom = fake_group_a &&
1540                         tp_strv_contains (bottom_groups, name_a);
1541                 b_in_bottom = fake_group_b &&
1542                         tp_strv_contains (bottom_groups, name_b);
1543
1544                 if (a_in_top && b_in_top) {
1545                         /* compare positions */
1546                         return CLAMP (get_position (top_groups, name_a) -
1547                                       get_position (top_groups, name_b),
1548                                       -1, 1);
1549                 } else if (a_in_bottom && b_in_bottom) {
1550                         /* compare positions */
1551                         return CLAMP (get_position (bottom_groups, name_a) -
1552                                       get_position (bottom_groups, name_b),
1553                                       -1, 1);
1554                 } else if (a_in_top || b_in_bottom) {
1555                         return -1;
1556                 } else if (b_in_top || a_in_bottom) {
1557                         return 1;
1558                 } else {
1559                         return g_utf8_collate (name_a, name_b);
1560                 }
1561         }
1562
1563         /* Two contacts, ordering depends of the sorting policy */
1564         return 0;
1565 }
1566
1567 static gint
1568 contact_list_store_contact_sort (EmpathyContact *contact_a,
1569                                  EmpathyContact *contact_b)
1570 {
1571         TpAccount *account_a, *account_b;
1572         gint ret_val;
1573
1574         g_return_val_if_fail (contact_a != NULL || contact_b != NULL, 0);
1575
1576         /* alias */
1577         ret_val = g_utf8_collate (empathy_contact_get_name (contact_a),
1578                                   empathy_contact_get_name (contact_b));
1579
1580         if (ret_val != 0)
1581                 goto out;
1582
1583         /* identifier */
1584         ret_val = g_utf8_collate (empathy_contact_get_id (contact_a),
1585                                   empathy_contact_get_id (contact_b));
1586
1587         if (ret_val != 0)
1588                 goto out;
1589
1590         account_a = empathy_contact_get_account (contact_a);
1591         account_b = empathy_contact_get_account (contact_b);
1592
1593         /* protocol */
1594         ret_val = strcmp (tp_account_get_protocol (account_a),
1595                           tp_account_get_protocol (account_a));
1596
1597         if (ret_val != 0)
1598                 goto out;
1599
1600         /* account ID */
1601         ret_val = strcmp (tp_proxy_get_object_path (account_a),
1602                           tp_proxy_get_object_path (account_a));
1603
1604 out:
1605         return ret_val;
1606 }
1607
1608 static gint
1609 contact_list_store_state_sort_func (GtkTreeModel *model,
1610                                     GtkTreeIter  *iter_a,
1611                                     GtkTreeIter  *iter_b,
1612                                     gpointer      user_data)
1613 {
1614         gint            ret_val;
1615         gchar          *name_a, *name_b;
1616         gboolean        is_separator_a, is_separator_b;
1617         EmpathyContact *contact_a, *contact_b;
1618         gboolean       fake_group_a, fake_group_b;
1619
1620         gtk_tree_model_get (model, iter_a,
1621                             EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name_a,
1622                             EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact_a,
1623                             EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator_a,
1624                             EMPATHY_CONTACT_LIST_STORE_COL_IS_FAKE_GROUP, &fake_group_a,
1625                             -1);
1626         gtk_tree_model_get (model, iter_b,
1627                             EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name_b,
1628                             EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact_b,
1629                             EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator_b,
1630                             EMPATHY_CONTACT_LIST_STORE_COL_IS_FAKE_GROUP, &fake_group_b,
1631                             -1);
1632
1633         if (contact_a == NULL || contact_b == NULL) {
1634                 ret_val = compare_separator_and_groups (is_separator_a, is_separator_b,
1635                         name_a, name_b, contact_a, contact_b, fake_group_a, fake_group_b);
1636                 goto free_and_out;
1637         }
1638
1639         /* If we managed to get this far, we can start looking at
1640          * the presences.
1641          */
1642         ret_val = -tp_connection_presence_type_cmp_availability (
1643                 empathy_contact_get_presence (EMPATHY_CONTACT (contact_a)),
1644                 empathy_contact_get_presence (EMPATHY_CONTACT (contact_b)));
1645
1646         if (ret_val == 0) {
1647                 /* Fallback: compare by name et al. */
1648                 ret_val = contact_list_store_contact_sort (contact_a, contact_b);
1649         }
1650
1651 free_and_out:
1652         g_free (name_a);
1653         g_free (name_b);
1654
1655         if (contact_a) {
1656                 g_object_unref (contact_a);
1657         }
1658
1659         if (contact_b) {
1660                 g_object_unref (contact_b);
1661         }
1662
1663         return ret_val;
1664 }
1665
1666 static gint
1667 contact_list_store_name_sort_func (GtkTreeModel *model,
1668                                    GtkTreeIter  *iter_a,
1669                                    GtkTreeIter  *iter_b,
1670                                    gpointer      user_data)
1671 {
1672         gchar         *name_a, *name_b;
1673         EmpathyContact *contact_a, *contact_b;
1674         gboolean       is_separator_a = FALSE, is_separator_b = FALSE;
1675         gint           ret_val;
1676         gboolean       fake_group_a, fake_group_b;
1677
1678         gtk_tree_model_get (model, iter_a,
1679                             EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name_a,
1680                             EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact_a,
1681                             EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator_a,
1682                             EMPATHY_CONTACT_LIST_STORE_COL_IS_FAKE_GROUP, &fake_group_a,
1683                             -1);
1684         gtk_tree_model_get (model, iter_b,
1685                             EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name_b,
1686                             EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact_b,
1687                             EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator_b,
1688                             EMPATHY_CONTACT_LIST_STORE_COL_IS_FAKE_GROUP, &fake_group_b,
1689                             -1);
1690
1691         if (contact_a == NULL || contact_b == NULL)
1692                 ret_val = compare_separator_and_groups (is_separator_a, is_separator_b,
1693                         name_a, name_b, contact_a, contact_b, fake_group_a, fake_group_b);
1694         else
1695                 ret_val = contact_list_store_contact_sort (contact_a, contact_b);
1696
1697         if (contact_a) {
1698                 g_object_unref (contact_a);
1699         }
1700
1701         if (contact_b) {
1702                 g_object_unref (contact_b);
1703         }
1704
1705         return ret_val;
1706 }
1707
1708 static gboolean
1709 contact_list_store_find_contact_foreach (GtkTreeModel *model,
1710                                          GtkTreePath  *path,
1711                                          GtkTreeIter  *iter,
1712                                          FindContact  *fc)
1713 {
1714         EmpathyContact *contact;
1715
1716         gtk_tree_model_get (model, iter,
1717                             EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
1718                             -1);
1719
1720         if (contact == fc->contact) {
1721                 fc->found = TRUE;
1722                 fc->iters = g_list_append (fc->iters, gtk_tree_iter_copy (iter));
1723         }
1724
1725         if (contact) {
1726                 g_object_unref (contact);
1727         }
1728
1729         return FALSE;
1730 }
1731
1732 static GList *
1733 contact_list_store_find_contact (EmpathyContactListStore *store,
1734                                  EmpathyContact          *contact)
1735 {
1736         EmpathyContactListStorePriv *priv;
1737         GtkTreeModel              *model;
1738         GList                     *l = NULL;
1739         FindContact                fc;
1740
1741         priv = GET_PRIV (store);
1742
1743         memset (&fc, 0, sizeof (fc));
1744
1745         fc.contact = contact;
1746
1747         model = GTK_TREE_MODEL (store);
1748         gtk_tree_model_foreach (model,
1749                                 (GtkTreeModelForeachFunc) contact_list_store_find_contact_foreach,
1750                                 &fc);
1751
1752         if (fc.found) {
1753                 l = fc.iters;
1754         }
1755
1756         return l;
1757 }
1758
1759 static gboolean
1760 contact_list_store_update_list_mode_foreach (GtkTreeModel           *model,
1761                                              GtkTreePath            *path,
1762                                              GtkTreeIter            *iter,
1763                                              EmpathyContactListStore *store)
1764 {
1765         EmpathyContactListStorePriv *priv;
1766         gboolean                     show_avatar = FALSE;
1767         EmpathyContact              *contact;
1768         GdkPixbuf                   *pixbuf_status;
1769
1770         priv = GET_PRIV (store);
1771
1772         if (priv->show_avatars && !priv->is_compact) {
1773                 show_avatar = TRUE;
1774         }
1775
1776         gtk_tree_model_get (model, iter,
1777                             EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
1778                             -1);
1779
1780         if (contact == NULL){
1781                 return FALSE;
1782         }
1783         /* get icon from hash_table */
1784         pixbuf_status = contact_list_store_get_contact_status_icon (store, contact);
1785
1786         gtk_tree_store_set (GTK_TREE_STORE (store), iter,
1787                             EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, pixbuf_status,
1788                             EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
1789                             EMPATHY_CONTACT_LIST_STORE_COL_COMPACT, priv->is_compact,
1790                             -1);
1791
1792         return FALSE;
1793 }
1794
1795 static GdkPixbuf *
1796 contact_list_store_get_contact_status_icon_with_icon_name (
1797                                         EmpathyContactListStore *store,
1798                                         EmpathyContact *contact,
1799                                         const gchar *status_icon_name)
1800 {
1801         GdkPixbuf                   *pixbuf_status = NULL;
1802         EmpathyContactListStorePriv *priv;
1803         const gchar                 *protocol_name = NULL;
1804         gchar                       *icon_name = NULL;
1805
1806         priv = GET_PRIV (store);
1807
1808         if (priv->show_protocols) {
1809                 protocol_name = empathy_protocol_name_for_contact (contact);
1810                 icon_name = g_strdup_printf ("%s-%s", status_icon_name, protocol_name);
1811         } else {
1812                 icon_name = g_strdup_printf ("%s", status_icon_name);
1813         }
1814         pixbuf_status = g_hash_table_lookup (priv->status_icons, icon_name);
1815         if (pixbuf_status == NULL) {
1816                 pixbuf_status = empathy_pixbuf_contact_status_icon_with_icon_name (contact,
1817                                     status_icon_name,
1818                                     priv->show_protocols);
1819                 if (pixbuf_status != NULL) {
1820                         g_hash_table_insert (priv->status_icons,
1821                             g_strdup (icon_name),
1822                             pixbuf_status);
1823                 }
1824         }
1825
1826         g_free (icon_name);
1827         return pixbuf_status;
1828 }
1829
1830 GdkPixbuf *
1831 contact_list_store_get_contact_status_icon (EmpathyContactListStore *store,
1832                                             EmpathyContact *contact)
1833 {
1834         GdkPixbuf                   *pixbuf_status = NULL;
1835         const gchar                 *status_icon_name = NULL;
1836
1837         status_icon_name = empathy_icon_name_for_contact (contact);
1838         if (status_icon_name == NULL)
1839                 return NULL;
1840
1841         pixbuf_status = contact_list_store_get_contact_status_icon_with_icon_name (
1842                             store,
1843                             contact,
1844                             status_icon_name);
1845
1846         return pixbuf_status;
1847 }