[gnome-calculator] Implemented exchange rate refresh interval setting



commit 9b4886706571d2c3505a00c43e1c5bb2e7fa3b02
Author: Robert Roth <robert roth bee-tf ro>
Date:   Sun Feb 3 16:31:28 2019 +0200

    Implemented exchange rate refresh interval setting

 data/org.gnome.calculator.gschema.xml |  5 +++++
 lib/currency.vala                     | 10 +++++++--
 src/gnome-calculator.vala             |  1 +
 src/math-preferences.vala             | 40 +++++++++++++++++++++++++++++++++++
 4 files changed, 54 insertions(+), 2 deletions(-)
---
diff --git a/data/org.gnome.calculator.gschema.xml b/data/org.gnome.calculator.gschema.xml
index 8cb1fc83..a3af435f 100644
--- a/data/org.gnome.calculator.gschema.xml
+++ b/data/org.gnome.calculator.gschema.xml
@@ -57,6 +57,11 @@
       <summary>Angle units</summary>
       <description>The angle units to use</description>
     </key>
+    <key name="refresh-interval" type="i">
+      <default>604800</default>
+      <summary>Currency update interval</summary>
+      <description>How often the currency exchange rates should be updated</description>
+    </key>
     <key name="button-mode" enum="org.gnome.calculator.ButtonMode">
       <default>'basic'</default>
       <summary>Button mode</summary>
diff --git a/lib/currency.vala b/lib/currency.vala
index d2f6d7a1..132fa52b 100644
--- a/lib/currency.vala
+++ b/lib/currency.vala
@@ -16,6 +16,9 @@ private static CurrencyManager? default_currency_manager = null;
 public class CurrencyManager : Object
 {
     private List<Currency> currencies;
+
+    public int refresh_interval { get; set; }
+
     public signal void updated ();
 
     public static CurrencyManager get_default ()
@@ -147,6 +150,9 @@ public class CurrencyManager : Object
      */
     private bool file_needs_update (string filename, double max_age)
     {
+        if (max_age == 0)
+            return false;
+
         if (!FileUtils.test (filename, FileTest.IS_REGULAR))
             return true;
 
@@ -377,14 +383,14 @@ public class CurrencyManager : Object
     {
         /* Update rates if necessary */
         var path = get_imf_rate_filepath ();
-        if (!downloading_imf_rates && file_needs_update (path, 60 * 60 * 24 * 7))
+        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, 60 * 60 * 24 * 7))
+        if (!downloading_ecb_rates && file_needs_update (path, refresh_interval))
         {
             downloading_ecb_rates = true;
             debug ("Downloading rates from the ECB...");
diff --git a/src/gnome-calculator.vala b/src/gnome-calculator.vala
index 5c09749c..ac61cc17 100644
--- a/src/gnome-calculator.vala
+++ b/src/gnome-calculator.vala
@@ -119,6 +119,7 @@ public class Calculator : Gtk.Application
         last_opened_window = create_new_window (settings);
         // restore the first window position from the settings
         load_window_position (last_opened_window);
+        CurrencyManager.get_default ().refresh_interval = settings.get_int ("refresh-interval");
     }
 
     private MathWindow get_active_math_window ()
diff --git a/src/math-preferences.vala b/src/math-preferences.vala
index d1ab7657..335b97c6 100644
--- a/src/math-preferences.vala
+++ b/src/math-preferences.vala
@@ -13,11 +13,13 @@ public class MathPreferencesDialog : Gtk.Dialog
     public MathEquation equation { private get; construct; }
 
     private Gtk.ComboBox angle_unit_combo;
+    private Gtk.ComboBox refresh_interval_combo;
     private Gtk.ComboBox number_format_combo;
     private Gtk.ComboBox word_size_combo;
     private Gtk.SpinButton decimal_places_spin;
     private Gtk.Switch thousands_separator_switch;
     private Gtk.Switch trailing_zeroes_switch;
+    private Settings settings;
 
     public MathPreferencesDialog (MathEquation eq)
     {
@@ -26,6 +28,7 @@ public class MathPreferencesDialog : Gtk.Dialog
 
     construct
     {
+        settings = new Settings ("org.gnome.calculator");
         set_title (/* Title of preferences dialog */
                    _("Preferences"));
         border_width = 8;
@@ -180,6 +183,30 @@ public class MathPreferencesDialog : Gtk.Dialog
         word_size_combo.pack_start (renderer, true);
         word_size_combo.add_attribute (renderer, "text", 0);
 
+        label = new Gtk.Label.with_mnemonic (/* Preferences dialog: Label for word size combo box */
+                                             _("Exchange rate refresh interval"));
+        label.show ();
+        label.xalign = 0;
+        grid.attach (label, 0, 7, 1, 1);
+
+        refresh_interval_combo = new Gtk.ComboBox ();
+        label.mnemonic_widget = refresh_interval_combo;
+        refresh_interval_combo.show ();
+        refresh_interval_combo.changed.connect (refresh_interval_combo_changed_cb);
+        grid.attach (refresh_interval_combo, 1, 7, 1, 1);
+
+        model = new Gtk.ListStore (2, typeof (string), typeof (int));
+        refresh_interval_combo.model = model;
+        model.append (out iter);
+        model.set (iter, 0, /* Refresh interval combo: never */ _("never"), 1, 0);
+        model.append (out iter);
+        model.set (iter, 0, /* Refresh interval combo: daily */ _("daily"), 1, 60 * 60 * 24);
+        model.append (out iter);
+        model.set (iter, 0, /* Refresh interval combo: weekly */ _("weekly"), 1, 60 * 60 * 24 * 7);
+        renderer = new Gtk.CellRendererText ();
+        refresh_interval_combo.pack_start (renderer, true);
+        refresh_interval_combo.add_attribute (renderer, "text", 0);
+
         decimal_places_spin.set_value (equation.accuracy);
         equation.notify["accuracy"].connect ((pspec) => { decimal_places_spin.set_value (equation.accuracy); 
});
 
@@ -197,6 +224,8 @@ public class MathPreferencesDialog : Gtk.Dialog
 
         set_combo_box_from_int (angle_unit_combo, equation.angle_units);
         equation.notify["angle-units"].connect ((pspec) => { set_combo_box_from_int (angle_unit_combo, 
equation.angle_units); });
+
+        set_combo_box_from_int (refresh_interval_combo, settings.get_int ("refresh-interval"));
     }
 
     protected override void response (int id)
@@ -237,6 +266,17 @@ public class MathPreferencesDialog : Gtk.Dialog
         equation.word_size = value;
     }
 
+    private void refresh_interval_combo_changed_cb (Gtk.ComboBox combo)
+    {
+        Gtk.TreeIter iter;
+        combo.get_active_iter (out iter);
+        int value;
+        combo.model.get (iter, 1, out value, -1);
+        settings.set_int ("refresh-interval", value);
+        CurrencyManager.get_default ().refresh_interval = value;
+
+    }
+
     private void set_combo_box_from_int (Gtk.ComboBox combo, int value)
     {
         Gtk.TreeIter iter;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]