diff --git a/libgnucash/engine/Account.cpp b/libgnucash/engine/Account.cpp index ed2bde4f341..76f4c392e89 100644 --- a/libgnucash/engine/Account.cpp +++ b/libgnucash/engine/Account.cpp @@ -2480,6 +2480,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) @@ -2502,8 +2504,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)); @@ -2517,17 +2518,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 @@ -2543,44 +2569,42 @@ 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); -static const char* -get_kvp_string_tag (const Account *acc, const char *tag, GValue *v) -{ - return get_kvp_string_path (acc, {tag}, v); + 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; } 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 @@ -2604,7 +2628,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); } @@ -2614,25 +2638,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 @@ -2745,29 +2751,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); } /********************************************************************\ @@ -3329,51 +3323,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 !g_strcmp0 (get_kvp_string_path (acc, { "sort-reversed" }), "true"); } 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* @@ -3382,42 +3361,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 * @@ -4082,10 +4040,8 @@ xaccAccountSetTaxRelated (Account *acc, gboolean 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 @@ -4097,10 +4053,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 @@ -4109,39 +4063,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); } /*********************************************************************\ @@ -4205,11 +4174,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 @@ -4217,7 +4182,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 @@ -4627,22 +4592,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; } @@ -4652,16 +4611,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); } /********************************************************************\ @@ -4671,31 +4622,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; } /********************************************************************\ @@ -4704,22 +4643,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); } /********************************************************************\ @@ -4833,10 +4759,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" }); } /********************************************************************\ @@ -4845,15 +4769,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); } @@ -5032,37 +4948,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; } @@ -5075,7 +4971,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); } /********************************************************************\ @@ -5091,10 +4987,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" }); } /********************************************************************\ @@ -5105,7 +4998,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); } /********************************************************************\ @@ -5116,10 +5009,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" }); } /********************************************************************\ @@ -5474,19 +5364,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 */ @@ -5496,11 +5378,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); @@ -5509,7 +5388,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); } /*-------------------------------------------------------------------------- @@ -6024,12 +5902,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), ==, " ");