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>
34 #include <telepathy-glib/util.h>
35 #include <libmissioncontrol/mc-account.h>
37 #include <libempathy/empathy-account-manager.h>
38 #include <libempathy/empathy-call-factory.h>
39 #include <libempathy/empathy-tp-contact-factory.h>
40 #include <libempathy/empathy-contact-list.h>
41 #include <libempathy/empathy-contact-groups.h>
42 #include <libempathy/empathy-dispatcher.h>
43 #include <libempathy/empathy-utils.h>
45 #include "empathy-contact-list-view.h"
46 #include "empathy-contact-list-store.h"
47 #include "empathy-images.h"
48 #include "empathy-cell-renderer-expander.h"
49 #include "empathy-cell-renderer-text.h"
50 #include "empathy-cell-renderer-activatable.h"
51 #include "empathy-ui-utils.h"
52 #include "empathy-gtk-enum-types.h"
53 #include "empathy-gtk-marshal.h"
55 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
56 #include <libempathy/empathy-debug.h>
58 /* Active users are those which have recently changed state
59 * (e.g. online, offline or from normal to a busy state).
62 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyContactListView)
64 EmpathyContactListStore *store;
65 GtkTreeRowReference *drag_row;
66 EmpathyContactListFeatureFlags list_features;
67 EmpathyContactFeatureFlags contact_features;
68 GtkWidget *tooltip_widget;
69 } EmpathyContactListViewPriv;
72 EmpathyContactListView *view;
78 EmpathyContactListView *view;
79 EmpathyContact *contact;
87 PROP_CONTACT_FEATURES,
91 DND_DRAG_TYPE_CONTACT_ID,
96 static const GtkTargetEntry drag_types_dest[] = {
97 { "text/contact-id", 0, DND_DRAG_TYPE_CONTACT_ID },
98 { "text/uri-list", 0, DND_DRAG_TYPE_URL },
99 { "text/plain", 0, DND_DRAG_TYPE_STRING },
100 { "STRING", 0, DND_DRAG_TYPE_STRING },
103 static const GtkTargetEntry drag_types_source[] = {
104 { "text/contact-id", 0, DND_DRAG_TYPE_CONTACT_ID },
107 static GdkAtom drag_atoms_dest[G_N_ELEMENTS (drag_types_dest)];
108 static GdkAtom drag_atoms_source[G_N_ELEMENTS (drag_types_source)];
111 DRAG_CONTACT_RECEIVED,
115 static guint signals[LAST_SIGNAL];
117 G_DEFINE_TYPE (EmpathyContactListView, empathy_contact_list_view, GTK_TYPE_TREE_VIEW);
120 contact_list_view_tooltip_destroy_cb (GtkWidget *widget,
121 EmpathyContactListView *view)
123 EmpathyContactListViewPriv *priv = GET_PRIV (view);
125 if (priv->tooltip_widget) {
126 DEBUG ("Tooltip destroyed");
127 g_object_unref (priv->tooltip_widget);
128 priv->tooltip_widget = NULL;
133 contact_list_view_query_tooltip_cb (EmpathyContactListView *view,
136 gboolean keyboard_mode,
140 EmpathyContactListViewPriv *priv = GET_PRIV (view);
141 EmpathyContact *contact;
145 static gint running = 0;
146 gboolean ret = FALSE;
148 /* Avoid an infinite loop. See GNOME bug #574377 */
154 if (!gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (view), &x, &y,
156 &model, &path, &iter)) {
160 gtk_tree_view_set_tooltip_row (GTK_TREE_VIEW (view), tooltip, path);
161 gtk_tree_path_free (path);
163 gtk_tree_model_get (model, &iter,
164 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
170 if (!priv->tooltip_widget) {
171 priv->tooltip_widget = empathy_contact_widget_new (contact,
172 EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP);
173 g_object_ref (priv->tooltip_widget);
174 g_signal_connect (priv->tooltip_widget, "destroy",
175 G_CALLBACK (contact_list_view_tooltip_destroy_cb),
177 gtk_widget_show (priv->tooltip_widget);
179 empathy_contact_widget_set_contact (priv->tooltip_widget,
183 gtk_tooltip_set_custom (tooltip, priv->tooltip_widget);
186 g_object_unref (contact);
196 GdkDragAction action;
200 contact_list_view_dnd_get_contact_free (DndGetContactData *data)
202 g_free (data->new_group);
203 g_free (data->old_group);
204 g_slice_free (DndGetContactData, data);
208 contact_list_view_drag_got_contact (EmpathyTpContactFactory *factory,
209 EmpathyContact *contact,
214 EmpathyContactListViewPriv *priv = GET_PRIV (view);
215 DndGetContactData *data = user_data;
216 EmpathyContactList *list;
219 DEBUG ("Error: %s", error->message);
223 DEBUG ("contact %s (%d) dragged from '%s' to '%s'",
224 empathy_contact_get_id (contact),
225 empathy_contact_get_handle (contact),
226 data->old_group, data->new_group);
228 list = empathy_contact_list_store_get_list_iface (priv->store);
229 if (data->new_group) {
230 empathy_contact_list_add_to_group (list, contact, data->new_group);
232 if (data->old_group && data->action == GDK_ACTION_MOVE) {
233 empathy_contact_list_remove_from_group (list, contact, data->old_group);
238 contact_list_view_drag_data_received (GtkWidget *view,
239 GdkDragContext *context,
242 GtkSelectionData *selection,
246 EmpathyContactListViewPriv *priv;
247 EmpathyAccountManager *account_manager;
248 EmpathyTpContactFactory *factory = NULL;
251 GtkTreeViewDropPosition position;
255 const gchar *account_id;
256 const gchar *contact_id;
257 gchar *new_group = NULL;
258 gchar *old_group = NULL;
259 DndGetContactData *data;
261 gboolean success = TRUE;
263 priv = GET_PRIV (view);
264 model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
266 /* Get destination group information. */
267 is_row = gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (view),
274 new_group = empathy_contact_list_store_get_parent_group (model,
276 gtk_tree_path_free (path);
279 /* Get source group information. */
280 if (priv->drag_row) {
281 path = gtk_tree_row_reference_get_path (priv->drag_row);
283 old_group = empathy_contact_list_store_get_parent_group (
285 gtk_tree_path_free (path);
289 if (!tp_strdiff (old_group, new_group)) {
295 id = (const gchar*) selection->data;
296 DEBUG ("Received %s%s drag & drop contact from roster with id:'%s'",
297 context->action == GDK_ACTION_MOVE ? "move" : "",
298 context->action == GDK_ACTION_COPY ? "copy" : "",
301 strv = g_strsplit (id, "/", 2);
302 account_id = strv[0];
303 contact_id = strv[1];
304 account = mc_account_lookup (account_id);
306 TpConnection *connection;
308 /* FIXME: We assume we have already an account manager */
309 account_manager = empathy_account_manager_dup_singleton ();
310 connection = empathy_account_manager_get_connection (account_manager,
313 factory = empathy_tp_contact_factory_dup_singleton (connection);
315 g_object_unref (account_manager);
319 DEBUG ("Failed to get factory for account '%s'", account_id);
326 data = g_slice_new0 (DndGetContactData);
327 data->new_group = new_group;
328 data->old_group = old_group;
329 data->action = context->action;
331 /* FIXME: We should probably wait for the cb before calling
333 empathy_tp_contact_factory_get_from_id (factory, contact_id,
334 contact_list_view_drag_got_contact,
335 data, (GDestroyNotify) contact_list_view_dnd_get_contact_free,
338 g_object_unref (factory);
342 gtk_drag_finish (context, success, FALSE, GDK_CURRENT_TIME);
346 contact_list_view_drag_motion_cb (DragMotionData *data)
348 gtk_tree_view_expand_row (GTK_TREE_VIEW (data->view),
352 data->timeout_id = 0;
358 contact_list_view_drag_motion (GtkWidget *widget,
359 GdkDragContext *context,
364 static DragMotionData *dm = NULL;
367 gboolean is_different = FALSE;
368 gboolean cleanup = TRUE;
370 is_row = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
381 cleanup &= (dm && gtk_tree_path_compare (dm->path, path) != 0);
382 is_different = (!dm || (dm && gtk_tree_path_compare (dm->path, path) != 0));
387 if (!is_different && !cleanup) {
392 gtk_tree_path_free (dm->path);
393 if (dm->timeout_id) {
394 g_source_remove (dm->timeout_id);
402 if (!gtk_tree_view_row_expanded (GTK_TREE_VIEW (widget), path)) {
403 dm = g_new0 (DragMotionData, 1);
405 dm->view = EMPATHY_CONTACT_LIST_VIEW (widget);
406 dm->path = gtk_tree_path_copy (path);
408 dm->timeout_id = g_timeout_add_seconds (1,
409 (GSourceFunc) contact_list_view_drag_motion_cb,
417 contact_list_view_drag_begin (GtkWidget *widget,
418 GdkDragContext *context)
420 EmpathyContactListViewPriv *priv;
421 GtkTreeSelection *selection;
426 priv = GET_PRIV (widget);
428 GTK_WIDGET_CLASS (empathy_contact_list_view_parent_class)->drag_begin (widget,
431 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (widget));
432 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
436 path = gtk_tree_model_get_path (model, &iter);
437 priv->drag_row = gtk_tree_row_reference_new (model, path);
438 gtk_tree_path_free (path);
442 contact_list_view_drag_data_get (GtkWidget *widget,
443 GdkDragContext *context,
444 GtkSelectionData *selection,
448 EmpathyContactListViewPriv *priv;
449 GtkTreePath *src_path;
452 EmpathyContact *contact;
454 const gchar *contact_id;
455 const gchar *account_id;
458 priv = GET_PRIV (widget);
460 model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
461 if (!priv->drag_row) {
465 src_path = gtk_tree_row_reference_get_path (priv->drag_row);
470 if (!gtk_tree_model_get_iter (model, &iter, src_path)) {
471 gtk_tree_path_free (src_path);
475 gtk_tree_path_free (src_path);
477 contact = empathy_contact_list_view_dup_selected (EMPATHY_CONTACT_LIST_VIEW (widget));
482 account = empathy_contact_get_account (contact);
483 account_id = mc_account_get_unique_name (account);
484 contact_id = empathy_contact_get_id (contact);
485 g_object_unref (contact);
486 str = g_strconcat (account_id, "/", contact_id, NULL);
489 case DND_DRAG_TYPE_CONTACT_ID:
490 gtk_selection_data_set (selection, drag_atoms_source[info], 8,
491 (guchar*)str, strlen (str) + 1);
499 contact_list_view_drag_end (GtkWidget *widget,
500 GdkDragContext *context)
502 EmpathyContactListViewPriv *priv;
504 priv = GET_PRIV (widget);
506 GTK_WIDGET_CLASS (empathy_contact_list_view_parent_class)->drag_end (widget,
509 if (priv->drag_row) {
510 gtk_tree_row_reference_free (priv->drag_row);
511 priv->drag_row = NULL;
516 contact_list_view_drag_drop (GtkWidget *widget,
517 GdkDragContext *drag_context,
526 EmpathyContactListView *view;
532 contact_list_view_popup_menu_idle_cb (gpointer user_data)
534 MenuPopupData *data = user_data;
537 menu = empathy_contact_list_view_get_contact_menu (data->view);
539 menu = empathy_contact_list_view_get_group_menu (data->view);
543 gtk_widget_show (menu);
544 gtk_menu_popup (GTK_MENU (menu),
545 NULL, NULL, NULL, NULL,
546 data->button, data->time);
549 g_slice_free (MenuPopupData, data);
555 contact_list_view_button_press_event_cb (EmpathyContactListView *view,
556 GdkEventButton *event,
559 if (event->button == 3) {
562 data = g_slice_new (MenuPopupData);
564 data->button = event->button;
565 data->time = event->time;
566 g_idle_add (contact_list_view_popup_menu_idle_cb, data);
573 contact_list_view_key_press_event_cb (EmpathyContactListView *view,
577 if (event->keyval == GDK_Menu) {
580 data = g_slice_new (MenuPopupData);
583 data->time = event->time;
584 g_idle_add (contact_list_view_popup_menu_idle_cb, data);
591 contact_list_view_row_activated (GtkTreeView *view,
593 GtkTreeViewColumn *column)
595 EmpathyContactListViewPriv *priv = GET_PRIV (view);
596 EmpathyContact *contact;
600 if (!(priv->contact_features & EMPATHY_CONTACT_FEATURE_CHAT)) {
604 model = GTK_TREE_MODEL (priv->store);
605 gtk_tree_model_get_iter (model, &iter, path);
606 gtk_tree_model_get (model, &iter,
607 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
611 DEBUG ("Starting a chat");
612 empathy_dispatcher_chat_with_contact (contact, NULL, NULL);
613 g_object_unref (contact);
618 contact_list_view_voip_activated_cb (EmpathyCellRendererActivatable *cell,
619 const gchar *path_string,
620 EmpathyContactListView *view)
622 EmpathyContactListViewPriv *priv = GET_PRIV (view);
625 EmpathyContact *contact;
627 if (!(priv->contact_features & EMPATHY_CONTACT_FEATURE_CALL)) {
631 model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
632 if (!gtk_tree_model_get_iter_from_string (model, &iter, path_string)) {
636 gtk_tree_model_get (model, &iter,
637 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
641 EmpathyCallFactory *factory;
643 factory = empathy_call_factory_get ();
644 empathy_call_factory_new_call (factory, contact);
646 g_object_unref (contact);
651 contact_list_view_cell_set_background (EmpathyContactListView *view,
652 GtkCellRenderer *cell,
659 style = gtk_widget_get_style (GTK_WIDGET (view));
661 if (!is_group && is_active) {
662 color = style->bg[GTK_STATE_SELECTED];
664 /* Here we take the current theme colour and add it to
665 * the colour for white and average the two. This
666 * gives a colour which is inline with the theme but
669 color.red = (color.red + (style->white).red) / 2;
670 color.green = (color.green + (style->white).green) / 2;
671 color.blue = (color.blue + (style->white).blue) / 2;
674 "cell-background-gdk", &color,
678 "cell-background-gdk", NULL,
684 contact_list_view_pixbuf_cell_data_func (GtkTreeViewColumn *tree_column,
685 GtkCellRenderer *cell,
688 EmpathyContactListView *view)
694 gtk_tree_model_get (model, iter,
695 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
696 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, &is_active,
697 EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, &icon_name,
701 "visible", !is_group,
702 "icon-name", icon_name,
707 contact_list_view_cell_set_background (view, cell, is_group, is_active);
711 contact_list_view_voip_cell_data_func (GtkTreeViewColumn *tree_column,
712 GtkCellRenderer *cell,
715 EmpathyContactListView *view)
721 gtk_tree_model_get (model, iter,
722 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
723 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, &is_active,
724 EMPATHY_CONTACT_LIST_STORE_COL_CAN_VOIP, &can_voip,
728 "visible", !is_group && can_voip,
729 "icon-name", EMPATHY_IMAGE_VOIP,
732 contact_list_view_cell_set_background (view, cell, is_group, is_active);
736 contact_list_view_avatar_cell_data_func (GtkTreeViewColumn *tree_column,
737 GtkCellRenderer *cell,
740 EmpathyContactListView *view)
743 gboolean show_avatar;
747 gtk_tree_model_get (model, iter,
748 EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR, &pixbuf,
749 EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR_VISIBLE, &show_avatar,
750 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
751 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, &is_active,
755 "visible", !is_group && show_avatar,
760 g_object_unref (pixbuf);
763 contact_list_view_cell_set_background (view, cell, is_group, is_active);
767 contact_list_view_text_cell_data_func (GtkTreeViewColumn *tree_column,
768 GtkCellRenderer *cell,
771 EmpathyContactListView *view)
775 gboolean show_status;
777 gtk_tree_model_get (model, iter,
778 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
779 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, &is_active,
780 EMPATHY_CONTACT_LIST_STORE_COL_STATUS_VISIBLE, &show_status,
784 "show-status", show_status,
787 contact_list_view_cell_set_background (view, cell, is_group, is_active);
791 contact_list_view_expander_cell_data_func (GtkTreeViewColumn *column,
792 GtkCellRenderer *cell,
795 EmpathyContactListView *view)
800 gtk_tree_model_get (model, iter,
801 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
802 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, &is_active,
805 if (gtk_tree_model_iter_has_child (model, iter)) {
807 gboolean row_expanded;
809 path = gtk_tree_model_get_path (model, iter);
810 row_expanded = gtk_tree_view_row_expanded (GTK_TREE_VIEW (column->tree_view), path);
811 gtk_tree_path_free (path);
815 "expander-style", row_expanded ? GTK_EXPANDER_EXPANDED : GTK_EXPANDER_COLLAPSED,
818 g_object_set (cell, "visible", FALSE, NULL);
821 contact_list_view_cell_set_background (view, cell, is_group, is_active);
825 contact_list_view_row_expand_or_collapse_cb (EmpathyContactListView *view,
830 EmpathyContactListViewPriv *priv = GET_PRIV (view);
835 if (!(priv->list_features & EMPATHY_CONTACT_LIST_FEATURE_GROUPS_SAVE)) {
839 model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
841 gtk_tree_model_get (model, iter,
842 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
845 expanded = GPOINTER_TO_INT (user_data);
846 empathy_contact_group_set_expanded (name, expanded);
852 contact_list_view_row_has_child_toggled_cb (GtkTreeModel *model,
855 EmpathyContactListView *view)
857 EmpathyContactListViewPriv *priv = GET_PRIV (view);
858 gboolean is_group = FALSE;
861 gtk_tree_model_get (model, iter,
862 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
863 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
866 if (!is_group || EMP_STR_EMPTY (name)) {
871 if (!(priv->list_features & EMPATHY_CONTACT_LIST_FEATURE_GROUPS_SAVE) ||
872 empathy_contact_group_get_expanded (name)) {
873 g_signal_handlers_block_by_func (view,
874 contact_list_view_row_expand_or_collapse_cb,
875 GINT_TO_POINTER (TRUE));
876 gtk_tree_view_expand_row (GTK_TREE_VIEW (view), path, TRUE);
877 g_signal_handlers_unblock_by_func (view,
878 contact_list_view_row_expand_or_collapse_cb,
879 GINT_TO_POINTER (TRUE));
881 g_signal_handlers_block_by_func (view,
882 contact_list_view_row_expand_or_collapse_cb,
883 GINT_TO_POINTER (FALSE));
884 gtk_tree_view_collapse_row (GTK_TREE_VIEW (view), path);
885 g_signal_handlers_unblock_by_func (view,
886 contact_list_view_row_expand_or_collapse_cb,
887 GINT_TO_POINTER (FALSE));
894 contact_list_view_setup (EmpathyContactListView *view)
896 EmpathyContactListViewPriv *priv;
897 GtkCellRenderer *cell;
898 GtkTreeViewColumn *col;
901 priv = GET_PRIV (view);
903 gtk_tree_view_set_search_equal_func (GTK_TREE_VIEW (view),
904 empathy_contact_list_store_search_equal_func,
907 g_signal_connect (priv->store, "row-has-child-toggled",
908 G_CALLBACK (contact_list_view_row_has_child_toggled_cb),
910 gtk_tree_view_set_model (GTK_TREE_VIEW (view),
911 GTK_TREE_MODEL (priv->store));
915 "headers-visible", FALSE,
917 "show-expanders", FALSE,
920 col = gtk_tree_view_column_new ();
923 cell = gtk_cell_renderer_pixbuf_new ();
924 gtk_tree_view_column_pack_start (col, cell, FALSE);
925 gtk_tree_view_column_set_cell_data_func (
927 (GtkTreeCellDataFunc) contact_list_view_pixbuf_cell_data_func,
937 cell = empathy_cell_renderer_text_new ();
938 gtk_tree_view_column_pack_start (col, cell, TRUE);
939 gtk_tree_view_column_set_cell_data_func (
941 (GtkTreeCellDataFunc) contact_list_view_text_cell_data_func,
944 gtk_tree_view_column_add_attribute (col, cell,
945 "name", EMPATHY_CONTACT_LIST_STORE_COL_NAME);
946 gtk_tree_view_column_add_attribute (col, cell,
947 "status", EMPATHY_CONTACT_LIST_STORE_COL_STATUS);
948 gtk_tree_view_column_add_attribute (col, cell,
949 "is_group", EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP);
951 /* Voip Capability Icon */
952 cell = empathy_cell_renderer_activatable_new ();
953 gtk_tree_view_column_pack_start (col, cell, FALSE);
954 gtk_tree_view_column_set_cell_data_func (
956 (GtkTreeCellDataFunc) contact_list_view_voip_cell_data_func,
963 g_signal_connect (cell, "path-activated",
964 G_CALLBACK (contact_list_view_voip_activated_cb),
968 cell = gtk_cell_renderer_pixbuf_new ();
969 gtk_tree_view_column_pack_start (col, cell, FALSE);
970 gtk_tree_view_column_set_cell_data_func (
972 (GtkTreeCellDataFunc) contact_list_view_avatar_cell_data_func,
984 cell = empathy_cell_renderer_expander_new ();
985 gtk_tree_view_column_pack_end (col, cell, FALSE);
986 gtk_tree_view_column_set_cell_data_func (
988 (GtkTreeCellDataFunc) contact_list_view_expander_cell_data_func,
991 /* Actually add the column now we have added all cell renderers */
992 gtk_tree_view_append_column (GTK_TREE_VIEW (view), col);
995 for (i = 0; i < G_N_ELEMENTS (drag_types_dest); ++i) {
996 drag_atoms_dest[i] = gdk_atom_intern (drag_types_dest[i].target,
1000 for (i = 0; i < G_N_ELEMENTS (drag_types_source); ++i) {
1001 drag_atoms_source[i] = gdk_atom_intern (drag_types_source[i].target,
1007 contact_list_view_set_list_features (EmpathyContactListView *view,
1008 EmpathyContactListFeatureFlags features)
1010 EmpathyContactListViewPriv *priv = GET_PRIV (view);
1011 gboolean has_tooltip;
1013 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view));
1015 priv->list_features = features;
1017 /* Update DnD source/dest */
1018 if (features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_DRAG) {
1019 gtk_drag_source_set (GTK_WIDGET (view),
1022 G_N_ELEMENTS (drag_types_source),
1023 GDK_ACTION_MOVE | GDK_ACTION_COPY);
1025 gtk_drag_source_unset (GTK_WIDGET (view));
1029 if (features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_DROP) {
1030 gtk_drag_dest_set (GTK_WIDGET (view),
1031 GTK_DEST_DEFAULT_ALL,
1033 G_N_ELEMENTS (drag_types_dest),
1034 GDK_ACTION_MOVE | GDK_ACTION_COPY);
1036 /* FIXME: URI could still be droped depending on FT feature */
1037 gtk_drag_dest_unset (GTK_WIDGET (view));
1040 /* Update has-tooltip */
1041 has_tooltip = (features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_TOOLTIP) != 0;
1042 gtk_widget_set_has_tooltip (GTK_WIDGET (view), has_tooltip);
1046 contact_list_view_finalize (GObject *object)
1048 EmpathyContactListViewPriv *priv;
1050 priv = GET_PRIV (object);
1053 g_object_unref (priv->store);
1055 if (priv->tooltip_widget) {
1056 gtk_widget_destroy (priv->tooltip_widget);
1059 G_OBJECT_CLASS (empathy_contact_list_view_parent_class)->finalize (object);
1063 contact_list_view_get_property (GObject *object,
1068 EmpathyContactListViewPriv *priv;
1070 priv = GET_PRIV (object);
1074 g_value_set_object (value, priv->store);
1076 case PROP_LIST_FEATURES:
1077 g_value_set_flags (value, priv->list_features);
1079 case PROP_CONTACT_FEATURES:
1080 g_value_set_flags (value, priv->contact_features);
1083 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1089 contact_list_view_set_property (GObject *object,
1091 const GValue *value,
1094 EmpathyContactListView *view = EMPATHY_CONTACT_LIST_VIEW (object);
1095 EmpathyContactListViewPriv *priv = GET_PRIV (object);
1099 priv->store = g_value_dup_object (value);
1100 contact_list_view_setup (view);
1102 case PROP_LIST_FEATURES:
1103 contact_list_view_set_list_features (view, g_value_get_flags (value));
1105 case PROP_CONTACT_FEATURES:
1106 priv->contact_features = g_value_get_flags (value);
1109 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1115 empathy_contact_list_view_class_init (EmpathyContactListViewClass *klass)
1117 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1118 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1119 GtkTreeViewClass *tree_view_class = GTK_TREE_VIEW_CLASS (klass);
1121 object_class->finalize = contact_list_view_finalize;
1122 object_class->get_property = contact_list_view_get_property;
1123 object_class->set_property = contact_list_view_set_property;
1125 widget_class->drag_data_received = contact_list_view_drag_data_received;
1126 widget_class->drag_drop = contact_list_view_drag_drop;
1127 widget_class->drag_begin = contact_list_view_drag_begin;
1128 widget_class->drag_data_get = contact_list_view_drag_data_get;
1129 widget_class->drag_end = contact_list_view_drag_end;
1130 widget_class->drag_motion = contact_list_view_drag_motion;
1132 /* We use the class method to let user of this widget to connect to
1133 * the signal and stop emission of the signal so the default handler
1134 * won't be called. */
1135 tree_view_class->row_activated = contact_list_view_row_activated;
1137 signals[DRAG_CONTACT_RECEIVED] =
1138 g_signal_new ("drag-contact-received",
1139 G_OBJECT_CLASS_TYPE (klass),
1143 _empathy_gtk_marshal_VOID__OBJECT_STRING_STRING,
1145 3, EMPATHY_TYPE_CONTACT, G_TYPE_STRING, G_TYPE_STRING);
1147 g_object_class_install_property (object_class,
1149 g_param_spec_object ("store",
1150 "The store of the view",
1151 "The store of the view",
1152 EMPATHY_TYPE_CONTACT_LIST_STORE,
1153 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
1154 g_object_class_install_property (object_class,
1156 g_param_spec_flags ("list-features",
1157 "Features of the view",
1158 "Falgs for all enabled features",
1159 EMPATHY_TYPE_CONTACT_LIST_FEATURE_FLAGS,
1160 EMPATHY_CONTACT_LIST_FEATURE_NONE,
1161 G_PARAM_READWRITE));
1162 g_object_class_install_property (object_class,
1163 PROP_CONTACT_FEATURES,
1164 g_param_spec_flags ("contact-features",
1165 "Features of the contact menu",
1166 "Falgs for all enabled features for the menu",
1167 EMPATHY_TYPE_CONTACT_FEATURE_FLAGS,
1168 EMPATHY_CONTACT_FEATURE_NONE,
1169 G_PARAM_READWRITE));
1171 g_type_class_add_private (object_class, sizeof (EmpathyContactListViewPriv));
1175 empathy_contact_list_view_init (EmpathyContactListView *view)
1177 EmpathyContactListViewPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (view,
1178 EMPATHY_TYPE_CONTACT_LIST_VIEW, EmpathyContactListViewPriv);
1181 /* Get saved group states. */
1182 empathy_contact_groups_get_all ();
1184 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (view),
1185 empathy_contact_list_store_row_separator_func,
1188 /* Connect to tree view signals rather than override. */
1189 g_signal_connect (view, "button-press-event",
1190 G_CALLBACK (contact_list_view_button_press_event_cb),
1192 g_signal_connect (view, "key-press-event",
1193 G_CALLBACK (contact_list_view_key_press_event_cb),
1195 g_signal_connect (view, "row-expanded",
1196 G_CALLBACK (contact_list_view_row_expand_or_collapse_cb),
1197 GINT_TO_POINTER (TRUE));
1198 g_signal_connect (view, "row-collapsed",
1199 G_CALLBACK (contact_list_view_row_expand_or_collapse_cb),
1200 GINT_TO_POINTER (FALSE));
1201 g_signal_connect (view, "query-tooltip",
1202 G_CALLBACK (contact_list_view_query_tooltip_cb),
1206 EmpathyContactListView *
1207 empathy_contact_list_view_new (EmpathyContactListStore *store,
1208 EmpathyContactListFeatureFlags list_features,
1209 EmpathyContactFeatureFlags contact_features)
1211 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), NULL);
1213 return g_object_new (EMPATHY_TYPE_CONTACT_LIST_VIEW,
1215 "contact-features", contact_features,
1216 "list-features", list_features,
1221 empathy_contact_list_view_dup_selected (EmpathyContactListView *view)
1223 EmpathyContactListViewPriv *priv;
1224 GtkTreeSelection *selection;
1226 GtkTreeModel *model;
1227 EmpathyContact *contact;
1229 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), NULL);
1231 priv = GET_PRIV (view);
1233 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
1234 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
1238 gtk_tree_model_get (model, &iter,
1239 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
1246 empathy_contact_list_view_get_selected_group (EmpathyContactListView *view)
1248 EmpathyContactListViewPriv *priv;
1249 GtkTreeSelection *selection;
1251 GtkTreeModel *model;
1255 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), NULL);
1257 priv = GET_PRIV (view);
1259 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
1260 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
1264 gtk_tree_model_get (model, &iter,
1265 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
1266 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
1278 contact_list_view_remove_dialog_show (GtkWindow *parent,
1279 const gchar *message,
1280 const gchar *secondary_text)
1285 dialog = gtk_message_dialog_new (parent, GTK_DIALOG_MODAL,
1286 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
1288 gtk_dialog_add_buttons (GTK_DIALOG (dialog),
1289 GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
1290 GTK_STOCK_DELETE, GTK_RESPONSE_YES,
1292 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
1293 "%s", secondary_text);
1295 gtk_widget_show (dialog);
1297 res = gtk_dialog_run (GTK_DIALOG (dialog));
1298 gtk_widget_destroy (dialog);
1300 return (res == GTK_RESPONSE_YES);
1304 contact_list_view_group_remove_activate_cb (GtkMenuItem *menuitem,
1305 EmpathyContactListView *view)
1307 EmpathyContactListViewPriv *priv = GET_PRIV (view);
1310 group = empathy_contact_list_view_get_selected_group (view);
1315 text = g_strdup_printf (_("Do you really want to remove the group '%s'?"), group);
1316 parent = empathy_get_toplevel_window (GTK_WIDGET (view));
1317 if (contact_list_view_remove_dialog_show (parent, _("Removing group"), text)) {
1318 EmpathyContactList *list;
1320 list = empathy_contact_list_store_get_list_iface (priv->store);
1321 empathy_contact_list_remove_group (list, group);
1331 empathy_contact_list_view_get_group_menu (EmpathyContactListView *view)
1333 EmpathyContactListViewPriv *priv = GET_PRIV (view);
1339 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), NULL);
1341 if (!(priv->list_features & (EMPATHY_CONTACT_LIST_FEATURE_GROUPS_RENAME |
1342 EMPATHY_CONTACT_LIST_FEATURE_GROUPS_REMOVE))) {
1346 group = empathy_contact_list_view_get_selected_group (view);
1351 menu = gtk_menu_new ();
1353 /* FIXME: Not implemented yet
1354 if (priv->features & EMPATHY_CONTACT_LIST_FEATURE_GROUPS_RENAME) {
1355 item = gtk_menu_item_new_with_mnemonic (_("Re_name"));
1356 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1357 gtk_widget_show (item);
1358 g_signal_connect (item, "activate",
1359 G_CALLBACK (contact_list_view_group_rename_activate_cb),
1363 if (priv->list_features & EMPATHY_CONTACT_LIST_FEATURE_GROUPS_REMOVE) {
1364 item = gtk_image_menu_item_new_with_mnemonic (_("_Remove"));
1365 image = gtk_image_new_from_icon_name (GTK_STOCK_REMOVE,
1366 GTK_ICON_SIZE_MENU);
1367 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1368 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1369 gtk_widget_show (item);
1370 g_signal_connect (item, "activate",
1371 G_CALLBACK (contact_list_view_group_remove_activate_cb),
1381 contact_list_view_remove_activate_cb (GtkMenuItem *menuitem,
1382 EmpathyContactListView *view)
1384 EmpathyContactListViewPriv *priv = GET_PRIV (view);
1385 EmpathyContact *contact;
1387 contact = empathy_contact_list_view_dup_selected (view);
1393 parent = empathy_get_toplevel_window (GTK_WIDGET (view));
1394 text = g_strdup_printf (_("Do you really want to remove the contact '%s'?"),
1395 empathy_contact_get_name (contact));
1396 if (contact_list_view_remove_dialog_show (parent, _("Removing contact"), text)) {
1397 EmpathyContactList *list;
1399 list = empathy_contact_list_store_get_list_iface (priv->store);
1400 empathy_contact_list_remove (list, contact,
1401 _("Sorry, I don't want you in my contact list anymore."));
1405 g_object_unref (contact);
1410 empathy_contact_list_view_get_contact_menu (EmpathyContactListView *view)
1412 EmpathyContactListViewPriv *priv = GET_PRIV (view);
1413 EmpathyContact *contact;
1418 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), NULL);
1420 contact = empathy_contact_list_view_dup_selected (view);
1425 menu = empathy_contact_menu_new (contact, priv->contact_features);
1427 if (!(priv->list_features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_REMOVE)) {
1428 g_object_unref (contact);
1434 item = gtk_separator_menu_item_new ();
1435 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1436 gtk_widget_show (item);
1438 menu = gtk_menu_new ();
1441 /* Remove contact */
1442 if (priv->list_features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_REMOVE) {
1443 item = gtk_image_menu_item_new_with_mnemonic (_("_Remove"));
1444 image = gtk_image_new_from_icon_name (GTK_STOCK_REMOVE,
1445 GTK_ICON_SIZE_MENU);
1446 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1447 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1448 gtk_widget_show (item);
1449 g_signal_connect (item, "activate",
1450 G_CALLBACK (contact_list_view_remove_activate_cb),
1454 g_object_unref (contact);