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 <libtelepathy/tp-helpers.h>
36 #include <libmissioncontrol/mc-account.h>
37 #include <libmissioncontrol/mission-control.h>
39 #include <libempathy/empathy-contact-factory.h>
40 #include <libempathy/empathy-contact-list.h>
41 #include <libempathy/empathy-log-manager.h>
42 #include <libempathy/empathy-tp-group.h>
43 #include <libempathy/empathy-debug.h>
44 #include <libempathy/empathy-utils.h>
45 #include <libempathy/empathy-marshal.h>
47 #include "empathy-contact-list-view.h"
48 #include "empathy-contact-list-store.h"
49 #include "empathy-images.h"
50 #include "empathy-contact-groups.h"
51 #include "empathy-cell-renderer-expander.h"
52 #include "empathy-cell-renderer-text.h"
53 #include "empathy-cell-renderer-activatable.h"
54 #include "empathy-ui-utils.h"
55 #include "empathy-contact-dialogs.h"
56 //#include "empathy-chat-invite.h"
57 //#include "empathy-ft-window.h"
58 #include "empathy-log-window.h"
60 #define DEBUG_DOMAIN "ContactListView"
62 /* Flashing delay for icons (milliseconds). */
63 #define FLASH_TIMEOUT 500
65 /* Active users are those which have recently changed state
66 * (e.g. online, offline or from normal to a busy state).
69 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_CONTACT_LIST_VIEW, EmpathyContactListViewPriv))
71 struct _EmpathyContactListViewPriv {
72 EmpathyContactListStore *store;
74 GtkTreeRowReference *drag_row;
79 EmpathyContactListView *view;
85 EmpathyContactListView *view;
86 EmpathyContact *contact;
90 static void empathy_contact_list_view_class_init (EmpathyContactListViewClass *klass);
91 static void empathy_contact_list_view_init (EmpathyContactListView *list);
92 static void contact_list_view_finalize (GObject *object);
93 static void contact_list_view_get_property (GObject *object,
97 static void contact_list_view_set_property (GObject *object,
101 static void contact_list_view_setup (EmpathyContactListView *view);
102 static void contact_list_view_row_has_child_toggled_cb (GtkTreeModel *model,
105 EmpathyContactListView *view);
106 static void contact_list_view_drag_data_received (GtkWidget *widget,
107 GdkDragContext *context,
110 GtkSelectionData *selection,
113 static gboolean contact_list_view_drag_motion (GtkWidget *widget,
114 GdkDragContext *context,
118 static gboolean contact_list_view_drag_motion_cb (DragMotionData *data);
119 static void contact_list_view_drag_begin (GtkWidget *widget,
120 GdkDragContext *context);
121 static void contact_list_view_drag_data_get (GtkWidget *widget,
122 GdkDragContext *context,
123 GtkSelectionData *selection,
126 static void contact_list_view_drag_end (GtkWidget *widget,
127 GdkDragContext *context);
128 static gboolean contact_list_view_drag_drop (GtkWidget *widget,
129 GdkDragContext *drag_context,
133 static void contact_list_view_cell_set_background (EmpathyContactListView *view,
134 GtkCellRenderer *cell,
137 static void contact_list_view_pixbuf_cell_data_func (GtkTreeViewColumn *tree_column,
138 GtkCellRenderer *cell,
141 EmpathyContactListView *view);
142 static void contact_list_view_voip_cell_data_func (GtkTreeViewColumn *tree_column,
143 GtkCellRenderer *cell,
146 EmpathyContactListView *view);
147 static void contact_list_view_avatar_cell_data_func (GtkTreeViewColumn *tree_column,
148 GtkCellRenderer *cell,
151 EmpathyContactListView *view);
152 static void contact_list_view_text_cell_data_func (GtkTreeViewColumn *tree_column,
153 GtkCellRenderer *cell,
156 EmpathyContactListView *view);
157 static void contact_list_view_expander_cell_data_func (GtkTreeViewColumn *column,
158 GtkCellRenderer *cell,
161 EmpathyContactListView *view);
162 static GtkWidget * contact_list_view_get_contact_menu (EmpathyContactListView *view,
163 gboolean can_send_file,
164 gboolean can_show_log,
166 static gboolean contact_list_view_button_press_event_cb (EmpathyContactListView *view,
167 GdkEventButton *event,
169 static void contact_list_view_row_activated_cb (EmpathyContactListView *view,
171 GtkTreeViewColumn *col,
173 static void contact_list_view_voip_activated_cb (EmpathyCellRendererActivatable *cell,
174 const gchar *path_string,
175 EmpathyContactListView *view);
176 static void contact_list_view_row_expand_or_collapse_cb (EmpathyContactListView *view,
180 static void contact_list_view_action_cb (GtkAction *action,
181 EmpathyContactListView *view);
182 static void contact_list_view_action_activated (EmpathyContactListView *view,
183 EmpathyContact *contact);
184 static void contact_list_view_voip_activated (EmpathyContactListView *view,
185 EmpathyContact *contact);
192 static const GtkActionEntry entries[] = {
193 { "ContactMenu", NULL,
194 N_("_Contact"), NULL, NULL,
198 N_("_Group"),NULL, NULL,
201 { "Chat", EMPATHY_IMAGE_MESSAGE,
202 N_("_Chat"), NULL, N_("Chat with contact"),
203 G_CALLBACK (contact_list_view_action_cb)
205 { "Information", EMPATHY_IMAGE_CONTACT_INFORMATION,
206 N_("Infor_mation"), "<control>I", N_("View contact information"),
207 G_CALLBACK (contact_list_view_action_cb)
210 N_("Re_name"), NULL, N_("Rename"),
211 G_CALLBACK (contact_list_view_action_cb)
213 { "Edit", GTK_STOCK_EDIT,
214 N_("_Edit"), NULL, N_("Edit the groups and name for this contact"),
215 G_CALLBACK (contact_list_view_action_cb)
217 { "Remove", GTK_STOCK_REMOVE,
218 N_("_Remove"), NULL, N_("Remove contact"),
219 G_CALLBACK (contact_list_view_action_cb)
221 { "Invite", EMPATHY_IMAGE_GROUP_MESSAGE,
222 N_("_Invite to Chat Room"), NULL, N_("Invite to a currently open chat room"),
223 G_CALLBACK (contact_list_view_action_cb)
226 N_("_Send File..."), NULL, N_("Send a file"),
227 G_CALLBACK (contact_list_view_action_cb)
229 { "Log", GTK_STOCK_JUSTIFY_LEFT,
230 N_("_View Previous Conversations"), NULL, N_("View previous conversations with this contact"),
231 G_CALLBACK (contact_list_view_action_cb)
233 { "Call", EMPATHY_IMAGE_VOIP,
234 N_("_Call"), NULL, N_("Start a voice or video conversation with this contact"),
235 G_CALLBACK (contact_list_view_action_cb)
239 static guint n_entries = G_N_ELEMENTS (entries);
241 static const gchar *ui_info =
243 " <popup name='Contact'>"
244 " <menuitem action='Chat'/>"
245 " <menuitem action='Call'/>"
246 " <menuitem action='Log'/>"
247 " <menuitem action='SendFile'/>"
249 " <menuitem action='Invite'/>"
251 " <menuitem action='Edit'/>"
252 " <menuitem action='Remove'/>"
254 " <menuitem action='Information'/>"
256 " <popup name='Group'>"
257 " <menuitem action='Rename'/>"
262 DND_DRAG_TYPE_CONTACT_ID,
264 DND_DRAG_TYPE_STRING,
267 static const GtkTargetEntry drag_types_dest[] = {
268 { "text/contact-id", 0, DND_DRAG_TYPE_CONTACT_ID },
269 { "text/uri-list", 0, DND_DRAG_TYPE_URL },
270 { "text/plain", 0, DND_DRAG_TYPE_STRING },
271 { "STRING", 0, DND_DRAG_TYPE_STRING },
274 static const GtkTargetEntry drag_types_source[] = {
275 { "text/contact-id", 0, DND_DRAG_TYPE_CONTACT_ID },
278 static GdkAtom drag_atoms_dest[G_N_ELEMENTS (drag_types_dest)];
279 static GdkAtom drag_atoms_source[G_N_ELEMENTS (drag_types_source)];
282 DRAG_CONTACT_RECEIVED,
286 static guint signals[LAST_SIGNAL];
288 G_DEFINE_TYPE (EmpathyContactListView, empathy_contact_list_view, GTK_TYPE_TREE_VIEW);
291 empathy_contact_list_view_class_init (EmpathyContactListViewClass *klass)
293 GObjectClass *object_class = G_OBJECT_CLASS (klass);
294 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
296 object_class->finalize = contact_list_view_finalize;
297 object_class->get_property = contact_list_view_get_property;
298 object_class->set_property = contact_list_view_set_property;
300 widget_class->drag_data_received = contact_list_view_drag_data_received;
301 widget_class->drag_drop = contact_list_view_drag_drop;
302 widget_class->drag_begin = contact_list_view_drag_begin;
303 widget_class->drag_data_get = contact_list_view_drag_data_get;
304 widget_class->drag_end = contact_list_view_drag_end;
305 /* FIXME: noticed but when you drag the row over the treeview
306 * fast, it seems to stop redrawing itself, if we don't
307 * connect this signal, all is fine.
309 widget_class->drag_motion = contact_list_view_drag_motion;
311 signals[DRAG_CONTACT_RECEIVED] =
312 g_signal_new ("drag-contact-received",
313 G_OBJECT_CLASS_TYPE (klass),
317 empathy_marshal_VOID__OBJECT_STRING_STRING,
319 3, EMPATHY_TYPE_CONTACT, G_TYPE_STRING, G_TYPE_STRING);
321 g_object_class_install_property (object_class,
323 g_param_spec_boolean ("interactive",
324 "View is interactive",
325 "Is the view interactive",
329 g_type_class_add_private (object_class, sizeof (EmpathyContactListViewPriv));
333 empathy_contact_list_view_init (EmpathyContactListView *view)
335 EmpathyContactListViewPriv *priv;
336 GtkActionGroup *action_group;
337 GError *error = NULL;
339 priv = GET_PRIV (view);
341 /* Get saved group states. */
342 empathy_contact_groups_get_all ();
344 /* Set up UI Manager */
345 priv->ui = gtk_ui_manager_new ();
347 action_group = gtk_action_group_new ("Actions");
348 gtk_action_group_set_translation_domain (action_group, GETTEXT_PACKAGE);
349 gtk_action_group_add_actions (action_group, entries, n_entries, view);
350 gtk_ui_manager_insert_action_group (priv->ui, action_group, 0);
352 if (!gtk_ui_manager_add_ui_from_string (priv->ui, ui_info, -1, &error)) {
353 g_warning ("Could not build contact menus from string:'%s'", error->message);
354 g_error_free (error);
357 g_object_unref (action_group);
359 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (view),
360 empathy_contact_list_store_row_separator_func,
363 /* Connect to tree view signals rather than override. */
364 g_signal_connect (view,
365 "button-press-event",
366 G_CALLBACK (contact_list_view_button_press_event_cb),
368 g_signal_connect (view,
370 G_CALLBACK (contact_list_view_row_activated_cb),
372 g_signal_connect (view,
374 G_CALLBACK (contact_list_view_row_expand_or_collapse_cb),
375 GINT_TO_POINTER (TRUE));
376 g_signal_connect (view,
378 G_CALLBACK (contact_list_view_row_expand_or_collapse_cb),
379 GINT_TO_POINTER (FALSE));
383 contact_list_view_finalize (GObject *object)
385 EmpathyContactListViewPriv *priv;
387 priv = GET_PRIV (object);
390 g_object_unref (priv->ui);
393 g_object_unref (priv->store);
396 G_OBJECT_CLASS (empathy_contact_list_view_parent_class)->finalize (object);
400 contact_list_view_get_property (GObject *object,
405 EmpathyContactListViewPriv *priv;
407 priv = GET_PRIV (object);
410 case PROP_INTERACTIVE:
411 g_value_set_boolean (value, priv->interactive);
414 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
420 contact_list_view_set_property (GObject *object,
425 EmpathyContactListView *view = EMPATHY_CONTACT_LIST_VIEW (object);
426 EmpathyContactListViewPriv *priv;
428 priv = GET_PRIV (object);
431 case PROP_INTERACTIVE:
432 empathy_contact_list_view_set_interactive (view, g_value_get_boolean (value));
435 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
440 EmpathyContactListView *
441 empathy_contact_list_view_new (EmpathyContactListStore *store)
443 EmpathyContactListViewPriv *priv;
444 EmpathyContactListView *view;
446 view = g_object_new (EMPATHY_TYPE_CONTACT_LIST_VIEW, NULL);
447 priv = GET_PRIV (view);
449 priv->store = g_object_ref (store);
450 contact_list_view_setup (view);
456 empathy_contact_list_view_set_interactive (EmpathyContactListView *view,
457 gboolean interactive)
459 EmpathyContactListViewPriv *priv = GET_PRIV (view);
461 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view));
463 priv->interactive = interactive;
464 g_object_notify (G_OBJECT (view), "interactive");
468 empathy_contact_list_view_get_interactive (EmpathyContactListView *view)
470 EmpathyContactListViewPriv *priv = GET_PRIV (view);
472 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), FALSE);
474 return priv->interactive;
478 empathy_contact_list_view_get_selected (EmpathyContactListView *view)
480 EmpathyContactListViewPriv *priv;
481 GtkTreeSelection *selection;
484 EmpathyContact *contact;
486 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), NULL);
488 priv = GET_PRIV (view);
490 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
491 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
495 gtk_tree_model_get (model, &iter, COL_CONTACT, &contact, -1);
501 empathy_contact_list_view_get_selected_group (EmpathyContactListView *view)
503 EmpathyContactListViewPriv *priv;
504 GtkTreeSelection *selection;
510 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), NULL);
512 priv = GET_PRIV (view);
514 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
515 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
519 gtk_tree_model_get (model, &iter,
520 COL_IS_GROUP, &is_group,
533 empathy_contact_list_view_get_group_menu (EmpathyContactListView *view)
535 EmpathyContactListViewPriv *priv;
538 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), NULL);
540 priv = GET_PRIV (view);
542 widget = gtk_ui_manager_get_widget (priv->ui, "/Group");
548 empathy_contact_list_view_get_contact_menu (EmpathyContactListView *view,
549 EmpathyContact *contact)
551 EmpathyLogManager *log_manager;
552 gboolean can_show_log;
553 gboolean can_send_file;
556 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), NULL);
557 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
559 log_manager = empathy_log_manager_new ();
560 can_show_log = empathy_log_manager_exists (log_manager,
561 empathy_contact_get_account (contact),
562 empathy_contact_get_id (contact),
564 g_object_unref (log_manager);
565 can_send_file = FALSE;
566 can_voip = empathy_contact_can_voip (contact);
568 return contact_list_view_get_contact_menu (view,
575 contact_list_view_setup (EmpathyContactListView *view)
577 EmpathyContactListViewPriv *priv;
578 GtkCellRenderer *cell;
579 GtkTreeViewColumn *col;
582 priv = GET_PRIV (view);
584 gtk_tree_view_set_search_equal_func (GTK_TREE_VIEW (view),
585 empathy_contact_list_store_search_equal_func,
588 g_signal_connect (priv->store, "row-has-child-toggled",
589 G_CALLBACK (contact_list_view_row_has_child_toggled_cb),
591 gtk_tree_view_set_model (GTK_TREE_VIEW (view),
592 GTK_TREE_MODEL (priv->store));
596 "headers-visible", FALSE,
598 "show-expanders", FALSE,
601 col = gtk_tree_view_column_new ();
604 cell = gtk_cell_renderer_pixbuf_new ();
605 gtk_tree_view_column_pack_start (col, cell, FALSE);
606 gtk_tree_view_column_set_cell_data_func (
608 (GtkTreeCellDataFunc) contact_list_view_pixbuf_cell_data_func,
618 cell = empathy_cell_renderer_text_new ();
619 gtk_tree_view_column_pack_start (col, cell, TRUE);
620 gtk_tree_view_column_set_cell_data_func (
622 (GtkTreeCellDataFunc) contact_list_view_text_cell_data_func,
625 gtk_tree_view_column_add_attribute (col, cell,
627 gtk_tree_view_column_add_attribute (col, cell,
628 "status", COL_STATUS);
629 gtk_tree_view_column_add_attribute (col, cell,
630 "is_group", COL_IS_GROUP);
632 /* Voip Capability Icon */
633 cell = empathy_cell_renderer_activatable_new ();
634 gtk_tree_view_column_pack_start (col, cell, FALSE);
635 gtk_tree_view_column_set_cell_data_func (
637 (GtkTreeCellDataFunc) contact_list_view_voip_cell_data_func,
644 g_signal_connect (cell, "path-activated",
645 G_CALLBACK (contact_list_view_voip_activated_cb),
649 cell = gtk_cell_renderer_pixbuf_new ();
650 gtk_tree_view_column_pack_start (col, cell, FALSE);
651 gtk_tree_view_column_set_cell_data_func (
653 (GtkTreeCellDataFunc) contact_list_view_avatar_cell_data_func,
665 cell = empathy_cell_renderer_expander_new ();
666 gtk_tree_view_column_pack_end (col, cell, FALSE);
667 gtk_tree_view_column_set_cell_data_func (
669 (GtkTreeCellDataFunc) contact_list_view_expander_cell_data_func,
672 /* Actually add the column now we have added all cell renderers */
673 gtk_tree_view_append_column (GTK_TREE_VIEW (view), col);
676 for (i = 0; i < G_N_ELEMENTS (drag_types_dest); ++i) {
677 drag_atoms_dest[i] = gdk_atom_intern (drag_types_dest[i].target,
681 for (i = 0; i < G_N_ELEMENTS (drag_types_source); ++i) {
682 drag_atoms_source[i] = gdk_atom_intern (drag_types_source[i].target,
686 /* Note: We support the COPY action too, but need to make the
687 * MOVE action the default.
689 gtk_drag_source_set (GTK_WIDGET (view),
692 G_N_ELEMENTS (drag_types_source),
693 GDK_ACTION_MOVE | GDK_ACTION_COPY);
695 gtk_drag_dest_set (GTK_WIDGET (view),
696 GTK_DEST_DEFAULT_ALL,
698 G_N_ELEMENTS (drag_types_dest),
699 GDK_ACTION_MOVE | GDK_ACTION_COPY);
703 contact_list_view_row_has_child_toggled_cb (GtkTreeModel *model,
706 EmpathyContactListView *view)
708 gboolean is_group = FALSE;
711 gtk_tree_model_get (model, iter,
712 COL_IS_GROUP, &is_group,
716 if (!is_group || G_STR_EMPTY (name)) {
721 if (empathy_contact_group_get_expanded (name)) {
722 g_signal_handlers_block_by_func (view,
723 contact_list_view_row_expand_or_collapse_cb,
724 GINT_TO_POINTER (TRUE));
725 gtk_tree_view_expand_row (GTK_TREE_VIEW (view), path, TRUE);
726 g_signal_handlers_unblock_by_func (view,
727 contact_list_view_row_expand_or_collapse_cb,
728 GINT_TO_POINTER (TRUE));
730 g_signal_handlers_block_by_func (view,
731 contact_list_view_row_expand_or_collapse_cb,
732 GINT_TO_POINTER (FALSE));
733 gtk_tree_view_collapse_row (GTK_TREE_VIEW (view), path);
734 g_signal_handlers_unblock_by_func (view,
735 contact_list_view_row_expand_or_collapse_cb,
736 GINT_TO_POINTER (FALSE));
743 contact_list_view_drag_data_received (GtkWidget *widget,
744 GdkDragContext *context,
747 GtkSelectionData *selection,
751 EmpathyContactListViewPriv *priv;
752 EmpathyContactList *list;
753 EmpathyContactFactory *factory;
757 GtkTreeViewDropPosition position;
758 EmpathyContact *contact = NULL;
761 gchar *new_group = NULL;
762 gchar *old_group = NULL;
765 priv = GET_PRIV (widget);
767 id = (const gchar*) selection->data;
768 empathy_debug (DEBUG_DOMAIN, "Received %s%s drag & drop contact from roster with id:'%s'",
769 context->action == GDK_ACTION_MOVE ? "move" : "",
770 context->action == GDK_ACTION_COPY ? "copy" : "",
773 strv = g_strsplit (id, "/", 2);
774 factory = empathy_contact_factory_new ();
775 account = mc_account_lookup (strv[0]);
777 contact = empathy_contact_factory_get_from_id (factory,
780 g_object_unref (account);
782 g_object_unref (factory);
786 empathy_debug (DEBUG_DOMAIN, "No contact found associated with drag & drop");
790 model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
792 /* Get source group information. */
793 if (priv->drag_row) {
794 path = gtk_tree_row_reference_get_path (priv->drag_row);
796 old_group = empathy_contact_list_store_get_parent_group (model, path, NULL);
797 gtk_tree_path_free (path);
801 /* Get destination group information. */
802 is_row = gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget),
809 new_group = empathy_contact_list_store_get_parent_group (model, path, NULL);
810 gtk_tree_path_free (path);
813 empathy_debug (DEBUG_DOMAIN,
814 "contact %s (%d) dragged from '%s' to '%s'",
815 empathy_contact_get_id (contact),
816 empathy_contact_get_handle (contact),
817 old_group, new_group);
819 list = empathy_contact_list_store_get_list_iface (priv->store);
821 empathy_contact_list_add_to_group (list, contact, new_group);
823 if (old_group && context->action == GDK_ACTION_MOVE) {
824 empathy_contact_list_remove_from_group (list, contact, old_group);
830 gtk_drag_finish (context, TRUE, FALSE, GDK_CURRENT_TIME);
834 contact_list_view_drag_motion (GtkWidget *widget,
835 GdkDragContext *context,
840 static DragMotionData *dm = NULL;
843 gboolean is_different = FALSE;
844 gboolean cleanup = TRUE;
846 is_row = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
857 cleanup &= (dm && gtk_tree_path_compare (dm->path, path) != 0);
858 is_different = (!dm || (dm && gtk_tree_path_compare (dm->path, path) != 0));
863 if (!is_different && !cleanup) {
868 gtk_tree_path_free (dm->path);
869 if (dm->timeout_id) {
870 g_source_remove (dm->timeout_id);
878 if (!gtk_tree_view_row_expanded (GTK_TREE_VIEW (widget), path)) {
879 dm = g_new0 (DragMotionData, 1);
881 dm->view = EMPATHY_CONTACT_LIST_VIEW (widget);
882 dm->path = gtk_tree_path_copy (path);
884 dm->timeout_id = g_timeout_add (
886 (GSourceFunc) contact_list_view_drag_motion_cb,
894 contact_list_view_drag_motion_cb (DragMotionData *data)
896 gtk_tree_view_expand_row (GTK_TREE_VIEW (data->view),
900 data->timeout_id = 0;
906 contact_list_view_drag_begin (GtkWidget *widget,
907 GdkDragContext *context)
909 EmpathyContactListViewPriv *priv;
910 GtkTreeSelection *selection;
915 priv = GET_PRIV (widget);
917 GTK_WIDGET_CLASS (empathy_contact_list_view_parent_class)->drag_begin (widget,
920 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (widget));
921 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
925 path = gtk_tree_model_get_path (model, &iter);
926 priv->drag_row = gtk_tree_row_reference_new (model, path);
927 gtk_tree_path_free (path);
931 contact_list_view_drag_data_get (GtkWidget *widget,
932 GdkDragContext *context,
933 GtkSelectionData *selection,
937 EmpathyContactListViewPriv *priv;
938 GtkTreePath *src_path;
941 EmpathyContact *contact;
943 const gchar *contact_id;
944 const gchar *account_id;
948 priv = GET_PRIV (widget);
950 model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
951 if (!priv->drag_row) {
955 src_path = gtk_tree_row_reference_get_path (priv->drag_row);
960 if (!gtk_tree_model_get_iter (model, &iter, src_path)) {
961 gtk_tree_path_free (src_path);
965 gtk_tree_path_free (src_path);
967 contact = empathy_contact_list_view_get_selected (EMPATHY_CONTACT_LIST_VIEW (widget));
972 account = empathy_contact_get_account (contact);
973 account_id = mc_account_get_unique_name (account);
974 contact_id = empathy_contact_get_id (contact);
975 g_object_unref (contact);
976 str = g_strconcat (account_id, "/", contact_id, NULL);
979 case DND_DRAG_TYPE_CONTACT_ID:
980 gtk_selection_data_set (selection, drag_atoms_source[info], 8,
981 (guchar*)str, strlen (str) + 1);
989 contact_list_view_drag_end (GtkWidget *widget,
990 GdkDragContext *context)
992 EmpathyContactListViewPriv *priv;
994 priv = GET_PRIV (widget);
996 GTK_WIDGET_CLASS (empathy_contact_list_view_parent_class)->drag_end (widget,
999 if (priv->drag_row) {
1000 gtk_tree_row_reference_free (priv->drag_row);
1001 priv->drag_row = NULL;
1006 contact_list_view_drag_drop (GtkWidget *widget,
1007 GdkDragContext *drag_context,
1016 contact_list_view_cell_set_background (EmpathyContactListView *view,
1017 GtkCellRenderer *cell,
1024 style = gtk_widget_get_style (GTK_WIDGET (view));
1028 color = style->bg[GTK_STATE_SELECTED];
1030 /* Here we take the current theme colour and add it to
1031 * the colour for white and average the two. This
1032 * gives a colour which is inline with the theme but
1035 color.red = (color.red + (style->white).red) / 2;
1036 color.green = (color.green + (style->white).green) / 2;
1037 color.blue = (color.blue + (style->white).blue) / 2;
1040 "cell-background-gdk", &color,
1044 "cell-background-gdk", NULL,
1049 "cell-background-gdk", NULL,
1055 contact_list_view_pixbuf_cell_data_func (GtkTreeViewColumn *tree_column,
1056 GtkCellRenderer *cell,
1057 GtkTreeModel *model,
1059 EmpathyContactListView *view)
1065 gtk_tree_model_get (model, iter,
1066 COL_IS_GROUP, &is_group,
1067 COL_IS_ACTIVE, &is_active,
1068 COL_ICON_STATUS, &icon_name,
1072 "visible", !is_group,
1073 "icon-name", icon_name,
1078 contact_list_view_cell_set_background (view, cell, is_group, is_active);
1082 contact_list_view_voip_cell_data_func (GtkTreeViewColumn *tree_column,
1083 GtkCellRenderer *cell,
1084 GtkTreeModel *model,
1086 EmpathyContactListView *view)
1092 gtk_tree_model_get (model, iter,
1093 COL_IS_GROUP, &is_group,
1094 COL_IS_ACTIVE, &is_active,
1095 COL_CAN_VOIP, &can_voip,
1099 "visible", !is_group && can_voip,
1100 "icon-name", EMPATHY_IMAGE_VOIP,
1103 contact_list_view_cell_set_background (view, cell, is_group, is_active);
1107 contact_list_view_avatar_cell_data_func (GtkTreeViewColumn *tree_column,
1108 GtkCellRenderer *cell,
1109 GtkTreeModel *model,
1111 EmpathyContactListView *view)
1114 gboolean show_avatar;
1118 gtk_tree_model_get (model, iter,
1119 COL_PIXBUF_AVATAR, &pixbuf,
1120 COL_PIXBUF_AVATAR_VISIBLE, &show_avatar,
1121 COL_IS_GROUP, &is_group,
1122 COL_IS_ACTIVE, &is_active,
1126 "visible", !is_group && show_avatar,
1131 g_object_unref (pixbuf);
1134 contact_list_view_cell_set_background (view, cell, is_group, is_active);
1138 contact_list_view_text_cell_data_func (GtkTreeViewColumn *tree_column,
1139 GtkCellRenderer *cell,
1140 GtkTreeModel *model,
1142 EmpathyContactListView *view)
1146 gboolean show_status;
1148 gtk_tree_model_get (model, iter,
1149 COL_IS_GROUP, &is_group,
1150 COL_IS_ACTIVE, &is_active,
1151 COL_STATUS_VISIBLE, &show_status,
1155 "show-status", show_status,
1158 contact_list_view_cell_set_background (view, cell, is_group, is_active);
1162 contact_list_view_expander_cell_data_func (GtkTreeViewColumn *column,
1163 GtkCellRenderer *cell,
1164 GtkTreeModel *model,
1166 EmpathyContactListView *view)
1171 gtk_tree_model_get (model, iter,
1172 COL_IS_GROUP, &is_group,
1173 COL_IS_ACTIVE, &is_active,
1176 if (gtk_tree_model_iter_has_child (model, iter)) {
1178 gboolean row_expanded;
1180 path = gtk_tree_model_get_path (model, iter);
1181 row_expanded = gtk_tree_view_row_expanded (GTK_TREE_VIEW (column->tree_view), path);
1182 gtk_tree_path_free (path);
1186 "expander-style", row_expanded ? GTK_EXPANDER_EXPANDED : GTK_EXPANDER_COLLAPSED,
1189 g_object_set (cell, "visible", FALSE, NULL);
1192 contact_list_view_cell_set_background (view, cell, is_group, is_active);
1196 contact_list_view_get_contact_menu (EmpathyContactListView *view,
1197 gboolean can_send_file,
1198 gboolean can_show_log,
1201 EmpathyContactListViewPriv *priv;
1205 priv = GET_PRIV (view);
1207 /* Sort out sensitive items */
1208 action = gtk_ui_manager_get_action (priv->ui, "/Contact/Log");
1209 gtk_action_set_sensitive (action, can_show_log);
1211 action = gtk_ui_manager_get_action (priv->ui, "/Contact/Call");
1212 gtk_action_set_sensitive (action, can_voip);
1214 action = gtk_ui_manager_get_action (priv->ui, "/Contact/SendFile");
1215 gtk_action_set_visible (action, can_send_file);
1217 widget = gtk_ui_manager_get_widget (priv->ui, "/Contact");
1223 contact_list_view_button_press_event_cb (EmpathyContactListView *view,
1224 GdkEventButton *event,
1227 EmpathyContactListViewPriv *priv;
1228 EmpathyContact *contact;
1230 GtkTreeSelection *selection;
1231 GtkTreeModel *model;
1233 gboolean row_exists;
1236 priv = GET_PRIV (view);
1238 if (!priv->interactive || event->button != 3) {
1242 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
1243 model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
1245 gtk_widget_grab_focus (GTK_WIDGET (view));
1247 row_exists = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (view),
1255 gtk_tree_selection_unselect_all (selection);
1256 gtk_tree_selection_select_path (selection, path);
1258 gtk_tree_model_get_iter (model, &iter, path);
1259 gtk_tree_path_free (path);
1261 gtk_tree_model_get (model, &iter, COL_CONTACT, &contact, -1);
1264 menu = empathy_contact_list_view_get_contact_menu (view, contact);
1265 g_object_unref (contact);
1267 menu = empathy_contact_list_view_get_group_menu (view);
1274 gtk_widget_show (menu);
1276 gtk_menu_popup (GTK_MENU (menu),
1277 NULL, NULL, NULL, NULL,
1278 event->button, event->time);
1284 contact_list_view_row_activated_cb (EmpathyContactListView *view,
1286 GtkTreeViewColumn *col,
1289 EmpathyContactListViewPriv *priv = GET_PRIV (view);
1290 EmpathyContact *contact;
1291 GtkTreeModel *model;
1294 if (!priv->interactive) {
1298 model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
1300 gtk_tree_model_get_iter (model, &iter, path);
1301 gtk_tree_model_get (model, &iter, COL_CONTACT, &contact, -1);
1304 contact_list_view_action_activated (view, contact);
1305 g_object_unref (contact);
1310 contact_list_view_voip_activated_cb (EmpathyCellRendererActivatable *cell,
1311 const gchar *path_string,
1312 EmpathyContactListView *view)
1314 EmpathyContactListViewPriv *priv = GET_PRIV (view);
1315 GtkTreeModel *model;
1317 EmpathyContact *contact;
1319 if (!priv->interactive) {
1323 model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
1324 if (!gtk_tree_model_get_iter_from_string (model, &iter, path_string)) {
1328 gtk_tree_model_get (model, &iter, COL_CONTACT, &contact, -1);
1331 contact_list_view_voip_activated (view, contact);
1332 g_object_unref (contact);
1338 contact_list_view_row_expand_or_collapse_cb (EmpathyContactListView *view,
1343 GtkTreeModel *model;
1347 model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
1349 gtk_tree_model_get (model, iter,
1353 expanded = GPOINTER_TO_INT (user_data);
1354 empathy_contact_group_set_expanded (name, expanded);
1360 contact_list_view_action_cb (GtkAction *action,
1361 EmpathyContactListView *view)
1363 EmpathyContactListViewPriv *priv;
1364 EmpathyContact *contact;
1369 priv = GET_PRIV (view);
1371 name = gtk_action_get_name (action);
1376 empathy_debug (DEBUG_DOMAIN, "Action:'%s' activated", name);
1378 contact = empathy_contact_list_view_get_selected (view);
1379 group = empathy_contact_list_view_get_selected_group (view);
1380 parent = empathy_get_toplevel_window (GTK_WIDGET (view));
1382 if (contact && strcmp (name, "Chat") == 0) {
1383 contact_list_view_action_activated (view, contact);
1385 else if (contact && strcmp (name, "Call") == 0) {
1386 contact_list_view_voip_activated (view, contact);
1388 else if (contact && strcmp (name, "Information") == 0) {
1389 empathy_contact_information_dialog_show (contact, parent, FALSE);
1391 else if (contact && strcmp (name, "Edit") == 0) {
1392 empathy_contact_information_dialog_show (contact, parent, TRUE);
1394 else if (contact && strcmp (name, "Remove") == 0) {
1395 /* FIXME: Ask for confirmation */
1396 EmpathyContactList *list;
1398 list = empathy_contact_list_store_get_list_iface (priv->store);
1399 empathy_contact_list_remove (list, contact,
1400 _("Sorry, I don't want you in my contact list anymore."));
1402 else if (contact && strcmp (name, "Invite") == 0) {
1404 else if (contact && strcmp (name, "SendFile") == 0) {
1406 else if (contact && strcmp (name, "Log") == 0) {
1407 empathy_log_window_show (empathy_contact_get_account (contact),
1408 empathy_contact_get_id (contact),
1412 else if (group && strcmp (name, "Rename") == 0) {
1417 g_object_unref (contact);
1422 contact_list_view_action_activated (EmpathyContactListView *view,
1423 EmpathyContact *contact)
1427 mc = empathy_mission_control_new ();
1428 mission_control_request_channel (mc,
1429 empathy_contact_get_account (contact),
1430 TP_IFACE_CHANNEL_TYPE_TEXT,
1431 empathy_contact_get_handle (contact),
1432 TP_HANDLE_TYPE_CONTACT,
1434 g_object_unref (mc);
1438 contact_list_view_voip_activated (EmpathyContactListView *view,
1439 EmpathyContact *contact)
1445 const gchar *bus_name;
1447 EmpathyTpGroup *group;
1450 /* StreamedMedia channels must have handle=0 and handle_type=none.
1451 * To call a contact we have to add him in the group interface of the
1452 * channel. MissionControl will detect the channel creation and
1453 * dispatch it to the VoIP chandler automatically. */
1455 mc = empathy_mission_control_new ();
1456 account = empathy_contact_get_account (contact);
1457 tp_conn = mission_control_get_connection (mc, account, NULL);
1458 /* FIXME: Should be async */
1459 if (!tp_conn_request_channel (DBUS_G_PROXY (tp_conn),
1460 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
1461 TP_HANDLE_TYPE_NONE,
1466 empathy_debug (DEBUG_DOMAIN,
1467 "Couldn't request channel: %s",
1468 error ? error->message : "No error given");
1469 g_clear_error (&error);
1470 g_object_unref (mc);
1471 g_object_unref (tp_conn);
1475 bus_name = dbus_g_proxy_get_bus_name (DBUS_G_PROXY (tp_conn));
1476 new_chan = tp_chan_new (tp_get_bus (),
1479 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
1480 TP_HANDLE_TYPE_NONE,
1483 /* FIXME: group is leaked, we can't unref it directly because
1484 * _add_member is async so we have to wait for it to return before
1485 * finalizing the group. I think EmpathyTpGroup should ref itself
1486 * when it does async calls to avoid finalizing when there is calls
1487 * in fligth like that we could unref it here. */
1488 group = empathy_tp_group_new (account, new_chan);
1489 empathy_tp_group_add_member (group, contact, "");
1491 g_object_unref (mc);
1492 g_object_unref (tp_conn);
1493 g_object_unref (new_chan);
1494 g_free (object_path);