]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-contact-list-view.c
Port EmpathyContactWidget to new API
[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-2008 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-lib.h>
31 #include <gdk/gdkkeysyms.h>
32 #include <gtk/gtk.h>
33
34 #include <libmissioncontrol/mc-account.h>
35
36 #include <libempathy/empathy-call-factory.h>
37 #include <libempathy/empathy-contact-factory.h>
38 #include <libempathy/empathy-contact-list.h>
39 #include <libempathy/empathy-contact-groups.h>
40 #include <libempathy/empathy-dispatcher.h>
41 #include <libempathy/empathy-utils.h>
42
43 #include "empathy-contact-list-view.h"
44 #include "empathy-contact-list-store.h"
45 #include "empathy-images.h"
46 #include "empathy-cell-renderer-expander.h"
47 #include "empathy-cell-renderer-text.h"
48 #include "empathy-cell-renderer-activatable.h"
49 #include "empathy-ui-utils.h"
50 #include "empathy-gtk-enum-types.h"
51 #include "empathy-gtk-marshal.h"
52
53 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
54 #include <libempathy/empathy-debug.h>
55
56 /* Active users are those which have recently changed state
57  * (e.g. online, offline or from normal to a busy state).
58  */
59
60 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyContactListView)
61 typedef struct {
62         EmpathyContactListStore        *store;
63         GtkTreeRowReference            *drag_row;
64         EmpathyContactListFeatureFlags  list_features;
65         EmpathyContactFeatureFlags      contact_features;
66         GtkWidget                      *tooltip_widget;
67 } EmpathyContactListViewPriv;
68
69 typedef struct {
70         EmpathyContactListView *view;
71         GtkTreePath           *path;
72         guint                  timeout_id;
73 } DragMotionData;
74
75 typedef struct {
76         EmpathyContactListView *view;
77         EmpathyContact         *contact;
78         gboolean               remove;
79 } ShowActiveData;
80
81 enum {
82         PROP_0,
83         PROP_STORE,
84         PROP_LIST_FEATURES,
85         PROP_CONTACT_FEATURES,
86 };
87
88 enum DndDragType {
89         DND_DRAG_TYPE_CONTACT_ID,
90         DND_DRAG_TYPE_URL,
91         DND_DRAG_TYPE_STRING,
92 };
93
94 static const GtkTargetEntry drag_types_dest[] = {
95         { "text/contact-id", 0, DND_DRAG_TYPE_CONTACT_ID },
96         { "text/uri-list",   0, DND_DRAG_TYPE_URL },
97         { "text/plain",      0, DND_DRAG_TYPE_STRING },
98         { "STRING",          0, DND_DRAG_TYPE_STRING },
99 };
100
101 static const GtkTargetEntry drag_types_source[] = {
102         { "text/contact-id", 0, DND_DRAG_TYPE_CONTACT_ID },
103 };
104
105 static GdkAtom drag_atoms_dest[G_N_ELEMENTS (drag_types_dest)];
106 static GdkAtom drag_atoms_source[G_N_ELEMENTS (drag_types_source)];
107
108 enum {
109         DRAG_CONTACT_RECEIVED,
110         LAST_SIGNAL
111 };
112
113 static guint signals[LAST_SIGNAL];
114
115 G_DEFINE_TYPE (EmpathyContactListView, empathy_contact_list_view, GTK_TYPE_TREE_VIEW);
116
117 static void
118 contact_list_view_tooltip_destroy_cb (GtkWidget              *widget,
119                                       EmpathyContactListView *view)
120 {
121         EmpathyContactListViewPriv *priv = GET_PRIV (view);
122         
123         if (priv->tooltip_widget) {
124                 DEBUG ("Tooltip destroyed");
125                 priv->tooltip_widget = NULL;
126                 g_object_unref (widget);
127         }
128 }
129
130 static gboolean
131 contact_list_view_query_tooltip_cb (EmpathyContactListView *view,
132                                     gint                    x,
133                                     gint                    y,
134                                     gboolean                keyboard_mode,
135                                     GtkTooltip             *tooltip,
136                                     gpointer                user_data)
137 {
138         EmpathyContactListViewPriv *priv = GET_PRIV (view);
139         EmpathyContact             *contact;
140         GtkTreeModel               *model;
141         GtkTreeIter                 iter;
142         GtkTreePath                *path;
143         static gint                 running = 0;
144         gboolean                    ret = FALSE;
145
146         /* Avoid an infinite loop. See GNOME bug #574377 */
147         if (running > 0) {
148                 return FALSE;
149         }
150         running++;
151
152         if (!gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (view), &x, &y,
153                                                 keyboard_mode,
154                                                 &model, &path, &iter)) {
155                 goto OUT;
156         }
157
158         gtk_tree_view_set_tooltip_row (GTK_TREE_VIEW (view), tooltip, path);
159         gtk_tree_path_free (path);
160
161         gtk_tree_model_get (model, &iter,
162                             EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
163                             -1);
164         if (!contact) {
165                 goto OUT;
166         }
167
168         if (!priv->tooltip_widget) {
169                 priv->tooltip_widget = empathy_contact_widget_new (contact,
170                         EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP);
171                 g_object_ref (priv->tooltip_widget);
172                 g_signal_connect (priv->tooltip_widget, "destroy",
173                                   G_CALLBACK (contact_list_view_tooltip_destroy_cb),
174                                   view);
175                 gtk_widget_show (priv->tooltip_widget);
176         } else {
177                 empathy_contact_widget_set_contact (priv->tooltip_widget,
178                                                     contact);
179         }
180
181         gtk_tooltip_set_custom (tooltip, priv->tooltip_widget);
182         ret = TRUE;
183
184         g_object_unref (contact);
185 OUT:
186         running--;
187
188         return ret;
189 }
190
191 static void
192 contact_list_view_drag_data_received (GtkWidget         *widget,
193                                       GdkDragContext    *context,
194                                       gint               x,
195                                       gint               y,
196                                       GtkSelectionData  *selection,
197                                       guint              info,
198                                       guint              time)
199 {
200         EmpathyContactListViewPriv *priv;
201         EmpathyContactList         *list;
202         EmpathyContactFactory      *factory;
203         McAccount                  *account;
204         GtkTreeModel               *model;
205         GtkTreePath                *path;
206         GtkTreeViewDropPosition     position;
207         EmpathyContact             *contact = NULL;
208         const gchar                *id;
209         gchar                     **strv;
210         gchar                      *new_group = NULL;
211         gchar                      *old_group = NULL;
212         gboolean                    is_row;
213
214         priv = GET_PRIV (widget);
215
216         id = (const gchar*) selection->data;
217         DEBUG ("Received %s%s drag & drop contact from roster with id:'%s'",
218                 context->action == GDK_ACTION_MOVE ? "move" : "",
219                 context->action == GDK_ACTION_COPY ? "copy" : "",
220                 id);
221
222         strv = g_strsplit (id, "/", 2);
223         factory = empathy_contact_factory_dup_singleton ();
224         account = mc_account_lookup (strv[0]);
225         if (account) {
226                 contact = empathy_contact_factory_get_from_id (factory,
227                                                                account,
228                                                                strv[1]);
229                 g_object_unref (account);
230         }
231         g_object_unref (factory);
232         g_strfreev (strv);
233
234         if (!contact) {
235                 DEBUG ("No contact found associated with drag & drop");
236                 return;
237         }
238
239         empathy_contact_run_until_ready (contact,
240                                          EMPATHY_CONTACT_READY_HANDLE,
241                                          NULL);
242
243         model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
244
245         /* Get source group information. */
246         if (priv->drag_row) {
247                 path = gtk_tree_row_reference_get_path (priv->drag_row);
248                 if (path) {
249                         old_group = empathy_contact_list_store_get_parent_group (model, path, NULL);
250                         gtk_tree_path_free (path);
251                 }
252         }
253
254         /* Get destination group information. */
255         is_row = gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget),
256                                                     x,
257                                                     y,
258                                                     &path,
259                                                     &position);
260
261         if (is_row) {
262                 new_group = empathy_contact_list_store_get_parent_group (model, path, NULL);
263                 gtk_tree_path_free (path);
264         }
265
266         DEBUG ("contact %s (%d) dragged from '%s' to '%s'",
267                 empathy_contact_get_id (contact),
268                 empathy_contact_get_handle (contact),
269                 old_group, new_group);
270
271         list = empathy_contact_list_store_get_list_iface (priv->store);
272         if (new_group) {
273                 empathy_contact_list_add_to_group (list, contact, new_group);
274         }
275         if (old_group && context->action == GDK_ACTION_MOVE) {  
276                 empathy_contact_list_remove_from_group (list, contact, old_group);
277         }
278
279         g_free (old_group);
280         g_free (new_group);
281
282         gtk_drag_finish (context, TRUE, FALSE, GDK_CURRENT_TIME);
283 }
284
285 static gboolean
286 contact_list_view_drag_motion_cb (DragMotionData *data)
287 {
288         gtk_tree_view_expand_row (GTK_TREE_VIEW (data->view),
289                                   data->path,
290                                   FALSE);
291
292         data->timeout_id = 0;
293
294         return FALSE;
295 }
296
297 static gboolean
298 contact_list_view_drag_motion (GtkWidget      *widget,
299                                GdkDragContext *context,
300                                gint            x,
301                                gint            y,
302                                guint           time)
303 {
304         static DragMotionData *dm = NULL;
305         GtkTreePath           *path;
306         gboolean               is_row;
307         gboolean               is_different = FALSE;
308         gboolean               cleanup = TRUE;
309
310         is_row = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
311                                                 x,
312                                                 y,
313                                                 &path,
314                                                 NULL,
315                                                 NULL,
316                                                 NULL);
317
318         cleanup &= (!dm);
319
320         if (is_row) {
321                 cleanup &= (dm && gtk_tree_path_compare (dm->path, path) != 0);
322                 is_different = (!dm || (dm && gtk_tree_path_compare (dm->path, path) != 0));
323         } else {
324                 cleanup &= FALSE;
325         }
326
327         if (!is_different && !cleanup) {
328                 return TRUE;
329         }
330
331         if (dm) {
332                 gtk_tree_path_free (dm->path);
333                 if (dm->timeout_id) {
334                         g_source_remove (dm->timeout_id);
335                 }
336
337                 g_free (dm);
338
339                 dm = NULL;
340         }
341
342         if (!gtk_tree_view_row_expanded (GTK_TREE_VIEW (widget), path)) {
343                 dm = g_new0 (DragMotionData, 1);
344
345                 dm->view = EMPATHY_CONTACT_LIST_VIEW (widget);
346                 dm->path = gtk_tree_path_copy (path);
347
348                 dm->timeout_id = g_timeout_add_seconds (1,
349                         (GSourceFunc) contact_list_view_drag_motion_cb,
350                         dm);
351         }
352
353         return TRUE;
354 }
355
356 static void
357 contact_list_view_drag_begin (GtkWidget      *widget,
358                               GdkDragContext *context)
359 {
360         EmpathyContactListViewPriv *priv;
361         GtkTreeSelection          *selection;
362         GtkTreeModel              *model;
363         GtkTreePath               *path;
364         GtkTreeIter                iter;
365
366         priv = GET_PRIV (widget);
367
368         GTK_WIDGET_CLASS (empathy_contact_list_view_parent_class)->drag_begin (widget,
369                                                                               context);
370
371         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (widget));
372         if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
373                 return;
374         }
375
376         path = gtk_tree_model_get_path (model, &iter);
377         priv->drag_row = gtk_tree_row_reference_new (model, path);
378         gtk_tree_path_free (path);
379 }
380
381 static void
382 contact_list_view_drag_data_get (GtkWidget        *widget,
383                                  GdkDragContext   *context,
384                                  GtkSelectionData *selection,
385                                  guint             info,
386                                  guint             time)
387 {
388         EmpathyContactListViewPriv *priv;
389         GtkTreePath                *src_path;
390         GtkTreeIter                 iter;
391         GtkTreeModel               *model;
392         EmpathyContact             *contact;
393         McAccount                  *account;
394         const gchar                *contact_id;
395         const gchar                *account_id;
396         gchar                      *str;
397
398         priv = GET_PRIV (widget);
399
400         model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
401         if (!priv->drag_row) {
402                 return;
403         }
404
405         src_path = gtk_tree_row_reference_get_path (priv->drag_row);
406         if (!src_path) {
407                 return;
408         }
409
410         if (!gtk_tree_model_get_iter (model, &iter, src_path)) {
411                 gtk_tree_path_free (src_path);
412                 return;
413         }
414
415         gtk_tree_path_free (src_path);
416
417         contact = empathy_contact_list_view_get_selected (EMPATHY_CONTACT_LIST_VIEW (widget));
418         if (!contact) {
419                 return;
420         }
421
422         account = empathy_contact_get_account (contact);
423         account_id = mc_account_get_unique_name (account);
424         contact_id = empathy_contact_get_id (contact);
425         g_object_unref (contact);
426         str = g_strconcat (account_id, "/", contact_id, NULL);
427
428         switch (info) {
429         case DND_DRAG_TYPE_CONTACT_ID:
430                 gtk_selection_data_set (selection, drag_atoms_source[info], 8,
431                                         (guchar*)str, strlen (str) + 1);
432                 break;
433         }
434
435         g_free (str);
436 }
437
438 static void
439 contact_list_view_drag_end (GtkWidget      *widget,
440                             GdkDragContext *context)
441 {
442         EmpathyContactListViewPriv *priv;
443
444         priv = GET_PRIV (widget);
445
446         GTK_WIDGET_CLASS (empathy_contact_list_view_parent_class)->drag_end (widget,
447                                                                             context);
448
449         if (priv->drag_row) {
450                 gtk_tree_row_reference_free (priv->drag_row);
451                 priv->drag_row = NULL;
452         }
453 }
454
455 static gboolean
456 contact_list_view_drag_drop (GtkWidget      *widget,
457                              GdkDragContext *drag_context,
458                              gint            x,
459                              gint            y,
460                              guint           time)
461 {
462         return FALSE;
463 }
464
465 typedef struct {
466         EmpathyContactListView *view;
467         guint                   button;
468         guint32                 time;
469 } MenuPopupData;
470
471 static gboolean
472 contact_list_view_popup_menu_idle_cb (gpointer user_data)
473 {
474         MenuPopupData *data = user_data;
475         GtkWidget     *menu;
476
477         menu = empathy_contact_list_view_get_contact_menu (data->view);
478         if (!menu) {
479                 menu = empathy_contact_list_view_get_group_menu (data->view);
480         }
481
482         if (menu) {
483                 gtk_widget_show (menu);
484                 gtk_menu_popup (GTK_MENU (menu),
485                                 NULL, NULL, NULL, NULL,
486                                 data->button, data->time);
487         }
488
489         g_slice_free (MenuPopupData, data);
490
491         return FALSE;
492 }
493
494 static gboolean
495 contact_list_view_button_press_event_cb (EmpathyContactListView *view,
496                                          GdkEventButton         *event,
497                                          gpointer                user_data)
498 {
499         if (event->button == 3) {
500                 MenuPopupData *data;
501
502                 data = g_slice_new (MenuPopupData);
503                 data->view = view;
504                 data->button = event->button;
505                 data->time = event->time;
506                 g_idle_add (contact_list_view_popup_menu_idle_cb, data);
507         }
508
509         return FALSE;
510 }
511
512 static gboolean
513 contact_list_view_key_press_event_cb (EmpathyContactListView *view,
514                                       GdkEventKey            *event,
515                                       gpointer                user_data)
516 {
517         if (event->keyval == GDK_Menu) {
518                 MenuPopupData *data;
519
520                 data = g_slice_new (MenuPopupData);
521                 data->view = view;
522                 data->button = 0;
523                 data->time = event->time;
524                 g_idle_add (contact_list_view_popup_menu_idle_cb, data);
525         }
526
527         return FALSE;
528 }
529
530 static void
531 contact_list_view_row_activated (GtkTreeView       *view,
532                                  GtkTreePath       *path,
533                                  GtkTreeViewColumn *column)
534 {
535         EmpathyContactListViewPriv *priv = GET_PRIV (view);
536         EmpathyContact             *contact;
537         GtkTreeModel               *model;
538         GtkTreeIter                 iter;
539
540         if (!(priv->contact_features & EMPATHY_CONTACT_FEATURE_CHAT)) {
541                 return;
542         }
543
544         model = GTK_TREE_MODEL (priv->store);
545         gtk_tree_model_get_iter (model, &iter, path);
546         gtk_tree_model_get (model, &iter,
547                             EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
548                             -1);
549
550         if (contact) {
551                 DEBUG ("Starting a chat");
552                 empathy_dispatcher_chat_with_contact (contact, NULL, NULL);
553                 g_object_unref (contact);
554         }
555 }
556
557 static void
558 contact_list_view_voip_activated_cb (EmpathyCellRendererActivatable *cell,
559                                      const gchar                    *path_string,
560                                      EmpathyContactListView         *view)
561 {
562         EmpathyContactListViewPriv *priv = GET_PRIV (view);
563         GtkTreeModel               *model;
564         GtkTreeIter                 iter;
565         EmpathyContact             *contact;
566
567         if (!(priv->contact_features & EMPATHY_CONTACT_FEATURE_CALL)) {
568                 return;
569         }
570
571         model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
572         if (!gtk_tree_model_get_iter_from_string (model, &iter, path_string)) {
573                 return;
574         }
575
576         gtk_tree_model_get (model, &iter,
577                             EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
578                             -1);
579
580         if (contact) {
581                 EmpathyCallFactory *factory;
582
583                 factory = empathy_call_factory_get ();
584                 empathy_call_factory_new_call (factory, contact);
585
586                 g_object_unref (contact);
587         }
588 }
589
590 static void
591 contact_list_view_cell_set_background (EmpathyContactListView *view,
592                                        GtkCellRenderer       *cell,
593                                        gboolean               is_group,
594                                        gboolean               is_active)
595 {
596         GdkColor  color;
597         GtkStyle *style;
598
599         style = gtk_widget_get_style (GTK_WIDGET (view));
600
601         if (!is_group && is_active) {
602                 color = style->bg[GTK_STATE_SELECTED];
603
604                 /* Here we take the current theme colour and add it to
605                  * the colour for white and average the two. This
606                  * gives a colour which is inline with the theme but
607                  * slightly whiter.
608                  */
609                 color.red = (color.red + (style->white).red) / 2;
610                 color.green = (color.green + (style->white).green) / 2;
611                 color.blue = (color.blue + (style->white).blue) / 2;
612
613                 g_object_set (cell,
614                               "cell-background-gdk", &color,
615                               NULL);
616         } else {
617                 g_object_set (cell,
618                               "cell-background-gdk", NULL,
619                               NULL);
620         }
621 }
622
623 static void
624 contact_list_view_pixbuf_cell_data_func (GtkTreeViewColumn     *tree_column,
625                                          GtkCellRenderer       *cell,
626                                          GtkTreeModel          *model,
627                                          GtkTreeIter           *iter,
628                                          EmpathyContactListView *view)
629 {
630         gchar    *icon_name;
631         gboolean  is_group;
632         gboolean  is_active;
633
634         gtk_tree_model_get (model, iter,
635                             EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
636                             EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, &is_active,
637                             EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, &icon_name,
638                             -1);
639
640         g_object_set (cell,
641                       "visible", !is_group,
642                       "icon-name", icon_name,
643                       NULL);
644
645         g_free (icon_name);
646
647         contact_list_view_cell_set_background (view, cell, is_group, is_active);
648 }
649
650 static void
651 contact_list_view_voip_cell_data_func (GtkTreeViewColumn      *tree_column,
652                                        GtkCellRenderer        *cell,
653                                        GtkTreeModel           *model,
654                                        GtkTreeIter            *iter,
655                                        EmpathyContactListView *view)
656 {
657         gboolean is_group;
658         gboolean is_active;
659         gboolean can_voip;
660
661         gtk_tree_model_get (model, iter,
662                             EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
663                             EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, &is_active,
664                             EMPATHY_CONTACT_LIST_STORE_COL_CAN_VOIP, &can_voip,
665                             -1);
666
667         g_object_set (cell,
668                       "visible", !is_group && can_voip,
669                       "icon-name", EMPATHY_IMAGE_VOIP,
670                       NULL);
671
672         contact_list_view_cell_set_background (view, cell, is_group, is_active);
673 }
674
675 static void
676 contact_list_view_avatar_cell_data_func (GtkTreeViewColumn     *tree_column,
677                                          GtkCellRenderer       *cell,
678                                          GtkTreeModel          *model,
679                                          GtkTreeIter           *iter,
680                                          EmpathyContactListView *view)
681 {
682         GdkPixbuf *pixbuf;
683         gboolean   show_avatar;
684         gboolean   is_group;
685         gboolean   is_active;
686
687         gtk_tree_model_get (model, iter,
688                             EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR, &pixbuf,
689                             EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR_VISIBLE, &show_avatar,
690                             EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
691                             EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, &is_active,
692                             -1);
693
694         g_object_set (cell,
695                       "visible", !is_group && show_avatar,
696                       "pixbuf", pixbuf,
697                       NULL);
698
699         if (pixbuf) {
700                 g_object_unref (pixbuf);
701         }
702
703         contact_list_view_cell_set_background (view, cell, is_group, is_active);
704 }
705
706 static void
707 contact_list_view_text_cell_data_func (GtkTreeViewColumn     *tree_column,
708                                        GtkCellRenderer       *cell,
709                                        GtkTreeModel          *model,
710                                        GtkTreeIter           *iter,
711                                        EmpathyContactListView *view)
712 {
713         gboolean is_group;
714         gboolean is_active;
715         gboolean show_status;
716
717         gtk_tree_model_get (model, iter,
718                             EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
719                             EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, &is_active,
720                             EMPATHY_CONTACT_LIST_STORE_COL_STATUS_VISIBLE, &show_status,
721                             -1);
722
723         g_object_set (cell,
724                       "show-status", show_status,
725                       NULL);
726
727         contact_list_view_cell_set_background (view, cell, is_group, is_active);
728 }
729
730 static void
731 contact_list_view_expander_cell_data_func (GtkTreeViewColumn     *column,
732                                            GtkCellRenderer       *cell,
733                                            GtkTreeModel          *model,
734                                            GtkTreeIter           *iter,
735                                            EmpathyContactListView *view)
736 {
737         gboolean is_group;
738         gboolean is_active;
739
740         gtk_tree_model_get (model, iter,
741                             EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
742                             EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, &is_active,
743                             -1);
744
745         if (gtk_tree_model_iter_has_child (model, iter)) {
746                 GtkTreePath *path;
747                 gboolean     row_expanded;
748
749                 path = gtk_tree_model_get_path (model, iter);
750                 row_expanded = gtk_tree_view_row_expanded (GTK_TREE_VIEW (column->tree_view), path);
751                 gtk_tree_path_free (path);
752
753                 g_object_set (cell,
754                               "visible", TRUE,
755                               "expander-style", row_expanded ? GTK_EXPANDER_EXPANDED : GTK_EXPANDER_COLLAPSED,
756                               NULL);
757         } else {
758                 g_object_set (cell, "visible", FALSE, NULL);
759         }
760
761         contact_list_view_cell_set_background (view, cell, is_group, is_active);
762 }
763
764 static void
765 contact_list_view_row_expand_or_collapse_cb (EmpathyContactListView *view,
766                                              GtkTreeIter           *iter,
767                                              GtkTreePath           *path,
768                                              gpointer               user_data)
769 {
770         EmpathyContactListViewPriv *priv = GET_PRIV (view);
771         GtkTreeModel               *model;
772         gchar                      *name;
773         gboolean                    expanded;
774
775         if (!(priv->list_features & EMPATHY_CONTACT_LIST_FEATURE_GROUPS_SAVE)) {
776                 return;
777         }
778
779         model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
780
781         gtk_tree_model_get (model, iter,
782                             EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
783                             -1);
784
785         expanded = GPOINTER_TO_INT (user_data);
786         empathy_contact_group_set_expanded (name, expanded);
787
788         g_free (name);
789 }
790
791 static void
792 contact_list_view_row_has_child_toggled_cb (GtkTreeModel          *model,
793                                             GtkTreePath           *path,
794                                             GtkTreeIter           *iter,
795                                             EmpathyContactListView *view)
796 {
797         EmpathyContactListViewPriv *priv = GET_PRIV (view);
798         gboolean  is_group = FALSE;
799         gchar    *name = NULL;
800
801         gtk_tree_model_get (model, iter,
802                             EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
803                             EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
804                             -1);
805
806         if (!is_group || EMP_STR_EMPTY (name)) {
807                 g_free (name);
808                 return;
809         }
810
811         if (!(priv->list_features & EMPATHY_CONTACT_LIST_FEATURE_GROUPS_SAVE) ||
812             empathy_contact_group_get_expanded (name)) {
813                 g_signal_handlers_block_by_func (view,
814                                                  contact_list_view_row_expand_or_collapse_cb,
815                                                  GINT_TO_POINTER (TRUE));
816                 gtk_tree_view_expand_row (GTK_TREE_VIEW (view), path, TRUE);
817                 g_signal_handlers_unblock_by_func (view,
818                                                    contact_list_view_row_expand_or_collapse_cb,
819                                                    GINT_TO_POINTER (TRUE));
820         } else {
821                 g_signal_handlers_block_by_func (view,
822                                                  contact_list_view_row_expand_or_collapse_cb,
823                                                  GINT_TO_POINTER (FALSE));
824                 gtk_tree_view_collapse_row (GTK_TREE_VIEW (view), path);
825                 g_signal_handlers_unblock_by_func (view,
826                                                    contact_list_view_row_expand_or_collapse_cb,
827                                                    GINT_TO_POINTER (FALSE));
828         }
829
830         g_free (name);
831 }
832
833 static void
834 contact_list_view_setup (EmpathyContactListView *view)
835 {
836         EmpathyContactListViewPriv *priv;
837         GtkCellRenderer           *cell;
838         GtkTreeViewColumn         *col;
839         gint                       i;
840
841         priv = GET_PRIV (view);
842
843         gtk_tree_view_set_search_equal_func (GTK_TREE_VIEW (view),
844                                              empathy_contact_list_store_search_equal_func,
845                                              NULL, NULL);
846
847         g_signal_connect (priv->store, "row-has-child-toggled",
848                           G_CALLBACK (contact_list_view_row_has_child_toggled_cb),
849                           view);
850         gtk_tree_view_set_model (GTK_TREE_VIEW (view),
851                                  GTK_TREE_MODEL (priv->store));
852
853         /* Setup view */
854         g_object_set (view,
855                       "headers-visible", FALSE,
856                       "reorderable", TRUE,
857                       "show-expanders", FALSE,
858                       NULL);
859
860         col = gtk_tree_view_column_new ();
861
862         /* State */
863         cell = gtk_cell_renderer_pixbuf_new ();
864         gtk_tree_view_column_pack_start (col, cell, FALSE);
865         gtk_tree_view_column_set_cell_data_func (
866                 col, cell,
867                 (GtkTreeCellDataFunc) contact_list_view_pixbuf_cell_data_func,
868                 view, NULL);
869
870         g_object_set (cell,
871                       "xpad", 5,
872                       "ypad", 1,
873                       "visible", FALSE,
874                       NULL);
875
876         /* Name */
877         cell = empathy_cell_renderer_text_new ();
878         gtk_tree_view_column_pack_start (col, cell, TRUE);
879         gtk_tree_view_column_set_cell_data_func (
880                 col, cell,
881                 (GtkTreeCellDataFunc) contact_list_view_text_cell_data_func,
882                 view, NULL);
883
884         gtk_tree_view_column_add_attribute (col, cell,
885                                             "name", EMPATHY_CONTACT_LIST_STORE_COL_NAME);
886         gtk_tree_view_column_add_attribute (col, cell,
887                                             "status", EMPATHY_CONTACT_LIST_STORE_COL_STATUS);
888         gtk_tree_view_column_add_attribute (col, cell,
889                                             "is_group", EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP);
890
891         /* Voip Capability Icon */
892         cell = empathy_cell_renderer_activatable_new ();
893         gtk_tree_view_column_pack_start (col, cell, FALSE);
894         gtk_tree_view_column_set_cell_data_func (
895                 col, cell,
896                 (GtkTreeCellDataFunc) contact_list_view_voip_cell_data_func,
897                 view, NULL);
898
899         g_object_set (cell,
900                       "visible", FALSE,
901                       NULL);
902
903         g_signal_connect (cell, "path-activated",
904                           G_CALLBACK (contact_list_view_voip_activated_cb),
905                           view);
906
907         /* Avatar */
908         cell = gtk_cell_renderer_pixbuf_new ();
909         gtk_tree_view_column_pack_start (col, cell, FALSE);
910         gtk_tree_view_column_set_cell_data_func (
911                 col, cell,
912                 (GtkTreeCellDataFunc) contact_list_view_avatar_cell_data_func,
913                 view, NULL);
914
915         g_object_set (cell,
916                       "xpad", 0,
917                       "ypad", 0,
918                       "visible", FALSE,
919                       "width", 32,
920                       "height", 32,
921                       NULL);
922
923         /* Expander */
924         cell = empathy_cell_renderer_expander_new ();
925         gtk_tree_view_column_pack_end (col, cell, FALSE);
926         gtk_tree_view_column_set_cell_data_func (
927                 col, cell,
928                 (GtkTreeCellDataFunc) contact_list_view_expander_cell_data_func,
929                 view, NULL);
930
931         /* Actually add the column now we have added all cell renderers */
932         gtk_tree_view_append_column (GTK_TREE_VIEW (view), col);
933
934         /* Drag & Drop. */
935         for (i = 0; i < G_N_ELEMENTS (drag_types_dest); ++i) {
936                 drag_atoms_dest[i] = gdk_atom_intern (drag_types_dest[i].target,
937                                                       FALSE);
938         }
939
940         for (i = 0; i < G_N_ELEMENTS (drag_types_source); ++i) {
941                 drag_atoms_source[i] = gdk_atom_intern (drag_types_source[i].target,
942                                                         FALSE);
943         }
944 }
945
946 static void
947 contact_list_view_set_list_features (EmpathyContactListView         *view,
948                                      EmpathyContactListFeatureFlags  features)
949 {
950         EmpathyContactListViewPriv *priv = GET_PRIV (view);
951         gboolean                    has_tooltip;
952
953         g_return_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view));
954
955         priv->list_features = features;
956
957         /* Update DnD source/dest */
958         if (features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_DRAG) {
959                 gtk_drag_source_set (GTK_WIDGET (view),
960                                      GDK_BUTTON1_MASK,
961                                      drag_types_source,
962                                      G_N_ELEMENTS (drag_types_source),
963                                      GDK_ACTION_MOVE | GDK_ACTION_COPY);
964         } else {
965                 gtk_drag_source_unset (GTK_WIDGET (view));
966
967         }
968
969         if (features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_DROP) {
970                 gtk_drag_dest_set (GTK_WIDGET (view),
971                                    GTK_DEST_DEFAULT_ALL,
972                                    drag_types_dest,
973                                    G_N_ELEMENTS (drag_types_dest),
974                                    GDK_ACTION_MOVE | GDK_ACTION_COPY);
975         } else {
976                 /* FIXME: URI could still be droped depending on FT feature */
977                 gtk_drag_dest_unset (GTK_WIDGET (view));
978         }
979
980         /* Update has-tooltip */
981         has_tooltip = (features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_TOOLTIP) != 0;
982         gtk_widget_set_has_tooltip (GTK_WIDGET (view), has_tooltip);
983 }
984
985 static void
986 contact_list_view_finalize (GObject *object)
987 {
988         EmpathyContactListViewPriv *priv;
989
990         priv = GET_PRIV (object);
991
992         if (priv->store) {
993                 g_object_unref (priv->store);
994         }
995         if (priv->tooltip_widget) {
996                 gtk_widget_destroy (priv->tooltip_widget);
997         }
998
999         G_OBJECT_CLASS (empathy_contact_list_view_parent_class)->finalize (object);
1000 }
1001
1002 static void
1003 contact_list_view_get_property (GObject    *object,
1004                                 guint       param_id,
1005                                 GValue     *value,
1006                                 GParamSpec *pspec)
1007 {
1008         EmpathyContactListViewPriv *priv;
1009
1010         priv = GET_PRIV (object);
1011
1012         switch (param_id) {
1013         case PROP_STORE:
1014                 g_value_set_object (value, priv->store);
1015                 break;
1016         case PROP_LIST_FEATURES:
1017                 g_value_set_flags (value, priv->list_features);
1018                 break;
1019         case PROP_CONTACT_FEATURES:
1020                 g_value_set_flags (value, priv->contact_features);
1021                 break;
1022         default:
1023                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1024                 break;
1025         };
1026 }
1027
1028 static void
1029 contact_list_view_set_property (GObject      *object,
1030                                 guint         param_id,
1031                                 const GValue *value,
1032                                 GParamSpec   *pspec)
1033 {
1034         EmpathyContactListView     *view = EMPATHY_CONTACT_LIST_VIEW (object);
1035         EmpathyContactListViewPriv *priv = GET_PRIV (object);
1036
1037         switch (param_id) {
1038         case PROP_STORE:
1039                 priv->store = g_value_dup_object (value);
1040                 contact_list_view_setup (view);
1041                 break;
1042         case PROP_LIST_FEATURES:
1043                 contact_list_view_set_list_features (view, g_value_get_flags (value));
1044                 break;
1045         case PROP_CONTACT_FEATURES:
1046                 priv->contact_features = g_value_get_flags (value);
1047                 break;
1048         default:
1049                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1050                 break;
1051         };
1052 }
1053
1054 static void
1055 empathy_contact_list_view_class_init (EmpathyContactListViewClass *klass)
1056 {
1057         GObjectClass     *object_class = G_OBJECT_CLASS (klass);
1058         GtkWidgetClass   *widget_class = GTK_WIDGET_CLASS (klass);
1059         GtkTreeViewClass *tree_view_class = GTK_TREE_VIEW_CLASS (klass);
1060
1061         object_class->finalize = contact_list_view_finalize;
1062         object_class->get_property = contact_list_view_get_property;
1063         object_class->set_property = contact_list_view_set_property;
1064
1065         widget_class->drag_data_received = contact_list_view_drag_data_received;
1066         widget_class->drag_drop          = contact_list_view_drag_drop;
1067         widget_class->drag_begin         = contact_list_view_drag_begin;
1068         widget_class->drag_data_get      = contact_list_view_drag_data_get;
1069         widget_class->drag_end           = contact_list_view_drag_end;
1070         widget_class->drag_motion        = contact_list_view_drag_motion;
1071
1072         /* We use the class method to let user of this widget to connect to
1073          * the signal and stop emission of the signal so the default handler
1074          * won't be called. */
1075         tree_view_class->row_activated = contact_list_view_row_activated;
1076
1077         signals[DRAG_CONTACT_RECEIVED] =
1078                 g_signal_new ("drag-contact-received",
1079                               G_OBJECT_CLASS_TYPE (klass),
1080                               G_SIGNAL_RUN_LAST,
1081                               0,
1082                               NULL, NULL,
1083                               _empathy_gtk_marshal_VOID__OBJECT_STRING_STRING,
1084                               G_TYPE_NONE,
1085                               3, EMPATHY_TYPE_CONTACT, G_TYPE_STRING, G_TYPE_STRING);
1086
1087         g_object_class_install_property (object_class,
1088                                          PROP_STORE,
1089                                          g_param_spec_object ("store",
1090                                                              "The store of the view",
1091                                                              "The store of the view",
1092                                                               EMPATHY_TYPE_CONTACT_LIST_STORE,
1093                                                               G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
1094         g_object_class_install_property (object_class,
1095                                          PROP_LIST_FEATURES,
1096                                          g_param_spec_flags ("list-features",
1097                                                              "Features of the view",
1098                                                              "Falgs for all enabled features",
1099                                                               EMPATHY_TYPE_CONTACT_LIST_FEATURE_FLAGS,
1100                                                               EMPATHY_CONTACT_LIST_FEATURE_NONE,
1101                                                               G_PARAM_READWRITE));
1102         g_object_class_install_property (object_class,
1103                                          PROP_CONTACT_FEATURES,
1104                                          g_param_spec_flags ("contact-features",
1105                                                              "Features of the contact menu",
1106                                                              "Falgs for all enabled features for the menu",
1107                                                               EMPATHY_TYPE_CONTACT_FEATURE_FLAGS,
1108                                                               EMPATHY_CONTACT_FEATURE_NONE,
1109                                                               G_PARAM_READWRITE));
1110
1111         g_type_class_add_private (object_class, sizeof (EmpathyContactListViewPriv));
1112 }
1113
1114 static void
1115 empathy_contact_list_view_init (EmpathyContactListView *view)
1116 {
1117         EmpathyContactListViewPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (view,
1118                 EMPATHY_TYPE_CONTACT_LIST_VIEW, EmpathyContactListViewPriv);
1119
1120         view->priv = priv;
1121         /* Get saved group states. */
1122         empathy_contact_groups_get_all ();
1123
1124         gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (view), 
1125                                               empathy_contact_list_store_row_separator_func,
1126                                               NULL, NULL);
1127
1128         /* Connect to tree view signals rather than override. */
1129         g_signal_connect (view, "button-press-event",
1130                           G_CALLBACK (contact_list_view_button_press_event_cb),
1131                           NULL);
1132         g_signal_connect (view, "key-press-event",
1133                           G_CALLBACK (contact_list_view_key_press_event_cb),
1134                           NULL);
1135         g_signal_connect (view, "row-expanded",
1136                           G_CALLBACK (contact_list_view_row_expand_or_collapse_cb),
1137                           GINT_TO_POINTER (TRUE));
1138         g_signal_connect (view, "row-collapsed",
1139                           G_CALLBACK (contact_list_view_row_expand_or_collapse_cb),
1140                           GINT_TO_POINTER (FALSE));
1141         g_signal_connect (view, "query-tooltip",
1142                           G_CALLBACK (contact_list_view_query_tooltip_cb),
1143                           NULL);
1144 }
1145
1146 EmpathyContactListView *
1147 empathy_contact_list_view_new (EmpathyContactListStore        *store,
1148                                EmpathyContactListFeatureFlags  list_features,
1149                                EmpathyContactFeatureFlags      contact_features)
1150 {
1151         g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), NULL);
1152         
1153         return g_object_new (EMPATHY_TYPE_CONTACT_LIST_VIEW,
1154                              "store", store,
1155                              "contact-features", contact_features,
1156                              "list-features", list_features,
1157                              NULL);
1158 }
1159
1160 EmpathyContact *
1161 empathy_contact_list_view_get_selected (EmpathyContactListView *view)
1162 {
1163         EmpathyContactListViewPriv *priv;
1164         GtkTreeSelection          *selection;
1165         GtkTreeIter                iter;
1166         GtkTreeModel              *model;
1167         EmpathyContact             *contact;
1168
1169         g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), NULL);
1170
1171         priv = GET_PRIV (view);
1172
1173         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
1174         if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
1175                 return NULL;
1176         }
1177
1178         gtk_tree_model_get (model, &iter,
1179                             EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
1180                             -1);
1181
1182         return contact;
1183 }
1184
1185 gchar *
1186 empathy_contact_list_view_get_selected_group (EmpathyContactListView *view)
1187 {
1188         EmpathyContactListViewPriv *priv;
1189         GtkTreeSelection          *selection;
1190         GtkTreeIter                iter;
1191         GtkTreeModel              *model;
1192         gboolean                   is_group;
1193         gchar                     *name;
1194
1195         g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), NULL);
1196
1197         priv = GET_PRIV (view);
1198
1199         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
1200         if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
1201                 return NULL;
1202         }
1203
1204         gtk_tree_model_get (model, &iter,
1205                             EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
1206                             EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
1207                             -1);
1208
1209         if (!is_group) {
1210                 g_free (name);
1211                 return NULL;
1212         }
1213
1214         return name;
1215 }
1216
1217 static gboolean
1218 contact_list_view_remove_dialog_show (GtkWindow   *parent, 
1219                                       const gchar *message, 
1220                                       const gchar *secondary_text)
1221 {
1222         GtkWidget *dialog;
1223         gboolean res;
1224         
1225         dialog = gtk_message_dialog_new (parent, GTK_DIALOG_MODAL,
1226                                          GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
1227                                          "%s", message);
1228         gtk_dialog_add_buttons (GTK_DIALOG (dialog),
1229                                 GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
1230                                 GTK_STOCK_DELETE, GTK_RESPONSE_YES,
1231                                 NULL);
1232         gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
1233                                                   "%s", secondary_text);
1234          
1235         gtk_widget_show (dialog);
1236          
1237         res = gtk_dialog_run (GTK_DIALOG (dialog));
1238         gtk_widget_destroy (dialog);
1239
1240         return (res == GTK_RESPONSE_YES);
1241 }
1242
1243 static void
1244 contact_list_view_group_remove_activate_cb (GtkMenuItem            *menuitem,
1245                                             EmpathyContactListView *view)
1246 {
1247         EmpathyContactListViewPriv *priv = GET_PRIV (view);
1248         gchar                      *group;
1249
1250         group = empathy_contact_list_view_get_selected_group (view);
1251         if (group) {
1252                 gchar     *text;
1253                 GtkWindow *parent;
1254
1255                 text = g_strdup_printf (_("Do you really want to remove the group '%s'?"), group);
1256                 parent = empathy_get_toplevel_window (GTK_WIDGET (view));
1257                 if (contact_list_view_remove_dialog_show (parent, _("Removing group"), text)) {
1258                         EmpathyContactList *list;
1259
1260                         list = empathy_contact_list_store_get_list_iface (priv->store);
1261                         empathy_contact_list_remove_group (list, group);
1262                 }
1263
1264                 g_free (text);
1265         }
1266
1267         g_free (group);
1268 }
1269
1270 GtkWidget *
1271 empathy_contact_list_view_get_group_menu (EmpathyContactListView *view)
1272 {
1273         EmpathyContactListViewPriv *priv = GET_PRIV (view);
1274         gchar                      *group;
1275         GtkWidget                  *menu;
1276         GtkWidget                  *item;
1277         GtkWidget                  *image;
1278
1279         g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), NULL);
1280
1281         if (!(priv->list_features & (EMPATHY_CONTACT_LIST_FEATURE_GROUPS_RENAME |
1282                                      EMPATHY_CONTACT_LIST_FEATURE_GROUPS_REMOVE))) {
1283                 return NULL;
1284         }
1285
1286         group = empathy_contact_list_view_get_selected_group (view);
1287         if (!group) {
1288                 return NULL;
1289         }
1290
1291         menu = gtk_menu_new ();
1292
1293         /* FIXME: Not implemented yet
1294         if (priv->features & EMPATHY_CONTACT_LIST_FEATURE_GROUPS_RENAME) {
1295                 item = gtk_menu_item_new_with_mnemonic (_("Re_name"));
1296                 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1297                 gtk_widget_show (item);
1298                 g_signal_connect (item, "activate",
1299                                   G_CALLBACK (contact_list_view_group_rename_activate_cb),
1300                                   view);
1301         }*/
1302
1303         if (priv->list_features & EMPATHY_CONTACT_LIST_FEATURE_GROUPS_REMOVE) {
1304                 item = gtk_image_menu_item_new_with_mnemonic (_("_Remove"));
1305                 image = gtk_image_new_from_icon_name (GTK_STOCK_REMOVE,
1306                                                       GTK_ICON_SIZE_MENU);
1307                 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1308                 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1309                 gtk_widget_show (item);
1310                 g_signal_connect (item, "activate",
1311                                   G_CALLBACK (contact_list_view_group_remove_activate_cb),
1312                                   view);
1313         }
1314
1315         g_free (group);
1316
1317         return menu;
1318 }
1319
1320 static void
1321 contact_list_view_remove_activate_cb (GtkMenuItem            *menuitem,
1322                                       EmpathyContactListView *view)
1323 {
1324         EmpathyContactListViewPriv *priv = GET_PRIV (view);
1325         EmpathyContact             *contact;
1326                 
1327         contact = empathy_contact_list_view_get_selected (view);
1328
1329         if (contact) {
1330                 gchar     *text; 
1331                 GtkWindow *parent;
1332
1333                 parent = empathy_get_toplevel_window (GTK_WIDGET (view));
1334                 text = g_strdup_printf (_("Do you really want to remove the contact '%s'?"),
1335                                         empathy_contact_get_name (contact));                                            
1336                 if (contact_list_view_remove_dialog_show (parent, _("Removing contact"), text)) {
1337                         EmpathyContactList *list;
1338
1339                         list = empathy_contact_list_store_get_list_iface (priv->store);
1340                         empathy_contact_list_remove (list, contact, 
1341                                 _("Sorry, I don't want you in my contact list anymore."));
1342                 }
1343
1344                 g_free (text);
1345                 g_object_unref (contact);
1346         }
1347 }
1348
1349 GtkWidget *
1350 empathy_contact_list_view_get_contact_menu (EmpathyContactListView *view)
1351 {
1352         EmpathyContactListViewPriv *priv = GET_PRIV (view);
1353         EmpathyContact             *contact;
1354         GtkWidget                  *menu;
1355         GtkWidget                  *item;
1356         GtkWidget                  *image;
1357
1358         g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), NULL);
1359
1360         contact = empathy_contact_list_view_get_selected (view);
1361         if (!contact) {
1362                 return NULL;
1363         }
1364
1365         menu = empathy_contact_menu_new (contact, priv->contact_features);
1366
1367         if (!(priv->list_features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_REMOVE)) {
1368                 g_object_unref (contact);
1369                 return menu;
1370         }
1371
1372         if (menu) {
1373                 /* Separator */
1374                 item = gtk_separator_menu_item_new ();
1375                 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1376                 gtk_widget_show (item);
1377         } else {
1378                 menu = gtk_menu_new ();
1379         }
1380
1381         /* Remove contact */
1382         if (priv->list_features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_REMOVE) {
1383                 item = gtk_image_menu_item_new_with_mnemonic (_("_Remove"));
1384                 image = gtk_image_new_from_icon_name (GTK_STOCK_REMOVE,
1385                                                       GTK_ICON_SIZE_MENU);
1386                 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1387                 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1388                 gtk_widget_show (item);
1389                 g_signal_connect (item, "activate",
1390                                   G_CALLBACK (contact_list_view_remove_activate_cb),
1391                                   view);
1392         }
1393
1394         g_object_unref (contact);
1395
1396         return menu;
1397 }
1398