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