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/gossip-debug.h>
40 #include <libempathy/gossip-utils.h>
41 #include <libempathy/empathy-marshal.h>
43 #include "gossip-contact-list-view.h"
44 #include "gossip-contact-list-store.h"
45 #include "empathy-images.h"
46 #include "gossip-contact-groups.h"
47 #include "gossip-cell-renderer-expander.h"
48 #include "gossip-cell-renderer-text.h"
49 #include "gossip-ui-utils.h"
50 //#include "gossip-chat-invite.h"
51 //#include "gossip-contact-info-dialog.h"
52 //#include "gossip-edit-contact-dialog.h"
53 //#include "gossip-ft-window.h"
54 //#include "gossip-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), GOSSIP_TYPE_CONTACT_LIST_VIEW, GossipContactListViewPriv))
67 struct _GossipContactListViewPriv {
68 GossipContactListStore *store;
70 GtkTreeRowReference *drag_row;
74 GossipContactListViewDragReceivedFunc drag_received;
75 gpointer drag_received_data;
79 GossipContactListView *view;
85 GossipContactListView *view;
86 GossipContact *contact;
90 static void gossip_contact_list_view_class_init (GossipContactListViewClass *klass);
91 static void gossip_contact_list_view_init (GossipContactListView *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 (GossipContactListView *view);
102 static void contact_list_view_row_has_child_toggled_cb (GtkTreeModel *model,
105 GossipContactListView *view);
106 static void contact_list_view_contact_received (GossipContactListView *view,
107 GossipContact *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,
119 static gboolean contact_list_view_drag_motion (GtkWidget *widget,
120 GdkDragContext *context,
125 static gboolean contact_list_view_drag_motion_cb (DragMotionData *data);
126 static void contact_list_view_drag_begin (GtkWidget *widget,
127 GdkDragContext *context,
129 static void contact_list_view_drag_data_get (GtkWidget *widget,
130 GdkDragContext *context,
131 GtkSelectionData *selection,
135 static void contact_list_view_drag_end (GtkWidget *widget,
136 GdkDragContext *context,
138 static void contact_list_view_cell_set_background (GossipContactListView *view,
139 GtkCellRenderer *cell,
142 static void contact_list_view_pixbuf_cell_data_func (GtkTreeViewColumn *tree_column,
143 GtkCellRenderer *cell,
146 GossipContactListView *view);
147 static void contact_list_view_avatar_cell_data_func (GtkTreeViewColumn *tree_column,
148 GtkCellRenderer *cell,
151 GossipContactListView *view);
152 static void contact_list_view_text_cell_data_func (GtkTreeViewColumn *tree_column,
153 GtkCellRenderer *cell,
156 GossipContactListView *view);
157 static void contact_list_view_expander_cell_data_func (GtkTreeViewColumn *column,
158 GtkCellRenderer *cell,
161 GossipContactListView *view);
162 static GtkWidget * contact_list_view_get_contact_menu (GossipContactListView *view,
163 gboolean can_send_file,
164 gboolean can_show_log);
165 static gboolean contact_list_view_button_press_event_cb (GossipContactListView *view,
166 GdkEventButton *event,
168 static void contact_list_view_row_activated_cb (GossipContactListView *view,
170 GtkTreeViewColumn *col,
172 static void contact_list_view_row_expand_or_collapse_cb (GossipContactListView *view,
176 static gboolean contact_list_view_filter_show_contact (GossipContact *contact,
177 const gchar *filter);
178 static gboolean contact_list_view_filter_show_group (GossipContactListView *view,
180 const gchar *filter);
181 static gboolean contact_list_view_filter_func (GtkTreeModel *model,
183 GossipContactListView *view);
184 static void contact_list_view_action_cb (GtkAction *action,
185 GossipContactListView *view);
186 static void contact_list_view_action_activated (GossipContactListView *view,
187 GossipContact *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 (GossipContactListView, gossip_contact_list_view, GTK_TYPE_TREE_VIEW);
288 gossip_contact_list_view_class_init (GossipContactListViewClass *klass)
290 GObjectClass *object_class = G_OBJECT_CLASS (klass);
292 object_class->finalize = contact_list_view_finalize;
293 object_class->get_property = contact_list_view_get_property;
294 object_class->set_property = contact_list_view_set_property;
296 signals[DRAG_CONTACT_RECEIVED] =
297 g_signal_new ("drag-contact-received",
298 G_OBJECT_CLASS_TYPE (klass),
302 empathy_marshal_VOID__OBJECT_STRING_STRING,
304 3, GOSSIP_TYPE_CONTACT, G_TYPE_STRING, G_TYPE_STRING);
306 g_object_class_install_property (object_class,
308 g_param_spec_string ("filter",
310 "The text to use to filter the contact list",
314 g_type_class_add_private (object_class, sizeof (GossipContactListViewPriv));
318 gossip_contact_list_view_init (GossipContactListView *view)
320 GossipContactListViewPriv *priv;
321 GtkActionGroup *action_group;
322 GError *error = NULL;
324 priv = GET_PRIV (view);
326 /* Get saved group states. */
327 gossip_contact_groups_get_all ();
329 /* Set up UI Manager */
330 priv->ui = gtk_ui_manager_new ();
332 action_group = gtk_action_group_new ("Actions");
333 gtk_action_group_set_translation_domain (action_group, GETTEXT_PACKAGE);
334 gtk_action_group_add_actions (action_group, entries, n_entries, view);
335 gtk_ui_manager_insert_action_group (priv->ui, action_group, 0);
337 if (!gtk_ui_manager_add_ui_from_string (priv->ui, ui_info, -1, &error)) {
338 g_warning ("Could not build contact menus from string:'%s'", error->message);
339 g_error_free (error);
342 g_object_unref (action_group);
344 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (view),
345 gossip_contact_list_store_row_separator_func,
348 /* Connect to tree view signals rather than override. */
349 g_signal_connect (view,
350 "button-press-event",
351 G_CALLBACK (contact_list_view_button_press_event_cb),
353 g_signal_connect (view,
355 G_CALLBACK (contact_list_view_row_activated_cb),
357 g_signal_connect (view,
359 G_CALLBACK (contact_list_view_row_expand_or_collapse_cb),
360 GINT_TO_POINTER (TRUE));
361 g_signal_connect (view,
363 G_CALLBACK (contact_list_view_row_expand_or_collapse_cb),
364 GINT_TO_POINTER (FALSE));
368 contact_list_view_finalize (GObject *object)
370 GossipContactListViewPriv *priv;
372 priv = GET_PRIV (object);
375 g_object_unref (priv->ui);
378 g_object_unref (priv->store);
381 g_object_unref (priv->filter);
383 g_free (priv->filter_text);
385 G_OBJECT_CLASS (gossip_contact_list_view_parent_class)->finalize (object);
389 contact_list_view_get_property (GObject *object,
394 GossipContactListViewPriv *priv;
396 priv = GET_PRIV (object);
400 g_value_set_string (value, priv->filter_text);
403 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
409 contact_list_view_set_property (GObject *object,
414 GossipContactListViewPriv *priv;
416 priv = GET_PRIV (object);
420 gossip_contact_list_view_set_filter (GOSSIP_CONTACT_LIST_VIEW (object),
421 g_value_get_string (value));
424 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
429 GossipContactListView *
430 gossip_contact_list_view_new (GossipContactListStore *store)
432 GossipContactListViewPriv *priv;
433 GossipContactListView *view;
435 view = g_object_new (GOSSIP_TYPE_CONTACT_LIST_VIEW, NULL);
436 priv = GET_PRIV (view);
438 priv->store = g_object_ref (store);
439 contact_list_view_setup (view);
445 gossip_contact_list_view_get_selected (GossipContactListView *view)
447 GossipContactListViewPriv *priv;
448 GtkTreeSelection *selection;
451 GossipContact *contact;
453 g_return_val_if_fail (GOSSIP_IS_CONTACT_LIST_VIEW (view), NULL);
455 priv = GET_PRIV (view);
457 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
458 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
462 gtk_tree_model_get (model, &iter, COL_CONTACT, &contact, -1);
468 gossip_contact_list_view_get_selected_group (GossipContactListView *view)
470 GossipContactListViewPriv *priv;
471 GtkTreeSelection *selection;
477 g_return_val_if_fail (GOSSIP_IS_CONTACT_LIST_VIEW (view), NULL);
479 priv = GET_PRIV (view);
481 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
482 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
486 gtk_tree_model_get (model, &iter,
487 COL_IS_GROUP, &is_group,
500 gossip_contact_list_view_get_group_menu (GossipContactListView *view)
502 GossipContactListViewPriv *priv;
505 g_return_val_if_fail (GOSSIP_IS_CONTACT_LIST_VIEW (view), NULL);
507 priv = GET_PRIV (view);
509 widget = gtk_ui_manager_get_widget (priv->ui, "/Group");
515 gossip_contact_list_view_get_contact_menu (GossipContactListView *view,
516 GossipContact *contact)
518 gboolean can_show_log;
519 gboolean can_send_file;
521 g_return_val_if_fail (GOSSIP_IS_CONTACT_LIST_VIEW (view), NULL);
522 g_return_val_if_fail (GOSSIP_IS_CONTACT (contact), NULL);
524 can_show_log = FALSE; /* FIXME: gossip_log_exists_for_contact (contact); */
525 can_send_file = FALSE;
527 return contact_list_view_get_contact_menu (view,
533 gossip_contact_list_view_set_filter (GossipContactListView *view,
536 GossipContactListViewPriv *priv;
538 g_return_if_fail (GOSSIP_IS_CONTACT_LIST_VIEW (view));
540 priv = GET_PRIV (view);
542 g_free (priv->filter_text);
544 priv->filter_text = g_utf8_casefold (filter, -1);
546 priv->filter_text = NULL;
549 gossip_debug (DEBUG_DOMAIN, "Refiltering with filter:'%s' (case folded)", filter);
550 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter));
554 gossip_contact_list_view_set_drag_received_func (GossipContactListView *view,
555 GossipContactListViewDragReceivedFunc func,
558 GossipContactListViewPriv *priv;
560 g_return_if_fail (GOSSIP_IS_CONTACT_LIST_VIEW (view));
562 priv = GET_PRIV (view);
565 priv->drag_received = func;
566 priv->drag_received_data = user_data;
568 priv->drag_received = NULL;
569 priv->drag_received_data = NULL;
574 contact_list_view_setup (GossipContactListView *view)
576 GossipContactListViewPriv *priv;
577 GtkCellRenderer *cell;
578 GtkTreeViewColumn *col;
581 priv = GET_PRIV (view);
584 priv->filter = gtk_tree_model_filter_new (GTK_TREE_MODEL (priv->store), NULL);
586 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (priv->filter),
587 (GtkTreeModelFilterVisibleFunc)
588 contact_list_view_filter_func,
590 gtk_tree_view_set_search_equal_func (GTK_TREE_VIEW (view),
591 gossip_contact_list_store_search_equal_func,
593 g_signal_connect (priv->filter, "row-has-child-toggled",
594 G_CALLBACK (contact_list_view_row_has_child_toggled_cb),
596 gtk_tree_view_set_model (GTK_TREE_VIEW (view), priv->filter);
601 "headers-visible", FALSE,
603 "show-expanders", FALSE,
606 col = gtk_tree_view_column_new ();
609 cell = gtk_cell_renderer_pixbuf_new ();
610 gtk_tree_view_column_pack_start (col, cell, FALSE);
611 gtk_tree_view_column_set_cell_data_func (
613 (GtkTreeCellDataFunc) contact_list_view_pixbuf_cell_data_func,
623 cell = gossip_cell_renderer_text_new ();
624 gtk_tree_view_column_pack_start (col, cell, TRUE);
625 gtk_tree_view_column_set_cell_data_func (
627 (GtkTreeCellDataFunc) contact_list_view_text_cell_data_func,
630 gtk_tree_view_column_add_attribute (col, cell,
632 gtk_tree_view_column_add_attribute (col, cell,
633 "status", COL_STATUS);
634 gtk_tree_view_column_add_attribute (col, cell,
635 "is_group", COL_IS_GROUP);
638 cell = gtk_cell_renderer_pixbuf_new ();
639 gtk_tree_view_column_pack_start (col, cell, FALSE);
640 gtk_tree_view_column_set_cell_data_func (
642 (GtkTreeCellDataFunc) contact_list_view_avatar_cell_data_func,
654 cell = gossip_cell_renderer_expander_new ();
655 gtk_tree_view_column_pack_end (col, cell, FALSE);
656 gtk_tree_view_column_set_cell_data_func (
658 (GtkTreeCellDataFunc) contact_list_view_expander_cell_data_func,
661 /* Actually add the column now we have added all cell renderers */
662 gtk_tree_view_append_column (GTK_TREE_VIEW (view), col);
665 for (i = 0; i < G_N_ELEMENTS (drag_types_dest); ++i) {
666 drag_atoms_dest[i] = gdk_atom_intern (drag_types_dest[i].target,
670 for (i = 0; i < G_N_ELEMENTS (drag_types_source); ++i) {
671 drag_atoms_source[i] = gdk_atom_intern (drag_types_source[i].target,
675 /* Note: We support the COPY action too, but need to make the
676 * MOVE action the default.
678 gtk_drag_source_set (GTK_WIDGET (view),
681 G_N_ELEMENTS (drag_types_source),
684 gtk_drag_dest_set (GTK_WIDGET (view),
685 GTK_DEST_DEFAULT_ALL,
687 G_N_ELEMENTS (drag_types_dest),
688 GDK_ACTION_MOVE | GDK_ACTION_LINK);
690 g_signal_connect (GTK_WIDGET (view),
691 "drag-data-received",
692 G_CALLBACK (contact_list_view_drag_data_received),
695 /* FIXME: noticed but when you drag the row over the treeview
696 * fast, it seems to stop redrawing itself, if we don't
697 * connect this signal, all is fine.
699 g_signal_connect (view,
701 G_CALLBACK (contact_list_view_drag_motion),
704 g_signal_connect (view,
706 G_CALLBACK (contact_list_view_drag_begin),
708 g_signal_connect (view,
710 G_CALLBACK (contact_list_view_drag_data_get),
712 g_signal_connect (view,
714 G_CALLBACK (contact_list_view_drag_end),
719 contact_list_view_row_has_child_toggled_cb (GtkTreeModel *model,
722 GossipContactListView *view)
724 gboolean is_group = FALSE;
727 gtk_tree_model_get (model, iter,
728 COL_IS_GROUP, &is_group,
732 if (!is_group || G_STR_EMPTY (name)) {
737 if (gossip_contact_group_get_expanded (name)) {
738 g_signal_handlers_block_by_func (view,
739 contact_list_view_row_expand_or_collapse_cb,
740 GINT_TO_POINTER (TRUE));
741 gtk_tree_view_expand_row (GTK_TREE_VIEW (view), path, TRUE);
742 g_signal_handlers_unblock_by_func (view,
743 contact_list_view_row_expand_or_collapse_cb,
744 GINT_TO_POINTER (TRUE));
746 g_signal_handlers_block_by_func (view,
747 contact_list_view_row_expand_or_collapse_cb,
748 GINT_TO_POINTER (FALSE));
749 gtk_tree_view_collapse_row (GTK_TREE_VIEW (view), path);
750 g_signal_handlers_unblock_by_func (view,
751 contact_list_view_row_expand_or_collapse_cb,
752 GINT_TO_POINTER (FALSE));
759 contact_list_view_contact_received (GossipContactListView *view,
760 GossipContact *contact,
761 GdkDragAction action,
762 const gchar *old_group,
763 const gchar *new_group)
765 GossipContactListViewPriv *priv;
767 GList *new_groups_list = NULL;
769 priv = GET_PRIV (view);
771 groups = gossip_contact_get_groups (contact);
772 for (l = groups; l; l = l->next) {
777 if (action == GDK_ACTION_MOVE &&
779 strcmp (str, old_group) == 0) {
783 if (new_group && strcmp (str, new_group) == 0) {
784 /* Otherwise we set it twice */
788 new_groups_list = g_list_prepend (new_groups_list, g_strdup (str));
792 new_groups_list = g_list_prepend (new_groups_list, g_strdup (new_group));
795 gossip_contact_set_groups (contact, new_groups_list);
799 contact_list_view_drag_data_received (GtkWidget *widget,
800 GdkDragContext *context,
803 GtkSelectionData *selection,
808 GossipContactListViewPriv *priv;
809 EmpathyContactManager *manager;
812 GtkTreeViewDropPosition position;
813 GossipContact *contact;
815 gchar *new_group = NULL;
816 gchar *old_group = NULL;
819 priv = GET_PRIV (widget);
821 id = (const gchar*) selection->data;
822 gossip_debug (DEBUG_DOMAIN, "Received %s%s drag & drop contact from roster with id:'%s'",
823 context->action == GDK_ACTION_MOVE ? "move" : "",
824 context->action == GDK_ACTION_COPY ? "copy" : "",
827 /* FIXME: This is ambigous, an id can come from multiple accounts */
828 manager = empathy_contact_manager_new ();
829 contact = empathy_contact_list_find (EMPATHY_CONTACT_LIST (manager), id);
830 g_object_unref (manager);
833 gossip_debug (DEBUG_DOMAIN, "No contact found associated with drag & drop");
837 model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
839 /* Get source group information. */
840 if (priv->drag_row) {
841 path = gtk_tree_row_reference_get_path (priv->drag_row);
843 old_group = gossip_contact_list_store_get_parent_group (model, path, NULL);
844 gtk_tree_path_free (path);
848 /* Get destination group information. */
849 is_row = gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget),
856 new_group = gossip_contact_list_store_get_parent_group (model, path, NULL);
857 gtk_tree_path_free (path);
860 gossip_debug (DEBUG_DOMAIN,
861 "contact '%s' dragged from '%s' to '%s'",
862 gossip_contact_get_name (contact),
863 old_group, new_group);
865 if (priv->drag_received) {
866 priv->drag_received (contact,
870 priv->drag_received_data);
872 contact_list_view_contact_received (GOSSIP_CONTACT_LIST_VIEW (widget),
882 gtk_drag_finish (context, TRUE, FALSE, GDK_CURRENT_TIME);
886 contact_list_view_drag_motion (GtkWidget *widget,
887 GdkDragContext *context,
893 static DragMotionData *dm = NULL;
896 gboolean is_different = FALSE;
897 gboolean cleanup = TRUE;
899 is_row = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
910 cleanup &= (dm && gtk_tree_path_compare (dm->path, path) != 0);
911 is_different = (!dm || (dm && gtk_tree_path_compare (dm->path, path) != 0));
916 if (!is_different && !cleanup) {
921 gtk_tree_path_free (dm->path);
922 if (dm->timeout_id) {
923 g_source_remove (dm->timeout_id);
931 if (!gtk_tree_view_row_expanded (GTK_TREE_VIEW (widget), path)) {
932 dm = g_new0 (DragMotionData, 1);
934 dm->view = GOSSIP_CONTACT_LIST_VIEW (widget);
935 dm->path = gtk_tree_path_copy (path);
937 dm->timeout_id = g_timeout_add (
939 (GSourceFunc) contact_list_view_drag_motion_cb,
947 contact_list_view_drag_motion_cb (DragMotionData *data)
949 gtk_tree_view_expand_row (GTK_TREE_VIEW (data->view),
953 data->timeout_id = 0;
959 contact_list_view_drag_begin (GtkWidget *widget,
960 GdkDragContext *context,
963 GossipContactListViewPriv *priv;
964 GtkTreeSelection *selection;
969 priv = GET_PRIV (widget);
971 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (widget));
972 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
976 path = gtk_tree_model_get_path (model, &iter);
977 priv->drag_row = gtk_tree_row_reference_new (model, path);
978 gtk_tree_path_free (path);
982 contact_list_view_drag_data_get (GtkWidget *widget,
983 GdkDragContext *context,
984 GtkSelectionData *selection,
989 GossipContactListViewPriv *priv;
990 GtkTreePath *src_path;
993 GossipContact *contact;
996 priv = GET_PRIV (widget);
998 model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
999 if (!priv->drag_row) {
1003 src_path = gtk_tree_row_reference_get_path (priv->drag_row);
1008 if (!gtk_tree_model_get_iter (model, &iter, src_path)) {
1009 gtk_tree_path_free (src_path);
1013 gtk_tree_path_free (src_path);
1015 contact = gossip_contact_list_view_get_selected (GOSSIP_CONTACT_LIST_VIEW (widget));
1020 id = gossip_contact_get_id (contact);
1021 g_object_unref (contact);
1024 case DND_DRAG_TYPE_CONTACT_ID:
1025 gtk_selection_data_set (selection, drag_atoms_source[info], 8,
1026 (guchar*)id, strlen (id) + 1);
1035 contact_list_view_drag_end (GtkWidget *widget,
1036 GdkDragContext *context,
1039 GossipContactListViewPriv *priv;
1041 priv = GET_PRIV (widget);
1043 if (priv->drag_row) {
1044 gtk_tree_row_reference_free (priv->drag_row);
1045 priv->drag_row = NULL;
1050 contact_list_view_cell_set_background (GossipContactListView *view,
1051 GtkCellRenderer *cell,
1058 style = gtk_widget_get_style (GTK_WIDGET (view));
1062 color = style->bg[GTK_STATE_SELECTED];
1064 /* Here we take the current theme colour and add it to
1065 * the colour for white and average the two. This
1066 * gives a colour which is inline with the theme but
1069 color.red = (color.red + (style->white).red) / 2;
1070 color.green = (color.green + (style->white).green) / 2;
1071 color.blue = (color.blue + (style->white).blue) / 2;
1074 "cell-background-gdk", &color,
1078 "cell-background-gdk", NULL,
1083 "cell-background-gdk", NULL,
1089 contact_list_view_pixbuf_cell_data_func (GtkTreeViewColumn *tree_column,
1090 GtkCellRenderer *cell,
1091 GtkTreeModel *model,
1093 GossipContactListView *view)
1099 gtk_tree_model_get (model, iter,
1100 COL_IS_GROUP, &is_group,
1101 COL_IS_ACTIVE, &is_active,
1102 COL_ICON_STATUS, &icon_name,
1106 "visible", !is_group,
1107 "icon-name", icon_name,
1112 contact_list_view_cell_set_background (view, cell, is_group, is_active);
1116 contact_list_view_avatar_cell_data_func (GtkTreeViewColumn *tree_column,
1117 GtkCellRenderer *cell,
1118 GtkTreeModel *model,
1120 GossipContactListView *view)
1123 gboolean show_avatar;
1127 gtk_tree_model_get (model, iter,
1128 COL_PIXBUF_AVATAR, &pixbuf,
1129 COL_PIXBUF_AVATAR_VISIBLE, &show_avatar,
1130 COL_IS_GROUP, &is_group,
1131 COL_IS_ACTIVE, &is_active,
1135 "visible", !is_group && show_avatar,
1140 g_object_unref (pixbuf);
1143 contact_list_view_cell_set_background (view, cell, is_group, is_active);
1147 contact_list_view_text_cell_data_func (GtkTreeViewColumn *tree_column,
1148 GtkCellRenderer *cell,
1149 GtkTreeModel *model,
1151 GossipContactListView *view)
1155 gboolean show_status;
1157 gtk_tree_model_get (model, iter,
1158 COL_IS_GROUP, &is_group,
1159 COL_IS_ACTIVE, &is_active,
1160 COL_STATUS_VISIBLE, &show_status,
1164 "show-status", show_status,
1167 contact_list_view_cell_set_background (view, cell, is_group, is_active);
1171 contact_list_view_expander_cell_data_func (GtkTreeViewColumn *column,
1172 GtkCellRenderer *cell,
1173 GtkTreeModel *model,
1175 GossipContactListView *view)
1180 gtk_tree_model_get (model, iter,
1181 COL_IS_GROUP, &is_group,
1182 COL_IS_ACTIVE, &is_active,
1185 if (gtk_tree_model_iter_has_child (model, iter)) {
1187 gboolean row_expanded;
1189 path = gtk_tree_model_get_path (model, iter);
1190 row_expanded = gtk_tree_view_row_expanded (GTK_TREE_VIEW (column->tree_view), path);
1191 gtk_tree_path_free (path);
1195 "expander-style", row_expanded ? GTK_EXPANDER_EXPANDED : GTK_EXPANDER_COLLAPSED,
1198 g_object_set (cell, "visible", FALSE, NULL);
1201 contact_list_view_cell_set_background (view, cell, is_group, is_active);
1205 contact_list_view_get_contact_menu (GossipContactListView *view,
1206 gboolean can_send_file,
1207 gboolean can_show_log)
1209 GossipContactListViewPriv *priv;
1213 priv = GET_PRIV (view);
1215 /* Sort out sensitive items */
1216 action = gtk_ui_manager_get_action (priv->ui, "/Contact/Log");
1217 gtk_action_set_sensitive (action, can_show_log);
1219 action = gtk_ui_manager_get_action (priv->ui, "/Contact/SendFile");
1220 gtk_action_set_visible (action, can_send_file);
1222 widget = gtk_ui_manager_get_widget (priv->ui, "/Contact");
1228 contact_list_view_button_press_event_cb (GossipContactListView *view,
1229 GdkEventButton *event,
1232 GossipContactListViewPriv *priv;
1233 GossipContact *contact;
1235 GtkTreeSelection *selection;
1236 GtkTreeModel *model;
1238 gboolean row_exists;
1241 if (event->button != 3) {
1245 priv = GET_PRIV (view);
1247 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
1248 model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
1250 gtk_widget_grab_focus (GTK_WIDGET (view));
1252 row_exists = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (view),
1260 gtk_tree_selection_unselect_all (selection);
1261 gtk_tree_selection_select_path (selection, path);
1263 gtk_tree_model_get_iter (model, &iter, path);
1264 gtk_tree_path_free (path);
1266 gtk_tree_model_get (model, &iter, COL_CONTACT, &contact, -1);
1269 menu = gossip_contact_list_view_get_contact_menu (view, contact);
1270 g_object_unref (contact);
1272 menu = gossip_contact_list_view_get_group_menu (view);
1279 gtk_widget_show (menu);
1281 gtk_menu_popup (GTK_MENU (menu),
1282 NULL, NULL, NULL, NULL,
1283 event->button, event->time);
1289 contact_list_view_row_activated_cb (GossipContactListView *view,
1291 GtkTreeViewColumn *col,
1294 GossipContact *contact;
1295 GtkTreeModel *model;
1298 model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
1300 gtk_tree_model_get_iter (model, &iter, path);
1301 gtk_tree_model_get (model, &iter, COL_CONTACT, &contact, -1);
1304 contact_list_view_action_activated (view, contact);
1305 g_object_unref (contact);
1310 contact_list_view_row_expand_or_collapse_cb (GossipContactListView *view,
1315 GtkTreeModel *model;
1319 model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
1321 gtk_tree_model_get (model, iter,
1325 expanded = GPOINTER_TO_INT (user_data);
1326 gossip_contact_group_set_expanded (name, expanded);
1332 contact_list_view_filter_show_contact (GossipContact *contact,
1333 const gchar *filter)
1338 /* Check contact id */
1339 str = g_utf8_casefold (gossip_contact_get_id (contact), -1);
1340 visible = G_STR_EMPTY (str) || strstr (str, filter);
1347 /* Check contact name */
1348 str = g_utf8_casefold (gossip_contact_get_name (contact), -1);
1349 visible = G_STR_EMPTY (str) || strstr (str, filter);
1356 contact_list_view_filter_show_group (GossipContactListView *view,
1358 const gchar *filter)
1360 GossipContactListViewPriv *priv;
1361 EmpathyContactList *list;
1362 GList *contacts, *l;
1364 gboolean show_group = FALSE;
1366 priv = GET_PRIV (view);
1368 str = g_utf8_casefold (group, -1);
1373 /* If the filter is the partially the group name, we show the
1376 if (strstr (str, filter)) {
1381 /* At this point, we need to check in advance if this
1382 * group should be shown because a contact we want to
1383 * show exists in it.
1385 list = gossip_contact_list_store_get_list_iface (priv->store);
1386 contacts = empathy_contact_list_get_contacts (list);
1387 for (l = contacts; l && !show_group; l = l->next) {
1388 if (!gossip_contact_is_in_group (l->data, group)) {
1392 if (contact_list_view_filter_show_contact (l->data, filter)) {
1396 g_list_foreach (contacts, (GFunc) g_object_unref, NULL);
1397 g_list_free (contacts);
1404 contact_list_view_filter_func (GtkTreeModel *model,
1406 GossipContactListView *view)
1408 GossipContactListViewPriv *priv;
1410 gboolean is_separator;
1411 gboolean visible = TRUE;
1413 priv = GET_PRIV (view);
1415 if (G_STR_EMPTY (priv->filter_text)) {
1419 /* Check to see if iter matches any group names */
1420 gtk_tree_model_get (model, iter,
1421 COL_IS_GROUP, &is_group,
1422 COL_IS_SEPARATOR, &is_separator,
1428 gtk_tree_model_get (model, iter, COL_NAME, &name, -1);
1429 visible &= contact_list_view_filter_show_group (view,
1433 } else if (is_separator) {
1434 /* Do nothing here */
1436 GossipContact *contact;
1438 /* Check contact id */
1439 gtk_tree_model_get (model, iter, COL_CONTACT, &contact, -1);
1440 visible &= contact_list_view_filter_show_contact (contact,
1442 g_object_unref (contact);
1449 contact_list_view_action_cb (GtkAction *action,
1450 GossipContactListView *view)
1452 GossipContact *contact;
1456 name = gtk_action_get_name (action);
1461 gossip_debug (DEBUG_DOMAIN, "Action:'%s' activated", name);
1463 contact = gossip_contact_list_view_get_selected (view);
1464 group = gossip_contact_list_view_get_selected_group (view);
1466 if (contact && strcmp (name, "Chat") == 0) {
1467 contact_list_view_action_activated (view, contact);
1469 else if (contact && strcmp (name, "Information") == 0) {
1471 else if (contact && strcmp (name, "Edit") == 0) {
1473 else if (contact && strcmp (name, "Remove") == 0) {
1475 else if (contact && strcmp (name, "Invite") == 0) {
1477 else if (contact && strcmp (name, "SendFile") == 0) {
1479 else if (contact && strcmp (name, "Log") == 0) {
1481 else if (group && strcmp (name, "Rename") == 0) {
1486 g_object_unref (contact);
1491 contact_list_view_action_activated (GossipContactListView *view,
1492 GossipContact *contact)
1496 mc = gossip_mission_control_new ();
1497 mission_control_request_channel (mc,
1498 gossip_contact_get_account (contact),
1499 TP_IFACE_CHANNEL_TYPE_TEXT,
1500 gossip_contact_get_handle (contact),
1501 TP_HANDLE_TYPE_CONTACT,
1503 g_object_unref (mc);