* don't expunge messages after marking them as deleted
[modest] / src / widgets / modest-folder-view.c
index e798afc..fe09923 100644 (file)
@@ -41,6 +41,7 @@
 #include <tny-folder.h>
 #include <tny-camel-folder.h>
 #include <tny-simple-list.h>
+#include <modest-tny-account.h>
 #include <modest-tny-folder.h>
 #include <modest-tny-local-folders-account.h>
 #include <modest-tny-outbox-account.h>
@@ -125,9 +126,9 @@ static gint         expand_row_timeout     (gpointer data);
 
 static void         setup_drag_and_drop    (GtkTreeView *self);
 
-static void          _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         _clear_hidding_filter (ModestFolderView *folder_view);
 
 
 
@@ -249,6 +250,85 @@ 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 
+{
+       ModestFolderView *self;
+       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,
                 GtkTreeModel *tree_model,  GtkTreeIter *iter,  gpointer data)
@@ -272,7 +352,7 @@ text_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
                            TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &instance,
                            -1);
        rendobj = G_OBJECT(renderer);
+
        if (!fname)
                return;
 
@@ -281,8 +361,8 @@ text_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
                return;
        }
 
-       
-       priv =  MODEST_FOLDER_VIEW_GET_PRIVATE (data);
+       ModestFolderView *self = MODEST_FOLDER_VIEW (data);
+       priv =  MODEST_FOLDER_VIEW_GET_PRIVATE (self);
        
        gchar *item_name = NULL;
        gint item_weight = 400;
@@ -319,6 +399,15 @@ text_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
                                TNY_ACCOUNT (instance))) {
                        item_name = g_strdup (priv->local_account_name);
                        item_weight = 800;
+               } else if (modest_tny_account_is_memory_card_account (
+                               TNY_ACCOUNT (instance))) {
+                       /* fname is only correct when the items are first 
+                        * added to the model, not when the account is 
+                        * changed later, so get the name from the account
+                        * instance: */
+                       item_name = g_strdup (tny_account_get_name (
+                               TNY_ACCOUNT (instance)));
+                       item_weight = 800;
                } else {
                        item_name = g_strdup (fname);
                        item_weight = 800;
@@ -333,8 +422,9 @@ text_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
                g_object_set (rendobj,"text", item_name, "weight", item_weight, NULL);
                
                /* Notify display name observers */
+               /* TODO: What listens for this signal, and how can it use only the new name? */
                if (G_OBJECT (priv->cur_folder_store) == instance) {
-                       g_signal_emit (G_OBJECT(data),
+                       g_signal_emit (G_OBJECT(self),
                                               signals[FOLDER_DISPLAY_NAME_CHANGED_SIGNAL], 0,
                                               item_name);
                }
@@ -343,6 +433,24 @@ text_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
                
        }
        
+       /* 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);
 }
@@ -574,8 +682,8 @@ modest_folder_view_finalize (GObject *obj)
 
        if (priv->cur_folder_store) {
                if (TNY_IS_FOLDER(priv->cur_folder_store))
-                       tny_folder_sync (TNY_FOLDER(priv->cur_folder_store), TRUE, NULL);
-                       /* expunge the message */
+                       tny_folder_sync (TNY_FOLDER(priv->cur_folder_store), FALSE, NULL);
+                       /* FALSE --> expunge the message */
 
                g_object_unref (priv->cur_folder_store);
                priv->cur_folder_store = NULL;
@@ -712,7 +820,13 @@ modest_folder_view_on_map (ModestFolderView *self,
        /* This won't happen often */
        if (G_UNLIKELY (priv->reselect)) {
                /* Select the first inbox or the local account if not found */
-               modest_folder_view_select_first_inbox_or_local (self);
+
+               /* TODO: this could cause a lock at startup, so we
+                  comment it for the moment. We know that this will
+                  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); */
                priv->reselect = FALSE;
        }
        return FALSE;
@@ -948,8 +1062,8 @@ on_selection_changed (GtkTreeSelection *sel, gpointer user_data)
        /* Current folder was unselected */
        if (priv->cur_folder_store) {
                if (TNY_IS_FOLDER(priv->cur_folder_store))
-                       tny_folder_sync (TNY_FOLDER(priv->cur_folder_store), TRUE, NULL);
-               /* expunge the message */
+                       tny_folder_sync (TNY_FOLDER(priv->cur_folder_store), FALSE, 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);
@@ -1054,6 +1168,13 @@ cmp_rows (GtkTreeModel *tree_model, GtkTreeIter *iter1, GtkTreeIter *iter2,
                            TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &folder2,
                            -1);
 
+       /* Return if we get no folder. This could happen when folder
+          operations are happening. The model is updated after the
+          folder copy/move actually occurs, so there could be
+          situations where the model to be drawn is not correct */
+       if (!folder1 || !folder2)
+               return 0;
+
        if (type == TNY_FOLDER_TYPE_ROOT) {
                /* Compare the types, so that 
                 * Remote accounts -> Local account -> MMC account .*/
@@ -1859,7 +1980,8 @@ modest_folder_view_cut_selection (ModestFolderView *folder_view)
        priv = MODEST_FOLDER_VIEW_GET_PRIVATE (folder_view);
 
        /* Copy selection */
-       _clipboard_set_selected_data (folder_view, TRUE);
+       if (!_clipboard_set_selected_data (folder_view, TRUE))
+               return;
 
        /* Get hidding ids */
        hidding = modest_email_clipboard_get_hidding_ids (priv->clipboard, &n_selected); 
@@ -1878,23 +2000,32 @@ modest_folder_view_cut_selection (ModestFolderView *folder_view)
        gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (model));
 }
 
-static void
+/* Returns FALSE if it did not selected anything */
+static gboolean
 _clipboard_set_selected_data (ModestFolderView *folder_view,
                              gboolean delete)
 {
        ModestFolderViewPrivate *priv = NULL;
        TnyFolderStore *folder = NULL;
+       gboolean retval = FALSE;
 
-       g_return_if_fail (MODEST_IS_FOLDER_VIEW (folder_view));
+       g_return_val_if_fail (MODEST_IS_FOLDER_VIEW (folder_view), FALSE);
        priv = MODEST_FOLDER_VIEW_GET_PRIVATE (folder_view);
                
        /* Set selected data on clipboard   */
-       g_return_if_fail (MODEST_IS_EMAIL_CLIPBOARD (priv->clipboard));
+       g_return_val_if_fail (MODEST_IS_EMAIL_CLIPBOARD (priv->clipboard), FALSE);
        folder = modest_folder_view_get_selected (folder_view);
-       modest_email_clipboard_set_data (priv->clipboard, TNY_FOLDER(folder), NULL, delete);
+
+       /* Do not allow to select an account */
+       if (TNY_IS_FOLDER (folder)) {
+               modest_email_clipboard_set_data (priv->clipboard, TNY_FOLDER(folder), NULL, delete);
+               retval = TRUE;
+       }
 
        /* Free */
        g_object_unref (folder);
+
+       return retval;
 }
 
 static void