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).


git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@13536 57a11ea4-9604-0410-9ed3-97b8803252fd
This commit is contained in:
David Hampton 2006-03-08 05:14:54 +00:00
parent 7f9bb0da7f
commit fb9607fcbd
3 changed files with 51 additions and 11 deletions

View File

@ -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 int auto_decimal_places = 2; /* default, can be changed */
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_locale_default_currency (void)
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 @@ gnc_default_currency (void)
DEBUG("mnemonic %s, result %p", mnemonic, currency);
g_free(mnemonic);
g_free(choice);
if (currency)
return currency;
}
return gnc_locale_default_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 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 @@ gnc_default_report_currency (void)
currency = gnc_commodity_table_lookup(gnc_get_current_commodities(),
GNC_COMMODITY_NS_ISO, mnemonic);
DEBUG("mnemonic %s, result %p", mnemonic, currency);
g_free(mnemonic);
g_free(choice);
if (currency)
return currency;
g_free(mnemonic);
}
return gnc_locale_default_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;
}
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_ui_util_init (void)
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);

View File

@ -311,6 +311,8 @@ gnc_hooks_init(void)
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,

View File

@ -63,6 +63,7 @@ void gnc_hooks_init(void);
#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"