[gnome-boxes] downloader: (Try to) Support non-HTTP cases



commit 9ce057bf1c875a4b863ce61e35974d223596cb5f
Author: Zeeshan Ali (Khattak) <zeeshanak gnome org>
Date:   Wed Jan 16 04:05:07 2013 +0200

    downloader: (Try to) Support non-HTTP cases
    
    This is not guaranteed to work for every kind of filesystem but at least
    now we don't assume HTTP blindly and support caching from local
    filesystem. Its a perfectly valid usecase for distros to provide product
    logos or driver files from local filesystem and with this patch we'll
    support that.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=691823

 src/downloader.vala |   66 ++++++++++++++++++++++++++++----------------------
 1 files changed, 37 insertions(+), 29 deletions(-)
---
diff --git a/src/downloader.vala b/src/downloader.vala
index 749116f..5911d0e 100644
--- a/src/downloader.vala
+++ b/src/downloader.vala
@@ -46,7 +46,6 @@ private class Boxes.Downloader : GLib.Object {
                                 string           cached_path,
                                 ActivityProgress progress = new ActivityProgress ()) throws GLib.Error {
         var uri = remote_file.get_uri ();
-
         if (downloads.contains (uri))
             // Already being downloaded
             return yield await_download (uri, cached_path); // FIXME: No progress report in this case.
@@ -61,34 +60,10 @@ private class Boxes.Downloader : GLib.Object {
         downloads.set (uri, cached_file);
 
         try {
-            var msg = new Soup.Message ("GET", uri);
-            var address = msg.get_address ();
-            var connectable = new NetworkAddress (address.name, (uint16) address.port);
-            var network_monitor = NetworkMonitor.get_default ();
-            if (!(yield network_monitor.can_reach_async (connectable)))
-                throw new Boxes.Error.INVALID ("Failed to reach host '%s' on port '%d'", address.name, address.port);
-
-            int64 total_num_bytes = 0;
-            msg.got_headers.connect (() => {
-                total_num_bytes =  msg.response_headers.get_content_length ();
-            });
-
-            int64 current_num_bytes = 0;
-            msg.got_chunk.connect ((msg, chunk) => {
-                if (total_num_bytes <= 0)
-                    return;
-
-                current_num_bytes += chunk.length;
-                progress.progress = (double) current_num_bytes / total_num_bytes;
-            });
-
-            session.queue_message (msg, (session, msg) => {
-                download.callback ();
-            });
-            yield;
-            if (msg.status_code != Soup.KnownStatusCode.OK)
-                throw new Boxes.Error.INVALID (msg.reason_phrase);
-            yield cached_file.replace_contents_async (msg.response_body.data, null, false, 0, null, null);
+            if (remote_file.has_uri_scheme ("http") || remote_file.has_uri_scheme ("https"))
+                yield download_from_http (uri, cached_file, progress);
+            else
+                yield copy_file (remote_file, cached_file); // FIXME: No progress report in this case.
         } catch (GLib.Error error) {
             download_failed (uri, cached_file, error);
 
@@ -103,6 +78,39 @@ private class Boxes.Downloader : GLib.Object {
         return cached_file;
     }
 
+    private async void download_from_http (string           uri,
+                                           File             cached_file,
+                                           ActivityProgress progress) throws GLib.Error {
+        var msg = new Soup.Message ("GET", uri);
+        var address = msg.get_address ();
+        var connectable = new NetworkAddress (address.name, (uint16) address.port);
+        var network_monitor = NetworkMonitor.get_default ();
+        if (!(yield network_monitor.can_reach_async (connectable)))
+            throw new Boxes.Error.INVALID ("Failed to reach host '%s' on port '%d'", address.name, address.port);
+
+        int64 total_num_bytes = 0;
+        msg.got_headers.connect (() => {
+            total_num_bytes =  msg.response_headers.get_content_length ();
+        });
+
+        int64 current_num_bytes = 0;
+        msg.got_chunk.connect ((msg, chunk) => {
+            if (total_num_bytes <= 0)
+                return;
+
+            current_num_bytes += chunk.length;
+            progress.progress = (double) current_num_bytes / total_num_bytes;
+        });
+
+        session.queue_message (msg, (session, msg) => {
+            download_from_http.callback ();
+        });
+        yield;
+        if (msg.status_code != Soup.KnownStatusCode.OK)
+            throw new Boxes.Error.INVALID (msg.reason_phrase);
+        yield cached_file.replace_contents_async (msg.response_body.data, null, false, 0, null, null);
+    }
+
     public static async void fetch_os_logo (Gtk.Image image, Osinfo.Os os, int size) {
         var logo_url = fetch_os_logo_url (os);
         if (logo_url == null)



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