From 4b1ac6fa136e4354ca9bf68605ce99b6a0f98cab Mon Sep 17 00:00:00 2001 From: Adrian Panella Date: Thu, 30 May 2019 21:56:35 -0500 Subject: [PATCH] [budget] Add 'notes' functionality to engine Add ability to save notes on each budget value (account/period). --- libgnucash/engine/gnc-budget.c | 54 ++++++++++++++++++++++++++++++++++ libgnucash/engine/gnc-budget.h | 7 +++++ 2 files changed, 61 insertions(+) diff --git a/libgnucash/engine/gnc-budget.c b/libgnucash/engine/gnc-budget.c index 7b435dd549..15df475961 100644 --- a/libgnucash/engine/gnc-budget.c +++ b/libgnucash/engine/gnc-budget.c @@ -589,6 +589,60 @@ gnc_budget_get_account_period_value(const GncBudget *budget, } +void +gnc_budget_set_account_period_note(GncBudget *budget, const Account *account, + guint period_num, const gchar *note) +{ + gchar path_part_one [GUID_ENCODING_LENGTH + 1]; + gchar path_part_two [GNC_BUDGET_MAX_NUM_PERIODS_DIGITS]; + + /* Watch out for an off-by-one error here: + * period_num starts from 0 while num_periods starts from 1 */ + if (period_num >= GET_PRIVATE(budget)->num_periods) + { + PWARN("Period %i does not exist", period_num); + return; + } + + g_return_if_fail (budget != NULL); + g_return_if_fail (account != NULL); + + make_period_path (account, period_num, path_part_one, path_part_two); + + gnc_budget_begin_edit(budget); + if (note == NULL) + qof_instance_set_kvp (QOF_INSTANCE (budget), NULL, 3, GNC_BUDGET_NOTES_PATH, path_part_one, path_part_two); + else + { + GValue v = G_VALUE_INIT; + g_value_init (&v, G_TYPE_STRING); + g_value_set_string (&v, note); + + qof_instance_set_kvp (QOF_INSTANCE (budget), &v, 3, GNC_BUDGET_NOTES_PATH, path_part_one, path_part_two); + } + qof_instance_set_dirty(&budget->inst); + gnc_budget_commit_edit(budget); + + qof_event_gen( &budget->inst, QOF_EVENT_MODIFY, NULL); + +} + +const gchar * +gnc_budget_get_account_period_note(const GncBudget *budget, + const Account *account, guint period_num) +{ + gchar path_part_one [GUID_ENCODING_LENGTH + 1]; + gchar path_part_two [GNC_BUDGET_MAX_NUM_PERIODS_DIGITS]; + GValue v = G_VALUE_INIT; + + g_return_val_if_fail(GNC_IS_BUDGET(budget), NULL); + g_return_val_if_fail(account, NULL); + + make_period_path (account, period_num, path_part_one, path_part_two); + qof_instance_get_kvp (QOF_INSTANCE (budget), &v, 3, GNC_BUDGET_NOTES_PATH, path_part_one, path_part_two); + return (G_VALUE_HOLDS_STRING(&v)) ? g_value_get_string(&v) : NULL; +} + time64 gnc_budget_get_period_start_date(const GncBudget *budget, guint period_num) { diff --git a/libgnucash/engine/gnc-budget.h b/libgnucash/engine/gnc-budget.h index 5482758339..e788de7519 100644 --- a/libgnucash/engine/gnc-budget.h +++ b/libgnucash/engine/gnc-budget.h @@ -90,6 +90,8 @@ GType gnc_budget_get_type(void); #define GNC_BUDGET_MAX_NUM_PERIODS_DIGITS 3 // max num periods == 999 +#define GNC_BUDGET_NOTES_PATH "notes" + gboolean gnc_budget_register(void); /** @@ -150,6 +152,11 @@ gnc_numeric gnc_budget_get_account_period_value( gnc_numeric gnc_budget_get_account_period_actual_value( const GncBudget *budget, Account *account, guint period_num); +void gnc_budget_set_account_period_note(GncBudget *budget, + const Account *account, guint period_num, const gchar *note); +const gchar *gnc_budget_get_account_period_note(const GncBudget *budget, + const Account *account, guint period_num); + /* Returns some budget in the book, or NULL. */ GncBudget* gnc_budget_get_default(QofBook *book);