Replace usage of SX kvp access with SX properties.

This commit is contained in:
John Ralls 2013-10-21 15:16:45 -07:00
parent db1b2caebf
commit 3a51d704c8
11 changed files with 285 additions and 451 deletions

View File

@ -176,10 +176,9 @@ _get_vars_helper(Transaction *txn, void *var_hash_data)
{ {
GHashTable *var_hash = (GHashTable*)var_hash_data; GHashTable *var_hash = (GHashTable*)var_hash_data;
GList *split_list; GList *split_list;
kvp_frame *kvpf;
kvp_value *kvp_val;
Split *s; Split *s;
char *str; gchar *credit_formula;
gchar *debit_formula;
gnc_commodity *first_cmdty = NULL; gnc_commodity *first_cmdty = NULL;
split_list = xaccTransGetSplitList(txn); split_list = xaccTransGetSplitList(txn);
@ -195,12 +194,12 @@ _get_vars_helper(Transaction *txn, void *var_hash_data)
Account *acct; Account *acct;
s = (Split*)split_list->data; s = (Split*)split_list->data;
kvpf = xaccSplitGetSlots(s);
kvp_val = kvp_frame_get_slot_path(kvpf, qof_instance_get (QOF_INSTANCE (s),
GNC_SX_ID, "sx-account", &acct_guid,
GNC_SX_ACCOUNT, "sx-credit-formula", &credit_formula,
NULL); "sx-debit-formula", &debit_formula,
acct_guid = kvp_value_get_guid(kvp_val); NULL);
acct = xaccAccountLookup(acct_guid, gnc_get_current_book()); acct = xaccAccountLookup(acct_guid, gnc_get_current_book());
split_cmdty = xaccAccountGetCommodity(acct); split_cmdty = xaccAccountGetCommodity(acct);
if (first_cmdty == NULL) if (first_cmdty == NULL)
@ -226,31 +225,16 @@ _get_vars_helper(Transaction *txn, void *var_hash_data)
} }
// existing... ------------------------------------------ // existing... ------------------------------------------
kvp_val = kvp_frame_get_slot_path(kvpf, if (credit_formula && strlen(credit_formula) != 0)
GNC_SX_ID, {
GNC_SX_CREDIT_FORMULA, gnc_sx_parse_vars_from_formula(credit_formula, var_hash, NULL);
NULL); }
if (kvp_val != NULL) if (debit_formula && strlen(debit_formula) != 0)
{ {
str = kvp_value_get_string(kvp_val); gnc_sx_parse_vars_from_formula(debit_formula, var_hash, NULL);
if (str && strlen(str) != 0) }
{ g_free (credit_formula);
gnc_sx_parse_vars_from_formula(str, var_hash, NULL); g_free (debit_formula);
}
}
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);
}
}
} }
return 0; return 0;
@ -903,28 +887,9 @@ static gboolean
_get_template_split_account(const SchedXaction* sx, const Split *template_split, Account **split_acct, GList **creation_errors) _get_template_split_account(const SchedXaction* sx, const Split *template_split, Account **split_acct, GList **creation_errors)
{ {
GncGUID *acct_guid; GncGUID *acct_guid;
kvp_frame *split_kvpf; qof_instance_get (QOF_INSTANCE (template_split),
kvp_value *kvp_val; "sx-account", &acct_guid,
NULL);
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 );
*split_acct = xaccAccountLookup(acct_guid, gnc_get_current_book()); *split_acct = xaccAccountLookup(acct_guid, gnc_get_current_book());
if (*split_acct == NULL) if (*split_acct == NULL)
{ {
@ -948,18 +913,13 @@ _get_template_split_account(const SchedXaction* sx, const Split *template_split,
static void 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) _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; char *formula_str, *parseErrorLoc;
qof_instance_get (QOF_INSTANCE (template_split),
formula_key, &formula_str,
numeric_key, &numeric,
NULL);
split_kvpf = xaccSplitGetSlots(template_split);
/* 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) if ((gnc_numeric_check(*numeric) == GNC_ERROR_OK)
&& !gnc_numeric_zero_p(*numeric)) && !gnc_numeric_zero_p(*numeric))
{ {
@ -969,11 +929,6 @@ _get_sx_formula_value(const SchedXaction* sx, const Split *template_split, gnc_n
return; 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) if (formula_str != NULL && strlen(formula_str) != 0)
{ {
GHashTable *parser_vars = NULL; GHashTable *parser_vars = NULL;
@ -1010,13 +965,17 @@ _get_sx_formula_value(const SchedXaction* sx, const Split *template_split, gnc_n
static void static void
_get_credit_formula_value(GncSxInstance *instance, const Split *template_split, gnc_numeric *credit_num, GList **creation_errors) _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 static void
_get_debit_formula_value(GncSxInstance *instance, const Split *template_split, gnc_numeric *debit_num, GList **creation_errors) _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 static gboolean
@ -1087,9 +1046,6 @@ create_each_transaction_helper(Transaction *template_txn, void *user_data)
break; break;
} }
/* clear out any copied Split frame data. */
qof_instance_set_slots(QOF_INSTANCE(copying_split), kvp_frame_new());
split_cmdty = xaccAccountGetCommodity(split_acct); split_cmdty = xaccAccountGetCommodity(split_acct);
if (first_cmdty == NULL) if (first_cmdty == NULL)
{ {
@ -1212,9 +1168,10 @@ create_each_transaction_helper(Transaction *template_txn, void *user_data)
} }
{ {
kvp_frame *txn_frame; qof_instance_set (QOF_INSTANCE (new_txn),
txn_frame = xaccTransGetSlots(new_txn); "from-sched-xaction",
kvp_frame_set_guid(txn_frame, "from-sched-xaction", xaccSchedXactionGetGUID(creation_data->instance->parent->sx)); xaccSchedXactionGetGUID(creation_data->instance->parent->sx),
NULL);
} }
xaccTransCommitEdit(new_txn); xaccTransCommitEdit(new_txn);
@ -1617,9 +1574,14 @@ create_cashflow_helper(Transaction *template_txn, void *user_data)
gint gncn_error; gint gncn_error;
/* Credit value */ /* 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 */ /* 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, /* The resulting cash flow number: debit minus credit,
* multiplied with the count factor. */ * multiplied with the count factor. */

View File

@ -304,12 +304,7 @@ void gnc_sx_remove_defer_instance( SchedXaction *sx, void *deferStateData );
GList *gnc_sx_get_defer_instances( SchedXaction *sx ); GList *gnc_sx_get_defer_instances( SchedXaction *sx );
/* #defines for KvpFrame strings and QOF */ /* #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_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_FREQ_SPEC "scheduled-frequency"
#define GNC_SX_NAME "sched-xname" #define GNC_SX_NAME "sched-xname"
#define GNC_SX_START_DATE "sched-start-date" #define GNC_SX_START_DATE "sched-start-date"

View File

@ -263,6 +263,13 @@ test_split_kvp_properties (Fixture *fixture, gconstpointer pData)
g_free (debit_numeric_r); g_free (debit_numeric_r);
g_free (credit_formula_r); g_free (credit_formula_r);
g_free (credit_numeric_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 (sx_shares_r);
g_free (online_id_r); g_free (online_id_r);
guid_free (sx_account); guid_free (sx_account);

View File

@ -397,30 +397,20 @@ const char *
gnc_tree_util_split_reg_template_get_transfer_entry (Split *split) gnc_tree_util_split_reg_template_get_transfer_entry (Split *split)
{ {
static char *name = NULL; 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) if (!split)
return NULL; return NULL;
qof_instance_get (QOF_INSTANCE (split),
kvpf = xaccSplitGetSlots (split); "sx-account", &guid,
NULL);
g_free (name); account = xaccAccountLookup (guid, gnc_get_current_book ());
name = account ? gnc_get_account_name_for_register (account) : NULL;
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;
return name; return name;
} }
@ -429,20 +419,27 @@ gnc_tree_util_split_reg_template_get_transfer_entry (Split *split)
const char * const char *
gnc_tree_util_split_reg_template_get_fdebt_entry (Split *split) gnc_tree_util_split_reg_template_get_fdebt_entry (Split *split)
{ {
kvp_frame *kvpf = xaccSplitGetSlots (split); gchar *formula;
return kvp_value_get_string( g_return_val_if_fail (split != NULL, NULL);
kvp_frame_get_slot_path (kvpf, "sched-xaction", "debit-formula", NULL)); qof_instance_get (QOF_INSTANCE (split),
"sx-debit-formula", &formula,
NULL);
return formula;
} }
const char * const char *
gnc_tree_util_split_reg_template_get_fcred_entry (Split *split) gnc_tree_util_split_reg_template_get_fcred_entry (Split *split)
{ {
kvp_frame *kvpf = xaccSplitGetSlots (split); gchar *formula;
return kvp_value_get_string( g_return_val_if_fail (split != NULL, NULL);
kvp_frame_get_slot_path (kvpf, "sched-xaction", "credit-formula", NULL)); qof_instance_get (QOF_INSTANCE (split),
"sx-credit-formula", &formula,
NULL);
return formula;
} }

View File

@ -4803,9 +4803,8 @@ gtv_sr_edited_template_cb (GtkCellRendererText *cell, const gchar *path_string,
if (viewcol == COL_TRANSFERVOID) if (viewcol == COL_TRANSFERVOID)
{ {
Account *template_acc; Account *template_acc;
Account *acct;
const GncGUID *acctGUID; const GncGUID *acctGUID;
kvp_frame *kvpf;
Account *acct;
/* save the account GncGUID into the kvp_data. */ /* save the account GncGUID into the kvp_data. */
view->priv->stop_cell_move = FALSE; view->priv->stop_cell_move = FALSE;
@ -4824,9 +4823,9 @@ gtv_sr_edited_template_cb (GtkCellRendererText *cell, const gchar *path_string,
} }
acctGUID = xaccAccountGetGUID (acct); acctGUID = xaccAccountGetGUID (acct);
kvpf = xaccSplitGetSlots (split); qof_instance_set (QOF_INSTANCE (split),
kvp_frame_set_slot_path (kvpf, kvp_value_new_guid (acctGUID), "sx-account", acctGUID,
GNC_SX_ID, GNC_SX_ACCOUNT, NULL); NULL);
template_acc = gnc_tree_model_split_reg_get_template_account (model); template_acc = gnc_tree_model_split_reg_get_template_account (model);
@ -4846,20 +4845,11 @@ gtv_sr_edited_template_cb (GtkCellRendererText *cell, const gchar *path_string,
/* Setup the debit and credit fields */ /* Setup the debit and credit fields */
if (viewcol == COL_DEBIT) if (viewcol == COL_DEBIT)
{ {
kvp_frame *kvpf;
char *error_loc; char *error_loc;
gnc_numeric new_value; gnc_numeric new_value;
gboolean parse_result; gboolean parse_result;
kvpf = xaccSplitGetSlots (split);
DEBUG ("kvp_frame debit before: %s\n", kvp_frame_to_string (kvpf));
/* Setup the debit formula */ /* Setup the debit formula */
kvp_frame_set_slot_path (kvpf, kvp_value_new_string (new_text),
GNC_SX_ID,
GNC_SX_DEBIT_FORMULA,
NULL);
/* If the value can be parsed into a numeric result, store that /* If the value can be parsed into a numeric result, store that
* numeric value additionally. See above comment.*/ * numeric value additionally. See above comment.*/
@ -4868,45 +4858,22 @@ gtv_sr_edited_template_cb (GtkCellRendererText *cell, const gchar *path_string,
{ {
new_value = gnc_numeric_zero(); new_value = gnc_numeric_zero();
} }
kvp_frame_set_slot_path (kvpf, kvp_value_new_numeric (new_value), qof_instance_set (QOF_INSTANCE (split),
GNC_SX_ID, "sx-debit-formula", new_text,
GNC_SX_DEBIT_NUMERIC, "sx-debit-numeric", &new_value,
NULL); "sx-credit-formula", NULL,
"sx-credit-numeric", NULL,
DEBUG ("kvp_frame debit after: %s\n", kvp_frame_to_string (kvpf)); NULL);
/* Blank the credit formula */
kvp_frame_set_slot_path (kvpf, kvp_value_new_string (NULL),
GNC_SX_ID,
GNC_SX_CREDIT_FORMULA,
NULL);
new_value = gnc_numeric_zero();
kvp_frame_set_slot_path (kvpf, kvp_value_new_numeric (new_value),
GNC_SX_ID,
GNC_SX_CREDIT_NUMERIC,
NULL);
} }
/* Setup the debit and credit fields */ /* Setup the debit and credit fields */
if (viewcol == COL_CREDIT) if (viewcol == COL_CREDIT)
{ {
kvp_frame *kvpf;
char *error_loc; char *error_loc;
gnc_numeric new_value; gnc_numeric new_value;
gboolean parse_result; gboolean parse_result;
kvpf = xaccSplitGetSlots (split); /* If the value can be parsed into a numeric result (without any
DEBUG ("kvp_frame credit before: %s\n", kvp_frame_to_string (kvpf));
/* Setup the credit formula */
kvp_frame_set_slot_path (kvpf, kvp_value_new_string (new_text),
GNC_SX_ID,
GNC_SX_CREDIT_FORMULA,
NULL);
/* If the value can be parsed into a numeric result (without any
* further variable definitions), store that numeric value * further variable definitions), store that numeric value
* additionally in the kvp. Otherwise store a zero numeric * additionally in the kvp. Otherwise store a zero numeric
* there.*/ * there.*/
@ -4915,24 +4882,12 @@ gtv_sr_edited_template_cb (GtkCellRendererText *cell, const gchar *path_string,
{ {
new_value = gnc_numeric_zero(); new_value = gnc_numeric_zero();
} }
kvp_frame_set_slot_path (kvpf, kvp_value_new_numeric (new_value), qof_instance_set (QOF_INSTANCE (split),
GNC_SX_ID, "sx-credit-formula", new_text,
GNC_SX_CREDIT_NUMERIC, "sx-credit-numeric", &new_value,
NULL); "sx-debit-formula", NULL,
"sx-debit-numeric", NULL,
DEBUG ("kvp_frame credit after: %s\n", kvp_frame_to_string (kvpf)); NULL);
/* Blank the debit formula */
kvp_frame_set_slot_path (kvpf, kvp_value_new_string (NULL),
GNC_SX_ID,
GNC_SX_DEBIT_FORMULA,
NULL);
new_value = gnc_numeric_zero();
kvp_frame_set_slot_path (kvpf, kvp_value_new_numeric (new_value),
GNC_SX_ID,
GNC_SX_DEBIT_NUMERIC,
NULL);
} }
/* set the amount to an innocuous value */ /* set the amount to an innocuous value */
xaccSplitSetValue (split, gnc_numeric_create (0, 1)); xaccSplitSetValue (split, gnc_numeric_create (0, 1));

View File

@ -549,9 +549,7 @@ gnc_sxed_check_consistent( GncSxEditorDialog *sxed )
int numIters, i; int numIters, i;
GHashTable *vars, *txns; GHashTable *vars, *txns;
GList *splitList = NULL; GList *splitList = NULL;
char *str; gchar *credit_formula, *debit_formula;
kvp_frame *f;
kvp_value *v;
Split *s; Split *s;
Transaction *t; Transaction *t;
gnc_numeric tmp; gnc_numeric tmp;
@ -559,8 +557,10 @@ gnc_sxed_check_consistent( GncSxEditorDialog *sxed )
gpointer unusedKey, unusedValue; gpointer unusedKey, unusedValue;
unbalanceable = FALSE; /* innocent until proven guilty */ unbalanceable = FALSE; /* innocent until proven guilty */
vars = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, (GDestroyNotify)gnc_sx_variable_free); vars = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
txns = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, g_free); (GDestroyNotify)gnc_sx_variable_free);
txns = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL,
g_free);
numIters = NUM_ITERS_NO_VARS; numIters = NUM_ITERS_NO_VARS;
/** /**
* Plan: * Plan:
@ -622,14 +622,11 @@ gnc_sxed_check_consistent( GncSxEditorDialog *sxed )
g_hash_table_insert( txns, (gpointer)t, (gpointer)tcds ); g_hash_table_insert( txns, (gpointer)t, (gpointer)tcds );
} }
f = xaccSplitGetSlots( s ); qof_instance_get (QOF_INSTANCE (s),
"sx-account", &acct_guid,
/* contains the guid of the split's actual account. */ "sx-credit-formula", &credit_formula,
v = kvp_frame_get_slot_path(f, "sx-debit-formula", &debit_formula,
GNC_SX_ID, NULL);
GNC_SX_ACCOUNT,
NULL);
acct_guid = kvp_value_get_guid( v );
acct = xaccAccountLookup( acct_guid, gnc_get_current_book ()); acct = xaccAccountLookup( acct_guid, gnc_get_current_book ());
split_cmdty = xaccAccountGetCommodity(acct); split_cmdty = xaccAccountGetCommodity(acct);
if (base_cmdty == NULL) if (base_cmdty == NULL)
@ -638,62 +635,48 @@ gnc_sxed_check_consistent( GncSxEditorDialog *sxed )
} }
multi_commodity |= !gnc_commodity_equal(split_cmdty, base_cmdty); multi_commodity |= !gnc_commodity_equal(split_cmdty, base_cmdty);
v = kvp_frame_get_slot_path( f, if ( g_strcmp0 (credit_formula, "") != 0 &&
GNC_SX_ID, gnc_sx_parse_vars_from_formula(credit_formula, vars,
GNC_SX_CREDIT_FORMULA, &tmp ) < 0 )
NULL ); {
if ( v GString *errStr;
&& (str = kvp_value_get_string(v))
&& strlen( str ) != 0 )
{
if ( gnc_sx_parse_vars_from_formula( str, vars, &tmp ) < 0 )
{
GString *errStr;
errStr = g_string_sized_new( 32 ); errStr = g_string_sized_new( 32 );
g_string_printf( errStr, g_string_printf( errStr,
_( "Couldn't parse credit formula for " _( "Couldn't parse credit formula for "
"split \"%s\"." ), "split \"%s\"." ),
xaccSplitGetMemo( s ) ); xaccSplitGetMemo( s ) );
gnc_error_dialog( GTK_WIDGET(sxed->dialog), "%s", gnc_error_dialog( GTK_WIDGET(sxed->dialog), "%s",
errStr->str ); errStr->str );
g_string_free( errStr, TRUE ); g_string_free( errStr, TRUE );
return FALSE; return FALSE;
} }
tcds->creditSum = tcds->creditSum =
gnc_numeric_add( tcds->creditSum, tmp, 100, gnc_numeric_add( tcds->creditSum, tmp, 100,
(GNC_DENOM_AUTO | GNC_HOW_DENOM_LCD) ); (GNC_DENOM_AUTO | GNC_HOW_DENOM_LCD) );
tmp = gnc_numeric_zero(); tmp = gnc_numeric_zero();
} if ( g_strcmp0 (debit_formula, "") != 0 &&
v = kvp_frame_get_slot_path( f, gnc_sx_parse_vars_from_formula( debit_formula, vars,
GNC_SX_ID, &tmp ) < 0 )
GNC_SX_DEBIT_FORMULA, {
NULL ); GString *errStr;
if ( v
&& (str = kvp_value_get_string(v))
&& strlen(str) != 0 )
{
if ( gnc_sx_parse_vars_from_formula( str, vars, &tmp ) < 0 )
{
GString *errStr;
errStr = g_string_sized_new( 32 ); errStr = g_string_sized_new( 32 );
g_string_printf( errStr, g_string_printf( errStr,
_( "Couldn't parse debit formula for " _( "Couldn't parse debit formula for "
"split \"%s\"." ), "split \"%s\"." ),
xaccSplitGetMemo( s ) ); xaccSplitGetMemo( s ) );
gnc_error_dialog( GTK_WIDGET(sxed->dialog), "%s", gnc_error_dialog( GTK_WIDGET(sxed->dialog), "%s",
(gchar*)errStr->str ); (gchar*)errStr->str );
g_string_free( errStr, TRUE ); g_string_free( errStr, TRUE );
return FALSE; return FALSE;
} }
tcds->debitSum = gnc_numeric_add( tcds->debitSum, tmp, 100, tcds->debitSum = gnc_numeric_add( tcds->debitSum, tmp, 100,
(GNC_DENOM_AUTO | GNC_HOW_DENOM_LCD) ); (GNC_DENOM_AUTO | GNC_HOW_DENOM_LCD) );
tmp = gnc_numeric_zero(); tmp = gnc_numeric_zero();
} }
}
g_hash_table_foreach( txns, g_hash_table_foreach( txns,
check_credit_debit_balance, check_credit_debit_balance,

View File

@ -546,9 +546,7 @@ gnc_sxed_check_consistent (GncSxEditorDialog2 *sxed)
int numIters, i; int numIters, i;
GHashTable *vars, *txns; GHashTable *vars, *txns;
GList *splitList = NULL; GList *splitList = NULL;
char *str; char *credit_formula, *debit_formula;
kvp_frame *f;
kvp_value *v;
Split *s; Split *s;
Transaction *t; Transaction *t;
gnc_numeric tmp; gnc_numeric tmp;
@ -615,77 +613,60 @@ gnc_sxed_check_consistent (GncSxEditorDialog2 *sxed)
g_hash_table_insert (txns, (gpointer)t, (gpointer)tcds); g_hash_table_insert (txns, (gpointer)t, (gpointer)tcds);
} }
f = xaccSplitGetSlots (s); qof_instance_get (QOF_INSTANCE (s),
"sx-account", &acct_guid,
/* contains the guid of the split's actual account. */ "sx-credit-formula", &credit_formula,
v = kvp_frame_get_slot_path (f, "sx-debit-formula", &debit_formula,
GNC_SX_ID, NULL);
GNC_SX_ACCOUNT, acct = xaccAccountLookup( acct_guid, gnc_get_current_book ());
NULL); split_cmdty = xaccAccountGetCommodity(acct);
acct_guid = kvp_value_get_guid (v);
acct = xaccAccountLookup (acct_guid, gnc_get_current_book ());
split_cmdty = xaccAccountGetCommodity (acct);
if (base_cmdty == NULL) if (base_cmdty == NULL)
{ {
base_cmdty = split_cmdty; base_cmdty = split_cmdty;
} }
multi_commodity |= !gnc_commodity_equal (split_cmdty, base_cmdty); multi_commodity |= !gnc_commodity_equal(split_cmdty, base_cmdty);
v = kvp_frame_get_slot_path (f, if ( g_strcmp0 (credit_formula, "") != 0 &&
GNC_SX_ID, gnc_sx_parse_vars_from_formula(credit_formula, vars,
GNC_SX_CREDIT_FORMULA, &tmp ) < 0 )
NULL); {
if (v GString *errStr;
&& (str = kvp_value_get_string (v))
&& strlen( str ) != 0)
{
if (gnc_sx_parse_vars_from_formula (str, vars, &tmp ) < 0)
{
GString *errStr;
errStr = g_string_sized_new (32); errStr = g_string_sized_new( 32 );
g_string_printf (errStr, g_string_printf( errStr,
_( "Couldn't parse credit formula for " _( "Couldn't parse credit formula for "
"split \"%s\"."), "split \"%s\"." ),
xaccSplitGetMemo (s)); xaccSplitGetMemo( s ) );
gnc_error_dialog (GTK_WIDGET (sxed->dialog), "%s", gnc_error_dialog( GTK_WIDGET(sxed->dialog), "%s",
errStr->str); errStr->str );
g_string_free (errStr, TRUE); g_string_free( errStr, TRUE );
return FALSE; return FALSE;
} }
tcds->creditSum = tcds->creditSum =
gnc_numeric_add (tcds->creditSum, tmp, 100, gnc_numeric_add( tcds->creditSum, tmp, 100,
(GNC_DENOM_AUTO | GNC_HOW_DENOM_LCD)); (GNC_DENOM_AUTO | GNC_HOW_DENOM_LCD) );
tmp = gnc_numeric_zero(); tmp = gnc_numeric_zero();
} if ( g_strcmp0 (debit_formula, "") != 0 &&
v = kvp_frame_get_slot_path (f, gnc_sx_parse_vars_from_formula( debit_formula, vars,
GNC_SX_ID, &tmp ) < 0 )
GNC_SX_DEBIT_FORMULA, {
NULL); GString *errStr;
if (v
&& (str = kvp_value_get_string (v))
&& strlen(str) != 0 )
{
if (gnc_sx_parse_vars_from_formula (str, vars, &tmp ) < 0)
{
GString *errStr;
errStr = g_string_sized_new (32); errStr = g_string_sized_new( 32 );
g_string_printf (errStr, g_string_printf( errStr,
_( "Couldn't parse debit formula for " _( "Couldn't parse debit formula for "
"split \"%s\"."), "split \"%s\"." ),
xaccSplitGetMemo (s)); xaccSplitGetMemo( s ) );
gnc_error_dialog (GTK_WIDGET (sxed->dialog), "%s", gnc_error_dialog( GTK_WIDGET(sxed->dialog), "%s",
(gchar*)errStr->str); (gchar*)errStr->str );
g_string_free (errStr, TRUE); g_string_free( errStr, TRUE );
return FALSE; return FALSE;
} }
tcds->debitSum = gnc_numeric_add (tcds->debitSum, tmp, 100, tcds->debitSum = gnc_numeric_add( tcds->debitSum, tmp, 100,
(GNC_DENOM_AUTO | GNC_HOW_DENOM_LCD)); (GNC_DENOM_AUTO | GNC_HOW_DENOM_LCD) );
tmp = gnc_numeric_zero(); tmp = gnc_numeric_zero();
}
} }
g_hash_table_foreach (txns, g_hash_table_foreach (txns,

View File

@ -3629,39 +3629,30 @@ gnc_plugin_page_register2_cmd_schedule (GtkAction *action,
/* If the transaction has a sched-xact KVP frame, then go to the editor /* If the transaction has a sched-xact KVP frame, then go to the editor
* for the existing SX; otherwise, do the sx-from-trans dialog. */ * for the existing SX; otherwise, do the sx-from-trans dialog. */
{ {
kvp_frame *txn_frame; GncGUID *fromSXId;
kvp_value *kvp_val; SchedXaction *theSX = NULL;
/* set a kvp-frame element in the transaction indicating and GList *sxElts;
* pointing-to the SX this was created from. */ qof_instance_get (QOF_INSTANCE (trans),
txn_frame = xaccTransGetSlots (trans); "from-sched-xaction", &fromSXId,
if ( txn_frame != NULL ) NULL);
{
kvp_val = kvp_frame_get_slot (txn_frame, "from-sched-xaction");
if (kvp_val)
{
GncGUID *fromSXId = kvp_value_get_guid (kvp_val);
SchedXaction *theSX = NULL;
GList *sxElts;
/* Get the correct SX */ /* Get the correct SX */
for ( sxElts = gnc_book_get_schedxactions (gnc_get_current_book())->sx_list; for ( sxElts = gnc_book_get_schedxactions (gnc_get_current_book())->sx_list;
(!theSX) && sxElts; (!theSX) && sxElts;
sxElts = sxElts->next ) sxElts = sxElts->next )
{ {
SchedXaction *sx = (SchedXaction*)sxElts->data; SchedXaction *sx = (SchedXaction*)sxElts->data;
theSX = theSX =
((guid_equal (xaccSchedXactionGetGUID (sx), fromSXId)) ((guid_equal (xaccSchedXactionGetGUID (sx), fromSXId))
? sx : NULL); ? sx : NULL);
} }
if (theSX) if (theSX)
{ {
gnc_ui_scheduled_xaction_editor_dialog_create2 (theSX, FALSE); gnc_ui_scheduled_xaction_editor_dialog_create2 (theSX, FALSE);
LEAVE(" "); LEAVE(" ");
return; return;
} }
}
}
} }
gnc_sx_create_from_trans (trans); gnc_sx_create_from_trans (trans);
LEAVE(" "); LEAVE(" ");

View File

@ -1204,38 +1204,29 @@ gsr_default_schedule_handler( GNCSplitReg *gsr, gpointer data )
/* If the transaction has a sched-xact KVP frame, then go to the editor /* If the transaction has a sched-xact KVP frame, then go to the editor
* for the existing SX; otherwise, do the sx-from-trans dialog. */ * for the existing SX; otherwise, do the sx-from-trans dialog. */
{ {
kvp_frame *txn_frame; GncGUID *fromSXId;
kvp_value *kvp_val; SchedXaction *theSX = NULL;
/* set a kvp-frame element in the transaction indicating and GList *sxElts;
* pointing-to the SX this was created from. */ qof_instance_get (QOF_INSTANCE (pending_trans),
txn_frame = xaccTransGetSlots( pending_trans ); "from-sched-xaction", &fromSXId,
if ( txn_frame != NULL ) NULL);
{
kvp_val = kvp_frame_get_slot( txn_frame, "from-sched-xaction" );
if ( kvp_val )
{
GncGUID *fromSXId = kvp_value_get_guid( kvp_val );
SchedXaction *theSX = NULL;
GList *sxElts;
/* Get the correct SX */ /* Get the correct SX */
for ( sxElts = gnc_book_get_schedxactions(gnc_get_current_book())->sx_list; for ( sxElts = gnc_book_get_schedxactions (gnc_get_current_book())->sx_list;
(!theSX) && sxElts; (!theSX) && sxElts;
sxElts = sxElts->next ) sxElts = sxElts->next )
{ {
SchedXaction *sx = (SchedXaction*)sxElts->data; SchedXaction *sx = (SchedXaction*)sxElts->data;
theSX = theSX =
( ( guid_equal( xaccSchedXactionGetGUID( sx ), fromSXId ) ) ((guid_equal (xaccSchedXactionGetGUID (sx), fromSXId))
? sx : NULL ); ? sx : NULL);
} }
if ( theSX ) if ( theSX )
{ {
gnc_ui_scheduled_xaction_editor_dialog_create(theSX, FALSE); gnc_ui_scheduled_xaction_editor_dialog_create(theSX, FALSE);
return; return;
} }
}
}
} }
gnc_sx_create_from_trans(pending_trans); gnc_sx_create_from_trans(pending_trans);

View File

@ -674,10 +674,9 @@ gnc_template_register_save_xfrm_cell (BasicCell * cell,
} }
acctGUID = xaccAccountGetGUID (acct); acctGUID = xaccAccountGetGUID (acct);
kvpf = xaccSplitGetSlots (sd->split); qof_instance_set (QOF_INSTANCE (sd->split),
kvp_frame_set_slot_path (kvpf, kvp_value_new_guid(acctGUID), "sx-account", acctGUID,
GNC_SX_ID, GNC_SX_ACCOUNT, NULL); NULL);
template_acc = xaccAccountLookup (&info->template_account, template_acc = xaccAccountLookup (&info->template_account,
gnc_get_current_book ()); gnc_get_current_book ());
@ -699,10 +698,9 @@ gnc_template_register_save_debcred_cell (BasicCell * cell,
{ {
SRSaveData *sd = save_data; SRSaveData *sd = save_data;
SplitRegister *reg = user_data; SplitRegister *reg = user_data;
kvp_frame *kvpf; const char *credit_formula, *debit_formula;
const char *value;
char *error_loc; char *error_loc;
gnc_numeric new_amount; gnc_numeric credit_amount, debit_amount;
gboolean parse_result; gboolean parse_result;
g_return_if_fail (gnc_basic_cell_has_name (cell, FDEBT_CELL) || g_return_if_fail (gnc_basic_cell_has_name (cell, FDEBT_CELL) ||
@ -711,54 +709,37 @@ gnc_template_register_save_debcred_cell (BasicCell * cell,
if (sd->handled_dc) if (sd->handled_dc)
return; return;
kvpf = xaccSplitGetSlots (sd->split);
DEBUG ("kvp_frame before: %s\n", kvp_frame_to_string (kvpf));
/* amountStr = gnc_numeric_to_string (new_amount); */ /* amountStr = gnc_numeric_to_string (new_amount); */
value = gnc_table_layout_get_cell_value (reg->table->layout, FCRED_CELL); credit_formula = gnc_table_layout_get_cell_value (reg->table->layout,
kvp_frame_set_slot_path (kvpf, kvp_value_new_string (value), FCRED_CELL);
GNC_SX_ID,
GNC_SX_CREDIT_FORMULA,
NULL);
/* If the value can be parsed into a numeric result (without any /* If the value can be parsed into a numeric result (without any
* further variable definitions), store that numeric value * further variable definitions), store that numeric value
* additionally in the kvp. Otherwise store a zero numeric * additionally in the kvp. Otherwise store a zero numeric
* there.*/ * there.*/
parse_result = gnc_exp_parser_parse_separate_vars(value, &new_amount, &error_loc, NULL); parse_result = gnc_exp_parser_parse_separate_vars(credit_formula,
&credit_amount,
&error_loc, NULL);
if (!parse_result) if (!parse_result)
{ credit_amount = gnc_numeric_zero();
new_amount = gnc_numeric_zero();
}
kvp_frame_set_slot_path (kvpf, kvp_value_new_numeric (new_amount),
GNC_SX_ID,
GNC_SX_CREDIT_NUMERIC,
NULL);
value = gnc_table_layout_get_cell_value (reg->table->layout, FDEBT_CELL); debit_formula = gnc_table_layout_get_cell_value (reg->table->layout,
FDEBT_CELL);
kvp_frame_set_slot_path (kvpf,
kvp_value_new_string (value),
GNC_SX_ID,
GNC_SX_DEBIT_FORMULA,
NULL);
/* If the value can be parsed into a numeric result, store that /* If the value can be parsed into a numeric result, store that
* numeric value additionally. See above comment.*/ * numeric value additionally. See above comment.*/
parse_result = gnc_exp_parser_parse_separate_vars(value, &new_amount, &error_loc, NULL); parse_result = gnc_exp_parser_parse_separate_vars(debit_formula,
&debit_amount,
&error_loc, NULL);
if (!parse_result) if (!parse_result)
{ debit_amount = gnc_numeric_zero();
new_amount = gnc_numeric_zero();
}
kvp_frame_set_slot_path (kvpf, kvp_value_new_numeric (new_amount),
GNC_SX_ID,
GNC_SX_DEBIT_NUMERIC,
NULL);
DEBUG ("kvp_frame after: %s\n", kvp_frame_to_string (kvpf));
qof_instance_set (QOF_INSTANCE (sd->split),
"sx-credit-formula", credit_formula,
"sx-credit-numeric", &credit_amount,
"sx-debit-formula", debit_formula,
"sx-debit-numeric", &debit_amount,
NULL);
/* set the amount to an innocuous value */ /* set the amount to an innocuous value */
xaccSplitSetValue (sd->split, gnc_numeric_create (0, 1)); xaccSplitSetValue (sd->split, gnc_numeric_create (0, 1));
@ -771,24 +752,13 @@ gnc_template_register_save_shares_cell (BasicCell * cell,
gpointer user_data) gpointer user_data)
{ {
SRSaveData *sd = save_data; SRSaveData *sd = save_data;
kvp_frame *kvpf;
char *sharesStr = "(x + y)/42"; char *sharesStr = "(x + y)/42";
g_return_if_fail (gnc_basic_cell_has_name (cell, SHRS_CELL)); g_return_if_fail (gnc_basic_cell_has_name (cell, SHRS_CELL));
kvpf = xaccSplitGetSlots (sd->split);
/* FIXME: shares cells are numeric by definition. */ /* FIXME: shares cells are numeric by definition. */
DEBUG ("kvp_frame before: %s\n", kvp_frame_to_string (kvpf)); qof_instance_set (QOF_INSTANCE (sd->split),
"sx-shares", sharesStr,
/* sharesStr = gnc_numeric_to_string( sharesStr ); */ NULL);
kvp_frame_set_slot_path (kvpf,
kvp_value_new_string (sharesStr),
GNC_SX_ID,
GNC_SX_SHARES,
NULL);
DEBUG ("kvp_frame after: %s\n", kvp_frame_to_string (kvpf));
/* set the shares to an innocuous value */ /* set the shares to an innocuous value */
xaccSplitSetSharePriceAndAmount (sd->split, xaccSplitSetSharePriceAndAmount (sd->split,

View File

@ -2095,31 +2095,23 @@ gnc_template_register_get_xfrm_entry (VirtualLocation virt_loc,
static char *name = NULL; static char *name = NULL;
SplitRegister *reg = user_data; SplitRegister *reg = user_data;
kvp_frame *kvpf;
Split *split; Split *split;
Account *account;
GncGUID *guid;
split = gnc_split_register_get_split (reg, virt_loc.vcell_loc); split = gnc_split_register_get_split (reg, virt_loc.vcell_loc);
if (!split) if (!split)
return NULL; return NULL;
/* Caller either uses the return as a temporary in a boolean
kvpf = xaccSplitGetSlots (split); * expression or g_strdups it, so we keep it static and free the
* old one on every call to avoid leaks. Ugly, but it works.
*/
g_free (name); g_free (name);
qof_instance_get (QOF_INSTANCE (split),
if (kvpf) "sx-account", &guid,
{ NULL);
Account *account; account = xaccAccountLookup (guid, gnc_get_current_book ());
GncGUID *guid; name = account ? gnc_get_account_name_for_register (account) : NULL;
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;
return name; return name;
} }
@ -2132,10 +2124,13 @@ gnc_template_register_get_fdebt_entry (VirtualLocation virt_loc,
{ {
SplitRegister *reg = user_data; SplitRegister *reg = user_data;
Split *split = gnc_split_register_get_split(reg, virt_loc.vcell_loc); Split *split = gnc_split_register_get_split(reg, virt_loc.vcell_loc);
kvp_frame *kvpf = xaccSplitGetSlots(split); char *formula;
return kvp_value_get_string( qof_instance_get (QOF_INSTANCE (split),
kvp_frame_get_slot_path (kvpf, "sched-xaction", "debit-formula", NULL)); "sx-debit-formula", &formula,
NULL);
return formula;
} }
static char * static char *
@ -2159,14 +2154,15 @@ gnc_template_register_get_fcred_entry (VirtualLocation virt_loc,
gpointer user_data) gpointer user_data)
{ {
SplitRegister *reg = user_data; SplitRegister *reg = user_data;
kvp_frame *kvpf; Split *split = gnc_split_register_get_split(reg, virt_loc.vcell_loc);
Split *split; char *formula;
split = gnc_split_register_get_split (reg, virt_loc.vcell_loc); qof_instance_get (QOF_INSTANCE (split),
kvpf = xaccSplitGetSlots (split); "sx-credit-formula", &formula,
NULL);
return formula;
return kvp_value_get_string(
kvp_frame_get_slot_path (kvpf, "sched-xaction", "credit-formula", NULL));
} }
static char * static char *
@ -2198,8 +2194,13 @@ gnc_template_register_get_debcred_entry (VirtualLocation virt_loc,
gboolean *conditionally_changed, gboolean *conditionally_changed,
gpointer user_data) gpointer user_data)
{ {
PERR("The function called always returned either NULL or an empty string "
"while issuing dire warnings about how incorrect it is. That code "
"has been removed and the function if called raises this error and "
"returns NULL");
return NULL;
#if 0
SplitRegister *reg = user_data; SplitRegister *reg = user_data;
kvp_frame *kvpf;
Split *split; Split *split;
split = gnc_split_register_get_split (reg, virt_loc.vcell_loc); split = gnc_split_register_get_split (reg, virt_loc.vcell_loc);
@ -2245,6 +2246,7 @@ gnc_template_register_get_debcred_entry (VirtualLocation virt_loc,
} }
return NULL; return NULL;
#endif
} }
static void static void