]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-contact-list-view.c
Completely reworked ContactList API. Fixes bug #471611, bug #467280, bug #459540...
[empathy.git] / libempathy-gtk / empathy-contact-list-view.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2005-2007 Imendio AB
4  * Copyright (C) 2007 Collabora Ltd.
5  *
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.
10  *
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.
15  *
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.
20  *
21  * Authors: Mikael Hallendal <micke@imendio.com>
22  *          Martyn Russell <martyn@imendio.com>
23  *          Xavier Claessens <xclaesse@gmail.com>
24  */
25
26 #include "config.h"
27
28 #include <string.h>
29
30 #include <glib/gi18n.h>
31 #include <gtk/gtk.h>
32 #include <glade/glade.h>
33
34 #include <libmissioncontrol/mc-account.h>
35 #include <libmissioncontrol/mission-control.h>
36
37 #include <libempathy/empathy-contact-factory.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>
43
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"
55
56 #define DEBUG_DOMAIN "ContactListView"
57
58 /* Flashing delay for icons (milliseconds). */
59 #define FLASH_TIMEOUT 500
60
61 /* Active users are those which have recently changed state
62  * (e.g. online, offline or from normal to a busy state).
63  */
64
65 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_CONTACT_LIST_VIEW, EmpathyContactListViewPriv))
66
67 struct _EmpathyContactListViewPriv {
68         EmpathyContactListStore *store;
69         GtkUIManager            *ui;
70         GtkTreeRowReference     *drag_row;
71 };
72
73 typedef struct {
74         EmpathyContactListView *view;
75         GtkTreePath           *path;
76         guint                  timeout_id;
77 } DragMotionData;
78
79 typedef struct {
80         EmpathyContactListView *view;
81         EmpathyContact         *contact;
82         gboolean               remove;
83 } ShowActiveData;
84
85 static void        empathy_contact_list_view_class_init         (EmpathyContactListViewClass *klass);
86 static void        empathy_contact_list_view_init               (EmpathyContactListView      *list);
87 static void        contact_list_view_finalize                  (GObject                    *object);
88 static void        contact_list_view_get_property              (GObject                    *object,
89                                                                 guint                       param_id,
90                                                                 GValue                     *value,
91                                                                 GParamSpec                 *pspec);
92 static void        contact_list_view_set_property              (GObject                    *object,
93                                                                 guint                       param_id,
94                                                                 const GValue               *value,
95                                                                 GParamSpec                 *pspec);
96 static void        contact_list_view_setup                     (EmpathyContactListView      *view);
97 static void        contact_list_view_row_has_child_toggled_cb  (GtkTreeModel               *model,
98                                                                 GtkTreePath                *path,
99                                                                 GtkTreeIter                *iter,
100                                                                 EmpathyContactListView      *view);
101 static void        contact_list_view_drag_data_received        (GtkWidget                  *widget,
102                                                                 GdkDragContext             *context,
103                                                                 gint                        x,
104                                                                 gint                        y,
105                                                                 GtkSelectionData           *selection,
106                                                                 guint                       info,
107                                                                 guint                       time);
108 static gboolean    contact_list_view_drag_motion               (GtkWidget                  *widget,
109                                                                 GdkDragContext             *context,
110                                                                 gint                        x,
111                                                                 gint                        y,
112                                                                 guint                       time);
113 static gboolean    contact_list_view_drag_motion_cb            (DragMotionData             *data);
114 static void        contact_list_view_drag_begin                (GtkWidget                  *widget,
115                                                                 GdkDragContext             *context);
116 static void        contact_list_view_drag_data_get             (GtkWidget                  *widget,
117                                                                 GdkDragContext             *context,
118                                                                 GtkSelectionData           *selection,
119                                                                 guint                       info,
120                                                                 guint                       time);
121 static void        contact_list_view_drag_end                  (GtkWidget                  *widget,
122                                                                 GdkDragContext             *context);
123 static gboolean    contact_list_view_drag_drop                 (GtkWidget                  *widget,
124                                                                 GdkDragContext             *drag_context,
125                                                                 gint                        x,
126                                                                 gint                        y,
127                                                                 guint                       time);
128 static void        contact_list_view_cell_set_background       (EmpathyContactListView      *view,
129                                                                 GtkCellRenderer            *cell,
130                                                                 gboolean                    is_group,
131                                                                 gboolean                    is_active);
132 static void        contact_list_view_pixbuf_cell_data_func     (GtkTreeViewColumn          *tree_column,
133                                                                 GtkCellRenderer            *cell,
134                                                                 GtkTreeModel               *model,
135                                                                 GtkTreeIter                *iter,
136                                                                 EmpathyContactListView      *view);
137 static void        contact_list_view_avatar_cell_data_func     (GtkTreeViewColumn          *tree_column,
138                                                                 GtkCellRenderer            *cell,
139                                                                 GtkTreeModel               *model,
140                                                                 GtkTreeIter                *iter,
141                                                                 EmpathyContactListView      *view);
142 static void        contact_list_view_text_cell_data_func       (GtkTreeViewColumn          *tree_column,
143                                                                 GtkCellRenderer            *cell,
144                                                                 GtkTreeModel               *model,
145                                                                 GtkTreeIter                *iter,
146                                                                 EmpathyContactListView      *view);
147 static void        contact_list_view_expander_cell_data_func   (GtkTreeViewColumn          *column,
148                                                                 GtkCellRenderer            *cell,
149                                                                 GtkTreeModel               *model,
150                                                                 GtkTreeIter                *iter,
151                                                                 EmpathyContactListView      *view);
152 static GtkWidget * contact_list_view_get_contact_menu          (EmpathyContactListView      *view,
153                                                                 gboolean                    can_send_file,
154                                                                 gboolean                    can_show_log);
155 static gboolean    contact_list_view_button_press_event_cb     (EmpathyContactListView      *view,
156                                                                 GdkEventButton             *event,
157                                                                 gpointer                    user_data);
158 static void        contact_list_view_row_activated_cb          (EmpathyContactListView      *view,
159                                                                 GtkTreePath                *path,
160                                                                 GtkTreeViewColumn          *col,
161                                                                 gpointer                    user_data);
162 static void        contact_list_view_row_expand_or_collapse_cb (EmpathyContactListView      *view,
163                                                                 GtkTreeIter                *iter,
164                                                                 GtkTreePath                *path,
165                                                                 gpointer                    user_data);
166 static void        contact_list_view_action_cb                 (GtkAction                  *action,
167                                                                 EmpathyContactListView      *view);
168 static void        contact_list_view_action_activated          (EmpathyContactListView      *view,
169                                                                 EmpathyContact              *contact);
170
171 enum {
172         PROP_0,
173 };
174
175 static const GtkActionEntry entries[] = {
176         { "ContactMenu", NULL,
177           N_("_Contact"), NULL, NULL,
178           NULL
179         },
180         { "GroupMenu", NULL,
181           N_("_Group"),NULL, NULL,
182           NULL
183         },
184         { "Chat", EMPATHY_IMAGE_MESSAGE,
185           N_("_Chat"), NULL, N_("Chat with contact"),
186           G_CALLBACK (contact_list_view_action_cb)
187         },
188         { "Information", EMPATHY_IMAGE_CONTACT_INFORMATION,
189           N_("Infor_mation"), "<control>I", N_("View contact information"),
190           G_CALLBACK (contact_list_view_action_cb)
191         },
192         { "Rename", NULL,
193           N_("Re_name"), NULL, N_("Rename"),
194           G_CALLBACK (contact_list_view_action_cb)
195         },
196         { "Edit", GTK_STOCK_EDIT,
197           N_("_Edit"), NULL, N_("Edit the groups and name for this contact"),
198           G_CALLBACK (contact_list_view_action_cb)
199         },
200         { "Remove", GTK_STOCK_REMOVE,
201           N_("_Remove"), NULL, N_("Remove contact"),
202           G_CALLBACK (contact_list_view_action_cb)
203         },
204         { "Invite", EMPATHY_IMAGE_GROUP_MESSAGE,
205           N_("_Invite to Chat Room"), NULL, N_("Invite to a currently open chat room"),
206           G_CALLBACK (contact_list_view_action_cb)
207         },
208         { "SendFile", NULL,
209           N_("_Send File..."), NULL, N_("Send a file"),
210           G_CALLBACK (contact_list_view_action_cb)
211         },
212         { "Log", GTK_STOCK_JUSTIFY_LEFT,
213           N_("_View Previous Conversations"), NULL, N_("View previous conversations with this contact"),
214           G_CALLBACK (contact_list_view_action_cb)
215         },
216 };
217
218 static guint n_entries = G_N_ELEMENTS (entries);
219
220 static const gchar *ui_info =
221         "<ui>"
222         "  <popup name='Contact'>"
223         "    <menuitem action='Chat'/>"
224         "    <menuitem action='Log'/>"
225         "    <menuitem action='SendFile'/>"
226         "    <separator/>"
227         "    <menuitem action='Invite'/>"
228         "    <separator/>"
229         "    <menuitem action='Edit'/>"
230         "    <menuitem action='Remove'/>"
231         "    <separator/>"
232         "    <menuitem action='Information'/>"
233         "  </popup>"
234         "  <popup name='Group'>"
235         "    <menuitem action='Rename'/>"
236         "  </popup>"
237         "</ui>";
238
239 enum DndDragType {
240         DND_DRAG_TYPE_CONTACT_ID,
241         DND_DRAG_TYPE_URL,
242         DND_DRAG_TYPE_STRING,
243 };
244
245 static const GtkTargetEntry drag_types_dest[] = {
246         { "text/contact-id", 0, DND_DRAG_TYPE_CONTACT_ID },
247         { "text/uri-list",   0, DND_DRAG_TYPE_URL },
248         { "text/plain",      0, DND_DRAG_TYPE_STRING },
249         { "STRING",          0, DND_DRAG_TYPE_STRING },
250 };
251
252 static const GtkTargetEntry drag_types_source[] = {
253         { "text/contact-id", 0, DND_DRAG_TYPE_CONTACT_ID },
254 };
255
256 static GdkAtom drag_atoms_dest[G_N_ELEMENTS (drag_types_dest)];
257 static GdkAtom drag_atoms_source[G_N_ELEMENTS (drag_types_source)];
258
259 enum {
260         DRAG_CONTACT_RECEIVED,
261         LAST_SIGNAL
262 };
263
264 static guint signals[LAST_SIGNAL];
265
266 G_DEFINE_TYPE (EmpathyContactListView, empathy_contact_list_view, GTK_TYPE_TREE_VIEW);
267
268 static void
269 empathy_contact_list_view_class_init (EmpathyContactListViewClass *klass)
270 {
271         GObjectClass   *object_class = G_OBJECT_CLASS (klass);
272         GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
273
274         object_class->finalize = contact_list_view_finalize;
275         object_class->get_property = contact_list_view_get_property;
276         object_class->set_property = contact_list_view_set_property;
277
278         widget_class->drag_data_received = contact_list_view_drag_data_received;
279         widget_class->drag_drop          = contact_list_view_drag_drop;
280         widget_class->drag_begin         = contact_list_view_drag_begin;
281         widget_class->drag_data_get      = contact_list_view_drag_data_get;
282         widget_class->drag_end           = contact_list_view_drag_end;
283         /* FIXME: noticed but when you drag the row over the treeview
284          * fast, it seems to stop redrawing itself, if we don't
285          * connect this signal, all is fine.
286          */
287         widget_class->drag_motion        = contact_list_view_drag_motion;
288
289         signals[DRAG_CONTACT_RECEIVED] =
290                 g_signal_new ("drag-contact-received",
291                               G_OBJECT_CLASS_TYPE (klass),
292                               G_SIGNAL_RUN_LAST,
293                               0,
294                               NULL, NULL,
295                               empathy_marshal_VOID__OBJECT_STRING_STRING,
296                               G_TYPE_NONE,
297                               3, EMPATHY_TYPE_CONTACT, G_TYPE_STRING, G_TYPE_STRING);
298
299         g_type_class_add_private (object_class, sizeof (EmpathyContactListViewPriv));
300 }
301
302 static void
303 empathy_contact_list_view_init (EmpathyContactListView *view)
304 {
305         EmpathyContactListViewPriv *priv;
306         GtkActionGroup            *action_group;
307         GError                    *error = NULL;
308
309         priv = GET_PRIV (view);
310
311         /* Get saved group states. */
312         empathy_contact_groups_get_all ();
313
314         /* Set up UI Manager */
315         priv->ui = gtk_ui_manager_new ();
316
317         action_group = gtk_action_group_new ("Actions");
318         gtk_action_group_set_translation_domain (action_group, GETTEXT_PACKAGE);
319         gtk_action_group_add_actions (action_group, entries, n_entries, view);
320         gtk_ui_manager_insert_action_group (priv->ui, action_group, 0);
321
322         if (!gtk_ui_manager_add_ui_from_string (priv->ui, ui_info, -1, &error)) {
323                 g_warning ("Could not build contact menus from string:'%s'", error->message);
324                 g_error_free (error);
325         }
326
327         g_object_unref (action_group);
328
329         gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (view), 
330                                               empathy_contact_list_store_row_separator_func,
331                                               NULL, NULL);
332
333         /* Connect to tree view signals rather than override. */
334         g_signal_connect (view,
335                           "button-press-event",
336                           G_CALLBACK (contact_list_view_button_press_event_cb),
337                           NULL);
338         g_signal_connect (view,
339                           "row-activated",
340                           G_CALLBACK (contact_list_view_row_activated_cb),
341                           NULL);
342         g_signal_connect (view,
343                           "row-expanded",
344                           G_CALLBACK (contact_list_view_row_expand_or_collapse_cb),
345                           GINT_TO_POINTER (TRUE));
346         g_signal_connect (view,
347                           "row-collapsed",
348                           G_CALLBACK (contact_list_view_row_expand_or_collapse_cb),
349                           GINT_TO_POINTER (FALSE));
350 }
351
352 static void
353 contact_list_view_finalize (GObject *object)
354 {
355         EmpathyContactListViewPriv *priv;
356
357         priv = GET_PRIV (object);
358
359         if (priv->ui) {
360                 g_object_unref (priv->ui);
361         }
362         if (priv->store) {
363                 g_object_unref (priv->store);
364         }
365
366         G_OBJECT_CLASS (empathy_contact_list_view_parent_class)->finalize (object);
367 }
368
369 static void
370 contact_list_view_get_property (GObject    *object,
371                                 guint       param_id,
372                                 GValue     *value,
373                                 GParamSpec *pspec)
374 {
375         EmpathyContactListViewPriv *priv;
376
377         priv = GET_PRIV (object);
378
379         switch (param_id) {
380         default:
381                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
382                 break;
383         };
384 }
385
386 static void
387 contact_list_view_set_property (GObject      *object,
388                                 guint         param_id,
389                                 const GValue *value,
390                                 GParamSpec   *pspec)
391 {
392         EmpathyContactListViewPriv *priv;
393
394         priv = GET_PRIV (object);
395
396         switch (param_id) {
397         default:
398                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
399                 break;
400         };
401 }
402
403 EmpathyContactListView *
404 empathy_contact_list_view_new (EmpathyContactListStore *store)
405 {
406         EmpathyContactListViewPriv *priv;
407         EmpathyContactListView     *view;
408         
409         view = g_object_new (EMPATHY_TYPE_CONTACT_LIST_VIEW, NULL);
410         priv = GET_PRIV (view);
411
412         priv->store = g_object_ref (store);
413         contact_list_view_setup (view);
414
415         return view;
416 }
417
418 EmpathyContact *
419 empathy_contact_list_view_get_selected (EmpathyContactListView *view)
420 {
421         EmpathyContactListViewPriv *priv;
422         GtkTreeSelection          *selection;
423         GtkTreeIter                iter;
424         GtkTreeModel              *model;
425         EmpathyContact             *contact;
426
427         g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), NULL);
428
429         priv = GET_PRIV (view);
430
431         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
432         if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
433                 return NULL;
434         }
435
436         gtk_tree_model_get (model, &iter, COL_CONTACT, &contact, -1);
437
438         return contact;
439 }
440
441 gchar *
442 empathy_contact_list_view_get_selected_group (EmpathyContactListView *view)
443 {
444         EmpathyContactListViewPriv *priv;
445         GtkTreeSelection          *selection;
446         GtkTreeIter                iter;
447         GtkTreeModel              *model;
448         gboolean                   is_group;
449         gchar                     *name;
450
451         g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), NULL);
452
453         priv = GET_PRIV (view);
454
455         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
456         if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
457                 return NULL;
458         }
459
460         gtk_tree_model_get (model, &iter,
461                             COL_IS_GROUP, &is_group,
462                             COL_NAME, &name,
463                             -1);
464
465         if (!is_group) {
466                 g_free (name);
467                 return NULL;
468         }
469
470         return name;
471 }
472
473 GtkWidget *
474 empathy_contact_list_view_get_group_menu (EmpathyContactListView *view)
475 {
476         EmpathyContactListViewPriv *priv;
477         GtkWidget                 *widget;
478
479         g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), NULL);
480
481         priv = GET_PRIV (view);
482
483         widget = gtk_ui_manager_get_widget (priv->ui, "/Group");
484
485         return widget;
486 }
487
488 GtkWidget *
489 empathy_contact_list_view_get_contact_menu (EmpathyContactListView *view,
490                                            EmpathyContact         *contact)
491 {
492         EmpathyLogManager *log_manager;
493         gboolean           can_show_log;
494         gboolean           can_send_file;
495
496         g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), NULL);
497         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
498
499         log_manager = empathy_log_manager_new ();
500         can_show_log = empathy_log_manager_exists (log_manager,
501                                                    empathy_contact_get_account (contact),
502                                                    empathy_contact_get_id (contact),
503                                                    FALSE);
504         can_send_file = FALSE;
505         g_object_unref (log_manager);
506
507         return contact_list_view_get_contact_menu (view,
508                                                    can_send_file,
509                                                    can_show_log);
510 }
511
512 static void
513 contact_list_view_setup (EmpathyContactListView *view)
514 {
515         EmpathyContactListViewPriv *priv;
516         GtkCellRenderer           *cell;
517         GtkTreeViewColumn         *col;
518         gint                       i;
519
520         priv = GET_PRIV (view);
521
522         g_signal_connect (priv->store, "row-has-child-toggled",
523                           G_CALLBACK (contact_list_view_row_has_child_toggled_cb),
524                           view);
525         gtk_tree_view_set_model (GTK_TREE_VIEW (view),
526                                  GTK_TREE_MODEL (priv->store));
527
528         /* Setup view */
529         g_object_set (view,
530                       "headers-visible", FALSE,
531                       "reorderable", TRUE,
532                       "show-expanders", FALSE,
533                       NULL);
534
535         col = gtk_tree_view_column_new ();
536
537         /* State */
538         cell = gtk_cell_renderer_pixbuf_new ();
539         gtk_tree_view_column_pack_start (col, cell, FALSE);
540         gtk_tree_view_column_set_cell_data_func (
541                 col, cell,
542                 (GtkTreeCellDataFunc) contact_list_view_pixbuf_cell_data_func,
543                 view, NULL);
544
545         g_object_set (cell,
546                       "xpad", 5,
547                       "ypad", 1,
548                       "visible", FALSE,
549                       NULL);
550
551         /* Name */
552         cell = empathy_cell_renderer_text_new ();
553         gtk_tree_view_column_pack_start (col, cell, TRUE);
554         gtk_tree_view_column_set_cell_data_func (
555                 col, cell,
556                 (GtkTreeCellDataFunc) contact_list_view_text_cell_data_func,
557                 view, NULL);
558
559         gtk_tree_view_column_add_attribute (col, cell,
560                                             "name", COL_NAME);
561         gtk_tree_view_column_add_attribute (col, cell,
562                                             "status", COL_STATUS);
563         gtk_tree_view_column_add_attribute (col, cell,
564                                             "is_group", COL_IS_GROUP);
565
566         /* Avatar */
567         cell = gtk_cell_renderer_pixbuf_new ();
568         gtk_tree_view_column_pack_start (col, cell, FALSE);
569         gtk_tree_view_column_set_cell_data_func (
570                 col, cell,
571                 (GtkTreeCellDataFunc) contact_list_view_avatar_cell_data_func,
572                 view, NULL);
573
574         g_object_set (cell,
575                       "xpad", 0,
576                       "ypad", 0,
577                       "visible", FALSE,
578                       "width", 32,
579                       "height", 32,
580                       NULL);
581
582         /* Expander */
583         cell = empathy_cell_renderer_expander_new ();
584         gtk_tree_view_column_pack_end (col, cell, FALSE);
585         gtk_tree_view_column_set_cell_data_func (
586                 col, cell,
587                 (GtkTreeCellDataFunc) contact_list_view_expander_cell_data_func,
588                 view, NULL);
589
590         /* Actually add the column now we have added all cell renderers */
591         gtk_tree_view_append_column (GTK_TREE_VIEW (view), col);
592
593         /* Drag & Drop. */
594         for (i = 0; i < G_N_ELEMENTS (drag_types_dest); ++i) {
595                 drag_atoms_dest[i] = gdk_atom_intern (drag_types_dest[i].target,
596                                                       FALSE);
597         }
598
599         for (i = 0; i < G_N_ELEMENTS (drag_types_source); ++i) {
600                 drag_atoms_source[i] = gdk_atom_intern (drag_types_source[i].target,
601                                                         FALSE);
602         }
603
604         /* Note: We support the COPY action too, but need to make the
605          * MOVE action the default.
606          */
607         gtk_drag_source_set (GTK_WIDGET (view),
608                              GDK_BUTTON1_MASK,
609                              drag_types_source,
610                              G_N_ELEMENTS (drag_types_source),
611                              GDK_ACTION_MOVE | GDK_ACTION_COPY);
612
613         gtk_drag_dest_set (GTK_WIDGET (view),
614                            GTK_DEST_DEFAULT_ALL,
615                            drag_types_dest,
616                            G_N_ELEMENTS (drag_types_dest),
617                            GDK_ACTION_MOVE | GDK_ACTION_COPY);
618 }
619
620 static void
621 contact_list_view_row_has_child_toggled_cb (GtkTreeModel          *model,
622                                             GtkTreePath           *path,
623                                             GtkTreeIter           *iter,
624                                             EmpathyContactListView *view)
625 {
626         gboolean  is_group = FALSE;
627         gchar    *name = NULL;
628
629         gtk_tree_model_get (model, iter,
630                             COL_IS_GROUP, &is_group,
631                             COL_NAME, &name,
632                             -1);
633
634         if (!is_group || G_STR_EMPTY (name)) {
635                 g_free (name);
636                 return;
637         }
638
639         if (empathy_contact_group_get_expanded (name)) {
640                 g_signal_handlers_block_by_func (view,
641                                                  contact_list_view_row_expand_or_collapse_cb,
642                                                  GINT_TO_POINTER (TRUE));
643                 gtk_tree_view_expand_row (GTK_TREE_VIEW (view), path, TRUE);
644                 g_signal_handlers_unblock_by_func (view,
645                                                    contact_list_view_row_expand_or_collapse_cb,
646                                                    GINT_TO_POINTER (TRUE));
647         } else {
648                 g_signal_handlers_block_by_func (view,
649                                                  contact_list_view_row_expand_or_collapse_cb,
650                                                  GINT_TO_POINTER (FALSE));
651                 gtk_tree_view_collapse_row (GTK_TREE_VIEW (view), path);
652                 g_signal_handlers_unblock_by_func (view,
653                                                    contact_list_view_row_expand_or_collapse_cb,
654                                                    GINT_TO_POINTER (FALSE));
655         }
656
657         g_free (name);
658 }
659
660 static void
661 contact_list_view_drag_data_received (GtkWidget         *widget,
662                                       GdkDragContext    *context,
663                                       gint               x,
664                                       gint               y,
665                                       GtkSelectionData  *selection,
666                                       guint              info,
667                                       guint              time)
668 {
669         EmpathyContactListViewPriv *priv;
670         EmpathyContactList         *list;
671         EmpathyContactFactory      *factory;
672         McAccount                  *account;
673         GtkTreeModel               *model;
674         GtkTreePath                *path;
675         GtkTreeViewDropPosition     position;
676         EmpathyContact             *contact = NULL;
677         const gchar                *id;
678         gchar                     **strv;
679         gchar                      *new_group = NULL;
680         gchar                      *old_group = NULL;
681         gboolean                    is_row;
682
683         priv = GET_PRIV (widget);
684
685         id = (const gchar*) selection->data;
686         empathy_debug (DEBUG_DOMAIN, "Received %s%s drag & drop contact from roster with id:'%s'",
687                       context->action == GDK_ACTION_MOVE ? "move" : "",
688                       context->action == GDK_ACTION_COPY ? "copy" : "",
689                       id);
690
691         strv = g_strsplit (id, "/", 2);
692         factory = empathy_contact_factory_new ();
693         account = mc_account_lookup (strv[0]);
694         if (account) {
695                 contact = empathy_contact_factory_get_from_id (factory,
696                                                                account,
697                                                                strv[1]);
698                 g_object_unref (account);
699         }
700         g_object_unref (factory);
701         g_object_unref (account);
702         g_strfreev (strv);
703
704         if (!contact) {
705                 empathy_debug (DEBUG_DOMAIN, "No contact found associated with drag & drop");
706                 return;
707         }
708
709         model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
710
711         /* Get source group information. */
712         if (priv->drag_row) {
713                 path = gtk_tree_row_reference_get_path (priv->drag_row);
714                 if (path) {
715                         old_group = empathy_contact_list_store_get_parent_group (model, path, NULL);
716                         gtk_tree_path_free (path);
717                 }
718         }
719
720         /* Get destination group information. */
721         is_row = gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget),
722                                                     x,
723                                                     y,
724                                                     &path,
725                                                     &position);
726
727         if (is_row) {
728                 new_group = empathy_contact_list_store_get_parent_group (model, path, NULL);
729                 gtk_tree_path_free (path);
730         }
731
732         empathy_debug (DEBUG_DOMAIN,
733                       "contact %s (%d) dragged from '%s' to '%s'",
734                       empathy_contact_get_id (contact),
735                       empathy_contact_get_handle (contact),
736                       old_group, new_group);
737
738         list = empathy_contact_list_store_get_list_iface (priv->store);
739         if (new_group) {
740                 empathy_contact_list_add_to_group (list, contact, new_group);
741         }
742         if (old_group && context->action == GDK_ACTION_MOVE) {  
743                 empathy_contact_list_remove_from_group (list, contact, old_group);
744         }
745
746         g_free (old_group);
747         g_free (new_group);
748
749         gtk_drag_finish (context, TRUE, FALSE, GDK_CURRENT_TIME);
750 }
751
752 static gboolean
753 contact_list_view_drag_motion (GtkWidget      *widget,
754                                GdkDragContext *context,
755                                gint            x,
756                                gint            y,
757                                guint           time)
758 {
759         static DragMotionData *dm = NULL;
760         GtkTreePath           *path;
761         gboolean               is_row;
762         gboolean               is_different = FALSE;
763         gboolean               cleanup = TRUE;
764
765         is_row = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
766                                                 x,
767                                                 y,
768                                                 &path,
769                                                 NULL,
770                                                 NULL,
771                                                 NULL);
772
773         cleanup &= (!dm);
774
775         if (is_row) {
776                 cleanup &= (dm && gtk_tree_path_compare (dm->path, path) != 0);
777                 is_different = (!dm || (dm && gtk_tree_path_compare (dm->path, path) != 0));
778         } else {
779                 cleanup &= FALSE;
780         }
781
782         if (!is_different && !cleanup) {
783                 return TRUE;
784         }
785
786         if (dm) {
787                 gtk_tree_path_free (dm->path);
788                 if (dm->timeout_id) {
789                         g_source_remove (dm->timeout_id);
790                 }
791
792                 g_free (dm);
793
794                 dm = NULL;
795         }
796
797         if (!gtk_tree_view_row_expanded (GTK_TREE_VIEW (widget), path)) {
798                 dm = g_new0 (DragMotionData, 1);
799
800                 dm->view = EMPATHY_CONTACT_LIST_VIEW (widget);
801                 dm->path = gtk_tree_path_copy (path);
802
803                 dm->timeout_id = g_timeout_add (
804                         1500,
805                         (GSourceFunc) contact_list_view_drag_motion_cb,
806                         dm);
807         }
808
809         return TRUE;
810 }
811
812 static gboolean
813 contact_list_view_drag_motion_cb (DragMotionData *data)
814 {
815         gtk_tree_view_expand_row (GTK_TREE_VIEW (data->view),
816                                   data->path,
817                                   FALSE);
818
819         data->timeout_id = 0;
820
821         return FALSE;
822 }
823
824 static void
825 contact_list_view_drag_begin (GtkWidget      *widget,
826                               GdkDragContext *context)
827 {
828         EmpathyContactListViewPriv *priv;
829         GtkTreeSelection          *selection;
830         GtkTreeModel              *model;
831         GtkTreePath               *path;
832         GtkTreeIter                iter;
833
834         priv = GET_PRIV (widget);
835
836         GTK_WIDGET_CLASS (empathy_contact_list_view_parent_class)->drag_begin (widget,
837                                                                               context);
838
839         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (widget));
840         if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
841                 return;
842         }
843
844         path = gtk_tree_model_get_path (model, &iter);
845         priv->drag_row = gtk_tree_row_reference_new (model, path);
846         gtk_tree_path_free (path);
847 }
848
849 static void
850 contact_list_view_drag_data_get (GtkWidget        *widget,
851                                  GdkDragContext   *context,
852                                  GtkSelectionData *selection,
853                                  guint             info,
854                                  guint             time)
855 {
856         EmpathyContactListViewPriv *priv;
857         GtkTreePath                *src_path;
858         GtkTreeIter                 iter;
859         GtkTreeModel               *model;
860         EmpathyContact             *contact;
861         McAccount                  *account;
862         const gchar                *contact_id;
863         const gchar                *account_id;
864         gchar                      *str;
865         
866
867         priv = GET_PRIV (widget);
868
869         model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
870         if (!priv->drag_row) {
871                 return;
872         }
873
874         src_path = gtk_tree_row_reference_get_path (priv->drag_row);
875         if (!src_path) {
876                 return;
877         }
878
879         if (!gtk_tree_model_get_iter (model, &iter, src_path)) {
880                 gtk_tree_path_free (src_path);
881                 return;
882         }
883
884         gtk_tree_path_free (src_path);
885
886         contact = empathy_contact_list_view_get_selected (EMPATHY_CONTACT_LIST_VIEW (widget));
887         if (!contact) {
888                 return;
889         }
890
891         account = empathy_contact_get_account (contact);
892         account_id = mc_account_get_unique_name (account);
893         contact_id = empathy_contact_get_id (contact);
894         g_object_unref (contact);
895         str = g_strconcat (account_id, "/", contact_id, NULL);
896
897         switch (info) {
898         case DND_DRAG_TYPE_CONTACT_ID:
899                 gtk_selection_data_set (selection, drag_atoms_source[info], 8,
900                                         (guchar*)str, strlen (str) + 1);
901                 break;
902         }
903
904         g_free (str);
905 }
906
907 static void
908 contact_list_view_drag_end (GtkWidget      *widget,
909                             GdkDragContext *context)
910 {
911         EmpathyContactListViewPriv *priv;
912
913         priv = GET_PRIV (widget);
914
915         GTK_WIDGET_CLASS (empathy_contact_list_view_parent_class)->drag_end (widget,
916                                                                             context);
917
918         if (priv->drag_row) {
919                 gtk_tree_row_reference_free (priv->drag_row);
920                 priv->drag_row = NULL;
921         }
922 }
923
924 static gboolean
925 contact_list_view_drag_drop (GtkWidget      *widget,
926                              GdkDragContext *drag_context,
927                              gint            x,
928                              gint            y,
929                              guint           time)
930 {
931         return FALSE;
932 }
933
934 static void
935 contact_list_view_cell_set_background (EmpathyContactListView *view,
936                                        GtkCellRenderer       *cell,
937                                        gboolean               is_group,
938                                        gboolean               is_active)
939 {
940         GdkColor  color;
941         GtkStyle *style;
942
943         style = gtk_widget_get_style (GTK_WIDGET (view));
944
945         if (!is_group) {
946                 if (is_active) {
947                         color = style->bg[GTK_STATE_SELECTED];
948
949                         /* Here we take the current theme colour and add it to
950                          * the colour for white and average the two. This
951                          * gives a colour which is inline with the theme but
952                          * slightly whiter.
953                          */
954                         color.red = (color.red + (style->white).red) / 2;
955                         color.green = (color.green + (style->white).green) / 2;
956                         color.blue = (color.blue + (style->white).blue) / 2;
957
958                         g_object_set (cell,
959                                       "cell-background-gdk", &color,
960                                       NULL);
961                 } else {
962                         g_object_set (cell,
963                                       "cell-background-gdk", NULL,
964                                       NULL);
965                 }
966         } else {
967                 g_object_set (cell,
968                               "cell-background-gdk", NULL,
969                               NULL);
970         }
971 }
972
973 static void
974 contact_list_view_pixbuf_cell_data_func (GtkTreeViewColumn     *tree_column,
975                                          GtkCellRenderer       *cell,
976                                          GtkTreeModel          *model,
977                                          GtkTreeIter           *iter,
978                                          EmpathyContactListView *view)
979 {
980         gchar    *icon_name;
981         gboolean  is_group;
982         gboolean  is_active;
983
984         gtk_tree_model_get (model, iter,
985                             COL_IS_GROUP, &is_group,
986                             COL_IS_ACTIVE, &is_active,
987                             COL_ICON_STATUS, &icon_name,
988                             -1);
989
990         g_object_set (cell,
991                       "visible", !is_group,
992                       "icon-name", icon_name,
993                       NULL);
994
995         g_free (icon_name);
996
997         contact_list_view_cell_set_background (view, cell, is_group, is_active);
998 }
999
1000 static void
1001 contact_list_view_avatar_cell_data_func (GtkTreeViewColumn     *tree_column,
1002                                          GtkCellRenderer       *cell,
1003                                          GtkTreeModel          *model,
1004                                          GtkTreeIter           *iter,
1005                                          EmpathyContactListView *view)
1006 {
1007         GdkPixbuf *pixbuf;
1008         gboolean   show_avatar;
1009         gboolean   is_group;
1010         gboolean   is_active;
1011
1012         gtk_tree_model_get (model, iter,
1013                             COL_PIXBUF_AVATAR, &pixbuf,
1014                             COL_PIXBUF_AVATAR_VISIBLE, &show_avatar,
1015                             COL_IS_GROUP, &is_group,
1016                             COL_IS_ACTIVE, &is_active,
1017                             -1);
1018
1019         g_object_set (cell,
1020                       "visible", !is_group && show_avatar,
1021                       "pixbuf", pixbuf,
1022                       NULL);
1023
1024         if (pixbuf) {
1025                 g_object_unref (pixbuf);
1026         }
1027
1028         contact_list_view_cell_set_background (view, cell, is_group, is_active);
1029 }
1030
1031 static void
1032 contact_list_view_text_cell_data_func (GtkTreeViewColumn     *tree_column,
1033                                        GtkCellRenderer       *cell,
1034                                        GtkTreeModel          *model,
1035                                        GtkTreeIter           *iter,
1036                                        EmpathyContactListView *view)
1037 {
1038         gboolean is_group;
1039         gboolean is_active;
1040         gboolean show_status;
1041
1042         gtk_tree_model_get (model, iter,
1043                             COL_IS_GROUP, &is_group,
1044                             COL_IS_ACTIVE, &is_active,
1045                             COL_STATUS_VISIBLE, &show_status,
1046                             -1);
1047
1048         g_object_set (cell,
1049                       "show-status", show_status,
1050                       NULL);
1051
1052         contact_list_view_cell_set_background (view, cell, is_group, is_active);
1053 }
1054
1055 static void
1056 contact_list_view_expander_cell_data_func (GtkTreeViewColumn     *column,
1057                                            GtkCellRenderer       *cell,
1058                                            GtkTreeModel          *model,
1059                                            GtkTreeIter           *iter,
1060                                            EmpathyContactListView *view)
1061 {
1062         gboolean is_group;
1063         gboolean is_active;
1064
1065         gtk_tree_model_get (model, iter,
1066                             COL_IS_GROUP, &is_group,
1067                             COL_IS_ACTIVE, &is_active,
1068                             -1);
1069
1070         if (gtk_tree_model_iter_has_child (model, iter)) {
1071                 GtkTreePath *path;
1072                 gboolean     row_expanded;
1073
1074                 path = gtk_tree_model_get_path (model, iter);
1075                 row_expanded = gtk_tree_view_row_expanded (GTK_TREE_VIEW (column->tree_view), path);
1076                 gtk_tree_path_free (path);
1077
1078                 g_object_set (cell,
1079                               "visible", TRUE,
1080                               "expander-style", row_expanded ? GTK_EXPANDER_EXPANDED : GTK_EXPANDER_COLLAPSED,
1081                               NULL);
1082         } else {
1083                 g_object_set (cell, "visible", FALSE, NULL);
1084         }
1085
1086         contact_list_view_cell_set_background (view, cell, is_group, is_active);
1087 }
1088
1089 static GtkWidget *
1090 contact_list_view_get_contact_menu (EmpathyContactListView *view,
1091                                     gboolean               can_send_file,
1092                                     gboolean               can_show_log)
1093 {
1094         EmpathyContactListViewPriv *priv;
1095         GtkAction                 *action;
1096         GtkWidget                 *widget;
1097
1098         priv = GET_PRIV (view);
1099
1100         /* Sort out sensitive items */
1101         action = gtk_ui_manager_get_action (priv->ui, "/Contact/Log");
1102         gtk_action_set_sensitive (action, can_show_log);
1103
1104         action = gtk_ui_manager_get_action (priv->ui, "/Contact/SendFile");
1105         gtk_action_set_visible (action, can_send_file);
1106
1107         widget = gtk_ui_manager_get_widget (priv->ui, "/Contact");
1108
1109         return widget;
1110 }
1111
1112 static gboolean
1113 contact_list_view_button_press_event_cb (EmpathyContactListView *view,
1114                                          GdkEventButton        *event,
1115                                          gpointer               user_data)
1116 {
1117         EmpathyContactListViewPriv *priv;
1118         EmpathyContact             *contact;
1119         GtkTreePath               *path;
1120         GtkTreeSelection          *selection;
1121         GtkTreeModel              *model;
1122         GtkTreeIter                iter;
1123         gboolean                   row_exists;
1124         GtkWidget                 *menu;
1125
1126         if (event->button != 3) {
1127                 return FALSE;
1128         }
1129
1130         priv = GET_PRIV (view);
1131
1132         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
1133         model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
1134
1135         gtk_widget_grab_focus (GTK_WIDGET (view));
1136
1137         row_exists = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (view),
1138                                                     event->x, event->y,
1139                                                     &path,
1140                                                     NULL, NULL, NULL);
1141         if (!row_exists) {
1142                 return FALSE;
1143         }
1144
1145         gtk_tree_selection_unselect_all (selection);
1146         gtk_tree_selection_select_path (selection, path);
1147
1148         gtk_tree_model_get_iter (model, &iter, path);
1149         gtk_tree_path_free (path);
1150
1151         gtk_tree_model_get (model, &iter, COL_CONTACT, &contact, -1);
1152
1153         if (contact) {
1154                 menu = empathy_contact_list_view_get_contact_menu (view, contact);
1155                 g_object_unref (contact);
1156         } else {
1157                 menu = empathy_contact_list_view_get_group_menu (view);
1158         }
1159
1160         if (!menu) {
1161                 return FALSE;
1162         }
1163
1164         gtk_widget_show (menu);
1165
1166         gtk_menu_popup (GTK_MENU (menu),
1167                         NULL, NULL, NULL, NULL,
1168                         event->button, event->time);
1169
1170         return TRUE;
1171 }
1172
1173 static void
1174 contact_list_view_row_activated_cb (EmpathyContactListView *view,
1175                                     GtkTreePath           *path,
1176                                     GtkTreeViewColumn     *col,
1177                                     gpointer               user_data)
1178 {
1179         EmpathyContact *contact;
1180         GtkTreeModel  *model;
1181         GtkTreeIter    iter;
1182
1183         model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
1184
1185         gtk_tree_model_get_iter (model, &iter, path);
1186         gtk_tree_model_get (model, &iter, COL_CONTACT, &contact, -1);
1187
1188         if (contact) {
1189                 contact_list_view_action_activated (view, contact);
1190                 g_object_unref (contact);
1191         }
1192 }
1193
1194 static void
1195 contact_list_view_row_expand_or_collapse_cb (EmpathyContactListView *view,
1196                                              GtkTreeIter           *iter,
1197                                              GtkTreePath           *path,
1198                                              gpointer               user_data)
1199 {
1200         GtkTreeModel *model;
1201         gchar        *name;
1202         gboolean      expanded;
1203
1204         model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
1205
1206         gtk_tree_model_get (model, iter,
1207                             COL_NAME, &name,
1208                             -1);
1209
1210         expanded = GPOINTER_TO_INT (user_data);
1211         empathy_contact_group_set_expanded (name, expanded);
1212
1213         g_free (name);
1214 }
1215
1216 static void
1217 contact_list_view_action_cb (GtkAction             *action,
1218                              EmpathyContactListView *view)
1219 {
1220         EmpathyContactListViewPriv *priv;
1221         EmpathyContact             *contact;
1222         const gchar               *name;
1223         gchar                     *group;
1224         GtkWindow                 *parent;
1225
1226         priv = GET_PRIV (view);
1227
1228         name = gtk_action_get_name (action);
1229         if (!name) {
1230                 return;
1231         }
1232
1233         empathy_debug (DEBUG_DOMAIN, "Action:'%s' activated", name);
1234
1235         contact = empathy_contact_list_view_get_selected (view);
1236         group = empathy_contact_list_view_get_selected_group (view);
1237         parent = empathy_get_toplevel_window (GTK_WIDGET (view));
1238
1239         if (contact && strcmp (name, "Chat") == 0) {
1240                 contact_list_view_action_activated (view, contact);
1241         }
1242         else if (contact && strcmp (name, "Information") == 0) {
1243                 empathy_contact_information_dialog_show (contact, parent, FALSE);
1244         }
1245         else if (contact && strcmp (name, "Edit") == 0) {
1246                 empathy_contact_information_dialog_show (contact, parent, TRUE);
1247         }
1248         else if (contact && strcmp (name, "Remove") == 0) {
1249                 /* FIXME: Ask for confirmation */
1250                 EmpathyContactList *list;
1251
1252                 list = empathy_contact_list_store_get_list_iface (priv->store);
1253                 empathy_contact_list_remove (list, contact,
1254                                              _("Sorry, I don't want you in my contact list anymore."));
1255         }
1256         else if (contact && strcmp (name, "Invite") == 0) {
1257         }
1258         else if (contact && strcmp (name, "SendFile") == 0) {
1259         }
1260         else if (contact && strcmp (name, "Log") == 0) {
1261                 empathy_log_window_show (empathy_contact_get_account (contact),
1262                                         empathy_contact_get_id (contact),
1263                                         FALSE,
1264                                         parent);
1265         }
1266         else if (group && strcmp (name, "Rename") == 0) {
1267         }
1268
1269         g_free (group);
1270         if (contact) {
1271                 g_object_unref (contact);
1272         }
1273 }
1274
1275 static void
1276 contact_list_view_action_activated (EmpathyContactListView *view,
1277                                     EmpathyContact         *contact)
1278 {
1279         MissionControl *mc;
1280
1281         mc = empathy_mission_control_new ();
1282         mission_control_request_channel (mc,
1283                                          empathy_contact_get_account (contact),
1284                                          TP_IFACE_CHANNEL_TYPE_TEXT,
1285                                          empathy_contact_get_handle (contact),
1286                                          TP_HANDLE_TYPE_CONTACT,
1287                                          NULL, NULL);
1288         g_object_unref (mc);
1289 }
1290