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>
30 #include <glib/gi18n-lib.h>
31 #include <gdk/gdkkeysyms.h>
33 #include <glade/glade.h>
35 #include <libmissioncontrol/mc-account.h>
37 #include <libempathy/empathy-call-factory.h>
38 #include <libempathy/empathy-contact-factory.h>
39 #include <libempathy/empathy-contact-list.h>
40 #include <libempathy/empathy-contact-groups.h>
41 #include <libempathy/empathy-dispatcher.h>
42 #include <libempathy/empathy-utils.h>
44 #include "empathy-contact-list-view.h"
45 #include "empathy-contact-list-store.h"
46 #include "empathy-images.h"
47 #include "empathy-cell-renderer-expander.h"
48 #include "empathy-cell-renderer-text.h"
49 #include "empathy-cell-renderer-activatable.h"
50 #include "empathy-ui-utils.h"
51 #include "empathy-gtk-enum-types.h"
52 #include "empathy-gtk-marshal.h"
54 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
55 #include <libempathy/empathy-debug.h>
57 /* Active users are those which have recently changed state
58 * (e.g. online, offline or from normal to a busy state).
61 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyContactListView)
63 EmpathyContactListStore *store;
64 GtkTreeRowReference *drag_row;
65 EmpathyContactListFeatureFlags list_features;
66 EmpathyContactFeatureFlags contact_features;
67 GtkWidget *tooltip_widget;
68 } EmpathyContactListViewPriv;
71 EmpathyContactListView *view;
77 EmpathyContactListView *view;
78 EmpathyContact *contact;
86 PROP_CONTACT_FEATURES,
90 DND_DRAG_TYPE_CONTACT_ID,
95 static const GtkTargetEntry drag_types_dest[] = {
96 { "text/contact-id", 0, DND_DRAG_TYPE_CONTACT_ID },
97 { "text/uri-list", 0, DND_DRAG_TYPE_URL },
98 { "text/plain", 0, DND_DRAG_TYPE_STRING },
99 { "STRING", 0, DND_DRAG_TYPE_STRING },
102 static const GtkTargetEntry drag_types_source[] = {
103 { "text/contact-id", 0, DND_DRAG_TYPE_CONTACT_ID },
106 static GdkAtom drag_atoms_dest[G_N_ELEMENTS (drag_types_dest)];
107 static GdkAtom drag_atoms_source[G_N_ELEMENTS (drag_types_source)];
110 DRAG_CONTACT_RECEIVED,
114 static guint signals[LAST_SIGNAL];
116 G_DEFINE_TYPE (EmpathyContactListView, empathy_contact_list_view, GTK_TYPE_TREE_VIEW);
119 contact_list_view_tooltip_destroy_cb (GtkWidget *widget,
120 EmpathyContactListView *view)
122 EmpathyContactListViewPriv *priv = GET_PRIV (view);
124 if (priv->tooltip_widget) {
125 DEBUG ("Tooltip destroyed");
126 priv->tooltip_widget = NULL;
127 g_object_unref (widget);
132 contact_list_view_query_tooltip_cb (EmpathyContactListView *view,
135 gboolean keyboard_mode,
139 EmpathyContactListViewPriv *priv = GET_PRIV (view);
140 EmpathyContact *contact;
144 static gint running = 0;
145 gboolean ret = FALSE;
147 /* Avoid an infinite loop. See GNOME bug #574377 */
153 if (!gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (view), &x, &y,
155 &model, &path, &iter)) {
159 gtk_tree_view_set_tooltip_row (GTK_TREE_VIEW (view), tooltip, path);
160 gtk_tree_path_free (path);
162 gtk_tree_model_get (model, &iter,
163 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
169 if (!priv->tooltip_widget) {
170 priv->tooltip_widget = empathy_contact_widget_new (contact,
171 EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP);
172 g_object_ref (priv->tooltip_widget);
173 g_signal_connect (priv->tooltip_widget, "destroy",
174 G_CALLBACK (contact_list_view_tooltip_destroy_cb),
177 empathy_contact_widget_set_contact (priv->tooltip_widget,
181 gtk_tooltip_set_custom (tooltip, priv->tooltip_widget);
184 g_object_unref (contact);
192 contact_list_view_drag_data_received (GtkWidget *widget,
193 GdkDragContext *context,
196 GtkSelectionData *selection,
200 EmpathyContactListViewPriv *priv;
201 EmpathyContactList *list;
202 EmpathyContactFactory *factory;
206 GtkTreeViewDropPosition position;
207 EmpathyContact *contact = NULL;
210 gchar *new_group = NULL;
211 gchar *old_group = NULL;
214 priv = GET_PRIV (widget);
216 id = (const gchar*) selection->data;
217 DEBUG ("Received %s%s drag & drop contact from roster with id:'%s'",
218 context->action == GDK_ACTION_MOVE ? "move" : "",
219 context->action == GDK_ACTION_COPY ? "copy" : "",
222 strv = g_strsplit (id, "/", 2);
223 factory = empathy_contact_factory_dup_singleton ();
224 account = mc_account_lookup (strv[0]);
226 contact = empathy_contact_factory_get_from_id (factory,
229 g_object_unref (account);
231 g_object_unref (factory);
235 DEBUG ("No contact found associated with drag & drop");
239 empathy_contact_run_until_ready (contact,
240 EMPATHY_CONTACT_READY_HANDLE,
243 model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
245 /* Get source group information. */
246 if (priv->drag_row) {
247 path = gtk_tree_row_reference_get_path (priv->drag_row);
249 old_group = empathy_contact_list_store_get_parent_group (model, path, NULL);
250 gtk_tree_path_free (path);
254 /* Get destination group information. */
255 is_row = gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget),
262 new_group = empathy_contact_list_store_get_parent_group (model, path, NULL);
263 gtk_tree_path_free (path);
266 DEBUG ("contact %s (%d) dragged from '%s' to '%s'",
267 empathy_contact_get_id (contact),
268 empathy_contact_get_handle (contact),
269 old_group, new_group);
271 list = empathy_contact_list_store_get_list_iface (priv->store);
273 empathy_contact_list_add_to_group (list, contact, new_group);
275 if (old_group && context->action == GDK_ACTION_MOVE) {
276 empathy_contact_list_remove_from_group (list, contact, old_group);
282 gtk_drag_finish (context, TRUE, FALSE, GDK_CURRENT_TIME);
286 contact_list_view_drag_motion_cb (DragMotionData *data)
288 gtk_tree_view_expand_row (GTK_TREE_VIEW (data->view),
292 data->timeout_id = 0;
298 contact_list_view_drag_motion (GtkWidget *widget,
299 GdkDragContext *context,
304 static DragMotionData *dm = NULL;
307 gboolean is_different = FALSE;
308 gboolean cleanup = TRUE;
310 is_row = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
321 cleanup &= (dm && gtk_tree_path_compare (dm->path, path) != 0);
322 is_different = (!dm || (dm && gtk_tree_path_compare (dm->path, path) != 0));
327 if (!is_different && !cleanup) {
332 gtk_tree_path_free (dm->path);
333 if (dm->timeout_id) {
334 g_source_remove (dm->timeout_id);
342 if (!gtk_tree_view_row_expanded (GTK_TREE_VIEW (widget), path)) {
343 dm = g_new0 (DragMotionData, 1);
345 dm->view = EMPATHY_CONTACT_LIST_VIEW (widget);
346 dm->path = gtk_tree_path_copy (path);
348 dm->timeout_id = g_timeout_add_seconds (1,
349 (GSourceFunc) contact_list_view_drag_motion_cb,
357 contact_list_view_drag_begin (GtkWidget *widget,
358 GdkDragContext *context)
360 EmpathyContactListViewPriv *priv;
361 GtkTreeSelection *selection;
366 priv = GET_PRIV (widget);
368 GTK_WIDGET_CLASS (empathy_contact_list_view_parent_class)->drag_begin (widget,
371 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (widget));
372 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
376 path = gtk_tree_model_get_path (model, &iter);
377 priv->drag_row = gtk_tree_row_reference_new (model, path);
378 gtk_tree_path_free (path);
382 contact_list_view_drag_data_get (GtkWidget *widget,
383 GdkDragContext *context,
384 GtkSelectionData *selection,
388 EmpathyContactListViewPriv *priv;
389 GtkTreePath *src_path;
392 EmpathyContact *contact;
394 const gchar *contact_id;
395 const gchar *account_id;
398 priv = GET_PRIV (widget);
400 model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
401 if (!priv->drag_row) {
405 src_path = gtk_tree_row_reference_get_path (priv->drag_row);
410 if (!gtk_tree_model_get_iter (model, &iter, src_path)) {
411 gtk_tree_path_free (src_path);
415 gtk_tree_path_free (src_path);
417 contact = empathy_contact_list_view_get_selected (EMPATHY_CONTACT_LIST_VIEW (widget));
422 account = empathy_contact_get_account (contact);
423 account_id = mc_account_get_unique_name (account);
424 contact_id = empathy_contact_get_id (contact);
425 g_object_unref (contact);
426 str = g_strconcat (account_id, "/", contact_id, NULL);
429 case DND_DRAG_TYPE_CONTACT_ID:
430 gtk_selection_data_set (selection, drag_atoms_source[info], 8,
431 (guchar*)str, strlen (str) + 1);
439 contact_list_view_drag_end (GtkWidget *widget,
440 GdkDragContext *context)
442 EmpathyContactListViewPriv *priv;
444 priv = GET_PRIV (widget);
446 GTK_WIDGET_CLASS (empathy_contact_list_view_parent_class)->drag_end (widget,
449 if (priv->drag_row) {
450 gtk_tree_row_reference_free (priv->drag_row);
451 priv->drag_row = NULL;
456 contact_list_view_drag_drop (GtkWidget *widget,
457 GdkDragContext *drag_context,
466 EmpathyContactListView *view;
472 contact_list_view_popup_menu_idle_cb (gpointer user_data)
474 MenuPopupData *data = user_data;
477 menu = empathy_contact_list_view_get_contact_menu (data->view);
479 menu = empathy_contact_list_view_get_group_menu (data->view);
483 gtk_widget_show (menu);
484 gtk_menu_popup (GTK_MENU (menu),
485 NULL, NULL, NULL, NULL,
486 data->button, data->time);
489 g_slice_free (MenuPopupData, data);
495 contact_list_view_button_press_event_cb (EmpathyContactListView *view,
496 GdkEventButton *event,
499 if (event->button == 3) {
502 data = g_slice_new (MenuPopupData);
504 data->button = event->button;
505 data->time = event->time;
506 g_idle_add (contact_list_view_popup_menu_idle_cb, data);
513 contact_list_view_key_press_event_cb (EmpathyContactListView *view,
517 if (event->keyval == GDK_Menu) {
520 data = g_slice_new (MenuPopupData);
523 data->time = event->time;
524 g_idle_add (contact_list_view_popup_menu_idle_cb, data);
531 contact_list_view_row_activated (GtkTreeView *view,
533 GtkTreeViewColumn *column)
535 EmpathyContactListViewPriv *priv = GET_PRIV (view);
536 EmpathyContact *contact;
540 if (!(priv->contact_features & EMPATHY_CONTACT_FEATURE_CHAT)) {
544 model = GTK_TREE_MODEL (priv->store);
545 gtk_tree_model_get_iter (model, &iter, path);
546 gtk_tree_model_get (model, &iter,
547 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
551 DEBUG ("Starting a chat");
552 empathy_dispatcher_chat_with_contact (contact, NULL, NULL);
553 g_object_unref (contact);
558 contact_list_view_voip_activated_cb (EmpathyCellRendererActivatable *cell,
559 const gchar *path_string,
560 EmpathyContactListView *view)
562 EmpathyContactListViewPriv *priv = GET_PRIV (view);
565 EmpathyContact *contact;
567 if (!(priv->contact_features & EMPATHY_CONTACT_FEATURE_CALL)) {
571 model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
572 if (!gtk_tree_model_get_iter_from_string (model, &iter, path_string)) {
576 gtk_tree_model_get (model, &iter,
577 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
581 EmpathyCallFactory *factory;
583 factory = empathy_call_factory_get ();
584 empathy_call_factory_new_call (factory, contact);
586 g_object_unref (contact);
591 contact_list_view_cell_set_background (EmpathyContactListView *view,
592 GtkCellRenderer *cell,
599 style = gtk_widget_get_style (GTK_WIDGET (view));
601 if (!is_group && is_active) {
602 color = style->bg[GTK_STATE_SELECTED];
604 /* Here we take the current theme colour and add it to
605 * the colour for white and average the two. This
606 * gives a colour which is inline with the theme but
609 color.red = (color.red + (style->white).red) / 2;
610 color.green = (color.green + (style->white).green) / 2;
611 color.blue = (color.blue + (style->white).blue) / 2;
614 "cell-background-gdk", &color,
618 "cell-background-gdk", NULL,
624 contact_list_view_pixbuf_cell_data_func (GtkTreeViewColumn *tree_column,
625 GtkCellRenderer *cell,
628 EmpathyContactListView *view)
634 gtk_tree_model_get (model, iter,
635 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
636 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, &is_active,
637 EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, &icon_name,
641 "visible", !is_group,
642 "icon-name", icon_name,
647 contact_list_view_cell_set_background (view, cell, is_group, is_active);
651 contact_list_view_voip_cell_data_func (GtkTreeViewColumn *tree_column,
652 GtkCellRenderer *cell,
655 EmpathyContactListView *view)
661 gtk_tree_model_get (model, iter,
662 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
663 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, &is_active,
664 EMPATHY_CONTACT_LIST_STORE_COL_CAN_VOIP, &can_voip,
668 "visible", !is_group && can_voip,
669 "icon-name", EMPATHY_IMAGE_VOIP,
672 contact_list_view_cell_set_background (view, cell, is_group, is_active);
676 contact_list_view_avatar_cell_data_func (GtkTreeViewColumn *tree_column,
677 GtkCellRenderer *cell,
680 EmpathyContactListView *view)
683 gboolean show_avatar;
687 gtk_tree_model_get (model, iter,
688 EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR, &pixbuf,
689 EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR_VISIBLE, &show_avatar,
690 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
691 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, &is_active,
695 "visible", !is_group && show_avatar,
700 g_object_unref (pixbuf);
703 contact_list_view_cell_set_background (view, cell, is_group, is_active);
707 contact_list_view_text_cell_data_func (GtkTreeViewColumn *tree_column,
708 GtkCellRenderer *cell,
711 EmpathyContactListView *view)
715 gboolean show_status;
717 gtk_tree_model_get (model, iter,
718 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
719 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, &is_active,
720 EMPATHY_CONTACT_LIST_STORE_COL_STATUS_VISIBLE, &show_status,
724 "show-status", show_status,
727 contact_list_view_cell_set_background (view, cell, is_group, is_active);
731 contact_list_view_expander_cell_data_func (GtkTreeViewColumn *column,
732 GtkCellRenderer *cell,
735 EmpathyContactListView *view)
740 gtk_tree_model_get (model, iter,
741 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
742 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, &is_active,
745 if (gtk_tree_model_iter_has_child (model, iter)) {
747 gboolean row_expanded;
749 path = gtk_tree_model_get_path (model, iter);
750 row_expanded = gtk_tree_view_row_expanded (GTK_TREE_VIEW (column->tree_view), path);
751 gtk_tree_path_free (path);
755 "expander-style", row_expanded ? GTK_EXPANDER_EXPANDED : GTK_EXPANDER_COLLAPSED,
758 g_object_set (cell, "visible", FALSE, NULL);
761 contact_list_view_cell_set_background (view, cell, is_group, is_active);
765 contact_list_view_row_expand_or_collapse_cb (EmpathyContactListView *view,
770 EmpathyContactListViewPriv *priv = GET_PRIV (view);
775 if (!(priv->list_features & EMPATHY_CONTACT_LIST_FEATURE_GROUPS_SAVE)) {
779 model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
781 gtk_tree_model_get (model, iter,
782 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
785 expanded = GPOINTER_TO_INT (user_data);
786 empathy_contact_group_set_expanded (name, expanded);
792 contact_list_view_row_has_child_toggled_cb (GtkTreeModel *model,
795 EmpathyContactListView *view)
797 EmpathyContactListViewPriv *priv = GET_PRIV (view);
798 gboolean is_group = FALSE;
801 gtk_tree_model_get (model, iter,
802 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
803 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
806 if (!is_group || EMP_STR_EMPTY (name)) {
811 if (!(priv->list_features & EMPATHY_CONTACT_LIST_FEATURE_GROUPS_SAVE) ||
812 empathy_contact_group_get_expanded (name)) {
813 g_signal_handlers_block_by_func (view,
814 contact_list_view_row_expand_or_collapse_cb,
815 GINT_TO_POINTER (TRUE));
816 gtk_tree_view_expand_row (GTK_TREE_VIEW (view), path, TRUE);
817 g_signal_handlers_unblock_by_func (view,
818 contact_list_view_row_expand_or_collapse_cb,
819 GINT_TO_POINTER (TRUE));
821 g_signal_handlers_block_by_func (view,
822 contact_list_view_row_expand_or_collapse_cb,
823 GINT_TO_POINTER (FALSE));
824 gtk_tree_view_collapse_row (GTK_TREE_VIEW (view), path);
825 g_signal_handlers_unblock_by_func (view,
826 contact_list_view_row_expand_or_collapse_cb,
827 GINT_TO_POINTER (FALSE));
834 contact_list_view_setup (EmpathyContactListView *view)
836 EmpathyContactListViewPriv *priv;
837 GtkCellRenderer *cell;
838 GtkTreeViewColumn *col;
841 priv = GET_PRIV (view);
843 gtk_tree_view_set_search_equal_func (GTK_TREE_VIEW (view),
844 empathy_contact_list_store_search_equal_func,
847 g_signal_connect (priv->store, "row-has-child-toggled",
848 G_CALLBACK (contact_list_view_row_has_child_toggled_cb),
850 gtk_tree_view_set_model (GTK_TREE_VIEW (view),
851 GTK_TREE_MODEL (priv->store));
855 "headers-visible", FALSE,
857 "show-expanders", FALSE,
860 col = gtk_tree_view_column_new ();
863 cell = gtk_cell_renderer_pixbuf_new ();
864 gtk_tree_view_column_pack_start (col, cell, FALSE);
865 gtk_tree_view_column_set_cell_data_func (
867 (GtkTreeCellDataFunc) contact_list_view_pixbuf_cell_data_func,
877 cell = empathy_cell_renderer_text_new ();
878 gtk_tree_view_column_pack_start (col, cell, TRUE);
879 gtk_tree_view_column_set_cell_data_func (
881 (GtkTreeCellDataFunc) contact_list_view_text_cell_data_func,
884 gtk_tree_view_column_add_attribute (col, cell,
885 "name", EMPATHY_CONTACT_LIST_STORE_COL_NAME);
886 gtk_tree_view_column_add_attribute (col, cell,
887 "status", EMPATHY_CONTACT_LIST_STORE_COL_STATUS);
888 gtk_tree_view_column_add_attribute (col, cell,
889 "is_group", EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP);
891 /* Voip Capability Icon */
892 cell = empathy_cell_renderer_activatable_new ();
893 gtk_tree_view_column_pack_start (col, cell, FALSE);
894 gtk_tree_view_column_set_cell_data_func (
896 (GtkTreeCellDataFunc) contact_list_view_voip_cell_data_func,
903 g_signal_connect (cell, "path-activated",
904 G_CALLBACK (contact_list_view_voip_activated_cb),
908 cell = gtk_cell_renderer_pixbuf_new ();
909 gtk_tree_view_column_pack_start (col, cell, FALSE);
910 gtk_tree_view_column_set_cell_data_func (
912 (GtkTreeCellDataFunc) contact_list_view_avatar_cell_data_func,
924 cell = empathy_cell_renderer_expander_new ();
925 gtk_tree_view_column_pack_end (col, cell, FALSE);
926 gtk_tree_view_column_set_cell_data_func (
928 (GtkTreeCellDataFunc) contact_list_view_expander_cell_data_func,
931 /* Actually add the column now we have added all cell renderers */
932 gtk_tree_view_append_column (GTK_TREE_VIEW (view), col);
935 for (i = 0; i < G_N_ELEMENTS (drag_types_dest); ++i) {
936 drag_atoms_dest[i] = gdk_atom_intern (drag_types_dest[i].target,
940 for (i = 0; i < G_N_ELEMENTS (drag_types_source); ++i) {
941 drag_atoms_source[i] = gdk_atom_intern (drag_types_source[i].target,
947 contact_list_view_set_list_features (EmpathyContactListView *view,
948 EmpathyContactListFeatureFlags features)
950 EmpathyContactListViewPriv *priv = GET_PRIV (view);
951 gboolean has_tooltip;
953 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view));
955 priv->list_features = features;
957 /* Update DnD source/dest */
958 if (features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_DRAG) {
959 gtk_drag_source_set (GTK_WIDGET (view),
962 G_N_ELEMENTS (drag_types_source),
963 GDK_ACTION_MOVE | GDK_ACTION_COPY);
965 gtk_drag_source_unset (GTK_WIDGET (view));
969 if (features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_DROP) {
970 gtk_drag_dest_set (GTK_WIDGET (view),
971 GTK_DEST_DEFAULT_ALL,
973 G_N_ELEMENTS (drag_types_dest),
974 GDK_ACTION_MOVE | GDK_ACTION_COPY);
976 /* FIXME: URI could still be droped depending on FT feature */
977 gtk_drag_dest_unset (GTK_WIDGET (view));
980 /* Update has-tooltip */
981 has_tooltip = (features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_TOOLTIP) != 0;
982 gtk_widget_set_has_tooltip (GTK_WIDGET (view), has_tooltip);
986 contact_list_view_finalize (GObject *object)
988 EmpathyContactListViewPriv *priv;
990 priv = GET_PRIV (object);
993 g_object_unref (priv->store);
995 if (priv->tooltip_widget) {
996 gtk_widget_destroy (priv->tooltip_widget);
999 G_OBJECT_CLASS (empathy_contact_list_view_parent_class)->finalize (object);
1003 contact_list_view_get_property (GObject *object,
1008 EmpathyContactListViewPriv *priv;
1010 priv = GET_PRIV (object);
1014 g_value_set_object (value, priv->store);
1016 case PROP_LIST_FEATURES:
1017 g_value_set_flags (value, priv->list_features);
1019 case PROP_CONTACT_FEATURES:
1020 g_value_set_flags (value, priv->contact_features);
1023 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1029 contact_list_view_set_property (GObject *object,
1031 const GValue *value,
1034 EmpathyContactListView *view = EMPATHY_CONTACT_LIST_VIEW (object);
1035 EmpathyContactListViewPriv *priv = GET_PRIV (object);
1039 priv->store = g_value_dup_object (value);
1040 contact_list_view_setup (view);
1042 case PROP_LIST_FEATURES:
1043 contact_list_view_set_list_features (view, g_value_get_flags (value));
1045 case PROP_CONTACT_FEATURES:
1046 priv->contact_features = g_value_get_flags (value);
1049 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1055 empathy_contact_list_view_class_init (EmpathyContactListViewClass *klass)
1057 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1058 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1059 GtkTreeViewClass *tree_view_class = GTK_TREE_VIEW_CLASS (klass);
1061 object_class->finalize = contact_list_view_finalize;
1062 object_class->get_property = contact_list_view_get_property;
1063 object_class->set_property = contact_list_view_set_property;
1065 widget_class->drag_data_received = contact_list_view_drag_data_received;
1066 widget_class->drag_drop = contact_list_view_drag_drop;
1067 widget_class->drag_begin = contact_list_view_drag_begin;
1068 widget_class->drag_data_get = contact_list_view_drag_data_get;
1069 widget_class->drag_end = contact_list_view_drag_end;
1070 widget_class->drag_motion = contact_list_view_drag_motion;
1072 /* We use the class method to let user of this widget to connect to
1073 * the signal and stop emission of the signal so the default handler
1074 * won't be called. */
1075 tree_view_class->row_activated = contact_list_view_row_activated;
1077 signals[DRAG_CONTACT_RECEIVED] =
1078 g_signal_new ("drag-contact-received",
1079 G_OBJECT_CLASS_TYPE (klass),
1083 _empathy_gtk_marshal_VOID__OBJECT_STRING_STRING,
1085 3, EMPATHY_TYPE_CONTACT, G_TYPE_STRING, G_TYPE_STRING);
1087 g_object_class_install_property (object_class,
1089 g_param_spec_object ("store",
1090 "The store of the view",
1091 "The store of the view",
1092 EMPATHY_TYPE_CONTACT_LIST_STORE,
1093 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
1094 g_object_class_install_property (object_class,
1096 g_param_spec_flags ("list-features",
1097 "Features of the view",
1098 "Falgs for all enabled features",
1099 EMPATHY_TYPE_CONTACT_LIST_FEATURE_FLAGS,
1100 EMPATHY_CONTACT_LIST_FEATURE_NONE,
1101 G_PARAM_READWRITE));
1102 g_object_class_install_property (object_class,
1103 PROP_CONTACT_FEATURES,
1104 g_param_spec_flags ("contact-features",
1105 "Features of the contact menu",
1106 "Falgs for all enabled features for the menu",
1107 EMPATHY_TYPE_CONTACT_FEATURE_FLAGS,
1108 EMPATHY_CONTACT_FEATURE_NONE,
1109 G_PARAM_READWRITE));
1111 g_type_class_add_private (object_class, sizeof (EmpathyContactListViewPriv));
1115 empathy_contact_list_view_init (EmpathyContactListView *view)
1117 EmpathyContactListViewPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (view,
1118 EMPATHY_TYPE_CONTACT_LIST_VIEW, EmpathyContactListViewPriv);
1121 /* Get saved group states. */
1122 empathy_contact_groups_get_all ();
1124 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (view),
1125 empathy_contact_list_store_row_separator_func,
1128 /* Connect to tree view signals rather than override. */
1129 g_signal_connect (view, "button-press-event",
1130 G_CALLBACK (contact_list_view_button_press_event_cb),
1132 g_signal_connect (view, "key-press-event",
1133 G_CALLBACK (contact_list_view_key_press_event_cb),
1135 g_signal_connect (view, "row-expanded",
1136 G_CALLBACK (contact_list_view_row_expand_or_collapse_cb),
1137 GINT_TO_POINTER (TRUE));
1138 g_signal_connect (view, "row-collapsed",
1139 G_CALLBACK (contact_list_view_row_expand_or_collapse_cb),
1140 GINT_TO_POINTER (FALSE));
1141 g_signal_connect (view, "query-tooltip",
1142 G_CALLBACK (contact_list_view_query_tooltip_cb),
1146 EmpathyContactListView *
1147 empathy_contact_list_view_new (EmpathyContactListStore *store,
1148 EmpathyContactListFeatureFlags list_features,
1149 EmpathyContactFeatureFlags contact_features)
1151 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), NULL);
1153 return g_object_new (EMPATHY_TYPE_CONTACT_LIST_VIEW,
1155 "contact-features", contact_features,
1156 "list-features", list_features,
1161 empathy_contact_list_view_get_selected (EmpathyContactListView *view)
1163 EmpathyContactListViewPriv *priv;
1164 GtkTreeSelection *selection;
1166 GtkTreeModel *model;
1167 EmpathyContact *contact;
1169 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), NULL);
1171 priv = GET_PRIV (view);
1173 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
1174 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
1178 gtk_tree_model_get (model, &iter,
1179 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
1186 empathy_contact_list_view_get_selected_group (EmpathyContactListView *view)
1188 EmpathyContactListViewPriv *priv;
1189 GtkTreeSelection *selection;
1191 GtkTreeModel *model;
1195 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), NULL);
1197 priv = GET_PRIV (view);
1199 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
1200 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
1204 gtk_tree_model_get (model, &iter,
1205 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
1206 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
1218 contact_list_view_remove_dialog_show (GtkWindow *parent,
1219 const gchar *message,
1220 const gchar *secondary_text)
1225 dialog = gtk_message_dialog_new (parent, GTK_DIALOG_MODAL,
1226 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
1228 gtk_dialog_add_buttons (GTK_DIALOG (dialog),
1229 GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
1230 GTK_STOCK_DELETE, GTK_RESPONSE_YES,
1232 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
1233 "%s", secondary_text);
1235 gtk_widget_show (dialog);
1237 res = gtk_dialog_run (GTK_DIALOG (dialog));
1238 gtk_widget_destroy (dialog);
1240 return (res == GTK_RESPONSE_YES);
1244 contact_list_view_group_remove_activate_cb (GtkMenuItem *menuitem,
1245 EmpathyContactListView *view)
1247 EmpathyContactListViewPriv *priv = GET_PRIV (view);
1250 group = empathy_contact_list_view_get_selected_group (view);
1255 text = g_strdup_printf (_("Do you really want to remove the group '%s'?"), group);
1256 parent = empathy_get_toplevel_window (GTK_WIDGET (view));
1257 if (contact_list_view_remove_dialog_show (parent, _("Removing group"), text)) {
1258 EmpathyContactList *list;
1260 list = empathy_contact_list_store_get_list_iface (priv->store);
1261 empathy_contact_list_remove_group (list, group);
1271 empathy_contact_list_view_get_group_menu (EmpathyContactListView *view)
1273 EmpathyContactListViewPriv *priv = GET_PRIV (view);
1279 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), NULL);
1281 if (!(priv->list_features & (EMPATHY_CONTACT_LIST_FEATURE_GROUPS_RENAME |
1282 EMPATHY_CONTACT_LIST_FEATURE_GROUPS_REMOVE))) {
1286 group = empathy_contact_list_view_get_selected_group (view);
1291 menu = gtk_menu_new ();
1293 /* FIXME: Not implemented yet
1294 if (priv->features & EMPATHY_CONTACT_LIST_FEATURE_GROUPS_RENAME) {
1295 item = gtk_menu_item_new_with_mnemonic (_("Re_name"));
1296 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1297 gtk_widget_show (item);
1298 g_signal_connect (item, "activate",
1299 G_CALLBACK (contact_list_view_group_rename_activate_cb),
1303 if (priv->list_features & EMPATHY_CONTACT_LIST_FEATURE_GROUPS_REMOVE) {
1304 item = gtk_image_menu_item_new_with_mnemonic (_("_Remove"));
1305 image = gtk_image_new_from_icon_name (GTK_STOCK_REMOVE,
1306 GTK_ICON_SIZE_MENU);
1307 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1308 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1309 gtk_widget_show (item);
1310 g_signal_connect (item, "activate",
1311 G_CALLBACK (contact_list_view_group_remove_activate_cb),
1321 contact_list_view_remove_activate_cb (GtkMenuItem *menuitem,
1322 EmpathyContactListView *view)
1324 EmpathyContactListViewPriv *priv = GET_PRIV (view);
1325 EmpathyContact *contact;
1327 contact = empathy_contact_list_view_get_selected (view);
1333 parent = empathy_get_toplevel_window (GTK_WIDGET (view));
1334 text = g_strdup_printf (_("Do you really want to remove the contact '%s'?"),
1335 empathy_contact_get_name (contact));
1336 if (contact_list_view_remove_dialog_show (parent, _("Removing contact"), text)) {
1337 EmpathyContactList *list;
1339 list = empathy_contact_list_store_get_list_iface (priv->store);
1340 empathy_contact_list_remove (list, contact,
1341 _("Sorry, I don't want you in my contact list anymore."));
1345 g_object_unref (contact);
1350 empathy_contact_list_view_get_contact_menu (EmpathyContactListView *view)
1352 EmpathyContactListViewPriv *priv = GET_PRIV (view);
1353 EmpathyContact *contact;
1358 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), NULL);
1360 contact = empathy_contact_list_view_get_selected (view);
1365 menu = empathy_contact_menu_new (contact, priv->contact_features);
1367 if (!(priv->list_features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_REMOVE)) {
1368 g_object_unref (contact);
1374 item = gtk_separator_menu_item_new ();
1375 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1376 gtk_widget_show (item);
1378 menu = gtk_menu_new ();
1381 /* Remove contact */
1382 if (priv->list_features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_REMOVE) {
1383 item = gtk_image_menu_item_new_with_mnemonic (_("_Remove"));
1384 image = gtk_image_new_from_icon_name (GTK_STOCK_REMOVE,
1385 GTK_ICON_SIZE_MENU);
1386 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1387 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1388 gtk_widget_show (item);
1389 g_signal_connect (item, "activate",
1390 G_CALLBACK (contact_list_view_remove_activate_cb),
1394 g_object_unref (contact);