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-event-manager.h"
50 #include "empathy-ui-utils.h"
51 #include "empathy-gtk-enum-types.h"
52 #include "empathy-gtk-marshal.h"
54 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
55 #include <libempathy/empathy-debug.h>
57 /* Flashing delay for icons (milliseconds). */
58 #define FLASH_TIMEOUT 500
60 /* Active users are those which have recently changed state
61 * (e.g. online, offline or from normal to a busy state).
64 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyContactListView)
66 EmpathyContactListStore *store;
67 GtkTreeRowReference *drag_row;
68 EmpathyContactListFeatureFlags list_features;
69 EmpathyContactFeatureFlags contact_features;
70 GtkWidget *tooltip_widget;
71 EmpathyEventManager *event_manager;
72 guint flash_timeout_id;
74 } EmpathyContactListViewPriv;
77 EmpathyContactListView *view;
83 EmpathyContactListView *view;
84 EmpathyContact *contact;
92 PROP_CONTACT_FEATURES,
96 DND_DRAG_TYPE_CONTACT_ID,
101 static const GtkTargetEntry drag_types_dest[] = {
102 { "text/contact-id", 0, DND_DRAG_TYPE_CONTACT_ID },
103 { "text/uri-list", 0, DND_DRAG_TYPE_URL },
104 { "text/plain", 0, DND_DRAG_TYPE_STRING },
105 { "STRING", 0, DND_DRAG_TYPE_STRING },
108 static const GtkTargetEntry drag_types_source[] = {
109 { "text/contact-id", 0, DND_DRAG_TYPE_CONTACT_ID },
112 static GdkAtom drag_atoms_dest[G_N_ELEMENTS (drag_types_dest)];
113 static GdkAtom drag_atoms_source[G_N_ELEMENTS (drag_types_source)];
116 DRAG_CONTACT_RECEIVED,
120 static guint signals[LAST_SIGNAL];
122 G_DEFINE_TYPE (EmpathyContactListView, empathy_contact_list_view, GTK_TYPE_TREE_VIEW);
125 contact_list_view_flash_stop (EmpathyContactListView *view)
127 EmpathyContactListViewPriv *priv = GET_PRIV (view);
129 if (priv->flash_timeout_id == 0) {
133 DEBUG ("Stop flashing");
134 g_source_remove (priv->flash_timeout_id);
135 priv->flash_timeout_id = 0;
136 priv->flash_on = FALSE;
145 contact_list_view_flash_foreach (GtkTreeModel *model,
150 FlashForeachData *data = (FlashForeachData*) user_data;
151 EmpathyContact *contact;
152 const gchar *icon_name;
153 GtkTreePath *parent_path = NULL;
154 GtkTreeIter parent_iter;
157 gtk_tree_model_get (model, iter,
158 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
161 if (contact != data->event->contact) {
163 g_object_unref (contact);
169 icon_name = data->event->icon_name;
171 icon_name = empathy_icon_name_for_contact (contact);
174 gtk_tree_store_set (GTK_TREE_STORE (model), iter,
175 EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, icon_name,
178 /* To make sure the parent is shown correctly, we emit
179 * the row-changed signal on the parent so it prompts
180 * it to be refreshed by the filter func.
182 if (gtk_tree_model_iter_parent (model, &parent_iter, iter)) {
183 parent_path = gtk_tree_model_get_path (model, &parent_iter);
186 gtk_tree_model_row_changed (model, parent_path, &parent_iter);
187 gtk_tree_path_free (parent_path);
190 g_object_unref (contact);
196 contact_list_view_flash_cb (EmpathyContactListView *view)
198 EmpathyContactListViewPriv *priv = GET_PRIV (view);
201 gboolean found_event = FALSE;
202 FlashForeachData data;
204 priv->flash_on = !priv->flash_on;
205 data.on = priv->flash_on;
206 model = GTK_TREE_MODEL (priv->store);
208 events = empathy_event_manager_get_events (priv->event_manager);
209 for (l = events; l; l = l->next) {
210 data.event = l->data;
211 if (!data.event->contact) {
216 gtk_tree_model_foreach (model,
217 contact_list_view_flash_foreach,
222 contact_list_view_flash_stop (view);
229 contact_list_view_flash_start (EmpathyContactListView *view)
231 EmpathyContactListViewPriv *priv = GET_PRIV (view);
233 if (priv->flash_timeout_id != 0) {
237 DEBUG ("Start flashing");
238 priv->flash_timeout_id = g_timeout_add (FLASH_TIMEOUT,
239 (GSourceFunc) contact_list_view_flash_cb,
244 contact_list_view_event_added_cb (EmpathyEventManager *manager,
246 EmpathyContactListView *view)
248 if (event->contact) {
249 contact_list_view_flash_start (view);
254 contact_list_view_event_removed_cb (EmpathyEventManager *manager,
256 EmpathyContactListView *view)
258 EmpathyContactListViewPriv *priv = GET_PRIV (view);
259 FlashForeachData data;
261 if (!event->contact) {
267 gtk_tree_model_foreach (GTK_TREE_MODEL (priv->store),
268 contact_list_view_flash_foreach,
273 contact_list_view_query_tooltip_cb (EmpathyContactListView *view,
276 gboolean keyboard_mode,
280 EmpathyContactListViewPriv *priv = GET_PRIV (view);
281 EmpathyContact *contact;
286 /* FIXME: We need GTK version >= 2.12.10. See GNOME bug #504087 */
287 if (gtk_check_version (2, 12, 10)) {
291 if (!gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (view), &x, &y,
293 &model, &path, &iter)) {
297 gtk_tree_view_set_tooltip_row (GTK_TREE_VIEW (view), tooltip, path);
298 gtk_tree_path_free (path);
300 gtk_tree_model_get (model, &iter,
301 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
307 if (!priv->tooltip_widget) {
308 priv->tooltip_widget = empathy_contact_widget_new (contact,
309 EMPATHY_CONTACT_WIDGET_EDIT_NONE);
310 g_object_add_weak_pointer (G_OBJECT (priv->tooltip_widget),
311 (gpointer) &priv->tooltip_widget);
313 empathy_contact_widget_set_contact (priv->tooltip_widget,
317 gtk_tooltip_set_custom (tooltip, priv->tooltip_widget);
319 g_object_unref (contact);
325 contact_list_view_drag_data_received (GtkWidget *widget,
326 GdkDragContext *context,
329 GtkSelectionData *selection,
333 EmpathyContactListViewPriv *priv;
334 EmpathyContactList *list;
335 EmpathyContactFactory *factory;
339 GtkTreeViewDropPosition position;
340 EmpathyContact *contact = NULL;
343 gchar *new_group = NULL;
344 gchar *old_group = NULL;
347 priv = GET_PRIV (widget);
349 id = (const gchar*) selection->data;
350 DEBUG ("Received %s%s drag & drop contact from roster with id:'%s'",
351 context->action == GDK_ACTION_MOVE ? "move" : "",
352 context->action == GDK_ACTION_COPY ? "copy" : "",
355 strv = g_strsplit (id, "/", 2);
356 factory = empathy_contact_factory_new ();
357 account = mc_account_lookup (strv[0]);
359 contact = empathy_contact_factory_get_from_id (factory,
362 g_object_unref (account);
364 g_object_unref (factory);
368 DEBUG ("No contact found associated with drag & drop");
372 empathy_contact_run_until_ready (contact,
373 EMPATHY_CONTACT_READY_HANDLE,
376 model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
378 /* Get source group information. */
379 if (priv->drag_row) {
380 path = gtk_tree_row_reference_get_path (priv->drag_row);
382 old_group = empathy_contact_list_store_get_parent_group (model, path, NULL);
383 gtk_tree_path_free (path);
387 /* Get destination group information. */
388 is_row = gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget),
395 new_group = empathy_contact_list_store_get_parent_group (model, path, NULL);
396 gtk_tree_path_free (path);
399 DEBUG ("contact %s (%d) dragged from '%s' to '%s'",
400 empathy_contact_get_id (contact),
401 empathy_contact_get_handle (contact),
402 old_group, new_group);
404 list = empathy_contact_list_store_get_list_iface (priv->store);
406 empathy_contact_list_add_to_group (list, contact, new_group);
408 if (old_group && context->action == GDK_ACTION_MOVE) {
409 empathy_contact_list_remove_from_group (list, contact, old_group);
415 gtk_drag_finish (context, TRUE, FALSE, GDK_CURRENT_TIME);
419 contact_list_view_drag_motion_cb (DragMotionData *data)
421 gtk_tree_view_expand_row (GTK_TREE_VIEW (data->view),
425 data->timeout_id = 0;
431 contact_list_view_drag_motion (GtkWidget *widget,
432 GdkDragContext *context,
437 static DragMotionData *dm = NULL;
440 gboolean is_different = FALSE;
441 gboolean cleanup = TRUE;
443 is_row = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
454 cleanup &= (dm && gtk_tree_path_compare (dm->path, path) != 0);
455 is_different = (!dm || (dm && gtk_tree_path_compare (dm->path, path) != 0));
460 if (!is_different && !cleanup) {
465 gtk_tree_path_free (dm->path);
466 if (dm->timeout_id) {
467 g_source_remove (dm->timeout_id);
475 if (!gtk_tree_view_row_expanded (GTK_TREE_VIEW (widget), path)) {
476 dm = g_new0 (DragMotionData, 1);
478 dm->view = EMPATHY_CONTACT_LIST_VIEW (widget);
479 dm->path = gtk_tree_path_copy (path);
481 dm->timeout_id = g_timeout_add_seconds (1,
482 (GSourceFunc) contact_list_view_drag_motion_cb,
490 contact_list_view_drag_begin (GtkWidget *widget,
491 GdkDragContext *context)
493 EmpathyContactListViewPriv *priv;
494 GtkTreeSelection *selection;
499 priv = GET_PRIV (widget);
501 GTK_WIDGET_CLASS (empathy_contact_list_view_parent_class)->drag_begin (widget,
504 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (widget));
505 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
509 path = gtk_tree_model_get_path (model, &iter);
510 priv->drag_row = gtk_tree_row_reference_new (model, path);
511 gtk_tree_path_free (path);
515 contact_list_view_drag_data_get (GtkWidget *widget,
516 GdkDragContext *context,
517 GtkSelectionData *selection,
521 EmpathyContactListViewPriv *priv;
522 GtkTreePath *src_path;
525 EmpathyContact *contact;
527 const gchar *contact_id;
528 const gchar *account_id;
531 priv = GET_PRIV (widget);
533 model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
534 if (!priv->drag_row) {
538 src_path = gtk_tree_row_reference_get_path (priv->drag_row);
543 if (!gtk_tree_model_get_iter (model, &iter, src_path)) {
544 gtk_tree_path_free (src_path);
548 gtk_tree_path_free (src_path);
550 contact = empathy_contact_list_view_get_selected (EMPATHY_CONTACT_LIST_VIEW (widget));
555 account = empathy_contact_get_account (contact);
556 account_id = mc_account_get_unique_name (account);
557 contact_id = empathy_contact_get_id (contact);
558 g_object_unref (contact);
559 str = g_strconcat (account_id, "/", contact_id, NULL);
562 case DND_DRAG_TYPE_CONTACT_ID:
563 gtk_selection_data_set (selection, drag_atoms_source[info], 8,
564 (guchar*)str, strlen (str) + 1);
572 contact_list_view_drag_end (GtkWidget *widget,
573 GdkDragContext *context)
575 EmpathyContactListViewPriv *priv;
577 priv = GET_PRIV (widget);
579 GTK_WIDGET_CLASS (empathy_contact_list_view_parent_class)->drag_end (widget,
582 if (priv->drag_row) {
583 gtk_tree_row_reference_free (priv->drag_row);
584 priv->drag_row = NULL;
589 contact_list_view_drag_drop (GtkWidget *widget,
590 GdkDragContext *drag_context,
599 EmpathyContactListView *view;
605 contact_list_view_popup_menu_idle_cb (gpointer user_data)
607 MenuPopupData *data = user_data;
610 menu = empathy_contact_list_view_get_contact_menu (data->view);
612 menu = empathy_contact_list_view_get_group_menu (data->view);
616 gtk_widget_show (menu);
617 gtk_menu_popup (GTK_MENU (menu),
618 NULL, NULL, NULL, NULL,
619 data->button, data->time);
622 g_slice_free (MenuPopupData, data);
628 contact_list_view_button_press_event_cb (EmpathyContactListView *view,
629 GdkEventButton *event,
632 if (event->button == 3) {
635 data = g_slice_new (MenuPopupData);
637 data->button = event->button;
638 data->time = event->time;
639 g_idle_add (contact_list_view_popup_menu_idle_cb, data);
646 contact_list_view_key_press_event_cb (EmpathyContactListView *view,
650 if (event->keyval == GDK_Menu) {
653 data = g_slice_new (MenuPopupData);
656 data->time = event->time;
657 g_idle_add (contact_list_view_popup_menu_idle_cb, data);
664 contact_list_view_row_activated_cb (EmpathyContactListView *view,
666 GtkTreeViewColumn *col,
669 EmpathyContactListViewPriv *priv = GET_PRIV (view);
670 EmpathyContact *contact;
674 model = GTK_TREE_MODEL (priv->store);
675 gtk_tree_model_get_iter (model, &iter, path);
676 gtk_tree_model_get (model, &iter,
677 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
684 if (priv->event_manager) {
687 events = empathy_event_manager_get_events (priv->event_manager);
688 for (l = events; l; l = l->next) {
689 EmpathyEvent *event = l->data;
691 if (event->contact == contact) {
692 empathy_event_activate (event);
698 if (priv->contact_features & EMPATHY_CONTACT_FEATURE_CHAT) {
699 empathy_dispatcher_chat_with_contact (contact);
703 g_object_unref (contact);
707 contact_list_view_voip_activated_cb (EmpathyCellRendererActivatable *cell,
708 const gchar *path_string,
709 EmpathyContactListView *view)
711 EmpathyContactListViewPriv *priv = GET_PRIV (view);
714 EmpathyContact *contact;
716 if (!(priv->contact_features & EMPATHY_CONTACT_FEATURE_CALL)) {
720 model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
721 if (!gtk_tree_model_get_iter_from_string (model, &iter, path_string)) {
725 gtk_tree_model_get (model, &iter,
726 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
730 empathy_dispatcher_call_with_contact (contact);
731 g_object_unref (contact);
736 contact_list_view_cell_set_background (EmpathyContactListView *view,
737 GtkCellRenderer *cell,
744 style = gtk_widget_get_style (GTK_WIDGET (view));
746 if (!is_group && is_active) {
747 color = style->bg[GTK_STATE_SELECTED];
749 /* Here we take the current theme colour and add it to
750 * the colour for white and average the two. This
751 * gives a colour which is inline with the theme but
754 color.red = (color.red + (style->white).red) / 2;
755 color.green = (color.green + (style->white).green) / 2;
756 color.blue = (color.blue + (style->white).blue) / 2;
759 "cell-background-gdk", &color,
763 "cell-background-gdk", NULL,
769 contact_list_view_pixbuf_cell_data_func (GtkTreeViewColumn *tree_column,
770 GtkCellRenderer *cell,
773 EmpathyContactListView *view)
779 gtk_tree_model_get (model, iter,
780 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
781 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, &is_active,
782 EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, &icon_name,
786 "visible", !is_group,
787 "icon-name", icon_name,
792 contact_list_view_cell_set_background (view, cell, is_group, is_active);
796 contact_list_view_voip_cell_data_func (GtkTreeViewColumn *tree_column,
797 GtkCellRenderer *cell,
800 EmpathyContactListView *view)
806 gtk_tree_model_get (model, iter,
807 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
808 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, &is_active,
809 EMPATHY_CONTACT_LIST_STORE_COL_CAN_VOIP, &can_voip,
813 "visible", !is_group && can_voip,
814 "icon-name", EMPATHY_IMAGE_VOIP,
817 contact_list_view_cell_set_background (view, cell, is_group, is_active);
821 contact_list_view_avatar_cell_data_func (GtkTreeViewColumn *tree_column,
822 GtkCellRenderer *cell,
825 EmpathyContactListView *view)
828 gboolean show_avatar;
832 gtk_tree_model_get (model, iter,
833 EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR, &pixbuf,
834 EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR_VISIBLE, &show_avatar,
835 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
836 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, &is_active,
840 "visible", !is_group && show_avatar,
845 g_object_unref (pixbuf);
848 contact_list_view_cell_set_background (view, cell, is_group, is_active);
852 contact_list_view_text_cell_data_func (GtkTreeViewColumn *tree_column,
853 GtkCellRenderer *cell,
856 EmpathyContactListView *view)
860 gboolean show_status;
862 gtk_tree_model_get (model, iter,
863 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
864 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, &is_active,
865 EMPATHY_CONTACT_LIST_STORE_COL_STATUS_VISIBLE, &show_status,
869 "show-status", show_status,
872 contact_list_view_cell_set_background (view, cell, is_group, is_active);
876 contact_list_view_expander_cell_data_func (GtkTreeViewColumn *column,
877 GtkCellRenderer *cell,
880 EmpathyContactListView *view)
885 gtk_tree_model_get (model, iter,
886 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
887 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, &is_active,
890 if (gtk_tree_model_iter_has_child (model, iter)) {
892 gboolean row_expanded;
894 path = gtk_tree_model_get_path (model, iter);
895 row_expanded = gtk_tree_view_row_expanded (GTK_TREE_VIEW (column->tree_view), path);
896 gtk_tree_path_free (path);
900 "expander-style", row_expanded ? GTK_EXPANDER_EXPANDED : GTK_EXPANDER_COLLAPSED,
903 g_object_set (cell, "visible", FALSE, NULL);
906 contact_list_view_cell_set_background (view, cell, is_group, is_active);
910 contact_list_view_row_expand_or_collapse_cb (EmpathyContactListView *view,
915 EmpathyContactListViewPriv *priv = GET_PRIV (view);
920 if (!(priv->list_features & EMPATHY_CONTACT_LIST_FEATURE_GROUPS_SAVE)) {
924 model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
926 gtk_tree_model_get (model, iter,
927 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
930 expanded = GPOINTER_TO_INT (user_data);
931 empathy_contact_group_set_expanded (name, expanded);
937 contact_list_view_row_has_child_toggled_cb (GtkTreeModel *model,
940 EmpathyContactListView *view)
942 EmpathyContactListViewPriv *priv = GET_PRIV (view);
943 gboolean is_group = FALSE;
946 gtk_tree_model_get (model, iter,
947 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
948 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
951 if (!is_group || G_STR_EMPTY (name)) {
956 if (!(priv->list_features & EMPATHY_CONTACT_LIST_FEATURE_GROUPS_SAVE) ||
957 empathy_contact_group_get_expanded (name)) {
958 g_signal_handlers_block_by_func (view,
959 contact_list_view_row_expand_or_collapse_cb,
960 GINT_TO_POINTER (TRUE));
961 gtk_tree_view_expand_row (GTK_TREE_VIEW (view), path, TRUE);
962 g_signal_handlers_unblock_by_func (view,
963 contact_list_view_row_expand_or_collapse_cb,
964 GINT_TO_POINTER (TRUE));
966 g_signal_handlers_block_by_func (view,
967 contact_list_view_row_expand_or_collapse_cb,
968 GINT_TO_POINTER (FALSE));
969 gtk_tree_view_collapse_row (GTK_TREE_VIEW (view), path);
970 g_signal_handlers_unblock_by_func (view,
971 contact_list_view_row_expand_or_collapse_cb,
972 GINT_TO_POINTER (FALSE));
979 contact_list_view_setup (EmpathyContactListView *view)
981 EmpathyContactListViewPriv *priv;
982 GtkCellRenderer *cell;
983 GtkTreeViewColumn *col;
986 priv = GET_PRIV (view);
988 gtk_tree_view_set_search_equal_func (GTK_TREE_VIEW (view),
989 empathy_contact_list_store_search_equal_func,
992 g_signal_connect (priv->store, "row-has-child-toggled",
993 G_CALLBACK (contact_list_view_row_has_child_toggled_cb),
995 gtk_tree_view_set_model (GTK_TREE_VIEW (view),
996 GTK_TREE_MODEL (priv->store));
1000 "headers-visible", FALSE,
1001 "reorderable", TRUE,
1002 "show-expanders", FALSE,
1005 col = gtk_tree_view_column_new ();
1008 cell = gtk_cell_renderer_pixbuf_new ();
1009 gtk_tree_view_column_pack_start (col, cell, FALSE);
1010 gtk_tree_view_column_set_cell_data_func (
1012 (GtkTreeCellDataFunc) contact_list_view_pixbuf_cell_data_func,
1022 cell = empathy_cell_renderer_text_new ();
1023 gtk_tree_view_column_pack_start (col, cell, TRUE);
1024 gtk_tree_view_column_set_cell_data_func (
1026 (GtkTreeCellDataFunc) contact_list_view_text_cell_data_func,
1029 gtk_tree_view_column_add_attribute (col, cell,
1030 "name", EMPATHY_CONTACT_LIST_STORE_COL_NAME);
1031 gtk_tree_view_column_add_attribute (col, cell,
1032 "status", EMPATHY_CONTACT_LIST_STORE_COL_STATUS);
1033 gtk_tree_view_column_add_attribute (col, cell,
1034 "is_group", EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP);
1036 /* Voip Capability Icon */
1037 cell = empathy_cell_renderer_activatable_new ();
1038 gtk_tree_view_column_pack_start (col, cell, FALSE);
1039 gtk_tree_view_column_set_cell_data_func (
1041 (GtkTreeCellDataFunc) contact_list_view_voip_cell_data_func,
1048 g_signal_connect (cell, "path-activated",
1049 G_CALLBACK (contact_list_view_voip_activated_cb),
1053 cell = gtk_cell_renderer_pixbuf_new ();
1054 gtk_tree_view_column_pack_start (col, cell, FALSE);
1055 gtk_tree_view_column_set_cell_data_func (
1057 (GtkTreeCellDataFunc) contact_list_view_avatar_cell_data_func,
1069 cell = empathy_cell_renderer_expander_new ();
1070 gtk_tree_view_column_pack_end (col, cell, FALSE);
1071 gtk_tree_view_column_set_cell_data_func (
1073 (GtkTreeCellDataFunc) contact_list_view_expander_cell_data_func,
1076 /* Actually add the column now we have added all cell renderers */
1077 gtk_tree_view_append_column (GTK_TREE_VIEW (view), col);
1080 for (i = 0; i < G_N_ELEMENTS (drag_types_dest); ++i) {
1081 drag_atoms_dest[i] = gdk_atom_intern (drag_types_dest[i].target,
1085 for (i = 0; i < G_N_ELEMENTS (drag_types_source); ++i) {
1086 drag_atoms_source[i] = gdk_atom_intern (drag_types_source[i].target,
1092 contact_list_view_set_list_features (EmpathyContactListView *view,
1093 EmpathyContactListFeatureFlags features)
1095 EmpathyContactListViewPriv *priv = GET_PRIV (view);
1096 gboolean has_tooltip;
1098 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view));
1100 priv->list_features = features;
1102 /* Update DnD source/dest */
1103 if (features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_DRAG) {
1104 gtk_drag_source_set (GTK_WIDGET (view),
1107 G_N_ELEMENTS (drag_types_source),
1108 GDK_ACTION_MOVE | GDK_ACTION_COPY);
1110 gtk_drag_source_unset (GTK_WIDGET (view));
1114 if (features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_DROP) {
1115 gtk_drag_dest_set (GTK_WIDGET (view),
1116 GTK_DEST_DEFAULT_ALL,
1118 G_N_ELEMENTS (drag_types_dest),
1119 GDK_ACTION_MOVE | GDK_ACTION_COPY);
1121 /* FIXME: URI could still be droped depending on FT feature */
1122 gtk_drag_dest_unset (GTK_WIDGET (view));
1125 /* Update has-tooltip */
1126 has_tooltip = (features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_TOOLTIP) != 0;
1127 gtk_widget_set_has_tooltip (GTK_WIDGET (view), has_tooltip);
1129 /* Enable event handling if needed */
1130 if (features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_EVENTS &&
1131 !priv->event_manager) {
1134 priv->event_manager = empathy_event_manager_new ();
1135 g_signal_connect (priv->event_manager, "event-added",
1136 G_CALLBACK (contact_list_view_event_added_cb),
1138 g_signal_connect (priv->event_manager, "event-removed",
1139 G_CALLBACK (contact_list_view_event_removed_cb),
1142 l = empathy_event_manager_get_events (priv->event_manager);
1144 contact_list_view_event_added_cb (priv->event_manager,
1150 /* Disable event handling if needed */
1151 if (!(features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_EVENTS) &&
1152 priv->event_manager) {
1153 g_signal_handlers_disconnect_by_func (priv->event_manager,
1154 contact_list_view_event_added_cb,
1156 g_signal_handlers_disconnect_by_func (priv->event_manager,
1157 contact_list_view_event_removed_cb,
1159 g_object_unref (priv->event_manager);
1160 priv->event_manager = NULL;
1165 contact_list_view_finalize (GObject *object)
1167 EmpathyContactListViewPriv *priv;
1169 priv = GET_PRIV (object);
1172 g_object_unref (priv->store);
1175 if (priv->event_manager) {
1176 g_signal_handlers_disconnect_by_func (priv->event_manager,
1177 contact_list_view_event_added_cb,
1179 g_signal_handlers_disconnect_by_func (priv->event_manager,
1180 contact_list_view_event_removed_cb,
1182 g_object_unref (priv->event_manager);
1185 G_OBJECT_CLASS (empathy_contact_list_view_parent_class)->finalize (object);
1189 contact_list_view_get_property (GObject *object,
1194 EmpathyContactListViewPriv *priv;
1196 priv = GET_PRIV (object);
1200 g_value_set_object (value, priv->store);
1202 case PROP_LIST_FEATURES:
1203 g_value_set_flags (value, priv->list_features);
1205 case PROP_CONTACT_FEATURES:
1206 g_value_set_flags (value, priv->contact_features);
1209 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1215 contact_list_view_set_property (GObject *object,
1217 const GValue *value,
1220 EmpathyContactListView *view = EMPATHY_CONTACT_LIST_VIEW (object);
1221 EmpathyContactListViewPriv *priv = GET_PRIV (object);
1225 priv->store = g_value_dup_object (value);
1226 contact_list_view_setup (view);
1228 case PROP_LIST_FEATURES:
1229 contact_list_view_set_list_features (view, g_value_get_flags (value));
1231 case PROP_CONTACT_FEATURES:
1232 priv->contact_features = g_value_get_flags (value);
1235 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1241 empathy_contact_list_view_class_init (EmpathyContactListViewClass *klass)
1243 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1244 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1246 object_class->finalize = contact_list_view_finalize;
1247 object_class->get_property = contact_list_view_get_property;
1248 object_class->set_property = contact_list_view_set_property;
1250 widget_class->drag_data_received = contact_list_view_drag_data_received;
1251 widget_class->drag_drop = contact_list_view_drag_drop;
1252 widget_class->drag_begin = contact_list_view_drag_begin;
1253 widget_class->drag_data_get = contact_list_view_drag_data_get;
1254 widget_class->drag_end = contact_list_view_drag_end;
1255 /* FIXME: noticed but when you drag the row over the treeview
1256 * fast, it seems to stop redrawing itself, if we don't
1257 * connect this signal, all is fine.
1259 widget_class->drag_motion = contact_list_view_drag_motion;
1261 signals[DRAG_CONTACT_RECEIVED] =
1262 g_signal_new ("drag-contact-received",
1263 G_OBJECT_CLASS_TYPE (klass),
1267 _empathy_gtk_marshal_VOID__OBJECT_STRING_STRING,
1269 3, EMPATHY_TYPE_CONTACT, G_TYPE_STRING, G_TYPE_STRING);
1271 g_object_class_install_property (object_class,
1273 g_param_spec_object ("store",
1274 "The store of the view",
1275 "The store of the view",
1276 EMPATHY_TYPE_CONTACT_LIST_STORE,
1277 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
1278 g_object_class_install_property (object_class,
1280 g_param_spec_flags ("list-features",
1281 "Features of the view",
1282 "Falgs for all enabled features",
1283 EMPATHY_TYPE_CONTACT_LIST_FEATURE_FLAGS,
1284 EMPATHY_CONTACT_LIST_FEATURE_NONE,
1285 G_PARAM_READWRITE));
1286 g_object_class_install_property (object_class,
1287 PROP_CONTACT_FEATURES,
1288 g_param_spec_flags ("contact-features",
1289 "Features of the contact menu",
1290 "Falgs for all enabled features for the menu",
1291 EMPATHY_TYPE_CONTACT_FEATURE_FLAGS,
1292 EMPATHY_CONTACT_FEATURE_NONE,
1293 G_PARAM_READWRITE));
1295 g_type_class_add_private (object_class, sizeof (EmpathyContactListViewPriv));
1299 empathy_contact_list_view_init (EmpathyContactListView *view)
1301 EmpathyContactListViewPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (view,
1302 EMPATHY_TYPE_CONTACT_LIST_VIEW, EmpathyContactListViewPriv);
1305 /* Get saved group states. */
1306 empathy_contact_groups_get_all ();
1308 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (view),
1309 empathy_contact_list_store_row_separator_func,
1312 /* Connect to tree view signals rather than override. */
1313 g_signal_connect (view, "button-press-event",
1314 G_CALLBACK (contact_list_view_button_press_event_cb),
1316 g_signal_connect (view, "key-press-event",
1317 G_CALLBACK (contact_list_view_key_press_event_cb),
1319 g_signal_connect (view, "row-activated",
1320 G_CALLBACK (contact_list_view_row_activated_cb),
1322 g_signal_connect (view, "row-expanded",
1323 G_CALLBACK (contact_list_view_row_expand_or_collapse_cb),
1324 GINT_TO_POINTER (TRUE));
1325 g_signal_connect (view, "row-collapsed",
1326 G_CALLBACK (contact_list_view_row_expand_or_collapse_cb),
1327 GINT_TO_POINTER (FALSE));
1328 g_signal_connect (view, "query-tooltip",
1329 G_CALLBACK (contact_list_view_query_tooltip_cb),
1333 EmpathyContactListView *
1334 empathy_contact_list_view_new (EmpathyContactListStore *store,
1335 EmpathyContactListFeatureFlags list_features,
1336 EmpathyContactFeatureFlags contact_features)
1338 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), NULL);
1340 return g_object_new (EMPATHY_TYPE_CONTACT_LIST_VIEW,
1342 "contact-features", contact_features,
1343 "list-features", list_features,
1348 empathy_contact_list_view_get_selected (EmpathyContactListView *view)
1350 EmpathyContactListViewPriv *priv;
1351 GtkTreeSelection *selection;
1353 GtkTreeModel *model;
1354 EmpathyContact *contact;
1356 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), NULL);
1358 priv = GET_PRIV (view);
1360 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
1361 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
1365 gtk_tree_model_get (model, &iter,
1366 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
1373 empathy_contact_list_view_get_selected_group (EmpathyContactListView *view)
1375 EmpathyContactListViewPriv *priv;
1376 GtkTreeSelection *selection;
1378 GtkTreeModel *model;
1382 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), NULL);
1384 priv = GET_PRIV (view);
1386 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
1387 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
1391 gtk_tree_model_get (model, &iter,
1392 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
1393 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
1405 contact_list_view_remove_dialog_show (GtkWindow *parent,
1406 const gchar *window_title,
1409 GtkWidget *dialog, *label, *image, *hbox;
1412 dialog = gtk_dialog_new_with_buttons (window_title, parent,
1414 GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
1415 GTK_STOCK_DELETE, GTK_RESPONSE_YES,
1417 gtk_dialog_set_has_separator (GTK_DIALOG(dialog), FALSE);
1419 label = gtk_label_new (text);
1420 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_QUESTION, GTK_ICON_SIZE_DIALOG);
1422 hbox = gtk_hbox_new (FALSE, 5);
1423 gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
1424 gtk_box_pack_start_defaults (GTK_BOX (hbox), image);
1425 gtk_box_pack_start_defaults (GTK_BOX (hbox), label);
1426 gtk_box_pack_start_defaults (GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox);
1428 gtk_widget_show (image);
1429 gtk_widget_show (label);
1430 gtk_widget_show (hbox);
1431 gtk_widget_show (dialog);
1433 res = gtk_dialog_run (GTK_DIALOG (dialog));
1434 gtk_widget_destroy (dialog);
1436 return (res == GTK_RESPONSE_YES);
1440 contact_list_view_group_remove_activate_cb (GtkMenuItem *menuitem,
1441 EmpathyContactListView *view)
1443 EmpathyContactListViewPriv *priv = GET_PRIV (view);
1446 group = empathy_contact_list_view_get_selected_group (view);
1451 text = g_strdup_printf (_("Do you really want to remove the group '%s'?"), group);
1452 parent = empathy_get_toplevel_window (GTK_WIDGET (view));
1453 if (contact_list_view_remove_dialog_show (parent, _("Removing group"), text)) {
1454 EmpathyContactList *list;
1456 list = empathy_contact_list_store_get_list_iface (priv->store);
1457 empathy_contact_list_remove_group (list, group);
1467 empathy_contact_list_view_get_group_menu (EmpathyContactListView *view)
1469 EmpathyContactListViewPriv *priv = GET_PRIV (view);
1475 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), NULL);
1477 if (!(priv->list_features & (EMPATHY_CONTACT_LIST_FEATURE_GROUPS_RENAME |
1478 EMPATHY_CONTACT_LIST_FEATURE_GROUPS_REMOVE))) {
1482 group = empathy_contact_list_view_get_selected_group (view);
1487 menu = gtk_menu_new ();
1489 /* FIXME: Not implemented yet
1490 if (priv->features & EMPATHY_CONTACT_LIST_FEATURE_GROUPS_RENAME) {
1491 item = gtk_menu_item_new_with_mnemonic (_("Re_name"));
1492 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1493 gtk_widget_show (item);
1494 g_signal_connect (item, "activate",
1495 G_CALLBACK (contact_list_view_group_rename_activate_cb),
1499 if (priv->list_features & EMPATHY_CONTACT_LIST_FEATURE_GROUPS_REMOVE) {
1500 item = gtk_image_menu_item_new_with_mnemonic (_("_Remove"));
1501 image = gtk_image_new_from_icon_name (GTK_STOCK_REMOVE,
1502 GTK_ICON_SIZE_MENU);
1503 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1504 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1505 gtk_widget_show (item);
1506 g_signal_connect (item, "activate",
1507 G_CALLBACK (contact_list_view_group_remove_activate_cb),
1517 contact_list_view_remove_activate_cb (GtkMenuItem *menuitem,
1518 EmpathyContactListView *view)
1520 EmpathyContactListViewPriv *priv = GET_PRIV (view);
1521 EmpathyContact *contact;
1523 contact = empathy_contact_list_view_get_selected (view);
1529 parent = empathy_get_toplevel_window (GTK_WIDGET (view));
1530 text = g_strdup_printf (_("Do you really want to remove the contact '%s'?"),
1531 empathy_contact_get_name (contact));
1532 if (contact_list_view_remove_dialog_show (parent, _("Removing contact"), text)) {
1533 EmpathyContactList *list;
1535 list = empathy_contact_list_store_get_list_iface (priv->store);
1536 empathy_contact_list_remove (list, contact,
1537 _("Sorry, I don't want you in my contact list anymore."));
1541 g_object_unref (contact);
1546 empathy_contact_list_view_get_contact_menu (EmpathyContactListView *view)
1548 EmpathyContactListViewPriv *priv = GET_PRIV (view);
1549 EmpathyContact *contact;
1554 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), NULL);
1556 contact = empathy_contact_list_view_get_selected (view);
1561 menu = empathy_contact_menu_new (contact, priv->contact_features);
1563 if (!(priv->list_features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_REMOVE)) {
1564 g_object_unref (contact);
1570 item = gtk_separator_menu_item_new ();
1571 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1572 gtk_widget_show (item);
1574 menu = gtk_menu_new ();
1577 /* Remove contact */
1578 if (priv->list_features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_REMOVE) {
1579 item = gtk_image_menu_item_new_with_mnemonic (_("_Remove"));
1580 image = gtk_image_new_from_icon_name (GTK_STOCK_REMOVE,
1581 GTK_ICON_SIZE_MENU);
1582 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1583 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1584 gtk_widget_show (item);
1585 g_signal_connect (item, "activate",
1586 G_CALLBACK (contact_list_view_remove_activate_cb),
1590 g_object_unref (contact);