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);
188 private void receive_poster_icon (Gdk.Pixbuf pixbuf, Movie movie) {
189 var poster = new Poster ();
190 poster.icon = pixbuf;
191 movie.poster = poster;
194 private void receive_poster_small (Gdk.Pixbuf pixbuf, Movie movie) {
195 var poster = new Poster ();
196 if (movie.poster != null && movie.poster.icon != null)
197 poster.icon = movie.poster.icon;
198 poster.small = pixbuf;
199 movie.poster = poster;
202 // Implement TreeModel interface
203 public virtual GLib.Type get_column_type (int index_) {
204 return_val_if_fail (index_ >= 0 && index_ < Columns.N_COLUMNS, 0);
206 return types[index_];
209 public virtual int get_n_columns () {
210 return Columns.N_COLUMNS;
213 public virtual void get_value (TreeIter iter, int column, out GLib.Value value) {
216 return_if_fail (column >= 0 && column < Columns.N_COLUMNS);
218 // Get the Movie from our parent's storage
220 base.get_value (iter, 0, out val);
221 movie = (Movie) val.get_object ();
223 value.init (get_column_type (column));
228 value.set_string (movie.title);
230 value.set_string ("");
236 value.set_int (movie.year);
243 base.get_value (iter, 2, out value);
247 if ((movie.poster != null) && (movie.poster.small != null)) {
248 value.set_object (movie.poster.small);
251 if (no_poster == null) try {
252 // var no_pic = new Gdk.Pixbuf.from_file ("/usr/share/icons/hicolor/64x64/hildon/imageviewer_no_pic.png");
253 var no_pic = new Gdk.Pixbuf.from_file ("/usr/share/icons/hicolor/64x64/hildon/general_no_thumbnail.png");
254 no_poster = new Gdk.Pixbuf (Gdk.Colorspace.RGB, true, 8, Poster.SMALL_WIDTH, Poster.SMALL_HEIGHT);
256 no_pic.copy_area (0, 0, no_pic.width, no_pic.height, no_poster,
257 (Poster.SMALL_WIDTH - no_pic.width) / 2, (Poster.SMALL_HEIGHT - no_pic.height) / 2);
259 critical ("Missing general_video icon: %s\n", e.message);
261 value.set_object (no_poster);
266 if ((movie.poster != null) && (movie.poster.icon != null)) {
267 value.set_object (movie.poster.icon);
269 value.set_object (null);
274 value.set_object (movie);
278 base.get_value (iter, 1, out value);
282 assert_not_reached ();