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