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-2008 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>
24 * Bruno Dusausoy <bdusauso@beeznest.net>
31 #include <glib/gi18n.h>
33 #include <glade/glade.h>
35 #include <libmissioncontrol/mc-account.h>
36 #include <libmissioncontrol/mission-control.h>
38 #include <libempathy/empathy-contact-factory.h>
39 #include <libempathy/empathy-contact-list.h>
40 #include <libempathy/empathy-log-manager.h>
41 #include <libempathy/empathy-tp-group.h>
42 #include <libempathy/empathy-contact-groups.h>
43 #include <libempathy/empathy-debug.h>
44 #include <libempathy/empathy-utils.h>
46 #include "empathy-contact-list-view.h"
47 #include "empathy-contact-list-store.h"
48 #include "empathy-images.h"
49 #include "empathy-cell-renderer-expander.h"
50 #include "empathy-cell-renderer-text.h"
51 #include "empathy-cell-renderer-activatable.h"
52 #include "empathy-ui-utils.h"
53 #include "empathy-contact-dialogs.h"
54 //#include "empathy-chat-invite.h"
55 //#include "empathy-ft-window.h"
56 #include "empathy-log-window.h"
57 #include "empathy-gtk-enum-types.h"
58 #include "empathy-gtk-marshal.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))
72 EmpathyContactListStore *store;
74 GtkTreeRowReference *drag_row;
75 EmpathyContactListFeatures features;
76 } EmpathyContactListViewPriv;
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);
143 static void contact_list_view_voip_cell_data_func (GtkTreeViewColumn *tree_column,
144 GtkCellRenderer *cell,
147 EmpathyContactListView *view);
149 static void contact_list_view_avatar_cell_data_func (GtkTreeViewColumn *tree_column,
150 GtkCellRenderer *cell,
153 EmpathyContactListView *view);
154 static void contact_list_view_text_cell_data_func (GtkTreeViewColumn *tree_column,
155 GtkCellRenderer *cell,
158 EmpathyContactListView *view);
159 static void contact_list_view_expander_cell_data_func (GtkTreeViewColumn *column,
160 GtkCellRenderer *cell,
163 EmpathyContactListView *view);
164 static GtkWidget * contact_list_view_get_contact_menu (EmpathyContactListView *view,
165 gboolean can_send_file,
166 gboolean can_show_log,
168 static gboolean contact_list_view_button_press_event_cb (EmpathyContactListView *view,
169 GdkEventButton *event,
171 static void contact_list_view_row_activated_cb (EmpathyContactListView *view,
173 GtkTreeViewColumn *col,
176 static void contact_list_view_voip_activated_cb (EmpathyCellRendererActivatable *cell,
177 const gchar *path_string,
178 EmpathyContactListView *view);
180 static void contact_list_view_row_expand_or_collapse_cb (EmpathyContactListView *view,
184 static void contact_list_view_action_cb (GtkAction *action,
185 EmpathyContactListView *view);
186 static void contact_list_view_voip_activated (EmpathyContactListView *view,
187 EmpathyContact *contact);
188 static gboolean contact_list_view_remove_dialog_show (GtkWindow *parent,
189 const gchar *window_title,
197 static const GtkActionEntry entries[] = {
198 { "ContactMenu", NULL,
199 N_("_Contact"), NULL, NULL,
203 N_("_Group"),NULL, NULL,
206 { "Chat", EMPATHY_IMAGE_MESSAGE,
207 N_("_Chat"), NULL, N_("Chat with contact"),
208 G_CALLBACK (contact_list_view_action_cb)
210 { "Information", EMPATHY_IMAGE_CONTACT_INFORMATION,
211 N_("Infor_mation"), "<control>I", N_("View contact information"),
212 G_CALLBACK (contact_list_view_action_cb)
215 N_("Re_name"), NULL, N_("Rename"),
216 G_CALLBACK (contact_list_view_action_cb)
218 { "Edit", GTK_STOCK_EDIT,
219 N_("_Edit"), NULL, N_("Edit the groups and name for this contact"),
220 G_CALLBACK (contact_list_view_action_cb)
222 { "Remove", GTK_STOCK_REMOVE,
223 N_("_Remove"), NULL, N_("Remove contact"),
224 G_CALLBACK (contact_list_view_action_cb)
226 { "Invite", EMPATHY_IMAGE_GROUP_MESSAGE,
227 N_("_Invite to Chat Room"), NULL, N_("Invite to a currently open chat room"),
228 G_CALLBACK (contact_list_view_action_cb)
231 N_("_Send File..."), NULL, N_("Send a file"),
232 G_CALLBACK (contact_list_view_action_cb)
234 { "Log", EMPATHY_IMAGE_LOG,
235 N_("_View Previous Conversations"), NULL, N_("View previous conversations with this contact"),
236 G_CALLBACK (contact_list_view_action_cb)
239 { "Call", EMPATHY_IMAGE_VOIP,
240 N_("_Call"), NULL, N_("Start a voice or video conversation with this contact"),
241 G_CALLBACK (contact_list_view_action_cb)
246 static guint n_entries = G_N_ELEMENTS (entries);
248 static const gchar *ui_info =
250 " <popup name='Contact'>"
251 " <menuitem action='Chat'/>"
253 " <menuitem action='Call'/>"
255 " <menuitem action='Log'/>"
256 " <menuitem action='SendFile'/>"
258 " <menuitem action='Invite'/>"
260 " <menuitem action='Edit'/>"
261 " <menuitem action='Remove'/>"
263 " <menuitem action='Information'/>"
265 " <popup name='Group'>"
266 " <menuitem action='Rename'/>"
267 " <menuitem action='Remove'/>"
272 DND_DRAG_TYPE_CONTACT_ID,
274 DND_DRAG_TYPE_STRING,
277 static const GtkTargetEntry drag_types_dest[] = {
278 { "text/contact-id", 0, DND_DRAG_TYPE_CONTACT_ID },
279 { "text/uri-list", 0, DND_DRAG_TYPE_URL },
280 { "text/plain", 0, DND_DRAG_TYPE_STRING },
281 { "STRING", 0, DND_DRAG_TYPE_STRING },
284 static const GtkTargetEntry drag_types_source[] = {
285 { "text/contact-id", 0, DND_DRAG_TYPE_CONTACT_ID },
288 static GdkAtom drag_atoms_dest[G_N_ELEMENTS (drag_types_dest)];
289 static GdkAtom drag_atoms_source[G_N_ELEMENTS (drag_types_source)];
292 DRAG_CONTACT_RECEIVED,
296 static guint signals[LAST_SIGNAL];
298 G_DEFINE_TYPE (EmpathyContactListView, empathy_contact_list_view, GTK_TYPE_TREE_VIEW);
301 empathy_contact_list_view_class_init (EmpathyContactListViewClass *klass)
303 GObjectClass *object_class = G_OBJECT_CLASS (klass);
304 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
306 object_class->finalize = contact_list_view_finalize;
307 object_class->get_property = contact_list_view_get_property;
308 object_class->set_property = contact_list_view_set_property;
310 widget_class->drag_data_received = contact_list_view_drag_data_received;
311 widget_class->drag_drop = contact_list_view_drag_drop;
312 widget_class->drag_begin = contact_list_view_drag_begin;
313 widget_class->drag_data_get = contact_list_view_drag_data_get;
314 widget_class->drag_end = contact_list_view_drag_end;
315 /* FIXME: noticed but when you drag the row over the treeview
316 * fast, it seems to stop redrawing itself, if we don't
317 * connect this signal, all is fine.
319 widget_class->drag_motion = contact_list_view_drag_motion;
321 signals[DRAG_CONTACT_RECEIVED] =
322 g_signal_new ("drag-contact-received",
323 G_OBJECT_CLASS_TYPE (klass),
327 _empathy_gtk_marshal_VOID__OBJECT_STRING_STRING,
329 3, EMPATHY_TYPE_CONTACT, G_TYPE_STRING, G_TYPE_STRING);
331 g_object_class_install_property (object_class,
333 g_param_spec_flags ("features",
334 "Features of the view",
335 "Falgs for all enabled features",
336 EMPATHY_TYPE_CONTACT_LIST_FEATURES,
340 g_type_class_add_private (object_class, sizeof (EmpathyContactListViewPriv));
344 empathy_contact_list_view_init (EmpathyContactListView *view)
346 EmpathyContactListViewPriv *priv;
347 GtkActionGroup *action_group;
348 GError *error = NULL;
350 priv = GET_PRIV (view);
352 /* Get saved group states. */
353 empathy_contact_groups_get_all ();
355 /* Set up UI Manager */
356 priv->ui = gtk_ui_manager_new ();
358 action_group = gtk_action_group_new ("Actions");
359 gtk_action_group_set_translation_domain (action_group, GETTEXT_PACKAGE);
360 gtk_action_group_add_actions (action_group, entries, n_entries, view);
361 gtk_ui_manager_insert_action_group (priv->ui, action_group, 0);
363 if (!gtk_ui_manager_add_ui_from_string (priv->ui, ui_info, -1, &error)) {
364 g_warning ("Could not build contact menus from string:'%s'", error->message);
365 g_error_free (error);
368 g_object_unref (action_group);
370 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (view),
371 empathy_contact_list_store_row_separator_func,
374 /* Connect to tree view signals rather than override. */
375 g_signal_connect (view,
376 "button-press-event",
377 G_CALLBACK (contact_list_view_button_press_event_cb),
379 g_signal_connect (view,
381 G_CALLBACK (contact_list_view_row_activated_cb),
383 g_signal_connect (view,
385 G_CALLBACK (contact_list_view_row_expand_or_collapse_cb),
386 GINT_TO_POINTER (TRUE));
387 g_signal_connect (view,
389 G_CALLBACK (contact_list_view_row_expand_or_collapse_cb),
390 GINT_TO_POINTER (FALSE));
394 contact_list_view_finalize (GObject *object)
396 EmpathyContactListViewPriv *priv;
398 priv = GET_PRIV (object);
401 g_object_unref (priv->ui);
404 g_object_unref (priv->store);
407 G_OBJECT_CLASS (empathy_contact_list_view_parent_class)->finalize (object);
411 contact_list_view_get_property (GObject *object,
416 EmpathyContactListViewPriv *priv;
418 priv = GET_PRIV (object);
422 g_value_set_flags (value, priv->features);
425 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
431 contact_list_view_set_property (GObject *object,
436 EmpathyContactListView *view = EMPATHY_CONTACT_LIST_VIEW (object);
437 EmpathyContactListViewPriv *priv;
439 priv = GET_PRIV (object);
443 empathy_contact_list_view_set_features (view, g_value_get_flags (value));
446 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
451 EmpathyContactListView *
452 empathy_contact_list_view_new (EmpathyContactListStore *store,
453 EmpathyContactListFeatures features)
455 EmpathyContactListView *view;
456 EmpathyContactListViewPriv *priv;
458 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), NULL);
460 view = g_object_new (EMPATHY_TYPE_CONTACT_LIST_VIEW,
461 "features", features,
464 priv = GET_PRIV (view);
465 priv->store = g_object_ref (store);
466 contact_list_view_setup (EMPATHY_CONTACT_LIST_VIEW (view));
472 empathy_contact_list_view_set_features (EmpathyContactListView *view,
473 EmpathyContactListFeatures features)
475 EmpathyContactListViewPriv *priv = GET_PRIV (view);
477 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view));
479 priv->features = features;
481 /* Update DnD source/dest */
482 if (features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_DRAG) {
483 gtk_drag_source_set (GTK_WIDGET (view),
486 G_N_ELEMENTS (drag_types_source),
487 GDK_ACTION_MOVE | GDK_ACTION_COPY);
489 gtk_drag_source_unset (GTK_WIDGET (view));
493 if (features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_DROP) {
494 gtk_drag_dest_set (GTK_WIDGET (view),
495 GTK_DEST_DEFAULT_ALL,
497 G_N_ELEMENTS (drag_types_dest),
498 GDK_ACTION_MOVE | GDK_ACTION_COPY);
500 /* FIXME: URI could still be droped depending on FT feature */
501 gtk_drag_dest_unset (GTK_WIDGET (view));
504 g_object_notify (G_OBJECT (view), "features");
507 EmpathyContactListFeatures
508 empathy_contact_list_view_get_features (EmpathyContactListView *view)
510 EmpathyContactListViewPriv *priv = GET_PRIV (view);
512 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), FALSE);
514 return priv->features;
518 empathy_contact_list_view_get_selected (EmpathyContactListView *view)
520 EmpathyContactListViewPriv *priv;
521 GtkTreeSelection *selection;
524 EmpathyContact *contact;
526 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), NULL);
528 priv = GET_PRIV (view);
530 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
531 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
535 gtk_tree_model_get (model, &iter,
536 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
543 empathy_contact_list_view_get_selected_group (EmpathyContactListView *view)
545 EmpathyContactListViewPriv *priv;
546 GtkTreeSelection *selection;
552 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), NULL);
554 priv = GET_PRIV (view);
556 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
557 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
561 gtk_tree_model_get (model, &iter,
562 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
563 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
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,
626 "name", EMPATHY_CONTACT_LIST_STORE_COL_NAME);
627 gtk_tree_view_column_add_attribute (col, cell,
628 "status", EMPATHY_CONTACT_LIST_STORE_COL_STATUS);
629 gtk_tree_view_column_add_attribute (col, cell,
630 "is_group", EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP);
633 /* Voip Capability Icon */
634 cell = empathy_cell_renderer_activatable_new ();
635 gtk_tree_view_column_pack_start (col, cell, FALSE);
636 gtk_tree_view_column_set_cell_data_func (
638 (GtkTreeCellDataFunc) contact_list_view_voip_cell_data_func,
645 g_signal_connect (cell, "path-activated",
646 G_CALLBACK (contact_list_view_voip_activated_cb),
651 cell = gtk_cell_renderer_pixbuf_new ();
652 gtk_tree_view_column_pack_start (col, cell, FALSE);
653 gtk_tree_view_column_set_cell_data_func (
655 (GtkTreeCellDataFunc) contact_list_view_avatar_cell_data_func,
667 cell = empathy_cell_renderer_expander_new ();
668 gtk_tree_view_column_pack_end (col, cell, FALSE);
669 gtk_tree_view_column_set_cell_data_func (
671 (GtkTreeCellDataFunc) contact_list_view_expander_cell_data_func,
674 /* Actually add the column now we have added all cell renderers */
675 gtk_tree_view_append_column (GTK_TREE_VIEW (view), col);
678 for (i = 0; i < G_N_ELEMENTS (drag_types_dest); ++i) {
679 drag_atoms_dest[i] = gdk_atom_intern (drag_types_dest[i].target,
683 for (i = 0; i < G_N_ELEMENTS (drag_types_source); ++i) {
684 drag_atoms_source[i] = gdk_atom_intern (drag_types_source[i].target,
690 contact_list_view_row_has_child_toggled_cb (GtkTreeModel *model,
693 EmpathyContactListView *view)
695 EmpathyContactListViewPriv *priv = GET_PRIV (view);
696 gboolean is_group = FALSE;
699 gtk_tree_model_get (model, iter,
700 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
701 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
704 if (!is_group || G_STR_EMPTY (name)) {
709 if (!(priv->features & EMPATHY_CONTACT_LIST_FEATURE_GROUPS_SAVE) ||
710 empathy_contact_group_get_expanded (name)) {
711 g_signal_handlers_block_by_func (view,
712 contact_list_view_row_expand_or_collapse_cb,
713 GINT_TO_POINTER (TRUE));
714 gtk_tree_view_expand_row (GTK_TREE_VIEW (view), path, TRUE);
715 g_signal_handlers_unblock_by_func (view,
716 contact_list_view_row_expand_or_collapse_cb,
717 GINT_TO_POINTER (TRUE));
719 g_signal_handlers_block_by_func (view,
720 contact_list_view_row_expand_or_collapse_cb,
721 GINT_TO_POINTER (FALSE));
722 gtk_tree_view_collapse_row (GTK_TREE_VIEW (view), path);
723 g_signal_handlers_unblock_by_func (view,
724 contact_list_view_row_expand_or_collapse_cb,
725 GINT_TO_POINTER (FALSE));
732 contact_list_view_drag_data_received (GtkWidget *widget,
733 GdkDragContext *context,
736 GtkSelectionData *selection,
740 EmpathyContactListViewPriv *priv;
741 EmpathyContactList *list;
742 EmpathyContactFactory *factory;
746 GtkTreeViewDropPosition position;
747 EmpathyContact *contact = NULL;
750 gchar *new_group = NULL;
751 gchar *old_group = NULL;
754 priv = GET_PRIV (widget);
756 id = (const gchar*) selection->data;
757 empathy_debug (DEBUG_DOMAIN, "Received %s%s drag & drop contact from roster with id:'%s'",
758 context->action == GDK_ACTION_MOVE ? "move" : "",
759 context->action == GDK_ACTION_COPY ? "copy" : "",
762 strv = g_strsplit (id, "/", 2);
763 factory = empathy_contact_factory_new ();
764 account = mc_account_lookup (strv[0]);
766 contact = empathy_contact_factory_get_from_id (factory,
769 g_object_unref (account);
771 g_object_unref (factory);
775 empathy_debug (DEBUG_DOMAIN, "No contact found associated with drag & drop");
779 empathy_contact_run_until_ready (contact,
780 EMPATHY_CONTACT_READY_HANDLE,
783 model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
785 /* Get source group information. */
786 if (priv->drag_row) {
787 path = gtk_tree_row_reference_get_path (priv->drag_row);
789 old_group = empathy_contact_list_store_get_parent_group (model, path, NULL);
790 gtk_tree_path_free (path);
794 /* Get destination group information. */
795 is_row = gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget),
802 new_group = empathy_contact_list_store_get_parent_group (model, path, NULL);
803 gtk_tree_path_free (path);
806 empathy_debug (DEBUG_DOMAIN,
807 "contact %s (%d) dragged from '%s' to '%s'",
808 empathy_contact_get_id (contact),
809 empathy_contact_get_handle (contact),
810 old_group, new_group);
812 list = empathy_contact_list_store_get_list_iface (priv->store);
814 empathy_contact_list_add_to_group (list, contact, new_group);
816 if (old_group && context->action == GDK_ACTION_MOVE) {
817 empathy_contact_list_remove_from_group (list, contact, old_group);
823 gtk_drag_finish (context, TRUE, FALSE, GDK_CURRENT_TIME);
827 contact_list_view_drag_motion (GtkWidget *widget,
828 GdkDragContext *context,
833 static DragMotionData *dm = NULL;
836 gboolean is_different = FALSE;
837 gboolean cleanup = TRUE;
839 is_row = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
850 cleanup &= (dm && gtk_tree_path_compare (dm->path, path) != 0);
851 is_different = (!dm || (dm && gtk_tree_path_compare (dm->path, path) != 0));
856 if (!is_different && !cleanup) {
861 gtk_tree_path_free (dm->path);
862 if (dm->timeout_id) {
863 g_source_remove (dm->timeout_id);
871 if (!gtk_tree_view_row_expanded (GTK_TREE_VIEW (widget), path)) {
872 dm = g_new0 (DragMotionData, 1);
874 dm->view = EMPATHY_CONTACT_LIST_VIEW (widget);
875 dm->path = gtk_tree_path_copy (path);
877 dm->timeout_id = g_timeout_add_seconds (1,
878 (GSourceFunc) contact_list_view_drag_motion_cb,
886 contact_list_view_drag_motion_cb (DragMotionData *data)
888 gtk_tree_view_expand_row (GTK_TREE_VIEW (data->view),
892 data->timeout_id = 0;
898 contact_list_view_drag_begin (GtkWidget *widget,
899 GdkDragContext *context)
901 EmpathyContactListViewPriv *priv;
902 GtkTreeSelection *selection;
907 priv = GET_PRIV (widget);
909 GTK_WIDGET_CLASS (empathy_contact_list_view_parent_class)->drag_begin (widget,
912 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (widget));
913 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
917 path = gtk_tree_model_get_path (model, &iter);
918 priv->drag_row = gtk_tree_row_reference_new (model, path);
919 gtk_tree_path_free (path);
923 contact_list_view_drag_data_get (GtkWidget *widget,
924 GdkDragContext *context,
925 GtkSelectionData *selection,
929 EmpathyContactListViewPriv *priv;
930 GtkTreePath *src_path;
933 EmpathyContact *contact;
935 const gchar *contact_id;
936 const gchar *account_id;
939 priv = GET_PRIV (widget);
941 model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
942 if (!priv->drag_row) {
946 src_path = gtk_tree_row_reference_get_path (priv->drag_row);
951 if (!gtk_tree_model_get_iter (model, &iter, src_path)) {
952 gtk_tree_path_free (src_path);
956 gtk_tree_path_free (src_path);
958 contact = empathy_contact_list_view_get_selected (EMPATHY_CONTACT_LIST_VIEW (widget));
963 account = empathy_contact_get_account (contact);
964 account_id = mc_account_get_unique_name (account);
965 contact_id = empathy_contact_get_id (contact);
966 g_object_unref (contact);
967 str = g_strconcat (account_id, "/", contact_id, NULL);
970 case DND_DRAG_TYPE_CONTACT_ID:
971 gtk_selection_data_set (selection, drag_atoms_source[info], 8,
972 (guchar*)str, strlen (str) + 1);
980 contact_list_view_drag_end (GtkWidget *widget,
981 GdkDragContext *context)
983 EmpathyContactListViewPriv *priv;
985 priv = GET_PRIV (widget);
987 GTK_WIDGET_CLASS (empathy_contact_list_view_parent_class)->drag_end (widget,
990 if (priv->drag_row) {
991 gtk_tree_row_reference_free (priv->drag_row);
992 priv->drag_row = NULL;
997 contact_list_view_drag_drop (GtkWidget *widget,
998 GdkDragContext *drag_context,
1007 contact_list_view_cell_set_background (EmpathyContactListView *view,
1008 GtkCellRenderer *cell,
1015 style = gtk_widget_get_style (GTK_WIDGET (view));
1017 if (!is_group && is_active) {
1018 color = style->bg[GTK_STATE_SELECTED];
1020 /* Here we take the current theme colour and add it to
1021 * the colour for white and average the two. This
1022 * gives a colour which is inline with the theme but
1025 color.red = (color.red + (style->white).red) / 2;
1026 color.green = (color.green + (style->white).green) / 2;
1027 color.blue = (color.blue + (style->white).blue) / 2;
1030 "cell-background-gdk", &color,
1034 "cell-background-gdk", NULL,
1040 contact_list_view_pixbuf_cell_data_func (GtkTreeViewColumn *tree_column,
1041 GtkCellRenderer *cell,
1042 GtkTreeModel *model,
1044 EmpathyContactListView *view)
1050 gtk_tree_model_get (model, iter,
1051 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
1052 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, &is_active,
1053 EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, &icon_name,
1057 "visible", !is_group,
1058 "icon-name", icon_name,
1063 contact_list_view_cell_set_background (view, cell, is_group, is_active);
1068 contact_list_view_voip_cell_data_func (GtkTreeViewColumn *tree_column,
1069 GtkCellRenderer *cell,
1070 GtkTreeModel *model,
1072 EmpathyContactListView *view)
1078 gtk_tree_model_get (model, iter,
1079 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
1080 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, &is_active,
1081 EMPATHY_CONTACT_LIST_STORE_COL_CAN_VOIP, &can_voip,
1085 "visible", !is_group && can_voip,
1086 "icon-name", EMPATHY_IMAGE_VOIP,
1089 contact_list_view_cell_set_background (view, cell, is_group, is_active);
1094 contact_list_view_avatar_cell_data_func (GtkTreeViewColumn *tree_column,
1095 GtkCellRenderer *cell,
1096 GtkTreeModel *model,
1098 EmpathyContactListView *view)
1101 gboolean show_avatar;
1105 gtk_tree_model_get (model, iter,
1106 EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR, &pixbuf,
1107 EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR_VISIBLE, &show_avatar,
1108 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
1109 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, &is_active,
1113 "visible", !is_group && show_avatar,
1118 g_object_unref (pixbuf);
1121 contact_list_view_cell_set_background (view, cell, is_group, is_active);
1125 contact_list_view_text_cell_data_func (GtkTreeViewColumn *tree_column,
1126 GtkCellRenderer *cell,
1127 GtkTreeModel *model,
1129 EmpathyContactListView *view)
1133 gboolean show_status;
1135 gtk_tree_model_get (model, iter,
1136 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
1137 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, &is_active,
1138 EMPATHY_CONTACT_LIST_STORE_COL_STATUS_VISIBLE, &show_status,
1142 "show-status", show_status,
1145 contact_list_view_cell_set_background (view, cell, is_group, is_active);
1149 contact_list_view_expander_cell_data_func (GtkTreeViewColumn *column,
1150 GtkCellRenderer *cell,
1151 GtkTreeModel *model,
1153 EmpathyContactListView *view)
1158 gtk_tree_model_get (model, iter,
1159 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
1160 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, &is_active,
1163 if (gtk_tree_model_iter_has_child (model, iter)) {
1165 gboolean row_expanded;
1167 path = gtk_tree_model_get_path (model, iter);
1168 row_expanded = gtk_tree_view_row_expanded (GTK_TREE_VIEW (column->tree_view), path);
1169 gtk_tree_path_free (path);
1173 "expander-style", row_expanded ? GTK_EXPANDER_EXPANDED : GTK_EXPANDER_COLLAPSED,
1176 g_object_set (cell, "visible", FALSE, NULL);
1179 contact_list_view_cell_set_background (view, cell, is_group, is_active);
1183 contact_list_view_get_contact_menu (EmpathyContactListView *view,
1184 gboolean can_send_file,
1185 gboolean can_show_log,
1188 EmpathyContactListViewPriv *priv = GET_PRIV (view);
1191 if (!(priv->features & (EMPATHY_CONTACT_LIST_FEATURE_CONTACT_CHAT |
1192 EMPATHY_CONTACT_LIST_FEATURE_CONTACT_CALL |
1193 EMPATHY_CONTACT_LIST_FEATURE_CONTACT_LOG |
1194 EMPATHY_CONTACT_LIST_FEATURE_CONTACT_FT |
1195 EMPATHY_CONTACT_LIST_FEATURE_CONTACT_INVITE |
1196 EMPATHY_CONTACT_LIST_FEATURE_CONTACT_EDIT |
1197 EMPATHY_CONTACT_LIST_FEATURE_CONTACT_INFO |
1198 EMPATHY_CONTACT_LIST_FEATURE_CONTACT_REMOVE))) {
1202 /* Sort out sensitive/visible items */
1203 action = gtk_ui_manager_get_action (priv->ui, "/Contact/Chat");
1204 gtk_action_set_visible (action, priv->features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_CHAT);
1207 action = gtk_ui_manager_get_action (priv->ui, "/Contact/Call");
1208 gtk_action_set_sensitive (action, can_voip);
1209 gtk_action_set_visible (action, priv->features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_CALL);
1212 action = gtk_ui_manager_get_action (priv->ui, "/Contact/Log");
1213 gtk_action_set_sensitive (action, can_show_log);
1214 gtk_action_set_visible (action, priv->features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_LOG);
1217 action = gtk_ui_manager_get_action (priv->ui, "/Contact/SendFile");
1218 gtk_action_set_visible (action, can_send_file && (priv->features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_FT));
1220 action = gtk_ui_manager_get_action (priv->ui, "/Contact/Invite");
1221 gtk_action_set_visible (action, priv->features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_INVITE);
1223 action = gtk_ui_manager_get_action (priv->ui, "/Contact/Edit");
1224 gtk_action_set_visible (action, priv->features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_EDIT);
1226 action = gtk_ui_manager_get_action (priv->ui, "/Contact/Information");
1227 gtk_action_set_visible (action, priv->features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_INFO);
1229 action = gtk_ui_manager_get_action (priv->ui, "/Contact/Remove");
1230 gtk_action_set_visible (action, priv->features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_REMOVE);
1232 return gtk_ui_manager_get_widget (priv->ui, "/Contact");
1236 empathy_contact_list_view_get_group_menu (EmpathyContactListView *view)
1238 EmpathyContactListViewPriv *priv = GET_PRIV (view);
1241 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), NULL);
1243 if (!(priv->features & (EMPATHY_CONTACT_LIST_FEATURE_GROUPS_RENAME |
1244 EMPATHY_CONTACT_LIST_FEATURE_GROUPS_REMOVE))) {
1248 action = gtk_ui_manager_get_action (priv->ui, "/Group/Rename");
1249 gtk_action_set_visible (action, priv->features & EMPATHY_CONTACT_LIST_FEATURE_GROUPS_RENAME);
1251 action = gtk_ui_manager_get_action (priv->ui, "/Group/Remove");
1252 gtk_action_set_visible (action, priv->features & EMPATHY_CONTACT_LIST_FEATURE_GROUPS_REMOVE);
1254 return gtk_ui_manager_get_widget (priv->ui, "/Group");
1258 empathy_contact_list_view_get_contact_menu (EmpathyContactListView *view,
1259 EmpathyContact *contact)
1261 EmpathyLogManager *log_manager;
1262 gboolean can_show_log;
1263 gboolean can_send_file;
1266 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), NULL);
1267 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
1269 log_manager = empathy_log_manager_new ();
1270 can_show_log = empathy_log_manager_exists (log_manager,
1271 empathy_contact_get_account (contact),
1272 empathy_contact_get_id (contact),
1274 g_object_unref (log_manager);
1275 can_send_file = FALSE;
1276 can_voip = empathy_contact_can_voip (contact);
1278 return contact_list_view_get_contact_menu (view,
1285 contact_list_view_button_press_event_cb (EmpathyContactListView *view,
1286 GdkEventButton *event,
1289 EmpathyContactListViewPriv *priv;
1290 EmpathyContact *contact;
1292 GtkTreeSelection *selection;
1293 GtkTreeModel *model;
1295 gboolean row_exists;
1298 priv = GET_PRIV (view);
1300 if (event->button != 3) {
1304 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
1305 model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
1307 gtk_widget_grab_focus (GTK_WIDGET (view));
1309 row_exists = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (view),
1317 gtk_tree_selection_unselect_all (selection);
1318 gtk_tree_selection_select_path (selection, path);
1320 gtk_tree_model_get_iter (model, &iter, path);
1321 gtk_tree_path_free (path);
1323 gtk_tree_model_get (model, &iter,
1324 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
1328 menu = empathy_contact_list_view_get_contact_menu (view, contact);
1329 g_object_unref (contact);
1331 menu = empathy_contact_list_view_get_group_menu (view);
1338 gtk_widget_show (menu);
1340 gtk_menu_popup (GTK_MENU (menu),
1341 NULL, NULL, NULL, NULL,
1342 event->button, event->time);
1348 contact_list_view_row_activated_cb (EmpathyContactListView *view,
1350 GtkTreeViewColumn *col,
1353 EmpathyContactListViewPriv *priv = GET_PRIV (view);
1354 EmpathyContact *contact;
1355 GtkTreeModel *model;
1358 if (!(priv->features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_CHAT)) {
1362 model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
1364 gtk_tree_model_get_iter (model, &iter, path);
1365 gtk_tree_model_get (model, &iter,
1366 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
1370 empathy_chat_with_contact (contact);
1371 g_object_unref (contact);
1377 contact_list_view_voip_activated_cb (EmpathyCellRendererActivatable *cell,
1378 const gchar *path_string,
1379 EmpathyContactListView *view)
1381 EmpathyContactListViewPriv *priv = GET_PRIV (view);
1382 GtkTreeModel *model;
1384 EmpathyContact *contact;
1386 if (!(priv->features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_CALL)) {
1390 model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
1391 if (!gtk_tree_model_get_iter_from_string (model, &iter, path_string)) {
1395 gtk_tree_model_get (model, &iter,
1396 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
1400 contact_list_view_voip_activated (view, contact);
1401 g_object_unref (contact);
1408 contact_list_view_row_expand_or_collapse_cb (EmpathyContactListView *view,
1413 EmpathyContactListViewPriv *priv = GET_PRIV (view);
1414 GtkTreeModel *model;
1418 if (!(priv->features & EMPATHY_CONTACT_LIST_FEATURE_GROUPS_SAVE)) {
1422 model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
1424 gtk_tree_model_get (model, iter,
1425 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
1428 expanded = GPOINTER_TO_INT (user_data);
1429 empathy_contact_group_set_expanded (name, expanded);
1435 contact_list_view_action_cb (GtkAction *action,
1436 EmpathyContactListView *view)
1438 EmpathyContactListViewPriv *priv;
1439 EmpathyContact *contact;
1444 priv = GET_PRIV (view);
1446 name = gtk_action_get_name (action);
1451 empathy_debug (DEBUG_DOMAIN, "Action:'%s' activated", name);
1453 contact = empathy_contact_list_view_get_selected (view);
1454 group = empathy_contact_list_view_get_selected_group (view);
1455 parent = empathy_get_toplevel_window (GTK_WIDGET (view));
1457 if (contact && strcmp (name, "Chat") == 0) {
1458 empathy_chat_with_contact (contact);
1460 else if (contact && strcmp (name, "Call") == 0) {
1461 contact_list_view_voip_activated (view, contact);
1463 else if (contact && strcmp (name, "Information") == 0) {
1464 empathy_contact_information_dialog_show (contact, parent, FALSE, FALSE);
1466 else if (contact && strcmp (name, "Edit") == 0) {
1467 empathy_contact_information_dialog_show (contact, parent, TRUE, FALSE);
1469 else if (contact && strcmp (name, "Remove") == 0) {
1470 EmpathyContactList *list;
1473 text = g_strdup_printf (_("Do you really want to remove the contact '%s' ?"),
1474 empathy_contact_get_name (contact));
1476 /* TRUE if user wants to remove the contact. FALSE otherwise.*/
1477 if (contact_list_view_remove_dialog_show (parent, _("Removing contact"), text)) {
1478 list = empathy_contact_list_store_get_list_iface (priv->store);
1479 empathy_contact_list_remove (list, contact,
1480 _("Sorry, I don't want you in my contact list anymore."));
1485 else if (contact && strcmp (name, "Invite") == 0) {
1487 else if (contact && strcmp (name, "SendFile") == 0) {
1489 else if (contact && strcmp (name, "Log") == 0) {
1490 empathy_log_window_show (empathy_contact_get_account (contact),
1491 empathy_contact_get_id (contact),
1495 else if (group && strcmp (name, "Rename") == 0) {
1497 else if (group && strcmp (name, "Remove") == 0) {
1498 EmpathyContactList *list;
1501 text = g_strdup_printf (_("Do you really want to remove the group '%s' ?"), group);
1503 if (contact_list_view_remove_dialog_show (parent, _("Removing group"), text)) {
1504 list = empathy_contact_list_store_get_list_iface (priv->store);
1505 empathy_contact_list_remove_group (list, group);
1513 g_object_unref (contact);
1518 contact_list_view_remove_dialog_show (GtkWindow *parent,
1519 const gchar *window_title,
1522 GtkWidget *dialog, *label, *image, *hbox;
1525 dialog = gtk_dialog_new_with_buttons (window_title, parent,
1527 GTK_STOCK_DELETE, GTK_RESPONSE_YES,
1528 GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
1530 gtk_dialog_set_has_separator (GTK_DIALOG(dialog), FALSE);
1532 label = gtk_label_new (text);
1533 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_QUESTION, GTK_ICON_SIZE_DIALOG);
1535 hbox = gtk_hbox_new (FALSE, 5);
1536 gtk_container_set_border_width (GTK_CONTAINER(hbox), 5);
1537 gtk_box_pack_start_defaults (GTK_BOX(hbox), image);
1538 gtk_box_pack_start_defaults (GTK_BOX(hbox), label);
1540 gtk_box_pack_start_defaults (GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox);
1541 gtk_widget_show_all (dialog);
1543 res = gtk_dialog_run (GTK_DIALOG (dialog));
1545 gtk_widget_destroy (dialog);
1546 return (res == GTK_RESPONSE_YES);
1550 contact_list_view_voip_activated (EmpathyContactListView *view,
1551 EmpathyContact *contact)
1553 empathy_call_with_contact (contact);