* all:
[modest] / src / widgets / modest-folder-view.c
index 1329957..2c2aa73 100644 (file)
 #include <glib/gi18n.h>
 #include <string.h>
 
+#include <tny-account-store-view.h>
 #include <tny-gtk-account-list-model.h>
 #include <tny-gtk-folder-store-tree-model.h>
 #include <tny-gtk-header-list-model.h>
 #include <tny-folder.h>
+#include <tny-folder-monitor.h>
 #include <tny-account-store.h>
 #include <tny-account.h>
 #include <tny-folder.h>
@@ -52,6 +54,12 @@ static void modest_folder_view_class_init  (ModestFolderViewClass *klass);
 static void modest_folder_view_init        (ModestFolderView *obj);
 static void modest_folder_view_finalize    (GObject *obj);
 
+static void         tny_account_store_view_init (gpointer g, 
+                                                gpointer iface_data);
+
+static void         modest_folder_view_set_account_store (TnyAccountStoreView *self, 
+                                                         TnyAccountStore     *account_store);
+
 static gboolean     update_model           (ModestFolderView *self,
                                            ModestTnyAccountStore *account_store);
 
@@ -59,20 +67,24 @@ static gboolean     update_model_empty     (ModestFolderView *self);
 
 static void         on_selection_changed   (GtkTreeSelection *sel, gpointer data);
 
+static void         on_account_update      (TnyAccountStore *account_store, 
+                                           const gchar *account,
+                                           gpointer user_data);
+
 static gint         cmp_rows               (GtkTreeModel *tree_model, 
                                            GtkTreeIter *iter1, 
                                            GtkTreeIter *iter2,
                                            gpointer user_data);
 
 /* DnD functions */
