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 "gossip-contact-list.h"
41 #include "gossip-contact-groups.h"
42 #include "gossip-cell-renderer-expander.h"
43 #include "gossip-cell-renderer-text.h"
44 #include "gossip-stock.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", GOSSIP_STOCK_MESSAGE,
292 N_("_Chat"), NULL, N_("Chat with contact"),
293 G_CALLBACK (contact_list_action_cb)
295 { "Information", GOSSIP_STOCK_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", GOSSIP_STOCK_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_presence;
606 GdkPixbuf *pixbuf_avatar;
608 priv = GET_PRIV (list);
610 model = GTK_TREE_MODEL (priv->store);
612 iters = contact_list_find_contact (list, contact);
619 /* Get online state now. */
620 now_online = gossip_contact_is_online (contact);
622 if (priv->show_offline || now_online) {
623 should_be_in_list = TRUE;
625 should_be_in_list = FALSE;
628 if (!in_list && !should_be_in_list) {
630 gossip_debug (DEBUG_DOMAIN,
631 "Contact:'%s' in list:NO, should be:NO",
632 gossip_contact_get_name (contact));
634 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
638 else if (in_list && !should_be_in_list) {
639 gossip_debug (DEBUG_DOMAIN,
640 "Contact:'%s' in list:YES, should be:NO",
641 gossip_contact_get_name (contact));
643 if (priv->show_active) {
645 do_set_active = TRUE;
646 do_set_refresh = TRUE;
649 gossip_debug (DEBUG_DOMAIN, "Remove item (after timeout)");
651 gossip_debug (DEBUG_DOMAIN, "Remove item (now)!");
652 contact_list_remove_contact (list, contact);
655 else if (!in_list && should_be_in_list) {
656 gossip_debug (DEBUG_DOMAIN,
657 "Contact:'%s' in list:NO, should be:YES",
658 gossip_contact_get_name (contact));
660 contact_list_add_contact (list, contact);
662 if (priv->show_active) {
663 do_set_active = TRUE;
665 gossip_debug (DEBUG_DOMAIN, "Set active (contact added)");
668 gossip_debug (DEBUG_DOMAIN,
669 "Contact:'%s' in list:YES, should be:YES",
670 gossip_contact_get_name (contact));
672 /* Get online state before. */
673 if (iters && g_list_length (iters) > 0) {
674 gtk_tree_model_get (model, iters->data, COL_IS_ONLINE, &was_online, -1);
677 /* Is this really an update or an online/offline. */
678 if (priv->show_active) {
679 if (was_online != now_online) {
682 do_set_active = TRUE;
683 do_set_refresh = TRUE;
686 str = "online -> offline";
688 str = "offline -> online";
691 gossip_debug (DEBUG_DOMAIN, "Set active (contact updated %s)", str);
693 /* Was TRUE for presence updates. */
694 /* do_set_active = FALSE; */
695 do_set_refresh = TRUE;
697 gossip_debug (DEBUG_DOMAIN, "Set active (contact updated)");
704 pixbuf_presence = gossip_pixbuf_for_contact (contact);
705 pixbuf_avatar = gossip_pixbuf_avatar_from_contact_scaled (contact, 32, 32);
706 for (l = iters; l && set_model; l = l->next) {
707 gtk_tree_store_set (priv->store, l->data,
708 COL_PIXBUF_STATUS, pixbuf_presence,
709 COL_STATUS, gossip_contact_get_status (contact),
710 COL_IS_ONLINE, now_online,
711 COL_NAME, gossip_contact_get_name (contact),
712 COL_PIXBUF_AVATAR, pixbuf_avatar,
716 if (pixbuf_presence) {
717 g_object_unref (pixbuf_presence);
720 g_object_unref (pixbuf_avatar);
723 if (priv->show_active && do_set_active) {
724 contact_list_contact_set_active (list, contact, do_set_active, do_set_refresh);
727 data = contact_list_contact_active_new (list, contact, do_remove);
728 g_timeout_add (ACTIVE_USER_SHOW_TIME,
729 (GSourceFunc) contact_list_contact_active_cb,
734 /* FIXME: when someone goes online then offline quickly, the
735 * first timeout sets the user to be inactive and the second
736 * timeout removes the user from the contact list, really we
737 * should remove the first timeout.
739 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
744 contact_list_contact_added_cb (EmpathyContactManager *manager,
745 GossipContact *contact,
746 GossipContactList *list)
748 GossipContactListPriv *priv;
750 priv = GET_PRIV (list);
752 gossip_debug (DEBUG_DOMAIN,
753 "Contact:'%s' added",
754 gossip_contact_get_name (contact));
756 g_signal_connect (contact, "notify::groups",
757 G_CALLBACK (contact_list_contact_groups_updated_cb),
759 g_signal_connect (contact, "notify::presence",
760 G_CALLBACK (contact_list_contact_updated_cb),
762 g_signal_connect (contact, "notify::name",
763 G_CALLBACK (contact_list_contact_updated_cb),
765 g_signal_connect (contact, "notify::avatar",
766 G_CALLBACK (contact_list_contact_updated_cb),
768 g_signal_connect (contact, "notify::type",
769 G_CALLBACK (contact_list_contact_updated_cb),
772 contact_list_add_contact (list, contact);
776 contact_list_contact_groups_updated_cb (GossipContact *contact,
778 GossipContactList *list)
780 GossipContactListPriv *priv;
782 priv = GET_PRIV (list);
784 if (priv->show_offline || gossip_contact_is_online (contact)) {
788 gossip_debug (DEBUG_DOMAIN, "Contact:'%s' groups updated",
789 gossip_contact_get_name (contact));
791 /* We do this to make sure the groups are correct, if not, we
792 * would have to check the groups already set up for each
793 * contact and then see what has been updated.
795 contact_list_remove_contact (list, contact);
796 contact_list_add_contact (list, contact);
800 contact_list_contact_updated_cb (GossipContact *contact,
802 GossipContactList *list)
804 gossip_debug (DEBUG_DOMAIN,
805 "Contact:'%s' updated, checking roster is in sync...",
806 gossip_contact_get_name (contact));
808 contact_list_contact_update (list, contact);
812 contact_list_contact_removed_cb (EmpathyContactManager *manager,
813 GossipContact *contact,
814 GossipContactList *list)
816 gossip_debug (DEBUG_DOMAIN, "Contact:'%s' removed",
817 gossip_contact_get_name (contact));
819 /* Disconnect signals */
820 g_signal_handlers_disconnect_by_func (contact,
821 G_CALLBACK (contact_list_contact_groups_updated_cb),
823 g_signal_handlers_disconnect_by_func (contact,
824 G_CALLBACK (contact_list_contact_updated_cb),
827 contact_list_remove_contact (list, contact);
831 contact_list_contact_set_active (GossipContactList *list,
832 GossipContact *contact,
834 gboolean set_changed)
836 GossipContactListPriv *priv;
840 priv = GET_PRIV (list);
842 model = GTK_TREE_MODEL (priv->store);
844 iters = contact_list_find_contact (list, contact);
845 for (l = iters; l; l = l->next) {
848 gtk_tree_store_set (priv->store, l->data,
849 COL_IS_ACTIVE, active,
852 gossip_debug (DEBUG_DOMAIN, "Set item %s", active ? "active" : "inactive");
855 path = gtk_tree_model_get_path (model, l->data);
856 gtk_tree_model_row_changed (model, path, l->data);
857 gtk_tree_path_free (path);
861 g_list_foreach (iters, (GFunc)gtk_tree_iter_free, NULL);
866 static ShowActiveData *
867 contact_list_contact_active_new (GossipContactList *list,
868 GossipContact *contact,
871 ShowActiveData *data;
873 g_return_val_if_fail (list != NULL, NULL);
874 g_return_val_if_fail (contact != NULL, NULL);
876 gossip_debug (DEBUG_DOMAIN,
877 "Contact:'%s' now active, and %s be removed",
878 gossip_contact_get_name (contact),
879 remove ? "WILL" : "WILL NOT");
881 data = g_slice_new0 (ShowActiveData);
883 data->list = g_object_ref (list);
884 data->contact = g_object_ref (contact);
886 data->remove = remove;
892 contact_list_contact_active_free (ShowActiveData *data)
894 g_return_if_fail (data != NULL);
896 g_object_unref (data->contact);
897 g_object_unref (data->list);
899 g_slice_free (ShowActiveData, data);
903 contact_list_contact_active_cb (ShowActiveData *data)
905 GossipContactListPriv *priv;
907 g_return_val_if_fail (data != NULL, FALSE);
909 priv = GET_PRIV (data->list);
912 !priv->show_offline &&
913 !gossip_contact_is_online (data->contact)) {
914 gossip_debug (DEBUG_DOMAIN,
915 "Contact:'%s' active timeout, removing item",
916 gossip_contact_get_name (data->contact));
917 contact_list_remove_contact (data->list,
921 gossip_debug (DEBUG_DOMAIN,
922 "Contact:'%s' no longer active",
923 gossip_contact_get_name (data->contact));
924 contact_list_contact_set_active (data->list,
929 contact_list_contact_active_free (data);
935 contact_list_get_parent_group (GtkTreeModel *model,
937 gboolean *path_is_group)
939 GtkTreeIter parent_iter, iter;
943 g_return_val_if_fail (model != NULL, NULL);
944 g_return_val_if_fail (path != NULL, NULL);
945 g_return_val_if_fail (path_is_group != NULL, NULL);
947 if (!gtk_tree_model_get_iter (model, &iter, path)) {
951 gtk_tree_model_get (model, &iter,
952 COL_IS_GROUP, &is_group,
956 if (!gtk_tree_model_iter_parent (model, &parent_iter, &iter)) {
962 gtk_tree_model_get (model, &iter,
963 COL_IS_GROUP, &is_group,
970 *path_is_group = TRUE;
973 gtk_tree_model_get (model, &iter,
981 contact_list_get_group_foreach (GtkTreeModel *model,
989 /* Groups are only at the top level. */
990 if (gtk_tree_path_get_depth (path) != 1) {
994 gtk_tree_model_get (model, iter,
996 COL_IS_GROUP, &is_group,
999 if (is_group && strcmp (str, fg->name) == 0) {
1010 contact_list_get_group (GossipContactList *list,
1012 GtkTreeIter *iter_group_to_set,
1013 GtkTreeIter *iter_separator_to_set,
1016 GossipContactListPriv *priv;
1017 GtkTreeModel *model;
1018 GtkTreeIter iter_group, iter_separator;
1021 priv = GET_PRIV (list);
1023 memset (&fg, 0, sizeof (fg));
1027 model = GTK_TREE_MODEL (priv->store);
1028 gtk_tree_model_foreach (model,
1029 (GtkTreeModelForeachFunc) contact_list_get_group_foreach,
1037 gtk_tree_store_append (priv->store, &iter_group, NULL);
1038 gtk_tree_store_set (priv->store, &iter_group,
1039 COL_PIXBUF_STATUS, NULL,
1042 COL_IS_ACTIVE, FALSE,
1043 COL_IS_SEPARATOR, FALSE,
1046 if (iter_group_to_set) {
1047 *iter_group_to_set = iter_group;
1050 gtk_tree_store_append (priv->store,
1053 gtk_tree_store_set (priv->store, &iter_separator,
1054 COL_IS_SEPARATOR, TRUE,
1057 if (iter_separator_to_set) {
1058 *iter_separator_to_set = iter_separator;
1065 if (iter_group_to_set) {
1066 *iter_group_to_set = fg.iter;
1069 iter_separator = fg.iter;
1071 if (gtk_tree_model_iter_next (model, &iter_separator)) {
1072 gboolean is_separator;
1074 gtk_tree_model_get (model, &iter_separator,
1075 COL_IS_SEPARATOR, &is_separator,
1078 if (is_separator && iter_separator_to_set) {
1079 *iter_separator_to_set = iter_separator;
1086 contact_list_add_contact (GossipContactList *list,
1087 GossipContact *contact)
1089 GossipContactListPriv *priv;
1090 GtkTreeIter iter, iter_group, iter_separator;
1091 GtkTreeModel *model;
1094 priv = GET_PRIV (list);
1096 if (!priv->show_offline && !gossip_contact_is_online (contact)) {
1100 model = gtk_tree_view_get_model (GTK_TREE_VIEW (list));
1102 /* If no groups just add it at the top level. */
1103 groups = gossip_contact_get_groups (contact);
1105 GdkPixbuf *pixbuf_status;
1106 GdkPixbuf *pixbuf_avatar;
1107 gboolean show_avatar = FALSE;
1109 pixbuf_status = gossip_pixbuf_for_contact (contact);
1110 pixbuf_avatar = gossip_pixbuf_avatar_from_contact_scaled (contact, 32, 32);
1112 if (priv->show_avatars && !priv->is_compact) {
1116 gossip_debug (DEBUG_DOMAIN, "");
1117 gossip_debug (DEBUG_DOMAIN,
1118 "vvvvvvvvvvvvvvvv FIXME: Errors may follow below (since filter work) vvvvvvvvvvvvvvvv");
1120 gossip_debug (DEBUG_DOMAIN,
1121 "**** GossipContact:%p, is GObject:%s, is GossipContact:%s, ADDING CONTACT #1",
1123 G_IS_OBJECT (contact) ? "yes" : "no",
1124 GOSSIP_IS_CONTACT (contact) ? "yes" : "no");
1126 gtk_tree_store_append (priv->store, &iter, NULL);
1127 gtk_tree_store_set (priv->store, &iter,
1128 COL_PIXBUF_STATUS, pixbuf_status,
1129 COL_PIXBUF_AVATAR, pixbuf_avatar,
1130 COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
1131 COL_NAME, gossip_contact_get_name (contact),
1132 COL_STATUS, gossip_contact_get_status (contact),
1133 COL_STATUS_VISIBLE, !priv->is_compact,
1134 COL_CONTACT, contact,
1135 COL_IS_GROUP, FALSE,
1136 COL_IS_ACTIVE, FALSE,
1137 COL_IS_ONLINE, gossip_contact_is_online (contact),
1138 COL_IS_SEPARATOR, FALSE,
1141 gossip_debug (DEBUG_DOMAIN,
1142 "^^^^^^^^^^^^^^^^ FIXME: Errors may occur above (since filter work) ^^^^^^^^^^^^^^^^");
1143 gossip_debug (DEBUG_DOMAIN, "");
1145 if (pixbuf_avatar) {
1146 g_object_unref (pixbuf_avatar);
1148 if (pixbuf_status) {
1149 g_object_unref (pixbuf_status);
1153 /* Else add to each group. */
1154 for (l = groups; l; l = l->next) {
1156 GtkTreeIter model_iter_group;
1157 GdkPixbuf *pixbuf_status;
1158 GdkPixbuf *pixbuf_avatar;
1162 gboolean show_avatar = FALSE;
1169 pixbuf_status = gossip_pixbuf_for_contact (contact);
1170 pixbuf_avatar = gossip_pixbuf_avatar_from_contact_scaled (contact, 32, 32);
1172 contact_list_get_group (list, name, &iter_group, &iter_separator, &created);
1174 if (priv->show_avatars && !priv->is_compact) {
1178 gossip_debug (DEBUG_DOMAIN, "");
1179 gossip_debug (DEBUG_DOMAIN,
1180 "vvvvvvvvvvvvvvvv FIXME: Errors may follow below (since filter work) vvvvvvvvvvvvvvvv");
1182 gossip_debug (DEBUG_DOMAIN,
1183 "**** GossipContact:%p, is GObject:%s, is GossipContact:%s, ADDING CONTACT #2",
1185 G_IS_OBJECT (contact) ? "yes" : "no",
1186 GOSSIP_IS_CONTACT (contact) ? "yes" : "no");
1188 gtk_tree_store_insert_after (priv->store, &iter, &iter_group, NULL);
1189 gtk_tree_store_set (priv->store, &iter,
1190 COL_PIXBUF_STATUS, pixbuf_status,
1191 COL_PIXBUF_AVATAR, pixbuf_avatar,
1192 COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
1193 COL_NAME, gossip_contact_get_name (contact),
1194 COL_STATUS, gossip_contact_get_status (contact),
1195 COL_STATUS_VISIBLE, !priv->is_compact,
1196 COL_CONTACT, contact,
1197 COL_IS_GROUP, FALSE,
1198 COL_IS_ACTIVE, FALSE,
1199 COL_IS_ONLINE, gossip_contact_is_online (contact),
1200 COL_IS_SEPARATOR, FALSE,
1203 gossip_debug (DEBUG_DOMAIN,
1204 "^^^^^^^^^^^^^^^^ FIXME: Errors may occur above (since filter work) ^^^^^^^^^^^^^^^^");
1205 gossip_debug (DEBUG_DOMAIN, "");
1207 if (pixbuf_avatar) {
1208 g_object_unref (pixbuf_avatar);
1210 if (pixbuf_status) {
1211 g_object_unref (pixbuf_status);
1218 found = gtk_tree_model_filter_convert_child_iter_to_iter (GTK_TREE_MODEL_FILTER (priv->filter),
1225 path = gtk_tree_model_get_path (model, &model_iter_group);
1230 if (gossip_contact_group_get_expanded (name)) {
1231 g_signal_handlers_block_by_func (list,
1232 contact_list_row_expand_or_collapse_cb,
1233 GINT_TO_POINTER (TRUE));
1234 gtk_tree_view_expand_row (GTK_TREE_VIEW (list), path, TRUE);
1235 g_signal_handlers_unblock_by_func (list,
1236 contact_list_row_expand_or_collapse_cb,
1237 GINT_TO_POINTER (TRUE));
1239 g_signal_handlers_block_by_func (list,
1240 contact_list_row_expand_or_collapse_cb,
1241 GINT_TO_POINTER (FALSE));
1242 gtk_tree_view_collapse_row (GTK_TREE_VIEW (list), path);
1243 g_signal_handlers_unblock_by_func (list,
1244 contact_list_row_expand_or_collapse_cb,
1245 GINT_TO_POINTER (FALSE));
1248 gtk_tree_path_free (path);
1253 contact_list_remove_contact (GossipContactList *list,
1254 GossipContact *contact)
1256 GossipContactListPriv *priv;
1257 GtkTreeModel *model;
1260 priv = GET_PRIV (list);
1262 iters = contact_list_find_contact (list, contact);
1267 /* Clean up model */
1268 model = GTK_TREE_MODEL (priv->store);
1270 for (l = iters; l; l = l->next) {
1273 /* NOTE: it is only <= 2 here because we have
1274 * separators after the group name, otherwise it
1277 if (gtk_tree_model_iter_parent (model, &parent, l->data) &&
1278 gtk_tree_model_iter_n_children (model, &parent) <= 2) {
1279 gtk_tree_store_remove (priv->store, &parent);
1281 gtk_tree_store_remove (priv->store, l->data);
1285 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
1286 g_list_free (iters);
1290 contact_list_create_model (GossipContactList *list)
1292 GossipContactListPriv *priv;
1293 GtkTreeModel *model;
1295 priv = GET_PRIV (list);
1298 g_object_unref (priv->store);
1302 g_object_unref (priv->filter);
1305 priv->store = gtk_tree_store_new (COL_COUNT,
1306 GDK_TYPE_PIXBUF, /* Status pixbuf */
1307 GDK_TYPE_PIXBUF, /* Avatar pixbuf */
1308 G_TYPE_BOOLEAN, /* Avatar pixbuf visible */
1309 G_TYPE_STRING, /* Name */
1310 G_TYPE_STRING, /* Status string */
1311 G_TYPE_BOOLEAN, /* Show status */
1312 GOSSIP_TYPE_CONTACT, /* Contact type */
1313 G_TYPE_BOOLEAN, /* Is group */
1314 G_TYPE_BOOLEAN, /* Is active */
1315 G_TYPE_BOOLEAN, /* Is online */
1316 G_TYPE_BOOLEAN); /* Is separator */
1318 /* Save normal model */
1319 model = GTK_TREE_MODEL (priv->store);
1321 /* Set up sorting */
1322 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (model),
1324 contact_list_sort_func,
1327 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (model),
1329 GTK_SORT_ASCENDING);
1332 priv->filter = gtk_tree_model_filter_new (model, NULL);
1334 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (priv->filter),
1335 (GtkTreeModelFilterVisibleFunc)
1336 contact_list_filter_func,
1339 gtk_tree_view_set_model (GTK_TREE_VIEW (list), priv->filter);
1343 contact_list_search_equal_func (GtkTreeModel *model,
1347 gpointer search_data)
1349 gchar *name, *name_folded;
1357 gtk_tree_model_get (model, iter, COL_NAME, &name, -1);
1363 name_folded = g_utf8_casefold (name, -1);
1364 key_folded = g_utf8_casefold (key, -1);
1366 if (name_folded && key_folded &&
1367 strstr (name_folded, key_folded)) {
1374 g_free (name_folded);
1375 g_free (key_folded);
1381 contact_list_setup_view (GossipContactList *list)
1383 GtkCellRenderer *cell;
1384 GtkTreeViewColumn *col;
1387 gtk_tree_view_set_search_equal_func (GTK_TREE_VIEW (list),
1388 contact_list_search_equal_func,
1393 "headers-visible", FALSE,
1394 "reorderable", TRUE,
1395 "show-expanders", FALSE,
1398 col = gtk_tree_view_column_new ();
1401 cell = gtk_cell_renderer_pixbuf_new ();
1402 gtk_tree_view_column_pack_start (col, cell, FALSE);
1403 gtk_tree_view_column_set_cell_data_func (
1405 (GtkTreeCellDataFunc) contact_list_pixbuf_cell_data_func,
1415 cell = gossip_cell_renderer_text_new ();
1416 gtk_tree_view_column_pack_start (col, cell, TRUE);
1417 gtk_tree_view_column_set_cell_data_func (
1419 (GtkTreeCellDataFunc) contact_list_text_cell_data_func,
1422 gtk_tree_view_column_add_attribute (col, cell,
1424 gtk_tree_view_column_add_attribute (col, cell,
1425 "status", COL_STATUS);
1426 gtk_tree_view_column_add_attribute (col, cell,
1427 "is_group", COL_IS_GROUP);
1430 cell = gtk_cell_renderer_pixbuf_new ();
1431 gtk_tree_view_column_pack_start (col, cell, FALSE);
1432 gtk_tree_view_column_set_cell_data_func (
1434 (GtkTreeCellDataFunc) contact_list_avatar_cell_data_func,
1446 cell = gossip_cell_renderer_expander_new ();
1447 gtk_tree_view_column_pack_end (col, cell, FALSE);
1448 gtk_tree_view_column_set_cell_data_func (
1450 (GtkTreeCellDataFunc) contact_list_expander_cell_data_func,
1453 /* Actually add the column now we have added all cell renderers */
1454 gtk_tree_view_append_column (GTK_TREE_VIEW (list), col);
1457 for (i = 0; i < G_N_ELEMENTS (drag_types_dest); ++i) {
1458 drag_atoms_dest[i] = gdk_atom_intern (drag_types_dest[i].target,
1462 for (i = 0; i < G_N_ELEMENTS (drag_types_source); ++i) {
1463 drag_atoms_source[i] = gdk_atom_intern (drag_types_source[i].target,
1467 /* Note: We support the COPY action too, but need to make the
1468 * MOVE action the default.
1470 gtk_drag_source_set (GTK_WIDGET (list),
1473 G_N_ELEMENTS (drag_types_source),
1476 gtk_drag_dest_set (GTK_WIDGET (list),
1477 GTK_DEST_DEFAULT_ALL,
1479 G_N_ELEMENTS (drag_types_dest),
1480 GDK_ACTION_MOVE | GDK_ACTION_LINK);
1482 g_signal_connect (GTK_WIDGET (list),
1483 "drag-data-received",
1484 G_CALLBACK (contact_list_drag_data_received),
1487 /* FIXME: noticed but when you drag the row over the treeview
1488 * fast, it seems to stop redrawing itself, if we don't
1489 * connect this signal, all is fine.
1491 g_signal_connect (GTK_WIDGET (list),
1493 G_CALLBACK (contact_list_drag_motion),
1496 g_signal_connect (GTK_WIDGET (list),
1498 G_CALLBACK (contact_list_drag_begin),
1500 g_signal_connect (GTK_WIDGET (list),
1502 G_CALLBACK (contact_list_drag_data_get),
1504 g_signal_connect (GTK_WIDGET (list),
1506 G_CALLBACK (contact_list_drag_end),
1511 contact_list_drag_data_received (GtkWidget *widget,
1512 GdkDragContext *context,
1515 GtkSelectionData *selection,
1520 GossipContactListPriv *priv;
1521 GtkTreeModel *model;
1523 GtkTreeViewDropPosition position;
1524 GossipContact *contact;
1529 gboolean drag_success = TRUE;
1530 gboolean drag_del = FALSE;
1532 priv = GET_PRIV (widget);
1534 id = (const gchar*) selection->data;
1535 gossip_debug (DEBUG_DOMAIN, "Received %s%s drag & drop contact from roster with id:'%s'",
1536 context->action == GDK_ACTION_MOVE ? "move" : "",
1537 context->action == GDK_ACTION_COPY ? "copy" : "",
1540 /* FIXME: This is ambigous, an id can come from multiple accounts */
1541 contact = empathy_contact_manager_find (priv->manager, id);
1543 gossip_debug (DEBUG_DOMAIN, "No contact found associated with drag & drop");
1547 groups = gossip_contact_get_groups (contact);
1549 is_row = gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget),
1556 if (g_list_length (groups) != 1) {
1557 /* if they have dragged a contact out of a
1558 * group then we would set the contact to have
1559 * NO groups but only if they were ONE group
1560 * to begin with - should we do this
1561 * regardless to how many groups they are in
1562 * already or not at all?
1567 gossip_contact_set_groups (contact, NULL);
1569 GList *l, *new_groups;
1573 model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
1574 name = contact_list_get_parent_group (model, path, &is_group);
1576 if (groups && name &&
1577 g_list_find_custom (groups, name, (GCompareFunc)strcmp)) {
1582 /* Get source group information. */
1583 priv = GET_PRIV (widget);
1584 if (!priv->drag_row) {
1589 path = gtk_tree_row_reference_get_path (priv->drag_row);
1595 old_group = contact_list_get_parent_group (model, path, &is_group);
1596 gtk_tree_path_free (path);
1598 if (!name && old_group && GDK_ACTION_MOVE) {
1599 drag_success = FALSE;
1602 if (context->action == GDK_ACTION_MOVE) {
1606 /* Create new groups GList. */
1607 for (l = groups, new_groups = NULL; l && drag_success; l = l->next) {
1611 if (context->action == GDK_ACTION_MOVE &&
1612 old_group != NULL &&
1613 strcmp (str, old_group) == 0) {
1621 new_groups = g_list_append (new_groups, g_strdup (str));
1626 new_groups = g_list_append (new_groups, name);
1628 gossip_contact_set_groups (contact, new_groups);
1634 gtk_drag_finish (context, drag_success, drag_del, GDK_CURRENT_TIME);
1638 contact_list_drag_motion (GtkWidget *widget,
1639 GdkDragContext *context,
1645 static DragMotionData *dm = NULL;
1648 gboolean is_different = FALSE;
1649 gboolean cleanup = TRUE;
1651 is_row = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
1662 cleanup &= (dm && gtk_tree_path_compare (dm->path, path) != 0);
1663 is_different = (!dm || (dm && gtk_tree_path_compare (dm->path, path) != 0));
1668 if (!is_different && !cleanup) {
1673 gtk_tree_path_free (dm->path);
1674 if (dm->timeout_id) {
1675 g_source_remove (dm->timeout_id);
1683 if (!gtk_tree_view_row_expanded (GTK_TREE_VIEW (widget), path)) {
1684 dm = g_new0 (DragMotionData, 1);
1686 dm->list = GOSSIP_CONTACT_LIST (widget);
1687 dm->path = gtk_tree_path_copy (path);
1689 dm->timeout_id = g_timeout_add (
1691 (GSourceFunc) contact_list_drag_motion_cb,
1699 contact_list_drag_motion_cb (DragMotionData *data)
1701 gtk_tree_view_expand_row (GTK_TREE_VIEW (data->list),
1705 data->timeout_id = 0;
1711 contact_list_drag_begin (GtkWidget *widget,
1712 GdkDragContext *context,
1715 GossipContactListPriv *priv;
1716 GtkTreeSelection *selection;
1717 GtkTreeModel *model;
1721 priv = GET_PRIV (widget);
1723 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (widget));
1724 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
1728 path = gtk_tree_model_get_path (model, &iter);
1729 priv->drag_row = gtk_tree_row_reference_new (model, path);
1730 gtk_tree_path_free (path);
1734 contact_list_drag_data_get (GtkWidget *widget,
1735 GdkDragContext *context,
1736 GtkSelectionData *selection,
1741 GossipContactListPriv *priv;
1742 GtkTreePath *src_path;
1744 GtkTreeModel *model;
1745 GossipContact *contact;
1748 priv = GET_PRIV (widget);
1750 model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
1751 if (!priv->drag_row) {
1755 src_path = gtk_tree_row_reference_get_path (priv->drag_row);
1760 if (!gtk_tree_model_get_iter (model, &iter, src_path)) {
1761 gtk_tree_path_free (src_path);
1765 gtk_tree_path_free (src_path);
1767 contact = gossip_contact_list_get_selected (GOSSIP_CONTACT_LIST (widget));
1772 id = gossip_contact_get_id (contact);
1773 g_object_unref (contact);
1776 case DND_DRAG_TYPE_CONTACT_ID:
1777 gtk_selection_data_set (selection, drag_atoms_source[info], 8,
1778 (guchar*)id, strlen (id) + 1);
1787 contact_list_drag_end (GtkWidget *widget,
1788 GdkDragContext *context,
1791 GossipContactListPriv *priv;
1793 priv = GET_PRIV (widget);
1795 if (priv->drag_row) {
1796 gtk_tree_row_reference_free (priv->drag_row);
1797 priv->drag_row = NULL;
1802 contact_list_cell_set_background (GossipContactList *list,
1803 GtkCellRenderer *cell,
1810 g_return_if_fail (list != NULL);
1811 g_return_if_fail (cell != NULL);
1813 style = gtk_widget_get_style (GTK_WIDGET (list));
1817 color = style->bg[GTK_STATE_SELECTED];
1819 /* Here we take the current theme colour and add it to
1820 * the colour for white and average the two. This
1821 * gives a colour which is inline with the theme but
1824 color.red = (color.red + (style->white).red) / 2;
1825 color.green = (color.green + (style->white).green) / 2;
1826 color.blue = (color.blue + (style->white).blue) / 2;
1829 "cell-background-gdk", &color,
1833 "cell-background-gdk", NULL,
1838 "cell-background-gdk", NULL,
1841 gint color_sum_normal;
1842 gint color_sum_selected;
1844 color = style->base[GTK_STATE_SELECTED];
1845 color_sum_normal = color.red+color.green+color.blue;
1846 color = style->base[GTK_STATE_NORMAL];
1847 color_sum_selected = color.red+color.green+color.blue;
1848 color = style->text_aa[GTK_STATE_INSENSITIVE];
1850 if (color_sum_normal < color_sum_selected) {
1851 /* Found a light theme */
1852 color.red = (color.red + (style->white).red) / 2;
1853 color.green = (color.green + (style->white).green) / 2;
1854 color.blue = (color.blue + (style->white).blue) / 2;
1856 /* Found a dark theme */
1857 color.red = (color.red + (style->black).red) / 2;
1858 color.green = (color.green + (style->black).green) / 2;
1859 color.blue = (color.blue + (style->black).blue) / 2;
1863 "cell-background-gdk", &color,
1870 contact_list_pixbuf_cell_data_func (GtkTreeViewColumn *tree_column,
1871 GtkCellRenderer *cell,
1872 GtkTreeModel *model,
1874 GossipContactList *list)
1880 gtk_tree_model_get (model, iter,
1881 COL_IS_GROUP, &is_group,
1882 COL_IS_ACTIVE, &is_active,
1883 COL_PIXBUF_STATUS, &pixbuf,
1887 "visible", !is_group,
1892 g_object_unref (pixbuf);
1895 contact_list_cell_set_background (list, cell, is_group, is_active);
1899 contact_list_avatar_cell_data_func (GtkTreeViewColumn *tree_column,
1900 GtkCellRenderer *cell,
1901 GtkTreeModel *model,
1903 GossipContactList *list)
1906 gboolean show_avatar;
1910 gtk_tree_model_get (model, iter,
1911 COL_PIXBUF_AVATAR, &pixbuf,
1912 COL_PIXBUF_AVATAR_VISIBLE, &show_avatar,
1913 COL_IS_GROUP, &is_group,
1914 COL_IS_ACTIVE, &is_active,
1918 "visible", !is_group && show_avatar,
1923 g_object_unref (pixbuf);
1926 contact_list_cell_set_background (list, cell, is_group, is_active);
1930 contact_list_text_cell_data_func (GtkTreeViewColumn *tree_column,
1931 GtkCellRenderer *cell,
1932 GtkTreeModel *model,
1934 GossipContactList *list)
1938 gboolean show_status;
1940 gtk_tree_model_get (model, iter,
1941 COL_IS_GROUP, &is_group,
1942 COL_IS_ACTIVE, &is_active,
1943 COL_STATUS_VISIBLE, &show_status,
1947 "show-status", show_status,
1950 contact_list_cell_set_background (list, cell, is_group, is_active);
1954 contact_list_expander_cell_data_func (GtkTreeViewColumn *column,
1955 GtkCellRenderer *cell,
1956 GtkTreeModel *model,
1958 GossipContactList *list)
1963 gtk_tree_model_get (model, iter,
1964 COL_IS_GROUP, &is_group,
1965 COL_IS_ACTIVE, &is_active,
1968 if (gtk_tree_model_iter_has_child (model, iter)) {
1970 gboolean row_expanded;
1972 path = gtk_tree_model_get_path (model, iter);
1973 row_expanded = gtk_tree_view_row_expanded (GTK_TREE_VIEW (column->tree_view), path);
1974 gtk_tree_path_free (path);
1978 "expander-style", row_expanded ? GTK_EXPANDER_EXPANDED : GTK_EXPANDER_COLLAPSED,
1981 g_object_set (cell, "visible", FALSE, NULL);
1984 contact_list_cell_set_background (list, cell, is_group, is_active);
1988 contact_list_get_contact_menu (GossipContactList *list,
1989 gboolean can_send_file,
1990 gboolean can_show_log)
1992 GossipContactListPriv *priv;
1996 priv = GET_PRIV (list);
1998 /* Sort out sensitive items */
1999 action = gtk_ui_manager_get_action (priv->ui, "/Contact/Log");
2000 gtk_action_set_sensitive (action, can_show_log);
2002 action = gtk_ui_manager_get_action (priv->ui, "/Contact/SendFile");
2003 gtk_action_set_visible (action, can_send_file);
2005 widget = gtk_ui_manager_get_widget (priv->ui, "/Contact");
2011 gossip_contact_list_get_group_menu (GossipContactList *list)
2013 GossipContactListPriv *priv;
2016 g_return_val_if_fail (GOSSIP_IS_CONTACT_LIST (list), NULL);
2018 priv = GET_PRIV (list);
2020 widget = gtk_ui_manager_get_widget (priv->ui, "/Group");
2026 gossip_contact_list_get_contact_menu (GossipContactList *list,
2027 GossipContact *contact)
2030 gboolean can_show_log;
2031 gboolean can_send_file;
2033 g_return_val_if_fail (GOSSIP_IS_CONTACT_LIST (list), NULL);
2034 g_return_val_if_fail (GOSSIP_IS_CONTACT (contact), NULL);
2036 can_show_log = FALSE; /* FIXME: gossip_log_exists_for_contact (contact); */
2037 can_send_file = FALSE;
2039 menu = contact_list_get_contact_menu (list,
2046 contact_list_button_press_event_cb (GossipContactList *list,
2047 GdkEventButton *event,
2050 GossipContactListPriv *priv;
2051 GossipContact *contact;
2053 GtkTreeSelection *selection;
2054 GtkTreeModel *model;
2056 gboolean row_exists;
2059 if (event->button != 3) {
2063 priv = GET_PRIV (list);
2065 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (list));
2066 model = GTK_TREE_MODEL (priv->store);
2068 gtk_widget_grab_focus (GTK_WIDGET (list));
2070 row_exists = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (list),
2078 gtk_tree_selection_unselect_all (selection);
2079 gtk_tree_selection_select_path (selection, path);
2081 gtk_tree_model_get_iter (model, &iter, path);
2082 gtk_tree_path_free (path);
2084 gtk_tree_model_get (model, &iter, COL_CONTACT, &contact, -1);
2087 menu = gossip_contact_list_get_contact_menu (list, contact);
2088 g_object_unref (contact);
2090 menu = gossip_contact_list_get_group_menu (list);
2097 gtk_widget_show (menu);
2099 gtk_menu_popup (GTK_MENU (menu),
2100 NULL, NULL, NULL, NULL,
2101 event->button, event->time);
2107 contact_list_row_activated_cb (GossipContactList *list,
2109 GtkTreeViewColumn *col,
2112 GossipContact *contact;
2114 GtkTreeModel *model;
2117 view = GTK_TREE_VIEW (list);
2118 model = gtk_tree_view_get_model (view);
2120 gtk_tree_model_get_iter (model, &iter, path);
2121 gtk_tree_model_get (model, &iter, COL_CONTACT, &contact, -1);
2124 contact_list_action_activated (list, contact);
2125 g_object_unref (contact);
2130 contact_list_row_expand_or_collapse_cb (GossipContactList *list,
2135 GtkTreeModel *model;
2139 model = gtk_tree_view_get_model (GTK_TREE_VIEW (list));
2141 gtk_tree_model_get (model, iter,
2145 expanded = GPOINTER_TO_INT (user_data);
2146 gossip_contact_group_set_expanded (name, expanded);
2152 contact_list_sort_func (GtkTreeModel *model,
2153 GtkTreeIter *iter_a,
2154 GtkTreeIter *iter_b,
2157 gchar *name_a, *name_b;
2158 GossipContact *contact_a, *contact_b;
2159 gboolean is_separator_a, is_separator_b;
2162 gtk_tree_model_get (model, iter_a,
2164 COL_CONTACT, &contact_a,
2165 COL_IS_SEPARATOR, &is_separator_a,
2167 gtk_tree_model_get (model, iter_b,
2169 COL_CONTACT, &contact_b,
2170 COL_IS_SEPARATOR, &is_separator_b,
2173 /* If contact is NULL it means it's a group. */
2175 if (is_separator_a || is_separator_b) {
2176 if (is_separator_a) {
2178 } else if (is_separator_b) {
2181 } else if (!contact_a && contact_b) {
2183 } else if (contact_a && !contact_b) {
2186 ret_val = g_utf8_collate (name_a, name_b);
2193 g_object_unref (contact_a);
2197 g_object_unref (contact_b);
2204 contact_list_filter_show_contact (GossipContact *contact,
2205 const gchar *filter)
2210 /* Check contact id */
2211 str = g_utf8_casefold (gossip_contact_get_id (contact), -1);
2212 visible = G_STR_EMPTY (str) || strstr (str, filter);
2219 /* Check contact name */
2220 str = g_utf8_casefold (gossip_contact_get_name (contact), -1);
2221 visible = G_STR_EMPTY (str) || strstr (str, filter);
2228 contact_list_filter_show_group (GossipContactList *list,
2230 const gchar *filter)
2232 GossipContactListPriv *priv;
2233 GList *contacts, *l;
2235 gboolean show_group = FALSE;
2237 priv = GET_PRIV (list);
2239 str = g_utf8_casefold (group, -1);
2244 /* If the filter is the partially the group name, we show the
2247 if (strstr (str, filter)) {
2252 /* At this point, we need to check in advance if this
2253 * group should be shown because a contact we want to
2254 * show exists in it.
2256 contacts = empathy_contact_manager_get_contacts (priv->manager);
2257 for (l = contacts; l && !show_group; l = l->next) {
2258 if (!gossip_contact_is_in_group (l->data, group)) {
2262 if (contact_list_filter_show_contact (l->data, filter)) {
2266 g_list_foreach (contacts, (GFunc) g_object_unref, NULL);
2267 g_list_free (contacts);
2274 contact_list_filter_func (GtkTreeModel *model,
2276 GossipContactList *list)
2278 GossipContactListPriv *priv;
2280 gboolean is_separator;
2281 gboolean visible = TRUE;
2283 priv = GET_PRIV (list);
2285 if (G_STR_EMPTY (priv->filter_text)) {
2289 /* Check to see if iter matches any group names */
2290 gtk_tree_model_get (model, iter,
2291 COL_IS_GROUP, &is_group,
2292 COL_IS_SEPARATOR, &is_separator,
2298 gtk_tree_model_get (model, iter, COL_NAME, &name, -1);
2299 visible &= contact_list_filter_show_group (list,
2303 } else if (is_separator) {
2304 /* Do nothing here */
2306 GossipContact *contact;
2308 /* Check contact id */
2309 gtk_tree_model_get (model, iter, COL_CONTACT, &contact, -1);
2310 visible &= contact_list_filter_show_contact (contact,
2312 g_object_unref (contact);
2319 contact_list_iter_equal_contact (GtkTreeModel *model,
2321 GossipContact *contact)
2326 gtk_tree_model_get (model, iter,
2334 equal = (c == contact);
2341 contact_list_find_contact_foreach (GtkTreeModel *model,
2346 if (contact_list_iter_equal_contact (model, iter, fc->contact)) {
2348 fc->iters = g_list_append (fc->iters, gtk_tree_iter_copy (iter));
2351 /* We want to find ALL contacts that match, this means if we
2352 * have the same contact in 3 groups, all iters should be
2359 contact_list_find_contact (GossipContactList *list,
2360 GossipContact *contact)
2362 GossipContactListPriv *priv;
2363 GtkTreeModel *model;
2367 priv = GET_PRIV (list);
2369 memset (&fc, 0, sizeof (fc));
2371 fc.contact = contact;
2373 model = GTK_TREE_MODEL (priv->store);
2374 gtk_tree_model_foreach (model,
2375 (GtkTreeModelForeachFunc) contact_list_find_contact_foreach,
2386 contact_list_action_cb (GtkAction *action,
2387 GossipContactList *list)
2389 GossipContact *contact;
2393 name = gtk_action_get_name (action);
2398 gossip_debug (DEBUG_DOMAIN, "Action:'%s' activated", name);
2400 contact = gossip_contact_list_get_selected (list);
2401 group = gossip_contact_list_get_selected_group (list);
2403 if (contact && strcmp (name, "Chat") == 0) {
2404 contact_list_action_activated (list, contact);
2406 else if (contact && strcmp (name, "Information") == 0) {
2408 else if (contact && strcmp (name, "Edit") == 0) {
2410 else if (contact && strcmp (name, "Remove") == 0) {
2412 else if (contact && strcmp (name, "Invite") == 0) {
2414 else if (contact && strcmp (name, "SendFile") == 0) {
2416 else if (contact && strcmp (name, "Log") == 0) {
2418 else if (group && strcmp (name, "Rename") == 0) {
2423 g_object_unref (contact);
2428 contact_list_action_activated (GossipContactList *list,
2429 GossipContact *contact)
2433 mc = mission_control_new (tp_get_bus ());
2434 mission_control_request_channel (mc,
2435 gossip_contact_get_account (contact),
2436 TP_IFACE_CHANNEL_TYPE_TEXT,
2437 gossip_contact_get_handle (contact),
2438 TP_HANDLE_TYPE_CONTACT,
2440 g_object_unref (mc);
2444 contact_list_update_list_mode_foreach (GtkTreeModel *model,
2447 GossipContactList *list)
2449 GossipContactListPriv *priv;
2450 gboolean show_avatar = FALSE;
2452 priv = GET_PRIV (list);
2454 if (priv->show_avatars && !priv->is_compact) {
2458 gtk_tree_store_set (priv->store, iter,
2459 COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
2460 COL_STATUS_VISIBLE, !priv->is_compact,
2467 gossip_contact_list_new (void)
2469 return g_object_new (GOSSIP_TYPE_CONTACT_LIST, NULL);
2473 gossip_contact_list_get_selected (GossipContactList *list)
2475 GossipContactListPriv *priv;
2476 GtkTreeSelection *selection;
2478 GtkTreeModel *model;
2479 GossipContact *contact;
2481 g_return_val_if_fail (GOSSIP_IS_CONTACT_LIST (list), NULL);
2483 priv = GET_PRIV (list);
2485 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (list));
2486 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
2490 gtk_tree_model_get (model, &iter, COL_CONTACT, &contact, -1);
2496 gossip_contact_list_get_selected_group (GossipContactList *list)
2498 GossipContactListPriv *priv;
2499 GtkTreeSelection *selection;
2501 GtkTreeModel *model;
2505 g_return_val_if_fail (GOSSIP_IS_CONTACT_LIST (list), NULL);
2507 priv = GET_PRIV (list);
2509 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (list));
2510 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
2514 gtk_tree_model_get (model, &iter,
2515 COL_IS_GROUP, &is_group,
2528 gossip_contact_list_get_show_offline (GossipContactList *list)
2530 GossipContactListPriv *priv;
2532 g_return_val_if_fail (GOSSIP_IS_CONTACT_LIST (list), FALSE);
2534 priv = GET_PRIV (list);
2536 return priv->show_offline;
2540 gossip_contact_list_get_show_avatars (GossipContactList *list)
2542 GossipContactListPriv *priv;
2544 g_return_val_if_fail (GOSSIP_IS_CONTACT_LIST (list), TRUE);
2546 priv = GET_PRIV (list);
2548 return priv->show_avatars;
2552 gossip_contact_list_get_is_compact (GossipContactList *list)
2554 GossipContactListPriv *priv;
2556 g_return_val_if_fail (GOSSIP_IS_CONTACT_LIST (list), TRUE);
2558 priv = GET_PRIV (list);
2560 return priv->is_compact;
2564 gossip_contact_list_set_show_offline (GossipContactList *list,
2565 gboolean show_offline)
2567 GossipContactListPriv *priv;
2568 GList *contacts, *l;
2569 gboolean show_active;
2571 g_return_if_fail (GOSSIP_IS_CONTACT_LIST (list));
2573 priv = GET_PRIV (list);
2575 priv->show_offline = show_offline;
2576 show_active = priv->show_active;
2578 /* Disable temporarily. */
2579 priv->show_active = FALSE;
2581 contacts = empathy_contact_manager_get_contacts (priv->manager);
2582 for (l = contacts; l; l = l->next) {
2583 GossipContact *contact;
2585 contact = GOSSIP_CONTACT (l->data);
2587 contact_list_contact_update (list, contact);
2589 g_object_unref (contact);
2591 g_list_free (contacts);
2593 /* Restore to original setting. */
2594 priv->show_active = show_active;
2598 gossip_contact_list_set_show_avatars (GossipContactList *list,
2599 gboolean show_avatars)
2601 GossipContactListPriv *priv;
2602 GtkTreeModel *model;
2604 g_return_if_fail (GOSSIP_IS_CONTACT_LIST (list));
2606 priv = GET_PRIV (list);
2608 priv->show_avatars = show_avatars;
2610 model = GTK_TREE_MODEL (priv->store);
2612 gtk_tree_model_foreach (model,
2613 (GtkTreeModelForeachFunc)
2614 contact_list_update_list_mode_foreach,
2619 gossip_contact_list_set_is_compact (GossipContactList *list,
2620 gboolean is_compact)
2622 GossipContactListPriv *priv;
2623 GtkTreeModel *model;
2625 g_return_if_fail (GOSSIP_IS_CONTACT_LIST (list));
2627 priv = GET_PRIV (list);
2629 priv->is_compact = is_compact;
2631 model = GTK_TREE_MODEL (priv->store);
2633 gtk_tree_model_foreach (model,
2634 (GtkTreeModelForeachFunc)
2635 contact_list_update_list_mode_foreach,
2640 gossip_contact_list_set_filter (GossipContactList *list,
2641 const gchar *filter)
2643 GossipContactListPriv *priv;
2645 g_return_if_fail (GOSSIP_IS_CONTACT_LIST (list));
2647 priv = GET_PRIV (list);
2649 g_free (priv->filter_text);
2651 priv->filter_text = g_utf8_casefold (filter, -1);
2653 priv->filter_text = NULL;
2656 gossip_debug (DEBUG_DOMAIN, "Refiltering with filter:'%s' (case folded)", filter);
2657 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter));