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 private MovieFilter filter;
43 public bool update_running { get; set; }
44 private Cancellable cancellable;
46 public signal void search_finished (int movies);
48 private MovieSource _source;
49 public MovieSource source {
59 set_column_types (base_type);
62 update_running = false;
64 poster_factory = MoviePoster.Factory.get_instance ();
67 public void add (Movie movie, out TreeIter iter) {
71 base.set (iter1, 0, movie);
73 movie.notify.connect (this.on_movie_changed);
78 public new bool remove (Movie movie) {
81 if (get_iter_from_movie (out iter, movie)) {
82 movie.notify.disconnect (this.on_movie_changed);
85 if (source.get_editable ()) {
86 source.delete_movie (movie);
95 private void on_movie_changed (GLib.Object source, GLib.ParamSpec spec) {
96 var movie = (Movie) source;
99 if (get_iter_from_movie (out iter, movie)) {
100 TreePath path = get_path (iter);
101 base.row_changed (path, iter);
105 public bool get_editable () {
106 return source.get_editable ();
109 public bool get_iter_from_movie (out TreeIter iter, Movie movie_a) {
110 if (get_iter_first (out iter)) {
113 get (iter, Columns.MOVIE, out movie_b);
114 if (movie_a == movie_b)
116 } while (iter_next (ref iter));
121 public bool start_search (MovieFilter _filter) {
122 if (update_running) {
123 stdout.printf ("aborting search ...\n");
124 cancellable.cancel ();
125 poster_factory.clear_queue ();
128 if (cancellable == null || cancellable.is_cancelled ())
129 cancellable = new Cancellable ();
132 stdout.printf ("begin search\n");
133 search_async.begin ();
134 update_running = true;
138 // Asynchronous update method
139 private async void search_async () {
140 stdout.printf ("search started: \"%s\"\n", filter.title);
144 if (source != null) {
145 // FIXME - arbitrary limit
146 int n = yield source.get_movies (filter, receive_movie, 100, cancellable);
150 update_running = false;
151 if (cancellable.is_cancelled ()) {
152 stdout.printf ("search aborted, starting new\n");
153 cancellable.reset ();
154 if (cancellable.is_cancelled ()) {
155 stdout.printf ("OW WEY\n");
157 start_search (filter);
159 stdout.printf ("search stopped\n");
163 private void receive_movie (Movie movie) {
166 if (cancellable.is_cancelled ())
169 add (movie, out iter);
171 poster_factory.queue_thumbnail (movie, 64, 64, false, receive_poster_thumbnail);
173 warning ("Failed to queue poster request: %s\n", e.message);
177 private void receive_poster_thumbnail (Gdk.Pixbuf pixbuf, Movie movie) {
178 var poster = new Poster ();
179 poster.thumbnail = pixbuf;
180 movie.poster = poster;
183 // Implement TreeModel interface
184 public virtual GLib.Type get_column_type (int index_) {
185 return_val_if_fail (index_ >= 0 && index_ < Columns.N_COLUMNS, 0);
187 return types[index_];
190 public virtual int get_n_columns () {
191 return Columns.N_COLUMNS;
194 public virtual void get_value (TreeIter iter, int column, out GLib.Value value) {
198 if (no_poster == null) try {
199 no_poster = new Gdk.Pixbuf.from_file ("/usr/share/icons/hicolor/64x64/hildon/general_video.png");
201 critical ("Missing general_video icon: %s\n", e.message);
204 return_if_fail (column >= 0 && column < Columns.N_COLUMNS);
206 // Get the Movie from our parent's storage
208 base.get_value (iter, 0, out val);
209 movie = (Movie) val.get_object ();
211 value.init (get_column_type (column));
216 value.set_string (movie.title);
218 value.set_string ("");
224 value.set_int (movie.year);
232 value.set_int (movie.rating);
239 if ((movie.poster != null) && (movie.poster.thumbnail != null))
240 value.set_object (movie.poster.thumbnail);
242 value.set_object (no_poster);
246 value.set_object (movie);
250 assert_not_reached ();