Merge branch 'maint-budget' into maint #1468

This commit is contained in:
Christopher Lam 2022-11-23 08:30:34 +08:00
commit f13cfa2142
6 changed files with 20 additions and 33 deletions

View File

@ -131,8 +131,6 @@ functions. */
%newobject gnc_localtime;
%newobject gnc_gmtime;
%newobject gnc_budget_get_account_period_note;
/* Parse the header file to generate wrappers */
%inline {
static QofIdType QOF_ID_BOOK_SCM (void) { return QOF_ID_BOOK; }

View File

@ -927,7 +927,7 @@ query_tooltip_tree_view_cb (GtkWidget *widget, gint x, gint y,
GncBudgetViewPrivate *priv = GNC_BUDGET_VIEW_GET_PRIVATE(view);
GtkTreePath *path = NULL;
GtkTreeViewColumn *column = NULL;
gchar *note;
const gchar *note;
guint period_num;
Account *account;
@ -964,7 +964,6 @@ query_tooltip_tree_view_cb (GtkWidget *widget, gint x, gint y,
gtk_tooltip_set_text (tooltip, note);
gtk_tree_view_set_tooltip_cell (tree_view, tooltip, path, column, NULL);
gtk_tree_path_free (path);
g_free (note);
return TRUE;
}
@ -1093,7 +1092,7 @@ budget_col_source (Account *account, GtkTreeViewColumn *col,
guint period_num;
gnc_numeric numeric;
gchar amtbuff[100]; //FIXME: overkill, where's the #define?
gchar *note;
const gchar *note;
budget_view = GNC_BUDGET_VIEW(g_object_get_data (G_OBJECT(col), "budget_view"));
period_num = GPOINTER_TO_UINT(g_object_get_data (G_OBJECT(col), "period_num"));
@ -1153,7 +1152,6 @@ budget_col_source (Account *account, GtkTreeViewColumn *col,
note = gnc_budget_get_account_period_note (priv->budget, account, period_num);
g_object_set (cell, "flagged", note != NULL, NULL);
g_free (note);
return g_strdup (amtbuff);
}

View File

@ -1287,9 +1287,8 @@ gnc_plugin_page_budget_cmd_budget_note(GtkAction *action,
GTK_WINDOW(gnc_plugin_page_get_window(GNC_PLUGIN_PAGE(page))));
note = GTK_WIDGET(gtk_builder_get_object(builder, "BudgetNote"));
txt = gnc_budget_get_account_period_note(priv->budget, acc, period_num);
xxxgtk_textview_set_text(GTK_TEXT_VIEW(note), txt);
g_free (txt);
xxxgtk_textview_set_text(GTK_TEXT_VIEW(note),
gnc_budget_get_account_period_note(priv->budget, acc, period_num));
gtk_widget_show_all(dialog);
result = gtk_dialog_run(GTK_DIALOG(dialog));

View File

@ -23,10 +23,6 @@
\********************************************************************/
#include <config.h>
#include <glib.h>
#include <glib/gprintf.h>
#include <glib/gi18n.h>
#include <time.h>
#include <qof.h>
#include <qofbookslots.h>
#include <qofinstance-p.h>
@ -67,6 +63,11 @@ struct PeriodData
std::string note;
bool value_is_set;
gnc_numeric value;
PeriodData () = default;
PeriodData (const char* note, bool value_is_set, gnc_numeric value)
: note (note)
, value_is_set (value_is_set)
, value (value) {};
};
using PeriodDataVec = std::vector<PeriodData>;
@ -470,6 +471,7 @@ gnc_budget_set_num_periods(GncBudget* budget, guint num_periods)
GncBudgetPrivate* priv;
g_return_if_fail(GNC_IS_BUDGET(budget));
g_return_if_fail(num_periods > 0);
priv = GET_PRIVATE(budget);
if ( priv->num_periods == num_periods ) return;
@ -642,13 +644,13 @@ gnc_budget_set_account_period_note(GncBudget *budget, const Account *account,
}
gchar *
const gchar *
gnc_budget_get_account_period_note (const GncBudget *budget,
const Account *account, guint period_num)
{
g_return_val_if_fail (period_num < GET_PRIVATE(budget)->num_periods, nullptr);
auto& data = get_perioddata (budget, account, period_num);
return data.note.empty () ? nullptr : g_strdup (data.note.c_str());
return data.note.empty () ? nullptr : data.note.c_str();
}
time64
@ -683,35 +685,27 @@ get_perioddata (const GncBudget *budget, const Account *account, guint period_nu
if (period_num >= priv->num_periods)
throw std::out_of_range("period_num >= num_periods");
auto& map = priv->acct_map;
auto map_iter = map->find (account);
auto& vec = priv->acct_map->operator[](account);
if (map_iter == map->end ())
if (vec.empty())
{
auto budget_kvp { QOF_INSTANCE (budget)->kvp_data };
PeriodDataVec vec {};
vec.reserve (priv->num_periods);
for (guint i = 0; i < priv->num_periods; i++)
{
std::string note;
auto kval1 { budget_kvp->get_slot (make_period_data_path (account, i)) };
auto kval2 { budget_kvp->get_slot (make_period_note_path (account, i)) };
auto is_set = kval1 && kval1->get_type() == KvpValue::Type::NUMERIC;
auto num = is_set ? kval1->get<gnc_numeric>() : gnc_numeric_zero ();
auto note = (kval2 && kval2->get_type() == KvpValue::Type::STRING) ?
kval2->get<const char*>() : "";
if (kval2 && kval2->get_type() == KvpValue::Type::STRING)
note = kval2->get<const char*>();
PeriodData data { std::move (note), is_set, num };
vec.push_back (std::move(data));
vec.emplace_back (note, is_set, num);
}
map_iter = map->insert_or_assign(account, std::move(vec)).first;
}
auto& vec = map_iter->second;
return vec.at(period_num);
}

View File

@ -161,11 +161,10 @@ 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);
/* get/set the budget account period's note, beware when retrieving
the period note, the latter must be g_freed by the caller */
/* get/set the budget account period's note */
void gnc_budget_set_account_period_note(GncBudget *budget,
const Account *account, guint period_num, const gchar *note);
gchar *gnc_budget_get_account_period_note (const GncBudget *budget,
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. */

View File

@ -78,7 +78,7 @@ test_gnc_set_budget_num_periods_data_retention ()
QofBook *book = qof_book_new();
GncBudget* budget = gnc_budget_new(book);
Account *acc = gnc_account_create_root(book);
gchar *note;
const gchar *note;
/* initially has 20 periods */
gnc_budget_set_num_periods(budget, 20);
@ -100,7 +100,6 @@ test_gnc_set_budget_num_periods_data_retention ()
g_assert (!gnc_budget_is_account_period_value_set(budget, acc, 15));
note = gnc_budget_get_account_period_note (budget, acc, 11);
g_assert_cmpstr (note, ==, NULL);
g_free (note);
gnc_budget_destroy(budget);
qof_book_destroy(book);