-static void         drag_data_get_cb       (GtkWidget *widget, 
+static void         on_drag_data_get       (GtkWidget *widget, 
                                            GdkDragContext *context, 
                                            GtkSelectionData *selection_data, 
                                            guint info, 
                                            guint time, 
                                            gpointer data);
 
-static void         drag_data_received_cb  (GtkWidget *widget, 
+static void         on_drag_data_received  (GtkWidget *widget, 
                                            GdkDragContext *context, 
                                            gint x, 
                                            gint y, 
@@ -81,7 +93,7 @@ static void         drag_data_received_cb  (GtkWidget *widget,
                                            guint time, 
                                            gpointer data);
 
-static gboolean     drag_motion_cb         (GtkWidget      *widget,
+static gboolean     on_drag_motion         (GtkWidget      *widget,
                                            GdkDragContext *context,
                                            gint            x,
                                            gint            y,
@@ -92,12 +104,16 @@ static gint         expand_row_timeout     (gpointer data);
 
 static void         setup_drag_and_drop    (GtkTreeView *self);
 
+enum {
+       TARGET_TREE_ROW,
+};
 
 static const GtkTargetEntry drag_types[] =
 {
-       { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, 2 }
+       { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, TARGET_TREE_ROW },
 };
 
+#define ROW_REF_DATA_NAME "row-ref"
 
 enum {
        FOLDER_SELECTION_CHANGED_SIGNAL,
@@ -113,13 +129,16 @@ struct _ModestFolderViewPrivate {
 
        gulong               sig1, sig2;
        GMutex              *lock;
+       
        GtkTreeSelection    *cur_selection;
        TnyFolderStoreQuery *query;
        guint                timer_expander;
+
+       TnyFolderMonitor    *monitor;
 };
-#define MODEST_FOLDER_VIEW_GET_PRIVATE(o)                              \
-       (G_TYPE_INSTANCE_GET_PRIVATE((o),                               \
-                                    MODEST_TYPE_FOLDER_VIEW,           \
+#define MODEST_FOLDER_VIEW_GET_PRIVATE(o)                      \
+       (G_TYPE_INSTANCE_GET_PRIVATE((o),                       \
+                                    MODEST_TYPE_FOLDER_VIEW,   \
                                     ModestFolderViewPrivate))
 /* globals */
 static GObjectClass *parent_class = NULL;
@@ -143,10 +162,21 @@ modest_folder_view_get_type (void)
                        (GInstanceInitFunc) modest_folder_view_init,
                        NULL
                };
+
+               static const GInterfaceInfo tny_account_store_view_info = {
+                       (GInterfaceInitFunc) tny_account_store_view_init, /* interface_init */
+                       NULL,         /* interface_finalize */
+                       NULL          /* interface_data */
+               };
+
                                
                my_type = g_type_register_static (GTK_TYPE_TREE_VIEW,
                                                  "ModestFolderView",
-                                                 &my_info, 0);         
+                                                 &my_info, 0);
+
+               g_type_add_interface_static (my_type, 
+                                            TNY_TYPE_ACCOUNT_STORE_VIEW, 
+                                            &tny_account_store_view_info);
        }
        return my_type;
 }
@@ -246,6 +276,9 @@ text_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
                g_object_set (rendobj,"text", fname, "weight", 400, NULL);
                
        g_free (fname);
+       if (folder)
+               g_object_unref (G_OBJECT(folder));
+       
 }
 
 
@@ -355,8 +388,10 @@ modest_folder_view_init (ModestFolderView *obj)
        priv->cur_folder     = NULL;
        priv->cur_row        = NULL;
        priv->query          = NULL;
+       priv->monitor        = NULL;
+
        priv->lock           = g_mutex_new ();
-       
+
        column = gtk_tree_view_column_new ();   
        gtk_tree_view_append_column (GTK_TREE_VIEW(obj),column);
        
@@ -383,6 +418,16 @@ modest_folder_view_init (ModestFolderView *obj)
 }
 
 static void
+tny_account_store_view_init (gpointer g, gpointer iface_data)
+{
+       TnyAccountStoreViewIface *klass = (TnyAccountStoreViewIface *)g;
+
+       klass->set_account_store_func = modest_folder_view_set_account_store;
+
+       return;
+}
+
+static void
 modest_folder_view_finalize (GObject *obj)
 {
        ModestFolderViewPrivate *priv;
@@ -423,6 +468,43 @@ modest_folder_view_finalize (GObject *obj)
 
 
 static void
+modest_folder_view_set_account_store (TnyAccountStoreView *self, TnyAccountStore *account_store)
+{
+       ModestFolderViewPrivate *priv;
+       TnyDevice *device;
+
+       g_return_if_fail (MODEST_IS_FOLDER_VIEW (self));
+       g_return_if_fail (TNY_IS_ACCOUNT_STORE (account_store));
+
+       priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
+       device = tny_account_store_get_device (account_store);
+
+       if (G_UNLIKELY (priv->account_store)) {
+               TnyDevice *old_device; 
+               
+               old_device = tny_account_store_get_device (priv->account_store);
+
+               if (g_signal_handler_is_connected (G_OBJECT (old_device), priv->sig1))
+                       g_signal_handler_disconnect (G_OBJECT (old_device), 
+                                                    priv->sig1);
+
+               g_object_unref (G_OBJECT (priv->account_store));
+               g_object_unref (G_OBJECT (old_device));
+       }
+
+       priv->account_store = g_object_ref (G_OBJECT (account_store));
+
+       priv->sig1 = g_signal_connect (G_OBJECT(account_store), "account_update",
+                                      G_CALLBACK (on_account_update), self);
+       
+       if (!update_model (MODEST_FOLDER_VIEW (self),
+                          MODEST_TNY_ACCOUNT_STORE (priv->account_store)))
+               g_printerr ("modest: failed to update model\n");
+
+       g_object_unref (G_OBJECT (device));
+}
+
+static void
 on_account_update (TnyAccountStore *account_store, const gchar *account,
                   gpointer user_data)
 {
@@ -451,32 +533,20 @@ modest_folder_view_set_title (ModestFolderView *self, const gchar *title)
 }
 
 GtkWidget*
-modest_folder_view_new (ModestTnyAccountStore *account_store, 
-                       TnyFolderStoreQuery *query)
+modest_folder_view_new (TnyFolderStoreQuery *query)
 {
        GObject *self;
        ModestFolderViewPrivate *priv;
        GtkTreeSelection *sel;
        
-       g_return_val_if_fail (account_store, NULL);
-       
-       self = G_OBJECT(g_object_new(MODEST_TYPE_FOLDER_VIEW, NULL));
-       priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
-       
-       priv->account_store = g_object_ref (G_OBJECT (account_store));
-       if (query)
-               priv->query = g_object_ref (G_OBJECT (query));
-       
-       if (!update_model (MODEST_FOLDER_VIEW(self),
-                          MODEST_TNY_ACCOUNT_STORE(account_store)))
-               g_printerr ("modest: failed to update model\n");
-       
-       priv->sig1 = g_signal_connect (G_OBJECT(account_store), "account_update",
-                                      G_CALLBACK (on_account_update), self);   
+       self = G_OBJECT (g_object_new (MODEST_TYPE_FOLDER_VIEW, NULL));
+       priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
+
+       priv->query = g_object_ref (query);
        
        sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(self));
        priv->sig2 = g_signal_connect (sel, "changed",
-                                      G_CALLBACK(on_selection_changed), self);
+                                      G_CALLBACK (on_selection_changed), self);
        return GTK_WIDGET(self);
 }
 
@@ -489,6 +559,16 @@ update_model_empty (ModestFolderView *self)
        g_return_val_if_fail (self, FALSE);
        priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
 
+       g_mutex_lock (priv->lock);
+       {
+               if (priv->monitor) {
+                       tny_folder_monitor_stop (priv->monitor);
+                       g_object_unref(G_OBJECT(priv->monitor));
+                       priv->monitor = NULL;
+               }
+       }
+       g_mutex_unlock (priv->lock);
+       
        g_signal_emit (G_OBJECT(self), signals[FOLDER_SELECTION_CHANGED_SIGNAL], 0,
                       NULL, TRUE);
        return TRUE;
@@ -524,6 +604,9 @@ update_model (ModestFolderView *self, ModestTnyAccountStore *account_store)
        /* Notify that there is no folder selected */
        update_model_empty (self);
        
+       /* FIXME: the local accounts are not shown when the query
+          selects only the subscribed folders. */
+/*     model        = tny_gtk_folder_store_tree_model_new (TRUE, priv->query); */
        model        = tny_gtk_folder_store_tree_model_new (TRUE, NULL);
        account_list = TNY_LIST(model);
 
@@ -542,9 +625,12 @@ update_model (ModestFolderView *self, ModestTnyAccountStore *account_store)
                /* Set new model */
                gtk_tree_view_set_model (GTK_TREE_VIEW(self), sortable);
                expand_root_items (self); /* expand all account folders */
+               g_object_unref (account_list);
        }
        
