Fix for NB#59458 (should dim sort button when the selected folder
[modest] / src / widgets / modest-account-view.c
index 0ad2462..34fe761 100644 (file)
 #include <gtk/gtkcellrenderertext.h>
 #include <gtk/gtktreeselection.h>
 #include <gtk/gtkliststore.h>
+#include <string.h> /* For strcmp(). */
 
 /* 'private'/'protected' functions */
 static void modest_account_view_class_init    (ModestAccountViewClass *klass);
 static void modest_account_view_init          (ModestAccountView *obj);
 static void modest_account_view_finalize      (GObject *obj);
 
+static void modest_account_view_select_account (ModestAccountView *account_view, 
+       const gchar* account_name);
 
 typedef enum {
        MODEST_ACCOUNT_VIEW_NAME_COLUMN,
@@ -161,7 +164,14 @@ update_account_view (ModestAccountMgr *account_mgr, ModestAccountView *view)
        GSList *account_names, *cursor;
        GtkListStore *model;
                
-       model = GTK_LIST_STORE(gtk_tree_view_get_model (GTK_TREE_VIEW(view)));  
+       model = GTK_LIST_STORE(gtk_tree_view_get_model (GTK_TREE_VIEW(view)));
+       
+       /* Get the ID of the currently-selected account, 
+        * so we can select it again after rebuilding the list.
+        * Note that the name doesn't change even when the display name changes.
+        */
+       gchar *selected_name = modest_account_view_get_selected_account (view);
+
        gtk_list_store_clear (model);
 
        /* Note: We do not show disabled accounts.
@@ -203,7 +213,7 @@ update_account_view (ModestAccountMgr *account_mgr, ModestAccountView *view)
                        if (last_updated > 0) 
                                last_updated_string = modest_text_utils_get_display_date(last_updated);
                        else
-                               last_updated_string = g_strdup (_("FIXME: Never"));
+                               last_updated_string = g_strdup (_("mcen_va_never"));
                        
                        if (account_data->is_enabled) {
                                gtk_list_store_insert_with_values (
@@ -226,12 +236,18 @@ update_account_view (ModestAccountMgr *account_mgr, ModestAccountView *view)
                cursor = cursor->next;
        }
        g_slist_free (account_names);
+       
+       /* Try to re-select the same account: */
+       if (selected_name) {
+               modest_account_view_select_account (view, selected_name);
+               g_free (selected_name);
+       }
 }
 
 
 static void
 on_account_changed (ModestAccountMgr *account_mgr,
-                   const gchar* account, const gchar* key,
+                   const gchar* account, GSList *keys,
                    gboolean server_account, ModestAccountView *self)
 {      
        /* Never update the view in response to gconf changes.
@@ -243,12 +259,8 @@ on_account_changed (ModestAccountMgr *account_mgr,
         * notification does not happen so long after the key was set.
         * (We have no way to know when the last key was set, to do a final update)..
         */
-        return;
         
-       ModestAccountViewPrivate* priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(self);
-       
-       if (!priv->block_conf_updates)
-               update_account_view (account_mgr, self);
+       update_account_view (account_mgr, self);
 }
 
 
@@ -257,9 +269,7 @@ on_account_removed (ModestAccountMgr *account_mgr,
                    const gchar* account, gboolean server_account,
                    ModestAccountView *self)
 {
-       ModestAccountViewPrivate* priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(self);
-       if (!priv->block_conf_updates)
-               on_account_changed (account_mgr, account, NULL, server_account, self);
+       on_account_changed (account_mgr, account, NULL, server_account, self);
 }
 
 
@@ -292,6 +302,23 @@ on_account_enable_toggled (GtkCellRendererToggle *cell_renderer, gchar *path,
 }
 #endif
 
+static gboolean
+find_default_account(ModestAccountView *self, GtkTreeIter *iter)
+{
+       GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (self));
+       gboolean result;
+       for (result = gtk_tree_model_get_iter_first(model, iter);
+            result == TRUE; result = gtk_tree_model_iter_next(model, iter))
+       {
+               gboolean is_default;
+               gtk_tree_model_get (model, iter, MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, &is_default, -1);
+               if(is_default)
+                       return TRUE;
+       }
+
+       return FALSE;
+}
+
 static void
 on_account_default_toggled (GtkCellRendererToggle *cell_renderer, gchar *path,
                           ModestAccountView *self)
@@ -318,7 +345,20 @@ on_account_default_toggled (GtkCellRendererToggle *cell_renderer, gchar *path,
                            -1);
        
        /* Set this previously-non-default account as the default: */
-       modest_account_mgr_set_default_account (priv->account_mgr, account_name);
+       if (modest_account_mgr_set_default_account (priv->account_mgr, account_name))
+       {
+               /* Explicitely set default column because we are ignoring gconf changes */
+               GtkTreeIter old_default_iter;
+               if (find_default_account (self, &old_default_iter)) {
+                       gtk_list_store_set (GTK_LIST_STORE (model), &old_default_iter,
+                                           MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, FALSE, -1);
+               } else {
+                       g_warning ("%s: Did not find old default account in view", __FUNCTION__);
+               }
+
+               gtk_list_store_set (GTK_LIST_STORE (model), &iter,
+                                   MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, TRUE, -1);
+       }
 
        g_free (account_name);
 }
@@ -452,17 +492,51 @@ modest_account_view_get_selected_account (ModestAccountView *self)
        return account_name;
 }
 
+/* This allows us to pass more than one piece of data to the signal handler,
+ * and get a result: */
+typedef struct 
+{
+               ModestAccountView* self;
+               const gchar *account_name;
+} ForEachData;
 
-void modest_account_view_block_conf_updates (ModestAccountView *account_view)
+static gboolean
+on_model_foreach_select_account(GtkTreeModel *model, 
+       GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
 {
-       ModestAccountViewPrivate* priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(account_view);
-       priv->block_conf_updates = TRUE;
+       ForEachData *state = (ForEachData*)(user_data);
+       
+       /* Select the item if it has the matching account name: */
+       gchar *this_account_name = NULL;
+       gtk_tree_model_get (model, iter, 
+               MODEST_ACCOUNT_VIEW_NAME_COLUMN, &this_account_name, 
+               -1); 
+       if(this_account_name && state->account_name 
+               && (strcmp (this_account_name, state->account_name) == 0)) {
+               
+               GtkTreeSelection *selection = 
+                       gtk_tree_view_get_selection (GTK_TREE_VIEW (state->self));
+               gtk_tree_selection_select_iter (selection, iter);
+               
+               return TRUE; /* Stop walking the tree. */
+       }
+       
+       return FALSE; /* Keep walking the tree. */
 }
 
-void modest_account_view_unblock_conf_updates (ModestAccountView *account_view)
-{
-       ModestAccountViewPrivate* priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(account_view);
-       priv->block_conf_updates = FALSE;
+static void modest_account_view_select_account (ModestAccountView *account_view, 
+       const gchar* account_name)
+{      
+       /* Create a state instance so we can send two items of data to the signal handler: */
+       ForEachData *state = g_new0 (ForEachData, 1);
+       state->self = account_view;
+       state->account_name = account_name;
        
-       update_account_view (modest_runtime_get_account_mgr(), account_view);
+       GtkTreeModel *model = gtk_tree_view_get_model (
+               GTK_TREE_VIEW (account_view));
+       gtk_tree_model_foreach (model, 
+               on_model_foreach_select_account, state);
+               
+       g_free (state);
 }
+