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