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 {
31 private GLib.Type[] types = {
39 private GLib.Type[] base_type = {
41 typeof (string), // Markup: "Title (Year)"
42 typeof (string) // Rating
44 private Gdk.Pixbuf no_poster;
45 private MoviePoster.Factory poster_factory;
46 private MovieFilter filter;
47 public bool update_running { get; set; }
48 public string year_markup = "<span size=\"small\">[%d]</span>";
49 private Cancellable cancellable;
51 public signal void search_finished (int movies);
53 private MovieSource _source;
54 public MovieSource source {
64 set_column_types (base_type);
67 update_running = false;
69 poster_factory = MoviePoster.Factory.get_instance ();
72 public void add (Movie movie, out TreeIter iter) {
74 var markup = new StringBuilder ();
75 markup.append (Markup.escape_text (movie.title));
78 markup.append_printf (year_markup, movie.year);
82 base.set (iter1, 0, movie,
84 2, (movie.rating >= 0) ? "%d.%d".printf (movie.rating / 10, movie.rating % 10) : null);
86 movie.notify.connect (this.on_movie_changed);
91 public new bool remove (Movie movie) {
94 if (get_iter_from_movie (out iter, movie)) {
95 movie.notify.disconnect (this.on_movie_changed);
98 if (SourceFlags.EDITABLE in source.get_flags ()) {
99 source.delete_movie (movie);
108 private void on_movie_changed (GLib.Object source, GLib.ParamSpec spec) {
109 var movie = (Movie) source;
112 if (get_iter_from_movie (out iter, movie)) {
113 TreePath path = get_path (iter);
114 base.row_changed (path, iter);
118 public bool get_editable () {
119 return (SourceFlags.EDITABLE in source.get_flags ());
122 public bool get_iter_from_movie (out TreeIter iter, Movie movie_a) {
123 if (get_iter_first (out iter)) {
126 get (iter, Columns.MOVIE, out movie_b);
127 if (movie_a == movie_b)
129 } while (iter_next (ref iter));
134 public bool start_search (MovieFilter _filter) {
135 if (update_running) {
136 stdout.printf ("aborting search ...\n");
137 cancellable.cancel ();
138 poster_factory.clear_queue ();
141 if (cancellable == null || cancellable.is_cancelled ())
142 cancellable = new Cancellable ();
145 stdout.printf ("begin search\n");
146 search_async.begin ();
147 update_running = true;
151 // Asynchronous update method
152 private async void search_async () {
153 stdout.printf ("search started: \"%s\"\n", filter.title);
157 if (source != null) {
158 // FIXME - arbitrary limit
159 int n = yield source.get_movies (filter, receive_movie, 100, cancellable);
163 update_running = false;
164 if (cancellable.is_cancelled ()) {
165 stdout.printf ("search aborted, starting new\n");
166 cancellable.reset ();
167 if (cancellable.is_cancelled ()) {
168 stdout.printf ("OW WEY\n");
170 start_search (filter);
172 stdout.printf ("search stopped\n");
176 private void receive_movie (SList<Movie> movies) {
179 if (cancellable.is_cancelled ())
182 foreach (Movie movie in movies) {
183 add (movie, out iter);
185 poster_factory.queue_thumbnail (movie, 64, 64, false, receive_poster_thumbnail);
187 warning ("Failed to queue poster request: %s\n", e.message);
192 private void receive_poster_thumbnail (Gdk.Pixbuf pixbuf, Movie movie) {
193 var poster = new Poster ();
194 poster.thumbnail = pixbuf;
195 movie.poster = poster;
198 // Implement TreeModel interface
199 public virtual GLib.Type get_column_type (int index_) {
200 return_val_if_fail (index_ >= 0 && index_ < Columns.N_COLUMNS, 0);
202 return types[index_];
205 public virtual int get_n_columns () {
206 return Columns.N_COLUMNS;
209 public virtual void get_value (TreeIter iter, int column, out GLib.Value value) {
213 if (no_poster == null) try {
214 no_poster = new Gdk.Pixbuf.from_file ("/usr/share/icons/hicolor/64x64/hildon/general_video.png");
216 critical ("Missing general_video icon: %s\n", e.message);
219 return_if_fail (column >= 0 && column < Columns.N_COLUMNS);
221 // Get the Movie from our parent's storage
223 base.get_value (iter, 0, out val);
224 movie = (Movie) val.get_object ();
226 value.init (get_column_type (column));
231 value.set_string (movie.title);
233 value.set_string ("");
239 value.set_int (movie.year);
246 base.get_value (iter, 2, out value);
250 if ((movie.poster != null) && (movie.poster.thumbnail != null))
251 value.set_object (movie.poster.thumbnail);
253 value.set_object (no_poster);
257 value.set_object (movie);
261 base.get_value (iter, 1, out value);
265 assert_not_reached ();