Movie list store: hold a reference to the view and freeze when updating
[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                 ICON,
28                 MOVIE,
29                 MARKUP,
30                 N_COLUMNS
31         }
32         private GLib.Type[] types = {
33                 typeof (string),
34                 typeof (int),
35                 typeof (string),
36                 typeof (Gdk.Pixbuf),
37                 typeof (Gdk.Pixbuf),
38                 typeof (Movie),
39                 typeof (string)
40         };
41         private GLib.Type[] base_type = {
42                 typeof (Movie),
43                 typeof (string), // Markup: "Title (Year)"
44                 typeof (string)  // Rating
45         };
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;
52         public Widget view;
53
54         public signal void search_finished (int movies);
55
56         private MovieSource _source;
57         public MovieSource source {
58                 get {
59                         return _source;
60                 }
61                 set {
62                         _source = value;
63                 }
64         }
65
66         construct {
67                 set_column_types (base_type);
68                 no_poster = null;
69                 source = null;
70                 update_running = false;
71
72                 poster_factory = MoviePoster.Factory.get_instance ();
73         }
74
75         public void add (Movie movie, out TreeIter iter) {
76                 TreeIter iter1;
77                 var markup = new StringBuilder ();
78                 markup.append (Markup.escape_text (movie.title));
79                 if (movie.year > 0) {
80                         markup.append (" ");
81                         markup.append_printf (year_markup, movie.year);
82                 }
83
84                 append (out iter1);
85                 base.set (iter1, 0, movie,
86                                  1, markup.str,
87                                  2, (movie.rating >= 0) ? "%d.%d".printf (movie.rating / 10, movie.rating % 10) : null);
88
89                 movie.notify.connect (this.on_movie_changed);
90
91                 iter = iter1;
92         }
93
94         public new bool remove (Movie movie) {
95                 TreeIter iter;
96
97                 if (get_iter_from_movie (out iter, movie)) {
98                         movie.notify.disconnect (this.on_movie_changed);
99                         base.remove (iter);
100
101                         if (SourceFlags.EDITABLE in source.get_flags ()) {
102                                 source.delete_movie (movie);
103                         }
104
105                         return true;
106                 }
107
108                 return false;
109         }
110
111         private void on_movie_changed (GLib.Object source, GLib.ParamSpec spec) {
112                 var movie = (Movie) source;
113
114                 TreeIter iter;
115                 if (get_iter_from_movie (out iter, movie)) {
116                         TreePath path = get_path (iter);
117                         base.row_changed (path, iter);
118                 }
119         }
120
121         public bool get_editable () {
122                 return (SourceFlags.EDITABLE in source.get_flags ());
123         }
124
125         public bool get_iter_from_movie (out TreeIter iter, Movie movie_a) {
126                 if (get_iter_first (out iter)) {
127                         do {
128                                 Movie movie_b;
129                                 get (iter, Columns.MOVIE, out movie_b);
130                                 if (movie_a == movie_b)
131                                         return true;
132                         } while (iter_next (ref iter));
133                 }
134                 return false;
135         }
136
137         public bool start_search (MovieFilter _filter) {
138                 if (update_running) {
139                         stdout.printf ("aborting search ...\n");
140                         cancellable.cancel ();
141                         poster_factory.clear_queue ();
142                         return false;
143                 }
144                 if (cancellable == null || cancellable.is_cancelled ())
145                         cancellable = new Cancellable ();
146
147                 filter = _filter;
148                 stdout.printf ("begin search\n");
149                 search_async.begin ();
150                 update_running = true;
151                 return true;
152         }
153
154         // Asynchronous update method
155         private async void search_async () {
156                 stdout.printf ("search started: \"%s\"\n", filter.title);
157
158                 clear ();
159
160                 if (source != null) {
161                         // FIXME - arbitrary limit
162                         int n = yield source.get_movies (filter, receive_movie, 100, cancellable);
163                         search_finished (n);
164                 }
165
166                 update_running = false;
167                 if (cancellable.is_cancelled ()) {
168                         stdout.printf ("search aborted, starting new\n");
169                         cancellable.reset ();
170                         if (cancellable.is_cancelled ()) {
171                                 stdout.printf ("OW WEY\n");
172                         }
173                         start_search (filter);
174                 } else {
175                         stdout.printf ("search stopped\n");
176                 }
177         }
178
179         private void receive_movie (SList<Movie> movies) {
180                 TreeIter iter;
181
182                 if (cancellable.is_cancelled ())
183                         return;
184
185                 view.freeze_child_notify ();
186                 foreach (Movie movie in movies)
187                         add (movie, out iter);
188                 view.thaw_child_notify ();
189         }
190
191         private void receive_poster_icon (Gdk.Pixbuf pixbuf, Movie movie) {
192                 var poster = new Poster ();
193                 poster.icon = pixbuf;
194                 movie.poster = poster;
195         }
196
197         private void receive_poster_small (Gdk.Pixbuf pixbuf, Movie movie) {
198                 var poster = new Poster ();
199                 if (movie.poster != null && movie.poster.icon != null)
200                         poster.icon = movie.poster.icon;
201                 poster.small = pixbuf;
202                 movie.poster = poster;
203         }
204
205         // Implement TreeModel interface
206         public virtual GLib.Type get_column_type (int index_) {
207                 return_val_if_fail (index_ >= 0 && index_ < Columns.N_COLUMNS, 0);
208
209                 return types[index_];
210         }
211
212         public virtual int get_n_columns () {
213                 return Columns.N_COLUMNS;
214         }
215
216         public virtual void get_value (TreeIter iter, int column, out GLib.Value value) {
217                 Movie movie;
218
219                 return_if_fail (column >= 0 && column < Columns.N_COLUMNS);
220
221                 // Get the Movie from our parent's storage
222                 Value val;
223                 base.get_value (iter, 0, out val);
224                 movie = (Movie) val.get_object ();
225
226                 value.init (get_column_type (column));
227
228                 switch (column) {
229                 case Columns.TITLE:
230                         if (movie != null) {
231                                 value.set_string (movie.title);
232                         } else {
233                                 value.set_string ("");
234                         }
235                         break;
236
237                 case Columns.YEAR:
238                         if (movie != null) {
239                                 value.set_int (movie.year);
240                         } else {
241                                 value.set_int (-1);
242                         }
243                         break;
244
245                 case Columns.RATING:
246                         base.get_value (iter, 2, out value);
247                         break;
248
249                 case Columns.POSTER:
250                         if ((movie.poster != null) && (movie.poster.small != null)) {
251                                 value.set_object (movie.poster.small);
252                         } else {
253                                 // FIXME
254                                 if (no_poster == null) try {
255                                 //      var no_pic = new Gdk.Pixbuf.from_file ("/usr/share/icons/hicolor/64x64/hildon/imageviewer_no_pic.png");
256                                         var no_pic = new Gdk.Pixbuf.from_file ("/usr/share/icons/hicolor/64x64/hildon/general_no_thumbnail.png");
257                                         no_poster = new Gdk.Pixbuf (Gdk.Colorspace.RGB, true, 8, Poster.SMALL_WIDTH, Poster.SMALL_HEIGHT);
258                                         no_poster.fill (0);
259                                         no_pic.copy_area (0, 0, no_pic.width, no_pic.height, no_poster,
260                                                           (Poster.SMALL_WIDTH - no_pic.width) / 2, (Poster.SMALL_HEIGHT - no_pic.height) / 2);
261                                 } catch (Error e) {
262                                         critical ("Missing general_video icon: %s\n", e.message);
263                                 }
264                                 value.set_object (no_poster);
265                         }
266                         break;
267
268                 case Columns.ICON:
269                         if ((movie.poster != null) && (movie.poster.icon != null)) {
270                                 value.set_object (movie.poster.icon);
271                         } else {
272                                 value.set_object (null);
273                         }
274                         break;
275
276                 case Columns.MOVIE:
277                         value.set_object (movie);
278                         break;
279
280                 case Columns.MARKUP:
281                         base.get_value (iter, 1, out value);
282                         break;
283
284                 default:
285                         assert_not_reached ();
286                 }
287         }
288 }