-       g_object_unref (model);
+       //if (model)
+       //      g_object_unref (model);
+               
        return TRUE;
 }
 
@@ -570,7 +656,7 @@ on_selection_changed (GtkTreeSelection *sel, gpointer user_data)
        if (!gtk_tree_selection_get_selected (sel, &model_sort, &iter_sort)) {
                priv->cur_folder = NULL; /* FIXME: need this? */
                priv->cur_row = NULL; /* FIXME: need this? */
-               return; 
+               return; 
        }
 
        model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model_sort));
@@ -592,14 +678,15 @@ on_selection_changed (GtkTreeSelection *sel, gpointer user_data)
         * and one for the selection of the new on */
        g_signal_emit (G_OBJECT(tree_view), signals[FOLDER_SELECTION_CHANGED_SIGNAL], 0,
                       priv->cur_folder, FALSE);
-       g_signal_emit (G_OBJECT(tree_view), signals[FOLDER_SELECTION_CHANGED_SIGNAL], 0,
-                      folder, TRUE);
+
        if (priv->cur_folder) {
-               tny_folder_sync (priv->cur_folder, TRUE, NULL); /* FIXME */
+/*             tny_folder_sync (priv->cur_folder, TRUE, NULL); /\* FIXME *\/ */
                gtk_tree_row_reference_free (priv->cur_row);
        }
-
        priv->cur_folder = folder;
+       g_signal_emit (G_OBJECT(tree_view), signals[FOLDER_SELECTION_CHANGED_SIGNAL], 0,
+                      folder, TRUE);
        path = gtk_tree_model_get_path (model_sort, &iter_sort);
        priv->cur_row = gtk_tree_row_reference_new (model_sort, path);
        gtk_tree_path_free (path);
@@ -752,6 +839,12 @@ cmp_rows (GtkTreeModel *tree_model, GtkTreeIter *iter1, GtkTreeIter *iter2,
                        cmp = modest_text_utils_utf8_strcmp (name1, name2, TRUE);
        } else 
                cmp = modest_text_utils_utf8_strcmp (name1, name2, TRUE);
