IMDb plaintext downloader: quit after a short timeout
authorPhilipp Zabel <philipp.zabel@gmail.com>
Mon, 9 Nov 2009 21:25:01 +0000 (22:25 +0100)
committerPhilipp Zabel <philipp.zabel@gmail.com>
Tue, 10 Nov 2009 11:21:15 +0000 (12:21 +0100)
src/imdb/imdb-plaintext-downloader.vala

index 89170d9..84a48b9 100644 (file)
@@ -6,6 +6,7 @@ class IMDbDownloadServer : Object, IMDbDownloader {
        int64 sofar;
        int64 total;
        bool running;
+       uint source_id;
        unowned IMDbSqlite sqlite;
        string[] mirrors = {
                "ftp.fu-berlin.de/pub/misc/movies/database/",
@@ -28,6 +29,10 @@ class IMDbDownloadServer : Object, IMDbDownloader {
                        return;
                }
                running = true;
+               if (source_id != 0) {
+                       Source.remove (source_id);
+               }
+
                message ("Download started (%x).", flags);
                progress (0);
                download_imdb_async.begin ("ftp://anonymous@" + mirror, flags, Priority.DEFAULT_IDLE);
@@ -62,11 +67,13 @@ class IMDbDownloadServer : Object, IMDbDownloader {
                                        m = yield movies.find_enclosing_mount_async(io_priority, cancellable);
                                } else {
                                        running = false;
+                                       timeout_quit ();
                                        return;
                                }
                        } catch (Error e1) {
                                critical ("Failed to mount: %s\n", e1.message);
                                running = false;
+                               timeout_quit ();
                                return;
                        }
                }
@@ -142,10 +149,20 @@ class IMDbDownloadServer : Object, IMDbDownloader {
                sqlite = null;
                running = false;
 
-               // FIXME - use a timeout?
-               loop.quit ();
+               timeout_quit ();
+       }
+
+       private void timeout_quit () {
+               source_id = Timeout.add (3000, quit);
        }
 
+        private bool quit () {
+               loop.quit ();
+
+                // One-shot only
+                return false;
+        }
+
        async void download_async (File f, LineParser parser, int io_priority) throws Error {
                int percent = 0;
                unowned string line = null;