gnucash maint: Multiple changes pushed

John Ralls jralls at code.gnucash.org
Wed Jan 27 20:44:32 EST 2021


Updated	 via  https://github.com/Gnucash/gnucash/commit/0f8d9f6d (commit)
	 via  https://github.com/Gnucash/gnucash/commit/bcbebe62 (commit)
	from  https://github.com/Gnucash/gnucash/commit/80180c2a (commit)



commit 0f8d9f6d7cadfcc40cec4efa5d6f370a739ae817
Author: John Ralls <jralls at ceridwen.us>
Date:   Tue Jan 26 16:16:25 2021 -0800

    Bug 798093 - Changing the symbol/abbreviation of a security after...
    
    the trading account was created breaks GnuCash.
    
    Introduces a new function, gnc_account_lookup_by_type_and_commodity
    that does that, though it also looks at name for the one special case
    of finding/creating the Namespace placeholder account. Adds a parameter
    checkname to xaccScrubUtilityGetOrMakeAccount to flag whether to look
    for the name.
    
    Namespaces aside this makes it possible for the user to rename trading
    accounts or securities independent of each other.

diff --git a/libgnucash/engine/Account.cpp b/libgnucash/engine/Account.cpp
index 090e7d837..718f5deba 100644
--- a/libgnucash/engine/Account.cpp
+++ b/libgnucash/engine/Account.cpp
@@ -3120,6 +3120,28 @@ gnc_account_lookup_by_full_name (const Account *any_acc,
     return found;
 }
 
+Account*
+gnc_account_lookup_by_type_and_commodity (Account* root,
+                                          const char* name,
+                                          GNCAccountType acctype,
+                                          gnc_commodity* commodity)
+{
+    auto rpriv{GET_PRIVATE(root)};
+    for (auto node = rpriv->children; node; node = node->next)
+    {
+        auto account{static_cast<Account*>(node->data)};
+        if (xaccAccountGetType (account) == acctype &&
+            gnc_commodity_equiv(xaccAccountGetCommodity (account), commodity))
+        {
+            if (name && strcmp(name, xaccAccountGetName(account)))
+                continue; //name doesn't match so skip this one
+
+            return account;
+        }
+    }
+    return nullptr;
+}
+
 void
 gnc_account_foreach_child (const Account *acc,
                            AccountCb thunk,
diff --git a/libgnucash/engine/Account.h b/libgnucash/engine/Account.h
index 78eff7244..22c02f076 100644
--- a/libgnucash/engine/Account.h
+++ b/libgnucash/engine/Account.h
@@ -938,6 +938,24 @@ Account *gnc_account_lookup_by_code (const Account *parent,
  */
 Account *gnc_account_lookup_by_opening_balance (Account *account, gnc_commodity *commodity);
 
+/** Find a direct child account matching name, GNCAccountType, and commodity.
+ *
+ * Note that commodity matching is by equivalence: If the
+ * mnemonic/symbol and namespace are the same, it matches.
+ *
+ *  @param root The account among whose children one expects to find
+ *  the account.
+ *  @param name The name of the account to look for. If nullptr the
+ *  returned account will match only on acctype and commodity.
+ *  @param acctype The GNCAccountType to match.
+ *  @param commodity The commodity in which the account should be denominated.
+ *  @return The book's trading account for the given commodity if
+ *  trading accounts are enabled and one exists; NULL otherwise.
+ */
+Account *gnc_account_lookup_by_type_and_commodity (Account* root,
+                                                   const char* name,
+                                                   GNCAccountType acctype,
+                                                   gnc_commodity* commodity);
 /** @} */
 
 /* ------------------ */
diff --git a/libgnucash/engine/Scrub.c b/libgnucash/engine/Scrub.c
index 3965ab1b0..0c77f9dd5 100644
--- a/libgnucash/engine/Scrub.c
+++ b/libgnucash/engine/Scrub.c
@@ -64,7 +64,8 @@ static Account* xaccScrubUtilityGetOrMakeAccount (Account *root,
                                                   gnc_commodity* currency,
                                                   const char* accname,
                                                   GNCAccountType acctype,
-                                                  gboolean placeholder);
+                                                  gboolean placeholder,
+                                                  gboolean checkname);
 
 void
 gnc_set_abort_scrub (gboolean abort)
@@ -124,7 +125,8 @@ TransScrubOrphansFast (Transaction *trans, Account *root)
                                gnc_commodity_get_mnemonic (trans->common_currency),
                                NULL);
         orph = xaccScrubUtilityGetOrMakeAccount (root, trans->common_currency,
-                                                 accname, ACCT_TYPE_BANK, FALSE);
+                                                 accname, ACCT_TYPE_BANK,
+                                                 FALSE, TRUE);
         g_free (accname);
         if (!orph) continue;
 
