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;
156 /* To be used with gtk_tree_model_foreach, update the status icon
157 * of the contact to show the event icon (on=TRUE) or the presence
159 gtk_tree_model_get (model, iter,
160 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
163 if (contact != data->event->contact) {
165 g_object_unref (contact);
171 icon_name = data->event->icon_name;
173 icon_name = empathy_icon_name_for_contact (contact);
176 gtk_tree_store_set (GTK_TREE_STORE (model), iter,
177 EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, icon_name,
180 /* To make sure the parent is shown correctly, we emit
181 * the row-changed signal on the parent so it prompts
182 * it to be refreshed by the filter func.
184 if (gtk_tree_model_iter_parent (model, &parent_iter, iter)) {
185 parent_path = gtk_tree_model_get_path (model, &parent_iter);
188 gtk_tree_model_row_changed (model, parent_path, &parent_iter);
189 gtk_tree_path_free (parent_path);
192 g_object_unref (contact);
198 contact_list_view_flash_cb (EmpathyContactListView *view)
200 EmpathyContactListViewPriv *priv = GET_PRIV (view);
203 gboolean found_event = FALSE;
204 FlashForeachData data;
206 priv->flash_on = !priv->flash_on;
207 data.on = priv->flash_on;
208 model = GTK_TREE_MODEL (priv->store);
210 events = empathy_event_manager_get_events (priv->event_manager);
211 for (l = events; l; l = l->next) {
212 data.event = l->data;
213 if (!data.event->contact) {
218 gtk_tree_model_foreach (model,
219 contact_list_view_flash_foreach,
224 contact_list_view_flash_stop (view);
231 contact_list_view_flash_start (EmpathyContactListView *view)
233 EmpathyContactListViewPriv *priv = GET_PRIV (view);
235 if (priv->flash_timeout_id != 0) {
239 DEBUG ("Start flashing");
240 priv->flash_timeout_id = g_timeout_add (FLASH_TIMEOUT,
241 (GSourceFunc) contact_list_view_flash_cb,
243 contact_list_view_flash_cb (view);
247 contact_list_view_event_added_cb (EmpathyEventManager *manager,
249 EmpathyContactListView *view)
251 if (event->contact) {
252 contact_list_view_flash_start (view);
257 contact_list_view_event_removed_cb (EmpathyEventManager *manager,
259 EmpathyContactListView *view)
261 EmpathyContactListViewPriv *priv = GET_PRIV (view);
262 FlashForeachData data;
264 if (!event->contact) {
270 gtk_tree_model_foreach (GTK_TREE_MODEL (priv->store),
271 contact_list_view_flash_foreach,
276 contact_list_view_query_tooltip_cb (EmpathyContactListView *view,
279 gboolean keyboard_mode,
283 EmpathyContactListViewPriv *priv = GET_PRIV (view);
284 EmpathyContact *contact;
289 /* FIXME: We need GTK version >= 2.12.10. See GNOME bug #504087 */
290 if (gtk_check_version (2, 12, 10)) {
294 if (!gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (view), &x, &y,
296 &model, &path, &iter)) {
300 gtk_tree_view_set_tooltip_row (GTK_TREE_VIEW (view), tooltip, path);
301 gtk_tree_path_free (path);
303 gtk_tree_model_get (model, &iter,
304 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
310 if (!priv->tooltip_widget) {
311 priv->tooltip_widget = empathy_contact_widget_new (contact,
312 EMPATHY_CONTACT_WIDGET_EDIT_NONE);
313 g_object_add_weak_pointer (G_OBJECT (priv->tooltip_widget),
314 (gpointer) &priv->tooltip_widget);
316 empathy_contact_widget_set_contact (priv->tooltip_widget,
320 gtk_tooltip_set_custom (tooltip, priv->tooltip_widget);
322 g_object_unref (contact);
328 contact_list_view_drag_data_received (GtkWidget *widget,
329 GdkDragContext *context,
332 GtkSelectionData *selection,
336 EmpathyContactListViewPriv *priv;
337 EmpathyContactList *list;
338 EmpathyContactFactory *factory;
342 GtkTreeViewDropPosition position;
343 EmpathyContact *contact = NULL;
346 gchar *new_group = NULL;
347 gchar *old_group = NULL;
350 priv = GET_PRIV (widget);
352 id = (const gchar*) selection->data;
353 DEBUG ("Received %s%s drag & drop contact from roster with id:'%s'",
354 context->action == GDK_ACTION_MOVE ? "move" : "",
355 context->action == GDK_ACTION_COPY ? "copy" : "",
358 strv = g_strsplit (id, "/", 2);
359 factory = empathy_contact_factory_new ();
360 account = mc_account_lookup (strv[0]);
362 contact = empathy_contact_factory_get_from_id (factory,
365 g_object_unref (account);
367 g_object_unref (factory);
371 DEBUG ("No contact found associated with drag & drop");
375 empathy_contact_run_until_ready (contact,
376 EMPATHY_CONTACT_READY_HANDLE,
379 model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
381 /* Get source group information. */
382 if (priv->drag_row) {
383 path = gtk_tree_row_reference_get_path (priv->drag_row);
385 old_group = empathy_contact_list_store_get_parent_group (model, path, NULL);
386 gtk_tree_path_free (path);
390 /* Get destination group information. */
391 is_row = gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget),
398 new_group = empathy_contact_list_store_get_parent_group (model, path, NULL);
399 gtk_tree_path_free (path);
402 DEBUG ("contact %s (%d) dragged from '%s' to '%s'",
403 empathy_contact_get_id (contact),
404 empathy_contact_get_handle (contact),
405 old_group, new_group);
407 list = empathy_contact_list_store_get_list_iface (priv->store);
409 empathy_contact_list_add_to_group (list, contact, new_group);
411 if (old_group && context->action == GDK_ACTION_MOVE) {
412 empathy_contact_list_remove_from_group (list, contact, old_group);
418 gtk_drag_finish (context, TRUE, FALSE, GDK_CURRENT_TIME);
422 contact_list_view_drag_motion_cb (DragMotionData *data)
424 gtk_tree_view_expand_row (GTK_TREE_VIEW (data->view),
428 data->timeout_id = 0;
434 contact_list_view_drag_motion (GtkWidget *widget,
435 GdkDragContext *context,
440 static DragMotionData *dm = NULL;
443 gboolean is_different = FALSE;
444 gboolean cleanup = TRUE;
446 is_row = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
457 cleanup &= (dm && gtk_tree_path_compare (dm->path, path) != 0);
458 is_different = (!dm || (dm && gtk_tree_path_compare (dm->path, path) != 0));
463 if (!is_different && !cleanup) {
468 gtk_tree_path_free (dm->path);
469 if (dm->timeout_id) {
470 g_source_remove (dm->timeout_id);
478 if (!gtk_tree_view_row_expanded (GTK_TREE_VIEW (widget), path)) {
479 dm = g_new0 (DragMotionData, 1);
481 dm->view = EMPATHY_CONTACT_LIST_VIEW (widget);
482 dm->path = gtk_tree_path_copy (path);
484 dm->timeout_id = g_timeout_add_seconds (1,
485 (GSourceFunc) contact_list_view_drag_motion_cb,
493 contact_list_view_drag_begin (GtkWidget *widget,
494 GdkDragContext *context)
496 EmpathyContactListViewPriv *priv;
497 GtkTreeSelection *selection;
502 priv = GET_PRIV (widget);
504 GTK_WIDGET_CLASS (empathy_contact_list_view_parent_class)->drag_begin (widget,
507 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (widget));
508 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
512 path = gtk_tree_model_get_path (model, &iter);
513 priv->drag_row = gtk_tree_row_reference_new (model, path);
514 gtk_tree_path_free (path);
518 contact_list_view_drag_data_get (GtkWidget *widget,
519 GdkDragContext *context,
520 GtkSelectionData *selection,
524 EmpathyContactListViewPriv *priv;
525 GtkTreePath *src_path;
528 EmpathyContact *contact;
530 const gchar *contact_id;
531 const gchar *account_id;
534 priv = GET_PRIV (widget);
536 model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
537 if (!priv->drag_row) {
541 src_path = gtk_tree_row_reference_get_path (priv->drag_row);
546 if (!gtk_tree_model_get_iter (model, &iter, src_path)) {
547 gtk_tree_path_free (src_path);
551 gtk_tree_path_free (src_path);
553 contact = empathy_contact_list_view_get_selected (EMPATHY_CONTACT_LIST_VIEW (widget));
558 account = empathy_contact_get_account (contact);
559 account_id = mc_account_get_unique_name (account);
560 contact_id = empathy_contact_get_id (contact);
561 g_object_unref (contact);
562 str = g_strconcat (account_id, "/", contact_id, NULL);
565 case DND_DRAG_TYPE_CONTACT_ID:
566 gtk_selection_data_set (selection, drag_atoms_source[info], 8,
567 (guchar*)str, strlen (str) + 1);
575 contact_list_view_drag_end (GtkWidget *widget,
576 GdkDragContext *context)
578 EmpathyContactListViewPriv *priv;
580 priv = GET_PRIV (widget);
582 GTK_WIDGET_CLASS (empathy_contact_list_view_parent_class)->drag_end (widget,
585 if (priv->drag_row) {
586 gtk_tree_row_reference_free (priv->drag_row);
587 priv->drag_row = NULL;
592 contact_list_view_drag_drop (GtkWidget *widget,
593 GdkDragContext *drag_context,
602 EmpathyContactListView *view;
608 contact_list_view_popup_menu_idle_cb (gpointer user_data)
610 MenuPopupData *data = user_data;
613 menu = empathy_contact_list_view_get_contact_menu (data->view);
615 menu = empathy_contact_list_view_get_group_menu (data->view);
619 gtk_widget_show (menu);
620 gtk_menu_popup (GTK_MENU (menu),
621 NULL, NULL, NULL, NULL,
622 data->button, data->time);
625 g_slice_free (MenuPopupData, data);
631 contact_list_view_button_press_event_cb (EmpathyContactListView *view,
632 GdkEventButton *event,
635 if (event->button == 3) {
638 data = g_slice_new (MenuPopupData);
640 data->button = event->button;
641 data->time = event->time;
642 g_idle_add (contact_list_view_popup_menu_idle_cb, data);
649 contact_list_view_key_press_event_cb (EmpathyContactListView *view,
653 if (event->keyval == GDK_Menu) {
656 data = g_slice_new (MenuPopupData);
659 data->time = event->time;
660 g_idle_add (contact_list_view_popup_menu_idle_cb, data);
667 contact_list_view_row_activated_cb (EmpathyContactListView *view,
669 GtkTreeViewColumn *col,
672 EmpathyContactListViewPriv *priv = GET_PRIV (view);
673 EmpathyContact *contact;
677 model = GTK_TREE_MODEL (priv->store);
678 gtk_tree_model_get_iter (model, &iter, path);
679 gtk_tree_model_get (model, &iter,
680 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
687 /* If the contact has an event, activate it */
688 if (priv->contact_features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_EVENTS) {
691 events = empathy_event_manager_get_events (priv->event_manager);
692 for (l = events; l; l = l->next) {
693 EmpathyEvent *event = l->data;
695 if (event->contact == contact) {
696 empathy_event_activate (event);
702 /* There is no event for the contact, default action is starting a chat */
703 if (priv->contact_features & EMPATHY_CONTACT_FEATURE_CHAT) {
704 empathy_dispatcher_chat_with_contact (contact);
708 g_object_unref (contact);
712 contact_list_view_voip_activated_cb (EmpathyCellRendererActivatable *cell,
713 const gchar *path_string,
714 EmpathyContactListView *view)
716 EmpathyContactListViewPriv *priv = GET_PRIV (view);
719 EmpathyContact *contact;
721 if (!(priv->contact_features & EMPATHY_CONTACT_FEATURE_CALL)) {
725 model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
726 if (!gtk_tree_model_get_iter_from_string (model, &iter, path_string)) {
730 gtk_tree_model_get (model, &iter,
731 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
735 empathy_dispatcher_call_with_contact (contact);
736 g_object_unref (contact);
741 contact_list_view_cell_set_background (EmpathyContactListView *view,
742 GtkCellRenderer *cell,
749 style = gtk_widget_get_style (GTK_WIDGET (view));
751 if (!is_group && is_active) {
752 color = style->bg[GTK_STATE_SELECTED];
754 /* Here we take the current theme colour and add it to
755 * the colour for white and average the two. This
756 * gives a colour which is inline with the theme but
759 color.red = (color.red + (style->white).red) / 2;
760 color.green = (color.green + (style->white).green) / 2;
761 color.blue = (color.blue + (style->white).blue) / 2;
764 "cell-background-gdk", &color,
768 "cell-background-gdk", NULL,
774 contact_list_view_pixbuf_cell_data_func (GtkTreeViewColumn *tree_column,
775 GtkCellRenderer *cell,
778 EmpathyContactListView *view)
784 gtk_tree_model_get (model, iter,
785 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
786 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, &is_active,
787 EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, &icon_name,
791 "visible", !is_group,
792 "icon-name", icon_name,
797 contact_list_view_cell_set_background (view, cell, is_group, is_active);
801 contact_list_view_voip_cell_data_func (GtkTreeViewColumn *tree_column,
802 GtkCellRenderer *cell,
805 EmpathyContactListView *view)
811 gtk_tree_model_get (model, iter,
812 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
813 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, &is_active,
814 EMPATHY_CONTACT_LIST_STORE_COL_CAN_VOIP, &can_voip,
818 "visible", !is_group && can_voip,
819 "icon-name", EMPATHY_IMAGE_VOIP,
822 contact_list_view_cell_set_background (view, cell, is_group, is_active);
826 contact_list_view_avatar_cell_data_func (GtkTreeViewColumn *tree_column,
827 GtkCellRenderer *cell,
830 EmpathyContactListView *view)
833 gboolean show_avatar;
837 gtk_tree_model_get (model, iter,
838 EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR, &pixbuf,
839 EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR_VISIBLE, &show_avatar,
840 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
841 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, &is_active,
845 "visible", !is_group && show_avatar,
850 g_object_unref (pixbuf);
853 contact_list_view_cell_set_background (view, cell, is_group, is_active);
857 contact_list_view_text_cell_data_func (GtkTreeViewColumn *tree_column,
858 GtkCellRenderer *cell,
861 EmpathyContactListView *view)
865 gboolean show_status;
867 gtk_tree_model_get (model, iter,
868 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
869 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, &is_active,
870 EMPATHY_CONTACT_LIST_STORE_COL_STATUS_VISIBLE, &show_status,
874 "show-status", show_status,
877 contact_list_view_cell_set_background (view, cell, is_group, is_active);
881 contact_list_view_expander_cell_data_func (GtkTreeViewColumn *column,
882 GtkCellRenderer *cell,
885 EmpathyContactListView *view)
890 gtk_tree_model_get (model, iter,
891 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
892 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, &is_active,
895 if (gtk_tree_model_iter_has_child (model, iter)) {
897 gboolean row_expanded;
899 path = gtk_tree_model_get_path (model, iter);
900 row_expanded = gtk_tree_view_row_expanded (GTK_TREE_VIEW (column->tree_view), path);
901 gtk_tree_path_free (path);
905 "expander-style", row_expanded ? GTK_EXPANDER_EXPANDED : GTK_EXPANDER_COLLAPSED,
908 g_object_set (cell, "visible", FALSE, NULL);
911 contact_list_view_cell_set_background (view, cell, is_group, is_active);
915 contact_list_view_row_expand_or_collapse_cb (EmpathyContactListView *view,
920 EmpathyContactListViewPriv *priv = GET_PRIV (view);
925 if (!(priv->list_features & EMPATHY_CONTACT_LIST_FEATURE_GROUPS_SAVE)) {
929 model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
931 gtk_tree_model_get (model, iter,
932 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
935 expanded = GPOINTER_TO_INT (user_data);
936 empathy_contact_group_set_expanded (name, expanded);
942 contact_list_view_row_has_child_toggled_cb (GtkTreeModel *model,
945 EmpathyContactListView *view)
947 EmpathyContactListViewPriv *priv = GET_PRIV (view);
948 gboolean is_group = FALSE;
951 gtk_tree_model_get (model, iter,
952 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
953 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
956 if (!is_group || G_STR_EMPTY (name)) {
961 if (!(priv->list_features & EMPATHY_CONTACT_LIST_FEATURE_GROUPS_SAVE) ||
962 empathy_contact_group_get_expanded (name)) {
963 g_signal_handlers_block_by_func (view,
964 contact_list_view_row_expand_or_collapse_cb,
965 GINT_TO_POINTER (TRUE));
966 gtk_tree_view_expand_row (GTK_TREE_VIEW (view), path, TRUE);
967 g_signal_handlers_unblock_by_func (view,
968 contact_list_view_row_expand_or_collapse_cb,
969 GINT_TO_POINTER (TRUE));
971 g_signal_handlers_block_by_func (view,
972 contact_list_view_row_expand_or_collapse_cb,
973 GINT_TO_POINTER (FALSE));
974 gtk_tree_view_collapse_row (GTK_TREE_VIEW (view), path);
975 g_signal_handlers_unblock_by_func (view,
976 contact_list_view_row_expand_or_collapse_cb,
977 GINT_TO_POINTER (FALSE));
984 contact_list_view_setup (EmpathyContactListView *view)
986 EmpathyContactListViewPriv *priv;
987 GtkCellRenderer *cell;
988 GtkTreeViewColumn *col;
991 priv = GET_PRIV (view);
993 gtk_tree_view_set_search_equal_func (GTK_TREE_VIEW (view),
994 empathy_contact_list_store_search_equal_func,
997 g_signal_connect (priv->store, "row-has-child-toggled",
998 G_CALLBACK (contact_list_view_row_has_child_toggled_cb),
1000 gtk_tree_view_set_model (GTK_TREE_VIEW (view),
1001 GTK_TREE_MODEL (priv->store));
1005 "headers-visible", FALSE,
1006 "reorderable", TRUE,
1007 "show-expanders", FALSE,
1010 col = gtk_tree_view_column_new ();
1013 cell = gtk_cell_renderer_pixbuf_new ();
1014 gtk_tree_view_column_pack_start (col, cell, FALSE);
1015 gtk_tree_view_column_set_cell_data_func (
1017 (GtkTreeCellDataFunc) contact_list_view_pixbuf_cell_data_func,
1027 cell = empathy_cell_renderer_text_new ();
1028 gtk_tree_view_column_pack_start (col, cell, TRUE);
1029 gtk_tree_view_column_set_cell_data_func (
1031 (GtkTreeCellDataFunc) contact_list_view_text_cell_data_func,
1034 gtk_tree_view_column_add_attribute (col, cell,
1035 "name", EMPATHY_CONTACT_LIST_STORE_COL_NAME);
1036 gtk_tree_view_column_add_attribute (col, cell,
1037 "status", EMPATHY_CONTACT_LIST_STORE_COL_STATUS);
1038 gtk_tree_view_column_add_attribute (col, cell,
1039 "is_group", EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP);
1041 /* Voip Capability Icon */
1042 cell = empathy_cell_renderer_activatable_new ();
1043 gtk_tree_view_column_pack_start (col, cell, FALSE);
1044 gtk_tree_view_column_set_cell_data_func (
1046 (GtkTreeCellDataFunc) contact_list_view_voip_cell_data_func,
1053 g_signal_connect (cell, "path-activated",
1054 G_CALLBACK (contact_list_view_voip_activated_cb),
1058 cell = gtk_cell_renderer_pixbuf_new ();
1059 gtk_tree_view_column_pack_start (col, cell, FALSE);
1060 gtk_tree_view_column_set_cell_data_func (
1062 (GtkTreeCellDataFunc) contact_list_view_avatar_cell_data_func,
1074 cell = empathy_cell_renderer_expander_new ();
1075 gtk_tree_view_column_pack_end (col, cell, FALSE);
1076 gtk_tree_view_column_set_cell_data_func (
1078 (GtkTreeCellDataFunc) contact_list_view_expander_cell_data_func,
1081 /* Actually add the column now we have added all cell renderers */
1082 gtk_tree_view_append_column (GTK_TREE_VIEW (view), col);
1085 for (i = 0; i < G_N_ELEMENTS (drag_types_dest); ++i) {
1086 drag_atoms_dest[i] = gdk_atom_intern (drag_types_dest[i].target,
1090 for (i = 0; i < G_N_ELEMENTS (drag_types_source); ++i) {
1091 drag_atoms_source[i] = gdk_atom_intern (drag_types_source[i].target,
1097 contact_list_view_set_list_features (EmpathyContactListView *view,
1098 EmpathyContactListFeatureFlags features)
1100 EmpathyContactListViewPriv *priv = GET_PRIV (view);
1101 gboolean has_tooltip;
1103 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view));
1105 priv->list_features = features;
1107 /* Update DnD source/dest */
1108 if (features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_DRAG) {
1109 gtk_drag_source_set (GTK_WIDGET (view),
1112 G_N_ELEMENTS (drag_types_source),
1113 GDK_ACTION_MOVE | GDK_ACTION_COPY);
1115 gtk_drag_source_unset (GTK_WIDGET (view));
1119 if (features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_DROP) {
1120 gtk_drag_dest_set (GTK_WIDGET (view),
1121 GTK_DEST_DEFAULT_ALL,
1123 G_N_ELEMENTS (drag_types_dest),
1124 GDK_ACTION_MOVE | GDK_ACTION_COPY);
1126 /* FIXME: URI could still be droped depending on FT feature */
1127 gtk_drag_dest_unset (GTK_WIDGET (view));
1130 /* Update has-tooltip */
1131 has_tooltip = (features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_TOOLTIP) != 0;
1132 gtk_widget_set_has_tooltip (GTK_WIDGET (view), has_tooltip);
1134 /* Enable event handling if needed */
1135 if (features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_EVENTS &&
1136 !priv->event_manager) {
1139 priv->event_manager = empathy_event_manager_new ();
1140 g_signal_connect (priv->event_manager, "event-added",
1141 G_CALLBACK (contact_list_view_event_added_cb),
1143 g_signal_connect (priv->event_manager, "event-removed",
1144 G_CALLBACK (contact_list_view_event_removed_cb),
1147 l = empathy_event_manager_get_events (priv->event_manager);
1149 contact_list_view_event_added_cb (priv->event_manager,
1155 /* Disable event handling if needed */
1156 if (!(features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_EVENTS) &&
1157 priv->event_manager) {
1158 g_signal_handlers_disconnect_by_func (priv->event_manager,
1159 contact_list_view_event_added_cb,
1161 g_signal_handlers_disconnect_by_func (priv->event_manager,
1162 contact_list_view_event_removed_cb,
1164 g_object_unref (priv->event_manager);
1165 priv->event_manager = NULL;
1170 contact_list_view_finalize (GObject *object)
1172 EmpathyContactListViewPriv *priv;
1174 priv = GET_PRIV (object);
1177 g_object_unref (priv->store);
1180 if (priv->event_manager) {
1181 g_signal_handlers_disconnect_by_func (priv->event_manager,
1182 contact_list_view_event_added_cb,
1184 g_signal_handlers_disconnect_by_func (priv->event_manager,
1185 contact_list_view_event_removed_cb,
1187 g_object_unref (priv->event_manager);
1190 G_OBJECT_CLASS (empathy_contact_list_view_parent_class)->finalize (object);
1194 contact_list_view_get_property (GObject *object,
1199 EmpathyContactListViewPriv *priv;
1201 priv = GET_PRIV (object);
1205 g_value_set_object (value, priv->store);
1207 case PROP_LIST_FEATURES:
1208 g_value_set_flags (value, priv->list_features);
1210 case PROP_CONTACT_FEATURES:
1211 g_value_set_flags (value, priv->contact_features);
1214 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1220 contact_list_view_set_property (GObject *object,
1222 const GValue *value,
1225 EmpathyContactListView *view = EMPATHY_CONTACT_LIST_VIEW (object);
1226 EmpathyContactListViewPriv *priv = GET_PRIV (object);
1230 priv->store = g_value_dup_object (value);
1231 contact_list_view_setup (view);
1233 case PROP_LIST_FEATURES:
1234 contact_list_view_set_list_features (view, g_value_get_flags (value));
1236 case PROP_CONTACT_FEATURES:
1237 priv->contact_features = g_value_get_flags (value);
1240 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1246 empathy_contact_list_view_class_init (EmpathyContactListViewClass *klass)
1248 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1249 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1251 object_class->finalize = contact_list_view_finalize;
1252 object_class->get_property = contact_list_view_get_property;
1253 object_class->set_property = contact_list_view_set_property;
1255 widget_class->drag_data_received = contact_list_view_drag_data_received;
1256 widget_class->drag_drop = contact_list_view_drag_drop;
1257 widget_class->drag_begin = contact_list_view_drag_begin;
1258 widget_class->drag_data_get = contact_list_view_drag_data_get;
1259 widget_class->drag_end = contact_list_view_drag_end;
1260 /* FIXME: noticed but when you drag the row over the treeview
1261 * fast, it seems to stop redrawing itself, if we don't
1262 * connect this signal, all is fine.
1264 widget_class->drag_motion = contact_list_view_drag_motion;
1266 signals[DRAG_CONTACT_RECEIVED] =
1267 g_signal_new ("drag-contact-received",
1268 G_OBJECT_CLASS_TYPE (klass),
1272 _empathy_gtk_marshal_VOID__OBJECT_STRING_STRING,
1274 3, EMPATHY_TYPE_CONTACT, G_TYPE_STRING, G_TYPE_STRING);
1276 g_object_class_install_property (object_class,
1278 g_param_spec_object ("store",
1279 "The store of the view",
1280 "The store of the view",
1281 EMPATHY_TYPE_CONTACT_LIST_STORE,
1282 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
1283 g_object_class_install_property (object_class,
1285 g_param_spec_flags ("list-features",
1286 "Features of the view",
1287 "Falgs for all enabled features",
1288 EMPATHY_TYPE_CONTACT_LIST_FEATURE_FLAGS,
1289 EMPATHY_CONTACT_LIST_FEATURE_NONE,
1290 G_PARAM_READWRITE));
1291 g_object_class_install_property (object_class,
1292 PROP_CONTACT_FEATURES,
1293 g_param_spec_flags ("contact-features",
1294 "Features of the contact menu",
1295 "Falgs for all enabled features for the menu",
1296 EMPATHY_TYPE_CONTACT_FEATURE_FLAGS,
1297 EMPATHY_CONTACT_FEATURE_NONE,
1298 G_PARAM_READWRITE));
1300 g_type_class_add_private (object_class, sizeof (EmpathyContactListViewPriv));
1304 empathy_contact_list_view_init (EmpathyContactListView *view)
1306 EmpathyContactListViewPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (view,
1307 EMPATHY_TYPE_CONTACT_LIST_VIEW, EmpathyContactListViewPriv);
1310 /* Get saved group states. */
1311 empathy_contact_groups_get_all ();
1313 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (view),
1314 empathy_contact_list_store_row_separator_func,
1317 /* Connect to tree view signals rather than override. */
1318 g_signal_connect (view, "button-press-event",
1319 G_CALLBACK (contact_list_view_button_press_event_cb),
1321 g_signal_connect (view, "key-press-event",
1322 G_CALLBACK (contact_list_view_key_press_event_cb),
1324 g_signal_connect (view, "row-activated",
1325 G_CALLBACK (contact_list_view_row_activated_cb),
1327 g_signal_connect (view, "row-expanded",
1328 G_CALLBACK (contact_list_view_row_expand_or_collapse_cb),
1329 GINT_TO_POINTER (TRUE));
1330 g_signal_connect (view, "row-collapsed",
1331 G_CALLBACK (contact_list_view_row_expand_or_collapse_cb),
1332 GINT_TO_POINTER (FALSE));
1333 g_signal_connect (view, "query-tooltip",
1334 G_CALLBACK (contact_list_view_query_tooltip_cb),
1338 EmpathyContactListView *
1339 empathy_contact_list_view_new (EmpathyContactListStore *store,
1340 EmpathyContactListFeatureFlags list_features,
1341 EmpathyContactFeatureFlags contact_features)
1343 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), NULL);
1345 return g_object_new (EMPATHY_TYPE_CONTACT_LIST_VIEW,
1347 "contact-features", contact_features,
1348 "list-features", list_features,
1353 empathy_contact_list_view_get_selected (EmpathyContactListView *view)
1355 EmpathyContactListViewPriv *priv;
1356 GtkTreeSelection *selection;
1358 GtkTreeModel *model;
1359 EmpathyContact *contact;
1361 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), NULL);
1363 priv = GET_PRIV (view);
1365 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
1366 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
1370 gtk_tree_model_get (model, &iter,
1371 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
1378 empathy_contact_list_view_get_selected_group (EmpathyContactListView *view)
1380 EmpathyContactListViewPriv *priv;
1381 GtkTreeSelection *selection;
1383 GtkTreeModel *model;
1387 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), NULL);
1389 priv = GET_PRIV (view);
1391 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
1392 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
1396 gtk_tree_model_get (model, &iter,
1397 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
1398 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
1410 contact_list_view_remove_dialog_show (GtkWindow *parent,
1411 const gchar *window_title,
1414 GtkWidget *dialog, *label, *image, *hbox;
1417 dialog = gtk_dialog_new_with_buttons (window_title, parent,
1419 GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
1420 GTK_STOCK_DELETE, GTK_RESPONSE_YES,
1422 gtk_dialog_set_has_separator (GTK_DIALOG(dialog), FALSE);
1424 label = gtk_label_new (text);
1425 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_QUESTION, GTK_ICON_SIZE_DIALOG);
1427 hbox = gtk_hbox_new (FALSE, 5);
1428 gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
1429 gtk_box_pack_start_defaults (GTK_BOX (hbox), image);
1430 gtk_box_pack_start_defaults (GTK_BOX (hbox), label);
1431 gtk_box_pack_start_defaults (GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox);
1433 gtk_widget_show (image);
1434 gtk_widget_show (label);
1435 gtk_widget_show (hbox);
1436 gtk_widget_show (dialog);
1438 res = gtk_dialog_run (GTK_DIALOG (dialog));
1439 gtk_widget_destroy (dialog);
1441 return (res == GTK_RESPONSE_YES);
1445 contact_list_view_group_remove_activate_cb (GtkMenuItem *menuitem,
1446 EmpathyContactListView *view)
1448 EmpathyContactListViewPriv *priv = GET_PRIV (view);
1451 group = empathy_contact_list_view_get_selected_group (view);
1456 text = g_strdup_printf (_("Do you really want to remove the group '%s'?"), group);
1457 parent = empathy_get_toplevel_window (GTK_WIDGET (view));
1458 if (contact_list_view_remove_dialog_show (parent, _("Removing group"), text)) {
1459 EmpathyContactList *list;
1461 list = empathy_contact_list_store_get_list_iface (priv->store);
1462 empathy_contact_list_remove_group (list, group);
1472 empathy_contact_list_view_get_group_menu (EmpathyContactListView *view)
1474 EmpathyContactListViewPriv *priv = GET_PRIV (view);
1480 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), NULL);
1482 if (!(priv->list_features & (EMPATHY_CONTACT_LIST_FEATURE_GROUPS_RENAME |
1483 EMPATHY_CONTACT_LIST_FEATURE_GROUPS_REMOVE))) {
1487 group = empathy_contact_list_view_get_selected_group (view);
1492 menu = gtk_menu_new ();
1494 /* FIXME: Not implemented yet
1495 if (priv->features & EMPATHY_CONTACT_LIST_FEATURE_GROUPS_RENAME) {
1496 item = gtk_menu_item_new_with_mnemonic (_("Re_name"));
1497 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1498 gtk_widget_show (item);
1499 g_signal_connect (item, "activate",
1500 G_CALLBACK (contact_list_view_group_rename_activate_cb),
1504 if (priv->list_features & EMPATHY_CONTACT_LIST_FEATURE_GROUPS_REMOVE) {
1505 item = gtk_image_menu_item_new_with_mnemonic (_("_Remove"));
1506 image = gtk_image_new_from_icon_name (GTK_STOCK_REMOVE,
1507 GTK_ICON_SIZE_MENU);
1508 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1509 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1510 gtk_widget_show (item);
1511 g_signal_connect (item, "activate",
1512 G_CALLBACK (contact_list_view_group_remove_activate_cb),
1522 contact_list_view_remove_activate_cb (GtkMenuItem *menuitem,
1523 EmpathyContactListView *view)
1525 EmpathyContactListViewPriv *priv = GET_PRIV (view);
1526 EmpathyContact *contact;
1528 contact = empathy_contact_list_view_get_selected (view);
1534 parent = empathy_get_toplevel_window (GTK_WIDGET (view));
1535 text = g_strdup_printf (_("Do you really want to remove the contact '%s'?"),
1536 empathy_contact_get_name (contact));
1537 if (contact_list_view_remove_dialog_show (parent, _("Removing contact"), text)) {
1538 EmpathyContactList *list;
1540 list = empathy_contact_list_store_get_list_iface (priv->store);
1541 empathy_contact_list_remove (list, contact,
1542 _("Sorry, I don't want you in my contact list anymore."));
1546 g_object_unref (contact);
1551 empathy_contact_list_view_get_contact_menu (EmpathyContactListView *view)
1553 EmpathyContactListViewPriv *priv = GET_PRIV (view);
1554 EmpathyContact *contact;
1559 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), NULL);
1561 contact = empathy_contact_list_view_get_selected (view);
1566 menu = empathy_contact_menu_new (contact, priv->contact_features);
1568 if (!(priv->list_features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_REMOVE)) {
1569 g_object_unref (contact);
1575 item = gtk_separator_menu_item_new ();
1576 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1577 gtk_widget_show (item);
1579 menu = gtk_menu_new ();
1582 /* Remove contact */
1583 if (priv->list_features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_REMOVE) {
1584 item = gtk_image_menu_item_new_with_mnemonic (_("_Remove"));
1585 image = gtk_image_new_from_icon_name (GTK_STOCK_REMOVE,
1586 GTK_ICON_SIZE_MENU);
1587 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1588 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1589 gtk_widget_show (item);
1590 g_signal_connect (item, "activate",
1591 G_CALLBACK (contact_list_view_remove_activate_cb),
1595 g_object_unref (contact);