[gnome-calculator] Use Soup instead of GVFS to download the IMF/ECB currency data



commit 124cdb661838f9be8bf1f88443cb5885d5d92629
Author: Alberto Ruiz <aruiz gnome org>
Date:   Thu Jan 28 14:30:06 2016 +0100

    Use Soup instead of GVFS to download the IMF/ECB currency data

 configure.ac      |    2 +
 lib/Makefile.am   |    1 +
 lib/currency.vala |   55 +++++++++++++++++++---------------------------------
 3 files changed, 23 insertions(+), 35 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index eed25ec..74bf123 100644
--- a/configure.ac
+++ b/configure.ac
@@ -22,6 +22,7 @@ dnl ###########################################################################
 GLIB_REQUIRED=2.40.0
 GTK_REQUIRED=3.11.6
 GTKSOURCEVIEW_REQUIRED=3.15.1
+LIBSOUP_REQUIRED=2.42
 
 AC_SUBST([GLIB_REQUIRED])
 
@@ -33,6 +34,7 @@ PKG_CHECK_MODULES(LIBCALCULATOR, [
     gio-2.0 >= $GLIB_REQUIRED
     gtksourceview-3.0 >= $GTKSOURCEVIEW_REQUIRED
     libxml-2.0
+    libsoup-2.4 >= $LIBSOUP_REQUIRED
 ])
 
 PKG_CHECK_MODULES(GNOME_CALCULATOR, [
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 5f69019..95634e7 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -25,6 +25,7 @@ libcalculator_la_VALAFLAGS = \
        --pkg posix \
        --pkg gtksourceview-3.0 \
        --pkg libxml-2.0 \
+       --pkg libsoup-2.4 \
        --header=libcalculator.h \
        --vapi=libcalculator.vapi
 
diff --git a/lib/currency.vala b/lib/currency.vala
index 8dfcf86..b45aa89 100644
--- a/lib/currency.vala
+++ b/lib/currency.vala
@@ -376,14 +376,14 @@ public class CurrencyManager : Object
         {
             downloading_imf_rates = true;
             debug ("Downloading rates from the IMF...");
-            download_file ("https://www.imf.org/external/np/fin/data/rms_five.aspx?tsvflag=Y";, path, 
download_imf_cb);
+            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))
         {
             downloading_ecb_rates = true;
             debug ("Downloading rates from the ECB...");
-            download_file ("https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml";, path, 
download_ecb_cb);
+            download_file.begin ("https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml";, path, 
"ECB");
         }
     }
 
@@ -429,50 +429,35 @@ public class CurrencyManager : Object
             return null;
     }
 
-    private void download_file (string uri, string filename, AsyncReadyCallback callback)
+    private async void download_file (string uri, string filename, string source)
     {
+
         var directory = Path.get_dirname (filename);
         DirUtils.create_with_parents (directory, 0755);
 
-        var source = File.new_for_uri (uri);
         var dest = File.new_for_path (filename);
-
-        source.copy_async.begin (dest, FileCopyFlags.OVERWRITE, GLib.Priority.DEFAULT, null, null, callback);
-    }
-
-    private void download_imf_cb (Object? object, AsyncResult result)
-    {
-        var f = object as File;
-
+        var session = new Soup.Session ();
+        var message = new Soup.Message ("GET", uri);
         try
         {
-            f.copy_async.end (result);
-            debug ("IMF rates updated");
+            var bodyinput = yield session.send_async (message);
+            var output = yield dest.replace_async (null, false, FileCreateFlags.REPLACE_DESTINATION, 
Priority.DEFAULT);
+            yield output.splice_async (bodyinput,
+                                       OutputStreamSpliceFlags.CLOSE_SOURCE | 
OutputStreamSpliceFlags.CLOSE_TARGET,
+                                       Priority.DEFAULT);
+            if (source == "IMF")
+                downloading_imf_rates = false;
+            else
+                downloading_ecb_rates = false;
+
+            load_rates ();
+            debug ("%s rates updated", source);
         }
         catch (Error e)
         {
-            warning ("Couldn't download IMF currency rate file: %s", e.message);
+            warning ("Couldn't download %s currency rate file: %s", source, e.message);
         }
-
-        downloading_imf_rates = false;
-        load_rates ();
-    }
-
-    private void download_ecb_cb (Object? object, AsyncResult result)
-    {
-        var f = object as File;
-        try
-        {
-            f.copy_async.end (result);
-            debug ("ECB rates updated");
-        }
-        catch (Error e)
-        {
-            warning ("Couldn't download ECB currency rate file: %s", e.message);
-        }
-        downloading_ecb_rates = false;
-        load_rates ();
-    }
+     }
 }
 
 public class Currency : Object


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