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 {
32 private GLib.Type[] types = {
41 private GLib.Type[] base_type = {
43 typeof (string), // Markup: "Title (Year)"
44 typeof (string) // Rating
46 private Gdk.Pixbuf no_poster;
47 private MoviePoster.Factory poster_factory;
48 private MovieFilter filter;
49 public bool update_running { get; set; }
50 public string year_markup = "<span size=\"small\">[%d]</span>";
51 private Cancellable cancellable;
53 public signal void search_finished (int movies);
55 private MovieSource _source;
56 public MovieSource source {
66 set_column_types (base_type);
69 update_running = false;
71 poster_factory = MoviePoster.Factory.get_instance ();
74 public void add (Movie movie, out TreeIter iter) {
76 var markup = new StringBuilder ();
77 markup.append (Markup.escape_text (movie.title));
80 markup.append_printf (year_markup, movie.year);
84 base.set (iter1, 0, movie,
86 2, (movie.rating >= 0) ? "%d.%d".printf (movie.rating / 10, movie.rating % 10) : null);
88 movie.notify.connect (this.on_movie_changed);
93 public new bool remove (Movie movie) {
96 if (get_iter_from_movie (out iter, movie)) {
97 movie.notify.disconnect (this.on_movie_changed);
100 if (SourceFlags.EDITABLE in source.get_flags ()) {
101 source.delete_movie (movie);
110 private void on_movie_changed (GLib.Object source, GLib.ParamSpec spec) {
111 var movie = (Movie) source;
114 if (get_iter_from_movie (out iter, movie)) {
115 TreePath path = get_path (iter);
116 base.row_changed (path, iter);
120 public bool get_editable () {
121 return (SourceFlags.EDITABLE in source.get_flags ());
124 public bool get_iter_from_movie (out TreeIter iter, Movie movie_a) {
125 if (get_iter_first (out iter)) {
128 get (iter, Columns.MOVIE, out movie_b);
129 if (movie_a == movie_b)
131 } while (iter_next (ref iter));
136 public bool start_search (MovieFilter _filter) {
137 if (update_running) {
138 stdout.printf ("aborting search ...\n");
139 cancellable.cancel ();
140 poster_factory.clear_queue ();
143 if (cancellable == null || cancellable.is_cancelled ())
144 cancellable = new Cancellable ();
147 stdout.printf ("begin search\n");
148 search_async.begin ();
149 update_running = true;
153 // Asynchronous update method
154 private async void search_async () {
155 stdout.printf ("search started: \"%s\"\n", filter.title);
159 if (source != null) {
160 // FIXME - arbitrary limit
161 int n = yield source.get_movies (filter, receive_movie, 100, cancellable);
165 update_running = false;
166 if (cancellable.is_cancelled ()) {
167 stdout.printf ("search aborted, starting new\n");
168 cancellable.reset ();
169 if (cancellable.is_cancelled ()) {
170 stdout.printf ("OW WEY\n");
172 start_search (filter);
174 stdout.printf ("search stopped\n");
178 private void receive_movie (SList<Movie> movies) {
181 if (cancellable.is_cancelled ())
184 foreach (Movie movie in movies) {
185 add (movie, out iter);
187 poster_factory.queue_thumbnail (movie, Poster.ICON_WIDTH, Poster.ICON_HEIGHT, false, receive_poster_icon);
188 poster_factory.queue_thumbnail (movie, Poster.SMALL_WIDTH, Poster.SMALL_HEIGHT, false, receive_poster_small);
190 warning ("Failed to queue poster request: %s\n", e.message);
195 private void receive_poster_icon (Gdk.Pixbuf pixbuf, Movie movie) {
196 var poster = new Poster ();
197 poster.icon = pixbuf;
198 movie.poster = poster;
201 private void receive_poster_small (Gdk.Pixbuf pixbuf, Movie movie) {
202 var poster = new Poster ();
203 if (movie.poster != null && movie.poster.icon != null)
204 poster.icon = movie.poster.icon;
205 poster.small = pixbuf;
206 movie.poster = poster;
209 // Implement TreeModel interface
210 public virtual GLib.Type get_column_type (int index_) {
211 return_val_if_fail (index_ >= 0 && index_ < Columns.N_COLUMNS, 0);
213 return types[index_];
216 public virtual int get_n_columns () {
217 return Columns.N_COLUMNS;
220 public virtual void get_value (TreeIter iter, int column, out GLib.Value value) {
224 if (no_poster == null) try {
225 no_poster = new Gdk.Pixbuf.from_file ("/usr/share/icons/hicolor/64x64/hildon/general_video.png");
227 critical ("Missing general_video icon: %s\n", e.message);
230 return_if_fail (column >= 0 && column < Columns.N_COLUMNS);
232 // Get the Movie from our parent's storage
234 base.get_value (iter, 0, out val);
235 movie = (Movie) val.get_object ();
237 value.init (get_column_type (column));
242 value.set_string (movie.title);
244 value.set_string ("");
250 value.set_int (movie.year);
257 base.get_value (iter, 2, out value);
261 if ((movie.poster != null) && (movie.poster.small != null))
262 value.set_object (movie.poster.small);
264 value.set_object (no_poster);
268 if ((movie.poster != null) && (movie.poster.icon != null))
269 value.set_object (movie.poster.icon);
271 value.set_object (no_poster);
275 value.set_object (movie);
279 base.get_value (iter, 1, out value);
283 assert_not_reached ();