From f70ee754fc635753ac9769dbf6c4fff6974118ca Mon Sep 17 00:00:00 2001 From: Christopher Lam Date: Sun, 14 Apr 2024 10:35:04 +0800 Subject: [PATCH] use xaccAccountGetSplits and gnc_account_find_split --- gnucash/gnome-utils/dialog-transfer.cpp | 5 +-- gnucash/gnome/assistant-stock-transaction.cpp | 10 ++--- .../gnome/gnc-plugin-page-account-tree.cpp | 19 ++++----- .../gtest-assistant-stock-transaction.cpp | 3 +- gnucash/gnome/window-reconcile.cpp | 30 ++++++-------- gnucash/import-export/import-backend.cpp | 5 ++- gnucash/import-export/import-main-matcher.cpp | 4 +- libgnucash/engine/Scrub.cpp | 10 ++--- libgnucash/engine/Scrub2.cpp | 8 +--- libgnucash/engine/cap-gains.cpp | 6 +-- libgnucash/engine/policy.cpp | 41 +++++-------------- .../engine/test-core/test-engine-stuff.cpp | 1 - 12 files changed, 52 insertions(+), 90 deletions(-) diff --git a/gnucash/gnome-utils/dialog-transfer.cpp b/gnucash/gnome-utils/dialog-transfer.cpp index f1d3acf048..8ed87051bf 100644 --- a/gnucash/gnome-utils/dialog-transfer.cpp +++ b/gnucash/gnome-utils/dialog-transfer.cpp @@ -44,6 +44,7 @@ #include "gnc-ui.h" #include "Transaction.h" #include "Account.h" +#include "Account.hpp" #include "engine-helpers.h" #include "QuickFill.h" #include @@ -472,10 +473,8 @@ gnc_xfer_dialog_reload_quickfill( XferDialog *xferData ) gnc_quickfill_destroy( xferData->qf ); xferData->qf = gnc_quickfill_new(); - auto splitlist = xaccAccountGetSplitList( account ); - for ( GList *node = splitlist; node; node = node->next ) + for (auto split : xaccAccountGetSplits (account)) { - auto split = static_cast (node->data); auto trans = xaccSplitGetParent (split); gnc_quickfill_insert( xferData->qf, xaccTransGetDescription (trans), QUICKFILL_LIFO); diff --git a/gnucash/gnome/assistant-stock-transaction.cpp b/gnucash/gnome/assistant-stock-transaction.cpp index 1b26ac18ab..5f7de1d349 100644 --- a/gnucash/gnome/assistant-stock-transaction.cpp +++ b/gnucash/gnome/assistant-stock-transaction.cpp @@ -37,6 +37,7 @@ #include #include "Account.h" +#include "Account.hpp" #include "Transaction.h" #include "engine-helpers.h" #include "dialog-utils.h" @@ -1262,9 +1263,8 @@ StockAssistantModel::set_txn_type (guint type_idx) }; static void -check_txn_date(GList* last_split_node, time64 txn_date, Logger& logger) +check_txn_date(Split* last_split, time64 txn_date, Logger& logger) { - auto last_split = static_cast(last_split_node->data); auto last_split_date = xaccTransGetDate(xaccSplitGetParent(last_split)); if (txn_date <= last_split_date) { auto last_split_date_str = qof_print_date(last_split_date); @@ -1301,9 +1301,9 @@ StockAssistantModel::generate_list_of_splits() { // transactions dated after the date specified, it is very likely // the later stock transactions will be invalidated. warn the user // to review them. - auto last_split_node = g_list_last (xaccAccountGetSplitList (m_acct)); - if (last_split_node) - check_txn_date(last_split_node, m_transaction_date, m_logger); + auto splits{xaccAccountGetSplits (m_acct)}; + if (!splits.empty()) + check_txn_date(splits.back(), m_transaction_date, m_logger); if (m_stock_entry->enabled() || m_stock_entry->has_amount()) { diff --git a/gnucash/gnome/gnc-plugin-page-account-tree.cpp b/gnucash/gnome/gnc-plugin-page-account-tree.cpp index ffb3bc2f15..15f764104b 100644 --- a/gnucash/gnome/gnc-plugin-page-account-tree.cpp +++ b/gnucash/gnome/gnc-plugin-page-account-tree.cpp @@ -39,6 +39,7 @@ #include "gnc-plugin-page-account-tree.h" #include "gnc-plugin-page-register.h" +#include "Account.hpp" #include "Scrub.h" #include "Scrub3.h" #include "ScrubBusiness.h" @@ -1145,22 +1146,19 @@ static gpointer delete_account_helper (Account * account, gpointer data) { auto helper_res = static_cast(data); - GList *splits; + auto splits{xaccAccountGetSplits (account)}; - splits = xaccAccountGetSplitList (account); - if (splits) + if (!splits.empty()) { helper_res->has_splits = TRUE; - while (splits) + for (auto s : splits) { - auto s = GNC_SPLIT(splits->data); Transaction *txn = xaccSplitGetParent (s); if (xaccTransGetReadOnly (txn)) { helper_res->has_ro_splits = TRUE; break; } - splits = splits->next; } } @@ -1383,7 +1381,6 @@ account_delete_dialog (Account *account, GtkWindow *parent, Adopters* adopt) gchar *title = NULL; GtkBuilder *builder = gtk_builder_new(); gchar *acct_name = gnc_account_get_full_name(account); - GList* splits = xaccAccountGetSplitList(account); GList* filter = g_list_prepend(NULL, (gpointer)xaccAccountGetType(account)); if (!acct_name) @@ -1416,7 +1413,7 @@ account_delete_dialog (Account *account, GtkWindow *parent, Adopters* adopt) account, FALSE); // Does the selected account have splits - if (splits) + if (!xaccAccountGetSplits(account).empty()) { delete_helper_t delete_res2 = { FALSE, FALSE }; @@ -1537,8 +1534,7 @@ gnc_plugin_page_account_tree_cmd_delete_account (GSimpleAction *simple, } // If no transaction or children just delete it. - if (!(xaccAccountGetSplitList (account) != NULL || - gnc_account_n_children (account))) + if (xaccAccountGetSplits (account).empty() && gnc_account_n_children (account) == 0) { do_delete_account (account, NULL, NULL, NULL); return; @@ -1581,7 +1577,6 @@ confirm_delete_account (GSimpleAction *simple, GncPluginPageAccountTree *page, 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; @@ -1595,7 +1590,7 @@ confirm_delete_account (GSimpleAction *simple, GncPluginPageAccountTree *page, acct_name); g_free(acct_name); - if (splits) + if (!xaccAccountGetSplits (account).empty()) { if (ta) { diff --git a/gnucash/gnome/test/gtest-assistant-stock-transaction.cpp b/gnucash/gnome/test/gtest-assistant-stock-transaction.cpp index f328a324d1..3758ccba32 100644 --- a/gnucash/gnome/test/gtest-assistant-stock-transaction.cpp +++ b/gnucash/gnome/test/gtest-assistant-stock-transaction.cpp @@ -231,9 +231,8 @@ static void dump_acct (Account *acct) std::cout << '\n' << std::setw(20) << std::right << xaccAccountGetName (acct) << " Bal=" << std::setw(10) << std::right << GncNumeric (xaccAccountGetBalance (acct)) << std::endl; - for (auto n = xaccAccountGetSplitList (acct); n; n = n->next) + for (auto s : xaccAccountGetSplits (acct)) { - auto s = static_cast(n->data); bal += xaccSplitGetAmount (s); std::cout << std::setw(20) << std::right << GncDateTime (xaccTransGetDate (xaccSplitGetParent (s))).format_iso8601() << " amt=" << std::setw(10) << std::right << GncNumeric (xaccSplitGetAmount (s)) diff --git a/gnucash/gnome/window-reconcile.cpp b/gnucash/gnome/window-reconcile.cpp index 1d352f05d8..8b4a96b0d7 100644 --- a/gnucash/gnome/window-reconcile.cpp +++ b/gnucash/gnome/window-reconcile.cpp @@ -37,6 +37,7 @@ #endif #include +#include "Account.hpp" #include "Scrub.h" #include "Scrub3.h" #include "dialog-account.h" @@ -1621,10 +1622,8 @@ recn_set_watches_one_account (gpointer data, gpointer user_data) QOF_EVENT_MODIFY | QOF_EVENT_DESTROY); /* add a watch on each unreconciled or cleared split for the account */ - GList *splits = xaccAccountGetSplitList (account); - for (GList *node = splits; node; node = node->next) + for (auto split : xaccAccountGetSplits (account)) { - auto split = GNC_SPLIT(node->data); Transaction *trans; char recn; @@ -1942,22 +1941,16 @@ recnWindowWithBalance (GtkWidget *parent, Account *account, gnc_numeric new_endi GtkWidget *box = gtk_statusbar_get_message_area (bar); GtkWidget *image = gtk_image_new_from_icon_name ("dialog-warning", GTK_ICON_SIZE_SMALL_TOOLBAR); - GList *splits = xaccAccountGetSplitList (account); - for (GList *n = splits; n; n = n->next) - { - auto split = GNC_SPLIT(n->data); - time64 recn_date = xaccSplitGetDateReconciled (split); - gchar *datestr, *recnstr; - if ((xaccSplitGetReconcile (split) != YREC) || - (recn_date <= statement_date)) - continue; + auto find_split = [statement_date](const Split *split) + { return (xaccSplitGetReconcile (split) == YREC && + xaccSplitGetDateReconciled (split) > statement_date); }; - datestr = qof_print_date (xaccTransGetDate (xaccSplitGetParent (split))); - recnstr = qof_print_date (recn_date); + if (auto split = gnc_account_find_split (account, find_split, true)) + { + auto datestr = qof_print_date (xaccTransGetDate (xaccSplitGetParent (split))); + auto recnstr = qof_print_date (xaccSplitGetDateReconciled (split)); PWARN ("split posting_date=%s, recn_date=%s", datestr, recnstr); - g_free (datestr); - g_free (recnstr); gtk_statusbar_push (bar, context, _("WARNING! Account contains \ splits whose reconcile date is after statement date. Reconciliation may be \ @@ -1970,7 +1963,9 @@ use Find Transactions to find them, unreconcile, and re-reconcile.")); gtk_box_pack_start (GTK_BOX(box), image, FALSE, FALSE, 0); gtk_box_reorder_child (GTK_BOX(box), image, 0); - break; + + g_free (datestr); + g_free (recnstr); } } @@ -2305,6 +2300,7 @@ find_payment_account(Account *account) } } + g_list_free (list); return rv; } diff --git a/gnucash/import-export/import-backend.cpp b/gnucash/import-export/import-backend.cpp index 5fbf9a082a..d333d7bfbb 100644 --- a/gnucash/import-export/import-backend.cpp +++ b/gnucash/import-export/import-backend.cpp @@ -38,6 +38,7 @@ #include "import-backend.h" #include "import-utilities.h" #include "Account.h" +#include "Account.hpp" #include "Query.h" #include "gnc-engine.h" #include "engine-helpers.h" @@ -996,9 +997,9 @@ hash_account_online_ids (Account *account) { auto acct_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, nullptr); - for (GList *n = xaccAccountGetSplitList (account) ; n; n = n->next) + for (auto split : xaccAccountGetSplits (account)) { - auto id = gnc_import_get_split_online_id (static_cast(n->data)); + auto id = gnc_import_get_split_online_id (split); if (id && *id) g_hash_table_insert (acct_hash, (void*) id, GINT_TO_POINTER (1)); } diff --git a/gnucash/import-export/import-main-matcher.cpp b/gnucash/import-export/import-main-matcher.cpp index 8f7c18b751..5acc3ba474 100644 --- a/gnucash/import-export/import-main-matcher.cpp +++ b/gnucash/import-export/import-main-matcher.cpp @@ -44,6 +44,7 @@ #include "import-main-matcher.h" +#include "Account.hpp" #include "dialog-transfer.h" #include "dialog-utils.h" #include "gnc-glib-utils.h" @@ -468,9 +469,8 @@ load_hash_tables (GNCImportMainMatcher *info) } for (GList *m = accounts_list; m; m = m->next) { - for (GList *n = xaccAccountGetSplitList (static_cast(m->data)); n; n = n->next) + for (auto s : xaccAccountGetSplits (static_cast(m->data))) { - auto s = static_cast(n->data); const Transaction *t = xaccSplitGetParent (s); const gchar *key = xaccTransGetDescription (t); diff --git a/libgnucash/engine/Scrub.cpp b/libgnucash/engine/Scrub.cpp index 0530325dfc..f468f13f76 100644 --- a/libgnucash/engine/Scrub.cpp +++ b/libgnucash/engine/Scrub.cpp @@ -47,6 +47,7 @@ #include "Account.h" #include "AccountP.hpp" +#include "Account.hpp" #include "Scrub.h" #include "Transaction.h" #include "TransactionP.h" @@ -91,8 +92,8 @@ gnc_get_ongoing_scrub (void) static void add_transactions (const Account *account, GHashTable **ht) { - for (GList *m = xaccAccountGetSplitList (account); m; m = g_list_next (m)) - g_hash_table_add (*ht, xaccSplitGetParent (GNC_SPLIT(m->data))); + for (auto s : xaccAccountGetSplits (account)) + g_hash_table_add (*ht, xaccSplitGetParent (s)); } static GList* @@ -226,12 +227,11 @@ xaccAccountTreeScrubSplits (Account *account) void xaccAccountScrubSplits (Account *account) { - GList *node; scrub_depth++; - for (node = xaccAccountGetSplitList (account); node; node = node->next) + for (auto s : xaccAccountGetSplits (account)) { if (abort_now) break; - xaccSplitScrub (GNC_SPLIT(node->data)); + xaccSplitScrub (s); } scrub_depth--; } diff --git a/libgnucash/engine/Scrub2.cpp b/libgnucash/engine/Scrub2.cpp index 6743895177..265492f738 100644 --- a/libgnucash/engine/Scrub2.cpp +++ b/libgnucash/engine/Scrub2.cpp @@ -37,6 +37,7 @@ #include "qof.h" #include "Account.h" #include "AccountP.hpp" +#include "Account.hpp" #include "Transaction.h" #include "TransactionP.h" #include "Scrub2.h" @@ -57,19 +58,14 @@ static QofLogModule log_module = GNC_MOD_LOT; void xaccAccountAssignLots (Account *acc) { - SplitList *splits, *node; - if (!acc) return; ENTER ("acc=%s", xaccAccountGetName(acc)); xaccAccountBeginEdit (acc); restart_loop: - splits = xaccAccountGetSplitList(acc); - for (node = splits; node; node = node->next) + for (auto split : xaccAccountGetSplits (acc)) { - Split * split = GNC_SPLIT(node->data); - /* If already in lot, then no-op */ if (split->lot) continue; diff --git a/libgnucash/engine/cap-gains.cpp b/libgnucash/engine/cap-gains.cpp index 0754005fc7..3c004c552b 100644 --- a/libgnucash/engine/cap-gains.cpp +++ b/libgnucash/engine/cap-gains.cpp @@ -58,6 +58,7 @@ ToDo: #include #include +#include "Account.hpp" #include "AccountP.hpp" #include "Scrub2.h" #include "Scrub3.h" @@ -79,7 +80,6 @@ gboolean xaccAccountHasTrades (const Account *acc) { gnc_commodity *acc_comm; - SplitList *splits, *node; if (!acc) return FALSE; @@ -88,10 +88,8 @@ xaccAccountHasTrades (const Account *acc) acc_comm = xaccAccountGetCommodity(acc); - splits = xaccAccountGetSplitList(acc); - for (node = splits; node; node = node->next) + for (auto s : xaccAccountGetSplits (acc)) { - Split *s = GNC_SPLIT(node->data); Transaction *t = s->parent; if (s->gains == GAINS_STATUS_GAINS) continue; if (acc_comm != t->common_currency) return TRUE; diff --git a/libgnucash/engine/policy.cpp b/libgnucash/engine/policy.cpp index ce89264219..35febca6f4 100644 --- a/libgnucash/engine/policy.cpp +++ b/libgnucash/engine/policy.cpp @@ -34,6 +34,7 @@ #include #include "Account.h" +#include "Account.hpp" #include "Transaction.h" #include "TransactionP.h" #include "cap-gains.h" @@ -100,38 +101,16 @@ DirectionPolicyGetSplit (GNCPolicy *pcy, GNCLot *lot, short reverse) * hasn't been assigned to a lot. Return that split. * Make use of the fact that the splits in an account are * already in date order; so we don't have to sort. */ - auto splits = xaccAccountGetSplitList (lot_account); - - Split *rv = nullptr; - - for (auto node = reverse ? g_list_last (splits) : splits; !rv && node; - node = reverse ? node->prev : node->next) + auto find_split = [open_time, common_currency, want_positive](const Split* split) { - split = GNC_SPLIT(node->data); - if (split->lot) - continue; - - /* Skip it if it's too early */ - if (xaccTransRetDatePosted (xaccSplitGetParent (split)) < open_time) - { - if (reverse) - /* Going backwards, no point in looking further */ - break; - continue; - } - - /* Allow equiv currencies */ - if (!gnc_commodity_equiv (common_currency, split->parent->common_currency)) - continue; - - /* Disallow zero-amount splits in general. */ - if (gnc_numeric_zero_p(split->amount)) - continue; - - if (want_positive == gnc_numeric_positive_p (split->amount)) - rv = split; - } - return rv; + return (!split->lot && + xaccTransRetDatePosted (xaccSplitGetParent (split)) >= open_time && + gnc_commodity_equiv (common_currency, split->parent->common_currency) && + !gnc_numeric_zero_p (split->amount) && + want_positive == gnc_numeric_positive_p (split->amount)); + }; + + return gnc_account_find_split (lot_account, find_split, reverse); } /* ============================================================== */ diff --git a/libgnucash/engine/test-core/test-engine-stuff.cpp b/libgnucash/engine/test-core/test-engine-stuff.cpp index 3eb0479e36..2024f8fb9a 100644 --- a/libgnucash/engine/test-core/test-engine-stuff.cpp +++ b/libgnucash/engine/test-core/test-engine-stuff.cpp @@ -1074,7 +1074,6 @@ make_random_changes_to_level (QofBook *book, Account *parent) account = static_cast(get_random_list_element (accounts)); splits = xaccAccountGetSplitList (account); - splits = g_list_copy (splits); for (node = splits; node; node = node->next) {