* Added an improvement for d&d, now it's not required to use the SHIFT key to do...
[modest] / src / widgets / modest-folder-view.c
index 2140365..20819f7 100644 (file)
@@ -41,6 +41,7 @@
 #include <tny-folder.h>
 #include <tny-camel-folder.h>
 #include <tny-simple-list.h>
+#include <tny-camel-account.h>
 #include <modest-tny-account.h>
 #include <modest-tny-folder.h>
 #include <modest-tny-local-folders-account.h>
 #include <modest-text-utils.h>
 #include <modest-runtime.h>
 #include "modest-folder-view.h"
-#include <modest-dnd.h>
 #include <modest-platform.h>
 #include <modest-widget-memory.h>
 #include <modest-ui-actions.h>
+#include "modest-dnd.h"
+
+/* Folder view drag types */
+const GtkTargetEntry folder_view_drag_types[] =
+{
+       { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, MODEST_FOLDER_ROW },
+       { GTK_TREE_PATH_AS_STRING_LIST, GTK_TARGET_SAME_APP, MODEST_HEADER_ROW }
+};
 
 /* 'private'/'protected' functions */
 static void modest_folder_view_class_init  (ModestFolderViewClass *klass);
@@ -124,7 +132,7 @@ static gboolean     on_drag_motion         (GtkWidget      *widget,
                                            guint           time,
                                            gpointer        user_data);
 
-static void expand_root_items (ModestFolderView *self);
+static void         expand_root_items (ModestFolderView *self);
 
 static gint         expand_row_timeout     (gpointer data);
 
@@ -177,6 +185,7 @@ struct _ModestFolderViewPrivate {
 
        gboolean  reselect; /* we use this to force a reselection of the INBOX */
        gboolean  show_non_move;
+       gboolean  reexpand; /* next time we expose, we'll expand all root folders */
 };
 #define MODEST_FOLDER_VIEW_GET_PRIVATE(o)                      \
        (G_TYPE_INSTANCE_GET_PRIVATE((o),                       \
@@ -263,7 +272,58 @@ modest_folder_view_class_init (ModestFolderViewClass *klass)
                              G_TYPE_NONE, 1, G_TYPE_STRING);
 }
 
+/* Simplify checks for NULLs: */
+static gboolean
+strings_are_equal (const gchar *a, const gchar *b)
+{
+       if (!a && !b)
+               return TRUE;
+       if (a && b)
+       {
+               return (strcmp (a, b) == 0);
+       }
+       else
+               return FALSE;
+}
 
+static gboolean
+on_model_foreach_set_name(GtkTreeModel *model, GtkTreePath *path,  GtkTreeIter *iter, gpointer data)
+{
+       GObject *instance = NULL;
+       
+       gtk_tree_model_get (model, iter,
+                           TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &instance,
+                           -1);
+                           
+       if (!instance)
+               return FALSE; /* keep walking */
+                       
+       if (!TNY_IS_ACCOUNT (instance)) {
+               g_object_unref (instance);
+               return FALSE; /* keep walking */        
+       }    
+       
+       /* Check if this is the looked-for account: */
+       TnyAccount *this_account = TNY_ACCOUNT (instance);
+       TnyAccount *account = TNY_ACCOUNT (data);
+       
+       const gchar *this_account_id = tny_account_get_id(this_account);
+       const gchar *account_id = tny_account_get_id(account);
+       g_object_unref (instance);
+       instance = NULL;
+
+       /* printf ("DEBUG: %s: this_account_id=%s, account_id=%s\n", __FUNCTION__, this_account_id, account_id); */
+       if (strings_are_equal(this_account_id, account_id)) {
+               /* Tell the model that the data has changed, so that
+                * it calls the cell_data_func callbacks again: */
+               /* TODO: This does not seem to actually cause the new string to be shown: */
+               gtk_tree_model_row_changed (model, path, iter);
+               
+               return TRUE; /* stop walking */
+       }
+       
+       return FALSE; /* keep walking */
+}
 
 typedef struct 
 {
@@ -271,6 +331,28 @@ typedef struct
        gchar *previous_name;
 } GetMmcAccountNameData;
 
