* all:
[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 static gint
735 cmp_rows (GtkTreeModel *tree_model, GtkTreeIter *iter1, GtkTreeIter *iter2,
736           gpointer user_data)
737 {
738         gint cmp;
739         gchar         *name1, *name2;
740         TnyFolderType type;
741         TnyFolder     *folder1, *folder2;
742         
743         gtk_tree_model_get (tree_model, iter1,
744                             TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &name1,
745                             TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
746                             TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder1,
747                             -1);
748         gtk_tree_model_get (tree_model, iter2,
749                             TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &name2,
750                             TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder2,
751                             -1);
752
753         /* local_folders should be the last one */
754         if (type == TNY_FOLDER_TYPE_ROOT) {
755                 /* the account name is also the name of the root folder
756                  * in case of local folders */
757                 if (name1 && strcmp (name1, MODEST_LOCAL_FOLDERS_ACCOUNT_NAME) == 0)
758                         cmp = +1;
759                 else if (name2 && strcmp (name2, MODEST_LOCAL_FOLDERS_ACCOUNT_NAME) == 0)
760                         cmp = -1;
761                 else 
762                         cmp = modest_text_utils_utf8_strcmp (name1, name2, TRUE);
763         } else 
764                 cmp = modest_text_utils_utf8_strcmp (name1, name2, TRUE);
765
766         
767         if (folder1)
768                 g_object_unref(G_OBJECT(folder1));
769         if (folder2)
770                 g_object_unref(G_OBJECT(folder2));
771         
772         g_free (name1);
773         g_free (name2);
774
775         return cmp;     
776 }
777
778 /*****************************************************************************/
779 /*                        DRAG and DROP stuff                                */
780 /*****************************************************************************/
781
782 /*
783  * This function fills the #GtkSelectionData with the row and the
784  * model that has been dragged. It's called when this widget is a
785  * source for dnd after the event drop happened
786  */
787 static void
788 on_drag_data_get (GtkWidget *widget, 
789                   GdkDragContext *context, 
790                   GtkSelectionData *selection_data, 
791                   guint info, 
792                   guint time, 
793                   gpointer data)
794 {
795         GtkTreeSelection *selection;
796         GtkTreeModel *model_sort, *model;
797         GtkTreeIter iter;
798         GtkTreePath *source_row_sort;
799         GtkTreePath *source_row;
800
801         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (widget));
802         gtk_tree_selection_get_selected (selection, &model_sort, &iter);
803         source_row_sort = gtk_tree_model_get_path (model_sort, &iter);
804
805         /* Get the unsorted path and model */
806         model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model_sort));
807         source_row = gtk_tree_model_sort_convert_path_to_child_path (GTK_TREE_MODEL_SORT (model_sort),
808                                                                      source_row_sort);
809
810         gtk_tree_set_row_drag_data (selection_data,
811                                     model,
812                                     source_row);
813
814         gtk_tree_path_free (source_row_sort);
815         gtk_tree_path_free (source_row);
816 }
817
818 typedef struct _DndHelper
819 {
820         gboolean delete_source;
821         GtkWidget *source_widget;
822         GtkWidget *dest_widget;
823         GtkTreePath *source_row;
824         GdkDragContext *context;
825         guint time;
826 } DndHelper;
827
828
829 /*
830  * This function saves the source row in the source widget, will be
831  * used by the drag-data-delete handler to remove the source row
832  */
833 static void
834 save_and_clean (DndHelper *helper, 
835                 gboolean success)
836 {
837         /* Save row data */
838         if (success && helper->delete_source)
839                 g_object_set_data (G_OBJECT (helper->source_widget),
840                                    ROW_REF_DATA_NAME,
841                                    gtk_tree_path_copy (helper->source_row));
842
843         /* Clean dest row */
844         gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (helper->dest_widget),
845                                          NULL,
846                                          GTK_TREE_VIEW_DROP_BEFORE);
847
848 }
849
850 /*
851  * This function is the callback of the
852  * modest_mail_operation_xfer_msg() call. We check here if the message
853  * was correctly asynchronously transfered
854  */
855 static void
856 on_progress_changed (ModestMailOperation *mail_op, gpointer user_data)
857 {
858         ModestMailOperationQueue *queue;
859         gboolean success = FALSE;
860         DndHelper *helper;
861
862         helper = (DndHelper *) user_data;
863
864         if (modest_mail_operation_get_status (mail_op) == 
865             MODEST_MAIL_OPERATION_STATUS_SUCCESS) {
866                 success = TRUE;
867         } else {
868                 const GError *error;
869                 error = modest_mail_operation_get_error (mail_op);
870                 g_warning ("Error transferring messages: %s\n", error->message);
871         }
872
873         /* Remove the mail operation */ 
874         queue = modest_runtime_get_mail_operation_queue ();
875         modest_mail_operation_queue_remove (queue, mail_op);
876         g_object_unref (G_OBJECT (mail_op));
877
878         /* Save and clean */
879         save_and_clean (helper, success);       
880         
881         /* Notify the drag source */
882         gtk_drag_finish (helper->context, success, (success && helper->delete_source), helper->time);
883
884         /* Free the helper */
885         g_slice_free (DndHelper, helper);
886 }
887
888 /*
889  * This function is used by drag_data_received_cb to manage drag and
890  * drop of a header, i.e, and drag from the header view to the folder
891  * view.
892  */
893 static void
894 drag_and_drop_from_header_view (GtkTreeModel *source_model,
895                                 GtkTreeModel *dest_model,
896                                 GtkTreePath  *dest_row,
897                                 DndHelper    *helper)
898 {
899         TnyHeader *header;
900         TnyFolder *folder;
901         ModestMailOperationQueue *queue;
902         ModestMailOperation *mail_op;
903         gboolean started;
904         GtkTreeIter source_iter, dest_iter;
905
906         /* Get header */
907         gtk_tree_model_get_iter (source_model, &source_iter, helper->source_row);
908         gtk_tree_model_get (source_model, &source_iter, 
909                             TNY_GTK_HEADER_LIST_MODEL_INSTANCE_COLUMN, 
910                             &header, -1);
911
912         /* Get Folder */
913         gtk_tree_model_get_iter (dest_model, &dest_iter, dest_row);
914         gtk_tree_model_get (dest_model, &dest_iter, 
915                             TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, 
916                             &folder, -1);
917
918         /* Transfer message */
919         queue = modest_runtime_get_mail_operation_queue ();
920         mail_op = modest_mail_operation_new ();
921         started = modest_mail_operation_xfer_msg (mail_op, header,
922                                                   folder, helper->delete_source);
923         if (started) {
924                 g_signal_connect (G_OBJECT (mail_op), "progress_changed",
925                                   G_CALLBACK (on_progress_changed), helper);
926                 modest_mail_operation_queue_add (queue, mail_op);
927         } else {
928                 const GError *error;
929                 error = modest_mail_operation_get_error (mail_op);
930                 if (error)
931                         g_warning ("Error trying to transfer messages: %s\n",
932                                    error->message);
933
934                 g_slice_free (DndHelper, helper);
935         }
936
937         /* Frees */
938         g_object_unref (G_OBJECT (mail_op));
939         g_object_unref (G_OBJECT (header));
940         g_object_unref (G_OBJECT (folder));
941 }
942
943 /*
944  * This function is used by drag_data_received_cb to manage drag and
945  * drop of a folder, i.e, and drag from the folder view to the same
946  * folder view.
947  */
948 static void
949 drag_and_drop_from_folder_view (GtkTreeModel     *source_model,
950                                 GtkTreeModel     *dest_model,
951                                 GtkTreePath      *dest_row,
952                                 GtkSelectionData *selection_data,
953                                 DndHelper        *helper)
954 {
955         ModestMailOperation *mail_op;
956         const GError *error;
957         GtkTreeRowReference *source_row_reference;
958         GtkTreeIter parent_iter, iter;
959         TnyFolder *folder, *new_folder;
960         TnyFolderStore *parent_folder;
961         gboolean success = FALSE;
962
963         /* Check if the drag is possible */
964         if (!gtk_tree_path_compare (helper->source_row, dest_row))
965                 goto out;
966
967         if (!gtk_tree_drag_dest_row_drop_possible (GTK_TREE_DRAG_DEST (dest_model),
968                                                    dest_row,
969                                                    selection_data))
970                 goto out;
971
972         /* Get data */
973         gtk_tree_model_get_iter (source_model, &parent_iter, dest_row);
974         gtk_tree_model_get_iter (source_model, &iter, helper->source_row);
975         gtk_tree_model_get (source_model, &parent_iter, 
976                             TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, 
977                             &parent_folder, -1);
978         gtk_tree_model_get (source_model, &iter,
979                             TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN,
980                             &folder, -1);
981
982         /* Do the mail operation */
983         mail_op = modest_mail_operation_new ();
984         new_folder = modest_mail_operation_xfer_folder (mail_op, folder, parent_folder, 
985                                                         helper->delete_source);
986
987         g_object_unref (G_OBJECT (parent_folder));
988         g_object_unref (G_OBJECT (folder));
989
990         error = modest_mail_operation_get_error (mail_op);
991         if (error) {
992                 g_warning ("Error transferring folder: %s\n", error->message);
993                 g_object_unref (G_OBJECT (mail_op));
994                 goto out;
995         }
996         g_object_unref (G_OBJECT (mail_op));
997
998         /* Get a row reference to the source path because the path
999            could change after the insertion. The gtk_drag_finish() is
1000            not able to delete the source because that, so we have to
1001            do it manually */
1002         source_row_reference = gtk_tree_row_reference_new (source_model, helper->source_row);
1003         gtk_tree_path_free (helper->source_row);
1004
1005         /* Insert the dragged row as a child of the dest row */
1006         gtk_tree_path_down (dest_row);
1007         if (gtk_tree_drag_dest_drag_data_received (GTK_TREE_DRAG_DEST (dest_model),
1008                                                    dest_row,
1009                                                    selection_data)) {
1010
1011                 GtkTreeIter iter;
1012
1013                 /* Set the newly created folder as the instance in the row */
1014                 gtk_tree_model_get_iter (dest_model, &iter, dest_row);
1015                 gtk_tree_store_set (GTK_TREE_STORE (dest_model), &iter,
1016                                     TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, 
1017                                     new_folder, -1);
1018                 g_object_unref (G_OBJECT (new_folder));         
1019
1020                 helper->source_row = gtk_tree_row_reference_get_path (source_row_reference);
1021
1022                 success = TRUE;
1023         }
1024         gtk_tree_row_reference_free (source_row_reference);
1025
1026         /* Save and clean */
1027         save_and_clean (helper, success);       
1028
1029  out:
1030         gtk_drag_finish (helper->context, success, (success && helper->delete_source), helper->time);
1031 }
1032
1033 /*
1034  * This function receives the data set by the "drag-data-get" signal
1035  * handler. This information comes within the #GtkSelectionData. This
1036  * function will manage both the drags of folders of the treeview and
1037  * drags of headers of the header view widget.
1038  */
1039 static void 
1040 on_drag_data_received (GtkWidget *widget, 
1041                        GdkDragContext *context, 
1042                        gint x, 
1043                        gint y, 
1044                        GtkSelectionData *selection_data, 
1045                        guint target_type, 
1046                        guint time, 
1047                        gpointer data)
1048 {
1049         GtkWidget *source_widget;
1050         GtkTreeModel *model_sort=NULL, *dest_model, *source_model; /* FIXME -- removing = NULL gives warn */
1051         GtkTreePath *source_row, *dest_row, *child_dest_row;
1052         GtkTreeViewDropPosition pos;
1053         gboolean success = FALSE, delete_source = FALSE;
1054         DndHelper *helper;
1055
1056         /* Do not allow further process */
1057         g_signal_stop_emission_by_name (widget, "drag-data-received");
1058
1059         /* Get the action */
1060         if (context->action == GDK_ACTION_MOVE)
1061                 delete_source = TRUE;
1062
1063         /* Check if the get_data failed */
1064         if (selection_data == NULL || selection_data->length < 0)
1065                 gtk_drag_finish (context, success, (success && delete_source), time);
1066
1067         /* Get the models */
1068         source_widget = gtk_drag_get_source_widget (context);
1069         gtk_tree_get_row_drag_data (selection_data,
1070                                     &source_model,
1071                                     &source_row);
1072
1073         /* Select the destination model */
1074         if (source_widget == widget) {
1075                 dest_model = source_model;
1076         } else {
1077                 model_sort = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
1078                 dest_model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model_sort));
1079         }
1080
1081         /* Get the path to the destination row. Can not call
1082            gtk_tree_view_get_drag_dest_row() because the source row
1083            is not selected anymore */
1084         gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget), x, y,
1085                                            &dest_row, &pos);
1086
1087         /* Only allow drops IN other rows */
1088         if (!dest_row || pos == GTK_TREE_VIEW_DROP_BEFORE || pos == GTK_TREE_VIEW_DROP_AFTER)
1089                 gtk_drag_finish (context, success, (success && delete_source), time);
1090
1091         /* Create the helper */
1092         helper = g_slice_new0 (DndHelper);
1093         helper->delete_source = delete_source;
1094         helper->source_widget = source_widget;
1095         helper->dest_widget = widget;
1096         helper->source_row = source_row;
1097         helper->context = context;
1098         helper->time = time;
1099
1100         /* Get path from the unsorted model */
1101         child_dest_row = 
1102                 gtk_tree_model_sort_convert_path_to_child_path (GTK_TREE_MODEL_SORT (model_sort),
1103                                                                 dest_row);
1104         gtk_tree_path_free (dest_row);
1105
1106         /* Drags from the header view */
1107         if (source_widget != widget) {
1108
1109                 drag_and_drop_from_header_view (source_model,
1110                                                 dest_model,
1111                                                 child_dest_row,
1112                                                 helper);
1113         } else {
1114
1115
1116                 drag_and_drop_from_folder_view (source_model,
1117                                                 dest_model,
1118                                                 child_dest_row,
1119                                                 selection_data, 
1120                                                 helper);
1121         }
1122         gtk_tree_path_free (child_dest_row);
1123 }
1124
1125 /*
1126  * We define a "drag-drop" signal handler because we do not want to
1127  * use the default one, because the default one always calls
1128  * gtk_drag_finish and we prefer to do it in the "drag-data-received"
1129  * signal handler, because there we have all the information available
1130  * to know if the dnd was a success or not.
1131  */
1132 static gboolean
1133 drag_drop_cb (GtkWidget      *widget,
1134               GdkDragContext *context,
1135               gint            x,
1136               gint            y,
1137               guint           time,
1138               gpointer        user_data) 
1139 {
1140         gpointer target;
1141
1142         if (!context->targets)
1143                 return FALSE;
1144
1145         /* Check if we're dragging a folder row */
1146         target = gtk_drag_dest_find_target (widget, context, NULL);
1147
1148         /* Request the data from the source. */
1149         gtk_drag_get_data(widget, context, target, time);
1150
1151     return TRUE;
1152 }
1153
1154 /*
1155  * This function deletes the data that has been dragged from its
1156  * source widget. Since is a function received by the source of the
1157  * drag, this function only deletes rows of the folder view
1158  * widget. The header view widget will need to define its own one.
1159  */
1160 static void 
1161 drag_data_delete_cb (GtkWidget      *widget,
1162                      GdkDragContext *context,
1163                      gpointer        user_data)
1164 {
1165         GtkTreePath *source_row;
1166         GtkTreeModel *model_sort, *model;
1167
1168         model_sort = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
1169         model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model_sort));
1170         source_row = g_object_steal_data (G_OBJECT (widget), ROW_REF_DATA_NAME);
1171
1172         /* Delete the source row */
1173         gtk_tree_drag_source_drag_data_delete (GTK_TREE_DRAG_SOURCE (model),
1174                                                source_row);
1175
1176         gtk_tree_path_free (source_row);
1177 }
1178
1179 /*
1180  * This function expands a node of a tree view if it's not expanded
1181  * yet. Not sure why it needs the threads stuff, but gtk+`example code
1182  * does that, so that's why they're here.
1183  */
1184 static gint
1185 expand_row_timeout (gpointer data)
1186 {
1187         GtkTreeView *tree_view = data;
1188         GtkTreePath *dest_path = NULL;
1189         GtkTreeViewDropPosition pos;
1190         gboolean result = FALSE;
1191         
1192         GDK_THREADS_ENTER ();
1193         
1194         gtk_tree_view_get_drag_dest_row (tree_view,
1195                                          &dest_path,
1196                                          &pos);
1197         
1198         if (dest_path &&
1199             (pos == GTK_TREE_VIEW_DROP_INTO_OR_AFTER ||
1200              pos == GTK_TREE_VIEW_DROP_INTO_OR_BEFORE)) {
1201                 gtk_tree_view_expand_row (tree_view, dest_path, FALSE);
1202                 gtk_tree_path_free (dest_path);
1203         }
1204         else {
1205                 if (dest_path)
1206                         gtk_tree_path_free (dest_path);
1207                 
1208                 result = TRUE;
1209         }
1210         
1211         GDK_THREADS_LEAVE ();
1212
1213         return result;
1214 }
1215
1216 /*
1217  * This function is called whenever the pointer is moved over a widget
1218  * while dragging some data. It installs a timeout that will expand a
1219  * node of the treeview if not expanded yet. This function also calls
1220  * gdk_drag_status in order to set the suggested action that will be
1221  * used by the "drag-data-received" signal handler to know if we
1222  * should do a move or just a copy of the data.
1223  */
1224 static gboolean
1225 on_drag_motion (GtkWidget      *widget,
1226                 GdkDragContext *context,
1227                 gint            x,
1228                 gint            y,
1229                 guint           time,
1230                 gpointer        user_data)  
1231 {
1232         GtkTreeViewDropPosition pos;
1233         GtkTreePath *dest_row;
1234         ModestFolderViewPrivate *priv;
1235         GdkDragAction suggested_action;
1236
1237         priv = MODEST_FOLDER_VIEW_GET_PRIVATE (widget);
1238
1239         if (priv->timer_expander != 0) {
1240                 g_source_remove (priv->timer_expander);
1241                 priv->timer_expander = 0;
1242         }
1243
1244         gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget),
1245                                            x, y,
1246                                            &dest_row,
1247                                            &pos);
1248
1249         if (!dest_row)
1250                 return FALSE;
1251
1252         /* Expand the selected row after 1/2 second */
1253         if (!gtk_tree_view_row_expanded (GTK_TREE_VIEW (widget), dest_row)) {
1254                 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (widget), dest_row, pos);
1255                 priv->timer_expander = g_timeout_add (500, expand_row_timeout, widget);
1256         }
1257         gtk_tree_path_free (dest_row);
1258
1259         /* Select the desired action. By default we pick MOVE */
1260         suggested_action = GDK_ACTION_MOVE;
1261
1262         if (context->actions == GDK_ACTION_COPY)
1263             gdk_drag_status(context, GDK_ACTION_COPY, time);
1264         else if (context->actions == GDK_ACTION_MOVE)
1265             gdk_drag_status(context, GDK_ACTION_MOVE, time);
1266         else if (context->actions & suggested_action)
1267             gdk_drag_status(context, suggested_action, time);
1268         else
1269             gdk_drag_status(context, GDK_ACTION_DEFAULT, time);
1270
1271         return TRUE;
1272 }
1273
1274
1275 /* Folder view drag types */
1276 const GtkTargetEntry folder_view_drag_types[] =
1277 {
1278         { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, FOLDER_ROW },
1279         { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_APP, HEADER_ROW }
1280 };
1281
1282 /*
1283  * This function sets the treeview as a source and a target for dnd
1284  * events. It also connects all the requirede signals.
1285  */
1286 static void
1287 setup_drag_and_drop (GtkTreeView *self)
1288 {
1289         /* Set up the folder view as a dnd destination. Set only the
1290            highlight flag, otherwise gtk will have a different
1291            behaviour */
1292         gtk_drag_dest_set (GTK_WIDGET (self),
1293                            GTK_DEST_DEFAULT_HIGHLIGHT,
1294                            folder_view_drag_types,
1295                            G_N_ELEMENTS (folder_view_drag_types),
1296                            GDK_ACTION_MOVE | GDK_ACTION_COPY);
1297
1298         gtk_signal_connect(GTK_OBJECT (self),
1299                            "drag_data_received",
1300                            GTK_SIGNAL_FUNC(on_drag_data_received),
1301                            NULL);
1302
1303
1304         /* Set up the treeview as a dnd source */
1305         gtk_drag_source_set (GTK_WIDGET (self),
1306                              GDK_BUTTON1_MASK,
1307                              folder_view_drag_types,
1308                              G_N_ELEMENTS (folder_view_drag_types),
1309                              GDK_ACTION_MOVE | GDK_ACTION_COPY);
1310
1311         gtk_signal_connect(GTK_OBJECT (self),
1312                            "drag_data_delete",
1313                            GTK_SIGNAL_FUNC(drag_data_delete_cb),
1314                            NULL);
1315
1316         gtk_signal_connect(GTK_OBJECT (self),
1317                            "drag_motion",
1318                            GTK_SIGNAL_FUNC(on_drag_motion),
1319                            NULL);
1320
1321
1322         gtk_signal_connect(GTK_OBJECT (self),
1323                            "drag_data_get",
1324                            GTK_SIGNAL_FUNC(on_drag_data_get),
1325                            NULL);
1326
1327         gtk_signal_connect(GTK_OBJECT (self),
1328                            "drag_drop",
1329                            GTK_SIGNAL_FUNC(drag_drop_cb),
1330                            NULL);
1331 }