From bddcd343ec6a4aad86a7add9cd5abf77338ffb76 Mon Sep 17 00:00:00 2001 From: Ray Strode Date: Thu, 13 May 2021 10:44:33 -0400 Subject: [PATCH 2/5] currency-provider: Don't ever download rates if refresh interval is 0 If an admin has set the currency refresh interval to 0 they probably don't ever want the data refreshed from the network. This commit treats a refresh interval of 0 specially to mean "don't go on the network at all" --- data/org.gnome.calculator.gschema.xml | 2 +- lib/currency.vala | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/data/org.gnome.calculator.gschema.xml b/data/org.gnome.calculator.gschema.xml index a3af435f..fbde9b53 100644 --- a/data/org.gnome.calculator.gschema.xml +++ b/data/org.gnome.calculator.gschema.xml @@ -33,61 +33,61 @@ 10 Numeric Base The numeric base false Show Thousands Separators Indicates whether thousands separators are shown in large numbers. false Show Trailing Zeroes Indicates whether any trailing zeroes after the numeric point should be shown in the display value. 'automatic' Number format The format to display numbers in 'degrees' Angle units The angle units to use 604800 Currency update interval - How often the currency exchange rates should be updated + How often the currency exchange rates should be updated. A value of 0 means the currency exchange rates won't be fetched from the network at all. 'basic' Button mode The button mode '' Source currency Currency of the current calculation '' Target currency Currency to convert the current calculation into 'degree' Source units Units of the current calculation 'radian' Target units Units to convert the current calculation into 2000 Internal precision The internal precision used with the MPFR library diff --git a/lib/currency.vala b/lib/currency.vala index c098cc52..2adb11e4 100644 --- a/lib/currency.vala +++ b/lib/currency.vala @@ -354,83 +354,89 @@ public class CurrencyManager : Object { warning ("Couldn't create XPath context"); return; } xpath_ctx.register_ns ("xref", "http://www.ecb.int/vocabulary/2002-08-01/eurofxref"); var xpath_obj = xpath_ctx.eval_expression ("//xref:Cube[@currency][@rate]"); if (xpath_obj == null) { warning ("Couldn't create XPath object"); return; } var len = (xpath_obj->nodesetval != null) ? xpath_obj->nodesetval->length () : 0; for (var i = 0; i < len; i++) { var node = xpath_obj->nodesetval->item (i); if (node->type == Xml.ElementType.ELEMENT_NODE) set_ecb_rate (node, eur_rate); /* Avoid accessing removed elements */ if (node->type != Xml.ElementType.NAMESPACE_DECL) node = null; } Xml.Parser.cleanup (); } private void download_rates () { + if (refresh_interval == 0) + return; + /* Update rates if necessary */ var path = get_imf_rate_filepath (); if (!downloading_imf_rates && file_needs_update (path, refresh_interval)) { downloading_imf_rates = true; debug ("Downloading rates from the IMF..."); download_file.begin ("https://www.imf.org/external/np/fin/data/rms_five.aspx?tsvflag=Y", path, "IMF"); } path = get_ecb_rate_filepath (); if (!downloading_ecb_rates && file_needs_update (path, refresh_interval)) { downloading_ecb_rates = true; debug ("Downloading rates from the ECB..."); download_file.begin ("https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml", path, "ECB"); } } private bool load_rates () { /* Already loaded */ if (loaded_rates) return true; + if (refresh_interval == 0) + return false; + /* In process */ if (downloading_imf_rates || downloading_ecb_rates) return false; /* Use the IMF provided values and top up with currencies tracked by the ECB and not the IMF */ load_imf_rates (); load_ecb_rates (); /* Check if we couldn't find out a currency */ foreach (var c in currencies) if (c.get_value () == null || c.get_value ().is_zero ()) warning ("Currency %s is not provided by IMF or ECB", c.name); debug ("Rates loaded"); loaded_rates = true; updated (); return true; } public Number? get_value (string currency) { /* Make sure that the rates we're returning are up to date. (Just in case the application is running from a long long time) */ download_rates (); if (!load_rates ()) return null; var c = get_currency (currency); -- 2.31.1