From 2f2cf6a403656aef718e32cf3eee4fce3f0e7bc7 Mon Sep 17 00:00:00 2001 From: Christopher Lam Date: Sat, 5 Oct 2024 08:51:36 +0800 Subject: [PATCH] [account.cpp] more refactoring centralizing GValue uses centralize uses of GValue small modification of xaccAccountSetLastNum behaviour with empty-string last_num --- libgnucash/engine/Account.cpp | 592 ++++++++--------------- libgnucash/engine/test/utest-Account.cpp | 2 +- 2 files changed, 200 insertions(+), 394 deletions(-) diff --git a/libgnucash/engine/Account.cpp b/libgnucash/engine/Account.cpp index fd4e5f42cf4..b45f6352e39 100644 --- a/libgnucash/engine/Account.cpp +++ b/libgnucash/engine/Account.cpp @@ -2481,6 +2481,8 @@ xaccAccountSetDescription (Account *acc, const char *str) xaccAccountCommitEdit(acc); } +using StrVec = std::vector; + static void set_kvp_gnc_numeric_path (Account *acc, const std::vector& path, std::optional value) @@ -2503,8 +2505,7 @@ set_kvp_gnc_numeric_path (Account *acc, const std::vector& path, } static void -set_kvp_string_path (Account *acc, const std::vector& path, - const char *value) +set_kvp_string_path (Account *acc, const StrVec& path, const char *value) { g_return_if_fail(GNC_IS_ACCOUNT(acc)); @@ -2518,17 +2519,42 @@ set_kvp_string_path (Account *acc, const std::vector& path, g_value_unset (&v); } else - { qof_instance_set_path_kvp (QOF_INSTANCE (acc), nullptr, path); - } - mark_account (acc); + + qof_instance_set_dirty (QOF_INSTANCE (acc)); xaccAccountCommitEdit(acc); } +static const char* +get_kvp_string_path (const Account *acc, const StrVec& path) +{ + g_return_val_if_fail (GNC_IS_ACCOUNT(acc), nullptr); + GValue v = G_VALUE_INIT; + qof_instance_get_path_kvp (QOF_INSTANCE (acc), &v, path); + const char* rv = G_VALUE_HOLDS_STRING (&v) ? g_value_get_string (&v) : nullptr; + g_value_unset (&v); + return rv; +} + static void -set_kvp_string_tag (Account *acc, const char *tag, const char *value) +set_kvp_account_path (Account* acc, const StrVec& path, const Account* kvp_account) { - set_kvp_string_path (acc, {tag}, value); + g_return_if_fail (GNC_IS_ACCOUNT(acc)); + + xaccAccountBeginEdit(acc); + if (GNC_IS_ACCOUNT(kvp_account)) + { + GValue v = G_VALUE_INIT; + g_value_init (&v, GNC_TYPE_GUID); + g_value_set_static_boxed (&v, xaccAccountGetGUID (kvp_account)); + qof_instance_set_path_kvp (QOF_INSTANCE (acc), &v, path); + g_value_unset (&v); + } + else + qof_instance_set_path_kvp (QOF_INSTANCE (acc), nullptr, path); + + qof_instance_set_dirty (QOF_INSTANCE (acc)); + xaccAccountCommitEdit(acc); } static std::optional @@ -2544,44 +2570,74 @@ get_kvp_gnc_numeric_path (const Account *acc, const std::vector& pa return rv; } -static const char* -get_kvp_string_path (const Account *acc, std::vector const & path, - GValue *v) +static Account* +get_kvp_account_path (const Account *acc, const StrVec& path) { - *v = G_VALUE_INIT; - if (acc == nullptr) return nullptr; // how to check path is valid?? - qof_instance_get_path_kvp (QOF_INSTANCE (acc), v, path); - return G_VALUE_HOLDS_STRING (v) ? g_value_get_string (v) : nullptr; + g_return_val_if_fail (GNC_IS_ACCOUNT(acc), nullptr); + + GValue v = G_VALUE_INIT; + qof_instance_get_path_kvp (QOF_INSTANCE (acc), &v, path); + + auto guid = static_cast(G_VALUE_HOLDS_BOXED (&v) ? g_value_get_boxed(&v) : nullptr); + g_value_unset (&v); + + return guid ? xaccAccountLookup (guid, gnc_account_get_book (acc)) : nullptr; } -static const char* -get_kvp_string_tag (const Account *acc, const char *tag, GValue *v) +static void +set_kvp_boolean_path (Account *acc, const StrVec& path, gboolean option) { - return get_kvp_string_path (acc, {tag}, v); + GValue v = G_VALUE_INIT; + g_return_if_fail(GNC_IS_ACCOUNT(acc)); + + g_value_init (&v, G_TYPE_BOOLEAN); + g_value_set_boolean (&v, option); + xaccAccountBeginEdit (acc); + qof_instance_set_path_kvp (QOF_INSTANCE (acc), &v, path); + mark_account (acc); + xaccAccountCommitEdit (acc); + g_value_unset (&v); +} + +static gboolean +get_kvp_boolean_path (const Account *acc, const StrVec& path) +{ + GValue v = G_VALUE_INIT; + gboolean retval = FALSE; + g_return_val_if_fail(GNC_IS_ACCOUNT(acc), FALSE); + qof_instance_get_path_kvp (QOF_INSTANCE(acc), &v, path); + if (G_VALUE_HOLDS_INT64 (&v)) + retval = (g_value_get_int64 (&v) != 0); + if (G_VALUE_HOLDS_BOOLEAN (&v)) + retval = (g_value_get_boolean (&v)); + if (G_VALUE_HOLDS_STRING (&v)) + retval = !strcmp (g_value_get_string (&v), "true"); + g_value_unset (&v); + return retval; } void xaccAccountSetColor (Account *acc, const char *str) { - set_kvp_string_tag (acc, "color", str); + set_kvp_string_path (acc, { "color" }, str); } void xaccAccountSetFilter (Account *acc, const char *str) { - set_kvp_string_tag (acc, "filter", str); + set_kvp_string_path (acc, { "filter" }, str); } void xaccAccountSetSortOrder (Account *acc, const char *str) { - set_kvp_string_tag (acc, "sort-order", str); + set_kvp_string_path (acc, { "sort-order" }, str); } void xaccAccountSetSortReversed (Account *acc, gboolean sortreversed) { - set_kvp_string_tag (acc, "sort-reversed", sortreversed ? "true" : nullptr); + set_kvp_string_path (acc, { "sort-reversed" }, sortreversed ? "true" : nullptr); } static void @@ -2605,7 +2661,7 @@ qofAccountSetParent (Account *acc, QofInstance *parent) void xaccAccountSetNotes (Account *acc, const char *str) { - set_kvp_string_tag (acc, "notes", str); + set_kvp_string_path (acc, { "notes" }, str); } @@ -2615,25 +2671,7 @@ xaccAccountSetAssociatedAccount (Account *acc, const char *tag, const Account* a g_return_if_fail (GNC_IS_ACCOUNT(acc)); g_return_if_fail (tag && *tag); - std::vector path = { "associated-account", tag }; - xaccAccountBeginEdit(acc); - - PINFO ("setting %s assoc %s account = %s", xaccAccountGetName (acc), tag, - assoc_acct ? xaccAccountGetName (assoc_acct) : nullptr); - - if (GNC_IS_ACCOUNT(assoc_acct)) - { - GValue v = G_VALUE_INIT; - g_value_init (&v, GNC_TYPE_GUID); - g_value_set_static_boxed (&v, xaccAccountGetGUID (assoc_acct)); - qof_instance_set_path_kvp (QOF_INSTANCE (acc), &v, path); - g_value_unset (&v); - } - else - qof_instance_set_path_kvp (QOF_INSTANCE (acc), nullptr, path); - - mark_account (acc); - xaccAccountCommitEdit(acc); + set_kvp_account_path (acc, { "associated-account", tag }, assoc_acct); } void @@ -2746,29 +2784,17 @@ xaccAccountGetNonStdSCU (const Account * acc) void DxaccAccountSetCurrency (Account * acc, gnc_commodity * currency) { - QofBook *book; - GValue v = G_VALUE_INIT; - const char *s = gnc_commodity_get_unique_name (currency); - gnc_commodity *commodity; - gnc_commodity_table *table; - if ((!acc) || (!currency)) return; - g_value_init (&v, G_TYPE_STRING); - g_value_set_static_string (&v, s); - qof_instance_set_path_kvp (QOF_INSTANCE (acc), &v, {"old-currency"}); - mark_account (acc); - xaccAccountCommitEdit(acc); - g_value_unset (&v); - table = gnc_commodity_table_get_table (qof_instance_get_book(acc)); - commodity = gnc_commodity_table_lookup_unique (table, s); + auto s = gnc_commodity_get_unique_name (currency); + set_kvp_string_path (acc, {"old-currency"}, s); + + auto book = qof_instance_get_book(acc); + auto table = gnc_commodity_table_get_table (book); + auto commodity = gnc_commodity_table_lookup_unique (table, s); if (!commodity) - { - book = qof_instance_get_book(acc); - gnc_commodity_table_insert (gnc_commodity_table_get_table (book), - currency); - } + gnc_commodity_table_insert (table, currency); } /********************************************************************\ @@ -3330,51 +3356,36 @@ xaccAccountGetDescription (const Account *acc) const char * xaccAccountGetColor (const Account *acc) { - GValue v = G_VALUE_INIT; g_return_val_if_fail(GNC_IS_ACCOUNT(acc), nullptr); - auto rv = get_kvp_string_tag (acc, "color", &v); - g_value_unset (&v); - return rv; + return get_kvp_string_path (acc, { "color" }); } const char * xaccAccountGetFilter (const Account *acc) { - GValue v = G_VALUE_INIT; g_return_val_if_fail(GNC_IS_ACCOUNT(acc), 0); - auto rv = get_kvp_string_tag (acc, "filter", &v); - g_value_unset (&v); - return rv; + return get_kvp_string_path (acc, { "filter" }); } const char * xaccAccountGetSortOrder (const Account *acc) { g_return_val_if_fail(GNC_IS_ACCOUNT(acc), 0); - GValue v = G_VALUE_INIT; - auto rv = get_kvp_string_tag (acc, "sort-order", &v); - g_value_unset (&v); - return rv; + return get_kvp_string_path (acc, { "sort-order" }); } gboolean xaccAccountGetSortReversed (const Account *acc) { g_return_val_if_fail(GNC_IS_ACCOUNT(acc), FALSE); - GValue v = G_VALUE_INIT; - auto rv = !g_strcmp0 (get_kvp_string_tag (acc, "sort-reversed", &v), "true"); - g_value_unset (&v); - return rv; + return get_kvp_boolean_path (acc, { "sort-reversed" }); } const char * xaccAccountGetNotes (const Account *acc) { g_return_val_if_fail(GNC_IS_ACCOUNT(acc), nullptr); - GValue v = G_VALUE_INIT; - auto rv = get_kvp_string_tag (acc, "notes", &v); - g_value_unset (&v); - return rv; + return get_kvp_string_path (acc, { "notes" }); } Account* @@ -3383,42 +3394,21 @@ xaccAccountGetAssociatedAccount (const Account *acc, const char *tag) g_return_val_if_fail (GNC_IS_ACCOUNT(acc), nullptr); g_return_val_if_fail (tag && *tag, nullptr); - GValue v = G_VALUE_INIT; - qof_instance_get_path_kvp (QOF_INSTANCE (acc), &v, { "associated-account", tag }); - - auto guid = static_cast(G_VALUE_HOLDS_BOXED (&v) ? g_value_get_boxed(&v) : nullptr); - g_value_unset (&v); - - if (!guid) - return nullptr; - - auto assoc_acct = xaccAccountLookup (guid, gnc_account_get_book (acc)); - PINFO ("retuning %s assoc %s account = %s", xaccAccountGetName (acc), tag, - xaccAccountGetName (assoc_acct)); - return assoc_acct; + return get_kvp_account_path (acc, { "associated-account", tag }); } gnc_commodity * DxaccAccountGetCurrency (const Account *acc) { - GValue v = G_VALUE_INIT; - const char *s = nullptr; - gnc_commodity_table *table; - gnc_commodity *retval = nullptr; - if (!acc) return nullptr; - qof_instance_get_path_kvp (QOF_INSTANCE(acc), &v, {"old-currency"}); - if (G_VALUE_HOLDS_STRING (&v)) - s = g_value_get_string (&v); - if (s) + if (auto s = get_kvp_string_path (acc, {"old-currency"})) { - table = gnc_commodity_table_get_table (qof_instance_get_book(acc)); - retval = gnc_commodity_table_lookup_unique (table, s); + auto table = gnc_commodity_table_get_table (qof_instance_get_book(acc)); + return gnc_commodity_table_lookup_unique (table, s); } - g_value_unset (&v); - return retval; + return nullptr; } gnc_commodity * @@ -4032,37 +4022,6 @@ xaccAccountForEachLot(const Account *acc, return nullptr; } -static void -set_boolean_key (Account *acc, std::vector const & path, gboolean option) -{ - GValue v = G_VALUE_INIT; - g_return_if_fail(GNC_IS_ACCOUNT(acc)); - - g_value_init (&v, G_TYPE_BOOLEAN); - g_value_set_boolean (&v, option); - xaccAccountBeginEdit (acc); - qof_instance_set_path_kvp (QOF_INSTANCE (acc), &v, path); - mark_account (acc); - xaccAccountCommitEdit (acc); - g_value_unset (&v); -} - -static gboolean -boolean_from_key (const Account *acc, std::vector const & path) -{ - GValue v = G_VALUE_INIT; - gboolean retval = FALSE; - g_return_val_if_fail(GNC_IS_ACCOUNT(acc), FALSE); - qof_instance_get_path_kvp (QOF_INSTANCE(acc), &v, path); - if (G_VALUE_HOLDS_INT64 (&v)) - retval = (g_value_get_int64 (&v) != 0); - if (G_VALUE_HOLDS_BOOLEAN (&v)) - retval = (g_value_get_boolean (&v)); - if (G_VALUE_HOLDS_STRING (&v)) - retval = !strcmp (g_value_get_string (&v), "true"); - g_value_unset (&v); - return retval; -} /********************************************************************\ \********************************************************************/ @@ -4071,22 +4030,20 @@ boolean_from_key (const Account *acc, std::vector const & path) gboolean xaccAccountGetTaxRelated (const Account *acc) { - return boolean_from_key(acc, {"tax-related"}); + return get_kvp_boolean_path(acc, {"tax-related"}); } void xaccAccountSetTaxRelated (Account *acc, gboolean tax_related) { - set_boolean_key(acc, {"tax-related"}, tax_related); + set_kvp_boolean_path(acc, {"tax-related"}, tax_related); } const char * xaccAccountGetTaxUSCode (const Account *acc) { - GValue v = G_VALUE_INIT; g_return_val_if_fail(GNC_IS_ACCOUNT(acc), FALSE); - qof_instance_get_path_kvp (QOF_INSTANCE(acc), &v, {"tax-US", "code"}); - return G_VALUE_HOLDS_STRING (&v) ? g_value_get_string (&v) : nullptr; + return get_kvp_string_path (acc, {"tax-US", "code"}); } void @@ -4098,10 +4055,8 @@ xaccAccountSetTaxUSCode (Account *acc, const char *code) const char * xaccAccountGetTaxUSPayerNameSource (const Account *acc) { - GValue v = G_VALUE_INIT; g_return_val_if_fail(GNC_IS_ACCOUNT(acc), FALSE); - qof_instance_get_path_kvp (QOF_INSTANCE(acc), &v, {"tax-US", "payer-name-source"}); - return G_VALUE_HOLDS_STRING (&v) ? g_value_get_string (&v) : nullptr; + return get_kvp_string_path (acc, {"tax-US", "payer-name-source"}); } void @@ -4110,39 +4065,54 @@ xaccAccountSetTaxUSPayerNameSource (Account *acc, const char *source) set_kvp_string_path (acc, {"tax-US", "payer-name-source"}, source); } -gint64 -xaccAccountGetTaxUSCopyNumber (const Account *acc) -{ - gint64 copy_number = 0; - GValue v = G_VALUE_INIT; - g_return_val_if_fail(GNC_IS_ACCOUNT(acc), FALSE); - qof_instance_get_path_kvp (QOF_INSTANCE(acc), &v, {"tax-US", "copy-number"}); - if (G_VALUE_HOLDS_INT64 (&v)) - copy_number = g_value_get_int64 (&v); - - g_value_unset (&v); - return (copy_number == 0) ? 1 : copy_number; -} -void -xaccAccountSetTaxUSCopyNumber (Account *acc, gint64 copy_number) +static void +set_kvp_int64_path (Account *acc, const StrVec& path, std::optional value) { g_return_if_fail(GNC_IS_ACCOUNT(acc)); - xaccAccountBeginEdit (acc); - if (copy_number != 0) + + xaccAccountBeginEdit(acc); + if (value) { GValue v = G_VALUE_INIT; g_value_init (&v, G_TYPE_INT64); - g_value_set_int64 (&v, copy_number); - qof_instance_set_path_kvp (QOF_INSTANCE (acc), &v, {"tax-US", "copy-number"}); + g_value_set_int64 (&v, *value); + qof_instance_set_path_kvp (QOF_INSTANCE (acc), &v, path); g_value_unset (&v); } else - { - qof_instance_set_path_kvp (QOF_INSTANCE (acc), nullptr, {"tax-US", "copy-number"}); - } + qof_instance_set_path_kvp (QOF_INSTANCE (acc), nullptr, path); + mark_account (acc); - xaccAccountCommitEdit (acc); + xaccAccountCommitEdit(acc); +} + +static const std::optional +get_kvp_int64_path (const Account *acc, const StrVec& path) +{ + g_return_val_if_fail (GNC_IS_ACCOUNT(acc), std::nullopt); + GValue v = G_VALUE_INIT; + std::optional rv; + qof_instance_get_path_kvp (QOF_INSTANCE (acc), &v, path); + if (G_VALUE_HOLDS_INT64 (&v)) + rv = g_value_get_int64 (&v); + g_value_unset (&v); + return rv; +} + +gint64 +xaccAccountGetTaxUSCopyNumber (const Account *acc) +{ + g_return_val_if_fail(GNC_IS_ACCOUNT(acc), 0); + auto copy_number = get_kvp_int64_path (acc, {"tax-US", "copy-number"}); + return copy_number ? *copy_number : 1; +} + +void +xaccAccountSetTaxUSCopyNumber (Account *acc, gint64 copy_number) +{ + g_return_if_fail(GNC_IS_ACCOUNT(acc)); + set_kvp_int64_path (acc, {"tax-US", "copy-number"}, copy_number); } /*********************************************************************\ @@ -4179,25 +4149,25 @@ const char *gnc_account_get_credit_string (GNCAccountType acct_type) gboolean xaccAccountGetPlaceholder (const Account *acc) { - return boolean_from_key(acc, {"placeholder"}); + return get_kvp_boolean_path(acc, {"placeholder"}); } void xaccAccountSetPlaceholder (Account *acc, gboolean val) { - set_boolean_key(acc, {"placeholder"}, val); + set_kvp_boolean_path(acc, {"placeholder"}, val); } gboolean xaccAccountGetAppendText (const Account *acc) { - return boolean_from_key(acc, {"import-append-text"}); + return get_kvp_boolean_path(acc, {"import-append-text"}); } void xaccAccountSetAppendText (Account *acc, gboolean val) { - set_boolean_key(acc, {"import-append-text"}, val); + set_kvp_boolean_path(acc, {"import-append-text"}, val); } gboolean @@ -4206,11 +4176,7 @@ xaccAccountGetIsOpeningBalance (const Account *acc) if (GET_PRIVATE(acc)->type != ACCT_TYPE_EQUITY) return false; - GValue v = G_VALUE_INIT; - auto rv = !g_strcmp0 (get_kvp_string_tag (acc, "equity-type", &v), - "opening-balance"); - g_value_unset (&v); - return rv; + return !g_strcmp0 (get_kvp_string_path (acc, { "equity-type" }), "opening-balance"); } void @@ -4218,7 +4184,7 @@ xaccAccountSetIsOpeningBalance (Account *acc, gboolean val) { if (GET_PRIVATE(acc)->type != ACCT_TYPE_EQUITY) return; - set_kvp_string_tag(acc, "equity-type", val ? "opening-balance" : nullptr); + set_kvp_string_path(acc, { "equity-type" }, val ? "opening-balance" : nullptr); } GNCPlaceholderType @@ -4237,13 +4203,13 @@ xaccAccountGetDescendantPlaceholder (const Account *acc) gboolean xaccAccountGetAutoInterest (const Account *acc) { - return boolean_from_key (acc, {KEY_RECONCILE_INFO, "auto-interest-transfer"}); + return get_kvp_boolean_path (acc, {KEY_RECONCILE_INFO, "auto-interest-transfer"}); } void xaccAccountSetAutoInterest (Account *acc, gboolean val) { - set_boolean_key (acc, {KEY_RECONCILE_INFO, "auto-interest-transfer"}, val); + set_kvp_boolean_path (acc, {KEY_RECONCILE_INFO, "auto-interest-transfer"}, val); } /********************************************************************\ @@ -4252,13 +4218,13 @@ xaccAccountSetAutoInterest (Account *acc, gboolean val) gboolean xaccAccountGetHidden (const Account *acc) { - return boolean_from_key (acc, {"hidden"}); + return get_kvp_boolean_path (acc, {"hidden"}); } void xaccAccountSetHidden (Account *acc, gboolean val) { - set_boolean_key (acc, {"hidden"}, val); + set_kvp_boolean_path (acc, {"hidden"}, val); } gboolean @@ -4628,22 +4594,16 @@ xaccAccountIsPriced(const Account *acc) gboolean xaccAccountGetReconcileLastDate (const Account *acc, time64 *last_date) { - gint64 date = 0; - GValue v = G_VALUE_INIT; gboolean retval = FALSE; g_return_val_if_fail(GNC_IS_ACCOUNT(acc), FALSE); - qof_instance_get_path_kvp (QOF_INSTANCE(acc), &v, {KEY_RECONCILE_INFO, "last-date"}); - if (G_VALUE_HOLDS_INT64 (&v)) - date = g_value_get_int64 (&v); + auto date = get_kvp_int64_path (acc, {KEY_RECONCILE_INFO, "last-date"}); - g_value_unset (&v); if (date) { if (last_date) - *last_date = date; + *last_date = *date; retval = TRUE; } - g_value_unset (&v); return retval; } @@ -4653,16 +4613,8 @@ xaccAccountGetReconcileLastDate (const Account *acc, time64 *last_date) void xaccAccountSetReconcileLastDate (Account *acc, time64 last_date) { - GValue v = G_VALUE_INIT; g_return_if_fail(GNC_IS_ACCOUNT(acc)); - - g_value_init (&v, G_TYPE_INT64); - g_value_set_int64 (&v, last_date); - xaccAccountBeginEdit (acc); - qof_instance_set_path_kvp (QOF_INSTANCE (acc), &v, {KEY_RECONCILE_INFO, "last-date"}); - mark_account (acc); - xaccAccountCommitEdit (acc); - g_value_unset (&v); + set_kvp_int64_path (acc, {KEY_RECONCILE_INFO, "last-date"}, last_date); } /********************************************************************\ @@ -4672,31 +4624,19 @@ gboolean xaccAccountGetReconcileLastInterval (const Account *acc, int *months, int *days) { - GValue v1 = G_VALUE_INIT, v2 = G_VALUE_INIT; - int64_t m = 0, d = 0; - gboolean retval = FALSE; - if (!acc) return FALSE; g_return_val_if_fail(GNC_IS_ACCOUNT(acc), FALSE); - qof_instance_get_path_kvp (QOF_INSTANCE(acc), &v1, - {KEY_RECONCILE_INFO, "last-interval", "months"}); - qof_instance_get_path_kvp (QOF_INSTANCE(acc), &v2, - {KEY_RECONCILE_INFO, "last-interval", "days"}); - if (G_VALUE_HOLDS_INT64 (&v1)) - m = g_value_get_int64 (&v1); - if (G_VALUE_HOLDS_INT64 (&v2)) - d = g_value_get_int64 (&v2); + auto m{get_kvp_int64_path (acc, {KEY_RECONCILE_INFO, "last-interval", "months"})}; + auto d{get_kvp_int64_path (acc, {KEY_RECONCILE_INFO, "last-interval", "days"})}; if (m && d) { if (months) - *months = m; + *months = *m; if (days) - *days = d; - retval = TRUE; + *days = *d; + return true; } - g_value_unset (&v1); - g_value_unset (&v2); - return retval; + return false; } /********************************************************************\ @@ -4705,22 +4645,9 @@ xaccAccountGetReconcileLastInterval (const Account *acc, void xaccAccountSetReconcileLastInterval (Account *acc, int months, int days) { - GValue v1 = G_VALUE_INIT, v2 = G_VALUE_INIT; g_return_if_fail(GNC_IS_ACCOUNT(acc)); - - g_value_init (&v1, G_TYPE_INT64); - g_value_set_int64 (&v1, months); - g_value_init (&v2, G_TYPE_INT64); - g_value_set_int64 (&v2, days); - xaccAccountBeginEdit (acc); - qof_instance_set_path_kvp (QOF_INSTANCE (acc), &v1, - {KEY_RECONCILE_INFO, "last-interval", "months"}); - qof_instance_set_path_kvp (QOF_INSTANCE (acc), &v2, - {KEY_RECONCILE_INFO, "last-interval", "days"}); - mark_account (acc); - xaccAccountCommitEdit (acc); - g_value_unset (&v1); - g_value_unset (&v2); + set_kvp_int64_path (acc, {KEY_RECONCILE_INFO, "last-interval", "months"}, months); + set_kvp_int64_path (acc, {KEY_RECONCILE_INFO, "last-interval", "days"}, days); } /********************************************************************\ @@ -4729,23 +4656,15 @@ xaccAccountSetReconcileLastInterval (Account *acc, int months, int days) gboolean xaccAccountGetReconcilePostponeDate (const Account *acc, time64 *postpone_date) { - gint64 date = 0; - gboolean retval = FALSE; - GValue v = G_VALUE_INIT; g_return_val_if_fail(GNC_IS_ACCOUNT(acc), FALSE); - qof_instance_get_path_kvp (QOF_INSTANCE(acc), &v, - {KEY_RECONCILE_INFO, KEY_POSTPONE, "date"}); - if (G_VALUE_HOLDS_INT64 (&v)) - date = g_value_get_int64 (&v); - if (date) + if (auto date = get_kvp_int64_path (acc, {KEY_RECONCILE_INFO, KEY_POSTPONE, "date"})) { if (postpone_date) - *postpone_date = date; - retval = TRUE; + *postpone_date = *date; + return true; } - g_value_unset (&v); - return retval; + return false; } /********************************************************************\ @@ -4754,17 +4673,8 @@ xaccAccountGetReconcilePostponeDate (const Account *acc, time64 *postpone_date) void xaccAccountSetReconcilePostponeDate (Account *acc, time64 postpone_date) { - GValue v = G_VALUE_INIT; g_return_if_fail(GNC_IS_ACCOUNT(acc)); - - g_value_init (&v, G_TYPE_INT64); - g_value_set_int64 (&v, postpone_date); - xaccAccountBeginEdit (acc); - qof_instance_set_path_kvp (QOF_INSTANCE (acc), &v, - {KEY_RECONCILE_INFO, KEY_POSTPONE, "date"}); - mark_account (acc); - xaccAccountCommitEdit (acc); - g_value_unset (&v); + set_kvp_int64_path (acc, {KEY_RECONCILE_INFO, KEY_POSTPONE, "date"}, postpone_date); } /********************************************************************\ @@ -4774,24 +4684,14 @@ gboolean xaccAccountGetReconcilePostponeBalance (const Account *acc, gnc_numeric *balance) { - gnc_numeric bal = gnc_numeric_zero (); - GValue v = G_VALUE_INIT; - gboolean retval = FALSE; g_return_val_if_fail(GNC_IS_ACCOUNT(acc), FALSE); - qof_instance_get_path_kvp (QOF_INSTANCE(acc), &v, - {KEY_RECONCILE_INFO, KEY_POSTPONE, "balance"}); - if (G_VALUE_HOLDS_BOXED (&v)) + if (auto bal = get_kvp_gnc_numeric_path (acc, {KEY_RECONCILE_INFO, KEY_POSTPONE, "balance"})) { - bal = *(gnc_numeric*)g_value_get_boxed (&v); - if (bal.denom) - { - if (balance) - *balance = bal; - retval = TRUE; - } + if (balance) + *balance = *bal; + return true; } - g_value_unset (&v); - return retval; + return false; } /********************************************************************\ @@ -4800,17 +4700,8 @@ xaccAccountGetReconcilePostponeBalance (const Account *acc, void xaccAccountSetReconcilePostponeBalance (Account *acc, gnc_numeric balance) { - GValue v = G_VALUE_INIT; g_return_if_fail(GNC_IS_ACCOUNT(acc)); - - g_value_init (&v, GNC_TYPE_NUMERIC); - g_value_set_boxed (&v, &balance); - xaccAccountBeginEdit (acc); - qof_instance_set_path_kvp (QOF_INSTANCE (acc), &v, - {KEY_RECONCILE_INFO, KEY_POSTPONE, "balance"}); - mark_account (acc); - xaccAccountCommitEdit (acc); - g_value_unset (&v); + set_kvp_gnc_numeric_path (acc, {KEY_RECONCILE_INFO, KEY_POSTPONE, "balance"}, balance); } /********************************************************************\ @@ -4834,10 +4725,8 @@ xaccAccountClearReconcilePostpone (Account *acc) const char * xaccAccountGetLastNum (const Account *acc) { - GValue v = G_VALUE_INIT; - g_return_val_if_fail(GNC_IS_ACCOUNT(acc), FALSE); - qof_instance_get_path_kvp (QOF_INSTANCE(acc), &v, {"last-num"}); - return G_VALUE_HOLDS_STRING (&v) ? g_value_get_string (&v) : nullptr; + g_return_val_if_fail (GNC_IS_ACCOUNT(acc), nullptr); + return get_kvp_string_path (acc, { "last-num" }); } /********************************************************************\ @@ -4846,15 +4735,7 @@ xaccAccountGetLastNum (const Account *acc) void xaccAccountSetLastNum (Account *acc, const char *num) { - GValue v = G_VALUE_INIT; - g_return_if_fail(GNC_IS_ACCOUNT(acc)); - g_value_init (&v, G_TYPE_STRING); - - g_value_set_static_string (&v, num); - xaccAccountBeginEdit (acc); - qof_instance_set_path_kvp (QOF_INSTANCE (acc), &v, {"last-num"}); - mark_account (acc); - xaccAccountCommitEdit (acc); + set_kvp_string_path (acc, { "last-num" }, num); } @@ -4950,7 +4831,7 @@ xaccAccountGetIncludeSubAccountBalances (const Account *acc) if (!GET_PRIVATE(acc)->include_sub_account_balances.has_value()) { - gboolean inc_sub = boolean_from_key (acc, {KEY_BALANCE_LIMIT, + gboolean inc_sub = get_kvp_boolean_path (acc, {KEY_BALANCE_LIMIT, KEY_BALANCE_INCLUDE_SUB_ACCTS}); GET_PRIVATE(acc)->include_sub_account_balances = inc_sub; @@ -4965,20 +4846,8 @@ xaccAccountSetIncludeSubAccountBalances (Account *acc, gboolean inc_sub) if (inc_sub != xaccAccountGetIncludeSubAccountBalances (acc)) { - GValue v = G_VALUE_INIT; - g_value_init (&v, G_TYPE_BOOLEAN); - g_value_set_boolean (&v, inc_sub); - std::vector path {KEY_BALANCE_LIMIT, - KEY_BALANCE_INCLUDE_SUB_ACCTS}; - xaccAccountBeginEdit (acc); - if (inc_sub) - qof_instance_set_path_kvp (QOF_INSTANCE(acc), &v, path); - else - qof_instance_set_path_kvp (QOF_INSTANCE(acc), nullptr, path); + set_kvp_boolean_path (acc, {KEY_BALANCE_LIMIT, KEY_BALANCE_INCLUDE_SUB_ACCTS}, inc_sub); GET_PRIVATE(acc)->include_sub_account_balances = inc_sub; - mark_account (acc); - xaccAccountCommitEdit (acc); - g_value_unset (&v); } } @@ -5033,37 +4902,17 @@ GetOrMakeOrphanAccount (Account *root, gnc_commodity * currency) Account * xaccAccountGainsAccount (Account *acc, gnc_commodity *curr) { - GValue v = G_VALUE_INIT; - std::vector path {KEY_LOT_MGMT, "gains-acct", - gnc_commodity_get_unique_name (curr)}; - GncGUID *guid = nullptr; - Account *gains_account; - g_return_val_if_fail (acc != nullptr, nullptr); - qof_instance_get_path_kvp (QOF_INSTANCE(acc), &v, path); - if (G_VALUE_HOLDS_BOXED (&v)) - guid = (GncGUID*)g_value_get_boxed (&v); - if (guid == nullptr) /* No gains account for this currency */ + + StrVec path {KEY_LOT_MGMT, "gains-acct", gnc_commodity_get_unique_name (curr)}; + auto gains_account = get_kvp_account_path (acc, path); + + if (gains_account == nullptr) /* No gains account for this currency */ { - gains_account = GetOrMakeOrphanAccount (gnc_account_get_root (acc), - curr); - guid = (GncGUID*)qof_instance_get_guid (QOF_INSTANCE (gains_account)); - xaccAccountBeginEdit (acc); - { - GValue vr = G_VALUE_INIT; - g_value_init (&vr, GNC_TYPE_GUID); - g_value_set_boxed (&vr, guid); - qof_instance_set_path_kvp (QOF_INSTANCE (acc), &vr, path); - qof_instance_set_dirty (QOF_INSTANCE (acc)); - g_value_unset (&vr); - } - xaccAccountCommitEdit (acc); + gains_account = GetOrMakeOrphanAccount (gnc_account_get_root (acc), curr); + set_kvp_account_path (acc, path, gains_account); } - else - gains_account = xaccAccountLookup (guid, - qof_instance_get_book(acc)); - g_value_unset (&v); return gains_account; } @@ -5076,7 +4925,7 @@ dxaccAccountSetPriceSrc(Account *acc, const char *src) if (!acc) return; if (xaccAccountIsPriced(acc)) - set_kvp_string_tag (acc, "old-price-source", src); + set_kvp_string_path (acc, { "old-price-source" }, src); } /********************************************************************\ @@ -5092,10 +4941,7 @@ dxaccAccountGetPriceSrc(const Account *acc) g_free (source); - GValue v = G_VALUE_INIT; - auto rv = get_kvp_string_tag (acc, "old-price-source", &v); - g_value_unset (&v); - return rv; + return get_kvp_string_path (acc, { "old-price-source" }); } /********************************************************************\ @@ -5106,7 +4952,7 @@ dxaccAccountSetQuoteTZ(Account *acc, const char *tz) { if (!acc) return; if (!xaccAccountIsPriced(acc)) return; - set_kvp_string_tag (acc, "old-quote-tz", tz); + set_kvp_string_path (acc, { "old-quote-tz" }, tz); } /********************************************************************\ @@ -5117,10 +4963,7 @@ dxaccAccountGetQuoteTZ(const Account *acc) { if (!acc) return nullptr; if (!xaccAccountIsPriced(acc)) return nullptr; - GValue v = G_VALUE_INIT; - auto rv = get_kvp_string_tag (acc, "old-quote-tz", &v); - g_value_unset (&v); - return rv; + return get_kvp_string_path (acc, { "old-quote-tz" }); } /********************************************************************\ @@ -5129,21 +4972,13 @@ dxaccAccountGetQuoteTZ(const Account *acc) void xaccAccountSetReconcileChildrenStatus(Account *acc, gboolean status) { - GValue v = G_VALUE_INIT; if (!acc) return; - xaccAccountBeginEdit (acc); /* Would have been nice to use G_TYPE_BOOLEAN, but the other * boolean kvps save the value as "true" or "false" and that would * be file-incompatible with this. */ - g_value_init (&v, G_TYPE_INT64); - g_value_set_int64 (&v, status); - qof_instance_set_path_kvp (QOF_INSTANCE (acc), &v, - {KEY_RECONCILE_INFO, KEY_INCLUDE_CHILDREN}); - mark_account(acc); - xaccAccountCommitEdit (acc); - g_value_unset (&v); + set_kvp_int64_path (acc, {KEY_RECONCILE_INFO, KEY_INCLUDE_CHILDREN}, status); } /********************************************************************\ @@ -5156,14 +4991,8 @@ xaccAccountGetReconcileChildrenStatus(const Account *acc) * is found then we can assume not to include the children, that being * the default behaviour */ - GValue v = G_VALUE_INIT; - gboolean retval; if (!acc) return FALSE; - qof_instance_get_path_kvp (QOF_INSTANCE (acc), &v, - {KEY_RECONCILE_INFO, KEY_INCLUDE_CHILDREN}); - retval = G_VALUE_HOLDS_INT64 (&v) ? g_value_get_int64 (&v) : FALSE; - g_value_unset (&v); - return retval; + return get_kvp_boolean_path (acc, {KEY_RECONCILE_INFO, KEY_INCLUDE_CHILDREN}); } /********************************************************************\ @@ -5427,20 +5256,12 @@ gnc_account_imap_find_account (Account *acc, const char *category, const char *key) { - GValue v = G_VALUE_INIT; - GncGUID * guid = nullptr; - Account *retval; if (!acc || !key) return nullptr; std::vector path {IMAP_FRAME}; if (category) path.push_back (category); path.push_back (key); - qof_instance_get_path_kvp (QOF_INSTANCE (acc), &v, path); - if (G_VALUE_HOLDS_BOXED (&v)) - guid = (GncGUID*)g_value_get_boxed (&v); - retval = xaccAccountLookup (guid, gnc_account_get_book(acc)); - g_value_unset (&v); - return retval; + return get_kvp_account_path (acc, path); } Account* @@ -5475,19 +5296,11 @@ gnc_account_imap_add_account (Account *acc, const char *key, Account *added_acc) { - GValue v = G_VALUE_INIT; - if (!acc || !key || !added_acc || (strlen (key) == 0)) return; - std::vector path {IMAP_FRAME}; - if (category) - path.emplace_back (category); - path.emplace_back (key); - g_value_init (&v, GNC_TYPE_GUID); - g_value_set_boxed (&v, xaccAccountGetGUID (added_acc)); - xaccAccountBeginEdit (acc); - qof_instance_set_path_kvp (QOF_INSTANCE (acc), &v, path); - qof_instance_set_dirty (QOF_INSTANCE (acc)); - xaccAccountCommitEdit (acc); - g_value_unset (&v); + if (!acc || !key || !added_acc || !*key) return; + + auto path = category ? StrVec{ IMAP_FRAME, category, key } : StrVec{ IMAP_FRAME, key }; + + set_kvp_account_path (acc, path, added_acc); } /* Remove a reference to an Account in the map */ @@ -5497,11 +5310,8 @@ gnc_account_imap_delete_account (Account *acc, const char *key) { if (!acc || !key) return; - std::vector path {IMAP_FRAME}; - if (category) - path.emplace_back (category); - path.emplace_back (key); - xaccAccountBeginEdit (acc); + + auto path = category ? StrVec{ IMAP_FRAME, category, key } : StrVec{ IMAP_FRAME, key }; if (qof_instance_has_path_slot (QOF_INSTANCE (acc), path)) { qof_instance_slot_path_delete (QOF_INSTANCE (acc), path); @@ -5510,7 +5320,6 @@ gnc_account_imap_delete_account (Account *acc, qof_instance_slot_path_delete_if_empty (QOF_INSTANCE (acc), {IMAP_FRAME}); } qof_instance_set_dirty (QOF_INSTANCE (acc)); - xaccAccountCommitEdit (acc); } /*-------------------------------------------------------------------------- @@ -6025,12 +5834,9 @@ gnc_account_imap_get_info (Account *acc, const char *category) gchar * gnc_account_get_map_entry (Account *acc, const char *head, const char *category) { - GValue v = G_VALUE_INIT; - auto rv = g_strdup (category ? - get_kvp_string_path (acc, {head, category}, &v) : - get_kvp_string_path (acc, {head}, &v)); - g_value_unset (&v); - return rv; + return g_strdup (category ? + get_kvp_string_path (acc, {head, category}) : + get_kvp_string_path (acc, {head})); } diff --git a/libgnucash/engine/test/utest-Account.cpp b/libgnucash/engine/test/utest-Account.cpp index 1831d911097..7f8585c17de 100644 --- a/libgnucash/engine/test/utest-Account.cpp +++ b/libgnucash/engine/test/utest-Account.cpp @@ -1107,7 +1107,7 @@ test_gnc_account_kvp_setters_getters (Fixture *fixture, gconstpointer pData) g_assert_cmpstr (xaccAccountGetLastNum (account), ==, "red"); xaccAccountSetLastNum (account, ""); - g_assert_cmpstr (xaccAccountGetLastNum (account), ==, ""); + g_assert_cmpstr (xaccAccountGetLastNum (account), ==, nullptr); xaccAccountSetLastNum (account, " "); g_assert_cmpstr (xaccAccountGetLastNum (account), ==, " ");