Movie list store: use insert_with_values instead of append + set
[cinaest] / src / movie-list-store.vala
index 3bfd156..c87d1da 100644 (file)
@@ -24,44 +24,94 @@ public class MovieListStore : ListStore, TreeModel {
                YEAR,
                RATING,
                POSTER,
+               ICON,
                MOVIE,
+               MARKUP,
                N_COLUMNS
        }
        private GLib.Type[] types = {
                typeof (string),
                typeof (int),
-               typeof (int),
+               typeof (string),
+               typeof (Gdk.Pixbuf),
                typeof (Gdk.Pixbuf),
-               typeof (Movie)
+               typeof (Movie),
+               typeof (string)
        };
        private GLib.Type[] base_type = {
-               typeof (Movie)
+               typeof (Movie),
+               typeof (string), // Markup: "Title (Year)"
+               typeof (string)  // Rating
        };
        private Gdk.Pixbuf no_poster;
-       public MovieSource source;
-       private string query;
+       private MoviePoster.Factory poster_factory;
+       private MovieFilter filter;
        public bool update_running { get; set; }
+       public string year_markup = "<span size=\"small\">[%d]</span>";
+       private Cancellable cancellable;
+       public Widget view;
+
+       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) {
                TreeIter iter1;
+               var markup = new StringBuilder ();
+               markup.append (Markup.escape_text (movie.title));
+               if (movie.year > 0) {
+                       markup.append (" ");
+                       markup.append_printf (year_markup, movie.year);
+               }
 
                append (out iter1);
-               base.set (iter1, 0, movie);
 
-               movie.notify.connect ((source, property) => { on_movie_changed(source); });
+               base.insert_with_values (out iter1, -1,
+                                        0, movie,
+                                        1, markup.str,
+                                        2, (movie.rating >= 0) ? "%.1f".printf (movie.rating / 10.0) : null);
+
+               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 (SourceFlags.EDITABLE in source.get_flags ()) {
+                               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)) {
@@ -70,6 +120,10 @@ public class MovieListStore : ListStore, TreeModel {
                }
        }
 
+       public bool get_editable () {
+               return (SourceFlags.EDITABLE in source.get_flags ());
+       }
+
        public bool get_iter_from_movie (out TreeIter iter, Movie movie_a) {
                if (get_iter_first (out iter)) {
                        do {
@@ -82,48 +136,74 @@ public class MovieListStore : ListStore, TreeModel {
                return false;
        }
 
-       public bool start_search (string _query) {
-               if (update_running)
+       public bool start_search (MovieFilter _filter) {
+               if (update_running) {
+                       stdout.printf ("aborting search ...\n");
+                       cancellable.cancel ();
+                       poster_factory.clear_queue ();
                        return false;
-
-               query = _query;
-               try {
-                       Thread.create (search_thread, false);
-                       update_running = true;
-               } catch (ThreadError e) {
-                       warning ("Failed to start search thread: %s", e.message);
                }
-               return update_running;
+               if (cancellable == null || cancellable.is_cancelled ())
+                       cancellable = new Cancellable ();
+
+               filter = _filter;
+               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", query);
+       // 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 (query, receive_movie, 100);
+                       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) {
+       private void receive_movie (SList<Movie> movies) {
                TreeIter iter;
 
-               Gdk.threads_enter ();
-               add (movie, out iter);
-               Gdk.threads_leave ();
+               if (cancellable.is_cancelled ())
+                       return;
+
+               view.freeze_child_notify ();
+               foreach (Movie movie in movies)
+                       add (movie, out iter);
+               view.thaw_child_notify ();
        }
 
+       private void receive_poster_icon (Gdk.Pixbuf pixbuf, Movie movie) {
+               var poster = new Poster ();
+               poster.icon = pixbuf;
+               movie.poster = poster;
+       }
+
+       private void receive_poster_small (Gdk.Pixbuf pixbuf, Movie movie) {
+               var poster = new Poster ();
+               if (movie.poster != null && movie.poster.icon != null)
+                       poster.icon = movie.poster.icon;
+               poster.small = 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);
@@ -138,13 +218,6 @@ public class MovieListStore : ListStore, TreeModel {
        public virtual void get_value (TreeIter iter, int column, out GLib.Value value) {
                Movie movie;
 
-               // FIXME
-               if (no_poster == null) try {
-                       no_poster = new Gdk.Pixbuf.from_file ("/usr/share/icons/hicolor/64x64/hildon/general_video.png");
-               } catch (Error e) {
-                       critical ("Missing general_video icon: %s\n", e.message);
-               }
-
                return_if_fail (column >= 0 && column < Columns.N_COLUMNS);
 
                // Get the Movie from our parent's storage
@@ -172,24 +245,44 @@ public class MovieListStore : ListStore, TreeModel {
                        break;
 
                case Columns.RATING:
-                       if (movie != null) {
-                               value.set_int (movie.rating);
-                       } else {
-                               value.set_int (-1);
-                       }
+                       base.get_value (iter, 2, out value);
                        break;
 
                case Columns.POSTER:
-                       if ((movie.poster != null) && (movie.poster.thumbnail != null))
-                               value.set_object (movie.poster.thumbnail);
-                       else
+                       if ((movie.poster != null) && (movie.poster.small != null)) {
+                               value.set_object (movie.poster.small);
+                       } else {
+                               // FIXME
+                               if (no_poster == null) try {
+                               //      var no_pic = new Gdk.Pixbuf.from_file ("/usr/share/icons/hicolor/64x64/hildon/imageviewer_no_pic.png");
+                                       var no_pic = new Gdk.Pixbuf.from_file ("/usr/share/icons/hicolor/64x64/hildon/general_no_thumbnail.png");
+                                       no_poster = new Gdk.Pixbuf (Gdk.Colorspace.RGB, true, 8, Poster.SMALL_WIDTH, Poster.SMALL_HEIGHT);
+                                       no_poster.fill (0);
+                                       no_pic.copy_area (0, 0, no_pic.width, no_pic.height, no_poster,
+                                                         (Poster.SMALL_WIDTH - no_pic.width) / 2, (Poster.SMALL_HEIGHT - no_pic.height) / 2);
+                               } catch (Error e) {
+                                       critical ("Missing general_video icon: %s\n", e.message);
+                               }
                                value.set_object (no_poster);
+                       }
+                       break;
+
+               case Columns.ICON:
+                       if ((movie.poster != null) && (movie.poster.icon != null)) {
+                               value.set_object (movie.poster.icon);
+                       } else {
+                               value.set_object (null);
+                       }
                        break;
 
                case Columns.MOVIE:
                        value.set_object (movie);
                        break;
 
+               case Columns.MARKUP:
+                       base.get_value (iter, 1, out value);
+                       break;
+
                default:
                        assert_not_reached ();
                }