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-debug.h>
40 #include <libempathy/empathy-utils.h>
42 #include "empathy-contact-list-view.h"
43 #include "empathy-contact-list-store.h"
44 #include "empathy-contact-menu.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_DOMAIN "ContactListView"
55 /* Flashing delay for icons (milliseconds). */
56 #define FLASH_TIMEOUT 500
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) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_CONTACT_LIST_VIEW, EmpathyContactListViewPriv))
65 EmpathyContactListStore *store;
66 GtkTreeRowReference *drag_row;
67 EmpathyContactListFeatures features;
68 } EmpathyContactListViewPriv;
71 EmpathyContactListView *view;
77 EmpathyContactListView *view;
78 EmpathyContact *contact;
88 DND_DRAG_TYPE_CONTACT_ID,
93 static const GtkTargetEntry drag_types_dest[] = {
94 { "text/contact-id", 0, DND_DRAG_TYPE_CONTACT_ID },
95 { "text/uri-list", 0, DND_DRAG_TYPE_URL },
96 { "text/plain", 0, DND_DRAG_TYPE_STRING },
97 { "STRING", 0, DND_DRAG_TYPE_STRING },
100 static const GtkTargetEntry drag_types_source[] = {
101 { "text/contact-id", 0, DND_DRAG_TYPE_CONTACT_ID },
104 static GdkAtom drag_atoms_dest[G_N_ELEMENTS (drag_types_dest)];
105 static GdkAtom drag_atoms_source[G_N_ELEMENTS (drag_types_source)];
108 DRAG_CONTACT_RECEIVED,
112 static guint signals[LAST_SIGNAL];
114 G_DEFINE_TYPE (EmpathyContactListView, empathy_contact_list_view, GTK_TYPE_TREE_VIEW);
117 contact_list_view_drag_data_received (GtkWidget *widget,
118 GdkDragContext *context,
121 GtkSelectionData *selection,
125 EmpathyContactListViewPriv *priv;
126 EmpathyContactList *list;
127 EmpathyContactFactory *factory;
131 GtkTreeViewDropPosition position;
132 EmpathyContact *contact = NULL;
135 gchar *new_group = NULL;
136 gchar *old_group = NULL;
139 priv = GET_PRIV (widget);
141 id = (const gchar*) selection->data;
142 empathy_debug (DEBUG_DOMAIN, "Received %s%s drag & drop contact from roster with id:'%s'",
143 context->action == GDK_ACTION_MOVE ? "move" : "",
144 context->action == GDK_ACTION_COPY ? "copy" : "",
147 strv = g_strsplit (id, "/", 2);
148 factory = empathy_contact_factory_new ();
149 account = mc_account_lookup (strv[0]);
151 contact = empathy_contact_factory_get_from_id (factory,
154 g_object_unref (account);
156 g_object_unref (factory);
160 empathy_debug (DEBUG_DOMAIN, "No contact found associated with drag & drop");
164 empathy_contact_run_until_ready (contact,
165 EMPATHY_CONTACT_READY_HANDLE,
168 model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
170 /* Get source group information. */
171 if (priv->drag_row) {
172 path = gtk_tree_row_reference_get_path (priv->drag_row);
174 old_group = empathy_contact_list_store_get_parent_group (model, path, NULL);
175 gtk_tree_path_free (path);
179 /* Get destination group information. */
180 is_row = gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget),
187 new_group = empathy_contact_list_store_get_parent_group (model, path, NULL);
188 gtk_tree_path_free (path);
191 empathy_debug (DEBUG_DOMAIN,
192 "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 contact_list_view_button_press_event_cb (EmpathyContactListView *view,
393 GdkEventButton *event,
396 EmpathyContactListViewPriv *priv;
397 EmpathyContact *contact;
399 GtkTreeSelection *selection;
405 priv = GET_PRIV (view);
407 if (event->button != 3) {
411 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
412 model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
414 gtk_widget_grab_focus (GTK_WIDGET (view));
416 row_exists = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (view),
424 gtk_tree_selection_unselect_all (selection);
425 gtk_tree_selection_select_path (selection, path);
427 gtk_tree_model_get_iter (model, &iter, path);
428 gtk_tree_path_free (path);
430 gtk_tree_model_get (model, &iter,
431 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
435 menu = empathy_contact_list_view_get_contact_menu (view, contact);
436 g_object_unref (contact);
438 menu = empathy_contact_list_view_get_group_menu (view);
445 gtk_widget_show (menu);
447 gtk_menu_popup (GTK_MENU (menu),
448 NULL, NULL, NULL, NULL,
449 event->button, event->time);
455 contact_list_view_row_activated_cb (EmpathyContactListView *view,
457 GtkTreeViewColumn *col,
460 EmpathyContactListViewPriv *priv = GET_PRIV (view);
461 EmpathyContact *contact;
465 if (!(priv->features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_CHAT)) {
469 model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
471 gtk_tree_model_get_iter (model, &iter, path);
472 gtk_tree_model_get (model, &iter,
473 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
477 empathy_chat_with_contact (contact);
478 g_object_unref (contact);
483 contact_list_view_voip_activated_cb (EmpathyCellRendererActivatable *cell,
484 const gchar *path_string,
485 EmpathyContactListView *view)
487 EmpathyContactListViewPriv *priv = GET_PRIV (view);
490 EmpathyContact *contact;
492 if (!(priv->features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_CALL)) {
496 model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
497 if (!gtk_tree_model_get_iter_from_string (model, &iter, path_string)) {
501 gtk_tree_model_get (model, &iter,
502 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
506 empathy_call_with_contact (contact);
507 g_object_unref (contact);
512 contact_list_view_cell_set_background (EmpathyContactListView *view,
513 GtkCellRenderer *cell,
520 style = gtk_widget_get_style (GTK_WIDGET (view));
522 if (!is_group && is_active) {
523 color = style->bg[GTK_STATE_SELECTED];
525 /* Here we take the current theme colour and add it to
526 * the colour for white and average the two. This
527 * gives a colour which is inline with the theme but
530 color.red = (color.red + (style->white).red) / 2;
531 color.green = (color.green + (style->white).green) / 2;
532 color.blue = (color.blue + (style->white).blue) / 2;
535 "cell-background-gdk", &color,
539 "cell-background-gdk", NULL,
545 contact_list_view_pixbuf_cell_data_func (GtkTreeViewColumn *tree_column,
546 GtkCellRenderer *cell,
549 EmpathyContactListView *view)
555 gtk_tree_model_get (model, iter,
556 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
557 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, &is_active,
558 EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, &icon_name,
562 "visible", !is_group,
563 "icon-name", icon_name,
568 contact_list_view_cell_set_background (view, cell, is_group, is_active);
572 contact_list_view_voip_cell_data_func (GtkTreeViewColumn *tree_column,
573 GtkCellRenderer *cell,
576 EmpathyContactListView *view)
582 gtk_tree_model_get (model, iter,
583 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
584 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, &is_active,
585 EMPATHY_CONTACT_LIST_STORE_COL_CAN_VOIP, &can_voip,
589 "visible", !is_group && can_voip,
590 "icon-name", EMPATHY_IMAGE_VOIP,
593 contact_list_view_cell_set_background (view, cell, is_group, is_active);
597 contact_list_view_avatar_cell_data_func (GtkTreeViewColumn *tree_column,
598 GtkCellRenderer *cell,
601 EmpathyContactListView *view)
604 gboolean show_avatar;
608 gtk_tree_model_get (model, iter,
609 EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR, &pixbuf,
610 EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR_VISIBLE, &show_avatar,
611 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
612 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, &is_active,
616 "visible", !is_group && show_avatar,
621 g_object_unref (pixbuf);
624 contact_list_view_cell_set_background (view, cell, is_group, is_active);
628 contact_list_view_text_cell_data_func (GtkTreeViewColumn *tree_column,
629 GtkCellRenderer *cell,
632 EmpathyContactListView *view)
636 gboolean show_status;
638 gtk_tree_model_get (model, iter,
639 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
640 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, &is_active,
641 EMPATHY_CONTACT_LIST_STORE_COL_STATUS_VISIBLE, &show_status,
645 "show-status", show_status,
648 contact_list_view_cell_set_background (view, cell, is_group, is_active);
652 contact_list_view_expander_cell_data_func (GtkTreeViewColumn *column,
653 GtkCellRenderer *cell,
656 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,
666 if (gtk_tree_model_iter_has_child (model, iter)) {
668 gboolean row_expanded;
670 path = gtk_tree_model_get_path (model, iter);
671 row_expanded = gtk_tree_view_row_expanded (GTK_TREE_VIEW (column->tree_view), path);
672 gtk_tree_path_free (path);
676 "expander-style", row_expanded ? GTK_EXPANDER_EXPANDED : GTK_EXPANDER_COLLAPSED,
679 g_object_set (cell, "visible", FALSE, NULL);
682 contact_list_view_cell_set_background (view, cell, is_group, is_active);
686 contact_list_view_row_expand_or_collapse_cb (EmpathyContactListView *view,
691 EmpathyContactListViewPriv *priv = GET_PRIV (view);
696 if (!(priv->features & EMPATHY_CONTACT_LIST_FEATURE_GROUPS_SAVE)) {
700 model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
702 gtk_tree_model_get (model, iter,
703 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
706 expanded = GPOINTER_TO_INT (user_data);
707 empathy_contact_group_set_expanded (name, expanded);
713 contact_list_view_row_has_child_toggled_cb (GtkTreeModel *model,
716 EmpathyContactListView *view)
718 EmpathyContactListViewPriv *priv = GET_PRIV (view);
719 gboolean is_group = FALSE;
722 gtk_tree_model_get (model, iter,
723 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
724 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
727 if (!is_group || G_STR_EMPTY (name)) {
732 if (!(priv->features & EMPATHY_CONTACT_LIST_FEATURE_GROUPS_SAVE) ||
733 empathy_contact_group_get_expanded (name)) {
734 g_signal_handlers_block_by_func (view,
735 contact_list_view_row_expand_or_collapse_cb,
736 GINT_TO_POINTER (TRUE));
737 gtk_tree_view_expand_row (GTK_TREE_VIEW (view), path, TRUE);
738 g_signal_handlers_unblock_by_func (view,
739 contact_list_view_row_expand_or_collapse_cb,
740 GINT_TO_POINTER (TRUE));
742 g_signal_handlers_block_by_func (view,
743 contact_list_view_row_expand_or_collapse_cb,
744 GINT_TO_POINTER (FALSE));
745 gtk_tree_view_collapse_row (GTK_TREE_VIEW (view), path);
746 g_signal_handlers_unblock_by_func (view,
747 contact_list_view_row_expand_or_collapse_cb,
748 GINT_TO_POINTER (FALSE));
755 contact_list_view_setup (EmpathyContactListView *view)
757 EmpathyContactListViewPriv *priv;
758 GtkCellRenderer *cell;
759 GtkTreeViewColumn *col;
762 priv = GET_PRIV (view);
764 gtk_tree_view_set_search_equal_func (GTK_TREE_VIEW (view),
765 empathy_contact_list_store_search_equal_func,
768 g_signal_connect (priv->store, "row-has-child-toggled",
769 G_CALLBACK (contact_list_view_row_has_child_toggled_cb),
771 gtk_tree_view_set_model (GTK_TREE_VIEW (view),
772 GTK_TREE_MODEL (priv->store));
776 "headers-visible", FALSE,
778 "show-expanders", FALSE,
781 col = gtk_tree_view_column_new ();
784 cell = gtk_cell_renderer_pixbuf_new ();
785 gtk_tree_view_column_pack_start (col, cell, FALSE);
786 gtk_tree_view_column_set_cell_data_func (
788 (GtkTreeCellDataFunc) contact_list_view_pixbuf_cell_data_func,
798 cell = empathy_cell_renderer_text_new ();
799 gtk_tree_view_column_pack_start (col, cell, TRUE);
800 gtk_tree_view_column_set_cell_data_func (
802 (GtkTreeCellDataFunc) contact_list_view_text_cell_data_func,
805 gtk_tree_view_column_add_attribute (col, cell,
806 "name", EMPATHY_CONTACT_LIST_STORE_COL_NAME);
807 gtk_tree_view_column_add_attribute (col, cell,
808 "status", EMPATHY_CONTACT_LIST_STORE_COL_STATUS);
809 gtk_tree_view_column_add_attribute (col, cell,
810 "is_group", EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP);
812 /* Voip Capability Icon */
813 cell = empathy_cell_renderer_activatable_new ();
814 gtk_tree_view_column_pack_start (col, cell, FALSE);
815 gtk_tree_view_column_set_cell_data_func (
817 (GtkTreeCellDataFunc) contact_list_view_voip_cell_data_func,
824 g_signal_connect (cell, "path-activated",
825 G_CALLBACK (contact_list_view_voip_activated_cb),
829 cell = gtk_cell_renderer_pixbuf_new ();
830 gtk_tree_view_column_pack_start (col, cell, FALSE);
831 gtk_tree_view_column_set_cell_data_func (
833 (GtkTreeCellDataFunc) contact_list_view_avatar_cell_data_func,
845 cell = empathy_cell_renderer_expander_new ();
846 gtk_tree_view_column_pack_end (col, cell, FALSE);
847 gtk_tree_view_column_set_cell_data_func (
849 (GtkTreeCellDataFunc) contact_list_view_expander_cell_data_func,
852 /* Actually add the column now we have added all cell renderers */
853 gtk_tree_view_append_column (GTK_TREE_VIEW (view), col);
856 for (i = 0; i < G_N_ELEMENTS (drag_types_dest); ++i) {
857 drag_atoms_dest[i] = gdk_atom_intern (drag_types_dest[i].target,
861 for (i = 0; i < G_N_ELEMENTS (drag_types_source); ++i) {
862 drag_atoms_source[i] = gdk_atom_intern (drag_types_source[i].target,
868 contact_list_view_finalize (GObject *object)
870 EmpathyContactListViewPriv *priv;
872 priv = GET_PRIV (object);
875 g_object_unref (priv->store);
878 G_OBJECT_CLASS (empathy_contact_list_view_parent_class)->finalize (object);
882 contact_list_view_get_property (GObject *object,
887 EmpathyContactListViewPriv *priv;
889 priv = GET_PRIV (object);
893 g_value_set_flags (value, priv->features);
896 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
902 contact_list_view_set_property (GObject *object,
907 EmpathyContactListView *view = EMPATHY_CONTACT_LIST_VIEW (object);
908 EmpathyContactListViewPriv *priv;
910 priv = GET_PRIV (object);
914 empathy_contact_list_view_set_features (view, g_value_get_flags (value));
917 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
923 empathy_contact_list_view_class_init (EmpathyContactListViewClass *klass)
925 GObjectClass *object_class = G_OBJECT_CLASS (klass);
926 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
928 object_class->finalize = contact_list_view_finalize;
929 object_class->get_property = contact_list_view_get_property;
930 object_class->set_property = contact_list_view_set_property;
932 widget_class->drag_data_received = contact_list_view_drag_data_received;
933 widget_class->drag_drop = contact_list_view_drag_drop;
934 widget_class->drag_begin = contact_list_view_drag_begin;
935 widget_class->drag_data_get = contact_list_view_drag_data_get;
936 widget_class->drag_end = contact_list_view_drag_end;
937 /* FIXME: noticed but when you drag the row over the treeview
938 * fast, it seems to stop redrawing itself, if we don't
939 * connect this signal, all is fine.
941 widget_class->drag_motion = contact_list_view_drag_motion;
943 signals[DRAG_CONTACT_RECEIVED] =
944 g_signal_new ("drag-contact-received",
945 G_OBJECT_CLASS_TYPE (klass),
949 _empathy_gtk_marshal_VOID__OBJECT_STRING_STRING,
951 3, EMPATHY_TYPE_CONTACT, G_TYPE_STRING, G_TYPE_STRING);
953 g_object_class_install_property (object_class,
955 g_param_spec_flags ("features",
956 "Features of the view",
957 "Falgs for all enabled features",
958 EMPATHY_TYPE_CONTACT_LIST_FEATURES,
962 g_type_class_add_private (object_class, sizeof (EmpathyContactListViewPriv));
966 empathy_contact_list_view_init (EmpathyContactListView *view)
968 /* Get saved group states. */
969 empathy_contact_groups_get_all ();
971 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (view),
972 empathy_contact_list_store_row_separator_func,
975 /* Connect to tree view signals rather than override. */
976 g_signal_connect (view,
977 "button-press-event",
978 G_CALLBACK (contact_list_view_button_press_event_cb),
980 g_signal_connect (view,
982 G_CALLBACK (contact_list_view_row_activated_cb),
984 g_signal_connect (view,
986 G_CALLBACK (contact_list_view_row_expand_or_collapse_cb),
987 GINT_TO_POINTER (TRUE));
988 g_signal_connect (view,
990 G_CALLBACK (contact_list_view_row_expand_or_collapse_cb),
991 GINT_TO_POINTER (FALSE));
994 EmpathyContactListView *
995 empathy_contact_list_view_new (EmpathyContactListStore *store,
996 EmpathyContactListFeatures features)
998 EmpathyContactListView *view;
999 EmpathyContactListViewPriv *priv;
1001 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), NULL);
1003 view = g_object_new (EMPATHY_TYPE_CONTACT_LIST_VIEW,
1004 "features", features,
1007 priv = GET_PRIV (view);
1008 priv->store = g_object_ref (store);
1009 contact_list_view_setup (EMPATHY_CONTACT_LIST_VIEW (view));
1015 empathy_contact_list_view_set_features (EmpathyContactListView *view,
1016 EmpathyContactListFeatures features)
1018 EmpathyContactListViewPriv *priv = GET_PRIV (view);
1020 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view));
1022 priv->features = features;
1024 /* Update DnD source/dest */
1025 if (features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_DRAG) {
1026 gtk_drag_source_set (GTK_WIDGET (view),
1029 G_N_ELEMENTS (drag_types_source),
1030 GDK_ACTION_MOVE | GDK_ACTION_COPY);
1032 gtk_drag_source_unset (GTK_WIDGET (view));
1036 if (features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_DROP) {
1037 gtk_drag_dest_set (GTK_WIDGET (view),
1038 GTK_DEST_DEFAULT_ALL,
1040 G_N_ELEMENTS (drag_types_dest),
1041 GDK_ACTION_MOVE | GDK_ACTION_COPY);
1043 /* FIXME: URI could still be droped depending on FT feature */
1044 gtk_drag_dest_unset (GTK_WIDGET (view));
1047 g_object_notify (G_OBJECT (view), "features");
1050 EmpathyContactListFeatures
1051 empathy_contact_list_view_get_features (EmpathyContactListView *view)
1053 EmpathyContactListViewPriv *priv = GET_PRIV (view);
1055 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), FALSE);
1057 return priv->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);
1187 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), NULL);
1189 if (!(priv->features & (EMPATHY_CONTACT_LIST_FEATURE_GROUPS_RENAME |
1190 EMPATHY_CONTACT_LIST_FEATURE_GROUPS_REMOVE))) {
1194 menu = gtk_menu_new ();
1196 /* FIXME: Not implemented yet
1197 if (priv->features & EMPATHY_CONTACT_LIST_FEATURE_GROUPS_RENAME) {
1198 item = gtk_menu_item_new_with_mnemonic (_("Re_name"));
1199 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1200 gtk_widget_show (item);
1201 g_signal_connect (item, "activate",
1202 G_CALLBACK (contact_list_view_group_rename_activate_cb),
1206 if (priv->features & EMPATHY_CONTACT_LIST_FEATURE_GROUPS_REMOVE) {
1207 item = gtk_image_menu_item_new_with_mnemonic (_("_Remove"));
1208 image = gtk_image_new_from_icon_name (GTK_STOCK_REMOVE,
1209 GTK_ICON_SIZE_MENU);
1210 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1211 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1212 gtk_widget_show (item);
1213 g_signal_connect (item, "activate",
1214 G_CALLBACK (contact_list_view_group_remove_activate_cb),
1222 contact_list_view_remove_activate_cb (GtkMenuItem *menuitem,
1223 EmpathyContactListView *view)
1225 EmpathyContactListViewPriv *priv = GET_PRIV (view);
1226 EmpathyContact *contact;
1228 contact = empathy_contact_list_view_get_selected (view);
1234 parent = empathy_get_toplevel_window (GTK_WIDGET (view));
1235 text = g_strdup_printf (_("Do you really want to remove the contact '%s' ?"),
1236 empathy_contact_get_name (contact));
1237 if (contact_list_view_remove_dialog_show (parent, _("Removing contact"), text)) {
1238 EmpathyContactList *list;
1240 list = empathy_contact_list_store_get_list_iface (priv->store);
1241 empathy_contact_list_remove (list, contact,
1242 _("Sorry, I don't want you in my contact list anymore."));
1246 g_object_unref (contact);
1251 empathy_contact_list_view_get_contact_menu (EmpathyContactListView *view,
1252 EmpathyContact *contact)
1254 EmpathyContactListViewPriv *priv = GET_PRIV (view);
1256 GtkMenuShell *shell;
1260 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), NULL);
1261 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
1263 if (!(priv->features & (EMPATHY_CONTACT_LIST_FEATURE_CONTACT_CHAT |
1264 EMPATHY_CONTACT_LIST_FEATURE_CONTACT_CALL |
1265 EMPATHY_CONTACT_LIST_FEATURE_CONTACT_LOG |
1266 EMPATHY_CONTACT_LIST_FEATURE_CONTACT_FT |
1267 EMPATHY_CONTACT_LIST_FEATURE_CONTACT_INVITE |
1268 EMPATHY_CONTACT_LIST_FEATURE_CONTACT_EDIT |
1269 EMPATHY_CONTACT_LIST_FEATURE_CONTACT_INFO |
1270 EMPATHY_CONTACT_LIST_FEATURE_CONTACT_REMOVE))) {
1274 menu = gtk_menu_new ();
1275 shell = GTK_MENU_SHELL (menu);
1278 if (priv->features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_CHAT) {
1279 item = empathy_contact_chat_menu_item_new (contact);
1280 gtk_menu_shell_append (shell, item);
1281 gtk_widget_show (item);
1283 if (priv->features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_CALL) {
1284 item = empathy_contact_call_menu_item_new (contact);
1285 gtk_menu_shell_append (shell, item);
1286 gtk_widget_show (item);
1288 if (priv->features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_LOG) {
1289 item = empathy_contact_log_menu_item_new (contact);
1290 gtk_menu_shell_append (shell, item);
1291 gtk_widget_show (item);
1295 if (priv->features & (EMPATHY_CONTACT_LIST_FEATURE_CONTACT_EDIT |
1296 EMPATHY_CONTACT_LIST_FEATURE_CONTACT_INFO)) {
1297 item = gtk_separator_menu_item_new ();
1298 gtk_menu_shell_append (shell, item);
1299 gtk_widget_show (item);
1303 if (priv->features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_EDIT) {
1304 item = empathy_contact_edit_menu_item_new (contact);
1305 gtk_menu_shell_append (shell, item);
1306 gtk_widget_show (item);
1308 if (priv->features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_INFO) {
1309 item = empathy_contact_info_menu_item_new (contact);
1310 gtk_menu_shell_append (shell, item);
1311 gtk_widget_show (item);
1315 if (priv->features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_REMOVE) {
1316 item = gtk_separator_menu_item_new ();
1317 gtk_menu_shell_append (shell, item);
1318 gtk_widget_show (item);
1322 if (priv->features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_REMOVE) {
1323 item = gtk_image_menu_item_new_with_mnemonic (_("_Remove"));
1324 image = gtk_image_new_from_icon_name (GTK_STOCK_REMOVE,
1325 GTK_ICON_SIZE_MENU);
1326 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1327 gtk_menu_shell_append (shell, item);
1328 gtk_widget_show (item);
1329 g_signal_connect (item, "activate",
1330 G_CALLBACK (contact_list_view_remove_activate_cb),