+static void
+on_get_mmc_account_name (TnyStoreAccount* account, gpointer user_data)
+{
+       /* printf ("DEBU1G: %s: account name=%s\n", __FUNCTION__, tny_account_get_name (TNY_ACCOUNT(account))); */
+
+       GetMmcAccountNameData *data = (GetMmcAccountNameData*)user_data;
+       
+       if (!strings_are_equal (
+               tny_account_get_name(TNY_ACCOUNT(account)), 
+               data->previous_name)) {
+       
+               /* Tell the model that the data has changed, so that 
+                * it calls the cell_data_func callbacks again: */
+               ModestFolderView *self = data->self;
+               GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (self));
+               if (model)
+                       gtk_tree_model_foreach(model, on_model_foreach_set_name, account);
+       }
+
+       g_free (data->previous_name);
+       g_slice_free (GetMmcAccountNameData, data);
+}
 
 static void
 text_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
@@ -279,8 +361,6 @@ text_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
        ModestFolderViewPrivate *priv;
        GObject *rendobj;
        gchar *fname = NULL;
-       gint unread = 0;
-       gint all = 0;
        TnyFolderType type = TNY_FOLDER_TYPE_UNKNOWN;
        GObject *instance = NULL;
 
@@ -290,8 +370,6 @@ text_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
 
        gtk_tree_model_get (tree_model, iter,
                            TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &fname,
-                           TNY_GTK_FOLDER_STORE_TREE_MODEL_ALL_COLUMN, &all,
-                           TNY_GTK_FOLDER_STORE_TREE_MODEL_UNREAD_COLUMN, &unread,
                            TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
                            TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &instance,
                            -1);
@@ -323,12 +401,17 @@ text_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
                        }
                }
 
-               /* Select the number to show: the unread or unsent messages */
-               if ((type == TNY_FOLDER_TYPE_DRAFTS) || (type == TNY_FOLDER_TYPE_OUTBOX))
-                       number = all;
+               /* note: we cannot reliably get the counts from the tree model, we need
+                * to use explicit calls on tny_folder for some reason.
+                */
+               /* Select the number to show: the unread or unsent messages. in case of outbox/drafts, show all */
+               if ((type == TNY_FOLDER_TYPE_DRAFTS) ||
+                   (type == TNY_FOLDER_TYPE_OUTBOX) ||
+                   (type == TNY_FOLDER_TYPE_MERGE)) /* _OUTBOX actually returns _MERGE... */
+                       number = tny_folder_get_all_count (TNY_FOLDER(instance));
                else
-                       number = unread;
-               
+                       number = tny_folder_get_unread_count (TNY_FOLDER(instance));
+                                                               
                /* Use bold font style if there are unread or unset messages */
                if (number > 0) {
                        item_name = g_strdup_printf ("%s (%d)", fname, number);
@@ -375,7 +458,25 @@ text_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
                g_free (item_name);
                
        }
-                               
+       
+       /* If it is a Memory card account, make sure that we have the correct name.
+        * This function will be trigerred again when the name has been retrieved: */
+       if (TNY_IS_STORE_ACCOUNT (instance) && 
+               modest_tny_account_is_memory_card_account (TNY_ACCOUNT (instance))) {
+
+               /* Get the account name asynchronously: */
+               GetMmcAccountNameData *callback_data = 
+                       g_slice_new0(GetMmcAccountNameData);
+               callback_data->self = self;
+
+               const gchar *name = tny_account_get_name (TNY_ACCOUNT(instance));
+               if (name)
+                       callback_data->previous_name = g_strdup (name); 
+
+               modest_tny_account_get_mmc_account_name (TNY_STORE_ACCOUNT (instance), 
+                                                        on_get_mmc_account_name, callback_data);
+       }
+                       
        g_object_unref (G_OBJECT (instance));
        g_free (fname);
 }
@@ -389,7 +490,7 @@ icon_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
        TnyFolderType type = TNY_FOLDER_TYPE_UNKNOWN;
        const gchar *account_id = NULL;
        gboolean has_children;