@@ -413,7 +415,8 @@ get_balance_split (Transaction *trans, Account *root, Account *account,
         accname = g_strconcat (_("Imbalance"), "-",
                                gnc_commodity_get_mnemonic (commodity), NULL);
         account = xaccScrubUtilityGetOrMakeAccount (root, commodity,
-                                                    accname, ACCT_TYPE_BANK, FALSE);
+                                                    accname, ACCT_TYPE_BANK,
+                                                    FALSE, TRUE);
         g_free (accname);
         if (!account)
         {
@@ -476,7 +479,8 @@ get_trading_split (Transaction *trans, Account *root,
     trading_account = xaccScrubUtilityGetOrMakeAccount (root,
                                                         default_currency,
                                                         _("Trading"),
-                                                        ACCT_TYPE_TRADING, TRUE);
+                                                        ACCT_TYPE_TRADING,
+                                                        TRUE, FALSE);
     if (!trading_account)
     {
         PERR ("Can't get trading account");
@@ -486,7 +490,8 @@ get_trading_split (Transaction *trans, Account *root,
     ns_account = xaccScrubUtilityGetOrMakeAccount (trading_account,
                                                    default_currency,
                                                    gnc_commodity_get_namespace(commodity),
-                                                   ACCT_TYPE_TRADING, TRUE);
+                                                   ACCT_TYPE_TRADING,
+                                                   TRUE, TRUE);
     if (!ns_account)
     {
         PERR ("Can't get namespace account");
@@ -495,7 +500,8 @@ get_trading_split (Transaction *trans, Account *root,
 
     account = xaccScrubUtilityGetOrMakeAccount (ns_account, commodity,
                                                 gnc_commodity_get_mnemonic(commodity),
-                                                ACCT_TYPE_TRADING, FALSE);
+                                                ACCT_TYPE_TRADING,
+                                                FALSE, FALSE);
     if (!account)
     {
         PERR ("Can't get commodity account");
@@ -1447,7 +1453,7 @@ xaccAccountScrubColorNotSet (QofBook *book)
 Account *
 xaccScrubUtilityGetOrMakeAccount (Account *root, gnc_commodity * currency,
                                   const char *accname, GNCAccountType acctype,
-                                  gboolean placeholder)
+                                  gboolean placeholder, gboolean checkname)
 {
     Account * acc;
 
@@ -1461,7 +1467,9 @@ xaccScrubUtilityGetOrMakeAccount (Account *root, gnc_commodity * currency,
     }
 
     /* See if we've got one of these going already ... */
-    acc = gnc_account_lookup_by_name(root, accname);
+    acc = gnc_account_lookup_by_type_and_commodity (root,
+                                                    checkname ? accname : NULL,
+                                                    acctype, currency);
 
     if (acc == NULL)
     {

commit bcbebe62b1c297cac95db75b1261638e97d82b78
Author: John Ralls <jralls at ceridwen.us>
Date:   Tue Jan 26 14:56:29 2021 -0800

    Make xaccScrubUtilityGetOrMakeAccount static and remove ScrubP.h
    
    It being the only declaration in ScrubP.h

diff --git a/libgnucash/engine/CMakeLists.txt b/libgnucash/engine/CMakeLists.txt
index 14a242033..d860bd436 100644
--- a/libgnucash/engine/CMakeLists.txt
+++ b/libgnucash/engine/CMakeLists.txt
@@ -6,7 +6,6 @@ add_subdirectory(mocks)
 
 set(engine_noinst_HEADERS
   AccountP.h
-  ScrubP.h
   SplitP.h
   SX-book.h
   SX-ttinfo.h
diff --git a/libgnucash/engine/Scrub.c b/libgnucash/engine/Scrub.c
index d5007cac1..3965ab1b0 100644
--- a/libgnucash/engine/Scrub.c
+++ b/libgnucash/engine/Scrub.c
@@ -47,7 +47,6 @@
 #include "Account.h"
 #include "AccountP.h"
 #include "Scrub.h"
-#include "ScrubP.h"
 #include "Transaction.h"
 #include "TransactionP.h"
 #include "gnc-commodity.h"
@@ -60,6 +59,13 @@ static QofLogModule log_module = G_LOG_DOMAIN;
 static gboolean abort_now = FALSE;
 static gint scrub_depth = 0;
 
+
+static Account* xaccScrubUtilityGetOrMakeAccount (Account *root,
+                                                  gnc_commodity* currency,
+                                                  const char* accname,
+                                                  GNCAccountType acctype,
+                                                  gboolean placeholder);
+
 void
 gnc_set_abort_scrub (gboolean abort)
 {
diff --git a/libgnucash/engine/Scrub2.c b/libgnucash/engine/Scrub2.c
index 0aaf266a8..fd49f6d7b 100644
--- a/libgnucash/engine/Scrub2.c
+++ b/libgnucash/engine/Scrub2.c
@@ -40,7 +40,6 @@
 #include "Transaction.h"
 #include "TransactionP.h"
 #include "Scrub2.h"
-#include "ScrubP.h"
 #include "cap-gains.h"
 #include "gnc-engine.h"
 #include "gncInvoice.h"
diff --git a/libgnucash/engine/ScrubP.h b/libgnucash/engine/ScrubP.h
deleted file mode 100644
index b8818643e..000000000
--- a/libgnucash/engine/ScrubP.h
+++ /dev/null
@@ -1,41 +0,0 @@
-/********************************************************************\
- * This program is free software; you can redistribute it and/or    *
- * modify it under the terms of the GNU General Public License as   *
- * published by the Free Software Foundation; either version 2 of   *
- * the License, or (at your option) any later version.              *
- *                                                                  *
- * This program is distributed in the hope that it will be useful,  *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
- * GNU General Public License for more details.                     *
- *                                                                  *
- * You should have received a copy of the GNU General Public License*
- * along with this program; if not, contact:                        *
- *                                                                  *
- * Free Software Foundation           Voice:  +1-617-542-5942       *
- * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
- * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
-\********************************************************************/
-
-/** @file ScrubP.h
- *
- * This is the *private* header for the scrub routines.
- * No one outside of the engine should ever include this file.
- *
- * Copyright (C) 2003, Linas Vepstas <linas at linas.org>
- */
-
-#ifndef XACC_SCRUB_P_H
-#define XACC_SCRUB_P_H
-
-#include "Account.h"
-#include "gnc-commodity.h"
-#include "gnc-engine.h"
-
-/* Utility to make account by name.  Not for public use. */
-Account * xaccScrubUtilityGetOrMakeAccount (Account *root,
-        gnc_commodity * currency, const char *accname,
-        GNCAccountType acctype, gboolean placeholder);
-
-
-#endif /* XACC_SCRUB_P_H */



Summary of changes:
 libgnucash/engine/Account.cpp    | 22 +++++++++++++++++++++
 libgnucash/engine/Account.h      | 18 ++++++++++++++++++
 libgnucash/engine/CMakeLists.txt |  1 -
 libgnucash/engine/Scrub.c        | 30 +++++++++++++++++++++--------
 libgnucash/engine/Scrub2.c       |  1 -
 libgnucash/engine/ScrubP.h       | 41 ----------------------------------------
 6 files changed, 62 insertions(+), 51 deletions(-)
 delete mode 100644 libgnucash/engine/ScrubP.h



More information about the gnucash-changes mailing list