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>
31 #include <gdk/gdkkeysyms.h>
33 #include <glade/glade.h>
35 #include <libmissioncontrol/mc-account.h>
37 #include <libempathy/empathy-contact-factory.h>
38 #include <libempathy/empathy-contact-list.h>
39 #include <libempathy/empathy-contact-groups.h>
40 #include <libempathy/empathy-dispatcher.h>
41 #include <libempathy/empathy-utils.h>
43 #include "empathy-contact-list-view.h"
44 #include "empathy-contact-list-store.h"
45 #include "empathy-images.h"
46 #include "empathy-cell-renderer-expander.h"
47 #include "empathy-cell-renderer-text.h"
48 #include "empathy-cell-renderer-activatable.h"
49 #include "empathy-ui-utils.h"
50 #include "empathy-gtk-enum-types.h"
51 #include "empathy-gtk-marshal.h"
53 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
54 #include <libempathy/empathy-debug.h>
56 /* Active users are those which have recently changed state
57 * (e.g. online, offline or from normal to a busy state).
60 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyContactListView)
62 EmpathyContactListStore *store;
63 GtkTreeRowReference *drag_row;
64 EmpathyContactListFeatureFlags list_features;
65 EmpathyContactFeatureFlags contact_features;
66 GtkWidget *tooltip_widget;
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_query_tooltip_cb (EmpathyContactListView *view,
121 gboolean keyboard_mode,
125 EmpathyContactListViewPriv *priv = GET_PRIV (view);
126 EmpathyContact *contact;
131 /* FIXME: We need GTK version >= 2.12.10. See GNOME bug #504087 */
132 if (gtk_check_version (2, 12, 10)) {
136 if (!gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (view), &x, &y,
138 &model, &path, &iter)) {
142 gtk_tree_view_set_tooltip_row (GTK_TREE_VIEW (view), tooltip, path);
143 gtk_tree_path_free (path);
145 gtk_tree_model_get (model, &iter,
146 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
152 if (!priv->tooltip_widget) {
153 priv->tooltip_widget = empathy_contact_widget_new (contact,
154 EMPATHY_CONTACT_WIDGET_EDIT_NONE);
155 g_object_add_weak_pointer (G_OBJECT (priv->tooltip_widget),
156 (gpointer) &priv->tooltip_widget);
158 empathy_contact_widget_set_contact (priv->tooltip_widget,
162 gtk_tooltip_set_custom (tooltip, priv->tooltip_widget);
164 g_object_unref (contact);
170 contact_list_view_drag_data_received (GtkWidget *widget,
171 GdkDragContext *context,
174 GtkSelectionData *selection,
178 EmpathyContactListViewPriv *priv;
179 EmpathyContactList *list;
180 EmpathyContactFactory *factory;
184 GtkTreeViewDropPosition position;
185 EmpathyContact *contact = NULL;
188 gchar *new_group = NULL;
189 gchar *old_group = NULL;
192 priv = GET_PRIV (widget);
194 id = (const gchar*) selection->data;
195 DEBUG ("Received %s%s drag & drop contact from roster with id:'%s'",
196 context->action == GDK_ACTION_MOVE ? "move" : "",
197 context->action == GDK_ACTION_COPY ? "copy" : "",
200 strv = g_strsplit (id, "/", 2);
201 factory = empathy_contact_factory_new ();
202 account = mc_account_lookup (strv[0]);
204 contact = empathy_contact_factory_get_from_id (factory,
207 g_object_unref (account);
209 g_object_unref (factory);
213 DEBUG ("No contact found associated with drag & drop");
217 empathy_contact_run_until_ready (contact,
218 EMPATHY_CONTACT_READY_HANDLE,
221 model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
223 /* Get source group information. */
224 if (priv->drag_row) {
225 path = gtk_tree_row_reference_get_path (priv->drag_row);
227 old_group = empathy_contact_list_store_get_parent_group (model, path, NULL);
228 gtk_tree_path_free (path);
232 /* Get destination group information. */
233 is_row = gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget),
240 new_group = empathy_contact_list_store_get_parent_group (model, path, NULL);
241 gtk_tree_path_free (path);
244 DEBUG ("contact %s (%d) dragged from '%s' to '%s'",
245 empathy_contact_get_id (contact),
246 empathy_contact_get_handle (contact),
247 old_group, new_group);
249 list = empathy_contact_list_store_get_list_iface (priv->store);
251 empathy_contact_list_add_to_group (list, contact, new_group);
253 if (old_group && context->action == GDK_ACTION_MOVE) {
254 empathy_contact_list_remove_from_group (list, contact, old_group);
260 gtk_drag_finish (context, TRUE, FALSE, GDK_CURRENT_TIME);
264 contact_list_view_drag_motion_cb (DragMotionData *data)
266 gtk_tree_view_expand_row (GTK_TREE_VIEW (data->view),
270 data->timeout_id = 0;
276 contact_list_view_drag_motion (GtkWidget *widget,
277 GdkDragContext *context,
282 static DragMotionData *dm = NULL;
285 gboolean is_different = FALSE;
286 gboolean cleanup = TRUE;
288 is_row = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
299 cleanup &= (dm && gtk_tree_path_compare (dm->path, path) != 0);
300 is_different = (!dm || (dm && gtk_tree_path_compare (dm->path, path) != 0));
305 if (!is_different && !cleanup) {
310 gtk_tree_path_free (dm->path);
311 if (dm->timeout_id) {
312 g_source_remove (dm->timeout_id);
320 if (!gtk_tree_view_row_expanded (GTK_TREE_VIEW (widget), path)) {
321 dm = g_new0 (DragMotionData, 1);
323 dm->view = EMPATHY_CONTACT_LIST_VIEW (widget);
324 dm->path = gtk_tree_path_copy (path);
326 dm->timeout_id = g_timeout_add_seconds (1,
327 (GSourceFunc) contact_list_view_drag_motion_cb,
335 contact_list_view_drag_begin (GtkWidget *widget,
336 GdkDragContext *context)
338 EmpathyContactListViewPriv *priv;
339 GtkTreeSelection *selection;
344 priv = GET_PRIV (widget);
346 GTK_WIDGET_CLASS (empathy_contact_list_view_parent_class)->drag_begin (widget,
349 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (widget));
350 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
354 path = gtk_tree_model_get_path (model, &iter);
355 priv->drag_row = gtk_tree_row_reference_new (model, path);
356 gtk_tree_path_free (path);
360 contact_list_view_drag_data_get (GtkWidget *widget,
361 GdkDragContext *context,
362 GtkSelectionData *selection,
366 EmpathyContactListViewPriv *priv;
367 GtkTreePath *src_path;
370 EmpathyContact *contact;
372 const gchar *contact_id;
373 const gchar *account_id;
376 priv = GET_PRIV (widget);
378 model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
379 if (!priv->drag_row) {
383 src_path = gtk_tree_row_reference_get_path (priv->drag_row);
388 if (!gtk_tree_model_get_iter (model, &iter, src_path)) {
389 gtk_tree_path_free (src_path);
393 gtk_tree_path_free (src_path);
395 contact = empathy_contact_list_view_get_selected (EMPATHY_CONTACT_LIST_VIEW (widget));
400 account = empathy_contact_get_account (contact);
401 account_id = mc_account_get_unique_name (account);
402 contact_id = empathy_contact_get_id (contact);
403 g_object_unref (contact);
404 str = g_strconcat (account_id, "/", contact_id, NULL);
407 case DND_DRAG_TYPE_CONTACT_ID:
408 gtk_selection_data_set (selection, drag_atoms_source[info], 8,
409 (guchar*)str, strlen (str) + 1);
417 contact_list_view_drag_end (GtkWidget *widget,
418 GdkDragContext *context)
420 EmpathyContactListViewPriv *priv;
422 priv = GET_PRIV (widget);
424 GTK_WIDGET_CLASS (empathy_contact_list_view_parent_class)->drag_end (widget,
427 if (priv->drag_row) {
428 gtk_tree_row_reference_free (priv->drag_row);
429 priv->drag_row = NULL;
434 contact_list_view_drag_drop (GtkWidget *widget,
435 GdkDragContext *drag_context,
444 EmpathyContactListView *view;
450 contact_list_view_popup_menu_idle_cb (gpointer user_data)
452 MenuPopupData *data = user_data;
455 menu = empathy_contact_list_view_get_contact_menu (data->view);
457 menu = empathy_contact_list_view_get_group_menu (data->view);
461 gtk_widget_show (menu);
462 gtk_menu_popup (GTK_MENU (menu),
463 NULL, NULL, NULL, NULL,
464 data->button, data->time);
467 g_slice_free (MenuPopupData, data);
473 contact_list_view_button_press_event_cb (EmpathyContactListView *view,
474 GdkEventButton *event,
477 if (event->button == 3) {
480 data = g_slice_new (MenuPopupData);
482 data->button = event->button;
483 data->time = event->time;
484 g_idle_add (contact_list_view_popup_menu_idle_cb, data);
491 contact_list_view_key_press_event_cb (EmpathyContactListView *view,
495 if (event->keyval == GDK_Menu) {
498 data = g_slice_new (MenuPopupData);
501 data->time = event->time;
502 g_idle_add (contact_list_view_popup_menu_idle_cb, data);
509 contact_list_view_row_activated (GtkTreeView *view,
511 GtkTreeViewColumn *column)
513 EmpathyContactListViewPriv *priv = GET_PRIV (view);
514 EmpathyContact *contact;
518 if (!(priv->contact_features & EMPATHY_CONTACT_FEATURE_CHAT)) {
522 model = GTK_TREE_MODEL (priv->store);
523 gtk_tree_model_get_iter (model, &iter, path);
524 gtk_tree_model_get (model, &iter,
525 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
529 DEBUG ("Starting a chat");
530 empathy_dispatcher_chat_with_contact (contact);
531 g_object_unref (contact);
536 contact_list_view_voip_activated_cb (EmpathyCellRendererActivatable *cell,
537 const gchar *path_string,
538 EmpathyContactListView *view)
540 EmpathyContactListViewPriv *priv = GET_PRIV (view);
543 EmpathyContact *contact;
545 if (!(priv->contact_features & EMPATHY_CONTACT_FEATURE_CALL)) {
549 model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
550 if (!gtk_tree_model_get_iter_from_string (model, &iter, path_string)) {
554 gtk_tree_model_get (model, &iter,
555 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
559 empathy_dispatcher_call_with_contact (contact);
560 g_object_unref (contact);
565 contact_list_view_cell_set_background (EmpathyContactListView *view,
566 GtkCellRenderer *cell,
573 style = gtk_widget_get_style (GTK_WIDGET (view));
575 if (!is_group && is_active) {
576 color = style->bg[GTK_STATE_SELECTED];
578 /* Here we take the current theme colour and add it to
579 * the colour for white and average the two. This
580 * gives a colour which is inline with the theme but
583 color.red = (color.red + (style->white).red) / 2;
584 color.green = (color.green + (style->white).green) / 2;
585 color.blue = (color.blue + (style->white).blue) / 2;
588 "cell-background-gdk", &color,
592 "cell-background-gdk", NULL,
598 contact_list_view_pixbuf_cell_data_func (GtkTreeViewColumn *tree_column,
599 GtkCellRenderer *cell,
602 EmpathyContactListView *view)
608 gtk_tree_model_get (model, iter,
609 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
610 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, &is_active,
611 EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, &icon_name,
615 "visible", !is_group,
616 "icon-name", icon_name,
621 contact_list_view_cell_set_background (view, cell, is_group, is_active);
625 contact_list_view_voip_cell_data_func (GtkTreeViewColumn *tree_column,
626 GtkCellRenderer *cell,
629 EmpathyContactListView *view)
635 gtk_tree_model_get (model, iter,
636 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
637 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, &is_active,
638 EMPATHY_CONTACT_LIST_STORE_COL_CAN_VOIP, &can_voip,
642 "visible", !is_group && can_voip,
643 "icon-name", EMPATHY_IMAGE_VOIP,
646 contact_list_view_cell_set_background (view, cell, is_group, is_active);
650 contact_list_view_avatar_cell_data_func (GtkTreeViewColumn *tree_column,
651 GtkCellRenderer *cell,
654 EmpathyContactListView *view)
657 gboolean show_avatar;
661 gtk_tree_model_get (model, iter,
662 EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR, &pixbuf,
663 EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR_VISIBLE, &show_avatar,
664 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
665 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, &is_active,
669 "visible", !is_group && show_avatar,
674 g_object_unref (pixbuf);
677 contact_list_view_cell_set_background (view, cell, is_group, is_active);
681 contact_list_view_text_cell_data_func (GtkTreeViewColumn *tree_column,
682 GtkCellRenderer *cell,
685 EmpathyContactListView *view)
689 gboolean show_status;
691 gtk_tree_model_get (model, iter,
692 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
693 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, &is_active,
694 EMPATHY_CONTACT_LIST_STORE_COL_STATUS_VISIBLE, &show_status,
698 "show-status", show_status,
701 contact_list_view_cell_set_background (view, cell, is_group, is_active);
705 contact_list_view_expander_cell_data_func (GtkTreeViewColumn *column,
706 GtkCellRenderer *cell,
709 EmpathyContactListView *view)
714 gtk_tree_model_get (model, iter,
715 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
716 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, &is_active,
719 if (gtk_tree_model_iter_has_child (model, iter)) {
721 gboolean row_expanded;
723 path = gtk_tree_model_get_path (model, iter);
724 row_expanded = gtk_tree_view_row_expanded (GTK_TREE_VIEW (column->tree_view), path);
725 gtk_tree_path_free (path);
729 "expander-style", row_expanded ? GTK_EXPANDER_EXPANDED : GTK_EXPANDER_COLLAPSED,
732 g_object_set (cell, "visible", FALSE, NULL);
735 contact_list_view_cell_set_background (view, cell, is_group, is_active);
739 contact_list_view_row_expand_or_collapse_cb (EmpathyContactListView *view,
744 EmpathyContactListViewPriv *priv = GET_PRIV (view);
749 if (!(priv->list_features & EMPATHY_CONTACT_LIST_FEATURE_GROUPS_SAVE)) {
753 model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
755 gtk_tree_model_get (model, iter,
756 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
759 expanded = GPOINTER_TO_INT (user_data);
760 empathy_contact_group_set_expanded (name, expanded);
766 contact_list_view_row_has_child_toggled_cb (GtkTreeModel *model,
769 EmpathyContactListView *view)
771 EmpathyContactListViewPriv *priv = GET_PRIV (view);
772 gboolean is_group = FALSE;
775 gtk_tree_model_get (model, iter,
776 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
777 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
780 if (!is_group || G_STR_EMPTY (name)) {
785 if (!(priv->list_features & EMPATHY_CONTACT_LIST_FEATURE_GROUPS_SAVE) ||
786 empathy_contact_group_get_expanded (name)) {
787 g_signal_handlers_block_by_func (view,
788 contact_list_view_row_expand_or_collapse_cb,
789 GINT_TO_POINTER (TRUE));
790 gtk_tree_view_expand_row (GTK_TREE_VIEW (view), path, TRUE);
791 g_signal_handlers_unblock_by_func (view,
792 contact_list_view_row_expand_or_collapse_cb,
793 GINT_TO_POINTER (TRUE));
795 g_signal_handlers_block_by_func (view,
796 contact_list_view_row_expand_or_collapse_cb,
797 GINT_TO_POINTER (FALSE));
798 gtk_tree_view_collapse_row (GTK_TREE_VIEW (view), path);
799 g_signal_handlers_unblock_by_func (view,
800 contact_list_view_row_expand_or_collapse_cb,
801 GINT_TO_POINTER (FALSE));
808 contact_list_view_setup (EmpathyContactListView *view)
810 EmpathyContactListViewPriv *priv;
811 GtkCellRenderer *cell;
812 GtkTreeViewColumn *col;
815 priv = GET_PRIV (view);
817 gtk_tree_view_set_search_equal_func (GTK_TREE_VIEW (view),
818 empathy_contact_list_store_search_equal_func,
821 g_signal_connect (priv->store, "row-has-child-toggled",
822 G_CALLBACK (contact_list_view_row_has_child_toggled_cb),
824 gtk_tree_view_set_model (GTK_TREE_VIEW (view),
825 GTK_TREE_MODEL (priv->store));
829 "headers-visible", FALSE,
831 "show-expanders", FALSE,
834 col = gtk_tree_view_column_new ();
837 cell = gtk_cell_renderer_pixbuf_new ();
838 gtk_tree_view_column_pack_start (col, cell, FALSE);
839 gtk_tree_view_column_set_cell_data_func (
841 (GtkTreeCellDataFunc) contact_list_view_pixbuf_cell_data_func,
851 cell = empathy_cell_renderer_text_new ();
852 gtk_tree_view_column_pack_start (col, cell, TRUE);
853 gtk_tree_view_column_set_cell_data_func (
855 (GtkTreeCellDataFunc) contact_list_view_text_cell_data_func,
858 gtk_tree_view_column_add_attribute (col, cell,
859 "name", EMPATHY_CONTACT_LIST_STORE_COL_NAME);
860 gtk_tree_view_column_add_attribute (col, cell,
861 "status", EMPATHY_CONTACT_LIST_STORE_COL_STATUS);
862 gtk_tree_view_column_add_attribute (col, cell,
863 "is_group", EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP);
865 /* Voip Capability Icon */
866 cell = empathy_cell_renderer_activatable_new ();
867 gtk_tree_view_column_pack_start (col, cell, FALSE);
868 gtk_tree_view_column_set_cell_data_func (
870 (GtkTreeCellDataFunc) contact_list_view_voip_cell_data_func,
877 g_signal_connect (cell, "path-activated",
878 G_CALLBACK (contact_list_view_voip_activated_cb),
882 cell = gtk_cell_renderer_pixbuf_new ();
883 gtk_tree_view_column_pack_start (col, cell, FALSE);
884 gtk_tree_view_column_set_cell_data_func (
886 (GtkTreeCellDataFunc) contact_list_view_avatar_cell_data_func,
898 cell = empathy_cell_renderer_expander_new ();
899 gtk_tree_view_column_pack_end (col, cell, FALSE);
900 gtk_tree_view_column_set_cell_data_func (
902 (GtkTreeCellDataFunc) contact_list_view_expander_cell_data_func,
905 /* Actually add the column now we have added all cell renderers */
906 gtk_tree_view_append_column (GTK_TREE_VIEW (view), col);
909 for (i = 0; i < G_N_ELEMENTS (drag_types_dest); ++i) {
910 drag_atoms_dest[i] = gdk_atom_intern (drag_types_dest[i].target,
914 for (i = 0; i < G_N_ELEMENTS (drag_types_source); ++i) {
915 drag_atoms_source[i] = gdk_atom_intern (drag_types_source[i].target,
921 contact_list_view_set_list_features (EmpathyContactListView *view,
922 EmpathyContactListFeatureFlags features)
924 EmpathyContactListViewPriv *priv = GET_PRIV (view);
925 gboolean has_tooltip;
927 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view));
929 priv->list_features = features;
931 /* Update DnD source/dest */
932 if (features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_DRAG) {
933 gtk_drag_source_set (GTK_WIDGET (view),
936 G_N_ELEMENTS (drag_types_source),
937 GDK_ACTION_MOVE | GDK_ACTION_COPY);
939 gtk_drag_source_unset (GTK_WIDGET (view));
943 if (features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_DROP) {
944 gtk_drag_dest_set (GTK_WIDGET (view),
945 GTK_DEST_DEFAULT_ALL,
947 G_N_ELEMENTS (drag_types_dest),
948 GDK_ACTION_MOVE | GDK_ACTION_COPY);
950 /* FIXME: URI could still be droped depending on FT feature */
951 gtk_drag_dest_unset (GTK_WIDGET (view));
954 /* Update has-tooltip */
955 has_tooltip = (features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_TOOLTIP) != 0;
956 gtk_widget_set_has_tooltip (GTK_WIDGET (view), has_tooltip);
960 contact_list_view_finalize (GObject *object)
962 EmpathyContactListViewPriv *priv;
964 priv = GET_PRIV (object);
967 g_object_unref (priv->store);
970 G_OBJECT_CLASS (empathy_contact_list_view_parent_class)->finalize (object);
974 contact_list_view_get_property (GObject *object,
979 EmpathyContactListViewPriv *priv;
981 priv = GET_PRIV (object);
985 g_value_set_object (value, priv->store);
987 case PROP_LIST_FEATURES:
988 g_value_set_flags (value, priv->list_features);
990 case PROP_CONTACT_FEATURES:
991 g_value_set_flags (value, priv->contact_features);
994 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1000 contact_list_view_set_property (GObject *object,
1002 const GValue *value,
1005 EmpathyContactListView *view = EMPATHY_CONTACT_LIST_VIEW (object);
1006 EmpathyContactListViewPriv *priv = GET_PRIV (object);
1010 priv->store = g_value_dup_object (value);
1011 contact_list_view_setup (view);
1013 case PROP_LIST_FEATURES:
1014 contact_list_view_set_list_features (view, g_value_get_flags (value));
1016 case PROP_CONTACT_FEATURES:
1017 priv->contact_features = g_value_get_flags (value);
1020 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1026 empathy_contact_list_view_class_init (EmpathyContactListViewClass *klass)
1028 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1029 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1030 GtkTreeViewClass *tree_view_class = GTK_TREE_VIEW_CLASS (klass);
1032 object_class->finalize = contact_list_view_finalize;
1033 object_class->get_property = contact_list_view_get_property;
1034 object_class->set_property = contact_list_view_set_property;
1036 widget_class->drag_data_received = contact_list_view_drag_data_received;
1037 widget_class->drag_drop = contact_list_view_drag_drop;
1038 widget_class->drag_begin = contact_list_view_drag_begin;
1039 widget_class->drag_data_get = contact_list_view_drag_data_get;
1040 widget_class->drag_end = contact_list_view_drag_end;
1041 widget_class->drag_motion = contact_list_view_drag_motion;
1043 /* We use the class method to let user of this widget to connect to
1044 * the signal and stop emission of the signal so the default handler
1045 * won't be called. */
1046 tree_view_class->row_activated = contact_list_view_row_activated;
1048 signals[DRAG_CONTACT_RECEIVED] =
1049 g_signal_new ("drag-contact-received",
1050 G_OBJECT_CLASS_TYPE (klass),
1054 _empathy_gtk_marshal_VOID__OBJECT_STRING_STRING,
1056 3, EMPATHY_TYPE_CONTACT, G_TYPE_STRING, G_TYPE_STRING);
1058 g_object_class_install_property (object_class,
1060 g_param_spec_object ("store",
1061 "The store of the view",
1062 "The store of the view",
1063 EMPATHY_TYPE_CONTACT_LIST_STORE,
1064 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
1065 g_object_class_install_property (object_class,
1067 g_param_spec_flags ("list-features",
1068 "Features of the view",
1069 "Falgs for all enabled features",
1070 EMPATHY_TYPE_CONTACT_LIST_FEATURE_FLAGS,
1071 EMPATHY_CONTACT_LIST_FEATURE_NONE,
1072 G_PARAM_READWRITE));
1073 g_object_class_install_property (object_class,
1074 PROP_CONTACT_FEATURES,
1075 g_param_spec_flags ("contact-features",
1076 "Features of the contact menu",
1077 "Falgs for all enabled features for the menu",
1078 EMPATHY_TYPE_CONTACT_FEATURE_FLAGS,
1079 EMPATHY_CONTACT_FEATURE_NONE,
1080 G_PARAM_READWRITE));
1082 g_type_class_add_private (object_class, sizeof (EmpathyContactListViewPriv));
1086 empathy_contact_list_view_init (EmpathyContactListView *view)
1088 EmpathyContactListViewPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (view,
1089 EMPATHY_TYPE_CONTACT_LIST_VIEW, EmpathyContactListViewPriv);
1092 /* Get saved group states. */
1093 empathy_contact_groups_get_all ();
1095 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (view),
1096 empathy_contact_list_store_row_separator_func,
1099 /* Connect to tree view signals rather than override. */
1100 g_signal_connect (view, "button-press-event",
1101 G_CALLBACK (contact_list_view_button_press_event_cb),
1103 g_signal_connect (view, "key-press-event",
1104 G_CALLBACK (contact_list_view_key_press_event_cb),
1106 g_signal_connect (view, "row-expanded",
1107 G_CALLBACK (contact_list_view_row_expand_or_collapse_cb),
1108 GINT_TO_POINTER (TRUE));
1109 g_signal_connect (view, "row-collapsed",
1110 G_CALLBACK (contact_list_view_row_expand_or_collapse_cb),
1111 GINT_TO_POINTER (FALSE));
1112 g_signal_connect (view, "query-tooltip",
1113 G_CALLBACK (contact_list_view_query_tooltip_cb),
1117 EmpathyContactListView *
1118 empathy_contact_list_view_new (EmpathyContactListStore *store,
1119 EmpathyContactListFeatureFlags list_features,
1120 EmpathyContactFeatureFlags contact_features)
1122 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), NULL);
1124 return g_object_new (EMPATHY_TYPE_CONTACT_LIST_VIEW,
1126 "contact-features", contact_features,
1127 "list-features", list_features,
1132 empathy_contact_list_view_get_selected (EmpathyContactListView *view)
1134 EmpathyContactListViewPriv *priv;
1135 GtkTreeSelection *selection;
1137 GtkTreeModel *model;
1138 EmpathyContact *contact;
1140 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), NULL);
1142 priv = GET_PRIV (view);
1144 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
1145 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
1149 gtk_tree_model_get (model, &iter,
1150 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
1157 empathy_contact_list_view_get_selected_group (EmpathyContactListView *view)
1159 EmpathyContactListViewPriv *priv;
1160 GtkTreeSelection *selection;
1162 GtkTreeModel *model;
1166 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), NULL);
1168 priv = GET_PRIV (view);
1170 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
1171 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
1175 gtk_tree_model_get (model, &iter,
1176 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
1177 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
1189 contact_list_view_remove_dialog_show (GtkWindow *parent,
1190 const gchar *window_title,
1193 GtkWidget *dialog, *label, *image, *hbox;
1196 dialog = gtk_dialog_new_with_buttons (window_title, parent,
1198 GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
1199 GTK_STOCK_DELETE, GTK_RESPONSE_YES,
1201 gtk_dialog_set_has_separator (GTK_DIALOG(dialog), FALSE);
1203 label = gtk_label_new (text);
1204 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_QUESTION, GTK_ICON_SIZE_DIALOG);
1206 hbox = gtk_hbox_new (FALSE, 5);
1207 gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
1208 gtk_box_pack_start_defaults (GTK_BOX (hbox), image);
1209 gtk_box_pack_start_defaults (GTK_BOX (hbox), label);
1210 gtk_box_pack_start_defaults (GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox);
1212 gtk_widget_show (image);
1213 gtk_widget_show (label);
1214 gtk_widget_show (hbox);
1215 gtk_widget_show (dialog);
1217 res = gtk_dialog_run (GTK_DIALOG (dialog));
1218 gtk_widget_destroy (dialog);
1220 return (res == GTK_RESPONSE_YES);
1224 contact_list_view_group_remove_activate_cb (GtkMenuItem *menuitem,
1225 EmpathyContactListView *view)
1227 EmpathyContactListViewPriv *priv = GET_PRIV (view);
1230 group = empathy_contact_list_view_get_selected_group (view);
1235 text = g_strdup_printf (_("Do you really want to remove the group '%s'?"), group);
1236 parent = empathy_get_toplevel_window (GTK_WIDGET (view));
1237 if (contact_list_view_remove_dialog_show (parent, _("Removing group"), text)) {
1238 EmpathyContactList *list;
1240 list = empathy_contact_list_store_get_list_iface (priv->store);
1241 empathy_contact_list_remove_group (list, group);
1251 empathy_contact_list_view_get_group_menu (EmpathyContactListView *view)
1253 EmpathyContactListViewPriv *priv = GET_PRIV (view);
1259 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), NULL);
1261 if (!(priv->list_features & (EMPATHY_CONTACT_LIST_FEATURE_GROUPS_RENAME |
1262 EMPATHY_CONTACT_LIST_FEATURE_GROUPS_REMOVE))) {
1266 group = empathy_contact_list_view_get_selected_group (view);
1271 menu = gtk_menu_new ();
1273 /* FIXME: Not implemented yet
1274 if (priv->features & EMPATHY_CONTACT_LIST_FEATURE_GROUPS_RENAME) {
1275 item = gtk_menu_item_new_with_mnemonic (_("Re_name"));
1276 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1277 gtk_widget_show (item);
1278 g_signal_connect (item, "activate",
1279 G_CALLBACK (contact_list_view_group_rename_activate_cb),
1283 if (priv->list_features & EMPATHY_CONTACT_LIST_FEATURE_GROUPS_REMOVE) {
1284 item = gtk_image_menu_item_new_with_mnemonic (_("_Remove"));
1285 image = gtk_image_new_from_icon_name (GTK_STOCK_REMOVE,
1286 GTK_ICON_SIZE_MENU);
1287 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1288 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1289 gtk_widget_show (item);
1290 g_signal_connect (item, "activate",
1291 G_CALLBACK (contact_list_view_group_remove_activate_cb),
1301 contact_list_view_remove_activate_cb (GtkMenuItem *menuitem,
1302 EmpathyContactListView *view)
1304 EmpathyContactListViewPriv *priv = GET_PRIV (view);
1305 EmpathyContact *contact;
1307 contact = empathy_contact_list_view_get_selected (view);
1313 parent = empathy_get_toplevel_window (GTK_WIDGET (view));
1314 text = g_strdup_printf (_("Do you really want to remove the contact '%s'?"),
1315 empathy_contact_get_name (contact));
1316 if (contact_list_view_remove_dialog_show (parent, _("Removing contact"), text)) {
1317 EmpathyContactList *list;
1319 list = empathy_contact_list_store_get_list_iface (priv->store);
1320 empathy_contact_list_remove (list, contact,
1321 _("Sorry, I don't want you in my contact list anymore."));
1325 g_object_unref (contact);
1330 empathy_contact_list_view_get_contact_menu (EmpathyContactListView *view)
1332 EmpathyContactListViewPriv *priv = GET_PRIV (view);
1333 EmpathyContact *contact;
1338 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), NULL);
1340 contact = empathy_contact_list_view_get_selected (view);
1345 menu = empathy_contact_menu_new (contact, priv->contact_features);
1347 if (!(priv->list_features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_REMOVE)) {
1348 g_object_unref (contact);
1354 item = gtk_separator_menu_item_new ();
1355 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1356 gtk_widget_show (item);
1358 menu = gtk_menu_new ();
1361 /* Remove contact */
1362 if (priv->list_features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_REMOVE) {
1363 item = gtk_image_menu_item_new_with_mnemonic (_("_Remove"));
1364 image = gtk_image_new_from_icon_name (GTK_STOCK_REMOVE,
1365 GTK_ICON_SIZE_MENU);
1366 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1367 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1368 gtk_widget_show (item);
1369 g_signal_connect (item, "activate",
1370 G_CALLBACK (contact_list_view_remove_activate_cb),
1374 g_object_unref (contact);