* step 2 of the account fixes; for now, account changes
[modest] / src / widgets / modest-folder-view.c
index 497d323..0353cd8 100644 (file)
@@ -69,15 +69,16 @@ static void         modest_folder_view_set_account_store (TnyAccountStoreView *s
 
 static void         on_selection_changed   (GtkTreeSelection *sel, gpointer data);
 
-static void         on_account_update      (TnyAccountStore *account_store, 
-                                           const gchar *account,
+static void         on_account_removed     (TnyAccountStore *self, 
+                                           TnyAccount *account,
                                            gpointer user_data);
 
-static void         on_account_removed     (TnyAccountStore *self, 
+static void         on_account_inserted    (TnyAccountStore *self, 
                                            TnyAccount *account,
                                            gpointer user_data);
 
-static void         on_accounts_reloaded   (TnyAccountStore *store, 
+static void         on_account_changed    (TnyAccountStore *self, 
+                                           TnyAccount *account,
                                            gpointer user_data);
 
 static gint         cmp_rows               (GtkTreeModel *tree_model, 
@@ -93,10 +94,11 @@ static gboolean     on_key_pressed         (GtkWidget *self,
                                            GdkEventKey *event,
                                            gpointer user_data);
 
-static void         on_configuration_key_changed         (ModestConf* conf, 
-                                                         const gchar *key, 
-                                                         ModestConfEvent event, 
-                                                         ModestFolderView *self);
+static void         on_configuration_key_changed  (ModestConf* conf, 
+                                                  const gchar *key, 
+                                                  ModestConfEvent event,
+                                                  ModestConfNotificationId notification_id, 
+                                                  ModestFolderView *self);
 
 /* DnD functions */
 static void         on_drag_data_get       (GtkWidget *widget, 
@@ -126,11 +128,14 @@ static gint         expand_row_timeout     (gpointer data);
 
 static void         setup_drag_and_drop    (GtkTreeView *self);
 
-static gboolean     _clipboard_set_selected_data (ModestFolderView *folder_view, gboolean delete);
+static gboolean     _clipboard_set_selected_data (ModestFolderView *folder_view, 
+                                                 gboolean delete);
 
 static void         _clear_hidding_filter (ModestFolderView *folder_view);
 
-static void          on_row_changed_maybe_select_folder (GtkTreeModel *tree_model, GtkTreePath  *path, GtkTreeIter  *iter,
+static void          on_row_changed_maybe_select_folder (GtkTreeModel     *tree_model, 
+                                                        GtkTreePath      *path, 
+                                                        GtkTreeIter      *iter,
                                                         ModestFolderView *self);
 
 enum {
@@ -146,10 +151,12 @@ struct _ModestFolderViewPrivate {
 
        TnyFolder            *folder_to_select; /* folder to select after the next update */
 
-       gulong                account_update_signal;
+       ModestConfNotificationId notification_id;
+
        gulong                changed_signal;
-       gulong                accounts_reloaded_signal;
+       gulong                account_inserted_signal;
        gulong                account_removed_signal;
+       gulong                account_changed_signal;
        gulong                conf_key_signal;
        
        /* not unref this object, its a singlenton */
@@ -166,8 +173,8 @@ struct _ModestFolderViewPrivate {
        gchar                *visible_account_id;
        ModestFolderViewStyle style;
 
-       gboolean              reselect; /* we use this to force a reselection of the INBOX */
-       gboolean                                                        show_non_move;
+       gboolean  reselect; /* we use this to force a reselection of the INBOX */
+       gboolean  show_non_move;
 };
 #define MODEST_FOLDER_VIEW_GET_PRIVATE(o)                      \
        (G_TYPE_INSTANCE_GET_PRIVATE((o),                       \
@@ -255,7 +262,8 @@ modest_folder_view_class_init (ModestFolderViewClass *klass)
 }
 
 /* Simplify checks for NULLs: */
-static gboolean strings_are_equal (const gchar *a, const gchar *b)
+static gboolean
+strings_are_equal (const gchar *a, const gchar *b)
 {
        if (!a && !b)
                return TRUE;
@@ -267,7 +275,8 @@ static gboolean strings_are_equal (const gchar *a, const gchar *b)
                return FALSE;
 }
 
-static gboolean on_model_foreach_set_name(GtkTreeModel *model, GtkTreePath *path,  GtkTreeIter *iter, gpointer data)
+static gboolean
+on_model_foreach_set_name(GtkTreeModel *model, GtkTreePath *path,  GtkTreeIter *iter, gpointer data)
 {
        GObject *instance = NULL;
        
@@ -311,12 +320,13 @@ typedef struct
        gchar *previous_name;
 } GetMmcAccountNameData;
 
-static void on_get_mmc_account_name (TnyStoreAccount* account, gpointer user_data)
+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)) {
@@ -374,8 +384,9 @@ text_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
        if (type != TNY_FOLDER_TYPE_ROOT) {
                gint number = 0;
                
-               if (modest_tny_folder_is_local_folder (TNY_FOLDER (instance))) {
-                       type = modest_tny_folder_get_local_folder_type (TNY_FOLDER (instance));
+               if (modest_tny_folder_is_local_folder (TNY_FOLDER (instance)) ||
+                   modest_tny_folder_is_memory_card_folder (TNY_FOLDER (instance))) {
+                       type = modest_tny_folder_get_local_or_mmc_folder_type (TNY_FOLDER (instance));
                        if (type != TNY_FOLDER_TYPE_UNKNOWN) {
                                g_free (fname);
                                fname = g_strdup(modest_local_folder_info_get_type_display_name (type));
@@ -432,7 +443,6 @@ text_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
                                               signals[FOLDER_DISPLAY_NAME_CHANGED_SIGNAL], 0,
                                               item_name);
                }
-               
                g_free (item_name);
                
        }
@@ -459,8 +469,6 @@ text_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
        g_free (fname);
 }
 
-
-
 static void
 icon_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
                 GtkTreeModel *tree_model,  GtkTreeIter *iter, gpointer data)
@@ -468,28 +476,19 @@ icon_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
        GObject *rendobj = NULL, *instance = NULL;
        GdkPixbuf *pixbuf = NULL;
        TnyFolderType type = TNY_FOLDER_TYPE_UNKNOWN;
-       gchar *fname = NULL;
        const gchar *account_id = NULL;
-       gint unread = 0;
        gboolean has_children;
        
        rendobj = G_OBJECT(renderer);
        gtk_tree_model_get (tree_model, iter,
                            TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
-                           TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &fname,
-                           TNY_GTK_FOLDER_STORE_TREE_MODEL_UNREAD_COLUMN, &unread,
                            TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &instance,
                            -1);
        has_children = gtk_tree_model_iter_has_child (tree_model, iter);
 
-       if (!fname)
+       if (!instance) 
                return;
 
-       if (!instance) {
-               g_free (fname);
-               return;
-       }
-
        /* MERGE is not needed anymore as the folder now has the correct type jschmid */
        /* We include the MERGE type here because it's used to create
           the local OUTBOX folder */
@@ -541,7 +540,6 @@ icon_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
        }
        
        g_object_unref (G_OBJECT (instance));
-       g_free (fname);
 
        /* Set pixbuf */
        g_object_set (rendobj, "pixbuf", pixbuf, NULL);
@@ -661,10 +659,12 @@ modest_folder_view_init (ModestFolderView *obj)
         * Track changes in the local account name (in the device it
         * will be the device name)
         */
-       priv->conf_key_signal = 
-               g_signal_connect (G_OBJECT(conf), 
-                                 "key_changed",
-                                 G_CALLBACK(on_configuration_key_changed), obj);
+       priv->notification_id = modest_conf_listen_to_namespace (conf, 
+                                                                MODEST_CONF_NAMESPACE);
+       priv->conf_key_signal = g_signal_connect (G_OBJECT(conf), 
+                                                 "key_changed",
+                                                 G_CALLBACK(on_configuration_key_changed), 
+                                                 obj);
 }
 
 static void
@@ -687,6 +687,12 @@ modest_folder_view_finalize (GObject *obj)
        
        priv =  MODEST_FOLDER_VIEW_GET_PRIVATE(obj);
 
+       if (priv->notification_id) {
+               modest_conf_forget_namespace (modest_runtime_get_conf (),
+                                             MODEST_CONF_NAMESPACE,
+                                             priv->notification_id);
+       }
+
        if (priv->timer_expander != 0) {
                g_source_remove (priv->timer_expander);
                priv->timer_expander = 0;
@@ -694,11 +700,11 @@ modest_folder_view_finalize (GObject *obj)
 
        if (priv->account_store) {
                g_signal_handler_disconnect (G_OBJECT(priv->account_store),
-                                            priv->account_update_signal);
-               g_signal_handler_disconnect (G_OBJECT(priv->account_store),
-                                            priv->accounts_reloaded_signal);
+                                            priv->account_inserted_signal);
                g_signal_handler_disconnect (G_OBJECT(priv->account_store),
                                             priv->account_removed_signal);
+               g_signal_handler_disconnect (G_OBJECT(priv->account_store),
+                                            priv->account_changed_signal);
                g_object_unref (G_OBJECT(priv->account_store));
                priv->account_store = NULL;
        }
@@ -756,88 +762,124 @@ modest_folder_view_set_account_store (TnyAccountStoreView *self, TnyAccountStore
 
        if (G_UNLIKELY (priv->account_store)) {
 
-               if (g_signal_handler_is_connected (G_OBJECT (priv->account_store), 
-                                                  priv->account_update_signal))
-                       g_signal_handler_disconnect (G_OBJECT (priv->account_store), 
-                                                    priv->account_update_signal);
-               if (g_signal_handler_is_connected (G_OBJECT (priv->account_store), 
-                                                  priv->accounts_reloaded_signal))
-                       g_signal_handler_disconnect (G_OBJECT (priv->account_store), 
-                                                    priv->accounts_reloaded_signal);
+               if (g_signal_handler_is_connected (G_OBJECT (priv->account_store),
+                                                  priv->account_inserted_signal))
+                       g_signal_handler_disconnect (G_OBJECT (priv->account_store),
+                                                    priv->account_inserted_signal);
                if (g_signal_handler_is_connected (G_OBJECT (priv->account_store), 
                                                   priv->account_removed_signal))
                        g_signal_handler_disconnect (G_OBJECT (priv->account_store), 
                                                     priv->account_removed_signal);
-
+               if (g_signal_handler_is_connected (G_OBJECT (priv->account_store), 
+                                                  priv->account_changed_signal))
+                       g_signal_handler_disconnect (G_OBJECT (priv->account_store), 
+                                                    priv->account_changed_signal);
                g_object_unref (G_OBJECT (priv->account_store));
        }
 
        priv->account_store = g_object_ref (G_OBJECT (account_store));
 
-       priv->account_update_signal = 
-               g_signal_connect (G_OBJECT(account_store), "account_update",
-                                 G_CALLBACK (on_account_update), self);
-
        priv->account_removed_signal = 
                g_signal_connect (G_OBJECT(account_store), "account_removed",
                                  G_CALLBACK (on_account_removed), self);
 
-       priv->accounts_reloaded_signal = 
-               g_signal_connect (G_OBJECT(account_store), "accounts_reloaded",
-                                 G_CALLBACK (on_accounts_reloaded), self);
+       priv->account_inserted_signal =
+               g_signal_connect (G_OBJECT(account_store), "account_inserted",
+                                 G_CALLBACK (on_account_inserted), self);
 
-       g_signal_connect (G_OBJECT(account_store), "connecting_finished",
-                               G_CALLBACK (on_accounts_reloaded), self);
+       priv->account_changed_signal =
+               g_signal_connect (G_OBJECT(account_store), "account_changed",
+                                 G_CALLBACK (on_account_changed), self);
 
-       on_accounts_reloaded (account_store, (gpointer ) self);
+       modest_folder_view_update_model (MODEST_FOLDER_VIEW (self), account_store);
        
        g_object_unref (G_OBJECT (device));
 }
 
 static void
-on_account_removed (TnyAccountStore *account_store, 
-                   TnyAccount *account,
-                   gpointer user_data)
+on_account_inserted (TnyAccountStore *account_store, 
+                    TnyAccount *account,
+                    gpointer user_data)
 {
-       ModestFolderView *self = MODEST_FOLDER_VIEW (user_data);
        ModestFolderViewPrivate *priv;
+       GtkTreeModel *sort_model, *filter_model;
 
-       priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
+       /* Ignore transport account insertions, we're not showing them
+          in the folder view */
+       if (TNY_IS_TRANSPORT_ACCOUNT (account))
+               return;
 
-       /* If the removed account is the currently viewed one then
-          clear the configuration value. The new visible account will be the default account */
-       if (!strcmp (priv->visible_account_id, tny_account_get_id (account))) {
-               modest_folder_view_set_account_id_of_visible_server_account (self, NULL);
-       }
+       priv = MODEST_FOLDER_VIEW_GET_PRIVATE (user_data);
+
+       /* If we're adding a new account, and there is no previous
+          one, we need to select the visible server account */
+       if (priv->style == MODEST_FOLDER_VIEW_STYLE_SHOW_ONE &&
+           !priv->visible_account_id)
+               modest_widget_memory_restore (modest_runtime_get_conf(), 
+                                             G_OBJECT (user_data),
+                                             MODEST_CONF_FOLDER_VIEW_KEY);
+
+       /* 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));
+
+       /* 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));
+}
+
+
+static void
+on_account_changed (TnyAccountStore *account_store, TnyAccount *tny_account,
+                   gpointer user_data)
+{
+       g_warning ("%s: account_id = %s", __FUNCTION__, tny_account_get_id(tny_account));
 }
 
+
+
 static void
-on_account_update (TnyAccountStore *account_store, 
-                  const gchar *account,
-                  gpointer user_data)
+on_account_removed (TnyAccountStore *account_store, 
+                   TnyAccount *account,
+                   gpointer user_data)
 {
        ModestFolderView *self = NULL;
        ModestFolderViewPrivate *priv;
+       GtkTreeModel *sort_model, *filter_model;
+
+       /* Ignore transport account removals, we're not showing them
+          in the folder view */
+       if (TNY_IS_TRANSPORT_ACCOUNT (account))
+               return;
+
+       g_print ("--------------------- FOLDER ---------------\n");
 
-       g_return_if_fail (MODEST_IS_FOLDER_VIEW (user_data));
        self = MODEST_FOLDER_VIEW (user_data);
        priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
 
-       if (!priv->visible_account_id)
+       /* TODO: invalidate the cur_folder_* and folder_to_select things */
+
+       /* Remove the account from the model */
+       filter_model = gtk_tree_view_get_model (GTK_TREE_VIEW (self));
+       sort_model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (filter_model));
+       tny_list_remove (TNY_LIST (gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (sort_model))),
+                        G_OBJECT (account));
+
+       /* If the removed account is the currently viewed one then
+          clear the configuration value. The new visible account will be the default account */
+       if (priv->visible_account_id &&
+           !strcmp (priv->visible_account_id, tny_account_get_id (account))) {
+
+               /* Clear the current visible account_id */
+               modest_folder_view_set_account_id_of_visible_server_account (self, NULL);
+
+               /* Call the restore method, this will set the new visible account */
                modest_widget_memory_restore (modest_runtime_get_conf(), G_OBJECT(self),
                                              MODEST_CONF_FOLDER_VIEW_KEY);
+       }
 
-       if (!modest_folder_view_update_model (self, account_store))
-               g_printerr ("modest: failed to update model for changes in '%s'",
-                           account);
-}
-
-static void 
-on_accounts_reloaded   (TnyAccountStore *account_store, 
-                       gpointer user_data)
-{
-       g_return_if_fail (MODEST_IS_FOLDER_VIEW (user_data));
-       modest_folder_view_update_model (MODEST_FOLDER_VIEW (user_data), account_store);
+       /* Select the INBOX */
+       modest_folder_view_select_first_inbox_or_local (self);
 }
 
 void
@@ -876,8 +918,13 @@ modest_folder_view_on_map (ModestFolderView *self,
                   be a bug, because the INBOX is not selected, but we
                   need to rewrite some parts of Modest to avoid the
                   deathlock situation */
-/*             modest_folder_view_select_first_inbox_or_local (self); */
+               /* TODO: check if this is still the case */
                priv->reselect = FALSE;
+               modest_folder_view_select_first_inbox_or_local (self);
+               /* Notify the display name observers */
+               g_signal_emit (G_OBJECT(self),
+                              signals[FOLDER_DISPLAY_NAME_CHANGED_SIGNAL], 0,
+                              NULL);
        }
        return FALSE;
 }
@@ -936,7 +983,7 @@ filter_row (GtkTreeModel *model,
        guint i;
        gboolean found = FALSE;
        gboolean cleared = FALSE;
-       
+
        g_return_val_if_fail (MODEST_IS_FOLDER_VIEW (data), FALSE);
        priv = MODEST_FOLDER_VIEW_GET_PRIVATE (data);
 
@@ -961,11 +1008,17 @@ filter_row (GtkTreeModel *model,
        
                        /* If it isn't a special folder, 
                         * don't show it unless it is the visible account: */
-                       if (!modest_tny_account_is_virtual_local_folders (acc) &&
-                               strcmp (account_id, MODEST_MMC_ACCOUNT_ID)) { 
+                       if (priv->style == MODEST_FOLDER_VIEW_STYLE_SHOW_ONE &&
+                           !modest_tny_account_is_virtual_local_folders (acc) &&
+                           strcmp (account_id, MODEST_MMC_ACCOUNT_ID)) {
+                               
                                /* Show only the visible account id */
-                               if (priv->visible_account_id && strcmp (account_id, priv->visible_account_id))
+                               if (priv->visible_account_id) {
+                                       if (strcmp (account_id, priv->visible_account_id))
+                                               retval = FALSE;
+                               } else {
                                        retval = FALSE;
+                               }                               
                        }
                        
                        /* Never show these to the user. They are merged into one folder 
@@ -974,8 +1027,6 @@ filter_row (GtkTreeModel *model,
                                retval = FALSE;
                }
        }
-       
-       /* The virtual local-folders folder store is also shown by default. */
 
        /* Check hiding (if necessary) */
        cleared = modest_email_clipboard_cleared (priv->clipboard);            
@@ -989,18 +1040,23 @@ filter_row (GtkTreeModel *model,
                retval = !found;
        }
        
+       
+       /* If this is a move to dialog, hide Sent, Outbox and Drafts
+       folder as no message can be move there according to UI specs */
        if (!priv->show_non_move)
        {
                switch (type)
                {
                        case TNY_FOLDER_TYPE_OUTBOX:
                        case TNY_FOLDER_TYPE_SENT:
+                       case TNY_FOLDER_TYPE_DRAFTS:
                                retval = FALSE;
                                break;
                        case TNY_FOLDER_TYPE_UNKNOWN:
                        case TNY_FOLDER_TYPE_NORMAL:
                                type = modest_tny_folder_guess_folder_type(TNY_FOLDER(instance));
-                               if (type == TNY_FOLDER_TYPE_OUTBOX || type == TNY_FOLDER_TYPE_SENT)
+                               if (type == TNY_FOLDER_TYPE_OUTBOX || type == TNY_FOLDER_TYPE_SENT
+                                               || type == TNY_FOLDER_TYPE_DRAFTS)
                                {
                                        retval = FALSE;
                                }
@@ -1043,7 +1099,7 @@ modest_folder_view_update_model (ModestFolderView *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);
+       model        = tny_gtk_folder_store_tree_model_new (NULL);
        
        /* Deal with the model via its TnyList Interface,
         * filling the TnyList via a get_accounts() call: */
@@ -1064,7 +1120,7 @@ modest_folder_view_update_model (ModestFolderView *self,
                                              GTK_SORT_ASCENDING);
        gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (sortable),
                                         TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN,
-                                cmp_rows, NULL, NULL);
+                                        cmp_rows, NULL, NULL);
 
        /* Create filter model */
        filter_model = gtk_tree_model_filter_new (sortable, NULL);
@@ -1072,13 +1128,6 @@ modest_folder_view_update_model (ModestFolderView *self,
                                                filter_row,
                                                self,
                                                NULL);
-/*     if (priv->style == MODEST_FOLDER_VIEW_STYLE_SHOW_ONE) { */
-/*             filter_model = gtk_tree_model_filter_new (sortable, NULL); */
-/*             gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (filter_model), */
-/*                                                     filter_row, */
-/*                                                     self, */
-/*                                                     NULL); */
-/*     } */
 
        /* Set new model */
        gtk_tree_view_set_model (GTK_TREE_VIEW(self), filter_model);
@@ -1087,16 +1136,9 @@ modest_folder_view_update_model (ModestFolderView *self,
        g_signal_connect (G_OBJECT(filter_model), "row-inserted",
                          (GCallback)on_row_changed_maybe_select_folder, self);
 
-       
-
-/*     gtk_tree_view_set_model (GTK_TREE_VIEW(self),  */
-/*                              (filter_model) ? filter_model : sortable); */
 
        g_object_unref (model);
-       g_object_unref (filter_model);
-/*     if (filter_model) */
-/*             g_object_unref (filter_model); */
-                       
+       g_object_unref (filter_model);          
        g_object_unref (sortable);
 
        /* Force a reselection of the INBOX next time the widget is shown */
@@ -1141,13 +1183,14 @@ on_selection_changed (GtkTreeSelection *sel, gpointer user_data)
        
        /* Current folder was unselected */
        if (priv->cur_folder_store) {
+               g_signal_emit (G_OBJECT(tree_view), signals[FOLDER_SELECTION_CHANGED_SIGNAL], 0,
+                      priv->cur_folder_store, FALSE);
+
                if (TNY_IS_FOLDER(priv->cur_folder_store))
-                       tny_folder_sync_async (TNY_FOLDER(priv->cur_folder_store), 
+                       tny_folder_sync_async (TNY_FOLDER(priv->cur_folder_store),
                                               FALSE, NULL, NULL, NULL);
                /* FALSE --> don't expunge the messages */
 
-               g_signal_emit (G_OBJECT(tree_view), signals[FOLDER_SELECTION_CHANGED_SIGNAL], 0,
-                      priv->cur_folder_store, FALSE);
                g_object_unref (priv->cur_folder_store);
                priv->cur_folder_store = NULL;
        }
@@ -1313,8 +1356,8 @@ cmp_rows (GtkTreeModel *tree_model, GtkTreeIter *iter1, GtkTreeIter *iter2,
                        if ((parent_type == TNY_FOLDER_TYPE_ROOT) &&
                            TNY_IS_ACCOUNT (parent_folder) &&
                            modest_tny_account_is_virtual_local_folders (TNY_ACCOUNT (parent_folder))) {
-                               cmp1 = get_cmp_subfolder_type_pos (modest_tny_folder_get_local_folder_type (TNY_FOLDER (folder1)));
-                               cmp2 = get_cmp_subfolder_type_pos (modest_tny_folder_get_local_folder_type (TNY_FOLDER (folder2)));
+                               cmp1 = get_cmp_subfolder_type_pos (modest_tny_folder_get_local_or_mmc_folder_type (TNY_FOLDER (folder1)));
+                               cmp2 = get_cmp_subfolder_type_pos (modest_tny_folder_get_local_or_mmc_folder_type (TNY_FOLDER (folder2)));
                        }
                        g_object_unref (parent_folder);
                }
@@ -1421,6 +1464,22 @@ 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*
+tree_path_to_folder (GtkTreeModel *model, GtkTreePath *path)
+{
+       GtkTreeIter iter;
+       TnyFolder *folder = NULL;
+       
+       if (gtk_tree_model_get_iter (model,&iter, path))
+               gtk_tree_model_get (model, &iter,
+                                   TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder,
+                                   -1);
+       return folder;
+}
+
 /*
  * 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
@@ -1436,13 +1495,13 @@ drag_and_drop_from_header_view (GtkTreeModel *source_model,
        TnyHeader *header = NULL;
        TnyFolder *folder = NULL;
        ModestMailOperation *mail_op = NULL;
-       GtkTreeIter source_iter, dest_iter;
+       GtkTreeIter source_iter;
 
        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, 
@@ -1454,15 +1513,16 @@ drag_and_drop_from_header_view (GtkTreeModel *source_model,
        }
        
        /* Get 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);
-
+       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__);
                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;
+       }
+       
 
        /* Transfer message */
        mail_op = modest_mail_operation_new_with_error_handling (MODEST_MAIL_OPERATION_TYPE_RECEIVE, 
@@ -1510,13 +1570,35 @@ drag_and_drop_from_folder_view (GtkTreeModel     *source_model,
        GtkTreeIter parent_iter, iter;
        TnyFolderStore *parent_folder = NULL;
        TnyFolder *folder = NULL;
+       gboolean forbidden = TRUE;
+
+       /* check the folder rules for the destination */
+       folder = tree_path_to_folder (dest_model, dest_row);
+       if (folder) {
+               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");
+               g_object_unref (folder);
+       }
+       
+       if (!forbidden) {
+               /* check the folder rules for the source */
+               folder = tree_path_to_folder (source_model, helper->source_row);
+               if (folder) {
+                       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");
+                       g_object_unref (folder);
+               }
+       }
 
+       
        /* Check if the drag is possible */
-/*     if (!gtk_tree_path_compare (helper->source_row, dest_row) || */
-/*         !gtk_tree_drag_dest_row_drop_possible (GTK_TREE_DRAG_DEST (dest_model), */
-/*                                                dest_row, */
-/*                                                selection_data)) { */
-       if (!gtk_tree_path_compare (helper->source_row, dest_row)) {
+       if (forbidden || !gtk_tree_path_compare (helper->source_row, dest_row)) {
 
                gtk_drag_finish (helper->context, FALSE, FALSE, helper->time);
                gtk_tree_path_free (helper->source_row);        
@@ -1553,12 +1635,13 @@ drag_and_drop_from_folder_view (GtkTreeModel     *source_model,
                                           helper->delete_source,
                                           NULL,
                                           NULL);
+
+               g_object_unref (G_OBJECT (mail_op));    
        }
        
        /* Frees */
        g_object_unref (G_OBJECT (parent_folder));
        g_object_unref (G_OBJECT (folder));
-       g_object_unref (G_OBJECT (mail_op));
 }
 
 /*
@@ -1904,17 +1987,20 @@ on_key_pressed (GtkWidget *self,
 static void 
 on_configuration_key_changed (ModestConf* conf, 
                              const gchar *key, 
-                             ModestConfEvent event, 
+                             ModestConfEvent event,
+                             ModestConfNotificationId id, 
                              ModestFolderView *self)
 {
        ModestFolderViewPrivate *priv;
 
-       if (!key)
-               return;
 
        g_return_if_fail (MODEST_IS_FOLDER_VIEW (self));
        priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
 
+       /* Do not listen for changes in other namespaces */
+       if (priv->notification_id != id)
+                return;
+        
        if (!strcmp (key, MODEST_CONF_DEVICE_NAME)) {
                g_free (priv->local_account_name);
 
@@ -1933,7 +2019,7 @@ on_configuration_key_changed (ModestConf* conf,
        }
 }
 
-void 
+void
 modest_folder_view_set_style (ModestFolderView *self,
                              ModestFolderViewStyle style)
 {
@@ -1994,33 +2080,15 @@ find_inbox_iter (GtkTreeModel *model, GtkTreeIter *iter, GtkTreeIter *inbox_iter
        do {
                GtkTreeIter child;
                TnyFolderType type = TNY_FOLDER_TYPE_UNKNOWN;
-               gchar *name = NULL;
 
                gtk_tree_model_get (model, iter, 
-                                   TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &name,
                                    TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, 
                                    &type, -1);
-
-               /*
-               printf ("DEBUG: %s: name=%s, type=%d, TNY_FOLDER_TYPE_INBOX=%d\n", 
-                       __FUNCTION__, name, type, TNY_FOLDER_TYPE_INBOX);
-               */
                        
                gboolean result = FALSE;
                if (type == TNY_FOLDER_TYPE_INBOX) {
                        result = TRUE;
-               } else if (type == TNY_FOLDER_TYPE_NORMAL) {
-                       /* tinymail's camel implementation only provides TNY_FOLDER_TYPE_NORMAL
-                        * when getting folders from the cache, before connectin, so we do 
-                        * an extra check. We could fix this in tinymail, but it's easier 
-                        * to do here.
-                        */
-                        if (strcmp (name, "Inbox") == 0)
-                               result = TRUE;
-               }
-               
-               g_free (name);
-               
+               }               
                if (result) {
                        *inbox_iter = *iter;
                        return TRUE;
@@ -2045,10 +2113,7 @@ modest_folder_view_select_first_inbox_or_local (ModestFolderView *self)
        GtkTreeModel *model;
        GtkTreeIter iter, inbox_iter;
        GtkTreeSelection *sel;
-
-/*     /\* Do not set it if the folder view was not painted *\/ */
-/*     if (!GTK_WIDGET_MAPPED (self)) */
-/*             return; */
+       GtkTreePath *path = NULL;
 
        model = gtk_tree_view_get_model (GTK_TREE_VIEW (self));
        if (!model)
@@ -2058,15 +2123,15 @@ modest_folder_view_select_first_inbox_or_local (ModestFolderView *self)
        sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
 
        gtk_tree_model_get_iter_first (model, &iter);
-       gtk_tree_selection_unselect_all (sel);
 
-       if (find_inbox_iter (model, &iter, &inbox_iter)) {
-               gtk_tree_selection_select_iter (sel, &inbox_iter);
-       }
-       else {
-               gtk_tree_model_get_iter_first (model, &iter);
-               gtk_tree_selection_select_iter (sel, &iter);
-       }
+       if (find_inbox_iter (model, &iter, &inbox_iter))
+               path = gtk_tree_model_get_path (model, &inbox_iter);
+       else
+               path = gtk_tree_path_new_first ();
+
+       /* Select the row and free */
+       gtk_tree_view_set_cursor (GTK_TREE_VIEW (self), path, NULL, FALSE);
+       gtk_tree_path_free (path);
 }
 
 
@@ -2118,10 +2183,8 @@ on_row_changed_maybe_select_folder (GtkTreeModel *tree_model, GtkTreePath  *path
        if (!MODEST_IS_FOLDER_VIEW(self))
                return;
        
-       g_return_if_fail (MODEST_IS_FOLDER_VIEW (self));
        priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
        
-       
        if (priv->folder_to_select) {
                
                if (!modest_folder_view_select_folder (self, priv->folder_to_select,
@@ -2137,10 +2200,10 @@ on_row_changed_maybe_select_folder (GtkTreeModel *tree_model, GtkTreePath  *path
 
                        gtk_tree_path_free (path);
                
-               } 
+               }
                g_object_unref (priv->folder_to_select);
                priv->folder_to_select = NULL;
-       } 
+       }
 }
 
 
@@ -2159,6 +2222,10 @@ modest_folder_view_select_folder (ModestFolderView *self, TnyFolder *folder,
        priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
 
        if (after_change) {
+
+               sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
+               gtk_tree_selection_unselect_all (sel);
+
                if (priv->folder_to_select)
                        g_object_unref(priv->folder_to_select);
                priv->folder_to_select = TNY_FOLDER(g_object_ref(folder));