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