Le mardi 19 à 23:05, Derek Atkins a écrit :
> On Tue, November 19, 2013 4:58 pm, Frédéric Perrin wrote:
>> Anyway, this is due to r23345 : since we set the user_symbol of every
>> currency at startup, the KVP of every currency, even those not used, now
>> contains simething. Therefore, in gnc_commodity_dom_tree_create(),
>> kvpnode is !NULL, and every currency gets dumped to the XML file at save
>> time. That includes the new ZMW, and when your old gnucash that doesn't
>> know about ZMW sees that currency, it aborts with "Invalid commodity".
>>
>> I suppose we don't want to dump every currency into the save file
>> whenever the file is written. Do we want to check currency->usage_count ?
>> Do we want an API Bool gnc_account_currency_used(Account, gnc_commodity) ?
>> Thoughts ?
>
> I would recommend we do something slightly different. I would have TWO
> setter functions, a "set_default()" as well as a "set()". The
> gnc_commodity object can be extended to cache the value. The
> set_default() would only set the cached value. The set() function would
> both set the cached value *and* set the kvp. The get() function could
> first check the kvp() and, if that is empty or non-existing it can use the
> cached value. Either that, or at load time we cache the value from the
> kvp if it exists and then get() only needs to read the cached value.
That would also limit the modifications to the commodity class, rather
tham having each backend checking whether the commodity is used.
The attached patch compiles and seems to do what we want from 5 minutes
of testing.
--
Fred
diff --git a/src/engine/gnc-commodity.c b/src/engine/gnc-commodity.c
index 5998cfa..313f090 100644
--- a/src/engine/gnc-commodity.c
+++ b/src/engine/gnc-commodity.c
@@ -82,6 +82,9 @@ typedef struct CommodityPrivate
/* the number of accounts using this commodity - this field is not
* persisted */
int usage_count;
+
+ /* the default display_symbol, set in iso-4217-currencies at start-up */
+ const char * default_symbol;
} CommodityPrivate;
#define GET_PRIVATE(o) \
@@ -93,6 +96,7 @@ struct _GncCommodityClass
};
static void commodity_free(gnc_commodity * cm);
+static void gnc_commodity_set_default_symbol(gnc_commodity *, const char *);
struct gnc_commodity_namespace_s
{
@@ -1136,7 +1140,10 @@ gnc_commodity_get_user_symbol(const gnc_commodity *cm)
{
const char *str;
if (!cm) return NULL;
- return kvp_frame_get_string(cm->inst.kvp_data, "user_symbol");
+ str = kvp_frame_get_string(cm->inst.kvp_data, "user_symbol");
+ if (str && *str)
+ return str;
+ return GET_PRIVATE(cm)->default_symbol;
}
/********************************************************************
@@ -1393,6 +1400,18 @@ gnc_commodity_set_user_symbol(gnc_commodity * cm, const char * user_symbol)
}
/********************************************************************
+ * gnc_commodity_set_default_symbol
+ * Not made visible in gnc-commodity.h, it is only called from
+ * iso-4217-currencies.c at startup.
+ ********************************************************************/
+void
+gnc_commodity_set_default_symbol(gnc_commodity * cm,
+ const char * default_symbol)
+{
+ GET_PRIVATE(cm)->default_symbol = default_symbol;
+}
+
+/********************************************************************
* gnc_commodity_increment_usage_count
********************************************************************/
diff --git a/src/engine/iso-currencies-to-c.in b/src/engine/iso-currencies-to-c.in
index 00bf1e7..e8dd400 100755
--- a/src/engine/iso-currencies-to-c.in
+++ b/src/engine/iso-currencies-to-c.in
@@ -54,7 +54,7 @@ exec @GUILE@ -s $0 "$@"
PWARN(\"failed to insert %s into commodity table\", fullname);
}
}
- gnc_commodity_set_user_symbol(c, ~S);
+ gnc_commodity_set_default_symbol(c, ~S);
}\n"
fullname
namespace
_______________________________________________
gnucash-devel mailing list
[email protected]
https://lists.gnucash.org/mailman/listinfo/gnucash-devel