diff --git a/src/app-utils/gnc-ui-util.c b/src/app-utils/gnc-ui-util.c index 3810597bcc..eb23d80e92 100644 --- a/src/app-utils/gnc-ui-util.c +++ b/src/app-utils/gnc-ui-util.c @@ -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); + g_free(mnemonic); + } - 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_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); diff --git a/src/engine/gnc-hooks.c b/src/engine/gnc-hooks.c index d46f869cff..b58977b82d 100644 --- a/src/engine/gnc-hooks.c +++ b/src/engine/gnc-hooks.c @@ -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, diff --git a/src/engine/gnc-hooks.h b/src/engine/gnc-hooks.h index 8c0b23f10c..618b322db5 100644 --- a/src/engine/gnc-hooks.h +++ b/src/engine/gnc-hooks.h @@ -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"