Movie list store: clear the poster factory queue when aborting a search
[cinaest] / src / movie-list-store.vala
1 /* This file is part of Cinaest.
2  *
3  * Copyright (C) 2009 Philipp Zabel
4  *
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.
9  *
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.
14  *
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/>.
17  */
18
19 using Gtk;
20
21 public class MovieListStore : ListStore, TreeModel {
22         public enum Columns {
23                 TITLE,
24                 YEAR,
25                 RATING,
26                 POSTER,
27                 MOVIE,
28                 N_COLUMNS
29         }
30         private GLib.Type[] types = {
31                 typeof (string),
32                 typeof (int),
33                 typeof (int),
34                 typeof (Gdk.Pixbuf),
35                 typeof (Movie)
36         };
37         private GLib.Type[] base_type = {
38                 typeof (Movie)
39         };
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;
46
47         construct {
48                 set_column_types (base_type);
49                 no_poster = null;
50                 source = null;
51                 update_running = false;
52
53                 poster_factory = MoviePoster.Factory.get_instance ();
54         }
55
56         public void add (Movie movie, out TreeIter iter) {
57                 TreeIter iter1;
58
59                 append (out iter1);
60                 base.set (iter1, 0, movie);
61
62                 movie.notify.connect (this.on_movie_changed);
63
64                 iter = iter1;
65         }
66
67         private void on_movie_changed (GLib.Object source, GLib.ParamSpec spec) {
68                 var movie = (Movie) source;
69
70                 TreeIter iter;
71                 if (get_iter_from_movie (out iter, movie)) {
72                         TreePath path = get_path (iter);
73                         base.row_changed (path, iter);
74                 }
75         }
76
77         public bool get_iter_from_movie (out TreeIter iter, Movie movie_a) {
78                 if (get_iter_first (out iter)) {
79                         do {
80                                 Movie movie_b;
81                                 get (iter, Columns.MOVIE, out movie_b);
82                                 if (movie_a == movie_b)
83                                         return true;
84                         } while (iter_next (ref iter));
85                 }
86                 return false;
87         }
88
89         public bool start_search (MovieFilter _filter) {
90                 if (update_running) {
91                         stdout.printf ("aborting search ...\n");
92                         cancellable.cancel ();
93                         poster_factory.clear_queue ();
94                         return false;
95                 }
96                 if (cancellable == null || cancellable.is_cancelled ())
97                         cancellable = new Cancellable ();
98
99                 filter = _filter;
100                 stdout.printf ("begin search\n");
101                 search_async.begin ();
102                 update_running = true;
103                 return true;
104         }
105
106         // Asynchronous update method
107         private async void search_async () {
108                 stdout.printf ("search started: \"%s\"\n", filter.title);
109
110                 clear ();
111
112                 if (source != null)
113                         // FIXME - arbitrary limit
114                         yield source.get_movies (filter, receive_movie, 100, cancellable);
115
116                 update_running = false;
117                 if (cancellable.is_cancelled ()) {
118                         stdout.printf ("search aborted, starting new\n");
119                         cancellable.reset ();
120                         if (cancellable.is_cancelled ()) {
121                                 stdout.printf ("OW WEY\n");
122                         }
123                         start_search (filter);
124                 } else {
125                         stdout.printf ("search stopped\n");
126                 }
127         }
128
129         private void receive_movie (Movie movie) {
130                 TreeIter iter;
131
132                 if (cancellable.is_cancelled ())
133                         return;
134
135                 add (movie, out iter);
136                 try {
137                         poster_factory.queue_thumbnail (movie, 64, 64, false, receive_poster_thumbnail);
138                 } catch (Error e) {
139                         warning ("Failed to queue poster request: %s\n", e.message);
140                 }
141         }
142
143         private void receive_poster_thumbnail (Gdk.Pixbuf pixbuf, Movie movie) {
144                 var poster = new Poster ();
145                 poster.thumbnail = pixbuf;
146                 movie.poster = poster;
147         }
148
149         // Implement TreeModel interface
150         public virtual GLib.Type get_column_type (int index_) {
151                 return_val_if_fail (index_ >= 0 && index_ < Columns.N_COLUMNS, 0);
152
153                 return types[index_];
154         }
155
156         public virtual int get_n_columns () {
157                 return Columns.N_COLUMNS;
158         }
159
160         public virtual void get_value (TreeIter iter, int column, out GLib.Value value) {
161                 Movie movie;
162
163                 // FIXME
164                 if (no_poster == null) try {
165                         no_poster = new Gdk.Pixbuf.from_file ("/usr/share/icons/hicolor/64x64/hildon/general_video.png");
166                 } catch (Error e) {
167                         critical ("Missing general_video icon: %s\n", e.message);
168                 }
169
170                 return_if_fail (column >= 0 && column < Columns.N_COLUMNS);
171
172                 // Get the Movie from our parent's storage
173                 Value val;
174                 base.get_value (iter, 0, out val);
175                 movie = (Movie) val.get_object ();
176
177                 value.init (get_column_type (column));
178
179                 switch (column) {
180                 case Columns.TITLE:
181                         if (movie != null) {
182                                 value.set_string (movie.title);
183                         } else {
184                                 value.set_string ("");
185                         }
186                         break;
187
188                 case Columns.YEAR:
189                         if (movie != null) {
190                                 value.set_int (movie.year);
191                         } else {
192                                 value.set_int (-1);
193                         }
194                         break;
195
196                 case Columns.RATING:
197                         if (movie != null) {
198                                 value.set_int (movie.rating);
199                         } else {
200                                 value.set_int (-1);
201                         }
202                         break;
203
204                 case Columns.POSTER:
205                         if ((movie.poster != null) && (movie.poster.thumbnail != null))
206                                 value.set_object (movie.poster.thumbnail);
207                         else
208                                 value.set_object (no_poster);
209                         break;
210
211                 case Columns.MOVIE:
212                         value.set_object (movie);
213                         break;
214
215                 default:
216                         assert_not_reached ();
217                 }
218         }
219 }