Make search thread cancellable
[cinaest] / src / movie-list-store.vala
index e75894d..1ad805f 100644 (file)
@@ -41,6 +41,8 @@ public class MovieListStore : ListStore, TreeModel {
        public MovieSource source;
        private MovieFilter filter;
        public bool update_running { get; set; }
+       private weak Thread thread;
+       private Cancellable cancellable;
 
        construct {
                set_column_types (base_type);
@@ -83,12 +85,19 @@ public class MovieListStore : ListStore, TreeModel {
        }
 
        public bool start_search (MovieFilter _filter) {
-               if (update_running)
-                       return false;
+               if (update_running) {
+                       stdout.printf ("aborting search thread (%p)...\n", thread);
+                       cancellable.cancel ();
+               //      poster_factory.clear_queue ();
+                       thread.join ();
+                       stdout.printf ("search thread aborted\n");
+               }
+               if (cancellable == null || cancellable.is_cancelled ())
+                       cancellable = new Cancellable ();
 
                filter = _filter;
                try {
-                       Thread.create (search_thread, false);
+                       thread = Thread.create (search_thread, true);
                        update_running = true;
                } catch (ThreadError e) {
                        warning ("Failed to start search thread: %s", e.message);
@@ -106,7 +115,7 @@ public class MovieListStore : ListStore, TreeModel {
 
                if (source != null)
                        // FIXME - arbitrary limit
-                       source.get_movies (filter, receive_movie, 100);
+                       source.get_movies (filter, receive_movie, 100, cancellable);
 
                Gdk.threads_enter ();
                update_running = false;
@@ -119,6 +128,9 @@ public class MovieListStore : ListStore, TreeModel {
        private void receive_movie (Movie movie) {
                TreeIter iter;
 
+               if (cancellable.is_cancelled ())
+                       return;
+
                Gdk.threads_enter ();
                add (movie, out iter);
                Gdk.threads_leave ();