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 private MoviePoster.Factory poster_factory;
42 public MovieSource source;
43 private MovieFilter filter;
44 public bool update_running { get; set; }
45 private Cancellable cancellable;
48 set_column_types (base_type);
51 update_running = false;
53 poster_factory = MoviePoster.Factory.get_instance ();
56 public void add (Movie movie, out TreeIter iter) {
60 base.set (iter1, 0, movie);
62 movie.notify.connect (this.on_movie_changed);
67 private void on_movie_changed (GLib.Object source, GLib.ParamSpec spec) {
68 var movie = (Movie) source;
71 if (get_iter_from_movie (out iter, movie)) {
72 TreePath path = get_path (iter);
73 base.row_changed (path, iter);
77 public bool get_editable () {
78 return source.get_editable ();
81 public bool get_iter_from_movie (out TreeIter iter, Movie movie_a) {
82 if (get_iter_first (out iter)) {
85 get (iter, Columns.MOVIE, out movie_b);
86 if (movie_a == movie_b)
88 } while (iter_next (ref iter));
93 public bool start_search (MovieFilter _filter) {
95 stdout.printf ("aborting search ...\n");
96 cancellable.cancel ();
97 poster_factory.clear_queue ();
100 if (cancellable == null || cancellable.is_cancelled ())
101 cancellable = new Cancellable ();
104 stdout.printf ("begin search\n");
105 search_async.begin ();
106 update_running = true;
110 // Asynchronous update method
111 private async void search_async () {
112 stdout.printf ("search started: \"%s\"\n", filter.title);
117 // FIXME - arbitrary limit
118 yield source.get_movies (filter, receive_movie, 100, cancellable);
120 update_running = false;
121 if (cancellable.is_cancelled ()) {
122 stdout.printf ("search aborted, starting new\n");
123 cancellable.reset ();
124 if (cancellable.is_cancelled ()) {
125 stdout.printf ("OW WEY\n");
127 start_search (filter);
129 stdout.printf ("search stopped\n");
133 private void receive_movie (Movie movie) {
136 if (cancellable.is_cancelled ())
139 add (movie, out iter);
141 poster_factory.queue_thumbnail (movie, 64, 64, false, receive_poster_thumbnail);
143 warning ("Failed to queue poster request: %s\n", e.message);
147 private void receive_poster_thumbnail (Gdk.Pixbuf pixbuf, Movie movie) {
148 var poster = new Poster ();
149 poster.thumbnail = pixbuf;
150 movie.poster = poster;
153 // Implement TreeModel interface
154 public virtual GLib.Type get_column_type (int index_) {
155 return_val_if_fail (index_ >= 0 && index_ < Columns.N_COLUMNS, 0);
157 return types[index_];
160 public virtual int get_n_columns () {
161 return Columns.N_COLUMNS;
164 public virtual void get_value (TreeIter iter, int column, out GLib.Value value) {
168 if (no_poster == null) try {
169 no_poster = new Gdk.Pixbuf.from_file ("/usr/share/icons/hicolor/64x64/hildon/general_video.png");
171 critical ("Missing general_video icon: %s\n", e.message);
174 return_if_fail (column >= 0 && column < Columns.N_COLUMNS);
176 // Get the Movie from our parent's storage
178 base.get_value (iter, 0, out val);
179 movie = (Movie) val.get_object ();
181 value.init (get_column_type (column));
186 value.set_string (movie.title);
188 value.set_string ("");
194 value.set_int (movie.year);
202 value.set_int (movie.rating);
209 if ((movie.poster != null) && (movie.poster.thumbnail != null))
210 value.set_object (movie.poster.thumbnail);
212 value.set_object (no_poster);
216 value.set_object (movie);
220 assert_not_reached ();