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