Skip to content

Commit

Permalink
[dialog-commodities.cpp] when deleting commodity fails, show accounts…
Browse files Browse the repository at this point in the history
… with commodity
  • Loading branch information
christopherlam committed Sep 30, 2024
1 parent 78ef5be commit adddeae
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions gnucash/gnome/dialog-commodities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@
#include "gnc-gnome-utils.h"
#include "gnc-session.h"
#include "gnc-warnings.h"
#include "Account.hpp"

#include <sstream>
#include <vector>

#define DIALOG_COMMODITIES_CM_CLASS "dialog-commodities"
#define STATE_SECTION "dialogs/edit_commodities"
Expand Down Expand Up @@ -155,8 +158,6 @@ gnc_commodities_dialog_remove_clicked (GtkWidget *widget, gpointer data)
GNCPriceDB *pdb;
GList *node;
GList *prices;
GList *accounts;
gboolean can_delete;
gnc_commodity *commodity;
GtkWidget *dialog;
const gchar *message, *warning;
Expand All @@ -166,33 +167,33 @@ gnc_commodities_dialog_remove_clicked (GtkWidget *widget, gpointer data)
if (commodity == NULL)
return;

accounts = gnc_account_get_descendants (gnc_book_get_root_account(cd->book));
can_delete = TRUE;
std::vector<Account*> commodity_accounts;

for (node = accounts; node; node = node->next)
{
Account *account = GNC_ACCOUNT(node->data);

if (commodity == xaccAccountGetCommodity (account))
{
can_delete = FALSE;
break;
}
}
gnc_account_foreach_descendant (gnc_book_get_root_account(cd->book),
[commodity, &commodity_accounts](auto acct)
{
if (commodity == xaccAccountGetCommodity (acct))
commodity_accounts.push_back (acct);
});

/* FIXME check for transaction references */

if (!can_delete)
if (!commodity_accounts.empty())
{
const char *message = _("That commodity is currently used by "
"at least one of your accounts. You may "
"not delete it.");
std::ostringstream ss;
ss << _("This commodity is currently used by the following accounts. You may "
"not delete it.\n");

for (const auto acct : commodity_accounts)
{
auto full_name = gnc_account_get_full_name (acct);
ss << "\n* " << full_name;
g_free (full_name);
}

gnc_warning_dialog (GTK_WINDOW (cd->window), "%s", message);
g_list_free (accounts);
gnc_warning_dialog (GTK_WINDOW (cd->window), "%s", ss.str().c_str());
return;
}
g_list_free (accounts);

pdb = gnc_pricedb_get_db (cd->book);
prices = gnc_pricedb_get_prices (pdb, commodity, NULL);
Expand Down

0 comments on commit adddeae

Please sign in to comment.