-       
+
        rendobj = G_OBJECT(renderer);
        gtk_tree_model_get (tree_model, iter,
                            TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
@@ -544,6 +645,8 @@ modest_folder_view_init (ModestFolderView *obj)
        priv->visible_account_id = NULL;
        priv->folder_to_select = NULL;
 
+       priv->reexpand = TRUE;
+
        /* Initialize the local account name */
        conf = modest_runtime_get_conf();
        priv->local_account_name = modest_conf_get_string (conf, MODEST_CONF_DEVICE_NAME, NULL);
@@ -738,6 +841,7 @@ on_account_inserted (TnyAccountStore *account_store,
        /* Insert the account in the model */
        tny_list_append (TNY_LIST (gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (sort_model))),
                         G_OBJECT (account));
+
 }
 
 
@@ -760,7 +864,7 @@ on_account_changed (TnyAccountStore *account_store,
        /* Get the inner model */
        filter_model = gtk_tree_view_get_model (GTK_TREE_VIEW (user_data));
        sort_model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (filter_model));
-
+       
        /* Remove the account from the model */
        tny_list_remove (TNY_LIST (gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (sort_model))),
                         G_OBJECT (tny_account));
@@ -892,7 +996,10 @@ modest_folder_view_on_map (ModestFolderView *self,
                               NULL);
        }
 
-       expand_root_items (self); 
+       if (priv->reexpand) {
+               expand_root_items (self); 
+               priv->reexpand = FALSE;
+       }
 
        return FALSE;
 }
@@ -924,11 +1031,17 @@ static void
 expand_root_items (ModestFolderView *self)
 {
        GtkTreePath *path;
+       GtkTreeModel *model;
+       GtkTreeIter iter;
+
+       model = gtk_tree_view_get_model (GTK_TREE_VIEW (self));
        path = gtk_tree_path_new_first ();
 
        /* all folders should have child items, so.. */
-       while (gtk_tree_view_expand_row (GTK_TREE_VIEW(self), path, FALSE))
+       do {
+               gtk_tree_view_expand_row (GTK_TREE_VIEW(self), path, FALSE);
                gtk_tree_path_next (path);
+       } while (gtk_tree_model_get_iter (model, &iter, path));
        
        gtk_tree_path_free (path);
 }
@@ -1130,8 +1243,6 @@ on_selection_changed (GtkTreeSelection *sel, gpointer user_data)
        priv = MODEST_FOLDER_VIEW_GET_PRIVATE(user_data);
 
        selected = gtk_tree_selection_get_selected (sel, &model, &iter);
-/*     if(!gtk_tree_selection_get_selected (sel, &model, &iter)) */
-/*             return; */
 
        /* Notify the display name observers */
        g_signal_emit (G_OBJECT(user_data),
@@ -1146,7 +1257,7 @@ on_selection_changed (GtkTreeSelection *sel, gpointer user_data)
                                    -1);
 
                /* If the folder is the same do not notify */
