Movie list store: open/close sources when needed
[cinaest] / src / movie-list-store.vala
index 1ad805f..5957fe6 100644 (file)
@@ -38,17 +38,30 @@ public class MovieListStore : ListStore, TreeModel {
                typeof (Movie)
        };
        private Gdk.Pixbuf no_poster;
-       public MovieSource source;
+       private MoviePoster.Factory poster_factory;
        private MovieFilter filter;
        public bool update_running { get; set; }
-       private weak Thread thread;
        private Cancellable cancellable;
 
+       public signal void search_finished (int movies);
+
+       private MovieSource _source;
+       public MovieSource source {
+               get {
+                       return _source;
+               }
+               set {
+                       _source = value;
+               }
+       }
+
        construct {
                set_column_types (base_type);
                no_poster = null;
                source = null;
                update_running = false;
+
+               poster_factory = MoviePoster.Factory.get_instance ();
        }
 
        public void add (Movie movie, out TreeIter iter) {
@@ -57,13 +70,30 @@ public class MovieListStore : ListStore, TreeModel {
                append (out iter1);
                base.set (iter1, 0, movie);
 
-               movie.notify.connect ((source, property) => { on_movie_changed(source); });
+               movie.notify.connect (this.on_movie_changed);
 
                iter = iter1;
        }
 
-       private void on_movie_changed (GLib.Object source) {
-               Movie movie = (Movie) source;
+       public new bool remove (Movie movie) {
+               TreeIter iter;
+
+               if (get_iter_from_movie (out iter, movie)) {
+                       movie.notify.disconnect (this.on_movie_changed);
+                       base.remove (iter);
+
+                       if (source.get_editable ()) {
+                               source.delete_movie (movie);
+                       }
+
+                       return true;
+               }
+
+               return false;
+       }
+
+       private void on_movie_changed (GLib.Object source, GLib.ParamSpec spec) {
+               var movie = (Movie) source;
 
                TreeIter iter;
                if (get_iter_from_movie (out iter, movie)) {
@@ -72,6 +102,10 @@ public class MovieListStore : ListStore, TreeModel {
                }
        }
 
+       public bool get_editable () {
+               return source.get_editable ();
+       }
+
        public bool get_iter_from_movie (out TreeIter iter, Movie movie_a) {
                if (get_iter_first (out iter)) {
                        do {
@@ -86,43 +120,44 @@ public class MovieListStore : ListStore, TreeModel {
 
        public bool start_search (MovieFilter _filter) {
                if (update_running) {
-                       stdout.printf ("aborting search thread (%p)...\n", thread);
+                       stdout.printf ("aborting search ...\n");
                        cancellable.cancel ();
-               //      poster_factory.clear_queue ();
-                       thread.join ();
-                       stdout.printf ("search thread aborted\n");
+                       poster_factory.clear_queue ();
+                       return false;
                }
                if (cancellable == null || cancellable.is_cancelled ())
                        cancellable = new Cancellable ();
 
                filter = _filter;
-               try {
-                       thread = Thread.create (search_thread, true);
-                       update_running = true;
-               } catch (ThreadError e) {
-                       warning ("Failed to start search thread: %s", e.message);
-               }
-               return update_running;
+               stdout.printf ("begin search\n");
+               search_async.begin ();
+               update_running = true;
+               return true;
        }
 
-       // Update thread
-       private void* search_thread () {
-               stdout.printf ("search thread started: \"%s\"\n", filter.title);
+       // Asynchronous update method
+       private async void search_async () {
+               stdout.printf ("search started: \"%s\"\n", filter.title);
 
-               Gdk.threads_enter ();
                clear ();
-               Gdk.threads_leave ();
 
-               if (source != null)
+               if (source != null) {
                        // FIXME - arbitrary limit
-                       source.get_movies (filter, receive_movie, 100, cancellable);
+                       int n = yield source.get_movies (filter, receive_movie, 100, cancellable);
+                       search_finished (n);
+               }
 
-               Gdk.threads_enter ();
                update_running = false;
-               Gdk.threads_leave ();
-
-               stdout.printf ("search thread stopped\n");
-               return null;
+               if (cancellable.is_cancelled ()) {
+                       stdout.printf ("search aborted, starting new\n");
+                       cancellable.reset ();
+                       if (cancellable.is_cancelled ()) {
+                               stdout.printf ("OW WEY\n");
+                       }
+                       start_search (filter);
+               } else {
+                       stdout.printf ("search stopped\n");
+               }
        }
 
        private void receive_movie (Movie movie) {
@@ -131,11 +166,20 @@ public class MovieListStore : ListStore, TreeModel {
                if (cancellable.is_cancelled ())
                        return;
 
-               Gdk.threads_enter ();
                add (movie, out iter);
-               Gdk.threads_leave ();
+               try {
+                       poster_factory.queue_thumbnail (movie, 64, 64, false, receive_poster_thumbnail);
+               } catch (Error e) {
+                       warning ("Failed to queue poster request: %s\n", e.message);
+               }
        }
 
+       private void receive_poster_thumbnail (Gdk.Pixbuf pixbuf, Movie movie) {
+               var poster = new Poster ();
+               poster.thumbnail = pixbuf;
+               movie.poster = poster;
+       }
+
        // Implement TreeModel interface
        public virtual GLib.Type get_column_type (int index_) {
                return_val_if_fail (index_ >= 0 && index_ < Columns.N_COLUMNS, 0);