1 /* This file is part of Cinaest.
3 * Copyright (C) 2009 Philipp Zabel
5 * Cinaest is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * Cinaest is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with Cinaest. If not, see <http://www.gnu.org/licenses/>.
21 public class MovieListStore : ListStore, TreeModel {
30 private GLib.Type[] types = {
37 private GLib.Type[] base_type = {
40 private Gdk.Pixbuf no_poster;
41 public MovieSource source;
42 private MovieFilter filter;
43 public bool update_running { get; set; }
44 private weak Thread thread;
45 private Cancellable cancellable;
48 set_column_types (base_type);
51 update_running = false;
54 public void add (Movie movie, out TreeIter iter) {
58 base.set (iter1, 0, movie);
60 movie.notify.connect ((source, property) => { on_movie_changed(source); });
65 private void on_movie_changed (GLib.Object source) {
66 Movie movie = (Movie) source;
69 if (get_iter_from_movie (out iter, movie)) {
70 TreePath path = get_path (iter);
71 base.row_changed (path, iter);
75 public bool get_iter_from_movie (out TreeIter iter, Movie movie_a) {
76 if (get_iter_first (out iter)) {
79 get (iter, Columns.MOVIE, out movie_b);
80 if (movie_a == movie_b)
82 } while (iter_next (ref iter));
87 public bool start_search (MovieFilter _filter) {
89 stdout.printf ("aborting search thread (%p)...\n", thread);
90 cancellable.cancel ();
91 // poster_factory.clear_queue ();
93 stdout.printf ("search thread aborted\n");
95 if (cancellable == null || cancellable.is_cancelled ())
96 cancellable = new Cancellable ();
100 thread = Thread.create (search_thread, true);
101 update_running = true;
102 } catch (ThreadError e) {
103 warning ("Failed to start search thread: %s", e.message);
105 return update_running;
109 private void* search_thread () {
110 stdout.printf ("search thread started: \"%s\"\n", filter.title);
112 Gdk.threads_enter ();
114 Gdk.threads_leave ();
117 // FIXME - arbitrary limit
118 source.get_movies (filter, receive_movie, 100, cancellable);
120 Gdk.threads_enter ();
121 update_running = false;
122 Gdk.threads_leave ();
124 stdout.printf ("search thread stopped\n");
128 private void receive_movie (Movie movie) {
131 if (cancellable.is_cancelled ())
134 Gdk.threads_enter ();
135 add (movie, out iter);
136 Gdk.threads_leave ();
139 // Implement TreeModel interface
140 public virtual GLib.Type get_column_type (int index_) {
141 return_val_if_fail (index_ >= 0 && index_ < Columns.N_COLUMNS, 0);
143 return types[index_];
146 public virtual int get_n_columns () {
147 return Columns.N_COLUMNS;
150 public virtual void get_value (TreeIter iter, int column, out GLib.Value value) {
154 if (no_poster == null) try {
155 no_poster = new Gdk.Pixbuf.from_file ("/usr/share/icons/hicolor/64x64/hildon/general_video.png");
157 critical ("Missing general_video icon: %s\n", e.message);
160 return_if_fail (column >= 0 && column < Columns.N_COLUMNS);
162 // Get the Movie from our parent's storage
164 base.get_value (iter, 0, out val);
165 movie = (Movie) val.get_object ();
167 value.init (get_column_type (column));
172 value.set_string (movie.title);
174 value.set_string ("");
180 value.set_int (movie.year);
188 value.set_int (movie.rating);
195 if ((movie.poster != null) && (movie.poster.thumbnail != null))
196 value.set_object (movie.poster.thumbnail);
198 value.set_object (no_poster);
202 value.set_object (movie);
206 assert_not_reached ();