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 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>
35 #include <libmissioncontrol/mission-control.h>
37 #include <libempathy/empathy-contact-manager.h>
38 #include <libempathy/empathy-contact-list.h>
39 #include <libempathy/empathy-log-manager.h>
40 #include <libempathy/empathy-debug.h>
41 #include <libempathy/empathy-utils.h>
42 #include <libempathy/empathy-marshal.h>
44 #include "empathy-contact-list-view.h"
45 #include "empathy-contact-list-store.h"
46 #include "empathy-images.h"
47 #include "empathy-contact-groups.h"
48 #include "empathy-cell-renderer-expander.h"
49 #include "empathy-cell-renderer-text.h"
50 #include "empathy-ui-utils.h"
51 #include "empathy-contact-dialogs.h"
52 //#include "empathy-chat-invite.h"
53 //#include "empathy-ft-window.h"
54 #include "empathy-log-window.h"
56 #define DEBUG_DOMAIN "ContactListView"
58 /* Flashing delay for icons (milliseconds). */
59 #define FLASH_TIMEOUT 500
61 /* Active users are those which have recently changed state
62 * (e.g. online, offline or from normal to a busy state).
65 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_CONTACT_LIST_VIEW, EmpathyContactListViewPriv))
67 struct _EmpathyContactListViewPriv {
68 EmpathyContactListStore *store;
70 GtkTreeRowReference *drag_row;
74 EmpathyContactListViewDragReceivedFunc drag_received;
75 gpointer drag_received_data;
79 EmpathyContactListView *view;
85 EmpathyContactListView *view;
86 EmpathyContact *contact;
90 static void empathy_contact_list_view_class_init (EmpathyContactListViewClass *klass);
91 static void empathy_contact_list_view_init (EmpathyContactListView *list);
92 static void contact_list_view_finalize (GObject *object);
93 static void contact_list_view_get_property (GObject *object,
97 static void contact_list_view_set_property (GObject *object,
101 static void contact_list_view_setup (EmpathyContactListView *view);
102 static void contact_list_view_row_has_child_toggled_cb (GtkTreeModel *model,
105 EmpathyContactListView *view);
106 static void contact_list_view_contact_received (EmpathyContactListView *view,
107 EmpathyContact *contact,
108 GdkDragAction action,
109 const gchar *old_group,
110 const gchar *new_group);
111 static void contact_list_view_drag_data_received (GtkWidget *widget,
112 GdkDragContext *context,
115 GtkSelectionData *selection,
118 static gboolean contact_list_view_drag_motion (GtkWidget *widget,
119 GdkDragContext *context,
123 static gboolean contact_list_view_drag_motion_cb (DragMotionData *data);
124 static void contact_list_view_drag_begin (GtkWidget *widget,
125 GdkDragContext *context);
126 static void contact_list_view_drag_data_get (GtkWidget *widget,
127 GdkDragContext *context,
128 GtkSelectionData *selection,
131 static void contact_list_view_drag_end (GtkWidget *widget,
132 GdkDragContext *context);
133 static gboolean contact_list_view_drag_drop (GtkWidget *widget,
134 GdkDragContext *drag_context,
138 static void contact_list_view_cell_set_background (EmpathyContactListView *view,
139 GtkCellRenderer *cell,
142 static void contact_list_view_pixbuf_cell_data_func (GtkTreeViewColumn *tree_column,
143 GtkCellRenderer *cell,
146 EmpathyContactListView *view);
147 static void contact_list_view_avatar_cell_data_func (GtkTreeViewColumn *tree_column,
148 GtkCellRenderer *cell,
151 EmpathyContactListView *view);
152 static void contact_list_view_text_cell_data_func (GtkTreeViewColumn *tree_column,
153 GtkCellRenderer *cell,
156 EmpathyContactListView *view);
157 static void contact_list_view_expander_cell_data_func (GtkTreeViewColumn *column,
158 GtkCellRenderer *cell,
161 EmpathyContactListView *view);
162 static GtkWidget * contact_list_view_get_contact_menu (EmpathyContactListView *view,
163 gboolean can_send_file,
164 gboolean can_show_log);
165 static gboolean contact_list_view_button_press_event_cb (EmpathyContactListView *view,
166 GdkEventButton *event,
168 static void contact_list_view_row_activated_cb (EmpathyContactListView *view,
170 GtkTreeViewColumn *col,
172 static void contact_list_view_row_expand_or_collapse_cb (EmpathyContactListView *view,
176 static gboolean contact_list_view_filter_show_contact (EmpathyContact *contact,
177 const gchar *filter);
178 static gboolean contact_list_view_filter_show_group (EmpathyContactListView *view,
180 const gchar *filter);
181 static gboolean contact_list_view_filter_func (GtkTreeModel *model,
183 EmpathyContactListView *view);
184 static void contact_list_view_action_cb (GtkAction *action,
185 EmpathyContactListView *view);
186 static void contact_list_view_action_activated (EmpathyContactListView *view,
187 EmpathyContact *contact);
194 static const GtkActionEntry entries[] = {
195 { "ContactMenu", NULL,
196 N_("_Contact"), NULL, NULL,
200 N_("_Group"),NULL, NULL,
203 { "Chat", EMPATHY_IMAGE_MESSAGE,
204 N_("_Chat"), NULL, N_("Chat with contact"),
205 G_CALLBACK (contact_list_view_action_cb)
207 { "Information", EMPATHY_IMAGE_CONTACT_INFORMATION,
208 N_("Infor_mation"), "<control>I", N_("View contact information"),
209 G_CALLBACK (contact_list_view_action_cb)
212 N_("Re_name"), NULL, N_("Rename"),
213 G_CALLBACK (contact_list_view_action_cb)
215 { "Edit", GTK_STOCK_EDIT,
216 N_("_Edit"), NULL, N_("Edit the groups and name for this contact"),
217 G_CALLBACK (contact_list_view_action_cb)
219 { "Remove", GTK_STOCK_REMOVE,
220 N_("_Remove"), NULL, N_("Remove contact"),
221 G_CALLBACK (contact_list_view_action_cb)
223 { "Invite", EMPATHY_IMAGE_GROUP_MESSAGE,
224 N_("_Invite to Chat Room"), NULL, N_("Invite to a currently open chat room"),
225 G_CALLBACK (contact_list_view_action_cb)
228 N_("_Send File..."), NULL, N_("Send a file"),
229 G_CALLBACK (contact_list_view_action_cb)
231 { "Log", GTK_STOCK_JUSTIFY_LEFT,
232 N_("_View Previous Conversations"), NULL, N_("View previous conversations with this contact"),
233 G_CALLBACK (contact_list_view_action_cb)
237 static guint n_entries = G_N_ELEMENTS (entries);
239 static const gchar *ui_info =
241 " <popup name='Contact'>"
242 " <menuitem action='Chat'/>"
243 " <menuitem action='Log'/>"
244 " <menuitem action='SendFile'/>"
246 " <menuitem action='Invite'/>"
248 " <menuitem action='Edit'/>"
249 " <menuitem action='Remove'/>"
251 " <menuitem action='Information'/>"
253 " <popup name='Group'>"
254 " <menuitem action='Rename'/>"
259 DND_DRAG_TYPE_CONTACT_ID,
261 DND_DRAG_TYPE_STRING,
264 static const GtkTargetEntry drag_types_dest[] = {
265 { "text/contact-id", 0, DND_DRAG_TYPE_CONTACT_ID },
266 { "text/uri-list", 0, DND_DRAG_TYPE_URL },
267 { "text/plain", 0, DND_DRAG_TYPE_STRING },
268 { "STRING", 0, DND_DRAG_TYPE_STRING },
271 static const GtkTargetEntry drag_types_source[] = {
272 { "text/contact-id", 0, DND_DRAG_TYPE_CONTACT_ID },
275 static GdkAtom drag_atoms_dest[G_N_ELEMENTS (drag_types_dest)];
276 static GdkAtom drag_atoms_source[G_N_ELEMENTS (drag_types_source)];
279 DRAG_CONTACT_RECEIVED,
283 static guint signals[LAST_SIGNAL];
285 G_DEFINE_TYPE (EmpathyContactListView, empathy_contact_list_view, GTK_TYPE_TREE_VIEW);
288 empathy_contact_list_view_class_init (EmpathyContactListViewClass *klass)
290 GObjectClass *object_class = G_OBJECT_CLASS (klass);
291 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
293 object_class->finalize = contact_list_view_finalize;
294 object_class->get_property = contact_list_view_get_property;
295 object_class->set_property = contact_list_view_set_property;
297 widget_class->drag_data_received = contact_list_view_drag_data_received;
298 widget_class->drag_drop = contact_list_view_drag_drop;
299 widget_class->drag_begin = contact_list_view_drag_begin;
300 widget_class->drag_data_get = contact_list_view_drag_data_get;
301 widget_class->drag_end = contact_list_view_drag_end;
302 /* FIXME: noticed but when you drag the row over the treeview
303 * fast, it seems to stop redrawing itself, if we don't
304 * connect this signal, all is fine.
306 widget_class->drag_motion = contact_list_view_drag_motion;
308 signals[DRAG_CONTACT_RECEIVED] =
309 g_signal_new ("drag-contact-received",
310 G_OBJECT_CLASS_TYPE (klass),
314 empathy_marshal_VOID__OBJECT_STRING_STRING,
316 3, EMPATHY_TYPE_CONTACT, G_TYPE_STRING, G_TYPE_STRING);
318 g_object_class_install_property (object_class,
320 g_param_spec_string ("filter",
322 "The text to use to filter the contact list",
326 g_type_class_add_private (object_class, sizeof (EmpathyContactListViewPriv));
330 empathy_contact_list_view_init (EmpathyContactListView *view)
332 EmpathyContactListViewPriv *priv;
333 GtkActionGroup *action_group;
334 GError *error = NULL;
336 priv = GET_PRIV (view);
338 /* Get saved group states. */
339 empathy_contact_groups_get_all ();
341 /* Set up UI Manager */
342 priv->ui = gtk_ui_manager_new ();
344 action_group = gtk_action_group_new ("Actions");
345 gtk_action_group_set_translation_domain (action_group, GETTEXT_PACKAGE);
346 gtk_action_group_add_actions (action_group, entries, n_entries, view);
347 gtk_ui_manager_insert_action_group (priv->ui, action_group, 0);
349 if (!gtk_ui_manager_add_ui_from_string (priv->ui, ui_info, -1, &error)) {
350 g_warning ("Could not build contact menus from string:'%s'", error->message);
351 g_error_free (error);
354 g_object_unref (action_group);
356 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (view),
357 empathy_contact_list_store_row_separator_func,
360 /* Connect to tree view signals rather than override. */
361 g_signal_connect (view,
362 "button-press-event",
363 G_CALLBACK (contact_list_view_button_press_event_cb),
365 g_signal_connect (view,
367 G_CALLBACK (contact_list_view_row_activated_cb),
369 g_signal_connect (view,
371 G_CALLBACK (contact_list_view_row_expand_or_collapse_cb),
372 GINT_TO_POINTER (TRUE));
373 g_signal_connect (view,
375 G_CALLBACK (contact_list_view_row_expand_or_collapse_cb),
376 GINT_TO_POINTER (FALSE));
380 contact_list_view_finalize (GObject *object)
382 EmpathyContactListViewPriv *priv;
384 priv = GET_PRIV (object);
387 g_object_unref (priv->ui);
390 g_object_unref (priv->store);
393 g_object_unref (priv->filter);
395 g_free (priv->filter_text);
397 G_OBJECT_CLASS (empathy_contact_list_view_parent_class)->finalize (object);
401 contact_list_view_get_property (GObject *object,
406 EmpathyContactListViewPriv *priv;
408 priv = GET_PRIV (object);
412 g_value_set_string (value, priv->filter_text);
415 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
421 contact_list_view_set_property (GObject *object,
426 EmpathyContactListViewPriv *priv;
428 priv = GET_PRIV (object);
432 empathy_contact_list_view_set_filter (EMPATHY_CONTACT_LIST_VIEW (object),
433 g_value_get_string (value));
436 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
441 EmpathyContactListView *
442 empathy_contact_list_view_new (EmpathyContactListStore *store)
444 EmpathyContactListViewPriv *priv;
445 EmpathyContactListView *view;
447 view = g_object_new (EMPATHY_TYPE_CONTACT_LIST_VIEW, NULL);
448 priv = GET_PRIV (view);
450 priv->store = g_object_ref (store);
451 contact_list_view_setup (view);
457 empathy_contact_list_view_get_selected (EmpathyContactListView *view)
459 EmpathyContactListViewPriv *priv;
460 GtkTreeSelection *selection;
463 EmpathyContact *contact;
465 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), NULL);
467 priv = GET_PRIV (view);
469 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
470 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
474 gtk_tree_model_get (model, &iter, COL_CONTACT, &contact, -1);
480 empathy_contact_list_view_get_selected_group (EmpathyContactListView *view)
482 EmpathyContactListViewPriv *priv;
483 GtkTreeSelection *selection;
489 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), NULL);
491 priv = GET_PRIV (view);
493 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
494 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
498 gtk_tree_model_get (model, &iter,
499 COL_IS_GROUP, &is_group,
512 empathy_contact_list_view_get_group_menu (EmpathyContactListView *view)
514 EmpathyContactListViewPriv *priv;
517 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), NULL);
519 priv = GET_PRIV (view);
521 widget = gtk_ui_manager_get_widget (priv->ui, "/Group");
527 empathy_contact_list_view_get_contact_menu (EmpathyContactListView *view,
528 EmpathyContact *contact)
530 EmpathyLogManager *log_manager;
531 gboolean can_show_log;
532 gboolean can_send_file;
534 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), NULL);
535 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
537 log_manager = empathy_log_manager_new ();
538 can_show_log = empathy_log_manager_exists (log_manager,
539 empathy_contact_get_account (contact),
540 empathy_contact_get_id (contact),
542 can_send_file = FALSE;
543 g_object_unref (log_manager);
545 return contact_list_view_get_contact_menu (view,
551 empathy_contact_list_view_set_filter (EmpathyContactListView *view,
554 EmpathyContactListViewPriv *priv;
556 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view));
558 priv = GET_PRIV (view);
560 g_free (priv->filter_text);
562 priv->filter_text = g_utf8_casefold (filter, -1);
564 priv->filter_text = NULL;
567 empathy_debug (DEBUG_DOMAIN, "Refiltering with filter:'%s' (case folded)", filter);
568 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter));
572 empathy_contact_list_view_set_drag_received_func (EmpathyContactListView *view,
573 EmpathyContactListViewDragReceivedFunc func,
576 EmpathyContactListViewPriv *priv;
578 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view));
580 priv = GET_PRIV (view);
583 priv->drag_received = func;
584 priv->drag_received_data = user_data;
586 priv->drag_received = NULL;
587 priv->drag_received_data = NULL;
592 contact_list_view_setup (EmpathyContactListView *view)
594 EmpathyContactListViewPriv *priv;
595 GtkCellRenderer *cell;
596 GtkTreeViewColumn *col;
599 priv = GET_PRIV (view);
602 priv->filter = gtk_tree_model_filter_new (GTK_TREE_MODEL (priv->store), NULL);
604 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (priv->filter),
605 (GtkTreeModelFilterVisibleFunc)
606 contact_list_view_filter_func,
608 gtk_tree_view_set_search_equal_func (GTK_TREE_VIEW (view),
609 empathy_contact_list_store_search_equal_func,
611 g_signal_connect (priv->filter, "row-has-child-toggled",
612 G_CALLBACK (contact_list_view_row_has_child_toggled_cb),
614 gtk_tree_view_set_model (GTK_TREE_VIEW (view), priv->filter);
619 "headers-visible", FALSE,
621 "show-expanders", FALSE,
624 col = gtk_tree_view_column_new ();
627 cell = gtk_cell_renderer_pixbuf_new ();
628 gtk_tree_view_column_pack_start (col, cell, FALSE);
629 gtk_tree_view_column_set_cell_data_func (
631 (GtkTreeCellDataFunc) contact_list_view_pixbuf_cell_data_func,
641 cell = empathy_cell_renderer_text_new ();
642 gtk_tree_view_column_pack_start (col, cell, TRUE);
643 gtk_tree_view_column_set_cell_data_func (
645 (GtkTreeCellDataFunc) contact_list_view_text_cell_data_func,
648 gtk_tree_view_column_add_attribute (col, cell,
650 gtk_tree_view_column_add_attribute (col, cell,
651 "status", COL_STATUS);
652 gtk_tree_view_column_add_attribute (col, cell,
653 "is_group", COL_IS_GROUP);
656 cell = gtk_cell_renderer_pixbuf_new ();
657 gtk_tree_view_column_pack_start (col, cell, FALSE);
658 gtk_tree_view_column_set_cell_data_func (
660 (GtkTreeCellDataFunc) contact_list_view_avatar_cell_data_func,
672 cell = empathy_cell_renderer_expander_new ();
673 gtk_tree_view_column_pack_end (col, cell, FALSE);
674 gtk_tree_view_column_set_cell_data_func (
676 (GtkTreeCellDataFunc) contact_list_view_expander_cell_data_func,
679 /* Actually add the column now we have added all cell renderers */
680 gtk_tree_view_append_column (GTK_TREE_VIEW (view), col);
683 for (i = 0; i < G_N_ELEMENTS (drag_types_dest); ++i) {
684 drag_atoms_dest[i] = gdk_atom_intern (drag_types_dest[i].target,
688 for (i = 0; i < G_N_ELEMENTS (drag_types_source); ++i) {
689 drag_atoms_source[i] = gdk_atom_intern (drag_types_source[i].target,
693 /* Note: We support the COPY action too, but need to make the
694 * MOVE action the default.
696 gtk_drag_source_set (GTK_WIDGET (view),
699 G_N_ELEMENTS (drag_types_source),
702 gtk_drag_dest_set (GTK_WIDGET (view),
703 GTK_DEST_DEFAULT_ALL,
705 G_N_ELEMENTS (drag_types_dest),
706 GDK_ACTION_MOVE | GDK_ACTION_LINK);
710 contact_list_view_row_has_child_toggled_cb (GtkTreeModel *model,
713 EmpathyContactListView *view)
715 gboolean is_group = FALSE;
718 gtk_tree_model_get (model, iter,
719 COL_IS_GROUP, &is_group,
723 if (!is_group || G_STR_EMPTY (name)) {
728 if (empathy_contact_group_get_expanded (name)) {
729 g_signal_handlers_block_by_func (view,
730 contact_list_view_row_expand_or_collapse_cb,
731 GINT_TO_POINTER (TRUE));
732 gtk_tree_view_expand_row (GTK_TREE_VIEW (view), path, TRUE);
733 g_signal_handlers_unblock_by_func (view,
734 contact_list_view_row_expand_or_collapse_cb,
735 GINT_TO_POINTER (TRUE));
737 g_signal_handlers_block_by_func (view,
738 contact_list_view_row_expand_or_collapse_cb,
739 GINT_TO_POINTER (FALSE));
740 gtk_tree_view_collapse_row (GTK_TREE_VIEW (view), path);
741 g_signal_handlers_unblock_by_func (view,
742 contact_list_view_row_expand_or_collapse_cb,
743 GINT_TO_POINTER (FALSE));
750 contact_list_view_contact_received (EmpathyContactListView *view,
751 EmpathyContact *contact,
752 GdkDragAction action,
753 const gchar *old_group,
754 const gchar *new_group)
756 EmpathyContactListViewPriv *priv;
758 GList *new_groups_list = NULL;
760 priv = GET_PRIV (view);
762 groups = empathy_contact_get_groups (contact);
763 for (l = groups; l; l = l->next) {
768 if (action == GDK_ACTION_MOVE &&
770 strcmp (str, old_group) == 0) {
774 if (new_group && strcmp (str, new_group) == 0) {
775 /* Otherwise we set it twice */
779 new_groups_list = g_list_prepend (new_groups_list, g_strdup (str));
783 new_groups_list = g_list_prepend (new_groups_list, g_strdup (new_group));
786 empathy_contact_set_groups (contact, new_groups_list);
790 contact_list_view_drag_data_received (GtkWidget *widget,
791 GdkDragContext *context,
794 GtkSelectionData *selection,
798 EmpathyContactListViewPriv *priv;
799 EmpathyContactList *list;
802 GtkTreeViewDropPosition position;
803 EmpathyContact *contact;
805 gchar *new_group = NULL;
806 gchar *old_group = NULL;
809 priv = GET_PRIV (widget);
811 id = (const gchar*) selection->data;
812 empathy_debug (DEBUG_DOMAIN, "Received %s%s drag & drop contact from roster with id:'%s'",
813 context->action == GDK_ACTION_MOVE ? "move" : "",
814 context->action == GDK_ACTION_COPY ? "copy" : "",
817 /* FIXME: This is ambigous, an id can come from multiple accounts */
818 list = empathy_contact_list_store_get_list_iface (priv->store);
819 contact = empathy_contact_list_find (list, id);
822 empathy_debug (DEBUG_DOMAIN, "No contact found associated with drag & drop");
826 model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
828 /* Get source group information. */
829 if (priv->drag_row) {
830 path = gtk_tree_row_reference_get_path (priv->drag_row);
832 old_group = empathy_contact_list_store_get_parent_group (model, path, NULL);
833 gtk_tree_path_free (path);
837 /* Get destination group information. */
838 is_row = gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget),
845 new_group = empathy_contact_list_store_get_parent_group (model, path, NULL);
846 gtk_tree_path_free (path);
849 empathy_debug (DEBUG_DOMAIN,
850 "contact '%s' dragged from '%s' to '%s'",
851 empathy_contact_get_name (contact),
852 old_group, new_group);
854 if (priv->drag_received) {
855 priv->drag_received (contact,
859 priv->drag_received_data);
861 contact_list_view_contact_received (EMPATHY_CONTACT_LIST_VIEW (widget),
871 gtk_drag_finish (context, TRUE, FALSE, GDK_CURRENT_TIME);
875 contact_list_view_drag_motion (GtkWidget *widget,
876 GdkDragContext *context,
881 static DragMotionData *dm = NULL;
884 gboolean is_different = FALSE;
885 gboolean cleanup = TRUE;
887 is_row = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
898 cleanup &= (dm && gtk_tree_path_compare (dm->path, path) != 0);
899 is_different = (!dm || (dm && gtk_tree_path_compare (dm->path, path) != 0));
904 if (!is_different && !cleanup) {
909 gtk_tree_path_free (dm->path);
910 if (dm->timeout_id) {
911 g_source_remove (dm->timeout_id);
919 if (!gtk_tree_view_row_expanded (GTK_TREE_VIEW (widget), path)) {
920 dm = g_new0 (DragMotionData, 1);
922 dm->view = EMPATHY_CONTACT_LIST_VIEW (widget);
923 dm->path = gtk_tree_path_copy (path);
925 dm->timeout_id = g_timeout_add (
927 (GSourceFunc) contact_list_view_drag_motion_cb,
935 contact_list_view_drag_motion_cb (DragMotionData *data)
937 gtk_tree_view_expand_row (GTK_TREE_VIEW (data->view),
941 data->timeout_id = 0;
947 contact_list_view_drag_begin (GtkWidget *widget,
948 GdkDragContext *context)
950 EmpathyContactListViewPriv *priv;
951 GtkTreeSelection *selection;
956 priv = GET_PRIV (widget);
958 GTK_WIDGET_CLASS (empathy_contact_list_view_parent_class)->drag_begin (widget,
961 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (widget));
962 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
966 path = gtk_tree_model_get_path (model, &iter);
967 priv->drag_row = gtk_tree_row_reference_new (model, path);
968 gtk_tree_path_free (path);
972 contact_list_view_drag_data_get (GtkWidget *widget,
973 GdkDragContext *context,
974 GtkSelectionData *selection,
978 EmpathyContactListViewPriv *priv;
979 GtkTreePath *src_path;
982 EmpathyContact *contact;
985 priv = GET_PRIV (widget);
987 model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
988 if (!priv->drag_row) {
992 src_path = gtk_tree_row_reference_get_path (priv->drag_row);
997 if (!gtk_tree_model_get_iter (model, &iter, src_path)) {
998 gtk_tree_path_free (src_path);
1002 gtk_tree_path_free (src_path);
1004 contact = empathy_contact_list_view_get_selected (EMPATHY_CONTACT_LIST_VIEW (widget));
1009 id = empathy_contact_get_id (contact);
1010 g_object_unref (contact);
1013 case DND_DRAG_TYPE_CONTACT_ID:
1014 gtk_selection_data_set (selection, drag_atoms_source[info], 8,
1015 (guchar*)id, strlen (id) + 1);
1024 contact_list_view_drag_end (GtkWidget *widget,
1025 GdkDragContext *context)
1027 EmpathyContactListViewPriv *priv;
1029 priv = GET_PRIV (widget);
1031 GTK_WIDGET_CLASS (empathy_contact_list_view_parent_class)->drag_end (widget,
1034 if (priv->drag_row) {
1035 gtk_tree_row_reference_free (priv->drag_row);
1036 priv->drag_row = NULL;
1041 contact_list_view_drag_drop (GtkWidget *widget,
1042 GdkDragContext *drag_context,
1051 contact_list_view_cell_set_background (EmpathyContactListView *view,
1052 GtkCellRenderer *cell,
1059 style = gtk_widget_get_style (GTK_WIDGET (view));
1063 color = style->bg[GTK_STATE_SELECTED];
1065 /* Here we take the current theme colour and add it to
1066 * the colour for white and average the two. This
1067 * gives a colour which is inline with the theme but
1070 color.red = (color.red + (style->white).red) / 2;
1071 color.green = (color.green + (style->white).green) / 2;
1072 color.blue = (color.blue + (style->white).blue) / 2;
1075 "cell-background-gdk", &color,
1079 "cell-background-gdk", NULL,
1084 "cell-background-gdk", NULL,
1090 contact_list_view_pixbuf_cell_data_func (GtkTreeViewColumn *tree_column,
1091 GtkCellRenderer *cell,
1092 GtkTreeModel *model,
1094 EmpathyContactListView *view)
1100 gtk_tree_model_get (model, iter,
1101 COL_IS_GROUP, &is_group,
1102 COL_IS_ACTIVE, &is_active,
1103 COL_ICON_STATUS, &icon_name,
1107 "visible", !is_group,
1108 "icon-name", icon_name,
1113 contact_list_view_cell_set_background (view, cell, is_group, is_active);
1117 contact_list_view_avatar_cell_data_func (GtkTreeViewColumn *tree_column,
1118 GtkCellRenderer *cell,
1119 GtkTreeModel *model,
1121 EmpathyContactListView *view)
1124 gboolean show_avatar;
1128 gtk_tree_model_get (model, iter,
1129 COL_PIXBUF_AVATAR, &pixbuf,
1130 COL_PIXBUF_AVATAR_VISIBLE, &show_avatar,
1131 COL_IS_GROUP, &is_group,
1132 COL_IS_ACTIVE, &is_active,
1136 "visible", !is_group && show_avatar,
1141 g_object_unref (pixbuf);
1144 contact_list_view_cell_set_background (view, cell, is_group, is_active);
1148 contact_list_view_text_cell_data_func (GtkTreeViewColumn *tree_column,
1149 GtkCellRenderer *cell,
1150 GtkTreeModel *model,
1152 EmpathyContactListView *view)
1156 gboolean show_status;
1158 gtk_tree_model_get (model, iter,
1159 COL_IS_GROUP, &is_group,
1160 COL_IS_ACTIVE, &is_active,
1161 COL_STATUS_VISIBLE, &show_status,
1165 "show-status", show_status,
1168 contact_list_view_cell_set_background (view, cell, is_group, is_active);
1172 contact_list_view_expander_cell_data_func (GtkTreeViewColumn *column,
1173 GtkCellRenderer *cell,
1174 GtkTreeModel *model,
1176 EmpathyContactListView *view)
1181 gtk_tree_model_get (model, iter,
1182 COL_IS_GROUP, &is_group,
1183 COL_IS_ACTIVE, &is_active,
1186 if (gtk_tree_model_iter_has_child (model, iter)) {
1188 gboolean row_expanded;
1190 path = gtk_tree_model_get_path (model, iter);
1191 row_expanded = gtk_tree_view_row_expanded (GTK_TREE_VIEW (column->tree_view), path);
1192 gtk_tree_path_free (path);
1196 "expander-style", row_expanded ? GTK_EXPANDER_EXPANDED : GTK_EXPANDER_COLLAPSED,
1199 g_object_set (cell, "visible", FALSE, NULL);
1202 contact_list_view_cell_set_background (view, cell, is_group, is_active);
1206 contact_list_view_get_contact_menu (EmpathyContactListView *view,
1207 gboolean can_send_file,
1208 gboolean can_show_log)
1210 EmpathyContactListViewPriv *priv;
1214 priv = GET_PRIV (view);
1216 /* Sort out sensitive items */
1217 action = gtk_ui_manager_get_action (priv->ui, "/Contact/Log");
1218 gtk_action_set_sensitive (action, can_show_log);
1220 action = gtk_ui_manager_get_action (priv->ui, "/Contact/SendFile");
1221 gtk_action_set_visible (action, can_send_file);
1223 widget = gtk_ui_manager_get_widget (priv->ui, "/Contact");
1229 contact_list_view_button_press_event_cb (EmpathyContactListView *view,
1230 GdkEventButton *event,
1233 EmpathyContactListViewPriv *priv;
1234 EmpathyContact *contact;
1236 GtkTreeSelection *selection;
1237 GtkTreeModel *model;
1239 gboolean row_exists;
1242 if (event->button != 3) {
1246 priv = GET_PRIV (view);
1248 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
1249 model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
1251 gtk_widget_grab_focus (GTK_WIDGET (view));
1253 row_exists = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (view),
1261 gtk_tree_selection_unselect_all (selection);
1262 gtk_tree_selection_select_path (selection, path);
1264 gtk_tree_model_get_iter (model, &iter, path);
1265 gtk_tree_path_free (path);
1267 gtk_tree_model_get (model, &iter, COL_CONTACT, &contact, -1);
1270 menu = empathy_contact_list_view_get_contact_menu (view, contact);
1271 g_object_unref (contact);
1273 menu = empathy_contact_list_view_get_group_menu (view);
1280 gtk_widget_show (menu);
1282 gtk_menu_popup (GTK_MENU (menu),
1283 NULL, NULL, NULL, NULL,
1284 event->button, event->time);
1290 contact_list_view_row_activated_cb (EmpathyContactListView *view,
1292 GtkTreeViewColumn *col,
1295 EmpathyContact *contact;
1296 GtkTreeModel *model;
1299 model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
1301 gtk_tree_model_get_iter (model, &iter, path);
1302 gtk_tree_model_get (model, &iter, COL_CONTACT, &contact, -1);
1305 contact_list_view_action_activated (view, contact);
1306 g_object_unref (contact);
1311 contact_list_view_row_expand_or_collapse_cb (EmpathyContactListView *view,
1316 GtkTreeModel *model;
1320 model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
1322 gtk_tree_model_get (model, iter,
1326 expanded = GPOINTER_TO_INT (user_data);
1327 empathy_contact_group_set_expanded (name, expanded);
1333 contact_list_view_filter_show_contact (EmpathyContact *contact,
1334 const gchar *filter)
1339 /* Check contact id */
1340 str = g_utf8_casefold (empathy_contact_get_id (contact), -1);
1341 visible = G_STR_EMPTY (str) || strstr (str, filter);
1348 /* Check contact name */
1349 str = g_utf8_casefold (empathy_contact_get_name (contact), -1);
1350 visible = G_STR_EMPTY (str) || strstr (str, filter);
1357 contact_list_view_filter_show_group (EmpathyContactListView *view,
1359 const gchar *filter)
1361 EmpathyContactListViewPriv *priv;
1362 EmpathyContactList *list;
1363 GList *contacts, *l;
1365 gboolean show_group = FALSE;
1367 priv = GET_PRIV (view);
1369 str = g_utf8_casefold (group, -1);
1374 /* If the filter is the partially the group name, we show the
1377 if (strstr (str, filter)) {
1382 /* At this point, we need to check in advance if this
1383 * group should be shown because a contact we want to
1384 * show exists in it.
1386 list = empathy_contact_list_store_get_list_iface (priv->store);
1387 contacts = empathy_contact_list_get_members (list);
1388 for (l = contacts; l && !show_group; l = l->next) {
1389 if (!empathy_contact_is_in_group (l->data, group)) {
1390 g_object_unref (l->data);
1394 if (contact_list_view_filter_show_contact (l->data, filter)) {
1397 g_object_unref (l->data);
1399 g_list_free (contacts);
1406 contact_list_view_filter_func (GtkTreeModel *model,
1408 EmpathyContactListView *view)
1410 EmpathyContactListViewPriv *priv;
1412 gboolean is_separator;
1413 gboolean visible = TRUE;
1415 priv = GET_PRIV (view);
1417 if (G_STR_EMPTY (priv->filter_text)) {
1421 /* Check to see if iter matches any group names */
1422 gtk_tree_model_get (model, iter,
1423 COL_IS_GROUP, &is_group,
1424 COL_IS_SEPARATOR, &is_separator,
1430 gtk_tree_model_get (model, iter, COL_NAME, &name, -1);
1431 visible &= contact_list_view_filter_show_group (view,
1435 } else if (is_separator) {
1436 /* Do nothing here */
1438 EmpathyContact *contact;
1440 /* Check contact id */
1441 gtk_tree_model_get (model, iter, COL_CONTACT, &contact, -1);
1442 visible &= contact_list_view_filter_show_contact (contact,
1444 g_object_unref (contact);
1451 contact_list_view_action_cb (GtkAction *action,
1452 EmpathyContactListView *view)
1454 EmpathyContactListViewPriv *priv;
1455 EmpathyContact *contact;
1460 priv = GET_PRIV (view);
1462 name = gtk_action_get_name (action);
1467 empathy_debug (DEBUG_DOMAIN, "Action:'%s' activated", name);
1469 contact = empathy_contact_list_view_get_selected (view);
1470 group = empathy_contact_list_view_get_selected_group (view);
1471 parent = empathy_get_toplevel_window (GTK_WIDGET (view));
1473 if (contact && strcmp (name, "Chat") == 0) {
1474 contact_list_view_action_activated (view, contact);
1476 else if (contact && strcmp (name, "Information") == 0) {
1477 empathy_contact_information_dialog_show (contact, parent, FALSE);
1479 else if (contact && strcmp (name, "Edit") == 0) {
1480 empathy_contact_information_dialog_show (contact, parent, TRUE);
1482 else if (contact && strcmp (name, "Remove") == 0) {
1483 /* FIXME: Ask for confirmation */
1484 EmpathyContactList *list;
1486 list = empathy_contact_list_store_get_list_iface (priv->store);
1487 empathy_contact_list_remove (list, contact,
1488 _("Sorry, I don't want you in my contact list anymore."));
1490 else if (contact && strcmp (name, "Invite") == 0) {
1492 else if (contact && strcmp (name, "SendFile") == 0) {
1494 else if (contact && strcmp (name, "Log") == 0) {
1495 empathy_log_window_show (empathy_contact_get_account (contact),
1496 empathy_contact_get_id (contact),
1500 else if (group && strcmp (name, "Rename") == 0) {
1505 g_object_unref (contact);
1510 contact_list_view_action_activated (EmpathyContactListView *view,
1511 EmpathyContact *contact)
1515 mc = empathy_mission_control_new ();
1516 mission_control_request_channel (mc,
1517 empathy_contact_get_account (contact),
1518 TP_IFACE_CHANNEL_TYPE_TEXT,
1519 empathy_contact_get_handle (contact),
1520 TP_HANDLE_TYPE_CONTACT,
1522 g_object_unref (mc);