From 652527fd671cc695045b75893d7010c246585ef2 Mon Sep 17 00:00:00 2001 From: Murray Cumming Date: Mon, 30 Apr 2007 14:44:29 +0000 Subject: [PATCH] 2007-04-30 Murray Cumming * src/modest-account-mgr-helpers.h: * src/modest-account-mgr-helpers.c: Added modest_account_mgr_unset_default_account(), Added modest_account_mgr_set_first_account_as_default(). * src/modest-account-mgr.c: (modest_account_mgr_remove_account): If it was the default account, unset the default account name. * src/maemo/modest-account-view-window.c: (on_delete_button_clicked): If it was the default account, set the first remaining account as the default instead. * src/maemo/modest-main-window.c: (on_account_update): Do not try to use a NULL account or a NULL default account, to prevent a crash. This should not happen now anyway. Bug #55343 in projects.maemo.org/bugzilla. pmo-trunk-r1724 --- ChangeLog2 | 19 +++++++++++++++++++ src/maemo/modest-account-view-window.c | 18 ++++++++++++++++-- src/maemo/modest-main-window.c | 23 ++++++++++++++++------- src/modest-account-mgr-helpers.c | 31 +++++++++++++++++++++++++++++++ src/modest-account-mgr-helpers.h | 26 +++++++++++++++++++++++++- src/modest-account-mgr.c | 8 ++++++++ 6 files changed, 115 insertions(+), 10 deletions(-) diff --git a/ChangeLog2 b/ChangeLog2 index 0c53298..b5c569c 100644 --- a/ChangeLog2 +++ b/ChangeLog2 @@ -1,5 +1,24 @@ 2007-04-30 Murray Cumming + * src/modest-account-mgr-helpers.h: + * src/modest-account-mgr-helpers.c: + Added modest_account_mgr_unset_default_account(), + Added modest_account_mgr_set_first_account_as_default(). + + * src/modest-account-mgr.c: (modest_account_mgr_remove_account): + If it was the default account, unset the default account name. + + * src/maemo/modest-account-view-window.c: + (on_delete_button_clicked): If it was the default account, + set the first remaining account as the default instead. + + * src/maemo/modest-main-window.c: (on_account_update): Do not try to use + a NULL account or a NULL default account, to prevent a crash. This should + not happen now anyway. Bug #55343 in projects.maemo.org/bugzilla. + + +2007-04-30 Murray Cumming + * src/widgets/modest-recpt-editor.c: (modest_recpt_editor_on_key_press_event): Put an #ifdef around the use of gtk_text_buffer_get_has_selection() (from GTK+ 2.10), and add a diff --git a/src/maemo/modest-account-view-window.c b/src/maemo/modest-account-view-window.c index ad889e4..869600e 100644 --- a/src/maemo/modest-account-view-window.c +++ b/src/maemo/modest-account-view-window.c @@ -180,9 +180,23 @@ on_delete_button_clicked (GtkWidget *button, ModestAccountViewWindow *self) if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT) { /* Remove account. If succeeded it removes also the account from the ModestAccountView */ + + gboolean is_default = FALSE; + gchar *default_account_name = modest_account_mgr_get_default_account (account_mgr); + if (default_account_name && (strcmp (default_account_name, account_name) == 0)) + is_default = TRUE; + g_free (default_account_name); + removed = modest_account_mgr_remove_account (account_mgr, - account_name, - FALSE); + account_name, + FALSE); + + if (removed && is_default) { + /* Set a different account as the default, so there is always at least one default: + * This is not specified, and might be the wrong behaviour. murrayc. */ + modest_account_mgr_set_first_account_as_default (account_mgr); + } + if (removed) { /* Show confirmation dialog ??? */ } else { diff --git a/src/maemo/modest-main-window.c b/src/maemo/modest-main-window.c index 712bf38..5614da4 100644 --- a/src/maemo/modest-main-window.c +++ b/src/maemo/modest-main-window.c @@ -778,19 +778,28 @@ on_account_update (TnyAccountStore *account_store, default_account = modest_account_mgr_get_default_account (mgr); do { - TnyAccount *acc; - const gchar *acc_name; - gchar *display_name; + TnyAccount *acc = NULL; + const gchar *acc_name = NULL; /* Create tool item */ acc = TNY_ACCOUNT (tny_iterator_get_current (iter)); - acc_name = tny_account_get_name (acc); + if (acc) + acc_name = tny_account_get_name (acc); /* Create display name */ - if (!strcmp (default_account, acc_name)) - display_name = g_strdup_printf (_("mcen_me_toolbar_sendreceive_default"), acc_name); + gchar *display_name = NULL; + if (acc_name) { + if (default_account && !(strcmp (default_account, acc_name) == 0)) + display_name = g_strdup_printf (_("mcen_me_toolbar_sendreceive_default"), acc_name); + else + display_name = g_strdup_printf (_("mcen_me_toolbar_sendreceive_mailbox_n"), acc_name); + } else - display_name = g_strdup_printf (_("mcen_me_toolbar_sendreceive_mailbox_n"), acc_name); + { + /* TODO: This probably should never happen: */ + display_name = g_strdup_printf (_("mcen_me_toolbar_sendreceive_default"), ""); + } + item = gtk_menu_item_new_with_label (display_name); diff --git a/src/modest-account-mgr-helpers.c b/src/modest-account-mgr-helpers.c index 6b2c10d..6be7d5c 100644 --- a/src/modest-account-mgr-helpers.c +++ b/src/modest-account-mgr-helpers.c @@ -568,6 +568,37 @@ modest_account_mgr_set_default_account (ModestAccountMgr *self, const gchar* ac } +gboolean +modest_account_mgr_unset_default_account (ModestAccountMgr *self) +{ + ModestConf *conf; + + g_return_val_if_fail (self, FALSE); + + conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf; + + return modest_conf_remove_key (conf, MODEST_CONF_DEFAULT_ACCOUNT, NULL /* err */); + +} + +gboolean +modest_account_mgr_set_first_account_as_default (ModestAccountMgr *self) +{ + gboolean result = FALSE; + GSList *account_names = modest_account_mgr_account_names (self); + if(account_names) + { + const gchar* account_name = (const gchar*)account_names->data; + if (account_name) + result = modest_account_mgr_set_default_account (self, account_name); + } + + /* TODO: Free the strings too? */ + g_slist_free (account_names); + + return result; +} + gchar* modest_account_mgr_get_from_string (ModestAccountMgr *self, const gchar* name) { diff --git a/src/modest-account-mgr-helpers.h b/src/modest-account-mgr-helpers.h index f90dbcd..60d91ef 100644 --- a/src/modest-account-mgr-helpers.h +++ b/src/modest-account-mgr-helpers.h @@ -94,7 +94,7 @@ ModestAccountData *modest_account_mgr_get_account_data (ModestAccountMgr *se gchar* modest_account_mgr_get_default_account (ModestAccountMgr *self); /** - * modest_account_mgr_get_default_account: + * modest_account_mgr_set_default_account: * @self: a ModestAccountMgr instance * @account: the name of an existing account * @@ -106,6 +106,30 @@ gboolean modest_account_mgr_set_default_account (ModestAccountMgr *self, const gchar* account); /** + * modest_account_mgr_unset_default_account: + * @self: a ModestAccountMgr instance + * @account: the name of an account + * + * Unset the default account name, so that no account is the default. + * + * Returns: TRUE if succeeded, FALSE otherwise + */ +gboolean modest_account_mgr_unset_default_account (ModestAccountMgr *self); + +/** + * modest_account_mgr_set_first_account_as_default: + * @self: a ModestAccountMgr instance + * + * Guarantees that at least one account, if there are any accounts, is the default, + * so that modest_account_mgr_get_default_account() will return non-NULL if there + * are any accounts. + * + * Returns: TRUE if succeeded, FALSE otherwise + */ +gboolean +modest_account_mgr_set_first_account_as_default (ModestAccountMgr *self); + +/** * modest_account_mgr_free_account_data: * @self: a ModestAccountMgr instance * @data: a ModestAccountData instance diff --git a/src/modest-account-mgr.c b/src/modest-account-mgr.c index 89a3d5b..735ab8e 100644 --- a/src/modest-account-mgr.c +++ b/src/modest-account-mgr.c @@ -548,6 +548,14 @@ modest_account_mgr_remove_account (ModestAccountMgr * self, g_printerr ("modest: error removing key: %s\n", err->message); g_error_free (err); } + + /* If this was the default, then remove that setting: */ + if (!server_account) { + gchar *default_account_name = modest_account_mgr_get_default_account (self); + if (default_account_name && (strcmp (default_account_name, name) == 0)) + modest_account_mgr_unset_default_account (self); + g_free (default_account_name); + } return retval; } -- 1.7.9.5