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.h>
32 #include <glade/glade.h>
34 #include <libmissioncontrol/mc-account.h>
36 #include <libempathy/empathy-contact-factory.h>
37 #include <libempathy/empathy-contact-list.h>
38 #include <libempathy/empathy-contact-groups.h>
39 #include <libempathy/empathy-utils.h>
41 #include "empathy-contact-list-view.h"
42 #include "empathy-contact-list-store.h"
43 #include "empathy-images.h"
44 #include "empathy-cell-renderer-expander.h"
45 #include "empathy-cell-renderer-text.h"
46 #include "empathy-cell-renderer-activatable.h"
47 #include "empathy-ui-utils.h"
48 #include "empathy-gtk-enum-types.h"
49 #include "empathy-gtk-marshal.h"
51 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
52 #include <libempathy/empathy-debug.h>
54 /* Flashing delay for icons (milliseconds). */
55 #define FLASH_TIMEOUT 500
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 } EmpathyContactListViewPriv;
70 EmpathyContactListView *view;
76 EmpathyContactListView *view;
77 EmpathyContact *contact;
85 PROP_CONTACT_FEATURES,
89 DND_DRAG_TYPE_CONTACT_ID,
94 static const GtkTargetEntry drag_types_dest[] = {
95 { "text/contact-id", 0, DND_DRAG_TYPE_CONTACT_ID },
96 { "text/uri-list", 0, DND_DRAG_TYPE_URL },
97 { "text/plain", 0, DND_DRAG_TYPE_STRING },
98 { "STRING", 0, DND_DRAG_TYPE_STRING },
101 static const GtkTargetEntry drag_types_source[] = {
102 { "text/contact-id", 0, DND_DRAG_TYPE_CONTACT_ID },
105 static GdkAtom drag_atoms_dest[G_N_ELEMENTS (drag_types_dest)];
106 static GdkAtom drag_atoms_source[G_N_ELEMENTS (drag_types_source)];
109 DRAG_CONTACT_RECEIVED,
113 static guint signals[LAST_SIGNAL];
115 G_DEFINE_TYPE (EmpathyContactListView, empathy_contact_list_view, GTK_TYPE_TREE_VIEW);
118 contact_list_view_drag_data_received (GtkWidget *widget,
119 GdkDragContext *context,
122 GtkSelectionData *selection,
126 EmpathyContactListViewPriv *priv;
127 EmpathyContactList *list;
128 EmpathyContactFactory *factory;
132 GtkTreeViewDropPosition position;
133 EmpathyContact *contact = NULL;
136 gchar *new_group = NULL;
137 gchar *old_group = NULL;
140 priv = GET_PRIV (widget);
142 id = (const gchar*) selection->data;
143 DEBUG ("Received %s%s drag & drop contact from roster with id:'%s'",
144 context->action == GDK_ACTION_MOVE ? "move" : "",
145 context->action == GDK_ACTION_COPY ? "copy" : "",
148 strv = g_strsplit (id, "/", 2);
149 factory = empathy_contact_factory_new ();
150 account = mc_account_lookup (strv[0]);
152 contact = empathy_contact_factory_get_from_id (factory,
155 g_object_unref (account);
157 g_object_unref (factory);
161 DEBUG ("No contact found associated with drag & drop");
165 empathy_contact_run_until_ready (contact,
166 EMPATHY_CONTACT_READY_HANDLE,
169 model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
171 /* Get source group information. */
172 if (priv->drag_row) {
173 path = gtk_tree_row_reference_get_path (priv->drag_row);
175 old_group = empathy_contact_list_store_get_parent_group (model, path, NULL);
176 gtk_tree_path_free (path);
180 /* Get destination group information. */
181 is_row = gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget),
188 new_group = empathy_contact_list_store_get_parent_group (model, path, NULL);
189 gtk_tree_path_free (path);
192 DEBUG ("contact %s (%d) dragged from '%s' to '%s'",
193 empathy_contact_get_id (contact),
194 empathy_contact_get_handle (contact),
195 old_group, new_group);
197 list = empathy_contact_list_store_get_list_iface (priv->store);
199 empathy_contact_list_add_to_group (list, contact, new_group);
201 if (old_group && context->action == GDK_ACTION_MOVE) {
202 empathy_contact_list_remove_from_group (list, contact, old_group);
208 gtk_drag_finish (context, TRUE, FALSE, GDK_CURRENT_TIME);
212 contact_list_view_drag_motion_cb (DragMotionData *data)
214 gtk_tree_view_expand_row (GTK_TREE_VIEW (data->view),
218 data->timeout_id = 0;
224 contact_list_view_drag_motion (GtkWidget *widget,
225 GdkDragContext *context,
230 static DragMotionData *dm = NULL;
233 gboolean is_different = FALSE;
234 gboolean cleanup = TRUE;
236 is_row = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
247 cleanup &= (dm && gtk_tree_path_compare (dm->path, path) != 0);
248 is_different = (!dm || (dm && gtk_tree_path_compare (dm->path, path) != 0));
253 if (!is_different && !cleanup) {
258 gtk_tree_path_free (dm->path);
259 if (dm->timeout_id) {
260 g_source_remove (dm->timeout_id);
268 if (!gtk_tree_view_row_expanded (GTK_TREE_VIEW (widget), path)) {
269 dm = g_new0 (DragMotionData, 1);
271 dm->view = EMPATHY_CONTACT_LIST_VIEW (widget);
272 dm->path = gtk_tree_path_copy (path);
274 dm->timeout_id = g_timeout_add_seconds (1,
275 (GSourceFunc) contact_list_view_drag_motion_cb,
283 contact_list_view_drag_begin (GtkWidget *widget,
284 GdkDragContext *context)
286 EmpathyContactListViewPriv *priv;
287 GtkTreeSelection *selection;
292 priv = GET_PRIV (widget);
294 GTK_WIDGET_CLASS (empathy_contact_list_view_parent_class)->drag_begin (widget,
297 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (widget));
298 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
302 path = gtk_tree_model_get_path (model, &iter);
303 priv->drag_row = gtk_tree_row_reference_new (model, path);
304 gtk_tree_path_free (path);
308 contact_list_view_drag_data_get (GtkWidget *widget,
309 GdkDragContext *context,
310 GtkSelectionData *selection,
314 EmpathyContactListViewPriv *priv;
315 GtkTreePath *src_path;
318 EmpathyContact *contact;
320 const gchar *contact_id;
321 const gchar *account_id;
324 priv = GET_PRIV (widget);
326 model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
327 if (!priv->drag_row) {
331 src_path = gtk_tree_row_reference_get_path (priv->drag_row);
336 if (!gtk_tree_model_get_iter (model, &iter, src_path)) {
337 gtk_tree_path_free (src_path);
341 gtk_tree_path_free (src_path);
343 contact = empathy_contact_list_view_get_selected (EMPATHY_CONTACT_LIST_VIEW (widget));
348 account = empathy_contact_get_account (contact);
349 account_id = mc_account_get_unique_name (account);
350 contact_id = empathy_contact_get_id (contact);
351 g_object_unref (contact);
352 str = g_strconcat (account_id, "/", contact_id, NULL);
355 case DND_DRAG_TYPE_CONTACT_ID:
356 gtk_selection_data_set (selection, drag_atoms_source[info], 8,
357 (guchar*)str, strlen (str) + 1);
365 contact_list_view_drag_end (GtkWidget *widget,
366 GdkDragContext *context)
368 EmpathyContactListViewPriv *priv;
370 priv = GET_PRIV (widget);
372 GTK_WIDGET_CLASS (empathy_contact_list_view_parent_class)->drag_end (widget,
375 if (priv->drag_row) {
376 gtk_tree_row_reference_free (priv->drag_row);
377 priv->drag_row = NULL;
382 contact_list_view_drag_drop (GtkWidget *widget,
383 GdkDragContext *drag_context,
392 EmpathyContactListView *view;
398 contact_list_view_popup_menu_idle_cb (gpointer user_data)
400 MenuPopupData *data = user_data;
403 menu = empathy_contact_list_view_get_contact_menu (data->view);
405 menu = empathy_contact_list_view_get_group_menu (data->view);
412 gtk_widget_show (menu);
414 gtk_menu_popup (GTK_MENU (menu),
415 NULL, NULL, NULL, NULL,
416 data->button, data->time);
419 g_slice_free (MenuPopupData, data);
425 contact_list_view_button_press_event_cb (EmpathyContactListView *view,
426 GdkEventButton *event,
429 if (event->button == 3) {
432 data = g_slice_new (MenuPopupData);
434 data->button = event->button;
435 data->time = event->time;
436 g_idle_add (contact_list_view_popup_menu_idle_cb, data);
443 contact_list_view_row_activated_cb (EmpathyContactListView *view,
445 GtkTreeViewColumn *col,
448 EmpathyContactListViewPriv *priv = GET_PRIV (view);
449 EmpathyContact *contact;
453 if (!(priv->contact_features & EMPATHY_CONTACT_FEATURE_CHAT)) {
457 model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
459 gtk_tree_model_get_iter (model, &iter, path);
460 gtk_tree_model_get (model, &iter,
461 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
465 empathy_chat_with_contact (contact);
466 g_object_unref (contact);
471 contact_list_view_voip_activated_cb (EmpathyCellRendererActivatable *cell,
472 const gchar *path_string,
473 EmpathyContactListView *view)
475 EmpathyContactListViewPriv *priv = GET_PRIV (view);
478 EmpathyContact *contact;
480 if (!(priv->contact_features & EMPATHY_CONTACT_FEATURE_CALL)) {
484 model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
485 if (!gtk_tree_model_get_iter_from_string (model, &iter, path_string)) {
489 gtk_tree_model_get (model, &iter,
490 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
494 empathy_call_with_contact (contact);
495 g_object_unref (contact);
500 contact_list_view_cell_set_background (EmpathyContactListView *view,
501 GtkCellRenderer *cell,
508 style = gtk_widget_get_style (GTK_WIDGET (view));
510 if (!is_group && is_active) {
511 color = style->bg[GTK_STATE_SELECTED];
513 /* Here we take the current theme colour and add it to
514 * the colour for white and average the two. This
515 * gives a colour which is inline with the theme but
518 color.red = (color.red + (style->white).red) / 2;
519 color.green = (color.green + (style->white).green) / 2;
520 color.blue = (color.blue + (style->white).blue) / 2;
523 "cell-background-gdk", &color,
527 "cell-background-gdk", NULL,
533 contact_list_view_pixbuf_cell_data_func (GtkTreeViewColumn *tree_column,
534 GtkCellRenderer *cell,
537 EmpathyContactListView *view)
543 gtk_tree_model_get (model, iter,
544 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
545 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, &is_active,
546 EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, &icon_name,
550 "visible", !is_group,
551 "icon-name", icon_name,
556 contact_list_view_cell_set_background (view, cell, is_group, is_active);
560 contact_list_view_voip_cell_data_func (GtkTreeViewColumn *tree_column,
561 GtkCellRenderer *cell,
564 EmpathyContactListView *view)
570 gtk_tree_model_get (model, iter,
571 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
572 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, &is_active,
573 EMPATHY_CONTACT_LIST_STORE_COL_CAN_VOIP, &can_voip,
577 "visible", !is_group && can_voip,
578 "icon-name", EMPATHY_IMAGE_VOIP,
581 contact_list_view_cell_set_background (view, cell, is_group, is_active);
585 contact_list_view_avatar_cell_data_func (GtkTreeViewColumn *tree_column,
586 GtkCellRenderer *cell,
589 EmpathyContactListView *view)
592 gboolean show_avatar;
596 gtk_tree_model_get (model, iter,
597 EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR, &pixbuf,
598 EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR_VISIBLE, &show_avatar,
599 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
600 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, &is_active,
604 "visible", !is_group && show_avatar,
609 g_object_unref (pixbuf);
612 contact_list_view_cell_set_background (view, cell, is_group, is_active);
616 contact_list_view_text_cell_data_func (GtkTreeViewColumn *tree_column,
617 GtkCellRenderer *cell,
620 EmpathyContactListView *view)
624 gboolean show_status;
626 gtk_tree_model_get (model, iter,
627 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
628 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, &is_active,
629 EMPATHY_CONTACT_LIST_STORE_COL_STATUS_VISIBLE, &show_status,
633 "show-status", show_status,
636 contact_list_view_cell_set_background (view, cell, is_group, is_active);
640 contact_list_view_expander_cell_data_func (GtkTreeViewColumn *column,
641 GtkCellRenderer *cell,
644 EmpathyContactListView *view)
649 gtk_tree_model_get (model, iter,
650 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
651 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, &is_active,
654 if (gtk_tree_model_iter_has_child (model, iter)) {
656 gboolean row_expanded;
658 path = gtk_tree_model_get_path (model, iter);
659 row_expanded = gtk_tree_view_row_expanded (GTK_TREE_VIEW (column->tree_view), path);
660 gtk_tree_path_free (path);
664 "expander-style", row_expanded ? GTK_EXPANDER_EXPANDED : GTK_EXPANDER_COLLAPSED,
667 g_object_set (cell, "visible", FALSE, NULL);
670 contact_list_view_cell_set_background (view, cell, is_group, is_active);
674 contact_list_view_row_expand_or_collapse_cb (EmpathyContactListView *view,
679 EmpathyContactListViewPriv *priv = GET_PRIV (view);
684 if (!(priv->list_features & EMPATHY_CONTACT_LIST_FEATURE_GROUPS_SAVE)) {
688 model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
690 gtk_tree_model_get (model, iter,
691 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
694 expanded = GPOINTER_TO_INT (user_data);
695 empathy_contact_group_set_expanded (name, expanded);
701 contact_list_view_row_has_child_toggled_cb (GtkTreeModel *model,
704 EmpathyContactListView *view)
706 EmpathyContactListViewPriv *priv = GET_PRIV (view);
707 gboolean is_group = FALSE;
710 gtk_tree_model_get (model, iter,
711 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
712 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
715 if (!is_group || G_STR_EMPTY (name)) {
720 if (!(priv->list_features & EMPATHY_CONTACT_LIST_FEATURE_GROUPS_SAVE) ||
721 empathy_contact_group_get_expanded (name)) {
722 g_signal_handlers_block_by_func (view,
723 contact_list_view_row_expand_or_collapse_cb,
724 GINT_TO_POINTER (TRUE));
725 gtk_tree_view_expand_row (GTK_TREE_VIEW (view), path, TRUE);
726 g_signal_handlers_unblock_by_func (view,
727 contact_list_view_row_expand_or_collapse_cb,
728 GINT_TO_POINTER (TRUE));
730 g_signal_handlers_block_by_func (view,
731 contact_list_view_row_expand_or_collapse_cb,
732 GINT_TO_POINTER (FALSE));
733 gtk_tree_view_collapse_row (GTK_TREE_VIEW (view), path);
734 g_signal_handlers_unblock_by_func (view,
735 contact_list_view_row_expand_or_collapse_cb,
736 GINT_TO_POINTER (FALSE));
743 contact_list_view_setup (EmpathyContactListView *view)
745 EmpathyContactListViewPriv *priv;
746 GtkCellRenderer *cell;
747 GtkTreeViewColumn *col;
750 priv = GET_PRIV (view);
752 gtk_tree_view_set_search_equal_func (GTK_TREE_VIEW (view),
753 empathy_contact_list_store_search_equal_func,
756 g_signal_connect (priv->store, "row-has-child-toggled",
757 G_CALLBACK (contact_list_view_row_has_child_toggled_cb),
759 gtk_tree_view_set_model (GTK_TREE_VIEW (view),
760 GTK_TREE_MODEL (priv->store));
764 "headers-visible", FALSE,
766 "show-expanders", FALSE,
769 col = gtk_tree_view_column_new ();
772 cell = gtk_cell_renderer_pixbuf_new ();
773 gtk_tree_view_column_pack_start (col, cell, FALSE);
774 gtk_tree_view_column_set_cell_data_func (
776 (GtkTreeCellDataFunc) contact_list_view_pixbuf_cell_data_func,
786 cell = empathy_cell_renderer_text_new ();
787 gtk_tree_view_column_pack_start (col, cell, TRUE);
788 gtk_tree_view_column_set_cell_data_func (
790 (GtkTreeCellDataFunc) contact_list_view_text_cell_data_func,
793 gtk_tree_view_column_add_attribute (col, cell,
794 "name", EMPATHY_CONTACT_LIST_STORE_COL_NAME);
795 gtk_tree_view_column_add_attribute (col, cell,
796 "status", EMPATHY_CONTACT_LIST_STORE_COL_STATUS);
797 gtk_tree_view_column_add_attribute (col, cell,
798 "is_group", EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP);
800 /* Voip Capability Icon */
801 cell = empathy_cell_renderer_activatable_new ();
802 gtk_tree_view_column_pack_start (col, cell, FALSE);
803 gtk_tree_view_column_set_cell_data_func (
805 (GtkTreeCellDataFunc) contact_list_view_voip_cell_data_func,
812 g_signal_connect (cell, "path-activated",
813 G_CALLBACK (contact_list_view_voip_activated_cb),
817 cell = gtk_cell_renderer_pixbuf_new ();
818 gtk_tree_view_column_pack_start (col, cell, FALSE);
819 gtk_tree_view_column_set_cell_data_func (
821 (GtkTreeCellDataFunc) contact_list_view_avatar_cell_data_func,
833 cell = empathy_cell_renderer_expander_new ();
834 gtk_tree_view_column_pack_end (col, cell, FALSE);
835 gtk_tree_view_column_set_cell_data_func (
837 (GtkTreeCellDataFunc) contact_list_view_expander_cell_data_func,
840 /* Actually add the column now we have added all cell renderers */
841 gtk_tree_view_append_column (GTK_TREE_VIEW (view), col);
844 for (i = 0; i < G_N_ELEMENTS (drag_types_dest); ++i) {
845 drag_atoms_dest[i] = gdk_atom_intern (drag_types_dest[i].target,
849 for (i = 0; i < G_N_ELEMENTS (drag_types_source); ++i) {
850 drag_atoms_source[i] = gdk_atom_intern (drag_types_source[i].target,
856 contact_list_view_set_list_features (EmpathyContactListView *view,
857 EmpathyContactListFeatureFlags features)
859 EmpathyContactListViewPriv *priv = GET_PRIV (view);
861 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view));
863 priv->list_features = features;
865 /* Update DnD source/dest */
866 if (features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_DRAG) {
867 gtk_drag_source_set (GTK_WIDGET (view),
870 G_N_ELEMENTS (drag_types_source),
871 GDK_ACTION_MOVE | GDK_ACTION_COPY);
873 gtk_drag_source_unset (GTK_WIDGET (view));
877 if (features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_DROP) {
878 gtk_drag_dest_set (GTK_WIDGET (view),
879 GTK_DEST_DEFAULT_ALL,
881 G_N_ELEMENTS (drag_types_dest),
882 GDK_ACTION_MOVE | GDK_ACTION_COPY);
884 /* FIXME: URI could still be droped depending on FT feature */
885 gtk_drag_dest_unset (GTK_WIDGET (view));
890 contact_list_view_finalize (GObject *object)
892 EmpathyContactListViewPriv *priv;
894 priv = GET_PRIV (object);
897 g_object_unref (priv->store);
900 G_OBJECT_CLASS (empathy_contact_list_view_parent_class)->finalize (object);
904 contact_list_view_get_property (GObject *object,
909 EmpathyContactListViewPriv *priv;
911 priv = GET_PRIV (object);
915 g_value_set_object (value, priv->store);
917 case PROP_LIST_FEATURES:
918 g_value_set_flags (value, priv->list_features);
920 case PROP_CONTACT_FEATURES:
921 g_value_set_flags (value, priv->contact_features);
924 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
930 contact_list_view_set_property (GObject *object,
935 EmpathyContactListView *view = EMPATHY_CONTACT_LIST_VIEW (object);
936 EmpathyContactListViewPriv *priv = GET_PRIV (object);
940 priv->store = g_value_dup_object (value);
941 contact_list_view_setup (view);
943 case PROP_LIST_FEATURES:
944 contact_list_view_set_list_features (view, g_value_get_flags (value));
946 case PROP_CONTACT_FEATURES:
947 priv->contact_features = g_value_get_flags (value);
950 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
956 empathy_contact_list_view_class_init (EmpathyContactListViewClass *klass)
958 GObjectClass *object_class = G_OBJECT_CLASS (klass);
959 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
961 object_class->finalize = contact_list_view_finalize;
962 object_class->get_property = contact_list_view_get_property;
963 object_class->set_property = contact_list_view_set_property;
965 widget_class->drag_data_received = contact_list_view_drag_data_received;
966 widget_class->drag_drop = contact_list_view_drag_drop;
967 widget_class->drag_begin = contact_list_view_drag_begin;
968 widget_class->drag_data_get = contact_list_view_drag_data_get;
969 widget_class->drag_end = contact_list_view_drag_end;
970 /* FIXME: noticed but when you drag the row over the treeview
971 * fast, it seems to stop redrawing itself, if we don't
972 * connect this signal, all is fine.
974 widget_class->drag_motion = contact_list_view_drag_motion;
976 signals[DRAG_CONTACT_RECEIVED] =
977 g_signal_new ("drag-contact-received",
978 G_OBJECT_CLASS_TYPE (klass),
982 _empathy_gtk_marshal_VOID__OBJECT_STRING_STRING,
984 3, EMPATHY_TYPE_CONTACT, G_TYPE_STRING, G_TYPE_STRING);
986 g_object_class_install_property (object_class,
988 g_param_spec_object ("store",
989 "The store of the view",
990 "The store of the view",
991 EMPATHY_TYPE_CONTACT_LIST_STORE,
992 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
993 g_object_class_install_property (object_class,
995 g_param_spec_flags ("list-features",
996 "Features of the view",
997 "Falgs for all enabled features",
998 EMPATHY_TYPE_CONTACT_LIST_FEATURE_FLAGS,
999 EMPATHY_CONTACT_LIST_FEATURE_NONE,
1000 G_PARAM_READWRITE));
1001 g_object_class_install_property (object_class,
1002 PROP_CONTACT_FEATURES,
1003 g_param_spec_flags ("contact-features",
1004 "Features of the contact menu",
1005 "Falgs for all enabled features for the menu",
1006 EMPATHY_TYPE_CONTACT_FEATURE_FLAGS,
1007 EMPATHY_CONTACT_FEATURE_NONE,
1008 G_PARAM_READWRITE));
1010 g_type_class_add_private (object_class, sizeof (EmpathyContactListViewPriv));
1014 empathy_contact_list_view_init (EmpathyContactListView *view)
1016 EmpathyContactListViewPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (view,
1017 EMPATHY_TYPE_CONTACT_LIST_VIEW, EmpathyContactListViewPriv);
1020 /* Get saved group states. */
1021 empathy_contact_groups_get_all ();
1023 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (view),
1024 empathy_contact_list_store_row_separator_func,
1027 /* Connect to tree view signals rather than override. */
1028 g_signal_connect (view,
1029 "button-press-event",
1030 G_CALLBACK (contact_list_view_button_press_event_cb),
1032 g_signal_connect (view,
1034 G_CALLBACK (contact_list_view_row_activated_cb),
1036 g_signal_connect (view,
1038 G_CALLBACK (contact_list_view_row_expand_or_collapse_cb),
1039 GINT_TO_POINTER (TRUE));
1040 g_signal_connect (view,
1042 G_CALLBACK (contact_list_view_row_expand_or_collapse_cb),
1043 GINT_TO_POINTER (FALSE));
1046 EmpathyContactListView *
1047 empathy_contact_list_view_new (EmpathyContactListStore *store,
1048 EmpathyContactListFeatureFlags list_features,
1049 EmpathyContactFeatureFlags contact_features)
1051 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), NULL);
1053 return g_object_new (EMPATHY_TYPE_CONTACT_LIST_VIEW,
1055 "contact-features", contact_features,
1056 "list-features", list_features,
1061 empathy_contact_list_view_get_selected (EmpathyContactListView *view)
1063 EmpathyContactListViewPriv *priv;
1064 GtkTreeSelection *selection;
1066 GtkTreeModel *model;
1067 EmpathyContact *contact;
1069 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), NULL);
1071 priv = GET_PRIV (view);
1073 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
1074 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
1078 gtk_tree_model_get (model, &iter,
1079 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
1086 empathy_contact_list_view_get_selected_group (EmpathyContactListView *view)
1088 EmpathyContactListViewPriv *priv;
1089 GtkTreeSelection *selection;
1091 GtkTreeModel *model;
1095 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), NULL);
1097 priv = GET_PRIV (view);
1099 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
1100 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
1104 gtk_tree_model_get (model, &iter,
1105 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
1106 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
1118 contact_list_view_remove_dialog_show (GtkWindow *parent,
1119 const gchar *window_title,
1122 GtkWidget *dialog, *label, *image, *hbox;
1125 dialog = gtk_dialog_new_with_buttons (window_title, parent,
1127 GTK_STOCK_DELETE, GTK_RESPONSE_YES,
1128 GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
1130 gtk_dialog_set_has_separator (GTK_DIALOG(dialog), FALSE);
1132 label = gtk_label_new (text);
1133 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_QUESTION, GTK_ICON_SIZE_DIALOG);
1135 hbox = gtk_hbox_new (FALSE, 5);
1136 gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
1137 gtk_box_pack_start_defaults (GTK_BOX (hbox), image);
1138 gtk_box_pack_start_defaults (GTK_BOX (hbox), label);
1139 gtk_box_pack_start_defaults (GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox);
1141 gtk_widget_show (image);
1142 gtk_widget_show (label);
1143 gtk_widget_show (hbox);
1144 gtk_widget_show (dialog);
1146 res = gtk_dialog_run (GTK_DIALOG (dialog));
1147 gtk_widget_destroy (dialog);
1149 return (res == GTK_RESPONSE_YES);
1153 contact_list_view_group_remove_activate_cb (GtkMenuItem *menuitem,
1154 EmpathyContactListView *view)
1156 EmpathyContactListViewPriv *priv = GET_PRIV (view);
1159 group = empathy_contact_list_view_get_selected_group (view);
1164 text = g_strdup_printf (_("Do you really want to remove the group '%s'?"), group);
1165 parent = empathy_get_toplevel_window (GTK_WIDGET (view));
1166 if (contact_list_view_remove_dialog_show (parent, _("Removing group"), text)) {
1167 EmpathyContactList *list;
1169 list = empathy_contact_list_store_get_list_iface (priv->store);
1170 empathy_contact_list_remove_group (list, group);
1180 empathy_contact_list_view_get_group_menu (EmpathyContactListView *view)
1182 EmpathyContactListViewPriv *priv = GET_PRIV (view);
1188 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), NULL);
1190 if (!(priv->list_features & (EMPATHY_CONTACT_LIST_FEATURE_GROUPS_RENAME |
1191 EMPATHY_CONTACT_LIST_FEATURE_GROUPS_REMOVE))) {
1195 group = empathy_contact_list_view_get_selected_group (view);
1200 menu = gtk_menu_new ();
1202 /* FIXME: Not implemented yet
1203 if (priv->features & EMPATHY_CONTACT_LIST_FEATURE_GROUPS_RENAME) {
1204 item = gtk_menu_item_new_with_mnemonic (_("Re_name"));
1205 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1206 gtk_widget_show (item);
1207 g_signal_connect (item, "activate",
1208 G_CALLBACK (contact_list_view_group_rename_activate_cb),
1212 if (priv->list_features & EMPATHY_CONTACT_LIST_FEATURE_GROUPS_REMOVE) {
1213 item = gtk_image_menu_item_new_with_mnemonic (_("_Remove"));
1214 image = gtk_image_new_from_icon_name (GTK_STOCK_REMOVE,
1215 GTK_ICON_SIZE_MENU);
1216 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1217 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1218 gtk_widget_show (item);
1219 g_signal_connect (item, "activate",
1220 G_CALLBACK (contact_list_view_group_remove_activate_cb),
1230 contact_list_view_remove_activate_cb (GtkMenuItem *menuitem,
1231 EmpathyContactListView *view)
1233 EmpathyContactListViewPriv *priv = GET_PRIV (view);
1234 EmpathyContact *contact;
1236 contact = empathy_contact_list_view_get_selected (view);
1242 parent = empathy_get_toplevel_window (GTK_WIDGET (view));
1243 text = g_strdup_printf (_("Do you really want to remove the contact '%s'?"),
1244 empathy_contact_get_name (contact));
1245 if (contact_list_view_remove_dialog_show (parent, _("Removing contact"), text)) {
1246 EmpathyContactList *list;
1248 list = empathy_contact_list_store_get_list_iface (priv->store);
1249 empathy_contact_list_remove (list, contact,
1250 _("Sorry, I don't want you in my contact list anymore."));
1254 g_object_unref (contact);
1259 empathy_contact_list_view_get_contact_menu (EmpathyContactListView *view)
1261 EmpathyContactListViewPriv *priv = GET_PRIV (view);
1262 EmpathyContact *contact;
1267 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), NULL);
1269 contact = empathy_contact_list_view_get_selected (view);
1274 menu = empathy_contact_menu_new (contact, priv->contact_features);
1277 !(priv->list_features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_REMOVE)) {
1278 g_object_unref (contact);
1284 item = gtk_separator_menu_item_new ();
1285 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1286 gtk_widget_show (item);
1288 menu = gtk_menu_new ();
1291 /* Remove contact */
1292 if (priv->list_features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_REMOVE) {
1293 item = gtk_image_menu_item_new_with_mnemonic (_("_Remove"));
1294 image = gtk_image_new_from_icon_name (GTK_STOCK_REMOVE,
1295 GTK_ICON_SIZE_MENU);
1296 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1297 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1298 gtk_widget_show (item);
1299 g_signal_connect (item, "activate",
1300 G_CALLBACK (contact_list_view_remove_activate_cb),
1304 g_object_unref (contact);