1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2005-2007 Imendio AB
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Authors: Mikael Hallendal <micke@imendio.com>
21 * Martyn Russell <martyn@imendio.com>
28 #include <glib/gi18n.h>
30 #include <glade/glade.h>
32 #include <libtelepathy/tp-helpers.h>
34 #include <libmissioncontrol/mc-account.h>
35 #include <libmissioncontrol/mission-control.h>
37 #include <libempathy/empathy-contact-manager.h>
38 #include <libempathy/gossip-debug.h>
40 #include "empathy-images.h"
41 #include "gossip-contact-list.h"
42 #include "gossip-contact-groups.h"
43 #include "gossip-cell-renderer-expander.h"
44 #include "gossip-cell-renderer-text.h"
45 #include "gossip-ui-utils.h"
46 //#include "gossip-chat-invite.h"
47 //#include "gossip-contact-info-dialog.h"
48 //#include "gossip-edit-contact-dialog.h"
49 //#include "gossip-ft-window.h"
50 //#include "gossip-log-window.h"
52 #define DEBUG_DOMAIN "ContactListUI"
54 /* Flashing delay for icons (milliseconds). */
55 #define FLASH_TIMEOUT 500
57 /* Active users are those which have recently changed state
58 * (e.g. online, offline or from normal to a busy state).
61 /* Time user is shown as active */
62 #define ACTIVE_USER_SHOW_TIME 7000
64 /* Time after connecting which we wait before active users are enabled */
65 #define ACTIVE_USER_WAIT_TO_ENABLE_TIME 5000
67 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GOSSIP_TYPE_CONTACT_LIST, GossipContactListPriv))
69 struct _GossipContactListPriv {
70 EmpathyContactManager *manager;
75 GtkTreeRowReference *drag_row;
81 gboolean show_offline;
82 gboolean show_avatars;
94 GossipContact *contact;
100 GossipContactList *list;
106 GossipContactList *list;
107 GossipContact *contact;
111 static void gossip_contact_list_class_init (GossipContactListClass *klass);
112 static void gossip_contact_list_init (GossipContactList *list);
113 static void contact_list_finalize (GObject *object);
114 static void contact_list_get_property (GObject *object,
118 static void contact_list_set_property (GObject *object,
122 static gboolean contact_list_row_separator_func (GtkTreeModel *model,
125 static void contact_list_contact_update (GossipContactList *list,
126 GossipContact *contact);
127 static void contact_list_contact_added_cb (EmpathyContactManager *manager,
128 GossipContact *contact,
129 GossipContactList *list);
130 static void contact_list_contact_updated_cb (GossipContact *contact,
132 GossipContactList *list);
133 static void contact_list_contact_groups_updated_cb (GossipContact *contact,
135 GossipContactList *list);
136 static void contact_list_contact_removed_cb (EmpathyContactManager *manager,
137 GossipContact *contact,
138 GossipContactList *list);
139 static void contact_list_contact_set_active (GossipContactList *list,
140 GossipContact *contact,
142 gboolean set_changed);
143 static ShowActiveData *
144 contact_list_contact_active_new (GossipContactList *list,
145 GossipContact *contact,
147 static void contact_list_contact_active_free (ShowActiveData *data);
148 static gboolean contact_list_contact_active_cb (ShowActiveData *data);
149 static gchar * contact_list_get_parent_group (GtkTreeModel *model,
151 gboolean *path_is_group);
152 static void contact_list_get_group (GossipContactList *list,
154 GtkTreeIter *iter_group_to_set,
155 GtkTreeIter *iter_separator_to_set,
157 static gboolean contact_list_get_group_foreach (GtkTreeModel *model,
161 static void contact_list_add_contact (GossipContactList *list,
162 GossipContact *contact);
163 static void contact_list_remove_contact (GossipContactList *list,
164 GossipContact *contact);
165 static void contact_list_create_model (GossipContactList *list);
166 static gboolean contact_list_search_equal_func (GtkTreeModel *model,
170 gpointer search_data);
171 static void contact_list_setup_view (GossipContactList *list);
172 static void contact_list_drag_data_received (GtkWidget *widget,
173 GdkDragContext *context,
176 GtkSelectionData *selection,
180 static gboolean contact_list_drag_motion (GtkWidget *widget,
181 GdkDragContext *context,
186 static gboolean contact_list_drag_motion_cb (DragMotionData *data);
187 static void contact_list_drag_begin (GtkWidget *widget,
188 GdkDragContext *context,
190 static void contact_list_drag_data_get (GtkWidget *widget,
191 GdkDragContext *contact,
192 GtkSelectionData *selection,
196 static void contact_list_drag_end (GtkWidget *widget,
197 GdkDragContext *context,
199 static void contact_list_cell_set_background (GossipContactList *list,
200 GtkCellRenderer *cell,
203 static void contact_list_pixbuf_cell_data_func (GtkTreeViewColumn *tree_column,
204 GtkCellRenderer *cell,
207 GossipContactList *list);
208 static void contact_list_avatar_cell_data_func (GtkTreeViewColumn *tree_column,
209 GtkCellRenderer *cell,
212 GossipContactList *list);
213 static void contact_list_text_cell_data_func (GtkTreeViewColumn *tree_column,
214 GtkCellRenderer *cell,
217 GossipContactList *list);
218 static void contact_list_expander_cell_data_func (GtkTreeViewColumn *column,
219 GtkCellRenderer *cell,
222 GossipContactList *list);
223 static GtkWidget *contact_list_get_contact_menu (GossipContactList *list,
224 gboolean can_send_file,
225 gboolean can_show_log);
226 static gboolean contact_list_button_press_event_cb (GossipContactList *list,
227 GdkEventButton *event,
229 static void contact_list_row_activated_cb (GossipContactList *list,
231 GtkTreeViewColumn *col,
233 static void contact_list_row_expand_or_collapse_cb (GossipContactList *list,
237 static gint contact_list_sort_func (GtkTreeModel *model,
241 static gboolean contact_list_filter_func (GtkTreeModel *model,
243 GossipContactList *list);
244 static GList * contact_list_find_contact (GossipContactList *list,
245 GossipContact *contact);
246 static gboolean contact_list_find_contact_foreach (GtkTreeModel *model,
250 static void contact_list_action_cb (GtkAction *action,
251 GossipContactList *list);
252 static void contact_list_action_activated (GossipContactList *list,
253 GossipContact *contact);
254 static gboolean contact_list_update_list_mode_foreach (GtkTreeModel *model,
257 GossipContactList *list);
262 COL_PIXBUF_AVATAR_VISIBLE,
282 static const GtkActionEntry entries[] = {
283 { "ContactMenu", NULL,
284 N_("_Contact"), NULL, NULL,
288 N_("_Group"),NULL, NULL,
291 { "Chat", EMPATHY_IMAGE_MESSAGE,
292 N_("_Chat"), NULL, N_("Chat with contact"),
293 G_CALLBACK (contact_list_action_cb)
295 { "Information", EMPATHY_IMAGE_CONTACT_INFORMATION,
296 N_("Infor_mation"), "<control>I", N_("View contact information"),
297 G_CALLBACK (contact_list_action_cb)
300 N_("Re_name"), NULL, N_("Rename"),
301 G_CALLBACK (contact_list_action_cb)
303 { "Edit", GTK_STOCK_EDIT,
304 N_("_Edit"), NULL, N_("Edit the groups and name for this contact"),
305 G_CALLBACK (contact_list_action_cb)
307 { "Remove", GTK_STOCK_REMOVE,
308 N_("_Remove"), NULL, N_("Remove contact"),
309 G_CALLBACK (contact_list_action_cb)
311 { "Invite", EMPATHY_IMAGE_GROUP_MESSAGE,
312 N_("_Invite to Chat Room"), NULL, N_("Invite to a currently open chat room"),
313 G_CALLBACK (contact_list_action_cb)
316 N_("_Send File..."), NULL, N_("Send a file"),
317 G_CALLBACK (contact_list_action_cb)
319 { "Log", GTK_STOCK_JUSTIFY_LEFT,
320 N_("_View Previous Conversations"), NULL, N_("View previous conversations with this contact"),
321 G_CALLBACK (contact_list_action_cb)
325 static guint n_entries = G_N_ELEMENTS (entries);
327 static const gchar *ui_info =
329 " <popup name='Contact'>"
330 " <menuitem action='Chat'/>"
331 " <menuitem action='Log'/>"
332 " <menuitem action='SendFile'/>"
334 " <menuitem action='Invite'/>"
336 " <menuitem action='Edit'/>"
337 " <menuitem action='Remove'/>"
339 " <menuitem action='Information'/>"
341 " <popup name='Group'>"
342 " <menuitem action='Rename'/>"
347 DND_DRAG_TYPE_CONTACT_ID,
349 DND_DRAG_TYPE_STRING,
352 static const GtkTargetEntry drag_types_dest[] = {
353 { "text/contact-id", 0, DND_DRAG_TYPE_CONTACT_ID },
354 { "text/uri-list", 0, DND_DRAG_TYPE_URL },
355 { "text/plain", 0, DND_DRAG_TYPE_STRING },
356 { "STRING", 0, DND_DRAG_TYPE_STRING },
359 static const GtkTargetEntry drag_types_source[] = {
360 { "text/contact-id", 0, DND_DRAG_TYPE_CONTACT_ID },
363 static GdkAtom drag_atoms_dest[G_N_ELEMENTS (drag_types_dest)];
364 static GdkAtom drag_atoms_source[G_N_ELEMENTS (drag_types_source)];
366 G_DEFINE_TYPE (GossipContactList, gossip_contact_list, GTK_TYPE_TREE_VIEW);
369 gossip_contact_list_class_init (GossipContactListClass *klass)
371 GObjectClass *object_class = G_OBJECT_CLASS (klass);
373 object_class->finalize = contact_list_finalize;
374 object_class->get_property = contact_list_get_property;
375 object_class->set_property = contact_list_set_property;
377 g_object_class_install_property (object_class,
379 g_param_spec_boolean ("show-offline",
381 "Whether contact list should display "
385 g_object_class_install_property (object_class,
387 g_param_spec_boolean ("show-avatars",
389 "Whether contact list should display "
390 "avatars for contacts",
393 g_object_class_install_property (object_class,
395 g_param_spec_boolean ("is-compact",
397 "Whether the contact list is in compact mode or not",
401 g_object_class_install_property (object_class,
403 g_param_spec_string ("filter",
405 "The text to use to filter the contact list",
409 g_type_class_add_private (object_class, sizeof (GossipContactListPriv));
413 gossip_contact_list_init (GossipContactList *list)
415 GossipContactListPriv *priv;
416 GtkActionGroup *action_group;
418 GError *error = NULL;
420 priv = GET_PRIV (list);
422 priv->manager = empathy_contact_manager_new ();
423 priv->is_compact = FALSE;
424 priv->show_active = TRUE;
425 priv->show_avatars = TRUE;
427 contact_list_create_model (list);
428 contact_list_setup_view (list);
429 empathy_contact_manager_setup (priv->manager);
431 /* Get saved group states. */
432 gossip_contact_groups_get_all ();
434 /* Set up UI Manager */
435 priv->ui = gtk_ui_manager_new ();
437 action_group = gtk_action_group_new ("Actions");
438 gtk_action_group_set_translation_domain (action_group, GETTEXT_PACKAGE);
439 gtk_action_group_add_actions (action_group, entries, n_entries, list);
440 gtk_ui_manager_insert_action_group (priv->ui, action_group, 0);
442 if (!gtk_ui_manager_add_ui_from_string (priv->ui, ui_info, -1, &error)) {
443 g_warning ("Could not build contact menus from string:'%s'", error->message);
444 g_error_free (error);
447 g_object_unref (action_group);
449 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (list),
450 contact_list_row_separator_func,
453 /* Signal connection. */
454 g_signal_connect (priv->manager,
456 G_CALLBACK (contact_list_contact_added_cb),
458 g_signal_connect (priv->manager,
460 G_CALLBACK (contact_list_contact_removed_cb),
463 /* Connect to tree view signals rather than override. */
464 g_signal_connect (list,
465 "button-press-event",
466 G_CALLBACK (contact_list_button_press_event_cb),
468 g_signal_connect (list,
470 G_CALLBACK (contact_list_row_activated_cb),
472 g_signal_connect (list,
474 G_CALLBACK (contact_list_row_expand_or_collapse_cb),
475 GINT_TO_POINTER (TRUE));
476 g_signal_connect (list,
478 G_CALLBACK (contact_list_row_expand_or_collapse_cb),
479 GINT_TO_POINTER (FALSE));
481 /* Add contacts already created */
482 contacts = empathy_contact_manager_get_contacts (priv->manager);
483 for (l = contacts; l; l = l->next) {
484 GossipContact *contact;
488 contact_list_contact_added_cb (priv->manager, contact, list);
490 g_object_unref (contact);
492 g_list_free (contacts);
496 contact_list_finalize (GObject *object)
498 GossipContactListPriv *priv;
500 priv = GET_PRIV (object);
502 /* FIXME: disconnect all signals on the manager and contacts */
504 g_object_unref (priv->manager);
505 g_object_unref (priv->ui);
506 g_object_unref (priv->store);
507 g_object_unref (priv->filter);
508 g_free (priv->filter_text);
510 G_OBJECT_CLASS (gossip_contact_list_parent_class)->finalize (object);
514 contact_list_get_property (GObject *object,
519 GossipContactListPriv *priv;
521 priv = GET_PRIV (object);
524 case PROP_SHOW_OFFLINE:
525 g_value_set_boolean (value, priv->show_offline);
527 case PROP_SHOW_AVATARS:
528 g_value_set_boolean (value, priv->show_avatars);
530 case PROP_IS_COMPACT:
531 g_value_set_boolean (value, priv->is_compact);
534 g_value_set_string (value, priv->filter_text);
537 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
543 contact_list_set_property (GObject *object,
548 GossipContactListPriv *priv;
550 priv = GET_PRIV (object);
553 case PROP_SHOW_OFFLINE:
554 gossip_contact_list_set_show_offline (GOSSIP_CONTACT_LIST (object),
555 g_value_get_boolean (value));
557 case PROP_SHOW_AVATARS:
558 gossip_contact_list_set_show_avatars (GOSSIP_CONTACT_LIST (object),
559 g_value_get_boolean (value));
561 case PROP_IS_COMPACT:
562 gossip_contact_list_set_is_compact (GOSSIP_CONTACT_LIST (object),
563 g_value_get_boolean (value));
566 gossip_contact_list_set_filter (GOSSIP_CONTACT_LIST (object),
567 g_value_get_string (value));
570 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
576 contact_list_row_separator_func (GtkTreeModel *model,
580 gboolean is_separator = FALSE;
582 gtk_tree_model_get (model, iter,
583 COL_IS_SEPARATOR, &is_separator,
590 contact_list_contact_update (GossipContactList *list,
591 GossipContact *contact)
593 GossipContactListPriv *priv;
594 ShowActiveData *data;
598 gboolean should_be_in_list;
599 gboolean was_online = TRUE;
600 gboolean now_online = FALSE;
601 gboolean set_model = FALSE;
602 gboolean do_remove = FALSE;
603 gboolean do_set_active = FALSE;
604 gboolean do_set_refresh = FALSE;
605 GdkPixbuf *pixbuf_avatar;
607 priv = GET_PRIV (list);
609 model = GTK_TREE_MODEL (priv->store);
611 iters = contact_list_find_contact (list, contact);
618 /* Get online state now. */
619 now_online = gossip_contact_is_online (contact);
621 if (priv->show_offline || now_online) {
622 should_be_in_list = TRUE;
624 should_be_in_list = FALSE;
627 if (!in_list && !should_be_in_list) {
629 gossip_debug (DEBUG_DOMAIN,
630 "Contact:'%s' in list:NO, should be:NO",
631 gossip_contact_get_name (contact));
633 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
637 else if (in_list && !should_be_in_list) {
638 gossip_debug (DEBUG_DOMAIN,
639 "Contact:'%s' in list:YES, should be:NO",
640 gossip_contact_get_name (contact));
642 if (priv->show_active) {
644 do_set_active = TRUE;
645 do_set_refresh = TRUE;
648 gossip_debug (DEBUG_DOMAIN, "Remove item (after timeout)");
650 gossip_debug (DEBUG_DOMAIN, "Remove item (now)!");
651 contact_list_remove_contact (list, contact);
654 else if (!in_list && should_be_in_list) {
655 gossip_debug (DEBUG_DOMAIN,
656 "Contact:'%s' in list:NO, should be:YES",
657 gossip_contact_get_name (contact));
659 contact_list_add_contact (list, contact);
661 if (priv->show_active) {
662 do_set_active = TRUE;
664 gossip_debug (DEBUG_DOMAIN, "Set active (contact added)");
667 gossip_debug (DEBUG_DOMAIN,
668 "Contact:'%s' in list:YES, should be:YES",
669 gossip_contact_get_name (contact));
671 /* Get online state before. */
672 if (iters && g_list_length (iters) > 0) {
673 gtk_tree_model_get (model, iters->data, COL_IS_ONLINE, &was_online, -1);
676 /* Is this really an update or an online/offline. */
677 if (priv->show_active) {
678 if (was_online != now_online) {
681 do_set_active = TRUE;
682 do_set_refresh = TRUE;
685 str = "online -> offline";
687 str = "offline -> online";
690 gossip_debug (DEBUG_DOMAIN, "Set active (contact updated %s)", str);
692 /* Was TRUE for presence updates. */
693 /* do_set_active = FALSE; */
694 do_set_refresh = TRUE;
696 gossip_debug (DEBUG_DOMAIN, "Set active (contact updated)");
703 pixbuf_avatar = gossip_pixbuf_avatar_from_contact_scaled (contact, 32, 32);
704 for (l = iters; l && set_model; l = l->next) {
705 gtk_tree_store_set (priv->store, l->data,
706 COL_ICON_STATUS, gossip_icon_name_for_contact (contact),
707 COL_STATUS, gossip_contact_get_status (contact),
708 COL_IS_ONLINE, now_online,
709 COL_NAME, gossip_contact_get_name (contact),
710 COL_PIXBUF_AVATAR, pixbuf_avatar,
715 g_object_unref (pixbuf_avatar);
718 if (priv->show_active && do_set_active) {
719 contact_list_contact_set_active (list, contact, do_set_active, do_set_refresh);
722 data = contact_list_contact_active_new (list, contact, do_remove);
723 g_timeout_add (ACTIVE_USER_SHOW_TIME,
724 (GSourceFunc) contact_list_contact_active_cb,
729 /* FIXME: when someone goes online then offline quickly, the
730 * first timeout sets the user to be inactive and the second
731 * timeout removes the user from the contact list, really we
732 * should remove the first timeout.
734 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
739 contact_list_contact_added_cb (EmpathyContactManager *manager,
740 GossipContact *contact,
741 GossipContactList *list)
743 GossipContactListPriv *priv;
745 priv = GET_PRIV (list);
747 gossip_debug (DEBUG_DOMAIN,
748 "Contact:'%s' added",
749 gossip_contact_get_name (contact));
751 g_signal_connect (contact, "notify::groups",
752 G_CALLBACK (contact_list_contact_groups_updated_cb),
754 g_signal_connect (contact, "notify::presence",
755 G_CALLBACK (contact_list_contact_updated_cb),
757 g_signal_connect (contact, "notify::name",
758 G_CALLBACK (contact_list_contact_updated_cb),
760 g_signal_connect (contact, "notify::avatar",
761 G_CALLBACK (contact_list_contact_updated_cb),
763 g_signal_connect (contact, "notify::type",
764 G_CALLBACK (contact_list_contact_updated_cb),
767 contact_list_add_contact (list, contact);
771 contact_list_contact_groups_updated_cb (GossipContact *contact,
773 GossipContactList *list)
775 GossipContactListPriv *priv;
777 priv = GET_PRIV (list);
779 if (priv->show_offline || gossip_contact_is_online (contact)) {
783 gossip_debug (DEBUG_DOMAIN, "Contact:'%s' groups updated",
784 gossip_contact_get_name (contact));
786 /* We do this to make sure the groups are correct, if not, we
787 * would have to check the groups already set up for each
788 * contact and then see what has been updated.
790 contact_list_remove_contact (list, contact);
791 contact_list_add_contact (list, contact);
795 contact_list_contact_updated_cb (GossipContact *contact,
797 GossipContactList *list)
799 gossip_debug (DEBUG_DOMAIN,
800 "Contact:'%s' updated, checking roster is in sync...",
801 gossip_contact_get_name (contact));
803 contact_list_contact_update (list, contact);
807 contact_list_contact_removed_cb (EmpathyContactManager *manager,
808 GossipContact *contact,
809 GossipContactList *list)
811 gossip_debug (DEBUG_DOMAIN, "Contact:'%s' removed",
812 gossip_contact_get_name (contact));
814 /* Disconnect signals */
815 g_signal_handlers_disconnect_by_func (contact,
816 G_CALLBACK (contact_list_contact_groups_updated_cb),
818 g_signal_handlers_disconnect_by_func (contact,
819 G_CALLBACK (contact_list_contact_updated_cb),
822 contact_list_remove_contact (list, contact);
826 contact_list_contact_set_active (GossipContactList *list,
827 GossipContact *contact,
829 gboolean set_changed)
831 GossipContactListPriv *priv;
835 priv = GET_PRIV (list);
837 model = GTK_TREE_MODEL (priv->store);
839 iters = contact_list_find_contact (list, contact);
840 for (l = iters; l; l = l->next) {
843 gtk_tree_store_set (priv->store, l->data,
844 COL_IS_ACTIVE, active,
847 gossip_debug (DEBUG_DOMAIN, "Set item %s", active ? "active" : "inactive");
850 path = gtk_tree_model_get_path (model, l->data);
851 gtk_tree_model_row_changed (model, path, l->data);
852 gtk_tree_path_free (path);
856 g_list_foreach (iters, (GFunc)gtk_tree_iter_free, NULL);
861 static ShowActiveData *
862 contact_list_contact_active_new (GossipContactList *list,
863 GossipContact *contact,
866 ShowActiveData *data;
868 g_return_val_if_fail (list != NULL, NULL);
869 g_return_val_if_fail (contact != NULL, NULL);
871 gossip_debug (DEBUG_DOMAIN,
872 "Contact:'%s' now active, and %s be removed",
873 gossip_contact_get_name (contact),
874 remove ? "WILL" : "WILL NOT");
876 data = g_slice_new0 (ShowActiveData);
878 data->list = g_object_ref (list);
879 data->contact = g_object_ref (contact);
881 data->remove = remove;
887 contact_list_contact_active_free (ShowActiveData *data)
889 g_return_if_fail (data != NULL);
891 g_object_unref (data->contact);
892 g_object_unref (data->list);
894 g_slice_free (ShowActiveData, data);
898 contact_list_contact_active_cb (ShowActiveData *data)
900 GossipContactListPriv *priv;
902 g_return_val_if_fail (data != NULL, FALSE);
904 priv = GET_PRIV (data->list);
907 !priv->show_offline &&
908 !gossip_contact_is_online (data->contact)) {
909 gossip_debug (DEBUG_DOMAIN,
910 "Contact:'%s' active timeout, removing item",
911 gossip_contact_get_name (data->contact));
912 contact_list_remove_contact (data->list,
916 gossip_debug (DEBUG_DOMAIN,
917 "Contact:'%s' no longer active",
918 gossip_contact_get_name (data->contact));
919 contact_list_contact_set_active (data->list,
924 contact_list_contact_active_free (data);
930 contact_list_get_parent_group (GtkTreeModel *model,
932 gboolean *path_is_group)
934 GtkTreeIter parent_iter, iter;
938 g_return_val_if_fail (model != NULL, NULL);
939 g_return_val_if_fail (path != NULL, NULL);
940 g_return_val_if_fail (path_is_group != NULL, NULL);
942 if (!gtk_tree_model_get_iter (model, &iter, path)) {
946 gtk_tree_model_get (model, &iter,
947 COL_IS_GROUP, &is_group,
951 if (!gtk_tree_model_iter_parent (model, &parent_iter, &iter)) {
957 gtk_tree_model_get (model, &iter,
958 COL_IS_GROUP, &is_group,
965 *path_is_group = TRUE;
968 gtk_tree_model_get (model, &iter,
976 contact_list_get_group_foreach (GtkTreeModel *model,
984 /* Groups are only at the top level. */
985 if (gtk_tree_path_get_depth (path) != 1) {
989 gtk_tree_model_get (model, iter,
991 COL_IS_GROUP, &is_group,
994 if (is_group && strcmp (str, fg->name) == 0) {
1005 contact_list_get_group (GossipContactList *list,
1007 GtkTreeIter *iter_group_to_set,
1008 GtkTreeIter *iter_separator_to_set,
1011 GossipContactListPriv *priv;
1012 GtkTreeModel *model;
1013 GtkTreeIter iter_group, iter_separator;
1016 priv = GET_PRIV (list);
1018 memset (&fg, 0, sizeof (fg));
1022 model = GTK_TREE_MODEL (priv->store);
1023 gtk_tree_model_foreach (model,
1024 (GtkTreeModelForeachFunc) contact_list_get_group_foreach,
1032 gtk_tree_store_append (priv->store, &iter_group, NULL);
1033 gtk_tree_store_set (priv->store, &iter_group,
1034 COL_ICON_STATUS, NULL,
1037 COL_IS_ACTIVE, FALSE,
1038 COL_IS_SEPARATOR, FALSE,
1041 if (iter_group_to_set) {
1042 *iter_group_to_set = iter_group;
1045 gtk_tree_store_append (priv->store,
1048 gtk_tree_store_set (priv->store, &iter_separator,
1049 COL_IS_SEPARATOR, TRUE,
1052 if (iter_separator_to_set) {
1053 *iter_separator_to_set = iter_separator;
1060 if (iter_group_to_set) {
1061 *iter_group_to_set = fg.iter;
1064 iter_separator = fg.iter;
1066 if (gtk_tree_model_iter_next (model, &iter_separator)) {
1067 gboolean is_separator;
1069 gtk_tree_model_get (model, &iter_separator,
1070 COL_IS_SEPARATOR, &is_separator,
1073 if (is_separator && iter_separator_to_set) {
1074 *iter_separator_to_set = iter_separator;
1081 contact_list_add_contact (GossipContactList *list,
1082 GossipContact *contact)
1084 GossipContactListPriv *priv;
1085 GtkTreeIter iter, iter_group, iter_separator;
1086 GtkTreeModel *model;
1089 priv = GET_PRIV (list);
1091 if (!priv->show_offline && !gossip_contact_is_online (contact)) {
1095 model = gtk_tree_view_get_model (GTK_TREE_VIEW (list));
1097 /* If no groups just add it at the top level. */
1098 groups = gossip_contact_get_groups (contact);
1100 GdkPixbuf *pixbuf_avatar;
1101 gboolean show_avatar = FALSE;
1103 pixbuf_avatar = gossip_pixbuf_avatar_from_contact_scaled (contact, 32, 32);
1105 if (priv->show_avatars && !priv->is_compact) {
1109 gossip_debug (DEBUG_DOMAIN, "");
1110 gossip_debug (DEBUG_DOMAIN,
1111 "vvvvvvvvvvvvvvvv FIXME: Errors may follow below (since filter work) vvvvvvvvvvvvvvvv");
1113 gossip_debug (DEBUG_DOMAIN,
1114 "**** GossipContact:%p, is GObject:%s, is GossipContact:%s, ADDING CONTACT #1",
1116 G_IS_OBJECT (contact) ? "yes" : "no",
1117 GOSSIP_IS_CONTACT (contact) ? "yes" : "no");
1119 gtk_tree_store_append (priv->store, &iter, NULL);
1120 gtk_tree_store_set (priv->store, &iter,
1121 COL_ICON_STATUS, gossip_icon_name_for_contact (contact),
1122 COL_PIXBUF_AVATAR, pixbuf_avatar,
1123 COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
1124 COL_NAME, gossip_contact_get_name (contact),
1125 COL_STATUS, gossip_contact_get_status (contact),
1126 COL_STATUS_VISIBLE, !priv->is_compact,
1127 COL_CONTACT, contact,
1128 COL_IS_GROUP, FALSE,
1129 COL_IS_ACTIVE, FALSE,
1130 COL_IS_ONLINE, gossip_contact_is_online (contact),
1131 COL_IS_SEPARATOR, FALSE,
1134 gossip_debug (DEBUG_DOMAIN,
1135 "^^^^^^^^^^^^^^^^ FIXME: Errors may occur above (since filter work) ^^^^^^^^^^^^^^^^");
1136 gossip_debug (DEBUG_DOMAIN, "");
1138 if (pixbuf_avatar) {
1139 g_object_unref (pixbuf_avatar);
1143 /* Else add to each group. */
1144 for (l = groups; l; l = l->next) {
1146 GtkTreeIter model_iter_group;
1147 GdkPixbuf *pixbuf_avatar;
1151 gboolean show_avatar = FALSE;
1158 pixbuf_avatar = gossip_pixbuf_avatar_from_contact_scaled (contact, 32, 32);
1160 contact_list_get_group (list, name, &iter_group, &iter_separator, &created);
1162 if (priv->show_avatars && !priv->is_compact) {
1166 gossip_debug (DEBUG_DOMAIN, "");
1167 gossip_debug (DEBUG_DOMAIN,
1168 "vvvvvvvvvvvvvvvv FIXME: Errors may follow below (since filter work) vvvvvvvvvvvvvvvv");
1170 gossip_debug (DEBUG_DOMAIN,
1171 "**** GossipContact:%p, is GObject:%s, is GossipContact:%s, ADDING CONTACT #2",
1173 G_IS_OBJECT (contact) ? "yes" : "no",
1174 GOSSIP_IS_CONTACT (contact) ? "yes" : "no");
1176 gtk_tree_store_insert_after (priv->store, &iter, &iter_group, NULL);
1177 gtk_tree_store_set (priv->store, &iter,
1178 COL_ICON_STATUS, gossip_icon_name_for_contact (contact),
1179 COL_PIXBUF_AVATAR, pixbuf_avatar,
1180 COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
1181 COL_NAME, gossip_contact_get_name (contact),
1182 COL_STATUS, gossip_contact_get_status (contact),
1183 COL_STATUS_VISIBLE, !priv->is_compact,
1184 COL_CONTACT, contact,
1185 COL_IS_GROUP, FALSE,
1186 COL_IS_ACTIVE, FALSE,
1187 COL_IS_ONLINE, gossip_contact_is_online (contact),
1188 COL_IS_SEPARATOR, FALSE,
1191 gossip_debug (DEBUG_DOMAIN,
1192 "^^^^^^^^^^^^^^^^ FIXME: Errors may occur above (since filter work) ^^^^^^^^^^^^^^^^");
1193 gossip_debug (DEBUG_DOMAIN, "");
1195 if (pixbuf_avatar) {
1196 g_object_unref (pixbuf_avatar);
1203 found = gtk_tree_model_filter_convert_child_iter_to_iter (GTK_TREE_MODEL_FILTER (priv->filter),
1210 path = gtk_tree_model_get_path (model, &model_iter_group);
1215 if (gossip_contact_group_get_expanded (name)) {
1216 g_signal_handlers_block_by_func (list,
1217 contact_list_row_expand_or_collapse_cb,
1218 GINT_TO_POINTER (TRUE));
1219 gtk_tree_view_expand_row (GTK_TREE_VIEW (list), path, TRUE);
1220 g_signal_handlers_unblock_by_func (list,
1221 contact_list_row_expand_or_collapse_cb,
1222 GINT_TO_POINTER (TRUE));
1224 g_signal_handlers_block_by_func (list,
1225 contact_list_row_expand_or_collapse_cb,
1226 GINT_TO_POINTER (FALSE));
1227 gtk_tree_view_collapse_row (GTK_TREE_VIEW (list), path);
1228 g_signal_handlers_unblock_by_func (list,
1229 contact_list_row_expand_or_collapse_cb,
1230 GINT_TO_POINTER (FALSE));
1233 gtk_tree_path_free (path);
1238 contact_list_remove_contact (GossipContactList *list,
1239 GossipContact *contact)
1241 GossipContactListPriv *priv;
1242 GtkTreeModel *model;
1245 priv = GET_PRIV (list);
1247 iters = contact_list_find_contact (list, contact);
1252 /* Clean up model */
1253 model = GTK_TREE_MODEL (priv->store);
1255 for (l = iters; l; l = l->next) {
1258 /* NOTE: it is only <= 2 here because we have
1259 * separators after the group name, otherwise it
1262 if (gtk_tree_model_iter_parent (model, &parent, l->data) &&
1263 gtk_tree_model_iter_n_children (model, &parent) <= 2) {
1264 gtk_tree_store_remove (priv->store, &parent);
1266 gtk_tree_store_remove (priv->store, l->data);
1270 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
1271 g_list_free (iters);
1275 contact_list_create_model (GossipContactList *list)
1277 GossipContactListPriv *priv;
1278 GtkTreeModel *model;
1280 priv = GET_PRIV (list);
1283 g_object_unref (priv->store);
1287 g_object_unref (priv->filter);
1290 priv->store = gtk_tree_store_new (COL_COUNT,
1291 G_TYPE_STRING, /* Status icon-name */
1292 GDK_TYPE_PIXBUF, /* Avatar pixbuf */
1293 G_TYPE_BOOLEAN, /* Avatar pixbuf visible */
1294 G_TYPE_STRING, /* Name */
1295 G_TYPE_STRING, /* Status string */
1296 G_TYPE_BOOLEAN, /* Show status */
1297 GOSSIP_TYPE_CONTACT, /* Contact type */
1298 G_TYPE_BOOLEAN, /* Is group */
1299 G_TYPE_BOOLEAN, /* Is active */
1300 G_TYPE_BOOLEAN, /* Is online */
1301 G_TYPE_BOOLEAN); /* Is separator */
1303 /* Save normal model */
1304 model = GTK_TREE_MODEL (priv->store);
1306 /* Set up sorting */
1307 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (model),
1309 contact_list_sort_func,
1312 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (model),
1314 GTK_SORT_ASCENDING);
1317 priv->filter = gtk_tree_model_filter_new (model, NULL);
1319 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (priv->filter),
1320 (GtkTreeModelFilterVisibleFunc)
1321 contact_list_filter_func,
1324 gtk_tree_view_set_model (GTK_TREE_VIEW (list), priv->filter);
1328 contact_list_search_equal_func (GtkTreeModel *model,
1332 gpointer search_data)
1334 gchar *name, *name_folded;
1342 gtk_tree_model_get (model, iter, COL_NAME, &name, -1);
1348 name_folded = g_utf8_casefold (name, -1);
1349 key_folded = g_utf8_casefold (key, -1);
1351 if (name_folded && key_folded &&
1352 strstr (name_folded, key_folded)) {
1359 g_free (name_folded);
1360 g_free (key_folded);
1366 contact_list_setup_view (GossipContactList *list)
1368 GtkCellRenderer *cell;
1369 GtkTreeViewColumn *col;
1372 gtk_tree_view_set_search_equal_func (GTK_TREE_VIEW (list),
1373 contact_list_search_equal_func,
1378 "headers-visible", FALSE,
1379 "reorderable", TRUE,
1380 "show-expanders", FALSE,
1383 col = gtk_tree_view_column_new ();
1386 cell = gtk_cell_renderer_pixbuf_new ();
1387 gtk_tree_view_column_pack_start (col, cell, FALSE);
1388 gtk_tree_view_column_set_cell_data_func (
1390 (GtkTreeCellDataFunc) contact_list_pixbuf_cell_data_func,
1400 cell = gossip_cell_renderer_text_new ();
1401 gtk_tree_view_column_pack_start (col, cell, TRUE);
1402 gtk_tree_view_column_set_cell_data_func (
1404 (GtkTreeCellDataFunc) contact_list_text_cell_data_func,
1407 gtk_tree_view_column_add_attribute (col, cell,
1409 gtk_tree_view_column_add_attribute (col, cell,
1410 "status", COL_STATUS);
1411 gtk_tree_view_column_add_attribute (col, cell,
1412 "is_group", COL_IS_GROUP);
1415 cell = gtk_cell_renderer_pixbuf_new ();
1416 gtk_tree_view_column_pack_start (col, cell, FALSE);
1417 gtk_tree_view_column_set_cell_data_func (
1419 (GtkTreeCellDataFunc) contact_list_avatar_cell_data_func,
1431 cell = gossip_cell_renderer_expander_new ();
1432 gtk_tree_view_column_pack_end (col, cell, FALSE);
1433 gtk_tree_view_column_set_cell_data_func (
1435 (GtkTreeCellDataFunc) contact_list_expander_cell_data_func,
1438 /* Actually add the column now we have added all cell renderers */
1439 gtk_tree_view_append_column (GTK_TREE_VIEW (list), col);
1442 for (i = 0; i < G_N_ELEMENTS (drag_types_dest); ++i) {
1443 drag_atoms_dest[i] = gdk_atom_intern (drag_types_dest[i].target,
1447 for (i = 0; i < G_N_ELEMENTS (drag_types_source); ++i) {
1448 drag_atoms_source[i] = gdk_atom_intern (drag_types_source[i].target,
1452 /* Note: We support the COPY action too, but need to make the
1453 * MOVE action the default.
1455 gtk_drag_source_set (GTK_WIDGET (list),
1458 G_N_ELEMENTS (drag_types_source),
1461 gtk_drag_dest_set (GTK_WIDGET (list),
1462 GTK_DEST_DEFAULT_ALL,
1464 G_N_ELEMENTS (drag_types_dest),
1465 GDK_ACTION_MOVE | GDK_ACTION_LINK);
1467 g_signal_connect (GTK_WIDGET (list),
1468 "drag-data-received",
1469 G_CALLBACK (contact_list_drag_data_received),
1472 /* FIXME: noticed but when you drag the row over the treeview
1473 * fast, it seems to stop redrawing itself, if we don't
1474 * connect this signal, all is fine.
1476 g_signal_connect (GTK_WIDGET (list),
1478 G_CALLBACK (contact_list_drag_motion),
1481 g_signal_connect (GTK_WIDGET (list),
1483 G_CALLBACK (contact_list_drag_begin),
1485 g_signal_connect (GTK_WIDGET (list),
1487 G_CALLBACK (contact_list_drag_data_get),
1489 g_signal_connect (GTK_WIDGET (list),
1491 G_CALLBACK (contact_list_drag_end),
1496 contact_list_drag_data_received (GtkWidget *widget,
1497 GdkDragContext *context,
1500 GtkSelectionData *selection,
1505 GossipContactListPriv *priv;
1506 GtkTreeModel *model;
1508 GtkTreeViewDropPosition position;
1509 GossipContact *contact;
1514 gboolean drag_success = TRUE;
1515 gboolean drag_del = FALSE;
1517 priv = GET_PRIV (widget);
1519 id = (const gchar*) selection->data;
1520 gossip_debug (DEBUG_DOMAIN, "Received %s%s drag & drop contact from roster with id:'%s'",
1521 context->action == GDK_ACTION_MOVE ? "move" : "",
1522 context->action == GDK_ACTION_COPY ? "copy" : "",
1525 /* FIXME: This is ambigous, an id can come from multiple accounts */
1526 contact = empathy_contact_manager_find (priv->manager, id);
1528 gossip_debug (DEBUG_DOMAIN, "No contact found associated with drag & drop");
1532 groups = gossip_contact_get_groups (contact);
1534 is_row = gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget),
1541 if (g_list_length (groups) != 1) {
1542 /* if they have dragged a contact out of a
1543 * group then we would set the contact to have
1544 * NO groups but only if they were ONE group
1545 * to begin with - should we do this
1546 * regardless to how many groups they are in
1547 * already or not at all?
1552 gossip_contact_set_groups (contact, NULL);
1554 GList *l, *new_groups;
1558 model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
1559 name = contact_list_get_parent_group (model, path, &is_group);
1561 if (groups && name &&
1562 g_list_find_custom (groups, name, (GCompareFunc)strcmp)) {
1567 /* Get source group information. */
1568 priv = GET_PRIV (widget);
1569 if (!priv->drag_row) {
1574 path = gtk_tree_row_reference_get_path (priv->drag_row);
1580 old_group = contact_list_get_parent_group (model, path, &is_group);
1581 gtk_tree_path_free (path);
1583 if (!name && old_group && GDK_ACTION_MOVE) {
1584 drag_success = FALSE;
1587 if (context->action == GDK_ACTION_MOVE) {
1591 /* Create new groups GList. */
1592 for (l = groups, new_groups = NULL; l && drag_success; l = l->next) {
1596 if (context->action == GDK_ACTION_MOVE &&
1597 old_group != NULL &&
1598 strcmp (str, old_group) == 0) {
1606 new_groups = g_list_append (new_groups, g_strdup (str));
1611 new_groups = g_list_append (new_groups, name);
1613 gossip_contact_set_groups (contact, new_groups);
1619 gtk_drag_finish (context, drag_success, drag_del, GDK_CURRENT_TIME);
1623 contact_list_drag_motion (GtkWidget *widget,
1624 GdkDragContext *context,
1630 static DragMotionData *dm = NULL;
1633 gboolean is_different = FALSE;
1634 gboolean cleanup = TRUE;
1636 is_row = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
1647 cleanup &= (dm && gtk_tree_path_compare (dm->path, path) != 0);
1648 is_different = (!dm || (dm && gtk_tree_path_compare (dm->path, path) != 0));
1653 if (!is_different && !cleanup) {
1658 gtk_tree_path_free (dm->path);
1659 if (dm->timeout_id) {
1660 g_source_remove (dm->timeout_id);
1668 if (!gtk_tree_view_row_expanded (GTK_TREE_VIEW (widget), path)) {
1669 dm = g_new0 (DragMotionData, 1);
1671 dm->list = GOSSIP_CONTACT_LIST (widget);
1672 dm->path = gtk_tree_path_copy (path);
1674 dm->timeout_id = g_timeout_add (
1676 (GSourceFunc) contact_list_drag_motion_cb,
1684 contact_list_drag_motion_cb (DragMotionData *data)
1686 gtk_tree_view_expand_row (GTK_TREE_VIEW (data->list),
1690 data->timeout_id = 0;
1696 contact_list_drag_begin (GtkWidget *widget,
1697 GdkDragContext *context,
1700 GossipContactListPriv *priv;
1701 GtkTreeSelection *selection;
1702 GtkTreeModel *model;
1706 priv = GET_PRIV (widget);
1708 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (widget));
1709 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
1713 path = gtk_tree_model_get_path (model, &iter);
1714 priv->drag_row = gtk_tree_row_reference_new (model, path);
1715 gtk_tree_path_free (path);
1719 contact_list_drag_data_get (GtkWidget *widget,
1720 GdkDragContext *context,
1721 GtkSelectionData *selection,
1726 GossipContactListPriv *priv;
1727 GtkTreePath *src_path;
1729 GtkTreeModel *model;
1730 GossipContact *contact;
1733 priv = GET_PRIV (widget);
1735 model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
1736 if (!priv->drag_row) {
1740 src_path = gtk_tree_row_reference_get_path (priv->drag_row);
1745 if (!gtk_tree_model_get_iter (model, &iter, src_path)) {
1746 gtk_tree_path_free (src_path);
1750 gtk_tree_path_free (src_path);
1752 contact = gossip_contact_list_get_selected (GOSSIP_CONTACT_LIST (widget));
1757 id = gossip_contact_get_id (contact);
1758 g_object_unref (contact);
1761 case DND_DRAG_TYPE_CONTACT_ID:
1762 gtk_selection_data_set (selection, drag_atoms_source[info], 8,
1763 (guchar*)id, strlen (id) + 1);
1772 contact_list_drag_end (GtkWidget *widget,
1773 GdkDragContext *context,
1776 GossipContactListPriv *priv;
1778 priv = GET_PRIV (widget);
1780 if (priv->drag_row) {
1781 gtk_tree_row_reference_free (priv->drag_row);
1782 priv->drag_row = NULL;
1787 contact_list_cell_set_background (GossipContactList *list,
1788 GtkCellRenderer *cell,
1795 g_return_if_fail (list != NULL);
1796 g_return_if_fail (cell != NULL);
1798 style = gtk_widget_get_style (GTK_WIDGET (list));
1802 color = style->bg[GTK_STATE_SELECTED];
1804 /* Here we take the current theme colour and add it to
1805 * the colour for white and average the two. This
1806 * gives a colour which is inline with the theme but
1809 color.red = (color.red + (style->white).red) / 2;
1810 color.green = (color.green + (style->white).green) / 2;
1811 color.blue = (color.blue + (style->white).blue) / 2;
1814 "cell-background-gdk", &color,
1818 "cell-background-gdk", NULL,
1823 gint color_sum_normal;
1824 gint color_sum_selected;
1826 color = style->base[GTK_STATE_SELECTED];
1827 color_sum_normal = color.red+color.green+color.blue;
1828 color = style->base[GTK_STATE_NORMAL];
1829 color_sum_selected = color.red+color.green+color.blue;
1830 color = style->text_aa[GTK_STATE_INSENSITIVE];
1832 if (color_sum_normal < color_sum_selected) {
1833 /* Found a light theme */
1834 color.red = (color.red + (style->white).red) / 2;
1835 color.green = (color.green + (style->white).green) / 2;
1836 color.blue = (color.blue + (style->white).blue) / 2;
1838 /* Found a dark theme */
1839 color.red = (color.red + (style->black).red) / 2;
1840 color.green = (color.green + (style->black).green) / 2;
1841 color.blue = (color.blue + (style->black).blue) / 2;
1845 "cell-background-gdk", &color,
1852 contact_list_pixbuf_cell_data_func (GtkTreeViewColumn *tree_column,
1853 GtkCellRenderer *cell,
1854 GtkTreeModel *model,
1856 GossipContactList *list)
1862 gtk_tree_model_get (model, iter,
1863 COL_IS_GROUP, &is_group,
1864 COL_IS_ACTIVE, &is_active,
1865 COL_ICON_STATUS, &icon_name,
1869 "visible", !is_group,
1870 "icon-name", icon_name,
1875 contact_list_cell_set_background (list, cell, is_group, is_active);
1879 contact_list_avatar_cell_data_func (GtkTreeViewColumn *tree_column,
1880 GtkCellRenderer *cell,
1881 GtkTreeModel *model,
1883 GossipContactList *list)
1886 gboolean show_avatar;
1890 gtk_tree_model_get (model, iter,
1891 COL_PIXBUF_AVATAR, &pixbuf,
1892 COL_PIXBUF_AVATAR_VISIBLE, &show_avatar,
1893 COL_IS_GROUP, &is_group,
1894 COL_IS_ACTIVE, &is_active,
1898 "visible", !is_group && show_avatar,
1903 g_object_unref (pixbuf);
1906 contact_list_cell_set_background (list, cell, is_group, is_active);
1910 contact_list_text_cell_data_func (GtkTreeViewColumn *tree_column,
1911 GtkCellRenderer *cell,
1912 GtkTreeModel *model,
1914 GossipContactList *list)
1918 gboolean show_status;
1920 gtk_tree_model_get (model, iter,
1921 COL_IS_GROUP, &is_group,
1922 COL_IS_ACTIVE, &is_active,
1923 COL_STATUS_VISIBLE, &show_status,
1927 "show-status", show_status,
1930 contact_list_cell_set_background (list, cell, is_group, is_active);
1934 contact_list_expander_cell_data_func (GtkTreeViewColumn *column,
1935 GtkCellRenderer *cell,
1936 GtkTreeModel *model,
1938 GossipContactList *list)
1943 gtk_tree_model_get (model, iter,
1944 COL_IS_GROUP, &is_group,
1945 COL_IS_ACTIVE, &is_active,
1948 if (gtk_tree_model_iter_has_child (model, iter)) {
1950 gboolean row_expanded;
1952 path = gtk_tree_model_get_path (model, iter);
1953 row_expanded = gtk_tree_view_row_expanded (GTK_TREE_VIEW (column->tree_view), path);
1954 gtk_tree_path_free (path);
1958 "expander-style", row_expanded ? GTK_EXPANDER_EXPANDED : GTK_EXPANDER_COLLAPSED,
1961 g_object_set (cell, "visible", FALSE, NULL);
1964 contact_list_cell_set_background (list, cell, is_group, is_active);
1968 contact_list_get_contact_menu (GossipContactList *list,
1969 gboolean can_send_file,
1970 gboolean can_show_log)
1972 GossipContactListPriv *priv;
1976 priv = GET_PRIV (list);
1978 /* Sort out sensitive items */
1979 action = gtk_ui_manager_get_action (priv->ui, "/Contact/Log");
1980 gtk_action_set_sensitive (action, can_show_log);
1982 action = gtk_ui_manager_get_action (priv->ui, "/Contact/SendFile");
1983 gtk_action_set_visible (action, can_send_file);
1985 widget = gtk_ui_manager_get_widget (priv->ui, "/Contact");
1991 gossip_contact_list_get_group_menu (GossipContactList *list)
1993 GossipContactListPriv *priv;
1996 g_return_val_if_fail (GOSSIP_IS_CONTACT_LIST (list), NULL);
1998 priv = GET_PRIV (list);
2000 widget = gtk_ui_manager_get_widget (priv->ui, "/Group");
2006 gossip_contact_list_get_contact_menu (GossipContactList *list,
2007 GossipContact *contact)
2010 gboolean can_show_log;
2011 gboolean can_send_file;
2013 g_return_val_if_fail (GOSSIP_IS_CONTACT_LIST (list), NULL);
2014 g_return_val_if_fail (GOSSIP_IS_CONTACT (contact), NULL);
2016 can_show_log = FALSE; /* FIXME: gossip_log_exists_for_contact (contact); */
2017 can_send_file = FALSE;
2019 menu = contact_list_get_contact_menu (list,
2026 contact_list_button_press_event_cb (GossipContactList *list,
2027 GdkEventButton *event,
2030 GossipContactListPriv *priv;
2031 GossipContact *contact;
2033 GtkTreeSelection *selection;
2034 GtkTreeModel *model;
2036 gboolean row_exists;
2039 if (event->button != 3) {
2043 priv = GET_PRIV (list);
2045 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (list));
2046 model = GTK_TREE_MODEL (priv->store);
2048 gtk_widget_grab_focus (GTK_WIDGET (list));
2050 row_exists = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (list),
2058 gtk_tree_selection_unselect_all (selection);
2059 gtk_tree_selection_select_path (selection, path);
2061 gtk_tree_model_get_iter (model, &iter, path);
2062 gtk_tree_path_free (path);
2064 gtk_tree_model_get (model, &iter, COL_CONTACT, &contact, -1);
2067 menu = gossip_contact_list_get_contact_menu (list, contact);
2068 g_object_unref (contact);
2070 menu = gossip_contact_list_get_group_menu (list);
2077 gtk_widget_show (menu);
2079 gtk_menu_popup (GTK_MENU (menu),
2080 NULL, NULL, NULL, NULL,
2081 event->button, event->time);
2087 contact_list_row_activated_cb (GossipContactList *list,
2089 GtkTreeViewColumn *col,
2092 GossipContact *contact;
2094 GtkTreeModel *model;
2097 view = GTK_TREE_VIEW (list);
2098 model = gtk_tree_view_get_model (view);
2100 gtk_tree_model_get_iter (model, &iter, path);
2101 gtk_tree_model_get (model, &iter, COL_CONTACT, &contact, -1);
2104 contact_list_action_activated (list, contact);
2105 g_object_unref (contact);
2110 contact_list_row_expand_or_collapse_cb (GossipContactList *list,
2115 GtkTreeModel *model;
2119 model = gtk_tree_view_get_model (GTK_TREE_VIEW (list));
2121 gtk_tree_model_get (model, iter,
2125 expanded = GPOINTER_TO_INT (user_data);
2126 gossip_contact_group_set_expanded (name, expanded);
2132 contact_list_sort_func (GtkTreeModel *model,
2133 GtkTreeIter *iter_a,
2134 GtkTreeIter *iter_b,
2137 gchar *name_a, *name_b;
2138 GossipContact *contact_a, *contact_b;
2139 gboolean is_separator_a, is_separator_b;
2142 gtk_tree_model_get (model, iter_a,
2144 COL_CONTACT, &contact_a,
2145 COL_IS_SEPARATOR, &is_separator_a,
2147 gtk_tree_model_get (model, iter_b,
2149 COL_CONTACT, &contact_b,
2150 COL_IS_SEPARATOR, &is_separator_b,
2153 /* If contact is NULL it means it's a group. */
2155 if (is_separator_a || is_separator_b) {
2156 if (is_separator_a) {
2158 } else if (is_separator_b) {
2161 } else if (!contact_a && contact_b) {
2163 } else if (contact_a && !contact_b) {
2166 ret_val = g_utf8_collate (name_a, name_b);
2173 g_object_unref (contact_a);
2177 g_object_unref (contact_b);
2184 contact_list_filter_show_contact (GossipContact *contact,
2185 const gchar *filter)
2190 /* Check contact id */
2191 str = g_utf8_casefold (gossip_contact_get_id (contact), -1);
2192 visible = G_STR_EMPTY (str) || strstr (str, filter);
2199 /* Check contact name */
2200 str = g_utf8_casefold (gossip_contact_get_name (contact), -1);
2201 visible = G_STR_EMPTY (str) || strstr (str, filter);
2208 contact_list_filter_show_group (GossipContactList *list,
2210 const gchar *filter)
2212 GossipContactListPriv *priv;
2213 GList *contacts, *l;
2215 gboolean show_group = FALSE;
2217 priv = GET_PRIV (list);
2219 str = g_utf8_casefold (group, -1);
2224 /* If the filter is the partially the group name, we show the
2227 if (strstr (str, filter)) {
2232 /* At this point, we need to check in advance if this
2233 * group should be shown because a contact we want to
2234 * show exists in it.
2236 contacts = empathy_contact_manager_get_contacts (priv->manager);
2237 for (l = contacts; l && !show_group; l = l->next) {
2238 if (!gossip_contact_is_in_group (l->data, group)) {
2242 if (contact_list_filter_show_contact (l->data, filter)) {
2246 g_list_foreach (contacts, (GFunc) g_object_unref, NULL);
2247 g_list_free (contacts);
2254 contact_list_filter_func (GtkTreeModel *model,
2256 GossipContactList *list)
2258 GossipContactListPriv *priv;
2260 gboolean is_separator;
2261 gboolean visible = TRUE;
2263 priv = GET_PRIV (list);
2265 if (G_STR_EMPTY (priv->filter_text)) {
2269 /* Check to see if iter matches any group names */
2270 gtk_tree_model_get (model, iter,
2271 COL_IS_GROUP, &is_group,
2272 COL_IS_SEPARATOR, &is_separator,
2278 gtk_tree_model_get (model, iter, COL_NAME, &name, -1);
2279 visible &= contact_list_filter_show_group (list,
2283 } else if (is_separator) {
2284 /* Do nothing here */
2286 GossipContact *contact;
2288 /* Check contact id */
2289 gtk_tree_model_get (model, iter, COL_CONTACT, &contact, -1);
2290 visible &= contact_list_filter_show_contact (contact,
2292 g_object_unref (contact);
2299 contact_list_iter_equal_contact (GtkTreeModel *model,
2301 GossipContact *contact)
2306 gtk_tree_model_get (model, iter,
2314 equal = (c == contact);
2321 contact_list_find_contact_foreach (GtkTreeModel *model,
2326 if (contact_list_iter_equal_contact (model, iter, fc->contact)) {
2328 fc->iters = g_list_append (fc->iters, gtk_tree_iter_copy (iter));
2331 /* We want to find ALL contacts that match, this means if we
2332 * have the same contact in 3 groups, all iters should be
2339 contact_list_find_contact (GossipContactList *list,
2340 GossipContact *contact)
2342 GossipContactListPriv *priv;
2343 GtkTreeModel *model;
2347 priv = GET_PRIV (list);
2349 memset (&fc, 0, sizeof (fc));
2351 fc.contact = contact;
2353 model = GTK_TREE_MODEL (priv->store);
2354 gtk_tree_model_foreach (model,
2355 (GtkTreeModelForeachFunc) contact_list_find_contact_foreach,
2366 contact_list_action_cb (GtkAction *action,
2367 GossipContactList *list)
2369 GossipContact *contact;
2373 name = gtk_action_get_name (action);
2378 gossip_debug (DEBUG_DOMAIN, "Action:'%s' activated", name);
2380 contact = gossip_contact_list_get_selected (list);
2381 group = gossip_contact_list_get_selected_group (list);
2383 if (contact && strcmp (name, "Chat") == 0) {
2384 contact_list_action_activated (list, contact);
2386 else if (contact && strcmp (name, "Information") == 0) {
2388 else if (contact && strcmp (name, "Edit") == 0) {
2390 else if (contact && strcmp (name, "Remove") == 0) {
2392 else if (contact && strcmp (name, "Invite") == 0) {
2394 else if (contact && strcmp (name, "SendFile") == 0) {
2396 else if (contact && strcmp (name, "Log") == 0) {
2398 else if (group && strcmp (name, "Rename") == 0) {
2403 g_object_unref (contact);
2408 contact_list_action_activated (GossipContactList *list,
2409 GossipContact *contact)
2413 mc = mission_control_new (tp_get_bus ());
2414 mission_control_request_channel (mc,
2415 gossip_contact_get_account (contact),
2416 TP_IFACE_CHANNEL_TYPE_TEXT,
2417 gossip_contact_get_handle (contact),
2418 TP_HANDLE_TYPE_CONTACT,
2420 g_object_unref (mc);
2424 contact_list_update_list_mode_foreach (GtkTreeModel *model,
2427 GossipContactList *list)
2429 GossipContactListPriv *priv;
2430 gboolean show_avatar = FALSE;
2432 priv = GET_PRIV (list);
2434 if (priv->show_avatars && !priv->is_compact) {
2438 gtk_tree_store_set (priv->store, iter,
2439 COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
2440 COL_STATUS_VISIBLE, !priv->is_compact,
2447 gossip_contact_list_new (void)
2449 return g_object_new (GOSSIP_TYPE_CONTACT_LIST, NULL);
2453 gossip_contact_list_get_selected (GossipContactList *list)
2455 GossipContactListPriv *priv;
2456 GtkTreeSelection *selection;
2458 GtkTreeModel *model;
2459 GossipContact *contact;
2461 g_return_val_if_fail (GOSSIP_IS_CONTACT_LIST (list), NULL);
2463 priv = GET_PRIV (list);
2465 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (list));
2466 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
2470 gtk_tree_model_get (model, &iter, COL_CONTACT, &contact, -1);
2476 gossip_contact_list_get_selected_group (GossipContactList *list)
2478 GossipContactListPriv *priv;
2479 GtkTreeSelection *selection;
2481 GtkTreeModel *model;
2485 g_return_val_if_fail (GOSSIP_IS_CONTACT_LIST (list), NULL);
2487 priv = GET_PRIV (list);
2489 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (list));
2490 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
2494 gtk_tree_model_get (model, &iter,
2495 COL_IS_GROUP, &is_group,
2508 gossip_contact_list_get_show_offline (GossipContactList *list)
2510 GossipContactListPriv *priv;
2512 g_return_val_if_fail (GOSSIP_IS_CONTACT_LIST (list), FALSE);
2514 priv = GET_PRIV (list);
2516 return priv->show_offline;
2520 gossip_contact_list_get_show_avatars (GossipContactList *list)
2522 GossipContactListPriv *priv;
2524 g_return_val_if_fail (GOSSIP_IS_CONTACT_LIST (list), TRUE);
2526 priv = GET_PRIV (list);
2528 return priv->show_avatars;
2532 gossip_contact_list_get_is_compact (GossipContactList *list)
2534 GossipContactListPriv *priv;
2536 g_return_val_if_fail (GOSSIP_IS_CONTACT_LIST (list), TRUE);
2538 priv = GET_PRIV (list);
2540 return priv->is_compact;
2544 gossip_contact_list_set_show_offline (GossipContactList *list,
2545 gboolean show_offline)
2547 GossipContactListPriv *priv;
2548 GList *contacts, *l;
2549 gboolean show_active;
2551 g_return_if_fail (GOSSIP_IS_CONTACT_LIST (list));
2553 priv = GET_PRIV (list);
2555 priv->show_offline = show_offline;
2556 show_active = priv->show_active;
2558 /* Disable temporarily. */
2559 priv->show_active = FALSE;
2561 contacts = empathy_contact_manager_get_contacts (priv->manager);
2562 for (l = contacts; l; l = l->next) {
2563 GossipContact *contact;
2565 contact = GOSSIP_CONTACT (l->data);
2567 contact_list_contact_update (list, contact);
2569 g_object_unref (contact);
2571 g_list_free (contacts);
2573 /* Restore to original setting. */
2574 priv->show_active = show_active;
2578 gossip_contact_list_set_show_avatars (GossipContactList *list,
2579 gboolean show_avatars)
2581 GossipContactListPriv *priv;
2582 GtkTreeModel *model;
2584 g_return_if_fail (GOSSIP_IS_CONTACT_LIST (list));
2586 priv = GET_PRIV (list);
2588 priv->show_avatars = show_avatars;
2590 model = GTK_TREE_MODEL (priv->store);
2592 gtk_tree_model_foreach (model,
2593 (GtkTreeModelForeachFunc)
2594 contact_list_update_list_mode_foreach,
2599 gossip_contact_list_set_is_compact (GossipContactList *list,
2600 gboolean is_compact)
2602 GossipContactListPriv *priv;
2603 GtkTreeModel *model;
2605 g_return_if_fail (GOSSIP_IS_CONTACT_LIST (list));
2607 priv = GET_PRIV (list);
2609 priv->is_compact = is_compact;
2611 model = GTK_TREE_MODEL (priv->store);
2613 gtk_tree_model_foreach (model,
2614 (GtkTreeModelForeachFunc)
2615 contact_list_update_list_mode_foreach,
2620 gossip_contact_list_set_filter (GossipContactList *list,
2621 const gchar *filter)
2623 GossipContactListPriv *priv;
2625 g_return_if_fail (GOSSIP_IS_CONTACT_LIST (list));
2627 priv = GET_PRIV (list);
2629 g_free (priv->filter_text);
2631 priv->filter_text = g_utf8_casefold (filter, -1);
2633 priv->filter_text = NULL;
2636 gossip_debug (DEBUG_DOMAIN, "Refiltering with filter:'%s' (case folded)", filter);
2637 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter));