d034076df059c44d52dc37ae43fb3096d6916015
[modest] / src / widgets / modest-folder-view.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <glib/gi18n.h>
31 #include <string.h>
32
33 #include <tny-account-store-view.h>
34 #include <tny-gtk-account-list-model.h>
35 #include <tny-gtk-folder-store-tree-model.h>
36 #include <tny-gtk-header-list-model.h>
37 #include <tny-folder.h>
38 #include <tny-account-store.h>
39 #include <tny-account.h>
40 #include <tny-folder.h>
41 #include <tny-camel-folder.h>
42 #include <tny-simple-list.h>
43 #include <modest-tny-folder.h>
44 #include <modest-marshal.h>
45 #include <modest-icon-names.h>
46 #include <modest-tny-account-store.h>
47 #include <modest-text-utils.h>
48 #include <modest-runtime.h>
49 #include "modest-folder-view.h"
50 #include <modest-dnd.h>
51
52 /* 'private'/'protected' functions */
53 static void modest_folder_view_class_init  (ModestFolderViewClass *klass);
54 static void modest_folder_view_init        (ModestFolderView *obj);
55 static void modest_folder_view_finalize    (GObject *obj);
56
57 static void         tny_account_store_view_init (gpointer g, 
58                                                  gpointer iface_data);
59
60 static void         modest_folder_view_set_account_store (TnyAccountStoreView *self, 
61                                                           TnyAccountStore     *account_store);
62
63 static gboolean     update_model           (ModestFolderView *self,
64                                             ModestTnyAccountStore *account_store);
65
66 static void         on_selection_changed   (GtkTreeSelection *sel, gpointer data);
67
68 static void         on_account_update      (TnyAccountStore *account_store, 
69                                             const gchar *account,
70                                             gpointer user_data);
71
72 static void         on_accounts_reloaded   (TnyAccountStore *store, 
73                                             gpointer user_data);
74
75 static gint         cmp_rows               (GtkTreeModel *tree_model, 
76                                             GtkTreeIter *iter1, 
77                                             GtkTreeIter *iter2,
78                                             gpointer user_data);
79
80 /* DnD functions */
81 static void         on_drag_data_get       (GtkWidget *widget, 
82                                             GdkDragContext *context, 
83                                             GtkSelectionData *selection_data, 
84                                             guint info, 
85                                             guint time, 
86                                             gpointer data);
87
88 static void         on_drag_data_received  (GtkWidget *widget, 
89                                             GdkDragContext *context, 
90                                             gint x, 
91                                             gint y, 
92                                             GtkSelectionData *selection_data, 
93                                             guint info, 
94                                             guint time, 
95                                             gpointer data);
96
97 static gboolean     on_drag_motion         (GtkWidget      *widget,
98                                             GdkDragContext *context,
99                                             gint            x,
100                                             gint            y,
101                                             guint           time,
102                                             gpointer        user_data);
103
104 static gint         expand_row_timeout     (gpointer data);
105
106 static void         setup_drag_and_drop    (GtkTreeView *self);
107
108 enum {
109         FOLDER_SELECTION_CHANGED_SIGNAL,
110         LAST_SIGNAL
111 };
112
113 typedef struct _ModestFolderViewPrivate ModestFolderViewPrivate;
114 struct _ModestFolderViewPrivate {
115         TnyAccountStore     *account_store;
116         TnyFolder           *cur_folder;
117         GtkTreeRowReference *cur_row;
118
119         gulong               account_update_signal;
120         gulong               changed_signal;
121         gulong               accounts_reloaded_signal;
122         
123         GtkTreeSelection    *cur_selection;
124         TnyFolderStoreQuery *query;
125         guint                timer_expander;
126 };
127 #define MODEST_FOLDER_VIEW_GET_PRIVATE(o)                       \
128         (G_TYPE_INSTANCE_GET_PRIVATE((o),                       \
129                                      MODEST_TYPE_FOLDER_VIEW,   \
130                                      ModestFolderViewPrivate))
131 /* globals */
132 static GObjectClass *parent_class = NULL;
133
134 static guint signals[LAST_SIGNAL] = {0}; 
135
136 GType
137 modest_folder_view_get_type (void)
138 {
139         static GType my_type = 0;
140         if (!my_type) {
141                 static const GTypeInfo my_info = {
142                         sizeof(ModestFolderViewClass),
143                         NULL,           /* base init */
144                         NULL,           /* base finalize */
145                         (GClassInitFunc) modest_folder_view_class_init,
146                         NULL,           /* class finalize */
147                         NULL,           /* class data */
148                         sizeof(ModestFolderView),
149                         1,              /* n_preallocs */
150                         (GInstanceInitFunc) modest_folder_view_init,
151                         NULL
152                 };
153
154                 static const GInterfaceInfo tny_account_store_view_info = {
155                         (GInterfaceInitFunc) tny_account_store_view_init, /* interface_init */
156                         NULL,         /* interface_finalize */
157                         NULL          /* interface_data */
158                 };
159
160                                 
161                 my_type = g_type_register_static (GTK_TYPE_TREE_VIEW,
162                                                   "ModestFolderView",
163                                                   &my_info, 0);
164
165                 g_type_add_interface_static (my_type, 
166                                              TNY_TYPE_ACCOUNT_STORE_VIEW, 
167                                              &tny_account_store_view_info);
168         }
169         return my_type;
170 }
171
172 static void
173 modest_folder_view_class_init (ModestFolderViewClass *klass)
174 {
175         GObjectClass *gobject_class;
176         gobject_class = (GObjectClass*) klass;
177
178         parent_class            = g_type_class_peek_parent (klass);
179         gobject_class->finalize = modest_folder_view_finalize;
180
181         g_type_class_add_private (gobject_class,
182                                   sizeof(ModestFolderViewPrivate));
183         
184         signals[FOLDER_SELECTION_CHANGED_SIGNAL] = 
185                 g_signal_new ("folder_selection_changed",
186                               G_TYPE_FROM_CLASS (gobject_class),
187                               G_SIGNAL_RUN_FIRST,
188                               G_STRUCT_OFFSET (ModestFolderViewClass,
189                                                folder_selection_changed),
190                               NULL, NULL,
191                               modest_marshal_VOID__POINTER_BOOLEAN,
192                               G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_BOOLEAN);
193 }
194
195
196
197 static void
198 text_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
199                  GtkTreeModel *tree_model,  GtkTreeIter *iter,  gpointer data)
200 {
201         GObject *rendobj;
202         gchar *fname = NULL;
203         gint unread;
204         TnyFolderType type;
205         TnyFolder *folder = NULL;
206         
207         g_return_if_fail (column);
208         g_return_if_fail (tree_model);
209
210         gtk_tree_model_get (tree_model, iter,
211                             TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &fname,
212                             TNY_GTK_FOLDER_STORE_TREE_MODEL_UNREAD_COLUMN, &unread,
213                             TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
214                             TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder,
215                             -1);
216         rendobj = G_OBJECT(renderer);
217  
218         if (!fname)
219                 return;
220         
221         if (folder && type != TNY_FOLDER_TYPE_ROOT) { /* FIXME: tnymail bug? crashes with root folders */
222                 if (modest_tny_folder_is_local_folder (folder)) {
223                         TnyFolderType type;
224                         type = modest_tny_folder_get_local_folder_type (folder);
225                         if (type != TNY_FOLDER_TYPE_UNKNOWN) {
226                                 g_free (fname);
227                                 fname = g_strdup(modest_local_folder_info_get_type_display_name (type));
228                         }
229                 }
230         } else if (folder && type == TNY_FOLDER_TYPE_ROOT) {
231                 /* FIXME: todo */
232         }
233                         
234         if (unread > 0) {
235                 gchar *folder_title = g_strdup_printf ("%s (%d)", fname, unread);
236                 g_object_set (rendobj,"text", folder_title,  "weight", 800, NULL);
237                 g_free (folder_title);
238         } else 
239                 g_object_set (rendobj,"text", fname, "weight", 400, NULL);
240                 
241         g_free (fname);
242         if (folder) g_object_unref (G_OBJECT (folder));
243 }
244
245
246 static GdkPixbuf*
247 get_cached_icon (const gchar *name)
248 {
249         GError *err = NULL;
250         gpointer pixbuf;
251         gpointer orig_key;
252         static GHashTable *icon_cache = NULL;
253         
254         g_return_val_if_fail (name, NULL);
255
256         if (G_UNLIKELY(!icon_cache))
257                 icon_cache = modest_cache_mgr_get_cache (modest_runtime_get_cache_mgr(),
258                                                          MODEST_CACHE_MGR_CACHE_TYPE_PIXBUF);
259         
260         if (!icon_cache || !g_hash_table_lookup_extended (icon_cache, name, &orig_key, &pixbuf)) {
261                 pixbuf = (gpointer)gdk_pixbuf_new_from_file (name, &err);
262                 if (!pixbuf) {
263                         g_printerr ("modest: error in icon factory while loading '%s': %s\n",
264                                     name, err->message);
265                         g_error_free (err);
266                 }
267                 /* if we cannot find it, we still insert (if we have a cache), so we get the error
268                  * only once */
269                 if (icon_cache)
270                         g_hash_table_insert (icon_cache, g_strdup(name),(gpointer)pixbuf);
271         }
272         return GDK_PIXBUF(pixbuf);
273 }
274
275
276 static void
277 icon_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
278                  GtkTreeModel *tree_model,  GtkTreeIter *iter, gpointer data)
279 {
280         GObject *rendobj;
281         GdkPixbuf *pixbuf;
282         TnyFolderType type;
283         gchar *fname = NULL;
284         gint unread;
285         
286         rendobj = G_OBJECT(renderer);
287         gtk_tree_model_get (tree_model, iter,
288                             TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
289                             TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &fname,
290                             TNY_GTK_FOLDER_STORE_TREE_MODEL_UNREAD_COLUMN, &unread, 
291                             -1);
292         rendobj = G_OBJECT(renderer);
293         
294         if (type == TNY_FOLDER_TYPE_NORMAL || type == TNY_FOLDER_TYPE_UNKNOWN) {
295                 type = modest_tny_folder_guess_folder_type_from_name (fname);
296         }
297         g_free (fname);
298
299         switch (type) {
300         case TNY_FOLDER_TYPE_ROOT:
301                 pixbuf = get_cached_icon (MODEST_FOLDER_ICON_ACCOUNT);
302                 break;
303         case TNY_FOLDER_TYPE_INBOX:
304                 pixbuf = get_cached_icon (MODEST_FOLDER_ICON_INBOX);
305                 break;
306         case TNY_FOLDER_TYPE_OUTBOX:
307                 pixbuf = get_cached_icon (MODEST_FOLDER_ICON_OUTBOX);
308                 break;
309         case TNY_FOLDER_TYPE_JUNK:
310                 pixbuf = get_cached_icon (MODEST_FOLDER_ICON_JUNK);
311                 break;
312         case TNY_FOLDER_TYPE_SENT:
313                 pixbuf = get_cached_icon (MODEST_FOLDER_ICON_SENT);
314                 break;
315         case TNY_FOLDER_TYPE_TRASH:
316                 pixbuf = get_cached_icon (MODEST_FOLDER_ICON_TRASH);
317                 break;
318         case TNY_FOLDER_TYPE_DRAFTS:
319                 pixbuf = get_cached_icon (MODEST_FOLDER_ICON_DRAFTS);
320                 break;
321         case TNY_FOLDER_TYPE_NOTES:
322                 pixbuf = get_cached_icon (MODEST_FOLDER_ICON_NOTES);
323                 break;
324         case TNY_FOLDER_TYPE_CALENDAR:
325                 pixbuf = get_cached_icon (MODEST_FOLDER_ICON_CALENDAR);
326                 break;
327         case TNY_FOLDER_TYPE_CONTACTS:
328                 pixbuf = get_cached_icon (MODEST_FOLDER_ICON_CONTACTS);
329                 break;
330         case TNY_FOLDER_TYPE_NORMAL:
331         default:
332                 pixbuf = get_cached_icon (MODEST_FOLDER_ICON_NORMAL);
333                 break;
334         }
335         g_object_set (rendobj, "pixbuf", pixbuf, NULL);
336 }
337
338 static void
339 modest_folder_view_init (ModestFolderView *obj)
340 {
341         ModestFolderViewPrivate *priv;
342         GtkTreeViewColumn *column;
343         GtkCellRenderer *renderer;
344         GtkTreeSelection *sel;
345         
346         priv =  MODEST_FOLDER_VIEW_GET_PRIVATE(obj);
347         
348         priv->timer_expander = 0;
349         priv->account_store  = NULL;
350         priv->cur_folder     = NULL;
351         priv->cur_row        = NULL;
352         priv->query          = NULL;
353
354         column = gtk_tree_view_column_new ();   
355         gtk_tree_view_append_column (GTK_TREE_VIEW(obj),column);
356         
357         renderer = gtk_cell_renderer_pixbuf_new();
358         gtk_tree_view_column_pack_start (column, renderer, FALSE);
359         gtk_tree_view_column_set_cell_data_func(column, renderer,
360                                                 icon_cell_data, NULL, NULL);
361         
362         renderer = gtk_cell_renderer_text_new();
363         gtk_tree_view_column_pack_start (column, renderer, FALSE);
364         gtk_tree_view_column_set_cell_data_func(column, renderer,
365                                                 text_cell_data, NULL, NULL);
366         
367         sel = gtk_tree_view_get_selection (GTK_TREE_VIEW(obj));
368         gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
369
370         gtk_tree_view_column_set_spacing (column, 2);
371         gtk_tree_view_column_set_resizable (column, TRUE);
372         gtk_tree_view_column_set_fixed_width (column, TRUE);            
373         gtk_tree_view_set_headers_clickable (GTK_TREE_VIEW(obj), FALSE);
374         gtk_tree_view_set_enable_search     (GTK_TREE_VIEW(obj), FALSE);
375
376         setup_drag_and_drop (GTK_TREE_VIEW(obj));
377 }
378
379 static void
380 tny_account_store_view_init (gpointer g, gpointer iface_data)
381 {
382         TnyAccountStoreViewIface *klass = (TnyAccountStoreViewIface *)g;
383
384         klass->set_account_store_func = modest_folder_view_set_account_store;
385
386         return;
387 }
388
389 static void
390 modest_folder_view_finalize (GObject *obj)
391 {
392         ModestFolderViewPrivate *priv;
393         GtkTreeSelection    *sel;
394         
395         g_return_if_fail (obj);
396         
397         priv =  MODEST_FOLDER_VIEW_GET_PRIVATE(obj);
398
399         if (priv->timer_expander != 0) {
400                 g_source_remove (priv->timer_expander);
401                 priv->timer_expander = 0;
402         }
403
404         if (priv->account_store) {
405                 g_signal_handler_disconnect (G_OBJECT(priv->account_store),
406                                              priv->account_update_signal);
407                 g_signal_handler_disconnect (G_OBJECT(priv->account_store),
408                                              priv->accounts_reloaded_signal);
409                 g_object_unref (G_OBJECT(priv->account_store));
410                 priv->account_store = NULL;
411         }
412
413         if (priv->query) {
414                 g_object_unref (G_OBJECT (priv->query));
415                 priv->query = NULL;
416         }
417
418         sel = gtk_tree_view_get_selection (GTK_TREE_VIEW(obj));
419         if (sel)
420                 g_signal_handler_disconnect (G_OBJECT(sel), priv->changed_signal);
421         
422         G_OBJECT_CLASS(parent_class)->finalize (obj);
423 }
424
425
426 static void
427 modest_folder_view_set_account_store (TnyAccountStoreView *self, TnyAccountStore *account_store)
428 {
429         ModestFolderViewPrivate *priv;
430         TnyDevice *device;
431
432         g_return_if_fail (MODEST_IS_FOLDER_VIEW (self));
433         g_return_if_fail (TNY_IS_ACCOUNT_STORE (account_store));
434
435         priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
436         device = tny_account_store_get_device (account_store);
437
438         if (G_UNLIKELY (priv->account_store)) {
439
440                 if (g_signal_handler_is_connected (G_OBJECT (priv->account_store), 
441                                                    priv->account_update_signal))
442                         g_signal_handler_disconnect (G_OBJECT (priv->account_store), 
443                                                      priv->account_update_signal);
444                 if (g_signal_handler_is_connected (G_OBJECT (priv->account_store), 
445                                                    priv->accounts_reloaded_signal))
446                         g_signal_handler_disconnect (G_OBJECT (priv->account_store), 
447                                                      priv->accounts_reloaded_signal);
448
449                 g_object_unref (G_OBJECT (priv->account_store));
450         }
451
452         priv->account_store = g_object_ref (G_OBJECT (account_store));
453
454         priv->account_update_signal = 
455                 g_signal_connect (G_OBJECT(account_store), "account_update",
456                                   G_CALLBACK (on_account_update), self);
457
458         priv->accounts_reloaded_signal = 
459                 g_signal_connect (G_OBJECT(account_store), "accounts_reloaded",
460                                   G_CALLBACK (on_accounts_reloaded), self);
461         
462         if (!update_model (MODEST_FOLDER_VIEW (self),
463                            MODEST_TNY_ACCOUNT_STORE (priv->account_store)))
464                 g_printerr ("modest: failed to update model\n");
465
466         g_object_unref (G_OBJECT (device));
467 }
468
469 static void
470 on_account_update (TnyAccountStore *account_store, const gchar *account,
471                    gpointer user_data)
472 {
473         if (!update_model (MODEST_FOLDER_VIEW(user_data), 
474                            MODEST_TNY_ACCOUNT_STORE(account_store)))
475                 g_printerr ("modest: failed to update model for changes in '%s'",
476                             account);
477 }
478
479 static void 
480 on_accounts_reloaded   (TnyAccountStore *account_store, 
481                         gpointer user_data)
482 {
483         update_model (MODEST_FOLDER_VIEW (user_data), 
484                       MODEST_TNY_ACCOUNT_STORE(account_store));
485 }
486
487 void
488 modest_folder_view_set_title (ModestFolderView *self, const gchar *title)
489 {
490         GtkTreeViewColumn *col;
491         
492         g_return_if_fail (self);
493
494         col = gtk_tree_view_get_column (GTK_TREE_VIEW(self), 0);
495         if (!col) {
496                 g_printerr ("modest: failed get column for title\n");
497                 return;
498         }
499
500         gtk_tree_view_column_set_title (col, title);
501         gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(self),
502                                            title != NULL);
503 }
504
505 GtkWidget*
506 modest_folder_view_new (TnyFolderStoreQuery *query)
507 {
508         GObject *self;
509         ModestFolderViewPrivate *priv;
510         GtkTreeSelection *sel;
511         
512         self = G_OBJECT (g_object_new (MODEST_TYPE_FOLDER_VIEW, NULL));
513         priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
514
515         priv->query = g_object_ref (query);
516         
517         sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(self));
518         priv->changed_signal = g_signal_connect (sel, "changed",
519                                                  G_CALLBACK (on_selection_changed), self);
520         return GTK_WIDGET(self);
521 }
522
523 /* this feels dirty; any other way to expand all the root items? */
524 static void
525 expand_root_items (ModestFolderView *self)
526 {
527         GtkTreePath *path;
528         path = gtk_tree_path_new_first ();
529
530         /* all folders should have child items, so.. */
531         while (gtk_tree_view_expand_row (GTK_TREE_VIEW(self), path, FALSE))
532                 gtk_tree_path_next (path);
533         
534         gtk_tree_path_free (path);
535 }
536
537 static gboolean
538 update_model (ModestFolderView *self, ModestTnyAccountStore *account_store)
539 {
540         ModestFolderViewPrivate *priv;
541
542         TnyList          *account_list;
543         GtkTreeModel     *model, *sortable;
544
545         g_return_val_if_fail (account_store, FALSE);
546
547         priv =  MODEST_FOLDER_VIEW_GET_PRIVATE(self);
548         
549         /* Notify that there is no folder selected */
550         g_signal_emit (G_OBJECT(self), 
551                        signals[FOLDER_SELECTION_CHANGED_SIGNAL], 0,
552                        NULL, TRUE);
553         
554         /* FIXME: the local accounts are not shown when the query
555            selects only the subscribed folders. */
556 /*      model        = tny_gtk_folder_store_tree_model_new (TRUE, priv->query); */
557         model        = tny_gtk_folder_store_tree_model_new (TRUE, NULL);
558         account_list = TNY_LIST(model);
559
560         tny_account_store_get_accounts (TNY_ACCOUNT_STORE(account_store),
561                                         account_list,
562                                         TNY_ACCOUNT_STORE_STORE_ACCOUNTS);      
563         if (account_list) {
564                 sortable = gtk_tree_model_sort_new_with_model (model);
565                 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE(sortable),
566                                                       TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, 
567                                                       GTK_SORT_ASCENDING);
568                 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (sortable),
569                                                  TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN,
570                                                  cmp_rows, NULL, NULL);
571
572                 /* Set new model */
573                 gtk_tree_view_set_model (GTK_TREE_VIEW(self), sortable);
574                 expand_root_items (self); /* expand all account folders */
575                 g_object_unref (account_list);
576         }
577         
578         return TRUE;
579 }
580
581
582 static void
583 on_selection_changed (GtkTreeSelection *sel, gpointer user_data)
584 {
585         GtkTreeModel            *model_sort, *model;
586         TnyFolder               *folder = NULL;
587         GtkTreeIter             iter, iter_sort;
588         GtkTreePath            *path;
589         ModestFolderView        *tree_view;
590         ModestFolderViewPrivate *priv;
591         gint                    type;
592
593         g_return_if_fail (sel);
594         g_return_if_fail (user_data);
595         
596         priv = MODEST_FOLDER_VIEW_GET_PRIVATE(user_data);
597         priv->cur_selection = sel;
598         
599         /* folder was _un_selected if true */
600         if (!gtk_tree_selection_get_selected (sel, &model_sort, &iter_sort)) {
601                 priv->cur_folder = NULL; /* FIXME: need this? */
602                 gtk_tree_row_reference_free (priv->cur_row);
603                 priv->cur_row = NULL;
604                 return; 
605         }
606
607         model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model_sort));
608         gtk_tree_model_sort_convert_iter_to_child_iter (GTK_TREE_MODEL_SORT (model_sort),
609                                                         &iter,
610                                                         &iter_sort);
611
612         tree_view = MODEST_FOLDER_VIEW (user_data);
613
614         gtk_tree_model_get (model, &iter,
615                             TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
616                             TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder,
617                             -1);
618
619         if (type == TNY_FOLDER_TYPE_ROOT) {
620                 g_object_unref (folder);
621                 return;
622         }
623         
624         /* Current folder was unselected */
625         g_signal_emit (G_OBJECT(tree_view), signals[FOLDER_SELECTION_CHANGED_SIGNAL], 0,
626                        priv->cur_folder, FALSE);
627
628         if (priv->cur_row) {
629 /*              tny_folder_sync (priv->cur_folder, TRUE, NULL); /\* FIXME *\/ */
630                 gtk_tree_row_reference_free (priv->cur_row);
631         }
632
633         /* New current references */
634         path = gtk_tree_model_get_path (model_sort, &iter_sort);
635         priv->cur_folder = folder;
636         priv->cur_row = gtk_tree_row_reference_new (model_sort, path);
637
638         /* Frees */
639         gtk_tree_path_free (path);
640         g_object_unref (G_OBJECT (folder));
641
642         /* New folder has been selected */
643         g_signal_emit (G_OBJECT(tree_view), 
644                        signals[FOLDER_SELECTION_CHANGED_SIGNAL], 
645                        0, folder, TRUE); 
646 }
647
648 TnyFolder *
649 modest_folder_view_get_selected (ModestFolderView *self)
650 {
651         ModestFolderViewPrivate *priv;
652
653         g_return_val_if_fail (self, NULL);
654         
655         priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
656         if (priv->cur_folder)
657                 g_object_ref (priv->cur_folder);
658
659         return priv->cur_folder;
660 }
661
662 /* static gboolean */
663 /* get_model_iter (ModestFolderView *self,  */
664 /*              GtkTreeModel **model,  */
665 /*              GtkTreeIter *iter) */
666 /* { */
667 /*      GtkTreeModel *model_sort; */
668 /*      GtkTreeIter iter_sort; */
669 /*      GtkTreePath *path; */
670 /*      ModestFolderViewPrivate *priv; */
671
672 /*      priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self); */
673
674 /*      if (!priv->cur_folder) */
675 /*              return FALSE; */
676
677 /*      if (!gtk_tree_row_reference_valid (priv->cur_row)) */
678 /*              return FALSE; */
679
680 /*      model_sort = gtk_tree_view_get_model (GTK_TREE_VIEW (self)); */
681 /*      *model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model_sort)); */
682
683 /*      /\* Get path to retrieve iter *\/ */
684 /*      path = gtk_tree_row_reference_get_path (priv->cur_row); */
685 /*      if (!gtk_tree_model_get_iter (model_sort, &iter_sort, path)) */
686 /*              return FALSE; */
687
688 /*      gtk_tree_model_sort_convert_iter_to_child_iter (GTK_TREE_MODEL_SORT (model_sort), */
689 /*                                                      iter, */
690 /*                                                      &iter_sort); */
691 /*      return TRUE; */
692 /* } */
693
694 static gint
695 cmp_rows (GtkTreeModel *tree_model, GtkTreeIter *iter1, GtkTreeIter *iter2,
696           gpointer user_data)
697 {
698         gint cmp;
699         gchar         *name1, *name2;
700         TnyFolderType type;
701         TnyFolder     *folder1, *folder2;
702         
703         gtk_tree_model_get (tree_model, iter1,
704                             TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &name1,
705                             TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
706                             TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder1,
707                             -1);
708         gtk_tree_model_get (tree_model, iter2,
709                             TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &name2,
710                             TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder2,
711                             -1);
712
713         /* local_folders should be the last one */
714         if (type == TNY_FOLDER_TYPE_ROOT) {
715                 /* the account name is also the name of the root folder
716                  * in case of local folders */
717                 if (name1 && strcmp (name1, MODEST_LOCAL_FOLDERS_ACCOUNT_NAME) == 0)
718                         cmp = +1;
719                 else if (name2 && strcmp (name2, MODEST_LOCAL_FOLDERS_ACCOUNT_NAME) == 0)
720                         cmp = -1;
721                 else 
722                         cmp = modest_text_utils_utf8_strcmp (name1, name2, TRUE);
723         } else 
724                 cmp = modest_text_utils_utf8_strcmp (name1, name2, TRUE);
725
726         
727         if (folder1)
728                 g_object_unref(G_OBJECT(folder1));
729         if (folder2)
730                 g_object_unref(G_OBJECT(folder2));
731         
732         g_free (name1);
733         g_free (name2);
734
735         return cmp;     
736 }
737
738 /*****************************************************************************/
739 /*                        DRAG and DROP stuff                                */
740 /*****************************************************************************/
741
742 /*
743  * This function fills the #GtkSelectionData with the row and the
744  * model that has been dragged. It's called when this widget is a
745  * source for dnd after the event drop happened
746  */
747 static void
748 on_drag_data_get (GtkWidget *widget, 
749                   GdkDragContext *context, 
750                   GtkSelectionData *selection_data, 
751                   guint info, 
752                   guint time, 
753                   gpointer data)
754 {
755         GtkTreeSelection *selection;
756         GtkTreeModel *model_sort, *model;
757         GtkTreeIter iter;
758         GtkTreePath *source_row_sort;
759         GtkTreePath *source_row;
760
761         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (widget));
762         gtk_tree_selection_get_selected (selection, &model_sort, &iter);
763         source_row_sort = gtk_tree_model_get_path (model_sort, &iter);
764
765         /* Get the unsorted path and model */
766         model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model_sort));
767         source_row = gtk_tree_model_sort_convert_path_to_child_path (GTK_TREE_MODEL_SORT (model_sort),
768                                                                      source_row_sort);
769
770         gtk_tree_set_row_drag_data (selection_data,
771                                     model,
772                                     source_row);
773
774         gtk_tree_path_free (source_row_sort);
775         gtk_tree_path_free (source_row);
776 }
777
778 typedef struct _DndHelper {
779         gboolean delete_source;
780         GtkWidget *source_widget;
781         GtkWidget *dest_widget;
782         GtkTreePath *source_row;
783         GdkDragContext *context;
784         guint time;
785 } DndHelper;
786
787
788 /*
789  * This function saves the source row in the source widget, will be
790  * used by the drag-data-delete handler to remove the source row
791  */
792 static void
793 save_and_clean (DndHelper *helper, 
794                 gboolean success)
795 {
796         /* Save row data */
797         if (success && helper->delete_source)
798                 g_object_set_data (G_OBJECT (helper->source_widget),
799                                    ROW_REF_DATA_NAME,
800                                    gtk_tree_path_copy (helper->source_row));
801
802         /* Clean dest row */
803         gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (helper->dest_widget),
804                                          NULL,
805                                          GTK_TREE_VIEW_DROP_BEFORE);
806
807 }
808
809 /*
810  * This function is the callback of the
811  * modest_mail_operation_xfer_msg() call. We check here if the message
812  * was correctly asynchronously transfered
813  */
814 static void
815 on_progress_changed (ModestMailOperation *mail_op, gpointer user_data)
816 {
817         ModestMailOperationQueue *queue;
818         gboolean success = FALSE;
819         DndHelper *helper;
820
821         helper = (DndHelper *) user_data;
822
823         if (modest_mail_operation_get_status (mail_op) == 
824             MODEST_MAIL_OPERATION_STATUS_SUCCESS) {
825                 success = TRUE;
826         } else {
827                 const GError *error;
828                 error = modest_mail_operation_get_error (mail_op);
829                 g_warning ("Error transferring messages: %s\n", error->message);
830         }
831
832         /* Remove the mail operation */ 
833         queue = modest_runtime_get_mail_operation_queue ();
834         modest_mail_operation_queue_remove (queue, mail_op);
835         g_object_unref (G_OBJECT (mail_op));
836
837         /* Save and clean */
838         save_and_clean (helper, success);       
839         
840         /* Notify the drag source */
841         gtk_drag_finish (helper->context, success, (success && helper->delete_source), helper->time);
842
843         /* Free the helper */
844         g_slice_free (DndHelper, helper);
845 }
846
847 /*
848  * This function is used by drag_data_received_cb to manage drag and
849  * drop of a header, i.e, and drag from the header view to the folder
850  * view.
851  */
852 static void
853 drag_and_drop_from_header_view (GtkTreeModel *source_model,
854                                 GtkTreeModel *dest_model,
855                                 GtkTreePath  *dest_row,
856                                 DndHelper    *helper)
857 {
858         TnyHeader *header;
859         TnyFolder *folder;
860         ModestMailOperationQueue *queue;
861         ModestMailOperation *mail_op;
862         gboolean started;
863         GtkTreeIter source_iter, dest_iter;
864
865         /* Get header */
866         gtk_tree_model_get_iter (source_model, &source_iter, helper->source_row);
867         gtk_tree_model_get (source_model, &source_iter, 
868                             TNY_GTK_HEADER_LIST_MODEL_INSTANCE_COLUMN, 
869                             &header, -1);
870
871         /* Get Folder */
872         gtk_tree_model_get_iter (dest_model, &dest_iter, dest_row);
873         gtk_tree_model_get (dest_model, &dest_iter, 
874                             TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, 
875                             &folder, -1);
876
877         /* Transfer message */
878         queue = modest_runtime_get_mail_operation_queue ();
879         mail_op = modest_mail_operation_new ();
880         started = modest_mail_operation_xfer_msg (mail_op, header,
881                                                   folder, helper->delete_source);
882         if (started) {
883                 g_signal_connect (G_OBJECT (mail_op), "progress_changed",
884                                   G_CALLBACK (on_progress_changed), helper);
885                 modest_mail_operation_queue_add (queue, mail_op);
886         } else {
887                 const GError *error;
888                 error = modest_mail_operation_get_error (mail_op);
889                 if (error)
890                         g_warning ("Error trying to transfer messages: %s\n",
891                                    error->message);
892
893                 g_slice_free (DndHelper, helper);
894         }
895
896         /* Frees */
897         g_object_unref (G_OBJECT (mail_op));
898         g_object_unref (G_OBJECT (header));
899         g_object_unref (G_OBJECT (folder));
900 }
901
902 /*
903  * This function is used by drag_data_received_cb to manage drag and
904  * drop of a folder, i.e, and drag from the folder view to the same
905  * folder view.
906  */
907 static void
908 drag_and_drop_from_folder_view (GtkTreeModel     *source_model,
909                                 GtkTreeModel     *dest_model,
910                                 GtkTreePath      *dest_row,
911                                 GtkSelectionData *selection_data,
912                                 DndHelper        *helper)
913 {
914         ModestMailOperation *mail_op;
915         const GError *error;
916         GtkTreeRowReference *source_row_reference;
917         GtkTreeIter parent_iter, iter;
918         TnyFolder *folder, *new_folder;
919         TnyFolderStore *parent_folder;
920         gboolean success = FALSE;
921
922         /* Check if the drag is possible */
923         if (!gtk_tree_path_compare (helper->source_row, dest_row))
924                 goto out;
925
926         if (!gtk_tree_drag_dest_row_drop_possible (GTK_TREE_DRAG_DEST (dest_model),
927                                                    dest_row,
928                                                    selection_data))
929                 goto out;
930
931         /* Get data */
932         gtk_tree_model_get_iter (source_model, &parent_iter, dest_row);
933         gtk_tree_model_get_iter (source_model, &iter, helper->source_row);
934         gtk_tree_model_get (source_model, &parent_iter, 
935                             TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, 
936                             &parent_folder, -1);
937         gtk_tree_model_get (source_model, &iter,
938                             TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN,
939                             &folder, -1);
940
941         /* Do the mail operation */
942         mail_op = modest_mail_operation_new ();
943         new_folder = modest_mail_operation_xfer_folder (mail_op, folder, parent_folder, 
944                                                         helper->delete_source);
945
946         g_object_unref (G_OBJECT (parent_folder));
947         g_object_unref (G_OBJECT (folder));
948
949         error = modest_mail_operation_get_error (mail_op);
950         if (error) {
951                 g_warning ("Error transferring folder: %s\n", error->message);
952                 g_object_unref (G_OBJECT (mail_op));
953                 goto out;
954         }
955         g_object_unref (G_OBJECT (mail_op));
956
957         /* Get a row reference to the source path because the path
958            could change after the insertion. The gtk_drag_finish() is
959            not able to delete the source because that, so we have to
960            do it manually */
961         source_row_reference = gtk_tree_row_reference_new (source_model, helper->source_row);
962         gtk_tree_path_free (helper->source_row);
963
964         /* Insert the dragged row as a child of the dest row */
965         gtk_tree_path_down (dest_row);
966         if (gtk_tree_drag_dest_drag_data_received (GTK_TREE_DRAG_DEST (dest_model),
967                                                    dest_row,
968                                                    selection_data)) {
969
970                 GtkTreeIter iter;
971
972                 /* Set the newly created folder as the instance in the row */
973                 gtk_tree_model_get_iter (dest_model, &iter, dest_row);
974                 gtk_tree_store_set (GTK_TREE_STORE (dest_model), &iter,
975                                     TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, 
976                                     new_folder, -1);
977                 g_object_unref (G_OBJECT (new_folder));         
978
979                 helper->source_row = gtk_tree_row_reference_get_path (source_row_reference);
980
981                 success = TRUE;
982         }
983         gtk_tree_row_reference_free (source_row_reference);
984
985         /* Save and clean */
986         save_and_clean (helper, success);       
987
988  out:
989         gtk_drag_finish (helper->context, success, (success && helper->delete_source), helper->time);
990 }
991
992 /*
993  * This function receives the data set by the "drag-data-get" signal
994  * handler. This information comes within the #GtkSelectionData. This
995  * function will manage both the drags of folders of the treeview and
996  * drags of headers of the header view widget.
997  */
998 static void 
999 on_drag_data_received (GtkWidget *widget, 
1000                        GdkDragContext *context, 
1001                        gint x, 
1002                        gint y, 
1003                        GtkSelectionData *selection_data, 
1004                        guint target_type, 
1005                        guint time, 
1006                        gpointer data)
1007 {
1008         GtkWidget *source_widget;
1009         GtkTreeModel *model_sort, *dest_model, *source_model;
1010         GtkTreePath *source_row, *dest_row, *child_dest_row;
1011         GtkTreeViewDropPosition pos;
1012         gboolean success = FALSE, delete_source = FALSE;
1013         DndHelper *helper;
1014
1015         /* Do not allow further process */
1016         g_signal_stop_emission_by_name (widget, "drag-data-received");
1017
1018         /* Get the action */
1019         if (context->action == GDK_ACTION_MOVE)
1020                 delete_source = TRUE;
1021
1022         /* Check if the get_data failed */
1023         if (selection_data == NULL || selection_data->length < 0)
1024                 gtk_drag_finish (context, success, (success && delete_source), time);
1025
1026         /* Get the models */
1027         source_widget = gtk_drag_get_source_widget (context);
1028         gtk_tree_get_row_drag_data (selection_data,
1029                                     &source_model,
1030                                     &source_row);
1031
1032         model_sort = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
1033         /* Select the destination model */
1034         if (source_widget == widget) {
1035                 dest_model = source_model;
1036         } else {
1037                 dest_model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model_sort));
1038         }
1039
1040         /* Get the path to the destination row. Can not call
1041            gtk_tree_view_get_drag_dest_row() because the source row
1042            is not selected anymore */
1043         gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget), x, y,
1044                                            &dest_row, &pos);
1045
1046         /* Only allow drops IN other rows */
1047         if (!dest_row || pos == GTK_TREE_VIEW_DROP_BEFORE || pos == GTK_TREE_VIEW_DROP_AFTER)
1048                 gtk_drag_finish (context, success, (success && delete_source), time);
1049
1050         /* Create the helper */
1051         helper = g_slice_new0 (DndHelper);
1052         helper->delete_source = delete_source;
1053         helper->source_widget = source_widget;
1054         helper->dest_widget = widget;
1055         helper->source_row = source_row;
1056         helper->context = context;
1057         helper->time = time;
1058
1059         /* Get path from the unsorted model */
1060         child_dest_row = 
1061                 gtk_tree_model_sort_convert_path_to_child_path (GTK_TREE_MODEL_SORT (model_sort),
1062                                                                 dest_row);
1063         gtk_tree_path_free (dest_row);
1064
1065         /* Drags from the header view */
1066         if (source_widget != widget) {
1067
1068                 drag_and_drop_from_header_view (source_model,
1069                                                 dest_model,
1070                                                 child_dest_row,
1071                                                 helper);
1072         } else {
1073
1074
1075                 drag_and_drop_from_folder_view (source_model,
1076                                                 dest_model,
1077                                                 child_dest_row,
1078                                                 selection_data, 
1079                                                 helper);
1080         }
1081         gtk_tree_path_free (child_dest_row);
1082 }
1083
1084 /*
1085  * We define a "drag-drop" signal handler because we do not want to
1086  * use the default one, because the default one always calls
1087  * gtk_drag_finish and we prefer to do it in the "drag-data-received"
1088  * signal handler, because there we have all the information available
1089  * to know if the dnd was a success or not.
1090  */
1091 static gboolean
1092 drag_drop_cb (GtkWidget      *widget,
1093               GdkDragContext *context,
1094               gint            x,
1095               gint            y,
1096               guint           time,
1097               gpointer        user_data) 
1098 {
1099         gpointer target;
1100
1101         if (!context->targets)
1102                 return FALSE;
1103
1104         /* Check if we're dragging a folder row */
1105         target = gtk_drag_dest_find_target (widget, context, NULL);
1106
1107         /* Request the data from the source. */
1108         gtk_drag_get_data(widget, context, target, time);
1109
1110     return TRUE;
1111 }
1112
1113 /*
1114  * This function deletes the data that has been dragged from its
1115  * source widget. Since is a function received by the source of the
1116  * drag, this function only deletes rows of the folder view
1117  * widget. The header view widget will need to define its own one.
1118  */
1119 static void 
1120 drag_data_delete_cb (GtkWidget      *widget,
1121                      GdkDragContext *context,
1122                      gpointer        user_data)
1123 {
1124         GtkTreePath *source_row;
1125         GtkTreeModel *model_sort, *model;
1126
1127         model_sort = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
1128         model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model_sort));
1129         source_row = g_object_steal_data (G_OBJECT (widget), ROW_REF_DATA_NAME);
1130
1131         /* Delete the source row */
1132         gtk_tree_drag_source_drag_data_delete (GTK_TREE_DRAG_SOURCE (model),
1133                                                source_row);
1134
1135         gtk_tree_path_free (source_row);
1136 }
1137
1138 /*
1139  * This function expands a node of a tree view if it's not expanded
1140  * yet. Not sure why it needs the threads stuff, but gtk+`example code
1141  * does that, so that's why they're here.
1142  */
1143 static gint
1144 expand_row_timeout (gpointer data)
1145 {
1146         GtkTreeView *tree_view = data;
1147         GtkTreePath *dest_path = NULL;
1148         GtkTreeViewDropPosition pos;
1149         gboolean result = FALSE;
1150         
1151         GDK_THREADS_ENTER ();
1152         
1153         gtk_tree_view_get_drag_dest_row (tree_view,
1154                                          &dest_path,
1155                                          &pos);
1156         
1157         if (dest_path &&
1158             (pos == GTK_TREE_VIEW_DROP_INTO_OR_AFTER ||
1159              pos == GTK_TREE_VIEW_DROP_INTO_OR_BEFORE)) {
1160                 gtk_tree_view_expand_row (tree_view, dest_path, FALSE);
1161                 gtk_tree_path_free (dest_path);
1162         }
1163         else {
1164                 if (dest_path)
1165                         gtk_tree_path_free (dest_path);
1166                 
1167                 result = TRUE;
1168         }
1169         
1170         GDK_THREADS_LEAVE ();
1171
1172         return result;
1173 }
1174
1175 /*
1176  * This function is called whenever the pointer is moved over a widget
1177  * while dragging some data. It installs a timeout that will expand a
1178  * node of the treeview if not expanded yet. This function also calls
1179  * gdk_drag_status in order to set the suggested action that will be
1180  * used by the "drag-data-received" signal handler to know if we
1181  * should do a move or just a copy of the data.
1182  */
1183 static gboolean
1184 on_drag_motion (GtkWidget      *widget,
1185                 GdkDragContext *context,
1186                 gint            x,
1187                 gint            y,
1188                 guint           time,
1189                 gpointer        user_data)  
1190 {
1191         GtkTreeViewDropPosition pos;
1192         GtkTreePath *dest_row;
1193         ModestFolderViewPrivate *priv;
1194         GdkDragAction suggested_action;
1195
1196         priv = MODEST_FOLDER_VIEW_GET_PRIVATE (widget);
1197
1198         if (priv->timer_expander != 0) {
1199                 g_source_remove (priv->timer_expander);
1200                 priv->timer_expander = 0;
1201         }
1202
1203         gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget),
1204                                            x, y,
1205                                            &dest_row,
1206                                            &pos);
1207
1208         if (!dest_row)
1209                 return FALSE;
1210
1211         /* Expand the selected row after 1/2 second */
1212         if (!gtk_tree_view_row_expanded (GTK_TREE_VIEW (widget), dest_row)) {
1213                 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (widget), dest_row, pos);
1214                 priv->timer_expander = g_timeout_add (500, expand_row_timeout, widget);
1215         }
1216         gtk_tree_path_free (dest_row);
1217
1218         /* Select the desired action. By default we pick MOVE */
1219         suggested_action = GDK_ACTION_MOVE;
1220
1221         if (context->actions == GDK_ACTION_COPY)
1222             gdk_drag_status(context, GDK_ACTION_COPY, time);
1223         else if (context->actions == GDK_ACTION_MOVE)
1224             gdk_drag_status(context, GDK_ACTION_MOVE, time);
1225         else if (context->actions & suggested_action)
1226             gdk_drag_status(context, suggested_action, time);
1227         else
1228             gdk_drag_status(context, GDK_ACTION_DEFAULT, time);
1229
1230         return TRUE;
1231 }
1232
1233
1234 /* Folder view drag types */
1235 const GtkTargetEntry folder_view_drag_types[] =
1236 {
1237         { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, FOLDER_ROW },
1238         { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_APP, HEADER_ROW }
1239 };
1240
1241 /*
1242  * This function sets the treeview as a source and a target for dnd
1243  * events. It also connects all the requirede signals.
1244  */
1245 static void
1246 setup_drag_and_drop (GtkTreeView *self)
1247 {
1248         /* Set up the folder view as a dnd destination. Set only the
1249            highlight flag, otherwise gtk will have a different
1250            behaviour */
1251         gtk_drag_dest_set (GTK_WIDGET (self),
1252                            GTK_DEST_DEFAULT_HIGHLIGHT,
1253                            folder_view_drag_types,
1254                            G_N_ELEMENTS (folder_view_drag_types),
1255                            GDK_ACTION_MOVE | GDK_ACTION_COPY);
1256
1257         gtk_signal_connect(GTK_OBJECT (self),
1258                            "drag_data_received",
1259                            GTK_SIGNAL_FUNC(on_drag_data_received),
1260                            NULL);
1261
1262
1263         /* Set up the treeview as a dnd source */
1264         gtk_drag_source_set (GTK_WIDGET (self),
1265                              GDK_BUTTON1_MASK,
1266                              folder_view_drag_types,
1267                              G_N_ELEMENTS (folder_view_drag_types),
1268                              GDK_ACTION_MOVE | GDK_ACTION_COPY);
1269
1270         gtk_signal_connect(GTK_OBJECT (self),
1271                            "drag_data_delete",
1272                            GTK_SIGNAL_FUNC(drag_data_delete_cb),
1273                            NULL);
1274
1275         gtk_signal_connect(GTK_OBJECT (self),
1276                            "drag_motion",
1277                            GTK_SIGNAL_FUNC(on_drag_motion),
1278                            NULL);
1279
1280
1281         gtk_signal_connect(GTK_OBJECT (self),
1282                            "drag_data_get",
1283                            GTK_SIGNAL_FUNC(on_drag_data_get),
1284                            NULL);
1285
1286         gtk_signal_connect(GTK_OBJECT (self),
1287                            "drag_drop",
1288                            GTK_SIGNAL_FUNC(drag_drop_cb),
1289                            NULL);
1290 }