+
+       
+       if (folder1)
+               g_object_unref(G_OBJECT(folder1));
+       if (folder2)
+               g_object_unref(G_OBJECT(folder2));
        
        g_free (name1);
        g_free (name2);
@@ -762,8 +855,14 @@ cmp_rows (GtkTreeModel *tree_model, GtkTreeIter *iter1, GtkTreeIter *iter2,
 /*****************************************************************************/
 /*                        DRAG and DROP stuff                                */
 /*****************************************************************************/
+
+/*
+ * This function fills the #GtkSelectionData with the row and the
+ * model that has been dragged. It's called when this widget is a
+ * source for dnd after the event drop happened
+ */
 static void
-drag_data_get_cb (GtkWidget *widget, 
+on_drag_data_get (GtkWidget *widget, 
                  GdkDragContext *context, 
                  GtkSelectionData *selection_data, 
                  guint info, 
@@ -780,6 +879,7 @@ drag_data_get_cb (GtkWidget *widget,
        gtk_tree_selection_get_selected (selection, &model_sort, &iter);
        source_row_sort = gtk_tree_model_get_path (model_sort, &iter);
 
+       /* Get the unsorted path and model */
        model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model_sort));
        source_row = gtk_tree_model_sort_convert_path_to_child_path (GTK_TREE_MODEL_SORT (model_sort),
                                                                     source_row_sort);
@@ -792,37 +892,54 @@ drag_data_get_cb (GtkWidget *widget,
        gtk_tree_path_free (source_row);
 }
 
+/*
+ * This function receives the data set by the "drag-data-get" signal
+ * handler. This information comes within the #GtkSelectionData. This
+ * function will manage both the drags of folders of the treeview and
+ * drags of headers of the header view widget.
+ */
 static void 
-drag_data_received_cb (GtkWidget *widget, 
+on_drag_data_received (GtkWidget *widget, 
                       GdkDragContext *context, 
                       gint x, 
                       gint y, 
                       GtkSelectionData *selection_data, 
-                      guint info, 
+                      guint target_type, 
                       guint time, 
                       gpointer data)
 {
-       GtkTreeModel *model_sort, *model;
-       GtkTreeRowReference *source_row_reference;
+       GtkWidget *source_widget;
+       GtkTreeModel *model_sort, *dest_model, *source_model;
        GtkTreePath *source_row, *dest_row, *child_dest_row;
        GtkTreeViewDropPosition pos;
-       GtkTreeIter parent_iter, iter;
-       TnyFolder *folder;
-       TnyFolderStore *parent_folder;
-       gboolean done, success = FALSE;
+       gboolean success = FALSE, delete_source = FALSE;
 
        /* Do not allow further process */
        g_signal_stop_emission_by_name (widget, "drag-data-received");
 
-       if (selection_data->length == 0)
-               goto out;
+       /* Check if the get_data failed */
+       if (selection_data == NULL || selection_data->length < 0)
+               return;
+
+       /* Get the action */
+       if (context->action == GDK_ACTION_MOVE)
+               delete_source = TRUE;
 
-       /* Get the unsorted model, and the path to the source row */
+       /* Get the models */
+       source_widget = gtk_drag_get_source_widget (context);
        model_sort = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
+
        gtk_tree_get_row_drag_data (selection_data,
-                                   &model,
+                                   &source_model,
                                    &source_row);
 
+       /* Select the destination model */
+       if (source_widget == widget)
+               dest_model = source_model;
+       else
+               dest_model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model_sort));
+
+
        /* Get the path to the destination row. Can not call
           gtk_tree_view_get_drag_dest_row() because the source row
           it's not selected anymore */
@@ -835,7 +952,7 @@ drag_data_received_cb (GtkWidget *widget,
        if (!dest_row || 
            pos == GTK_TREE_VIEW_DROP_BEFORE ||
            pos == GTK_TREE_VIEW_DROP_AFTER)
-               goto out;
+               goto finish;
 
        /* Get the destination row in the usorted model */
        child_dest_row = 
@@ -843,100 +960,186 @@ drag_data_received_cb (GtkWidget *widget,
                                                                dest_row);
        gtk_tree_path_free (dest_row);
 
-       /* Check if the drag is possible */
-       if (!gtk_tree_drag_dest_row_drop_possible (GTK_TREE_DRAG_DEST (model),
-                                                  child_dest_row,
-                                                  selection_data)) {
-               gtk_tree_path_free (child_dest_row);
-               goto out;
+       /* Drags from the header view */
+       if ((target_type == TARGET_TREE_ROW) && (source_widget != widget)) {
+
+               /* TODO: do the mail operation */
+
+               success = TRUE;
+       } else {
+               GtkTreeRowReference *source_row_reference;
+               GtkTreeIter parent_iter, iter;
+               TnyFolder *folder;
+               TnyFolderStore *parent_folder;
+               gboolean done;
+
+               /* Check if the drag is possible */
+               if (!gtk_tree_path_compare (source_row, child_dest_row))
+                       goto out;
+
+               if (!gtk_tree_drag_dest_row_drop_possible (GTK_TREE_DRAG_DEST (dest_model),
+                                                          child_dest_row,
+                                                          selection_data))
+                       goto out;
+
+               /* Do the mail operation */
+               gtk_tree_model_get_iter (source_model, &parent_iter, child_dest_row);
+               gtk_tree_model_get_iter (source_model, &iter, source_row);
+               gtk_tree_model_get (source_model, &parent_iter, 
+                                   TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, 
+                                   &parent_folder, -1);
+               gtk_tree_model_get (source_model, &iter, 
+                                   TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, 
+                                   &folder, -1);
+               
+               g_signal_emit (G_OBJECT (widget), signals[FOLDER_MOVED_SIGNAL], 0,
+                              folder, parent_folder, &done);
+               if (!done)
+                       goto out;
+
+               /* Get a row reference to the source path because the path
+                  could change after the insertion. The gtk_drag_finish() is
+                  not able to delete the source because that, so we have to
+                  do it manually */
+               source_row_reference = gtk_tree_row_reference_new (source_model, source_row);
+               gtk_tree_path_free (source_row);
+
+               /* Insert the dragged row as a child of the dest row */
+               gtk_tree_path_down (child_dest_row);
+               if (gtk_tree_drag_dest_drag_data_received (GTK_TREE_DRAG_DEST (dest_model),
+                                                          child_dest_row,
+                                                          selection_data)) {
+
+                       source_row = gtk_tree_row_reference_get_path (source_row_reference);
+
+                       success = TRUE;
+               }
+               gtk_tree_row_reference_free (source_row_reference);
+               
+               if (folder)
+                       g_object_unref (G_OBJECT(folder));
+               if (parent_folder)
+                       g_object_unref (G_OBJECT(parent_folder));
+               
        }
+ out:
+       gtk_tree_path_free (child_dest_row);
 
-       /* Do the mail operation */
-       gtk_tree_model_get_iter (model, &parent_iter, child_dest_row);
-       gtk_tree_model_get_iter (model, &iter, source_row);
-       gtk_tree_model_get (model, &parent_iter, 
-                           TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &parent_folder, -1);
-       gtk_tree_model_get (model, &iter, 
-                           TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder, -1);
-
-       g_signal_emit (G_OBJECT (widget), signals[FOLDER_MOVED_SIGNAL], 0,
-                      folder, parent_folder, &done);
-       if (!done)
-               goto out;
-
-       /* Get a row reference to the source path because the path
-          could change after the insertion. The gtk_drag_finish() is
-          not able to delete the source because that, so we have to
-          do it manually */
-       source_row_reference = gtk_tree_row_reference_new (model, source_row);
-       gtk_tree_path_free (source_row);
+       /* Save the new path, will be used by the
+          drag-data-delete handler */
+       if (success)
+               g_object_set_data (G_OBJECT (source_widget),
+                                  ROW_REF_DATA_NAME,
+                                  gtk_tree_path_copy (source_row));
 
-       /* Insert the dragged row as a child of the dest row */
-       gtk_tree_path_down (child_dest_row);
-       if (gtk_tree_drag_dest_drag_data_received (GTK_TREE_DRAG_DEST (model),
-                                                  child_dest_row,
-                                                  selection_data)) {
+ finish:
+       gtk_drag_finish (context, success, (success && delete_source), time);
+}
 
-               /* Clean dest row */
-               gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (widget),
-                                                NULL,
-                                                GTK_TREE_VIEW_DROP_BEFORE);
+/*
+ * We define a "drag-drop" signal handler because we do not want to
+ * use the default one, because the default one always calls
+ * gtk_drag_finish and we prefer to do it in the "drag-data-received"
+ * signal handler, because there we have all the information available
+ * to know if the dnd was a success or not.
+ */
+static gboolean
+drag_drop_cb (GtkWidget      *widget,
+             GdkDragContext *context,
+             gint            x,
+             gint            y,
+             guint           time,
+             gpointer        user_data) 
+{
+       if (!context->targets)
+               return FALSE;
 
-               /* Get the new path of the source row */
-               source_row = gtk_tree_row_reference_get_path (source_row_reference);
+       /* Request the data from the source. */
+       gtk_drag_get_data(widget, 
+                         context, 
+                         GDK_POINTER_TO_ATOM (context->targets->data), 
+                         time);
 
-               /* Delete the source row */
-               gtk_tree_drag_source_drag_data_delete (GTK_TREE_DRAG_SOURCE (model),
-                                                      source_row);
+    return TRUE;
+}
 
-               success = TRUE;
+/*
+ * This function deletes the data that has been dragged from its
+ * source widget. Since is a function received by the source of the
+ * drag, this function only deletes rows of the folder view
+ * widget. The header view widget will need to define its own one.
+ */
+static void 
+drag_data_delete_cb (GtkWidget      *widget,
+                    GdkDragContext *context,
+                    gpointer        user_data)
+{
+       GtkTreePath *source_row;
+       GtkTreeModel *model_sort, *model;
 
-               gtk_tree_path_free (source_row);
-       }
+       /* Clean dest row */
+       gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (widget),
+                                        NULL,
+                                        GTK_TREE_VIEW_DROP_BEFORE);
 
-       gtk_tree_row_reference_free (source_row_reference);
-       gtk_tree_path_free (child_dest_row);
- out:
-       /* Never delete the source, we do it manually */
-       gtk_drag_finish (context, success, FALSE, time);
+       model_sort = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
+       model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model_sort));
+       source_row = g_object_steal_data (G_OBJECT (widget), ROW_REF_DATA_NAME);
+
+       /* Delete the source row */
+       gtk_tree_drag_source_drag_data_delete (GTK_TREE_DRAG_SOURCE (model),
+                                              source_row);
 
+       gtk_tree_path_free (source_row);
 }
 
+/*
+ * This function expands a node of a tree view if it's not expanded
+ * yet. Not sure why it needs the threads stuff, but gtk+`example code
+ * does that, so that's why they're here.
+ */
 static gint
 expand_row_timeout (gpointer data)
 {
-  GtkTreeView *tree_view = data;
-  GtkTreePath *dest_path = NULL;
-  GtkTreeViewDropPosition pos;
-  gboolean result = FALSE;
-
-  GDK_THREADS_ENTER ();
-
-  gtk_tree_view_get_drag_dest_row (tree_view,
-                                   &dest_path,
-                                   &pos);
-
-  if (dest_path &&
-      (pos == GTK_TREE_VIEW_DROP_INTO_OR_AFTER ||
-       pos == GTK_TREE_VIEW_DROP_INTO_OR_BEFORE)) {
-         gtk_tree_view_expand_row (tree_view, dest_path, FALSE);
-         gtk_tree_path_free (dest_path);
-  }
-  else {
-         if (dest_path)
-                 gtk_tree_path_free (dest_path);
-         
-         result = TRUE;
-  }
-  
-  GDK_THREADS_LEAVE ();
-
-  return result;
-}
+       GtkTreeView *tree_view = data;
+       GtkTreePath *dest_path = NULL;
+       GtkTreeViewDropPosition pos;
+       gboolean result = FALSE;
+       
+       GDK_THREADS_ENTER ();
+       
+       gtk_tree_view_get_drag_dest_row (tree_view,
+                                        &dest_path,
+                                        &pos);
+       
+       if (dest_path &&
+           (pos == GTK_TREE_VIEW_DROP_INTO_OR_AFTER ||
+            pos == GTK_TREE_VIEW_DROP_INTO_OR_BEFORE)) {
+               gtk_tree_view_expand_row (tree_view, dest_path, FALSE);
+               gtk_tree_path_free (dest_path);
+       }
+       else {
+               if (dest_path)
+                       gtk_tree_path_free (dest_path);
+               
+               result = TRUE;
+       }
+       
+       GDK_THREADS_LEAVE ();
 
+       return result;
+}
 
+/*
+ * This function is called whenever the pointer is moved over a widget
+ * while dragging some data. It installs a timeout that will expand a
+ * node of the treeview if not expanded yet. This function also calls
+ * gdk_drag_status in order to set the suggested action that will be
+ * used by the "drag-data-received" signal handler to know if we
+ * should do a move or just a copy of the data.
+ */
 static gboolean
-drag_motion_cb (GtkWidget      *widget,
+on_drag_motion (GtkWidget      *widget,
                GdkDragContext *context,
                gint            x,
                gint            y,
@@ -946,6 +1149,7 @@ drag_motion_cb (GtkWidget      *widget,
        GtkTreeViewDropPosition pos;
        GtkTreePath *dest_row;
        ModestFolderViewPrivate *priv;
+       GdkDragAction suggested_action;
 
        priv = MODEST_FOLDER_VIEW_GET_PRIVATE (widget);
 
@@ -969,39 +1173,68 @@ drag_motion_cb (GtkWidget      *widget,
        }
        gtk_tree_path_free (dest_row);
 
+       /* Select the desired action. By default we pick MOVE */
+       suggested_action = GDK_ACTION_MOVE;
+
+        if (context->actions == GDK_ACTION_COPY)
+            gdk_drag_status(context, GDK_ACTION_COPY, time);
+       else if (context->actions == GDK_ACTION_MOVE)
+            gdk_drag_status(context, GDK_ACTION_MOVE, time);
+       else if (context->actions & suggested_action)
+            gdk_drag_status(context, suggested_action, time);
+       else
+            gdk_drag_status(context, GDK_ACTION_DEFAULT, time);
+
        return TRUE;
 }
 
+/*
+ * This function sets the treeview as a source and a target for dnd
+ * events. It also connects all the requirede signals.
+ */
 static void
 setup_drag_and_drop (GtkTreeView *self)
 {
+       /* Set up the folder view as a dnd destination. Set only the
+          highlight flag, otherwise gtk will have a different
+          behaviour */
        gtk_drag_dest_set (GTK_WIDGET (self),
-                          GTK_DEST_DEFAULT_ALL,
+                          GTK_DEST_DEFAULT_HIGHLIGHT,
                           drag_types,
                           G_N_ELEMENTS (drag_types),
-                          GDK_ACTION_MOVE);
+                          GDK_ACTION_MOVE | GDK_ACTION_COPY);
 
        gtk_signal_connect(GTK_OBJECT (self),
                           "drag_data_received",
-                          GTK_SIGNAL_FUNC(drag_data_received_cb),
+                          GTK_SIGNAL_FUNC(on_drag_data_received),
                           NULL);
 
 
+       /* Set up the treeview as a dnd source */
        gtk_drag_source_set (GTK_WIDGET (self),
-                            GDK_BUTTON1_MASK | GDK_BUTTON3_MASK,
+                            GDK_BUTTON1_MASK,
                             drag_types,
                             G_N_ELEMENTS (drag_types),
-                            GDK_ACTION_MOVE);
+                            GDK_ACTION_MOVE | GDK_ACTION_COPY);
 
+       gtk_signal_connect(GTK_OBJECT (self),
+                          "drag_data_delete",
+                          GTK_SIGNAL_FUNC(drag_data_delete_cb),
+                          NULL);
 
        gtk_signal_connect(GTK_OBJECT (self),
                           "drag_motion",
-                          GTK_SIGNAL_FUNC(drag_motion_cb),
+                          GTK_SIGNAL_FUNC(on_drag_motion),
                           NULL);
 
 
        gtk_signal_connect(GTK_OBJECT (self),
                           "drag_data_get",
-                          GTK_SIGNAL_FUNC(drag_data_get_cb),
+                          GTK_SIGNAL_FUNC(on_drag_data_get),
+                          NULL);
+
+       gtk_signal_connect(GTK_OBJECT (self),
+                          "drag_drop",
+                          GTK_SIGNAL_FUNC(drag_drop_cb),
                           NULL);
 }