b7c85e25de942a8b278905350581ccbd53bc91a1
[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
53         public signal void search_finished (int movies);
54
55         private MovieSource _source;
56         public MovieSource source {
57                 get {
58                         return _source;
59                 }
60                 set {
61                         _source = value;
62                 }
63         }
64
65         construct {
66                 set_column_types (base_type);
67                 no_poster = null;
68                 source = null;
69                 update_running = false;
70
71                 poster_factory = MoviePoster.Factory.get_instance ();
72         }
73
74         public void add (Movie movie, out TreeIter iter) {
75                 TreeIter iter1;
76                 var markup = new StringBuilder ();
77                 markup.append (Markup.escape_text (movie.title));
78                 if (movie.year > 0) {
79                         markup.append (" ");
80                         markup.append_printf (year_markup, movie.year);
81                 }
82
83                 append (out iter1);
84                 base.set (iter1, 0, movie,
85                                  1, markup.str,
86                                  2, (movie.rating >= 0) ? "%d.%d".printf (movie.rating / 10, movie.rating % 10) : null);
87
88                 movie.notify.connect (this.on_movie_changed);
89
90                 iter = iter1;
91         }
92
93         public new bool remove (Movie movie) {
94                 TreeIter iter;
95
96                 if (get_iter_from_movie (out iter, movie)) {
97                         movie.notify.disconnect (this.on_movie_changed);
98                         base.remove (iter);
99
100                         if (SourceFlags.EDITABLE in source.get_flags ()) {
101                                 source.delete_movie (movie);
102                         }
103
104                         return true;
105                 }
106
107                 return false;
108         }
109
110         private void on_movie_changed (GLib.Object source, GLib.ParamSpec spec) {
111                 var movie = (Movie) source;
112
113                 TreeIter iter;
114                 if (get_iter_from_movie (out iter, movie)) {
115                         TreePath path = get_path (iter);
116                         base.row_changed (path, iter);
117                 }
118         }
119
120         public bool get_editable () {
121                 return (SourceFlags.EDITABLE in source.get_flags ());
122         }
123
124         public bool get_iter_from_movie (out TreeIter iter, Movie movie_a) {
125                 if (get_iter_first (out iter)) {
126                         do {
127                                 Movie movie_b;
128                                 get (iter, Columns.MOVIE, out movie_b);
129                                 if (movie_a == movie_b)
130                                         return true;
131                         } while (iter_next (ref iter));
132                 }
133                 return false;
134         }
135
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 ();
141                         return false;
142                 }
143                 if (cancellable == null || cancellable.is_cancelled ())
144                         cancellable = new Cancellable ();
145
146                 filter = _filter;
147                 stdout.printf ("begin search\n");
148                 search_async.begin ();
149                 update_running = true;
150                 return true;
151         }
152
153         // Asynchronous update method
154         private async void search_async () {
155                 stdout.printf ("search started: \"%s\"\n", filter.title);
156
157                 clear ();
158
159                 if (source != null) {
160                         // FIXME - arbitrary limit
161                         int n = yield source.get_movies (filter, receive_movie, 100, cancellable);
162                         search_finished (n);
163                 }
164
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");
171                         }
172                         start_search (filter);
173                 } else {
174                         stdout.printf ("search stopped\n");
175                 }
176         }
177
178         private void receive_movie (SList<Movie> movies) {
179                 TreeIter iter;
180
181                 if (cancellable.is_cancelled ())
182                         return;
183
184                 foreach (Movie movie in movies)
185                         add (movie, out iter);
186         }
187
188         private void receive_poster_icon (Gdk.Pixbuf pixbuf, Movie movie) {
189                 var poster = new Poster ();
190                 poster.icon = pixbuf;
191                 movie.poster = poster;
192         }
193
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;
200         }
201
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);
205
206                 return types[index_];
207         }
208
209         public virtual int get_n_columns () {
210                 return Columns.N_COLUMNS;
211         }
212
213         public virtual void get_value (TreeIter iter, int column, out GLib.Value value) {
214                 Movie movie;
215
216                 return_if_fail (column >= 0 && column < Columns.N_COLUMNS);
217
218                 // Get the Movie from our parent's storage
219                 Value val;
220                 base.get_value (iter, 0, out val);
221                 movie = (Movie) val.get_object ();
222
223                 value.init (get_column_type (column));
224
225                 switch (column) {
226                 case Columns.TITLE:
227                         if (movie != null) {
228                                 value.set_string (movie.title);
229                         } else {
230                                 value.set_string ("");
231                         }
232                         break;
233
234                 case Columns.YEAR:
235                         if (movie != null) {
236                                 value.set_int (movie.year);
237                         } else {
238                                 value.set_int (-1);
239                         }
240                         break;
241
242                 case Columns.RATING:
243                         base.get_value (iter, 2, out value);
244                         break;
245
246                 case Columns.POSTER:
247                         if ((movie.poster != null) && (movie.poster.small != null)) {
248                                 value.set_object (movie.poster.small);
249                         } else {
250                                 // FIXME
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);
255                                         no_poster.fill (0);
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);
258                                 } catch (Error e) {
259                                         critical ("Missing general_video icon: %s\n", e.message);
260                                 }
261                                 value.set_object (no_poster);
262                         }
263                         break;
264
265                 case Columns.ICON:
266                         if ((movie.poster != null) && (movie.poster.icon != null)) {
267                                 value.set_object (movie.poster.icon);
268                         } else {
269                                 value.set_object (null);
270                         }
271                         break;
272
273                 case Columns.MOVIE:
274                         value.set_object (movie);
275                         break;
276
277                 case Columns.MARKUP:
278                         base.get_value (iter, 1, out value);
279                         break;
280
281                 default:
282                         assert_not_reached ();
283                 }
284         }
285 }