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