-               if (priv->cur_folder_store == folder && folder) {
+               if (folder && priv->cur_folder_store == folder) {
                        g_object_unref (folder);
                        return;
                }
@@ -1361,7 +1472,6 @@ finish:
 /*****************************************************************************/
 /*                        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
@@ -1409,19 +1519,16 @@ typedef struct _DndHelper {
  * and drop action
  */
 static void
-on_progress_changed (ModestMailOperation *mail_op, 
-                    ModestMailOperationState *state,
-                    gpointer user_data)
+xfer_cb (ModestMailOperation *mail_op, 
+        gpointer user_data)
 {
        gboolean success;
        DndHelper *helper;
 
        helper = (DndHelper *) user_data;
 
-       if (!state->finished)
-               return;
-
-       if (state->status == MODEST_MAIL_OPERATION_STATUS_SUCCESS) {
+       if (modest_mail_operation_get_status (mail_op) == 
+           MODEST_MAIL_OPERATION_STATUS_SUCCESS) {
                success = TRUE;
        } else {
                success = FALSE;
@@ -1436,7 +1543,6 @@ on_progress_changed (ModestMailOperation *mail_op,
        g_slice_free (DndHelper, helper);
 }
 
-
 /* get the folder for the row the treepath refers to. */
 /* folder must be unref'd */
 static TnyFolder*
@@ -1452,18 +1558,6 @@ tree_path_to_folder (GtkTreeModel *model, GtkTreePath *path)
        return folder;
 }
 
-static void 
-show_banner_move_target_error ()
-{
-       ModestWindow *main_window;
-
-       main_window = modest_window_mgr_get_main_window(
-                       modest_runtime_get_window_mgr());
-                               
-       modest_platform_information_banner(GTK_WIDGET(main_window),
-                       NULL, _("mail_in_ui_folder_move_target_error"));
-}
-
 /*
  * This function is used by drag_data_received_cb to manage drag and
  * drop of a header, i.e, and drag from the header view to the folder
@@ -1473,79 +1567,78 @@ static void
 drag_and_drop_from_header_view (GtkTreeModel *source_model,
                                GtkTreeModel *dest_model,
                                GtkTreePath  *dest_row,
+                               GtkSelectionData *selection_data,
                                DndHelper    *helper)
 {
        TnyList *headers = NULL;
-       TnyHeader *header = NULL;
        TnyFolder *folder = NULL;
        ModestMailOperation *mail_op = NULL;
-       GtkTreeIter source_iter;
-       ModestWindowMgr *mgr = NULL; /*no need for unref*/
-       ModestWindow *main_win = NULL; /*no need for unref*/
-
-       g_return_if_fail (GTK_IS_TREE_MODEL(source_model));
-       g_return_if_fail (GTK_IS_TREE_MODEL(dest_model));
-       g_return_if_fail (dest_row);
-       g_return_if_fail (helper);
-
-       /* Get header */
-       gtk_tree_model_get_iter (source_model, &source_iter, helper->source_row);
-       gtk_tree_model_get (source_model, &source_iter, 
-                           TNY_GTK_HEADER_LIST_MODEL_INSTANCE_COLUMN, 
-                           &header, -1);
-       if (!TNY_IS_HEADER(header)) {
-               g_warning ("BUG: %s could not get a valid header", __FUNCTION__);
-               goto cleanup;
-       }
-       
-       /* Check if the selected message is in msg-view. If it is than
-        * do not enable drag&drop on that. */
+       GtkTreeIter source_iter, dest_iter;
+       ModestWindowMgr *mgr = NULL;
+       ModestWindow *main_win = NULL;
+       gchar **uris, **tmp;
+       gint response;
+
+       /* Build the list of headers */
        mgr = modest_runtime_get_window_mgr ();
-       if (modest_window_mgr_find_registered_header(mgr, header, NULL))
-               goto cleanup;
+       headers = tny_simple_list_new ();
+       uris = modest_dnd_selection_data_get_paths (selection_data);
+       tmp = uris;
 
-       /* Get Folder */
-       folder = tree_path_to_folder (dest_model, dest_row);
-       if (!TNY_IS_FOLDER(folder)) {
-               g_warning ("BUG: %s could not get a valid folder", __FUNCTION__);
-               show_banner_move_target_error();
-               goto cleanup;
-       }
-       if (modest_tny_folder_get_rules(folder) & MODEST_FOLDER_RULES_FOLDER_NON_WRITEABLE) {
-               g_debug ("folder rules: cannot write to that folder");
-               goto cleanup;
+       while (*tmp != NULL) {
+               TnyHeader *header;
+               GtkTreePath *path;
+
+               /* Get header */
+               path = gtk_tree_path_new_from_string (*tmp);
+               gtk_tree_model_get_iter (source_model, &source_iter, path);
+               gtk_tree_model_get (source_model, &source_iter, 
+                                   TNY_GTK_HEADER_LIST_MODEL_INSTANCE_COLUMN, 
+                                   &header, -1);
+
+               /* Do not enable d&d of headers already opened */
+               if (!modest_window_mgr_find_registered_header(mgr, header, NULL))
+                       tny_list_append (headers, G_OBJECT (header));
+
+               /* Free and go on */
+               gtk_tree_path_free (path);
+               g_object_unref (header);
+               tmp++;
        }
-       
-       headers = tny_simple_list_new ();
-       tny_list_append (headers, G_OBJECT (header));
+       g_strfreev (uris);
+
+       /* Get the target folder */
+       gtk_tree_model_get_iter (dest_model, &dest_iter, dest_row);
+       gtk_tree_model_get (dest_model, &dest_iter, 
+                           TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN,
+                           &folder, -1);
 
+       /* Ask for confirmation to move */
        main_win = modest_window_mgr_get_main_window(mgr);
-       if(msgs_move_to_confirmation(GTK_WINDOW(main_win), folder, TRUE, headers)
-                       == GTK_RESPONSE_CANCEL)
+       response = modest_ui_actions_msgs_move_to_confirmation (GTK_WINDOW(main_win), folder, 
+                                                               TRUE, headers);
+       if (response == GTK_RESPONSE_CANCEL)
                goto cleanup;
 
-       /* Transfer message */
+       /* Transfer messages */
        mail_op = modest_mail_operation_new_with_error_handling (MODEST_MAIL_OPERATION_TYPE_RECEIVE, 
                                                                 NULL,
                                                                 modest_ui_actions_move_folder_error_handler,
                                                                 NULL);
+
        modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
                                         mail_op);
-       g_signal_connect (G_OBJECT (mail_op), "progress-changed",
-                         G_CALLBACK (on_progress_changed), helper);
 
-       modest_mail_operation_xfer_msgs (mail_op, 
+       modest_mail_operation_xfer_msgs (mail_op,
                                         headers, 
                                         folder, 
                                         helper->delete_source, 
-                                        NULL, NULL);
+                                        xfer_cb, helper);
        
        /* Frees */
 cleanup:
        if (G_IS_OBJECT(mail_op))
                g_object_unref (G_OBJECT (mail_op));
-       if (G_IS_OBJECT(header))
-               g_object_unref (G_OBJECT (header));
        if (G_IS_OBJECT(folder))
                g_object_unref (G_OBJECT (folder));
        if (G_IS_OBJECT(headers))
@@ -1577,10 +1670,7 @@ drag_and_drop_from_folder_view (GtkTreeModel     *source_model,
                        ModestTnyFolderRules rules =
                                        modest_tny_folder_get_rules (folder);
                        forbidden = rules & MODEST_FOLDER_RULES_FOLDER_NON_WRITEABLE;
-
-                       if (forbidden)
-                               g_debug ("folder rules: cannot write to that folder");
-               } else if (TNY_IS_FOLDER_STORE(folder)){
+               } else if (TNY_IS_FOLDER_STORE(folder)) {
                        /* enable local root as destination for folders */
                        if (!MODEST_IS_TNY_LOCAL_FOLDERS_ACCOUNT (folder)
                                        && TNY_IS_ACCOUNT (folder))
@@ -1595,8 +1685,6 @@ drag_and_drop_from_folder_view (GtkTreeModel     *source_model,
                        ModestTnyFolderRules rules =
                                        modest_tny_folder_get_rules (folder);
                        forbidden = rules & MODEST_FOLDER_RULES_FOLDER_NON_MOVEABLE;
-                       if (forbidden)
-                               g_debug ("folder rules: cannot move that folder");
                } else
                        forbidden = TRUE;
                g_object_unref (folder);
@@ -1622,33 +1710,28 @@ drag_and_drop_from_folder_view (GtkTreeModel     *source_model,
                            &folder, -1);
 
        /* Offer the connection dialog if necessary, for the destination parent folder and source folder: */
-       if (modest_platform_connect_and_wait_if_network_folderstore (
-                               NULL, dest_folder) && 
-                       modest_platform_connect_and_wait_if_network_folderstore (
-                               NULL, TNY_FOLDER_STORE (folder))) {
+       if (modest_platform_connect_and_wait_if_network_folderstore (NULL, dest_folder) && 
+           modest_platform_connect_and_wait_if_network_folderstore (NULL, TNY_FOLDER_STORE (folder))) {
+               ModestWindowMgr *mgr = modest_runtime_get_window_mgr ();
+
                /* Do the mail operation */
-               mail_op = modest_mail_operation_new_with_error_handling (
-                               MODEST_MAIL_OPERATION_TYPE_RECEIVE, 
-                               NULL,
-                               modest_ui_actions_move_folder_error_handler,
-                               NULL);
-               modest_mail_operation_queue_add (
-                               modest_runtime_get_mail_operation_queue (), 
-                               mail_op);
-               g_signal_connect (
-                               G_OBJECT (mail_op),
-                               "progress-changed",
-                               G_CALLBACK (on_progress_changed),
-                               helper);
+               mail_op = 
+                       modest_mail_operation_new_with_error_handling (MODEST_MAIL_OPERATION_TYPE_RECEIVE, 
+                                                                      G_OBJECT (modest_window_mgr_get_main_window (mgr)),
+                                                                      modest_ui_actions_move_folder_error_handler,
+                                                                      folder);
+
+               modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), 
+                                                mail_op);
 
                modest_mail_operation_xfer_folder (mail_op, 
-                               folder, 
-                               dest_folder,
-                               helper->delete_source,
-                               NULL,
-                               NULL);
+                                                  folder, 
+                                                  dest_folder,
+                                                  helper->delete_source,
+                                                  xfer_cb,
+                                                  helper);
 
-               g_object_unref (G_OBJECT (mail_op));    
+               g_object_unref (G_OBJECT (mail_op));
        }
        
        /* Frees */
@@ -1694,16 +1777,8 @@ on_drag_data_received (GtkWidget *widget,
                   the operation, because the operation won't start if
                   the folder is in use */
                if (source_widget == widget) {
-                       ModestFolderViewPrivate *priv;
-
-                       priv = MODEST_FOLDER_VIEW_GET_PRIVATE (widget);
-                       if (priv->cur_folder_store) {
-                               g_object_unref (priv->cur_folder_store);
-                               priv->cur_folder_store = NULL;
-                       }
-
-                       g_signal_emit (G_OBJECT (widget), 
-                                      signals[FOLDER_SELECTION_CHANGED_SIGNAL], 0, NULL, FALSE);
+                       GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (widget));
+                       gtk_tree_selection_unselect_all (sel);
                }
        }
 
@@ -1711,17 +1786,8 @@ on_drag_data_received (GtkWidget *widget,
        if (selection_data == NULL || selection_data->length < 0)
                gtk_drag_finish (context, success, FALSE, time);
 
-       /* Get the models */
-       gtk_tree_get_row_drag_data (selection_data,
-                                   &source_model,
-                                   &source_row);
-
        /* Select the destination model */
-       if (source_widget == widget) {
-               dest_model = source_model;
-       } else {
-               dest_model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
-       }
+       dest_model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));  
 
        /* Get the path to the destination row. Can not call
           gtk_tree_view_get_drag_dest_row() because the source row
@@ -1730,35 +1796,43 @@ on_drag_data_received (GtkWidget *widget,
                                           &dest_row, &pos);
 
        /* Only allow drops IN other rows */
