Skip to content

Commit

Permalink
Replace usage of SX kvp access with SX properties.
Browse files Browse the repository at this point in the history
  • Loading branch information
jralls committed Nov 4, 2013
1 parent db1b2ca commit 3a51d70
Show file tree
Hide file tree
Showing 11 changed files with 299 additions and 465 deletions.
124 changes: 43 additions & 81 deletions src/app-utils/gnc-sx-instance-model.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,9 @@ _get_vars_helper(Transaction *txn, void *var_hash_data)
{
GHashTable *var_hash = (GHashTable*)var_hash_data;
GList *split_list;
kvp_frame *kvpf;
kvp_value *kvp_val;
Split *s;
char *str;
gchar *credit_formula;
gchar *debit_formula;
gnc_commodity *first_cmdty = NULL;

split_list = xaccTransGetSplitList(txn);
Expand All @@ -195,12 +194,12 @@ _get_vars_helper(Transaction *txn, void *var_hash_data)
Account *acct;

s = (Split*)split_list->data;
kvpf = xaccSplitGetSlots(s);
kvp_val = kvp_frame_get_slot_path(kvpf,
GNC_SX_ID,
GNC_SX_ACCOUNT,
NULL);
acct_guid = kvp_value_get_guid(kvp_val);

qof_instance_get (QOF_INSTANCE (s),
"sx-account", &acct_guid,
"sx-credit-formula", &credit_formula,
"sx-debit-formula", &debit_formula,
NULL);
acct = xaccAccountLookup(acct_guid, gnc_get_current_book());
split_cmdty = xaccAccountGetCommodity(acct);
if (first_cmdty == NULL)
Expand All @@ -226,31 +225,16 @@ _get_vars_helper(Transaction *txn, void *var_hash_data)
}

// existing... ------------------------------------------
kvp_val = kvp_frame_get_slot_path(kvpf,
GNC_SX_ID,
GNC_SX_CREDIT_FORMULA,
NULL);
if (kvp_val != NULL)
{
str = kvp_value_get_string(kvp_val);
if (str && strlen(str) != 0)
{
gnc_sx_parse_vars_from_formula(str, var_hash, NULL);
}
}

kvp_val = kvp_frame_get_slot_path(kvpf,
GNC_SX_ID,
GNC_SX_DEBIT_FORMULA,
NULL);
if (kvp_val != NULL)
{
str = kvp_value_get_string(kvp_val);
if (str && strlen(str) != 0)
{
gnc_sx_parse_vars_from_formula(str, var_hash, NULL);
}
}
if (credit_formula && strlen(credit_formula) != 0)
{
gnc_sx_parse_vars_from_formula(credit_formula, var_hash, NULL);
}
if (debit_formula && strlen(debit_formula) != 0)
{
gnc_sx_parse_vars_from_formula(debit_formula, var_hash, NULL);
}
g_free (credit_formula);
g_free (debit_formula);
}

