[dialog-commodities.cpp] when deleting commodity fails, show accounts with commodity

This commit is contained in:
Christopher Lam 2024-09-28 23:26:56 +08:00
parent 78ef5be7b2
commit 67919fa1df

View File

@ -39,7 +39,10 @@
#include "gnc-gnome-utils.h" #include "gnc-gnome-utils.h"
#include "gnc-session.h" #include "gnc-session.h"
#include "gnc-warnings.h" #include "gnc-warnings.h"
#include "Account.hpp"
#include <vector>
#include <string>
#define DIALOG_COMMODITIES_CM_CLASS "dialog-commodities" #define DIALOG_COMMODITIES_CM_CLASS "dialog-commodities"
#define STATE_SECTION "dialogs/edit_commodities" #define STATE_SECTION "dialogs/edit_commodities"
@ -155,8 +158,6 @@ gnc_commodities_dialog_remove_clicked (GtkWidget *widget, gpointer data)
GNCPriceDB *pdb; GNCPriceDB *pdb;
GList *node; GList *node;
GList *prices; GList *prices;
GList *accounts;
gboolean can_delete;
gnc_commodity *commodity; gnc_commodity *commodity;
GtkWidget *dialog; GtkWidget *dialog;
const gchar *message, *warning; const gchar *message, *warning;
@ -166,33 +167,32 @@ gnc_commodities_dialog_remove_clicked (GtkWidget *widget, gpointer data)
if (commodity == NULL) if (commodity == NULL)
return; return;
accounts = gnc_account_get_descendants (gnc_book_get_root_account(cd->book)); std::vector<Account*> commodity_accounts;
can_delete = TRUE;
for (node = accounts; node; node = node->next) gnc_account_foreach_descendant (gnc_book_get_root_account(cd->book),
{ [commodity, &commodity_accounts](auto acct)
Account *account = GNC_ACCOUNT(node->data); {
if (commodity == xaccAccountGetCommodity (acct))
if (commodity == xaccAccountGetCommodity (account)) commodity_accounts.push_back (acct);
{ });
can_delete = FALSE;
break;
}
}
/* FIXME check for transaction references */ /* FIXME check for transaction references */
if (!can_delete) if (!commodity_accounts.empty())
{ {
const char *message = _("That commodity is currently used by " std::string msg{_("This commodity is currently used by the following accounts. You may "
"at least one of your accounts. You may " "not delete it.\n")};
"not delete it.");
gnc_warning_dialog (GTK_WINDOW (cd->window), "%s", message); for (const auto acct : commodity_accounts)
g_list_free (accounts); {
auto full_name = gnc_account_get_full_name (acct);
msg.append ("\n* ").append (full_name);
g_free (full_name);
}
gnc_warning_dialog (GTK_WINDOW (cd->window), "%s", msg.c_str());
return; return;
} }
g_list_free (accounts);
pdb = gnc_pricedb_get_db (cd->book); pdb = gnc_pricedb_get_db (cd->book);
prices = gnc_pricedb_get_prices (pdb, commodity, NULL); prices = gnc_pricedb_get_prices (pdb, commodity, NULL);