-       if (!dest_row || pos == GTK_TREE_VIEW_DROP_BEFORE || pos == GTK_TREE_VIEW_DROP_AFTER)
+       if (!dest_row || 
+           pos == GTK_TREE_VIEW_DROP_BEFORE || 
+           pos == GTK_TREE_VIEW_DROP_AFTER)
                gtk_drag_finish (context, success, FALSE, time);
 
        /* Create the helper */
        helper = g_slice_new0 (DndHelper);
        helper->delete_source = delete_source;
-       helper->source_row = gtk_tree_path_copy (source_row);
        helper->context = context;
        helper->time = time;
 
        /* Drags from the header view */
        if (source_widget != widget) {
+               source_model = gtk_tree_view_get_model (GTK_TREE_VIEW (source_widget));
 
                drag_and_drop_from_header_view (source_model,
                                                dest_model,
                                                dest_row,
+                                               selection_data,
                                                helper);
        } else {
-
+               /* Get the source model and row */
+               gtk_tree_get_row_drag_data (selection_data,
+                                           &source_model,
+                                           &source_row);
+               helper->source_row = gtk_tree_path_copy (source_row);
 
                drag_and_drop_from_folder_view (source_model,
                                                dest_model,
                                                dest_row,
                                                selection_data, 
                                                helper);
+
+               gtk_tree_path_free (source_row);
        }
 
        /* Frees */
