1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2005-2007 Imendio AB
4 * Copyright (C) 2007 Collabora Ltd.
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.
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.
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., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
21 * Authors: Mikael Hallendal <micke@imendio.com>
22 * Martyn Russell <martyn@imendio.com>
23 * Xavier Claessens <xclaesse@gmail.com>
33 #include <libempathy/gossip-debug.h>
35 #include "gossip-contact-list-store.h"
36 #include "gossip-contact-groups.h"
37 #include "gossip-ui-utils.h"
39 #define DEBUG_DOMAIN "ContactListStore"
41 /* Active users are those which have recently changed state
42 * (e.g. online, offline or from normal to a busy state).
45 /* Time user is shown as active */
46 #define ACTIVE_USER_SHOW_TIME 7000
48 /* Time after connecting which we wait before active users are enabled */
49 #define ACTIVE_USER_WAIT_TO_ENABLE_TIME 5000
51 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GOSSIP_TYPE_CONTACT_LIST_STORE, GossipContactListStorePriv))
53 struct _GossipContactListStorePriv {
54 EmpathyContactList *list;
55 gboolean show_offline;
56 gboolean show_avatars;
59 GossipContactListStoreSort sort_criterium;
69 GossipContact *contact;
75 GossipContactListStore *store;
76 GossipContact *contact;
80 static void gossip_contact_list_store_class_init (GossipContactListStoreClass *klass);
81 static void gossip_contact_list_store_init (GossipContactListStore *list);
82 static void contact_list_store_finalize (GObject *object);
83 static void contact_list_store_get_property (GObject *object,
87 static void contact_list_store_set_property (GObject *object,
91 static void contact_list_store_setup (GossipContactListStore *store);
92 static void contact_list_store_contact_added_cb (EmpathyContactList *list_iface,
93 GossipContact *contact,
94 GossipContactListStore *store);
95 static void contact_list_store_add_contact (GossipContactListStore *store,
96 GossipContact *contact);
97 static void contact_list_store_contact_removed_cb (EmpathyContactList *list_iface,
98 GossipContact *contact,
99 GossipContactListStore *store);
100 static void contact_list_store_remove_contact (GossipContactListStore *store,
101 GossipContact *contact);
102 static void contact_list_store_contact_update (GossipContactListStore *store,
103 GossipContact *contact);
104 static void contact_list_store_contact_groups_updated_cb (GossipContact *contact,
106 GossipContactListStore *store);
107 static void contact_list_store_contact_updated_cb (GossipContact *contact,
109 GossipContactListStore *store);
110 static void contact_list_store_contact_set_active (GossipContactListStore *store,
111 GossipContact *contact,
113 gboolean set_changed);
114 static ShowActiveData * contact_list_store_contact_active_new (GossipContactListStore *store,
115 GossipContact *contact,
117 static void contact_list_store_contact_active_free (ShowActiveData *data);
118 static gboolean contact_list_store_contact_active_cb (ShowActiveData *data);
119 static gboolean contact_list_store_get_group_foreach (GtkTreeModel *model,
123 static void contact_list_store_get_group (GossipContactListStore *store,
125 GtkTreeIter *iter_group_to_set,
126 GtkTreeIter *iter_separator_to_set,
128 static gint contact_list_store_state_sort_func (GtkTreeModel *model,
132 static gint contact_list_store_name_sort_func (GtkTreeModel *model,
136 static gboolean contact_list_store_find_contact_foreach (GtkTreeModel *model,
140 static GList * contact_list_store_find_contact (GossipContactListStore *store,
141 GossipContact *contact);
142 static gboolean contact_list_store_update_list_mode_foreach (GtkTreeModel *model,
145 GossipContactListStore *store);
156 gossip_contact_list_store_sort_get_type (void)
158 static GType etype = 0;
161 static const GEnumValue values[] = {
162 { GOSSIP_CONTACT_LIST_STORE_SORT_NAME,
163 "GOSSIP_CONTACT_LIST_STORE_SORT_NAME",
165 { GOSSIP_CONTACT_LIST_STORE_SORT_STATE,
166 "GOSSIP_CONTACT_LIST_STORE_SORT_STATE",
171 etype = g_enum_register_static ("GossipContactListStoreSort", values);
177 G_DEFINE_TYPE (GossipContactListStore, gossip_contact_list_store, GTK_TYPE_TREE_STORE);
180 gossip_contact_list_store_class_init (GossipContactListStoreClass *klass)
182 GObjectClass *object_class = G_OBJECT_CLASS (klass);
184 object_class->finalize = contact_list_store_finalize;
185 object_class->get_property = contact_list_store_get_property;
186 object_class->set_property = contact_list_store_set_property;
188 g_object_class_install_property (object_class,
190 g_param_spec_boolean ("show-offline",
192 "Whether contact list should display "
196 g_object_class_install_property (object_class,
198 g_param_spec_boolean ("show-avatars",
200 "Whether contact list should display "
201 "avatars for contacts",
204 g_object_class_install_property (object_class,
206 g_param_spec_boolean ("is-compact",
208 "Whether the contact list is in compact mode or not",
212 g_object_class_install_property (object_class,
214 g_param_spec_enum ("sort-criterium",
216 "The sort criterium to use for sorting the contact list",
217 GOSSIP_TYPE_CONTACT_LIST_STORE_SORT,
218 GOSSIP_CONTACT_LIST_STORE_SORT_NAME,
221 g_type_class_add_private (object_class, sizeof (GossipContactListStorePriv));
225 gossip_contact_list_store_init (GossipContactListStore *store)
227 GossipContactListStorePriv *priv;
229 priv = GET_PRIV (store);
231 priv->is_compact = FALSE;
232 priv->show_active = TRUE;
233 priv->show_avatars = TRUE;
237 contact_list_store_finalize (GObject *object)
239 GossipContactListStorePriv *priv;
241 priv = GET_PRIV (object);
243 /* FIXME: disconnect all signals on the list and contacts */
246 g_object_unref (priv->list);
249 G_OBJECT_CLASS (gossip_contact_list_store_parent_class)->finalize (object);
253 contact_list_store_get_property (GObject *object,
258 GossipContactListStorePriv *priv;
260 priv = GET_PRIV (object);
263 case PROP_SHOW_OFFLINE:
264 g_value_set_boolean (value, priv->show_offline);
266 case PROP_SHOW_AVATARS:
267 g_value_set_boolean (value, priv->show_avatars);
269 case PROP_IS_COMPACT:
270 g_value_set_boolean (value, priv->is_compact);
272 case PROP_SORT_CRITERIUM:
273 g_value_set_enum (value, priv->sort_criterium);
276 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
282 contact_list_store_set_property (GObject *object,
287 GossipContactListStorePriv *priv;
289 priv = GET_PRIV (object);
292 case PROP_SHOW_OFFLINE:
293 gossip_contact_list_store_set_show_offline (GOSSIP_CONTACT_LIST_STORE (object),
294 g_value_get_boolean (value));
296 case PROP_SHOW_AVATARS:
297 gossip_contact_list_store_set_show_avatars (GOSSIP_CONTACT_LIST_STORE (object),
298 g_value_get_boolean (value));
300 case PROP_IS_COMPACT:
301 gossip_contact_list_store_set_is_compact (GOSSIP_CONTACT_LIST_STORE (object),
302 g_value_get_boolean (value));
304 case PROP_SORT_CRITERIUM:
305 gossip_contact_list_store_set_sort_criterium (GOSSIP_CONTACT_LIST_STORE (object),
306 g_value_get_enum (value));
309 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
314 GossipContactListStore *
315 gossip_contact_list_store_new (EmpathyContactList *list_iface)
317 GossipContactListStore *store;
318 GossipContactListStorePriv *priv;
321 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (list_iface), NULL);
323 store = g_object_new (GOSSIP_TYPE_CONTACT_LIST_STORE, NULL);
324 priv = GET_PRIV (store);
326 contact_list_store_setup (store);
327 priv->list = g_object_ref (list_iface);
329 /* Signal connection. */
330 g_signal_connect (priv->list,
332 G_CALLBACK (contact_list_store_contact_added_cb),
334 g_signal_connect (priv->list,
336 G_CALLBACK (contact_list_store_contact_removed_cb),
339 /* Add contacts already created */
340 contacts = empathy_contact_list_get_contacts (priv->list);
341 for (l = contacts; l; l = l->next) {
342 GossipContact *contact;
346 contact_list_store_contact_added_cb (priv->list, contact, store);
348 g_object_unref (contact);
350 g_list_free (contacts);
356 gossip_contact_list_store_get_list_iface (GossipContactListStore *store)
358 GossipContactListStorePriv *priv;
360 g_return_val_if_fail (GOSSIP_IS_CONTACT_LIST_STORE (store), FALSE);
362 priv = GET_PRIV (store);
368 gossip_contact_list_store_get_show_offline (GossipContactListStore *store)
370 GossipContactListStorePriv *priv;
372 g_return_val_if_fail (GOSSIP_IS_CONTACT_LIST_STORE (store), FALSE);
374 priv = GET_PRIV (store);
376 return priv->show_offline;
380 gossip_contact_list_store_set_show_offline (GossipContactListStore *store,
381 gboolean show_offline)
383 GossipContactListStorePriv *priv;
385 gboolean show_active;
387 g_return_if_fail (GOSSIP_IS_CONTACT_LIST_STORE (store));
389 priv = GET_PRIV (store);
391 priv->show_offline = show_offline;
392 show_active = priv->show_active;
394 /* Disable temporarily. */
395 priv->show_active = FALSE;
397 contacts = empathy_contact_list_get_contacts (priv->list);
398 for (l = contacts; l; l = l->next) {
399 GossipContact *contact;
401 contact = GOSSIP_CONTACT (l->data);
403 contact_list_store_contact_update (store, contact);
405 g_object_unref (contact);
407 g_list_free (contacts);
409 /* Restore to original setting. */
410 priv->show_active = show_active;
414 gossip_contact_list_store_get_show_avatars (GossipContactListStore *store)
416 GossipContactListStorePriv *priv;
418 g_return_val_if_fail (GOSSIP_IS_CONTACT_LIST_STORE (store), TRUE);
420 priv = GET_PRIV (store);
422 return priv->show_avatars;
426 gossip_contact_list_store_set_show_avatars (GossipContactListStore *store,
427 gboolean show_avatars)
429 GossipContactListStorePriv *priv;
432 g_return_if_fail (GOSSIP_IS_CONTACT_LIST_STORE (store));
434 priv = GET_PRIV (store);
436 priv->show_avatars = show_avatars;
438 model = GTK_TREE_MODEL (store);
440 gtk_tree_model_foreach (model,
441 (GtkTreeModelForeachFunc)
442 contact_list_store_update_list_mode_foreach,
447 gossip_contact_list_store_get_is_compact (GossipContactListStore *store)
449 GossipContactListStorePriv *priv;
451 g_return_val_if_fail (GOSSIP_IS_CONTACT_LIST_STORE (store), TRUE);
453 priv = GET_PRIV (store);
455 return priv->is_compact;
459 gossip_contact_list_store_set_is_compact (GossipContactListStore *store,
462 GossipContactListStorePriv *priv;
465 g_return_if_fail (GOSSIP_IS_CONTACT_LIST_STORE (store));
467 priv = GET_PRIV (store);
469 priv->is_compact = is_compact;
471 model = GTK_TREE_MODEL (store);
473 gtk_tree_model_foreach (model,
474 (GtkTreeModelForeachFunc)
475 contact_list_store_update_list_mode_foreach,
479 GossipContactListStoreSort
480 gossip_contact_list_store_get_sort_criterium (GossipContactListStore *store)
482 GossipContactListStorePriv *priv;
484 g_return_val_if_fail (GOSSIP_IS_CONTACT_LIST_STORE (store), 0);
486 priv = GET_PRIV (store);
488 return priv->sort_criterium;
492 gossip_contact_list_store_set_sort_criterium (GossipContactListStore *store,
493 GossipContactListStoreSort sort_criterium)
495 GossipContactListStorePriv *priv;
497 g_return_if_fail (GOSSIP_IS_CONTACT_LIST_STORE (store));
499 priv = GET_PRIV (store);
501 priv->sort_criterium = sort_criterium;
503 switch (sort_criterium) {
504 case GOSSIP_CONTACT_LIST_STORE_SORT_STATE:
505 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
510 case GOSSIP_CONTACT_LIST_STORE_SORT_NAME:
511 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
519 gossip_contact_list_store_row_separator_func (GtkTreeModel *model,
523 gboolean is_separator = FALSE;
525 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), FALSE);
527 gtk_tree_model_get (model, iter,
528 COL_IS_SEPARATOR, &is_separator,
535 gossip_contact_list_store_get_parent_group (GtkTreeModel *model,
537 gboolean *path_is_group)
539 GtkTreeIter parent_iter, iter;
543 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
546 *path_is_group = FALSE;
549 if (!gtk_tree_model_get_iter (model, &iter, path)) {
553 gtk_tree_model_get (model, &iter,
554 COL_IS_GROUP, &is_group,
562 if (!gtk_tree_model_iter_parent (model, &parent_iter, &iter)) {
568 gtk_tree_model_get (model, &iter,
569 COL_IS_GROUP, &is_group,
579 *path_is_group = TRUE;
586 gossip_contact_list_store_search_equal_func (GtkTreeModel *model,
590 gpointer search_data)
592 gchar *name, *name_folded;
596 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), FALSE);
602 gtk_tree_model_get (model, iter, COL_NAME, &name, -1);
608 name_folded = g_utf8_casefold (name, -1);
609 key_folded = g_utf8_casefold (key, -1);
611 if (name_folded && key_folded &&
612 strstr (name_folded, key_folded)) {
619 g_free (name_folded);
626 contact_list_store_setup (GossipContactListStore *store)
628 GossipContactListStorePriv *priv;
629 GType types[] = {G_TYPE_STRING, /* Status icon-name */
630 GDK_TYPE_PIXBUF, /* Avatar pixbuf */
631 G_TYPE_BOOLEAN, /* Avatar pixbuf visible */
632 G_TYPE_STRING, /* Name */
633 G_TYPE_STRING, /* Status string */
634 G_TYPE_BOOLEAN, /* Show status */
635 GOSSIP_TYPE_CONTACT, /* Contact type */
636 G_TYPE_BOOLEAN, /* Is group */
637 G_TYPE_BOOLEAN, /* Is active */
638 G_TYPE_BOOLEAN, /* Is online */
639 G_TYPE_BOOLEAN}; /* Is separator */
641 priv = GET_PRIV (store);
643 gtk_tree_store_set_column_types (GTK_TREE_STORE (store), COL_COUNT, types);
646 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store),
648 contact_list_store_name_sort_func,
650 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store),
652 contact_list_store_state_sort_func,
655 gossip_contact_list_store_set_sort_criterium (store, priv->sort_criterium);
659 contact_list_store_contact_added_cb (EmpathyContactList *list_iface,
660 GossipContact *contact,
661 GossipContactListStore *store)
663 GossipContactListStorePriv *priv;
665 priv = GET_PRIV (store);
667 gossip_debug (DEBUG_DOMAIN,
668 "Contact:'%s' added",
669 gossip_contact_get_name (contact));
671 g_signal_connect (contact, "notify::groups",
672 G_CALLBACK (contact_list_store_contact_groups_updated_cb),
674 g_signal_connect (contact, "notify::presence",
675 G_CALLBACK (contact_list_store_contact_updated_cb),
677 g_signal_connect (contact, "notify::name",
678 G_CALLBACK (contact_list_store_contact_updated_cb),
680 g_signal_connect (contact, "notify::avatar",
681 G_CALLBACK (contact_list_store_contact_updated_cb),
683 g_signal_connect (contact, "notify::type",
684 G_CALLBACK (contact_list_store_contact_updated_cb),
687 contact_list_store_add_contact (store, contact);
691 contact_list_store_add_contact (GossipContactListStore *store,
692 GossipContact *contact)
694 GossipContactListStorePriv *priv;
698 priv = GET_PRIV (store);
700 if (!priv->show_offline && !gossip_contact_is_online (contact)) {
704 /* If no groups just add it at the top level. */
705 groups = gossip_contact_get_groups (contact);
707 gtk_tree_store_append (GTK_TREE_STORE (store), &iter, NULL);
708 gtk_tree_store_set (GTK_TREE_STORE (store), &iter,
709 COL_CONTACT, contact,
713 /* Else add to each group. */
714 for (l = groups; l; l = l->next) {
715 GtkTreeIter iter_group;
723 contact_list_store_get_group (store, name, &iter_group, NULL, NULL);
725 gtk_tree_store_insert_after (GTK_TREE_STORE (store), &iter,
727 gtk_tree_store_set (GTK_TREE_STORE (store), &iter,
728 COL_CONTACT, contact,
732 contact_list_store_contact_update (store, contact);
736 contact_list_store_contact_removed_cb (EmpathyContactList *list_iface,
737 GossipContact *contact,
738 GossipContactListStore *store)
740 gossip_debug (DEBUG_DOMAIN, "Contact:'%s' removed",
741 gossip_contact_get_name (contact));
743 /* Disconnect signals */
744 g_signal_handlers_disconnect_by_func (contact,
745 G_CALLBACK (contact_list_store_contact_groups_updated_cb),
747 g_signal_handlers_disconnect_by_func (contact,
748 G_CALLBACK (contact_list_store_contact_updated_cb),
751 contact_list_store_remove_contact (store, contact);
755 contact_list_store_remove_contact (GossipContactListStore *store,
756 GossipContact *contact)
758 GossipContactListStorePriv *priv;
762 priv = GET_PRIV (store);
764 iters = contact_list_store_find_contact (store, contact);
770 model = GTK_TREE_MODEL (store);
772 for (l = iters; l; l = l->next) {
775 /* NOTE: it is only <= 2 here because we have
776 * separators after the group name, otherwise it
779 if (gtk_tree_model_iter_parent (model, &parent, l->data) &&
780 gtk_tree_model_iter_n_children (model, &parent) <= 2) {
781 gtk_tree_store_remove (GTK_TREE_STORE (store), &parent);
783 gtk_tree_store_remove (GTK_TREE_STORE (store), l->data);
787 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
792 contact_list_store_contact_update (GossipContactListStore *store,
793 GossipContact *contact)
795 GossipContactListStorePriv *priv;
796 ShowActiveData *data;
800 gboolean should_be_in_list;
801 gboolean was_online = TRUE;
802 gboolean now_online = FALSE;
803 gboolean set_model = FALSE;
804 gboolean do_remove = FALSE;
805 gboolean do_set_active = FALSE;
806 gboolean do_set_refresh = FALSE;
807 GdkPixbuf *pixbuf_avatar;
809 priv = GET_PRIV (store);
811 model = GTK_TREE_MODEL (store);
813 iters = contact_list_store_find_contact (store, contact);
820 /* Get online state now. */
821 now_online = gossip_contact_is_online (contact);
823 if (priv->show_offline || now_online) {
824 should_be_in_list = TRUE;
826 should_be_in_list = FALSE;
829 if (!in_list && !should_be_in_list) {
831 gossip_debug (DEBUG_DOMAIN,
832 "Contact:'%s' in list:NO, should be:NO",
833 gossip_contact_get_name (contact));
835 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
839 else if (in_list && !should_be_in_list) {
840 gossip_debug (DEBUG_DOMAIN,
841 "Contact:'%s' in list:YES, should be:NO",
842 gossip_contact_get_name (contact));
844 if (priv->show_active) {
846 do_set_active = TRUE;
847 do_set_refresh = TRUE;
850 gossip_debug (DEBUG_DOMAIN, "Remove item (after timeout)");
852 gossip_debug (DEBUG_DOMAIN, "Remove item (now)!");
853 contact_list_store_remove_contact (store, contact);
856 else if (!in_list && should_be_in_list) {
857 gossip_debug (DEBUG_DOMAIN,
858 "Contact:'%s' in list:NO, should be:YES",
859 gossip_contact_get_name (contact));
861 contact_list_store_add_contact (store, contact);
863 if (priv->show_active) {
864 do_set_active = TRUE;
866 gossip_debug (DEBUG_DOMAIN, "Set active (contact added)");
869 gossip_debug (DEBUG_DOMAIN,
870 "Contact:'%s' in list:YES, should be:YES",
871 gossip_contact_get_name (contact));
873 /* Get online state before. */
874 if (iters && g_list_length (iters) > 0) {
875 gtk_tree_model_get (model, iters->data,
876 COL_IS_ONLINE, &was_online,
880 /* Is this really an update or an online/offline. */
881 if (priv->show_active) {
882 if (was_online != now_online) {
883 do_set_active = TRUE;
884 do_set_refresh = TRUE;
886 gossip_debug (DEBUG_DOMAIN, "Set active (contact updated %s)",
887 was_online ? "online -> offline" :
888 "offline -> online");
890 /* Was TRUE for presence updates. */
891 /* do_set_active = FALSE; */
892 do_set_refresh = TRUE;
894 gossip_debug (DEBUG_DOMAIN, "Set active (contact updated)");
901 pixbuf_avatar = gossip_pixbuf_avatar_from_contact_scaled (contact, 32, 32);
902 for (l = iters; l && set_model; l = l->next) {
903 gtk_tree_store_set (GTK_TREE_STORE (store), l->data,
904 COL_ICON_STATUS, gossip_icon_name_for_contact (contact),
905 COL_PIXBUF_AVATAR, pixbuf_avatar,
906 COL_PIXBUF_AVATAR_VISIBLE, priv->show_avatars,
907 COL_NAME, gossip_contact_get_name (contact),
908 COL_STATUS, gossip_contact_get_status (contact),
909 COL_STATUS_VISIBLE, !priv->is_compact,
911 COL_IS_ONLINE, now_online,
912 COL_IS_SEPARATOR, FALSE,
917 g_object_unref (pixbuf_avatar);
920 if (priv->show_active && do_set_active) {
921 contact_list_store_contact_set_active (store, contact, do_set_active, do_set_refresh);
924 data = contact_list_store_contact_active_new (store, contact, do_remove);
925 g_timeout_add (ACTIVE_USER_SHOW_TIME,
926 (GSourceFunc) contact_list_store_contact_active_cb,
931 /* FIXME: when someone goes online then offline quickly, the
932 * first timeout sets the user to be inactive and the second
933 * timeout removes the user from the contact list, really we
934 * should remove the first timeout.
936 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
941 contact_list_store_contact_groups_updated_cb (GossipContact *contact,
943 GossipContactListStore *store)
945 gossip_debug (DEBUG_DOMAIN, "Contact:'%s' groups updated",
946 gossip_contact_get_name (contact));
948 /* We do this to make sure the groups are correct, if not, we
949 * would have to check the groups already set up for each
950 * contact and then see what has been updated.
952 contact_list_store_remove_contact (store, contact);
953 contact_list_store_add_contact (store, contact);
957 contact_list_store_contact_updated_cb (GossipContact *contact,
959 GossipContactListStore *store)
961 gossip_debug (DEBUG_DOMAIN,
962 "Contact:'%s' updated, checking roster is in sync...",
963 gossip_contact_get_name (contact));
965 contact_list_store_contact_update (store, contact);
969 contact_list_store_contact_set_active (GossipContactListStore *store,
970 GossipContact *contact,
972 gboolean set_changed)
974 GossipContactListStorePriv *priv;
978 priv = GET_PRIV (store);
979 model = GTK_TREE_MODEL (store);
981 iters = contact_list_store_find_contact (store, contact);
982 for (l = iters; l; l = l->next) {
985 gtk_tree_store_set (GTK_TREE_STORE (store), l->data,
986 COL_IS_ACTIVE, active,
989 gossip_debug (DEBUG_DOMAIN, "Set item %s", active ? "active" : "inactive");
992 path = gtk_tree_model_get_path (model, l->data);
993 gtk_tree_model_row_changed (model, path, l->data);
994 gtk_tree_path_free (path);
998 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
1003 static ShowActiveData *
1004 contact_list_store_contact_active_new (GossipContactListStore *store,
1005 GossipContact *contact,
1008 ShowActiveData *data;
1010 gossip_debug (DEBUG_DOMAIN,
1011 "Contact:'%s' now active, and %s be removed",
1012 gossip_contact_get_name (contact),
1013 remove ? "WILL" : "WILL NOT");
1015 data = g_slice_new0 (ShowActiveData);
1017 data->store = g_object_ref (store);
1018 data->contact = g_object_ref (contact);
1019 data->remove = remove;
1025 contact_list_store_contact_active_free (ShowActiveData *data)
1027 g_object_unref (data->contact);
1028 g_object_unref (data->store);
1030 g_slice_free (ShowActiveData, data);
1034 contact_list_store_contact_active_cb (ShowActiveData *data)
1036 GossipContactListStorePriv *priv;
1038 priv = GET_PRIV (data->store);
1041 !priv->show_offline &&
1042 !gossip_contact_is_online (data->contact)) {
1043 gossip_debug (DEBUG_DOMAIN,
1044 "Contact:'%s' active timeout, removing item",
1045 gossip_contact_get_name (data->contact));
1046 contact_list_store_remove_contact (data->store, data->contact);
1049 gossip_debug (DEBUG_DOMAIN,
1050 "Contact:'%s' no longer active",
1051 gossip_contact_get_name (data->contact));
1053 contact_list_store_contact_set_active (data->store,
1058 contact_list_store_contact_active_free (data);
1064 contact_list_store_get_group_foreach (GtkTreeModel *model,
1072 /* Groups are only at the top level. */
1073 if (gtk_tree_path_get_depth (path) != 1) {
1077 gtk_tree_model_get (model, iter,
1079 COL_IS_GROUP, &is_group,
1082 if (is_group && strcmp (str, fg->name) == 0) {
1093 contact_list_store_get_group (GossipContactListStore *store,
1095 GtkTreeIter *iter_group_to_set,
1096 GtkTreeIter *iter_separator_to_set,
1099 GossipContactListStorePriv *priv;
1100 GtkTreeModel *model;
1101 GtkTreeIter iter_group;
1102 GtkTreeIter iter_separator;
1105 priv = GET_PRIV (store);
1107 memset (&fg, 0, sizeof (fg));
1111 model = GTK_TREE_MODEL (store);
1112 gtk_tree_model_foreach (model,
1113 (GtkTreeModelForeachFunc) contact_list_store_get_group_foreach,
1121 gtk_tree_store_append (GTK_TREE_STORE (store), &iter_group, NULL);
1122 gtk_tree_store_set (GTK_TREE_STORE (store), &iter_group,
1123 COL_ICON_STATUS, NULL,
1126 COL_IS_ACTIVE, FALSE,
1127 COL_IS_SEPARATOR, FALSE,
1130 if (iter_group_to_set) {
1131 *iter_group_to_set = iter_group;
1134 gtk_tree_store_append (GTK_TREE_STORE (store),
1137 gtk_tree_store_set (GTK_TREE_STORE (store), &iter_separator,
1138 COL_IS_SEPARATOR, TRUE,
1141 if (iter_separator_to_set) {
1142 *iter_separator_to_set = iter_separator;
1149 if (iter_group_to_set) {
1150 *iter_group_to_set = fg.iter;
1153 iter_separator = fg.iter;
1155 if (gtk_tree_model_iter_next (model, &iter_separator)) {
1156 gboolean is_separator;
1158 gtk_tree_model_get (model, &iter_separator,
1159 COL_IS_SEPARATOR, &is_separator,
1162 if (is_separator && iter_separator_to_set) {
1163 *iter_separator_to_set = iter_separator;
1170 contact_list_store_state_sort_func (GtkTreeModel *model,
1171 GtkTreeIter *iter_a,
1172 GtkTreeIter *iter_b,
1176 gchar *name_a, *name_b;
1177 gboolean is_separator_a, is_separator_b;
1178 GossipContact *contact_a, *contact_b;
1179 GossipPresence *presence_a, *presence_b;
1180 McPresence state_a, state_b;
1182 gtk_tree_model_get (model, iter_a,
1184 COL_CONTACT, &contact_a,
1185 COL_IS_SEPARATOR, &is_separator_a,
1187 gtk_tree_model_get (model, iter_b,
1189 COL_CONTACT, &contact_b,
1190 COL_IS_SEPARATOR, &is_separator_b,
1193 /* Separator or group? */
1194 if (is_separator_a || is_separator_b) {
1195 if (is_separator_a) {
1197 } else if (is_separator_b) {
1200 } else if (!contact_a && contact_b) {
1202 } else if (contact_a && !contact_b) {
1204 } else if (!contact_a && !contact_b) {
1206 ret_val = g_utf8_collate (name_a, name_b);
1213 /* If we managed to get this far, we can start looking at
1216 presence_a = gossip_contact_get_presence (GOSSIP_CONTACT (contact_a));
1217 presence_b = gossip_contact_get_presence (GOSSIP_CONTACT (contact_b));
1219 if (!presence_a && presence_b) {
1221 } else if (presence_a && !presence_b) {
1223 } else if (!presence_a && !presence_b) {
1224 /* Both offline, sort by name */
1225 ret_val = g_utf8_collate (name_a, name_b);
1227 state_a = gossip_presence_get_state (presence_a);
1228 state_b = gossip_presence_get_state (presence_b);
1230 if (state_a < state_b) {
1232 } else if (state_a > state_b) {
1235 /* Fallback: compare by name */
1236 ret_val = g_utf8_collate (name_a, name_b);
1245 g_object_unref (contact_a);
1249 g_object_unref (contact_b);
1256 contact_list_store_name_sort_func (GtkTreeModel *model,
1257 GtkTreeIter *iter_a,
1258 GtkTreeIter *iter_b,
1261 gchar *name_a, *name_b;
1262 GossipContact *contact_a, *contact_b;
1263 gboolean is_separator_a, is_separator_b;
1266 gtk_tree_model_get (model, iter_a,
1268 COL_CONTACT, &contact_a,
1269 COL_IS_SEPARATOR, &is_separator_a,
1271 gtk_tree_model_get (model, iter_b,
1273 COL_CONTACT, &contact_b,
1274 COL_IS_SEPARATOR, &is_separator_b,
1277 /* If contact is NULL it means it's a group. */
1279 if (is_separator_a || is_separator_b) {
1280 if (is_separator_a) {
1282 } else if (is_separator_b) {
1285 } else if (!contact_a && contact_b) {
1287 } else if (contact_a && !contact_b) {
1290 ret_val = g_utf8_collate (name_a, name_b);
1297 g_object_unref (contact_a);
1301 g_object_unref (contact_b);
1308 contact_list_store_find_contact_foreach (GtkTreeModel *model,
1313 GossipContact *contact;
1315 gtk_tree_model_get (model, iter,
1316 COL_CONTACT, &contact,
1323 if (gossip_contact_equal (contact, fc->contact)) {
1325 fc->iters = g_list_append (fc->iters, gtk_tree_iter_copy (iter));
1327 g_object_unref (contact);
1333 contact_list_store_find_contact (GossipContactListStore *store,
1334 GossipContact *contact)
1336 GossipContactListStorePriv *priv;
1337 GtkTreeModel *model;
1341 priv = GET_PRIV (store);
1343 memset (&fc, 0, sizeof (fc));
1345 fc.contact = contact;
1347 model = GTK_TREE_MODEL (store);
1348 gtk_tree_model_foreach (model,
1349 (GtkTreeModelForeachFunc) contact_list_store_find_contact_foreach,
1360 contact_list_store_update_list_mode_foreach (GtkTreeModel *model,
1363 GossipContactListStore *store)
1365 GossipContactListStorePriv *priv;
1366 gboolean show_avatar = FALSE;
1368 priv = GET_PRIV (store);
1370 if (priv->show_avatars && !priv->is_compact) {
1374 gtk_tree_store_set (GTK_TREE_STORE (store), iter,
1375 COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
1376 COL_STATUS_VISIBLE, !priv->is_compact,