1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2005-2007 Imendio AB
4 * Copyright (C) 2007 Collabora Ltd.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
21 * Authors: Mikael Hallendal <micke@imendio.com>
22 * Martyn Russell <martyn@imendio.com>
23 * Xavier Claessens <xclaesse@gmail.com>
30 #include <glib/gi18n.h>
32 #include <glade/glade.h>
34 #include <libmissioncontrol/mc-account.h>
35 #include <libmissioncontrol/mission-control.h>
37 #include <libempathy/empathy-contact-manager.h>
38 #include <libempathy/empathy-contact-list.h>
39 #include <libempathy/gossip-debug.h>
40 #include <libempathy/gossip-utils.h>
41 #include <libempathy/empathy-marshal.h>
43 #include "gossip-contact-list-view.h"
44 #include "gossip-contact-list-store.h"
45 #include "empathy-images.h"
46 #include "gossip-contact-groups.h"
47 #include "gossip-cell-renderer-expander.h"
48 #include "gossip-cell-renderer-text.h"
49 #include "gossip-ui-utils.h"
50 #include "empathy-contact-dialogs.h"
51 //#include "gossip-chat-invite.h"
52 //#include "gossip-ft-window.h"
53 //#include "gossip-log-window.h"
55 #define DEBUG_DOMAIN "ContactListView"
57 /* Flashing delay for icons (milliseconds). */
58 #define FLASH_TIMEOUT 500
60 /* Active users are those which have recently changed state
61 * (e.g. online, offline or from normal to a busy state).
64 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GOSSIP_TYPE_CONTACT_LIST_VIEW, GossipContactListViewPriv))
66 struct _GossipContactListViewPriv {
67 GossipContactListStore *store;
69 GtkTreeRowReference *drag_row;
73 GossipContactListViewDragReceivedFunc drag_received;
74 gpointer drag_received_data;
78 GossipContactListView *view;
84 GossipContactListView *view;
85 GossipContact *contact;
89 static void gossip_contact_list_view_class_init (GossipContactListViewClass *klass);
90 static void gossip_contact_list_view_init (GossipContactListView *list);
91 static void contact_list_view_finalize (GObject *object);
92 static void contact_list_view_get_property (GObject *object,
96 static void contact_list_view_set_property (GObject *object,
100 static void contact_list_view_setup (GossipContactListView *view);
101 static void contact_list_view_row_has_child_toggled_cb (GtkTreeModel *model,
104 GossipContactListView *view);
105 static void contact_list_view_contact_received (GossipContactListView *view,
106 GossipContact *contact,
107 GdkDragAction action,
108 const gchar *old_group,
109 const gchar *new_group);
110 static void contact_list_view_drag_data_received (GtkWidget *widget,
111 GdkDragContext *context,
114 GtkSelectionData *selection,
118 static gboolean contact_list_view_drag_motion (GtkWidget *widget,
119 GdkDragContext *context,
124 static gboolean contact_list_view_drag_motion_cb (DragMotionData *data);
125 static void contact_list_view_drag_begin (GtkWidget *widget,
126 GdkDragContext *context,
128 static void contact_list_view_drag_data_get (GtkWidget *widget,
129 GdkDragContext *context,
130 GtkSelectionData *selection,
134 static void contact_list_view_drag_end (GtkWidget *widget,
135 GdkDragContext *context,
137 static void contact_list_view_cell_set_background (GossipContactListView *view,
138 GtkCellRenderer *cell,
141 static void contact_list_view_pixbuf_cell_data_func (GtkTreeViewColumn *tree_column,
142 GtkCellRenderer *cell,
145 GossipContactListView *view);
146 static void contact_list_view_avatar_cell_data_func (GtkTreeViewColumn *tree_column,
147 GtkCellRenderer *cell,
150 GossipContactListView *view);
151 static void contact_list_view_text_cell_data_func (GtkTreeViewColumn *tree_column,
152 GtkCellRenderer *cell,
155 GossipContactListView *view);
156 static void contact_list_view_expander_cell_data_func (GtkTreeViewColumn *column,
157 GtkCellRenderer *cell,
160 GossipContactListView *view);
161 static GtkWidget * contact_list_view_get_contact_menu (GossipContactListView *view,
162 gboolean can_send_file,
163 gboolean can_show_log);
164 static gboolean contact_list_view_button_press_event_cb (GossipContactListView *view,
165 GdkEventButton *event,
167 static void contact_list_view_row_activated_cb (GossipContactListView *view,
169 GtkTreeViewColumn *col,
171 static void contact_list_view_row_expand_or_collapse_cb (GossipContactListView *view,
175 static gboolean contact_list_view_filter_show_contact (GossipContact *contact,
176 const gchar *filter);
177 static gboolean contact_list_view_filter_show_group (GossipContactListView *view,
179 const gchar *filter);
180 static gboolean contact_list_view_filter_func (GtkTreeModel *model,
182 GossipContactListView *view);
183 static void contact_list_view_action_cb (GtkAction *action,
184 GossipContactListView *view);
185 static void contact_list_view_action_activated (GossipContactListView *view,
186 GossipContact *contact);
193 static const GtkActionEntry entries[] = {
194 { "ContactMenu", NULL,
195 N_("_Contact"), NULL, NULL,
199 N_("_Group"),NULL, NULL,
202 { "Chat", EMPATHY_IMAGE_MESSAGE,
203 N_("_Chat"), NULL, N_("Chat with contact"),
204 G_CALLBACK (contact_list_view_action_cb)
206 { "Information", EMPATHY_IMAGE_CONTACT_INFORMATION,
207 N_("Infor_mation"), "<control>I", N_("View contact information"),
208 G_CALLBACK (contact_list_view_action_cb)
211 N_("Re_name"), NULL, N_("Rename"),
212 G_CALLBACK (contact_list_view_action_cb)
214 { "Edit", GTK_STOCK_EDIT,
215 N_("_Edit"), NULL, N_("Edit the groups and name for this contact"),
216 G_CALLBACK (contact_list_view_action_cb)
218 { "Remove", GTK_STOCK_REMOVE,
219 N_("_Remove"), NULL, N_("Remove contact"),
220 G_CALLBACK (contact_list_view_action_cb)
222 { "Invite", EMPATHY_IMAGE_GROUP_MESSAGE,
223 N_("_Invite to Chat Room"), NULL, N_("Invite to a currently open chat room"),
224 G_CALLBACK (contact_list_view_action_cb)
227 N_("_Send File..."), NULL, N_("Send a file"),
228 G_CALLBACK (contact_list_view_action_cb)
230 { "Log", GTK_STOCK_JUSTIFY_LEFT,
231 N_("_View Previous Conversations"), NULL, N_("View previous conversations with this contact"),
232 G_CALLBACK (contact_list_view_action_cb)
236 static guint n_entries = G_N_ELEMENTS (entries);
238 static const gchar *ui_info =
240 " <popup name='Contact'>"
241 " <menuitem action='Chat'/>"
242 " <menuitem action='Log'/>"
243 " <menuitem action='SendFile'/>"
245 " <menuitem action='Invite'/>"
247 " <menuitem action='Edit'/>"
248 " <menuitem action='Remove'/>"
250 " <menuitem action='Information'/>"
252 " <popup name='Group'>"
253 " <menuitem action='Rename'/>"
258 DND_DRAG_TYPE_CONTACT_ID,
260 DND_DRAG_TYPE_STRING,
263 static const GtkTargetEntry drag_types_dest[] = {
264 { "text/contact-id", 0, DND_DRAG_TYPE_CONTACT_ID },
265 { "text/uri-list", 0, DND_DRAG_TYPE_URL },
266 { "text/plain", 0, DND_DRAG_TYPE_STRING },
267 { "STRING", 0, DND_DRAG_TYPE_STRING },
270 static const GtkTargetEntry drag_types_source[] = {
271 { "text/contact-id", 0, DND_DRAG_TYPE_CONTACT_ID },
274 static GdkAtom drag_atoms_dest[G_N_ELEMENTS (drag_types_dest)];
275 static GdkAtom drag_atoms_source[G_N_ELEMENTS (drag_types_source)];
278 DRAG_CONTACT_RECEIVED,
282 static guint signals[LAST_SIGNAL];
284 G_DEFINE_TYPE (GossipContactListView, gossip_contact_list_view, GTK_TYPE_TREE_VIEW);
287 gossip_contact_list_view_class_init (GossipContactListViewClass *klass)
289 GObjectClass *object_class = G_OBJECT_CLASS (klass);
291 object_class->finalize = contact_list_view_finalize;
292 object_class->get_property = contact_list_view_get_property;
293 object_class->set_property = contact_list_view_set_property;
295 signals[DRAG_CONTACT_RECEIVED] =
296 g_signal_new ("drag-contact-received",
297 G_OBJECT_CLASS_TYPE (klass),
301 empathy_marshal_VOID__OBJECT_STRING_STRING,
303 3, GOSSIP_TYPE_CONTACT, G_TYPE_STRING, G_TYPE_STRING);
305 g_object_class_install_property (object_class,
307 g_param_spec_string ("filter",
309 "The text to use to filter the contact list",
313 g_type_class_add_private (object_class, sizeof (GossipContactListViewPriv));
317 gossip_contact_list_view_init (GossipContactListView *view)
319 GossipContactListViewPriv *priv;
320 GtkActionGroup *action_group;
321 GError *error = NULL;
323 priv = GET_PRIV (view);
325 /* Get saved group states. */
326 gossip_contact_groups_get_all ();
328 /* Set up UI Manager */
329 priv->ui = gtk_ui_manager_new ();
331 action_group = gtk_action_group_new ("Actions");
332 gtk_action_group_set_translation_domain (action_group, GETTEXT_PACKAGE);
333 gtk_action_group_add_actions (action_group, entries, n_entries, view);
334 gtk_ui_manager_insert_action_group (priv->ui, action_group, 0);
336 if (!gtk_ui_manager_add_ui_from_string (priv->ui, ui_info, -1, &error)) {
337 g_warning ("Could not build contact menus from string:'%s'", error->message);
338 g_error_free (error);
341 g_object_unref (action_group);
343 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (view),
344 gossip_contact_list_store_row_separator_func,
347 /* Connect to tree view signals rather than override. */
348 g_signal_connect (view,
349 "button-press-event",
350 G_CALLBACK (contact_list_view_button_press_event_cb),
352 g_signal_connect (view,
354 G_CALLBACK (contact_list_view_row_activated_cb),
356 g_signal_connect (view,
358 G_CALLBACK (contact_list_view_row_expand_or_collapse_cb),
359 GINT_TO_POINTER (TRUE));
360 g_signal_connect (view,
362 G_CALLBACK (contact_list_view_row_expand_or_collapse_cb),
363 GINT_TO_POINTER (FALSE));
367 contact_list_view_finalize (GObject *object)
369 GossipContactListViewPriv *priv;
371 priv = GET_PRIV (object);
374 g_object_unref (priv->ui);
377 g_object_unref (priv->store);
380 g_object_unref (priv->filter);
382 g_free (priv->filter_text);
384 G_OBJECT_CLASS (gossip_contact_list_view_parent_class)->finalize (object);
388 contact_list_view_get_property (GObject *object,
393 GossipContactListViewPriv *priv;
395 priv = GET_PRIV (object);
399 g_value_set_string (value, priv->filter_text);
402 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
408 contact_list_view_set_property (GObject *object,
413 GossipContactListViewPriv *priv;
415 priv = GET_PRIV (object);
419 gossip_contact_list_view_set_filter (GOSSIP_CONTACT_LIST_VIEW (object),
420 g_value_get_string (value));
423 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
428 GossipContactListView *
429 gossip_contact_list_view_new (GossipContactListStore *store)
431 GossipContactListViewPriv *priv;
432 GossipContactListView *view;
434 view = g_object_new (GOSSIP_TYPE_CONTACT_LIST_VIEW, NULL);
435 priv = GET_PRIV (view);
437 priv->store = g_object_ref (store);
438 contact_list_view_setup (view);
444 gossip_contact_list_view_get_selected (GossipContactListView *view)
446 GossipContactListViewPriv *priv;
447 GtkTreeSelection *selection;
450 GossipContact *contact;
452 g_return_val_if_fail (GOSSIP_IS_CONTACT_LIST_VIEW (view), NULL);
454 priv = GET_PRIV (view);
456 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
457 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
461 gtk_tree_model_get (model, &iter, COL_CONTACT, &contact, -1);
467 gossip_contact_list_view_get_selected_group (GossipContactListView *view)
469 GossipContactListViewPriv *priv;
470 GtkTreeSelection *selection;
476 g_return_val_if_fail (GOSSIP_IS_CONTACT_LIST_VIEW (view), NULL);
478 priv = GET_PRIV (view);
480 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
481 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
485 gtk_tree_model_get (model, &iter,
486 COL_IS_GROUP, &is_group,
499 gossip_contact_list_view_get_group_menu (GossipContactListView *view)
501 GossipContactListViewPriv *priv;
504 g_return_val_if_fail (GOSSIP_IS_CONTACT_LIST_VIEW (view), NULL);
506 priv = GET_PRIV (view);
508 widget = gtk_ui_manager_get_widget (priv->ui, "/Group");
514 gossip_contact_list_view_get_contact_menu (GossipContactListView *view,
515 GossipContact *contact)
517 gboolean can_show_log;
518 gboolean can_send_file;
520 g_return_val_if_fail (GOSSIP_IS_CONTACT_LIST_VIEW (view), NULL);
521 g_return_val_if_fail (GOSSIP_IS_CONTACT (contact), NULL);
523 can_show_log = FALSE; /* FIXME: gossip_log_exists_for_contact (contact); */
524 can_send_file = FALSE;
526 return contact_list_view_get_contact_menu (view,
532 gossip_contact_list_view_set_filter (GossipContactListView *view,
535 GossipContactListViewPriv *priv;
537 g_return_if_fail (GOSSIP_IS_CONTACT_LIST_VIEW (view));
539 priv = GET_PRIV (view);
541 g_free (priv->filter_text);
543 priv->filter_text = g_utf8_casefold (filter, -1);
545 priv->filter_text = NULL;
548 gossip_debug (DEBUG_DOMAIN, "Refiltering with filter:'%s' (case folded)", filter);
549 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter));
553 gossip_contact_list_view_set_drag_received_func (GossipContactListView *view,
554 GossipContactListViewDragReceivedFunc func,
557 GossipContactListViewPriv *priv;
559 g_return_if_fail (GOSSIP_IS_CONTACT_LIST_VIEW (view));
561 priv = GET_PRIV (view);
564 priv->drag_received = func;
565 priv->drag_received_data = user_data;
567 priv->drag_received = NULL;
568 priv->drag_received_data = NULL;
573 contact_list_view_setup (GossipContactListView *view)
575 GossipContactListViewPriv *priv;
576 GtkCellRenderer *cell;
577 GtkTreeViewColumn *col;
580 priv = GET_PRIV (view);
583 priv->filter = gtk_tree_model_filter_new (GTK_TREE_MODEL (priv->store), NULL);
585 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (priv->filter),
586 (GtkTreeModelFilterVisibleFunc)
587 contact_list_view_filter_func,
589 gtk_tree_view_set_search_equal_func (GTK_TREE_VIEW (view),
590 gossip_contact_list_store_search_equal_func,
592 g_signal_connect (priv->filter, "row-has-child-toggled",
593 G_CALLBACK (contact_list_view_row_has_child_toggled_cb),
595 gtk_tree_view_set_model (GTK_TREE_VIEW (view), priv->filter);
600 "headers-visible", FALSE,
602 "show-expanders", FALSE,
605 col = gtk_tree_view_column_new ();
608 cell = gtk_cell_renderer_pixbuf_new ();
609 gtk_tree_view_column_pack_start (col, cell, FALSE);
610 gtk_tree_view_column_set_cell_data_func (
612 (GtkTreeCellDataFunc) contact_list_view_pixbuf_cell_data_func,
622 cell = gossip_cell_renderer_text_new ();
623 gtk_tree_view_column_pack_start (col, cell, TRUE);
624 gtk_tree_view_column_set_cell_data_func (
626 (GtkTreeCellDataFunc) contact_list_view_text_cell_data_func,
629 gtk_tree_view_column_add_attribute (col, cell,
631 gtk_tree_view_column_add_attribute (col, cell,
632 "status", COL_STATUS);
633 gtk_tree_view_column_add_attribute (col, cell,
634 "is_group", COL_IS_GROUP);
637 cell = gtk_cell_renderer_pixbuf_new ();
638 gtk_tree_view_column_pack_start (col, cell, FALSE);
639 gtk_tree_view_column_set_cell_data_func (
641 (GtkTreeCellDataFunc) contact_list_view_avatar_cell_data_func,
653 cell = gossip_cell_renderer_expander_new ();
654 gtk_tree_view_column_pack_end (col, cell, FALSE);
655 gtk_tree_view_column_set_cell_data_func (
657 (GtkTreeCellDataFunc) contact_list_view_expander_cell_data_func,
660 /* Actually add the column now we have added all cell renderers */
661 gtk_tree_view_append_column (GTK_TREE_VIEW (view), col);
664 for (i = 0; i < G_N_ELEMENTS (drag_types_dest); ++i) {
665 drag_atoms_dest[i] = gdk_atom_intern (drag_types_dest[i].target,
669 for (i = 0; i < G_N_ELEMENTS (drag_types_source); ++i) {
670 drag_atoms_source[i] = gdk_atom_intern (drag_types_source[i].target,
674 /* Note: We support the COPY action too, but need to make the
675 * MOVE action the default.
677 gtk_drag_source_set (GTK_WIDGET (view),
680 G_N_ELEMENTS (drag_types_source),
683 gtk_drag_dest_set (GTK_WIDGET (view),
684 GTK_DEST_DEFAULT_ALL,
686 G_N_ELEMENTS (drag_types_dest),
687 GDK_ACTION_MOVE | GDK_ACTION_LINK);
689 g_signal_connect (GTK_WIDGET (view),
690 "drag-data-received",
691 G_CALLBACK (contact_list_view_drag_data_received),
694 /* FIXME: noticed but when you drag the row over the treeview
695 * fast, it seems to stop redrawing itself, if we don't
696 * connect this signal, all is fine.
698 g_signal_connect (view,
700 G_CALLBACK (contact_list_view_drag_motion),
703 g_signal_connect (view,
705 G_CALLBACK (contact_list_view_drag_begin),
707 g_signal_connect (view,
709 G_CALLBACK (contact_list_view_drag_data_get),
711 g_signal_connect (view,
713 G_CALLBACK (contact_list_view_drag_end),
718 contact_list_view_row_has_child_toggled_cb (GtkTreeModel *model,
721 GossipContactListView *view)
723 gboolean is_group = FALSE;
726 gtk_tree_model_get (model, iter,
727 COL_IS_GROUP, &is_group,
731 if (!is_group || G_STR_EMPTY (name)) {
736 if (gossip_contact_group_get_expanded (name)) {
737 g_signal_handlers_block_by_func (view,
738 contact_list_view_row_expand_or_collapse_cb,
739 GINT_TO_POINTER (TRUE));
740 gtk_tree_view_expand_row (GTK_TREE_VIEW (view), path, TRUE);
741 g_signal_handlers_unblock_by_func (view,
742 contact_list_view_row_expand_or_collapse_cb,
743 GINT_TO_POINTER (TRUE));
745 g_signal_handlers_block_by_func (view,
746 contact_list_view_row_expand_or_collapse_cb,
747 GINT_TO_POINTER (FALSE));
748 gtk_tree_view_collapse_row (GTK_TREE_VIEW (view), path);
749 g_signal_handlers_unblock_by_func (view,
750 contact_list_view_row_expand_or_collapse_cb,
751 GINT_TO_POINTER (FALSE));
758 contact_list_view_contact_received (GossipContactListView *view,
759 GossipContact *contact,
760 GdkDragAction action,
761 const gchar *old_group,
762 const gchar *new_group)
764 GossipContactListViewPriv *priv;
766 GList *new_groups_list = NULL;
768 priv = GET_PRIV (view);
770 groups = gossip_contact_get_groups (contact);
771 for (l = groups; l; l = l->next) {
776 if (action == GDK_ACTION_MOVE &&
778 strcmp (str, old_group) == 0) {
782 if (new_group && strcmp (str, new_group) == 0) {
783 /* Otherwise we set it twice */
787 new_groups_list = g_list_prepend (new_groups_list, g_strdup (str));
791 new_groups_list = g_list_prepend (new_groups_list, g_strdup (new_group));
794 gossip_contact_set_groups (contact, new_groups_list);
798 contact_list_view_drag_data_received (GtkWidget *widget,
799 GdkDragContext *context,
802 GtkSelectionData *selection,
807 GossipContactListViewPriv *priv;
808 EmpathyContactList *list;
811 GtkTreeViewDropPosition position;
812 GossipContact *contact;
814 gchar *new_group = NULL;
815 gchar *old_group = NULL;
818 priv = GET_PRIV (widget);
820 id = (const gchar*) selection->data;
821 gossip_debug (DEBUG_DOMAIN, "Received %s%s drag & drop contact from roster with id:'%s'",
822 context->action == GDK_ACTION_MOVE ? "move" : "",
823 context->action == GDK_ACTION_COPY ? "copy" : "",
826 /* FIXME: This is ambigous, an id can come from multiple accounts */
827 list = gossip_contact_list_store_get_list_iface (priv->store);
828 contact = empathy_contact_list_find (list, id);
831 gossip_debug (DEBUG_DOMAIN, "No contact found associated with drag & drop");
835 model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
837 /* Get source group information. */
838 if (priv->drag_row) {
839 path = gtk_tree_row_reference_get_path (priv->drag_row);
841 old_group = gossip_contact_list_store_get_parent_group (model, path, NULL);
842 gtk_tree_path_free (path);
846 /* Get destination group information. */
847 is_row = gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget),
854 new_group = gossip_contact_list_store_get_parent_group (model, path, NULL);
855 gtk_tree_path_free (path);
858 gossip_debug (DEBUG_DOMAIN,
859 "contact '%s' dragged from '%s' to '%s'",
860 gossip_contact_get_name (contact),
861 old_group, new_group);
863 if (priv->drag_received) {
864 priv->drag_received (contact,
868 priv->drag_received_data);
870 contact_list_view_contact_received (GOSSIP_CONTACT_LIST_VIEW (widget),
880 gtk_drag_finish (context, TRUE, FALSE, GDK_CURRENT_TIME);
884 contact_list_view_drag_motion (GtkWidget *widget,
885 GdkDragContext *context,
891 static DragMotionData *dm = NULL;
894 gboolean is_different = FALSE;
895 gboolean cleanup = TRUE;
897 is_row = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
908 cleanup &= (dm && gtk_tree_path_compare (dm->path, path) != 0);
909 is_different = (!dm || (dm && gtk_tree_path_compare (dm->path, path) != 0));
914 if (!is_different && !cleanup) {
919 gtk_tree_path_free (dm->path);
920 if (dm->timeout_id) {
921 g_source_remove (dm->timeout_id);
929 if (!gtk_tree_view_row_expanded (GTK_TREE_VIEW (widget), path)) {
930 dm = g_new0 (DragMotionData, 1);
932 dm->view = GOSSIP_CONTACT_LIST_VIEW (widget);
933 dm->path = gtk_tree_path_copy (path);
935 dm->timeout_id = g_timeout_add (
937 (GSourceFunc) contact_list_view_drag_motion_cb,
945 contact_list_view_drag_motion_cb (DragMotionData *data)
947 gtk_tree_view_expand_row (GTK_TREE_VIEW (data->view),
951 data->timeout_id = 0;
957 contact_list_view_drag_begin (GtkWidget *widget,
958 GdkDragContext *context,
961 GossipContactListViewPriv *priv;
962 GtkTreeSelection *selection;
967 priv = GET_PRIV (widget);
969 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (widget));
970 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
974 path = gtk_tree_model_get_path (model, &iter);
975 priv->drag_row = gtk_tree_row_reference_new (model, path);
976 gtk_tree_path_free (path);
980 contact_list_view_drag_data_get (GtkWidget *widget,
981 GdkDragContext *context,
982 GtkSelectionData *selection,
987 GossipContactListViewPriv *priv;
988 GtkTreePath *src_path;
991 GossipContact *contact;
994 priv = GET_PRIV (widget);
996 model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
997 if (!priv->drag_row) {
1001 src_path = gtk_tree_row_reference_get_path (priv->drag_row);
1006 if (!gtk_tree_model_get_iter (model, &iter, src_path)) {
1007 gtk_tree_path_free (src_path);
1011 gtk_tree_path_free (src_path);
1013 contact = gossip_contact_list_view_get_selected (GOSSIP_CONTACT_LIST_VIEW (widget));
1018 id = gossip_contact_get_id (contact);
1019 g_object_unref (contact);
1022 case DND_DRAG_TYPE_CONTACT_ID:
1023 gtk_selection_data_set (selection, drag_atoms_source[info], 8,
1024 (guchar*)id, strlen (id) + 1);
1033 contact_list_view_drag_end (GtkWidget *widget,
1034 GdkDragContext *context,
1037 GossipContactListViewPriv *priv;
1039 priv = GET_PRIV (widget);
1041 if (priv->drag_row) {
1042 gtk_tree_row_reference_free (priv->drag_row);
1043 priv->drag_row = NULL;
1048 contact_list_view_cell_set_background (GossipContactListView *view,
1049 GtkCellRenderer *cell,
1056 style = gtk_widget_get_style (GTK_WIDGET (view));
1060 color = style->bg[GTK_STATE_SELECTED];
1062 /* Here we take the current theme colour and add it to
1063 * the colour for white and average the two. This
1064 * gives a colour which is inline with the theme but
1067 color.red = (color.red + (style->white).red) / 2;
1068 color.green = (color.green + (style->white).green) / 2;
1069 color.blue = (color.blue + (style->white).blue) / 2;
1072 "cell-background-gdk", &color,
1076 "cell-background-gdk", NULL,
1081 "cell-background-gdk", NULL,
1087 contact_list_view_pixbuf_cell_data_func (GtkTreeViewColumn *tree_column,
1088 GtkCellRenderer *cell,
1089 GtkTreeModel *model,
1091 GossipContactListView *view)
1097 gtk_tree_model_get (model, iter,
1098 COL_IS_GROUP, &is_group,
1099 COL_IS_ACTIVE, &is_active,
1100 COL_ICON_STATUS, &icon_name,
1104 "visible", !is_group,
1105 "icon-name", icon_name,
1110 contact_list_view_cell_set_background (view, cell, is_group, is_active);
1114 contact_list_view_avatar_cell_data_func (GtkTreeViewColumn *tree_column,
1115 GtkCellRenderer *cell,
1116 GtkTreeModel *model,
1118 GossipContactListView *view)
1121 gboolean show_avatar;
1125 gtk_tree_model_get (model, iter,
1126 COL_PIXBUF_AVATAR, &pixbuf,
1127 COL_PIXBUF_AVATAR_VISIBLE, &show_avatar,
1128 COL_IS_GROUP, &is_group,
1129 COL_IS_ACTIVE, &is_active,
1133 "visible", !is_group && show_avatar,
1138 g_object_unref (pixbuf);
1141 contact_list_view_cell_set_background (view, cell, is_group, is_active);
1145 contact_list_view_text_cell_data_func (GtkTreeViewColumn *tree_column,
1146 GtkCellRenderer *cell,
1147 GtkTreeModel *model,
1149 GossipContactListView *view)
1153 gboolean show_status;
1155 gtk_tree_model_get (model, iter,
1156 COL_IS_GROUP, &is_group,
1157 COL_IS_ACTIVE, &is_active,
1158 COL_STATUS_VISIBLE, &show_status,
1162 "show-status", show_status,
1165 contact_list_view_cell_set_background (view, cell, is_group, is_active);
1169 contact_list_view_expander_cell_data_func (GtkTreeViewColumn *column,
1170 GtkCellRenderer *cell,
1171 GtkTreeModel *model,
1173 GossipContactListView *view)
1178 gtk_tree_model_get (model, iter,
1179 COL_IS_GROUP, &is_group,
1180 COL_IS_ACTIVE, &is_active,
1183 if (gtk_tree_model_iter_has_child (model, iter)) {
1185 gboolean row_expanded;
1187 path = gtk_tree_model_get_path (model, iter);
1188 row_expanded = gtk_tree_view_row_expanded (GTK_TREE_VIEW (column->tree_view), path);
1189 gtk_tree_path_free (path);
1193 "expander-style", row_expanded ? GTK_EXPANDER_EXPANDED : GTK_EXPANDER_COLLAPSED,
1196 g_object_set (cell, "visible", FALSE, NULL);
1199 contact_list_view_cell_set_background (view, cell, is_group, is_active);
1203 contact_list_view_get_contact_menu (GossipContactListView *view,
1204 gboolean can_send_file,
1205 gboolean can_show_log)
1207 GossipContactListViewPriv *priv;
1211 priv = GET_PRIV (view);
1213 /* Sort out sensitive items */
1214 action = gtk_ui_manager_get_action (priv->ui, "/Contact/Log");
1215 gtk_action_set_sensitive (action, can_show_log);
1217 action = gtk_ui_manager_get_action (priv->ui, "/Contact/SendFile");
1218 gtk_action_set_visible (action, can_send_file);
1220 widget = gtk_ui_manager_get_widget (priv->ui, "/Contact");
1226 contact_list_view_button_press_event_cb (GossipContactListView *view,
1227 GdkEventButton *event,
1230 GossipContactListViewPriv *priv;
1231 GossipContact *contact;
1233 GtkTreeSelection *selection;
1234 GtkTreeModel *model;
1236 gboolean row_exists;
1239 if (event->button != 3) {
1243 priv = GET_PRIV (view);
1245 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
1246 model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
1248 gtk_widget_grab_focus (GTK_WIDGET (view));
1250 row_exists = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (view),
1258 gtk_tree_selection_unselect_all (selection);
1259 gtk_tree_selection_select_path (selection, path);
1261 gtk_tree_model_get_iter (model, &iter, path);
1262 gtk_tree_path_free (path);
1264 gtk_tree_model_get (model, &iter, COL_CONTACT, &contact, -1);
1267 menu = gossip_contact_list_view_get_contact_menu (view, contact);
1268 g_object_unref (contact);
1270 menu = gossip_contact_list_view_get_group_menu (view);
1277 gtk_widget_show (menu);
1279 gtk_menu_popup (GTK_MENU (menu),
1280 NULL, NULL, NULL, NULL,
1281 event->button, event->time);
1287 contact_list_view_row_activated_cb (GossipContactListView *view,
1289 GtkTreeViewColumn *col,
1292 GossipContact *contact;
1293 GtkTreeModel *model;
1296 model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
1298 gtk_tree_model_get_iter (model, &iter, path);
1299 gtk_tree_model_get (model, &iter, COL_CONTACT, &contact, -1);
1302 contact_list_view_action_activated (view, contact);
1303 g_object_unref (contact);
1308 contact_list_view_row_expand_or_collapse_cb (GossipContactListView *view,
1313 GtkTreeModel *model;
1317 model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
1319 gtk_tree_model_get (model, iter,
1323 expanded = GPOINTER_TO_INT (user_data);
1324 gossip_contact_group_set_expanded (name, expanded);
1330 contact_list_view_filter_show_contact (GossipContact *contact,
1331 const gchar *filter)
1336 /* Check contact id */
1337 str = g_utf8_casefold (gossip_contact_get_id (contact), -1);
1338 visible = G_STR_EMPTY (str) || strstr (str, filter);
1345 /* Check contact name */
1346 str = g_utf8_casefold (gossip_contact_get_name (contact), -1);
1347 visible = G_STR_EMPTY (str) || strstr (str, filter);
1354 contact_list_view_filter_show_group (GossipContactListView *view,
1356 const gchar *filter)
1358 GossipContactListViewPriv *priv;
1359 EmpathyContactList *list;
1360 GList *contacts, *l;
1362 gboolean show_group = FALSE;
1364 priv = GET_PRIV (view);
1366 str = g_utf8_casefold (group, -1);
1371 /* If the filter is the partially the group name, we show the
1374 if (strstr (str, filter)) {
1379 /* At this point, we need to check in advance if this
1380 * group should be shown because a contact we want to
1381 * show exists in it.
1383 list = gossip_contact_list_store_get_list_iface (priv->store);
1384 contacts = empathy_contact_list_get_members (list);
1385 for (l = contacts; l && !show_group; l = l->next) {
1386 if (!gossip_contact_is_in_group (l->data, group)) {
1390 if (contact_list_view_filter_show_contact (l->data, filter)) {
1394 g_list_foreach (contacts, (GFunc) g_object_unref, NULL);
1395 g_list_free (contacts);
1402 contact_list_view_filter_func (GtkTreeModel *model,
1404 GossipContactListView *view)
1406 GossipContactListViewPriv *priv;
1408 gboolean is_separator;
1409 gboolean visible = TRUE;
1411 priv = GET_PRIV (view);
1413 if (G_STR_EMPTY (priv->filter_text)) {
1417 /* Check to see if iter matches any group names */
1418 gtk_tree_model_get (model, iter,
1419 COL_IS_GROUP, &is_group,
1420 COL_IS_SEPARATOR, &is_separator,
1426 gtk_tree_model_get (model, iter, COL_NAME, &name, -1);
1427 visible &= contact_list_view_filter_show_group (view,
1431 } else if (is_separator) {
1432 /* Do nothing here */
1434 GossipContact *contact;
1436 /* Check contact id */
1437 gtk_tree_model_get (model, iter, COL_CONTACT, &contact, -1);
1438 visible &= contact_list_view_filter_show_contact (contact,
1440 g_object_unref (contact);
1447 contact_list_view_action_cb (GtkAction *action,
1448 GossipContactListView *view)
1450 GossipContactListViewPriv *priv;
1451 GossipContact *contact;
1456 priv = GET_PRIV (view);
1458 name = gtk_action_get_name (action);
1463 gossip_debug (DEBUG_DOMAIN, "Action:'%s' activated", name);
1465 contact = gossip_contact_list_view_get_selected (view);
1466 group = gossip_contact_list_view_get_selected_group (view);
1467 parent = gossip_get_toplevel_window (GTK_WIDGET (view));
1469 if (contact && strcmp (name, "Chat") == 0) {
1470 contact_list_view_action_activated (view, contact);
1472 else if (contact && strcmp (name, "Information") == 0) {
1473 empathy_contact_information_dialog_show (contact, parent, FALSE);
1475 else if (contact && strcmp (name, "Edit") == 0) {
1476 empathy_contact_information_dialog_show (contact, parent, TRUE);
1478 else if (contact && strcmp (name, "Remove") == 0) {
1479 /* FIXME: Ask for confirmation */
1480 EmpathyContactList *list;
1482 list = gossip_contact_list_store_get_list_iface (priv->store);
1483 empathy_contact_list_remove (list, contact,
1484 _("Sorry, I don't want you in my contact list anymore."));
1486 else if (contact && strcmp (name, "Invite") == 0) {
1488 else if (contact && strcmp (name, "SendFile") == 0) {
1490 else if (contact && strcmp (name, "Log") == 0) {
1492 else if (group && strcmp (name, "Rename") == 0) {
1497 g_object_unref (contact);
1502 contact_list_view_action_activated (GossipContactListView *view,
1503 GossipContact *contact)
1507 mc = gossip_mission_control_new ();
1508 mission_control_request_channel (mc,
1509 gossip_contact_get_account (contact),
1510 TP_IFACE_CHANNEL_TYPE_TEXT,
1511 gossip_contact_get_handle (contact),
1512 TP_HANDLE_TYPE_CONTACT,
1514 g_object_unref (mc);