From 733e6e91dad9729b604da8d4e68851819a42fcbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Perrin?= Date: Wed, 26 Mar 2014 00:07:49 +0000 Subject: [PATCH] Bug 723145 - Currency display does not respect locale Use the system-provided symbol for the locale currency --- src/app-utils/gnc-ui-util.c | 13 ----- src/app-utils/gnc-ui-util.h | 1 - src/engine/gnc-commodity.c | 56 ++++++++++++++++++++-- src/engine/gnc-commodity.h | 23 +++++++++ src/gnome-utils/dialog-commodity.c | 7 +-- src/gnome-utils/gnc-tree-model-commodity.c | 2 +- 6 files changed, 78 insertions(+), 24 deletions(-) diff --git a/src/app-utils/gnc-ui-util.c b/src/app-utils/gnc-ui-util.c index 09593b0449..668e1b4261 100644 --- a/src/app-utils/gnc-ui-util.c +++ b/src/app-utils/gnc-ui-util.c @@ -1747,19 +1747,6 @@ printable_value (gdouble val, gint denom) return xaccPrintAmount (num, info); } -const char* -gnc_commodity_get_nice_symbol(const gnc_commodity *cm) -{ - const char *nice_symbol; - if (!cm) return NULL; - - nice_symbol = gnc_commodity_get_user_symbol(cm); - if (nice_symbol && *nice_symbol) - return nice_symbol; - else - return gnc_commodity_get_mnemonic(cm); -} - /********************************************************************\ * xaccParseAmount * * parses amount strings using locale data * diff --git a/src/app-utils/gnc-ui-util.h b/src/app-utils/gnc-ui-util.h index 83fa84f6db..281ffc69e5 100644 --- a/src/app-utils/gnc-ui-util.h +++ b/src/app-utils/gnc-ui-util.h @@ -259,7 +259,6 @@ const char * xaccPrintAmount (gnc_numeric val, GNCPrintAmountInfo info); int xaccSPrintAmount (char *buf, gnc_numeric val, GNCPrintAmountInfo info); const gchar *printable_value(gdouble val, gint denom); -const char*gnc_commodity_get_nice_symbol(const gnc_commodity *cm); gchar *number_to_words(gdouble val, gint64 denom); gchar *numeric_to_words(gnc_numeric val); diff --git a/src/engine/gnc-commodity.c b/src/engine/gnc-commodity.c index 77c643b9cb..54b52a2655 100644 --- a/src/engine/gnc-commodity.c +++ b/src/engine/gnc-commodity.c @@ -35,6 +35,7 @@ #include #include "gnc-commodity.h" +#include "gnc-locale-utils.h" #include "gnc-prefs.h" static QofLogModule log_module = GNC_MOD_COMMODITY; @@ -1140,18 +1141,51 @@ gnc_commodity_get_quote_tz(const gnc_commodity *cm) /******************************************************************** * gnc_commodity_get_user_symbol ********************************************************************/ - const char* gnc_commodity_get_user_symbol(const gnc_commodity *cm) { const char *str; if (!cm) return NULL; - str = kvp_frame_get_string(cm->inst.kvp_data, "user_symbol"); - if (str && *str) - return str; + return kvp_frame_get_string(cm->inst.kvp_data, "user_symbol"); +} + +/******************************************************************** + * gnc_commodity_get_default_symbol + *******************************************************************/ +const char* +gnc_commodity_get_default_symbol(const gnc_commodity *cm) +{ + const char *str; + if (!cm) return NULL; return GET_PRIVATE(cm)->default_symbol; } +/******************************************************************** + * gnc_commodity_get_nice_symbol + *******************************************************************/ +const char* +gnc_commodity_get_nice_symbol (const gnc_commodity *cm) +{ + const char *nice_symbol; + struct lconv *lc; + if (!cm) return NULL; + + nice_symbol = gnc_commodity_get_user_symbol(cm); + if (nice_symbol && *nice_symbol) + return nice_symbol; + + lc = gnc_localeconv(); + nice_symbol = lc->currency_symbol; + if (!g_strcmp0(gnc_commodity_get_mnemonic(cm), lc->int_curr_symbol)) + return nice_symbol; + + nice_symbol = gnc_commodity_get_default_symbol(cm); + if (nice_symbol && *nice_symbol) + return nice_symbol; + + return gnc_commodity_get_mnemonic(cm); +} + /******************************************************************** * gnc_commodity_set_mnemonic ********************************************************************/ @@ -1393,11 +1427,25 @@ gnc_commodity_set_quote_tz(gnc_commodity *cm, const char *tz) void gnc_commodity_set_user_symbol(gnc_commodity * cm, const char * user_symbol) { + struct lconv *lc; + if (!cm) return; ENTER ("(cm=%p, symbol=%s)", cm, user_symbol ? user_symbol : "(null)"); gnc_commodity_begin_edit(cm); + + lc = gnc_localeconv(); + if (!user_symbol || !*user_symbol) + user_symbol = NULL; + else if (!g_strcmp0(lc->int_curr_symbol, gnc_commodity_get_mnemonic(cm)) && + !g_strcmp0(lc->currency_symbol, user_symbol)) + /* if the user gives the ISO symbol for the locale currency or the + * default symbol, actually remove the user symbol */ + user_symbol = NULL; + else if (!g_strcmp0(user_symbol, gnc_commodity_get_default_symbol(cm))) + user_symbol = NULL; + kvp_frame_set_string(cm->inst.kvp_data, "user_symbol", user_symbol); mark_commodity_dirty(cm); gnc_commodity_commit_edit(cm); diff --git a/src/engine/gnc-commodity.h b/src/engine/gnc-commodity.h index bb023b9ef0..f64684c509 100644 --- a/src/engine/gnc-commodity.h +++ b/src/engine/gnc-commodity.h @@ -470,6 +470,29 @@ const char* gnc_commodity_get_quote_tz(const gnc_commodity *cm); * should not be freed by the caller. */ const char* gnc_commodity_get_user_symbol(const gnc_commodity *cm); + +/** Retrieve the default symbol for the specified commodity. This will + * be a pointer to a nul terminated string like "£", "US$", etc. Note + * that for the locale currency, you probably want to look at the + * system-provided symbol first. See gnc_commodity_get_nice_symbol. + * + * @param cm A pointer to a commodity data structure. + * + * @return A pointer to the default symbol for this commodity. + */ +const char* gnc_commodity_get_default_symbol(const gnc_commodity *cm); + +/** Retrieve a symbol for the specified commodity, suitable for + * display to the user. This will be a pointer to a nul terminated + * string like "£", "US$", etc. That function is locale-aware and + * will base its choice of symbol on the user-configured symbol, + * the locale a + * + * @param cm A pointer to a commodity data structure. + * + * @return A pointer to the symbol for this commodity. + */ +const char*gnc_commodity_get_nice_symbol(const gnc_commodity *cm); /** @} */ /** @name Commodity Accessor Routines - Set diff --git a/src/gnome-utils/dialog-commodity.c b/src/gnome-utils/dialog-commodity.c index 35507d784a..86f01e02f9 100644 --- a/src/gnome-utils/dialog-commodity.c +++ b/src/gnome-utils/dialog-commodity.c @@ -1157,7 +1157,7 @@ gnc_ui_common_commodity_modal(gnc_commodity *commodity, namespace = gnc_commodity_get_namespace (commodity); fullname = gnc_commodity_get_fullname (commodity); mnemonic = gnc_commodity_get_mnemonic (commodity); - user_symbol = gnc_commodity_get_user_symbol (commodity); + user_symbol = gnc_commodity_get_nice_symbol (commodity); cusip = gnc_commodity_get_cusip (commodity); fraction = gnc_commodity_get_fraction (commodity); } @@ -1316,10 +1316,7 @@ gnc_ui_commodity_dialog_to_object(CommodityWindow * w) else gnc_commodity_set_quote_tz(c, NULL); - if (user_symbol && *user_symbol) - gnc_commodity_set_user_symbol(c, user_symbol); - else - gnc_commodity_set_user_symbol(c, NULL); + gnc_commodity_set_user_symbol(c, user_symbol); gnc_commodity_commit_edit(c); return TRUE; diff --git a/src/gnome-utils/gnc-tree-model-commodity.c b/src/gnome-utils/gnc-tree-model-commodity.c index e52e1dc868..a3a893438e 100644 --- a/src/gnome-utils/gnc-tree-model-commodity.c +++ b/src/gnome-utils/gnc-tree-model-commodity.c @@ -690,7 +690,7 @@ gnc_tree_model_commodity_get_value (GtkTreeModel *tree_model, case GNC_TREE_MODEL_COMMODITY_COL_USER_SYMBOL: g_value_init (value, G_TYPE_STRING); - g_value_set_string (value, gnc_commodity_get_user_symbol (commodity)); + g_value_set_string (value, gnc_commodity_get_nice_symbol (commodity)); break; case GNC_TREE_MODEL_COMMODITY_COL_VISIBILITY: g_value_init (value, G_TYPE_BOOLEAN);