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