Fixes NB#66410
[modest] / src / widgets / modest-account-view.c
index e09f6b0..a5ebda0 100644 (file)
@@ -58,6 +58,8 @@ static void on_display_name_changed            (ModestAccountMgr *self,
                                                const gchar *account,
                                                gpointer user_data);
 
+static void modest_account_view_select_first_account (ModestAccountView *account_view);
+
 typedef enum {
        MODEST_ACCOUNT_VIEW_NAME_COLUMN,
        MODEST_ACCOUNT_VIEW_DISPLAY_NAME_COLUMN,
@@ -146,22 +148,23 @@ modest_account_view_finalize (GObject *obj)
        G_OBJECT_CLASS(parent_class)->finalize (obj);
 }
 
-/* Get the string for the last updated time. Result must be g_freed */
-static gchar*
+/* Get the string for the last updated time. Result must NOT be g_freed */
+static const gchar*
 get_last_updated_string(ModestAccountMgr* account_mgr, ModestAccountData *account_data)
 {
        /* FIXME: let's assume that 'last update' applies to the store account... */
-       gchar* last_updated_string;
+       const gchar* last_updated_string;
        time_t last_updated = account_data->store_account->last_updated;
        if (!modest_account_mgr_account_is_busy(account_mgr, account_data->account_name)) {
                if (last_updated > 0) 
                        last_updated_string = modest_text_utils_get_display_date(last_updated);
                else
-                       last_updated_string = g_strdup (_("mcen_va_never"));
+                       last_updated_string = _("mcen_va_never");
        } else  {
                /* FIXME: There should be a logical name in the UI specs */
-               last_updated_string = g_strdup(_("mcen_va_refreshing"));
+               last_updated_string = _("mcen_va_refreshing");
        }
+       
        return last_updated_string;
 }
 
@@ -207,8 +210,9 @@ update_account_view (ModestAccountMgr *account_mgr, ModestAccountView *view)
                if (account_data->store_account) {
 
                        GtkTreeIter iter;
-                       
-                       gchar *last_updated_string = get_last_updated_string(account_mgr, account_data);
+
+                       /* don't free */
+                       const gchar *last_updated_string = get_last_updated_string(account_mgr, account_data);
                        
                        if (account_data->is_enabled) {
                                const gchar *proto_name;
@@ -224,7 +228,6 @@ update_account_view (ModestAccountMgr *account_mgr, ModestAccountView *view)
                                        MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN,  last_updated_string,
                                        -1);
                        }
-                       g_free (last_updated_string);
                }
 
                modest_account_mgr_free_account_data (account_mgr, account_data);
@@ -267,11 +270,10 @@ on_account_busy_changed(ModestAccountMgr *account_mgr,
                                g_free (cur_name);
                                return;
                        }
-                       gchar* last_updated_string = get_last_updated_string(account_mgr, account_data);
+                       const gchar* last_updated_string = get_last_updated_string(account_mgr, account_data);
                        gtk_list_store_set(model, &iter, 
                                           MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN, last_updated_string,
                                           -1);
-                       g_free (last_updated_string);
                        modest_account_mgr_free_account_data (account_mgr, account_data);
                        found = TRUE;
                }
@@ -310,7 +312,15 @@ on_account_removed (TnyAccountStore *account_store,
 
        self = MODEST_ACCOUNT_VIEW (user_data);
        priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE (self);
-
+       
+       gchar *selected_name = modest_account_view_get_selected_account (self);
+       if (selected_name == NULL) {
+               /* we select the first account if none is selected */
+               modest_account_view_select_first_account (self);                
+       } else {
+               g_free (selected_name);
+       }
+       
        update_account_view (priv->account_mgr, self);
 }
 
@@ -588,6 +598,21 @@ modest_account_view_select_account (ModestAccountView *account_view,
 }
 
 static void
+modest_account_view_select_first_account (ModestAccountView *account_view)
+{
+       GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (account_view));
+       g_return_if_fail (model != NULL);
+
+       GtkTreeIter iter;
+       gtk_tree_model_get_iter_first (model, &iter);
+
+       GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (account_view));
+       g_return_if_fail (selection != NULL);
+
+       gtk_tree_selection_select_iter (selection, &iter);
+}
+
+static void
 on_default_account_changed (ModestAccountMgr *mgr,
                            gpointer user_data)
 {
@@ -610,7 +635,7 @@ on_default_account_changed (ModestAccountMgr *mgr,
                                    -1);
 
                /* Update the default account column */
-               if (!strcmp (name, default_account_name))
+               if ((default_account_name != NULL) && (!strcmp (name, default_account_name)))
                        gtk_list_store_set (GTK_LIST_STORE (model), &iter,
                                            MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, TRUE, -1);
                else