-       gtk_tree_path_free (source_row);
        gtk_tree_path_free (dest_row);
 }
 
@@ -1846,9 +1920,11 @@ on_drag_motion (GtkWidget      *widget,
 {
        GtkTreeViewDropPosition pos;
        GtkTreePath *dest_row;
+       GtkTreeModel *dest_model;
        ModestFolderViewPrivate *priv;
        GdkDragAction suggested_action;
        gboolean valid_location = FALSE;
+       TnyFolder *folder;
 
        priv = MODEST_FOLDER_VIEW_GET_PRIVATE (widget);
 
@@ -1874,6 +1950,19 @@ on_drag_motion (GtkWidget      *widget,
                valid_location = TRUE;
        }
 
+       /* Check that the destination folder is writable */
+       dest_model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
+       folder = tree_path_to_folder (dest_model, dest_row);
+       if (folder) {
+               ModestTnyFolderRules rules = modest_tny_folder_get_rules(folder);
+
+               if (rules & MODEST_FOLDER_RULES_FOLDER_NON_WRITEABLE) {
+                       valid_location = FALSE;
+                       goto out;
+               }
+               g_object_unref (folder);
+       }
+
        /* Expand the selected row after 1/2 second */
        if (!gtk_tree_view_row_expanded (GTK_TREE_VIEW (widget), dest_row)) {
                gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (widget), dest_row, pos);
@@ -1896,17 +1985,10 @@ on_drag_motion (GtkWidget      *widget,
        if (dest_row)
                gtk_tree_path_free (dest_row);
        g_signal_stop_emission_by_name (widget, "drag-motion");
+
        return valid_location;
 }
 
-
-/* Folder view drag types */
-const GtkTargetEntry folder_view_drag_types[] =
-{
-       { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, MODEST_FOLDER_ROW },
-       { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_APP,    MODEST_HEADER_ROW }
-};
-
 /*
  * This function sets the treeview as a source and a target for dnd
  * events. It also connects all the requirede signals.
@@ -2023,10 +2105,14 @@ on_configuration_key_changed (ModestConf* conf,
                                                                           MODEST_CONF_DEVICE_NAME, NULL);
 
                /* Force a redraw */
-#if GTK_CHECK_VERSION(2, 8, 0) /* gtk_tree_view_column_queue_resize is only available in GTK+ 2.8 */
-               GtkTreeViewColumn * tree_column = gtk_tree_view_get_column (GTK_TREE_VIEW (self), 
-                                                                           TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN);
+#if GTK_CHECK_VERSION(2, 8, 0)
+               GtkTreeViewColumn * tree_column;
+
+               tree_column = gtk_tree_view_get_column (GTK_TREE_VIEW (self), 
+                                                       TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN);
                gtk_tree_view_column_queue_resize (tree_column);
+#else
+               gtk_widget_queue_draw (GTK_WIDGET (self));
 #endif
        }
 }
