[Gnucash-changes] r13536 - gnucash/trunk/src - Cache the result of
a gconf lookup for a currency choice. Add a new
David Hampton
hampton at cvs.gnucash.org
Wed Mar 8 00:14:55 EST 2006
Author: hampton
Date: 2006-03-08 00:14:54 -0500 (Wed, 08 Mar 2006)
New Revision: 13536
Trac: http://svn.gnucash.org/trac/changeset/13536
Modified:
gnucash/trunk/src/app-utils/gnc-ui-util.c
gnucash/trunk/src/engine/gnc-hooks.c
gnucash/trunk/src/engine/gnc-hooks.h
Log:
Cache the result of a gconf lookup for a currency choice. Add a new
callback hook for when a user changes a currency setting (default
account or report).
Modified: gnucash/trunk/src/app-utils/gnc-ui-util.c
===================================================================
--- gnucash/trunk/src/app-utils/gnc-ui-util.c 2006-03-08 04:48:32 UTC (rev 13535)
+++ gnucash/trunk/src/app-utils/gnc-ui-util.c 2006-03-08 05:14:54 UTC (rev 13536)
@@ -41,6 +41,7 @@
#include "gnc-engine.h"
#include "gnc-euro.h"
#include "gnc-gconf-utils.h"
+#include "gnc-hooks.h"
#include "gnc-module.h"
#include "gnc-ui-util.h"
#include "Group.h"
@@ -60,6 +61,12 @@
static gboolean reverse_balance_inited = FALSE;
static gboolean reverse_type[NUM_ACCOUNT_TYPES];
+/* Cache currency ISO codes and only look them up in gconf when
+ * absolutely necessary. Can't cache a pointer to the data structure
+ * as that will change any time the book changes. */
+static gchar *user_default_currency = NULL;
+static gchar *user_report_currency = NULL;
+
/********************************************************************\
* gnc_configure_account_separator *
* updates the current account separator character *
@@ -817,9 +824,14 @@
gnc_commodity *
gnc_default_currency (void)
{
- gnc_commodity *currency;
+ gnc_commodity *currency = NULL;
gchar *choice, *mnemonic;
+ if (user_default_currency)
+ return gnc_commodity_table_lookup(gnc_get_current_commodities(),
+ GNC_COMMODITY_NS_ISO,
+ user_default_currency);
+
choice = gnc_gconf_get_string(GCONF_GENERAL, KEY_CURRENCY_CHOICE, NULL);
if (choice && strcmp(choice, "other") == 0) {
mnemonic = gnc_gconf_get_string(GCONF_GENERAL, KEY_CURRENCY_OTHER, NULL);
@@ -828,20 +840,28 @@
DEBUG("mnemonic %s, result %p", mnemonic, currency);
g_free(mnemonic);
g_free(choice);
+ }
- if (currency)
- return currency;
+ if (!currency)
+ currency = gnc_locale_default_currency ();
+ if (currency) {
+ mnemonic = user_default_currency;
+ user_default_currency = g_strdup(gnc_commodity_get_mnemonic(currency));
+ g_free(mnemonic);
}
-
- return gnc_locale_default_currency ();
+ return currency;
}
gnc_commodity *
gnc_default_report_currency (void)
{
- gnc_commodity *currency;
+ gnc_commodity *currency = NULL;
gchar *choice, *mnemonic;
+ if (user_report_currency)
+ return gnc_commodity_table_lookup(gnc_get_current_commodities(),
+ GNC_COMMODITY_NS_ISO,
+ user_report_currency);
choice = gnc_gconf_get_string(GCONF_GENERAL_REPORT,
KEY_CURRENCY_CHOICE, NULL);
if (choice && strcmp(choice, "other") == 0) {
@@ -850,14 +870,27 @@
currency = gnc_commodity_table_lookup(gnc_get_current_commodities(),
GNC_COMMODITY_NS_ISO, mnemonic);
DEBUG("mnemonic %s, result %p", mnemonic, currency);
+ g_free(choice);
g_free(mnemonic);
- g_free(choice);
+ }
- if (currency)
- return currency;
+ if (!currency)
+ currency = gnc_locale_default_currency ();
+ if (currency) {
+ mnemonic = user_report_currency;
+ user_report_currency = g_strdup(gnc_commodity_get_mnemonic(currency));
+ g_free(mnemonic);
}
+ return currency;
+}
- return gnc_locale_default_currency ();
+
+static void
+gnc_currency_changed_cb (GConfEntry *entry, gpointer user_data)
+{
+ user_default_currency = NULL;
+ user_report_currency = NULL;
+ gnc_hook_run(HOOK_CURRENCY_CHANGED, NULL);
}
@@ -2051,6 +2084,10 @@
gnc_gconf_general_register_cb(KEY_REVERSED_ACCOUNTS,
(GncGconfGeneralCb)gnc_configure_reverse_balance,
NULL);
+ gnc_gconf_general_register_cb(KEY_CURRENCY_CHOICE,
+ gnc_currency_changed_cb, NULL);
+ gnc_gconf_general_register_cb(KEY_CURRENCY_OTHER,
+ gnc_currency_changed_cb, NULL);
gnc_gconf_general_register_cb("auto_decimal_point",
gnc_set_auto_decimal_enabled,
NULL);
Modified: gnucash/trunk/src/engine/gnc-hooks.c
===================================================================
--- gnucash/trunk/src/engine/gnc-hooks.c 2006-03-08 04:48:32 UTC (rev 13535)
+++ gnucash/trunk/src/engine/gnc-hooks.c 2006-03-08 05:14:54 UTC (rev 13536)
@@ -311,6 +311,8 @@
gnc_hook_create(HOOK_REPORT, 0,
"Run just before the reports are pushed into the menus."
" Hook args: ()");
+ gnc_hook_create(HOOK_CURRENCY_CHANGED, 0,
+ "Functions to run when the user changes currency settings. Hook args: ()");
gnc_hook_create(HOOK_SAVE_OPTIONS, 0,
"Functions to run when saving options. Hook args: ()");
gnc_hook_create(HOOK_ADD_EXTENSION, 0,
Modified: gnucash/trunk/src/engine/gnc-hooks.h
===================================================================
--- gnucash/trunk/src/engine/gnc-hooks.h 2006-03-08 04:48:32 UTC (rev 13535)
+++ gnucash/trunk/src/engine/gnc-hooks.h 2006-03-08 05:14:54 UTC (rev 13536)
@@ -63,6 +63,7 @@
#define HOOK_UI_SHUTDOWN "hook_ui_shutdown"
#define HOOK_NEW_BOOK "hook_new_book"
#define HOOK_REPORT "hook_report"
+#define HOOK_CURRENCY_CHANGED "hook_currency_changed"
#define HOOK_SAVE_OPTIONS "hook_save_options"
#define HOOK_ADD_EXTENSION "hook_add_extension"
More information about the gnucash-changes
mailing list