Google poster downloader: add unqueue method, make downloads cancellable
authorPhilipp Zabel <philipp.zabel@gmail.com>
Tue, 10 Nov 2009 19:18:30 +0000 (20:18 +0100)
committerPhilipp Zabel <philipp.zabel@gmail.com>
Thu, 12 Nov 2009 18:23:24 +0000 (19:23 +0100)
src/poster/google-poster-downloader.vala

index d12cacb..5dee8c2 100644 (file)
@@ -4,6 +4,7 @@ using GLib;
 public interface Provider {
        public abstract int Fetch (string title, string year, string kind) throws DBus.Error;
        public abstract int FetchThumbnail (string title, string year, string kind) throws DBus.Error;
+       public abstract void Unqueue (int handle) throws DBus.Error;
        public signal void fetched (int handle, string path);
 }
 
@@ -16,6 +17,7 @@ public interface Provider {
 
 public class GoogleImages : Object, Provider {
        int fetch_handle;
+       Cancellable cancellable;
 
        public int Fetch (string title, string year, string kind) throws DBus.Error {
                int handle = fetch_handle++;
@@ -31,6 +33,11 @@ public class GoogleImages : Object, Provider {
                return handle;
        }
 
+       public void Unqueue (int handle) {
+               // FIXME - cancel everything for now
+               cancellable.cancel ();
+       }
+
        private async void _fetch_async (string title, string year, string kind, bool thumbnail, int handle) {
                uint u = 0;
                size_t hread = 0;
@@ -38,6 +45,9 @@ public class GoogleImages : Object, Provider {
                string stitched = "";
                bool first = true;
 
+               // FIXME - cancel everything for now
+               cancellable = new Cancellable ();
+
                stdout.printf ("Fetching %s \"%s (%s)\" ...\n", kind, title, year);
 
                if (title == null || title == "")
@@ -88,8 +98,12 @@ public class GoogleImages : Object, Provider {
                        InputStream stream = google_search.read (null);
 
                        while (total < 40000) {
-                               hread = yield stream.read_async ((char *)buffer + total, 40000 - total, Priority.DEFAULT_IDLE, null);
+                               hread = yield stream.read_async ((char *)buffer + total, 40000 - total, Priority.DEFAULT_IDLE, cancellable);
                                total += hread;
+                               if (cancellable.is_cancelled ()) {
+                                       stdout.printf ("CANCELED\n");
+                                       return;
+                               }
                                if (hread == 0)
                                        break;
                        }
@@ -160,8 +174,12 @@ public class GoogleImages : Object, Provider {
                                yield online_image.copy_async (cache_image,
                                                               FileCopyFlags.NONE,
                                                               Priority.DEFAULT_IDLE,
-                                                              null,
+                                                              cancellable,
                                                               null);
+                               if (cancellable.is_cancelled ()) {
+                                       stdout.printf ("CANCELED\n");
+                                       return;
+                               }
 
                                FileUtils.rename (cache_path + ".part", cache_path);