@@ -2143,6 +2229,7 @@ modest_folder_view_select_first_inbox_or_local (ModestFolderView *self)
 
        /* Select the row and free */
        gtk_tree_view_set_cursor (GTK_TREE_VIEW (self), path, NULL, FALSE);
+       gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (self), path, NULL, FALSE, 0.0, 0.0);
        gtk_tree_path_free (path);
 
        /* set focus */
@@ -2192,11 +2279,25 @@ on_row_inserted_maybe_select_folder (GtkTreeModel *tree_model, GtkTreePath  *pat
 {
        ModestFolderViewPrivate *priv = NULL;
        GtkTreeSelection *sel;
+       TnyFolderType type = TNY_FOLDER_TYPE_UNKNOWN;
+       GObject *instance = NULL;
 
        if (!MODEST_IS_FOLDER_VIEW(self))
                return;
        
        priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
+
+       priv->reexpand = TRUE;
+
+       gtk_tree_model_get (tree_model, iter, 
+                           TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
+                           TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &instance,
+                           -1);
+       if (type == TNY_FOLDER_TYPE_INBOX && priv->folder_to_select == NULL) {
+               priv->folder_to_select = g_object_ref (instance);
+       }
+       g_object_unref (instance);
+
        
        if (priv->folder_to_select) {