Refactor, breaking large function.

- gnc_plugin_page_account_tree_cmd_delete_account() is still too long but would be messy to break
- rename the _int function and break it further
- remove passing of account name
- simplify some of the code by reversing if(...) to if(!...)
This commit is contained in:
jean 2020-05-06 20:49:14 -07:00
parent 402b1c86a2
commit 4f8652c2e5

View File

@ -74,6 +74,25 @@
/* This static indicates the debugging module that this .o belongs to. */
static QofLogModule log_module = GNC_MOD_GUI;
/********************************************************************
* delete_account_helper
* See if this account has any splits present. Set the user data
* and return the same value to stop walking the account tree if
* appropriate.
********************************************************************/
typedef struct _delete_helper
{
gboolean has_splits;
gboolean has_ro_splits;
} delete_helper_t;
static void delete_account_next (GtkAction *action, GncPluginPageAccountTree *page, Account* ta,
Account* sta, Account* saa, delete_helper_t delete_res);
static void delete_account_final (Account* account, Account* saa, Account* sta, Account* ta);
#define PLUGIN_PAGE_ACCT_TREE_CM_CLASS "plugin-page-acct-tree"
#define STATE_SECTION "Account Hierarchy"
@ -1207,18 +1226,6 @@ gnc_plugin_page_account_tree_cmd_cascade_color_account (GtkAction *action, GncPl
LEAVE(" ");
}
/********************************************************************
* delete_account_helper
* See if this account has any splits present. Set the user data
* and return the same value to stop walking the account tree if
* appropriate.
********************************************************************/
typedef struct _delete_helper
{
gboolean has_splits;
gboolean has_ro_splits;
} delete_helper_t;
static gpointer
delete_account_helper (Account * account, gpointer data)
{
@ -1348,158 +1355,6 @@ gppat_setup_account_selector (GtkBuilder *builder, GtkWidget *dialog,
return selector;
}
// This is a helper function for gnc_plugin_page_account_tree_cmd_delete_account
static void
gnc_plugin_page_account_tree_cmd_delete_account_int (GtkAction *action, GncPluginPageAccountTree *page, Account* ta,
Account* sta, Account* saa, gchar* acct_name, delete_helper_t delete_res)
{
Account *account = gnc_plugin_page_account_tree_get_current_account (page);
GList* splits = xaccAccountGetSplitList(account);
GtkWidget* window = gnc_plugin_page_get_window(GNC_PLUGIN_PAGE(page));
gint response;
/*
* Present a message to the user which specifies what will be
* deleted and what will be reparented, then ask for verification.
*/
const char *format = _("The account %s will be deleted.");
char *lines[8];
char *message;
char *name;
int i = 0;
GtkWidget *dialog;
lines[0] = g_strdup_printf(format, acct_name);
if (splits)
{
if (ta)
{
name = gnc_account_get_full_name(ta);
format = _("All transactions in this account will be moved to "
"the account %s.");
lines[++i] = g_strdup_printf(format, name);
}
else if (splits)
{
format = _("All transactions in this account will be deleted.");
lines[++i] = g_strdup_printf("%s", format);
}
}
if (gnc_account_n_children(account) > 0)
{
if (saa)
{
name = gnc_account_get_full_name(saa);
format = _("All of its sub-accounts will be moved to "
"the account %s.");
lines[++i] = g_strdup_printf(format, name);
}
else
{
format = _("All of its subaccounts will be deleted.");
lines[++i] = g_strdup_printf("%s", format);
if (sta)
{
name = gnc_account_get_full_name(sta);
format = _("All sub-account transactions will be moved to "
"the account %s.");
lines[++i] = g_strdup_printf(format, name);
}
else if (delete_res.has_splits)
{
format = _("All sub-account transactions will be deleted.");
lines[++i] = g_strdup_printf("%s", format);
}
}
}
lines[++i] = _("Are you sure you want to do this?");
lines[i] = NULL;
i--; /* Don't try to free the constant question. */
message = g_strjoinv(" ", lines);
while (i--)
{
g_free(lines[i]);
}
dialog = gtk_message_dialog_new(GTK_WINDOW(window),
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_QUESTION,
GTK_BUTTONS_NONE,
"%s", message);
g_free(message);
gtk_dialog_add_buttons(GTK_DIALOG(dialog),
_("_Cancel"), GTK_RESPONSE_CANCEL,
_("_Delete"), GTK_RESPONSE_ACCEPT,
(gchar *)NULL);
gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_CANCEL);
response = gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
if (GTK_RESPONSE_ACCEPT == response)
{
GList *acct_list, *ptr;
const GncGUID *guid;
gchar guidstr[GUID_ENCODING_LENGTH+1];
gnc_set_busy_cursor(NULL, TRUE);
gnc_suspend_gui_refresh ();
/* Move subaccounts and transactions if this was requested */
xaccAccountBeginEdit (account);
if (saa)
{
xaccAccountBeginEdit (saa);
acct_list = gnc_account_get_children(account);
for (ptr = acct_list; ptr; ptr = g_list_next(ptr))
gnc_account_append_child (saa, ptr->data);
g_list_free(acct_list);
xaccAccountCommitEdit (saa);
}
else if (sta)
{
/* Move the splits of its subaccounts, if any. */
gnc_account_foreach_descendant(account,
(AccountCb)xaccAccountMoveAllSplits,
sta);
}
if (ta)
{
/* Move the splits of the account to be deleted. */
xaccAccountMoveAllSplits (account, ta);
}
xaccAccountCommitEdit (account);
/* Drop all references from the state file for
* any subaccount the account still has
*/
acct_list = gnc_account_get_children(account);
for (ptr = acct_list; ptr; ptr = g_list_next(ptr))
{
guid = xaccAccountGetGUID (ptr->data);
guid_to_string_buff (guid, guidstr);
gnc_state_drop_sections_for (guidstr);
}
g_list_free(acct_list);
/* Drop all references from the state file for this account
*/
guid = xaccAccountGetGUID (account);
guid_to_string_buff (guid, guidstr);
gnc_state_drop_sections_for (guidstr);
/*
* Finally, delete the account, any subaccounts it may still
* have, and any splits it or its subaccounts may still have.
*/
xaccAccountBeginEdit (account);
xaccAccountDestroy (account);
gnc_resume_gui_refresh ();
gnc_unset_busy_cursor(NULL);
}
g_free(acct_name);
}
static void
gnc_plugin_page_account_tree_cmd_delete_account (GtkAction *action, GncPluginPageAccountTree *page)
{
@ -1519,8 +1374,14 @@ gnc_plugin_page_account_tree_cmd_delete_account (GtkAction *action, GncPluginPag
gboolean ta_match = FALSE;
gboolean sta_match = FALSE;
gboolean saa_match = FALSE;
GList *filter = NULL;
GtkBuilder *builder = NULL;
GtkWidget *dialog = NULL;
GtkWidget *widget = NULL;
gchar *title = NULL;
gnc_commodity *account_currency = NULL;
if (NULL == account)
if (account == NULL)
return;
/* If the account has objects referring to it, show the list - the account can't be deleted until these
@ -1538,24 +1399,20 @@ gnc_plugin_page_account_tree_cmd_delete_account (GtkAction *action, GncPluginPag
window = gnc_plugin_page_get_window(GNC_PLUGIN_PAGE(page));
acct_name = gnc_account_get_full_name(account);
if (!acct_name)
{
acct_name = g_strdup (_("(no name)"));
}
splits = xaccAccountGetSplitList(account);
/*
* If the account has transactions or child accounts then present a
* dialog to allow the user to specify what should be done with them.
*/
if ((NULL != splits) || (gnc_account_n_children(account) > 0))
{
GList *filter = NULL;
GtkBuilder *builder = NULL;
GtkWidget *dialog = NULL;
GtkWidget *widget = NULL;
gchar *title = NULL;
gnc_commodity *account_currency = NULL;
if ((splits == NULL) && (gnc_account_n_children(account) == 0))
return;
if (gnc_account_n_children(account) > 1) {
gchar* message = g_strdup_printf("Account '%s' has more than one subaccount, move subaccounts or delete them before attempting to delete this account.", acct_name);
gnc_error_dialog(GTK_WINDOW(window),"%s", message);
g_free (message);
g_free(acct_name);
return;
}
builder = gtk_builder_new();
gnc_builder_add_from_file (builder, "dialog-account.glade", "account_delete_dialog");
@ -1567,6 +1424,7 @@ gnc_plugin_page_account_tree_cmd_delete_account (GtkAction *action, GncPluginPag
title = g_strdup_printf(_("Deleting account %s"), acct_name);
gtk_label_set_text(GTK_LABEL(widget), title);
g_free(title);
g_free(acct_name);
widget = GTK_WIDGET(gtk_builder_get_object (builder, DELETE_DIALOG_OK_BUTTON));
g_object_set_data(G_OBJECT(dialog), DELETE_DIALOG_OK_BUTTON, widget);
@ -1612,22 +1470,6 @@ gnc_plugin_page_account_tree_cmd_delete_account (GtkAction *action, GncPluginPag
g_object_set_data(G_OBJECT(dialog), DELETE_DIALOG_SA_TRANS,
GTK_WIDGET(gtk_builder_get_object (builder, "subaccount_trans")));
// Does the selected account have sub accounts
if (gnc_account_n_children(account) > 1) {
// We don't delete accounts with more than 1 subaccount.
gchar* message = g_strdup_printf("Account '%s' has more than one subaccount, move subaccounts or delete them before attempting to delete this account.", acct_name);
dialog = gtk_message_dialog_new (GTK_WINDOW(window),
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_OK,
"%s", message);
gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (dialog);
g_free (message);
g_list_free(filter);
g_free(acct_name);
return;
}
if (gnc_account_n_children(account) > 0)
{
// Check for RO txns in descendants
@ -1684,7 +1526,6 @@ gnc_plugin_page_account_tree_cmd_delete_account (GtkAction *action, GncPluginPag
/* Account deletion is cancelled, so clean up and return. */
gtk_widget_destroy(dialog);
g_list_free(filter);
g_free(acct_name);
return;
}
if (trans_mas && gtk_widget_is_sensitive(trans_mas))
@ -1726,8 +1567,148 @@ gnc_plugin_page_account_tree_cmd_delete_account (GtkAction *action, GncPluginPag
}
gtk_widget_destroy(dialog);
g_list_free(filter);
} /* (NULL != splits) || (NULL != children) */
gnc_plugin_page_account_tree_cmd_delete_account_int (action, page, ta, sta, saa, acct_name, delete_res);
delete_account_next (action, page, ta, sta, saa, delete_res);
}
static void
delete_account_next (GtkAction *action, GncPluginPageAccountTree *page, Account* ta,
Account* sta, Account* saa, delete_helper_t delete_res)
{
Account *account = gnc_plugin_page_account_tree_get_current_account (page);
GList* splits = xaccAccountGetSplitList(account);
GtkWidget* window = gnc_plugin_page_get_window(GNC_PLUGIN_PAGE(page));
gint response;
const char *format = _("The account %s will be deleted.");
char *lines[8];
char *message;
char *name;
int i = 0;
GtkWidget *dialog;
gchar* acct_name = gnc_account_get_full_name(account);
lines[0] = g_strdup_printf(format, acct_name);
g_free(acct_name);
if (splits)
{
if (ta)
{
lines[++i] = g_strdup_printf(_("All transactions in this account will be moved to "
"the account %s."), gnc_account_get_full_name(ta));
}
else
{
lines[++i] = g_strdup_printf("%s", _("All transactions in this account will be deleted."));
}
}
if (gnc_account_n_children(account) > 0)
{
if (saa)
{
lines[++i] = g_strdup_printf(_("All of its sub-accounts will be moved to "
"the account %s."), gnc_account_get_full_name(saa));
}
else
{
lines[++i] = g_strdup_printf("%s", _("All of its subaccounts will be deleted."));
if (sta)
{
lines[++i] = g_strdup_printf(_("All sub-account transactions will be moved to "
"the account %s."), gnc_account_get_full_name(sta));
}
else if (delete_res.has_splits)
{
lines[++i] = g_strdup_printf("%s", _("All sub-account transactions will be deleted."));
}
}
}
lines[++i] = _("Are you sure you want to do this?");
lines[i] = NULL;
i--; /* Don't try to free the constant question. */
message = g_strjoinv(" ", lines);
while (i--)
{
g_free(lines[i]);
}
dialog = gtk_message_dialog_new(GTK_WINDOW(window),
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_QUESTION,
GTK_BUTTONS_NONE,
"%s", message);
g_free(message);
gtk_dialog_add_buttons(GTK_DIALOG(dialog),
_("_Cancel"), GTK_RESPONSE_CANCEL,
_("_Delete"), GTK_RESPONSE_ACCEPT,
(gchar *)NULL);
gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_CANCEL);
response = gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
if (response == GTK_RESPONSE_ACCEPT)
delete_account_final (account, saa, sta, ta);
}
void delete_account_final (Account* account, Account* saa, Account* sta, Account* ta)
{
GList *acct_list, *ptr;
const GncGUID *guid;
gchar guidstr[GUID_ENCODING_LENGTH+1];
gnc_set_busy_cursor(NULL, TRUE);
gnc_suspend_gui_refresh ();
/* Move subaccounts and transactions if this was requested */
xaccAccountBeginEdit (account);
if (saa)
{
xaccAccountBeginEdit (saa);
acct_list = gnc_account_get_children(account);
for (ptr = acct_list; ptr; ptr = g_list_next(ptr))
gnc_account_append_child (saa, ptr->data);
g_list_free(acct_list);
xaccAccountCommitEdit (saa);
}
else if (sta)
{
/* Move the splits of its subaccounts, if any. */
gnc_account_foreach_descendant(account,
(AccountCb)xaccAccountMoveAllSplits,
sta);
}
if (ta)
{
/* Move the splits of the account to be deleted. */
xaccAccountMoveAllSplits (account, ta);
}
xaccAccountCommitEdit (account);
/* Drop all references from the state file for
* any subaccount the account still has
*/
acct_list = gnc_account_get_children(account);
for (ptr = acct_list; ptr; ptr = g_list_next(ptr))
{
guid = xaccAccountGetGUID (ptr->data);
guid_to_string_buff (guid, guidstr);
gnc_state_drop_sections_for (guidstr);
}
g_list_free(acct_list);
/* Drop all references from the state file for this account
*/
guid = xaccAccountGetGUID (account);
guid_to_string_buff (guid, guidstr);
gnc_state_drop_sections_for (guidstr);
/*
* Finally, delete the account, any subaccounts it may still
* have, and any splits it or its subaccounts may still have.
*/
xaccAccountBeginEdit (account);
xaccAccountDestroy (account);
gnc_resume_gui_refresh ();
gnc_unset_busy_cursor(NULL);
}
static void