Reconnecting after setting account settings
[modest] / src / widgets / modest-folder-view.c
index d4a0001..9297351 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>
@@ -124,7 +125,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 +178,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),                       \
@@ -352,18 +354,15 @@ 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;
-       
+
        g_return_if_fail (column);
        g_return_if_fail (tree_model);
+       g_return_if_fail (iter != NULL);
 
        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);
@@ -395,12 +394,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);
@@ -463,7 +467,7 @@ text_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
                        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);
+                                                        on_get_mmc_account_name, callback_data);
        }
                        
        g_object_unref (G_OBJECT (instance));
@@ -479,7 +483,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,
@@ -634,6 +638,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);
@@ -715,6 +721,7 @@ modest_folder_view_finalize (GObject *obj)
                priv->query = NULL;
        }
 
+/*     modest_folder_view_disable_next_folder_selection (MODEST_FOLDER_VIEW(obj)); */
        if (priv->folder_to_select) {
                g_object_unref (G_OBJECT(priv->folder_to_select));
                priv->folder_to_select = NULL;
@@ -827,14 +834,37 @@ 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));
+
 }
 
 
 static void
-on_account_changed (TnyAccountStore *account_store, TnyAccount *tny_account,
+on_account_changed (TnyAccountStore *account_store, 
+                   TnyAccount *tny_account,
                    gpointer user_data)
 {
        /* do nothing */
+       ModestFolderViewPrivate *priv;
+       GtkTreeModel *sort_model, *filter_model;
+
+       /* Ignore transport account insertions, we're not showing them
+          in the folder view */
+       if (TNY_IS_TRANSPORT_ACCOUNT (tny_account))
+               return;
+
+       priv = MODEST_FOLDER_VIEW_GET_PRIVATE (user_data);
+
+       /* 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));
+
+       /* 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 (tny_account));
 }
 
 
@@ -857,14 +887,38 @@ on_account_removed (TnyAccountStore *account_store,
        self = MODEST_FOLDER_VIEW (user_data);
        priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
 
-       /* invalidate the cur_folder_* things */
-       sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
-       gtk_tree_selection_unselect_all (sel);
+       /* Invalidate the cur_folder_store only if the selected folder
+          belongs to the account that is being removed */
+       if (priv->cur_folder_store) {
+               TnyAccount *selected_folder_account = NULL;
+
+               if (TNY_IS_FOLDER (priv->cur_folder_store)) {
+                       selected_folder_account = 
+                               tny_folder_get_account (TNY_FOLDER (priv->cur_folder_store));
+               } else {
+                       selected_folder_account = 
+                               TNY_ACCOUNT (g_object_ref (priv->cur_folder_store));
+               }
+
+               if (selected_folder_account == account) {
+                       sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
+                       gtk_tree_selection_unselect_all (sel);
+               }
+               g_object_unref (selected_folder_account);
+       }
 
-       /* Invalidate row to select */
+       /* Invalidate row to select only if the folder to select
+          belongs to the account that is being removed*/
        if (priv->folder_to_select) {
-               g_object_unref (priv->folder_to_select);
-               priv->folder_to_select = NULL;
+               TnyAccount *folder_to_select_account = NULL;
+
+               folder_to_select_account = tny_folder_get_account (priv->folder_to_select);
+               if (folder_to_select_account == account) {
+/*                     modest_folder_view_disable_next_folder_selection (self); */
+                       g_object_unref (priv->folder_to_select);
+                       priv->folder_to_select = NULL;
+               }
+               g_object_unref (folder_to_select_account);
        }
 
        /* Remove the account from the model */
@@ -935,7 +989,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;
 }
@@ -967,11 +1024,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);
 }
@@ -1173,8 +1236,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),
@@ -2066,10 +2127,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
        }
 }
@@ -2186,6 +2251,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 */
@@ -2235,11 +2301,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) {
                
@@ -2257,12 +2337,29 @@ on_row_inserted_maybe_select_folder (GtkTreeModel *tree_model, GtkTreePath  *pat
                        gtk_tree_path_free (path);
                
                }
-               g_object_unref (priv->folder_to_select);
-               priv->folder_to_select = NULL;
+
+               /* Disable next */
+               modest_folder_view_disable_next_folder_selection (self);
+/*             g_object_unref (priv->folder_to_select); */
+/*             priv->folder_to_select = NULL; */
        }
 }
 
 
+void
+modest_folder_view_disable_next_folder_selection (ModestFolderView *self) 
+{
+       ModestFolderViewPrivate *priv = NULL;
+
+       g_return_if_fail (MODEST_IS_FOLDER_VIEW (self));        
+       priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
+
+       if (priv->folder_to_select)
+               g_object_unref(priv->folder_to_select);
+       
+       priv->folder_to_select = NULL;
+}
+
 gboolean
 modest_folder_view_select_folder (ModestFolderView *self, TnyFolder *folder, 
                                  gboolean after_change)