return 0;
Expand Down Expand Up @@ -903,28 +887,9 @@ static gboolean
_get_template_split_account(const SchedXaction* sx, const Split *template_split, Account **split_acct, GList **creation_errors)
{
GncGUID *acct_guid;
kvp_frame *split_kvpf;
kvp_value *kvp_val;

split_kvpf = xaccSplitGetSlots(template_split);
/* contains the guid of the split's actual account. */
kvp_val = kvp_frame_get_slot_path(split_kvpf,
GNC_SX_ID,
GNC_SX_ACCOUNT,
NULL);
if (kvp_val == NULL)
{
GString *err = g_string_new("");
g_string_printf(err, "Null account kvp value for SX [%s], cancelling creation.",
xaccSchedXactionGetName(sx));
g_critical("%s", err->str);
if (creation_errors != NULL)
*creation_errors = g_list_append(*creation_errors, err);
else
g_string_free(err, TRUE);
return FALSE;
}
acct_guid = kvp_value_get_guid( kvp_val );
qof_instance_get (QOF_INSTANCE (template_split),
"sx-account", &acct_guid,
NULL);
*split_acct = xaccAccountLookup(acct_guid, gnc_get_current_book());
if (*split_acct == NULL)
{
Expand All @@ -948,18 +913,13 @@ _get_template_split_account(const SchedXaction* sx, const Split *template_split,
static void
_get_sx_formula_value(const SchedXaction* sx, const Split *template_split, gnc_numeric *numeric, GList **creation_errors, const char *formula_key, const char* numeric_key, GHashTable *variable_bindings)
{
kvp_frame *split_kvpf;
kvp_value *kvp_val;
char *formula_str, *parseErrorLoc;

split_kvpf = xaccSplitGetSlots(template_split);
char *formula_str, *parseErrorLoc;
qof_instance_get (QOF_INSTANCE (template_split),
formula_key, &formula_str,
numeric_key, &numeric,
NULL);

/* First look up the gnc_numeric value in the template split */
kvp_val = kvp_frame_get_slot_path(split_kvpf,
GNC_SX_ID,
numeric_key,
NULL);
*numeric = kvp_value_get_numeric(kvp_val);
if ((gnc_numeric_check(*numeric) == GNC_ERROR_OK)
&& !gnc_numeric_zero_p(*numeric))
{
Expand All @@ -969,11 +929,6 @@ _get_sx_formula_value(const SchedXaction* sx, const Split *template_split, gnc_n
return;
}

kvp_val = kvp_frame_get_slot_path(split_kvpf,
GNC_SX_ID,
formula_key,
NULL);
formula_str = kvp_value_get_string(kvp_val);
if (formula_str != NULL && strlen(formula_str) != 0)
{
GHashTable *parser_vars = NULL;
Expand Down Expand Up @@ -1010,13 +965,17 @@ _get_sx_formula_value(const SchedXaction* sx, const Split *template_split, gnc_n
static void
_get_credit_formula_value(GncSxInstance *instance, const Split *template_split, gnc_numeric *credit_num, GList **creation_errors)
{
_get_sx_formula_value(instance->parent->sx, template_split, credit_num, creation_errors, GNC_SX_CREDIT_FORMULA, GNC_SX_CREDIT_NUMERIC, instance->variable_bindings);
_get_sx_formula_value(instance->parent->sx, template_split, credit_num,
creation_errors, "sx-credit-formula",
"sx-credit-numeric", instance->variable_bindings);
}

static void
_get_debit_formula_value(GncSxInstance *instance, const Split *template_split, gnc_numeric *debit_num, GList **creation_errors)
{
_get_sx_formula_value(instance->parent->sx, template_split, debit_num, creation_errors, GNC_SX_DEBIT_FORMULA, GNC_SX_DEBIT_NUMERIC, instance->variable_bindings);
_get_sx_formula_value(instance->parent->sx, template_split, debit_num,
creation_errors, "sx-debit-formula",
"sx-debit-numeric", instance->variable_bindings);
}

static gboolean
Expand Down Expand Up @@ -1087,9 +1046,6 @@ create_each_transaction_helper(Transaction *template_txn, void *user_data)
break;
}

/* clear out any copied Split frame data. */
qof_instance_set_slots(QOF_INSTANCE(copying_split), kvp_frame_new());

split_cmdty = xaccAccountGetCommodity(split_acct);
if (first_cmdty == NULL)
{
Expand Down Expand Up @@ -1212,9 +1168,10 @@ create_each_transaction_helper(Transaction *template_txn, void *user_data)
}

{
kvp_frame *txn_frame;
txn_frame = xaccTransGetSlots(new_txn);
kvp_frame_set_guid(txn_frame, "from-sched-xaction", xaccSchedXactionGetGUID(creation_data->instance->parent->sx));
qof_instance_set (QOF_INSTANCE (new_txn),
"from-sched-xaction",
xaccSchedXactionGetGUID(creation_data->instance->parent->sx),
NULL);
}

xaccTransCommitEdit(new_txn);
Expand Down Expand Up @@ -1617,9 +1574,14 @@ create_cashflow_helper(Transaction *template_txn, void *user_data)
gint gncn_error;

/* Credit value */
_get_sx_formula_value(creation_data->sx, template_split, &credit_num, creation_data->creation_errors, GNC_SX_CREDIT_FORMULA, GNC_SX_CREDIT_NUMERIC, NULL);
_get_sx_formula_value(creation_data->sx, template_split,
&credit_num, creation_data->creation_errors,
"sx-credit-formula", "sx-credit-numeric",
NULL);
/* Debit value */
_get_sx_formula_value(creation_data->sx, template_split, &debit_num, creation_data->creation_errors, GNC_SX_DEBIT_FORMULA, GNC_SX_DEBIT_NUMERIC, NULL);
_get_sx_formula_value(creation_data->sx, template_split,
&debit_num, creation_data->creation_errors,
"sx-debit-formula", "sx-debit-numeric", NULL);

/* The resulting cash flow number: debit minus credit,
* multiplied with the count factor. */
Expand Down
5 changes: 0 additions & 5 deletions src/engine/SchedXaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -304,12 +304,7 @@ void gnc_sx_remove_defer_instance( SchedXaction *sx, void *deferStateData );
GList *gnc_sx_get_defer_instances( SchedXaction *sx );

/* #defines for KvpFrame strings and QOF */
#define GNC_SX_ID "sched-xaction"
#define GNC_SX_CREDIT_NUMERIC "credit-numeric"
#define GNC_SX_DEBIT_NUMERIC "debit-numeric"
#define GNC_SX_SHARES "shares"
#define GNC_SX_AMOUNT "amnt"
#define GNC_SX_FROM_SCHED_XACTION "from-sched-xaction"
#define GNC_SX_FREQ_SPEC "scheduled-frequency"
#define GNC_SX_NAME "sched-xname"
#define GNC_SX_START_DATE "sched-start-date"
Expand Down
7 changes: 7 additions & 0 deletions src/engine/test/test-engine-kvp-properties.c
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,13 @@ test_split_kvp_properties (Fixture *fixture, gconstpointer pData)
g_free (debit_numeric_r);
g_free (credit_formula_r);
g_free (credit_numeric_r);
qof_instance_set (QOF_INSTANCE (fixture->split),
"sx-credit-formula", NULL,
NULL);
qof_instance_get (QOF_INSTANCE (fixture->split),
"sx-credit-formula", &credit_numeric_r,
NULL);
g_assert (credit_numeric_r == NULL);
g_free (sx_shares_r);
g_free (online_id_r);
guid_free (sx_account);
Expand Down
51 changes: 24 additions & 27 deletions src/gnome-utils/gnc-tree-util-split-reg.c
Original file line number Diff line number Diff line change
Expand Up @@ -397,30 +397,20 @@ const char *
gnc_tree_util_split_reg_template_get_transfer_entry (Split *split)
{
static char *name = NULL;
Account *account;
GncGUID *guid;

kvp_frame *kvpf;
/* Callers either g_strdup the return or use it as a temp for comparison,
so we keep our static ref and free it on every call. */
g_free (name);

if (!split)
return NULL;

kvpf = xaccSplitGetSlots (split);

g_free (name);

if (kvpf)
{
Account *account;
GncGUID *guid;

guid = kvp_value_get_guid(
kvp_frame_get_slot_path (kvpf, "sched-xaction", "account", NULL));

account = xaccAccountLookup (guid, gnc_get_current_book ());

name = account ? gnc_get_account_name_for_register (account) : NULL;
}
else
name = NULL;
qof_instance_get (QOF_INSTANCE (split),
"sx-account", &guid,
NULL);
account = xaccAccountLookup (guid, gnc_get_current_book ());
name = account ? gnc_get_account_name_for_register (account) : NULL;

return name;
}
Expand All @@ -429,20 +419,27 @@ gnc_tree_util_split_reg_template_get_transfer_entry (Split *split)
const char *
gnc_tree_util_split_reg_template_get_fdebt_entry (Split *split)
{
kvp_frame *kvpf = xaccSplitGetSlots (split);
gchar *formula;

return kvp_value_get_string(
kvp_frame_get_slot_path (kvpf, "sched-xaction", "debit-formula", NULL));
}
g_return_val_if_fail (split != NULL, NULL);
qof_instance_get (QOF_INSTANCE (split),
"sx-debit-formula", &formula,
NULL);

return formula;
}

const char *
gnc_tree_util_split_reg_template_get_fcred_entry (Split *split)
{
kvp_frame *kvpf = xaccSplitGetSlots (split);
gchar *formula;

g_return_val_if_fail (split != NULL, NULL);
qof_instance_get (QOF_INSTANCE (split),
"sx-credit-formula", &formula,
NULL);

return kvp_value_get_string(
kvp_frame_get_slot_path (kvpf, "sched-xaction", "credit-formula", NULL));
return formula;
}


Expand Down
Loading

0 comments on commit 3a51d70

Please sign in to comment.