9bf23f305ee4bce7f8f19a2ca5ece86848a4e2ca
[cinaest] / src / movie-list-view.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 using Hildon;
21
22 public class MovieListView : PannableArea {
23         public MovieListStore store;
24         TreeView tree;
25         IconView icons;
26
27         private bool more_movies_available;
28         private CellRendererText title_renderer;
29         private CellRendererText secondary_renderer;
30         private CellRendererText rating_renderer;
31         private CellRendererText date_renderer;
32         private MoviePoster.Factory poster_factory;
33         private int last_a = -1;
34         private int last_b = -1;
35
36         private bool poster_mode_;
37         public bool poster_mode {
38                 get {
39                         return poster_mode_;
40                 }
41                 set {
42                         if (value & !poster_mode_) {
43                                 remove (tree);
44                                 tree.set_model (null);
45                                 icons.set_model (store);
46                                 store.view = (Widget) icons;
47                                 add (icons);
48                         } else if (!value & poster_mode_) {
49                                 remove (icons);
50                                 icons.set_model (null);
51                                 tree.set_model (store);
52                                 store.view = (Widget) tree;
53                                 add (tree);
54                         }
55                         if (value != poster_mode_) {
56                                 poster_mode_ = value;
57                                 last_a = -1;
58                                 last_b = -1;
59
60                                 // FIXME - just calling on_adjustment_value_changed () here doesn't work
61                                 // because icons.get_visible_range () returns wrong paths.
62                                 check_posters (0, 10);
63                                 jump_to (0, 0);
64                         }
65                 }
66         }
67
68         public signal void movie_activated (Movie movie);
69
70         private Gtk.TreeView create_treeview (Gtk.Window window, bool show_date) {
71                 // Tree View
72                 var tree = (TreeView) Hildon.gtk_tree_view_new_with_model (UIMode.NORMAL, store);
73                 tree.set_headers_visible (false);
74                 tree.set_rules_hint (true);
75
76                 // Tree selection object
77                 var selection = tree.get_selection ();
78                 selection.set_mode (SelectionMode.SINGLE);
79
80                 // Title column with poster
81                 var title_column = new TreeViewColumn ();
82                 title_column.set_title (_("Movie"));
83                 title_column.set_sort_column_id (MovieListStore.Columns.TITLE);
84                 title_column.set_reorderable (false);
85                 title_column.set_sizing (TreeViewColumnSizing.AUTOSIZE);
86                 title_column.set_expand (true);
87
88                 // Add poster icon to column
89                 var pixbuf_renderer = new CellRendererPixbuf ();
90                 pixbuf_renderer.width = 64;
91                 pixbuf_renderer.xalign = 0.0f;
92                 title_column.pack_start (pixbuf_renderer, false);
93                 title_column.add_attribute (pixbuf_renderer, "pixbuf", MovieListStore.Columns.ICON);
94
95                 // Add text to column
96                 var vbox_renderer = new CellRendererVBox ();
97
98                 title_renderer = new CellRendererText ();
99                 title_renderer.yalign = 1.0f;
100                 title_renderer.ellipsize = Pango.EllipsizeMode.END;
101
102                 vbox_renderer.append (title_renderer, true);
103
104                 // Add secondary text to column (Genres, Director, etc.)
105                 secondary_renderer = new CellRendererText ();
106                 secondary_renderer.yalign = 0;
107                 secondary_renderer.ellipsize = Pango.EllipsizeMode.END;
108                 secondary_renderer.attributes = get_attributes (window, "SmallSystemFont", "SecondaryTextColor");
109
110                 vbox_renderer.append (secondary_renderer, true);
111
112                 title_column.pack_start (vbox_renderer, true);
113                 title_column.set_cell_data_func (vbox_renderer, title_data_func);
114
115                 tree.append_column (title_column);
116
117                 // Year column
118                 var year_column = new TreeViewColumn ();
119                 year_column.set_title (_("Year"));
120                 year_column.set_sort_column_id (MovieListStore.Columns.YEAR);
121                 year_column.set_reorderable (false);
122                 year_column.set_sort_order (SortType.DESCENDING);
123                 tree.append_column (year_column);
124
125                 // Rating column
126                 var rating_column = new TreeViewColumn ();
127                 rating_column.set_title (_("Rating"));
128                 rating_column.set_sort_column_id (MovieListStore.Columns.RATING);
129                 rating_column.set_reorderable (false);
130                 rating_column.set_sort_order (SortType.DESCENDING);
131                 rating_column.xalign = 1.0f;
132
133                 vbox_renderer = new CellRendererVBox ();
134
135                 rating_renderer = new CellRendererText ();
136                 rating_renderer.xalign = 1.0f;
137                 if (show_date)
138                         rating_renderer.yalign = 1.0f;
139
140                 vbox_renderer.append (rating_renderer, true);
141
142                 date_renderer = new CellRendererText ();
143                 date_renderer.yalign = 0;
144                 date_renderer.attributes = get_attributes (window, "SmallSystemFont", "SecondaryTextColor");
145
146                 if (show_date)
147                         vbox_renderer.append (date_renderer, true);
148
149                 rating_column.pack_start (vbox_renderer, true);
150                 rating_column.set_cell_data_func (vbox_renderer, rating_data_func);
151
152                 tree.append_column (rating_column);
153                 tree.show ();
154                 return tree;
155         }
156
157         private Gtk.IconView create_iconview () {
158                 var iconview = (Gtk.IconView) Hildon.gtk_icon_view_new (Hildon.UIMode.NORMAL);
159                 iconview.set_column_spacing (0);
160                 iconview.set_pixbuf_column (MovieListStore.Columns.POSTER);
161                 iconview.margin = 0;
162                 iconview.item_width = Poster.SMALL_WIDTH;
163                 iconview.column_spacing = Hildon.MARGIN_HALF;
164                 iconview.row_spacing = Hildon.MARGIN_HALF;
165                 iconview.show ();
166
167                 return iconview;
168         }
169
170         public MovieListView (Gtk.Window window, bool show_date = false) {
171                 store = new MovieListStore ();
172
173                 // Sort by title
174                 store.set_sort_column_id (MovieListStore.Columns.TITLE, SortType.ASCENDING);
175
176                 Gdk.Color color;
177                 window.ensure_style ();
178                 if (window.style.lookup_color ("SecondaryTextColor", out color)) {
179                         store.year_markup = "<span size=\"small\" fgcolor=\"%s\">(%%d)</span>".printf (color.to_string ());
180                 }
181
182                 tree = create_treeview (window, show_date);
183                 store.view = (Widget) tree;
184
185                 icons = create_iconview ();
186
187                 add (tree);
188
189                 // Connect signals
190                 get_vadjustment ().value_changed.connect (on_adjustment_value_changed);
191                 tree.row_activated.connect (on_row_activated);
192                 icons.item_activated.connect (on_item_activated);
193                 store.row_changed.connect (on_row_changed);
194                 store.row_inserted.connect (on_row_changed);
195                 store.search_finished.connect (on_search_finished);
196         }
197
198         construct {
199                 hscrollbar_policy = Gtk.PolicyType.NEVER;
200
201                 poster_factory = MoviePoster.Factory.get_instance ();
202         }
203
204         public void set_hildon_ui_mode (UIMode mode) {
205                 var selection = tree.get_selection ();
206
207                 if (mode == UIMode.NORMAL) {
208                         selection.set_mode (SelectionMode.NONE);
209                         icons.set_selection_mode (SelectionMode.NONE);
210                 }
211                 Hildon.gtk_tree_view_set_ui_mode (tree, mode);
212                 Hildon.gtk_icon_view_set_ui_mode (icons, mode);
213                 if (mode == UIMode.EDIT) {
214                         selection.set_mode (SelectionMode.MULTIPLE);
215                         icons.set_selection_mode (SelectionMode.MULTIPLE);
216                 }
217         }
218
219         private Pango.AttrList get_attributes (Gtk.Window window, string font_name, string color_name) {
220                 Pango.AttrList attr_list = new Pango.AttrList ();
221                 var style = Gtk.rc_get_style_by_paths (Gtk.Settings.get_default (), font_name, null, typeof (void));
222                 if (style != null) {
223                         var attr_font_desc = new Pango.AttrFontDesc (style.font_desc.copy ());
224                         attr_list.insert ((owned) attr_font_desc);
225                 }
226                 Gdk.Color color;
227                 window.ensure_style ();
228                 if (window.style.lookup_color (color_name, out color)) {
229                         Pango.Attribute attr_color = Pango.attr_foreground_new (color.red, color.green, color.blue);
230                         attr_list.insert ((owned) attr_color);
231                 }
232                 return attr_list;
233         }
234
235         public void unselect_all () {
236                 tree.get_selection ().unselect_all ();
237                 icons.unselect_all ();
238         }
239
240         public List<Movie> get_selected_movies () {
241                 var movies = new List<Movie> ();
242                 List<TreePath> paths;
243
244                 if (poster_mode_)
245                         paths = icons.get_selected_items ();
246                 else
247                         paths = tree.get_selection ().get_selected_rows (null);
248
249                 // get selected movies from the store
250                 foreach (TreePath path in paths) {
251                         TreeIter iter;
252
253                         if (store.get_iter (out iter, path)) {
254                                 Movie movie;
255
256                                 store.get (iter, MovieListStore.Columns.MOVIE, out movie);
257                                 if (movie != null) {
258                                         movies.append (movie);
259                                 }
260                         }
261                 }
262
263                 return movies;
264         }
265
266         // TODO: after scrolling down 80% of the list, load more
267         //       results if available.
268         private void on_adjustment_value_changed () {
269                 if (more_movies_available) {
270                         var vadj = get_vadjustment ();
271                         if (vadj.value > 0.8 * vadj.upper) {
272                                 Banner.show_information (this, null, _("More results available - refine search to reduce the dataset"));
273                                 more_movies_available = false;
274                         }
275                 }
276
277                 TreePath a_;
278                 TreePath b_;
279                 bool range;
280                 if (poster_mode_) {
281                         range = icons.get_visible_range (out a_, out b_);
282                 } else {
283                         range = tree.get_visible_range (out a_, out b_);
284                 }
285                 if (range) {
286                         // We know the list store is flat
287                         int a = a_.get_indices ()[0];
288                         int b = b_.get_indices ()[0];
289                         assert (a <= b);
290
291                         if (a == last_a && b == last_b)
292                                 return;
293
294                         if (a > last_b || b < last_a) {
295                                 check_posters (a, b);
296                         } else if (a >= last_a && b > last_b) {
297                                 check_posters (last_b + 1, b);
298                         } else if (b <= last_b && a < last_a) {
299                                 check_posters (a, last_a - 1);
300                         }
301
302                         last_a = a;
303                         last_b = b;
304                 }
305         }
306
307         private void check_posters (int a, int b) {
308                 for (int i = a; i <= b; i++) {
309                         var path = new TreePath.from_indices (i);
310                         TreeIter iter;
311                         if (!store.get_iter (out iter, path))
312                                 continue;
313
314                         Movie movie;
315                         store.get (iter, MovieListStore.Columns.MOVIE, out movie);
316                         if (movie == null)
317                                 continue;
318
319                         if (poster_mode_) {
320                                 if (movie.poster == null || movie.poster.small == null) try {
321                                         poster_factory.queue (movie,
322                                                               Poster.SMALL_WIDTH,
323                                                               Poster.SMALL_HEIGHT,
324                                                               true,
325                                                               receive_poster_small);
326                                 } catch (Error e) {
327                                         warning ("Failed to queue poster request: %s\n", e.message);
328                                 }
329                         } else {
330                                 if (movie.poster == null || movie.poster.icon == null) try {
331                                         poster_factory.queue (movie,
332                                                               Poster.ICON_WIDTH,
333                                                               Poster.ICON_HEIGHT,
334                                                               true,
335                                                               receive_poster_icon);
336                                 } catch (Error e) {
337                                         warning ("Failed to queue poster request: %s\n", e.message);
338                                 }
339                         }
340                 }
341         }
342
343         private void on_row_activated (TreeView tree, TreePath path, TreeViewColumn column) {
344                 on_item_activated (path);
345         }
346
347         private void on_item_activated (TreePath path) {
348                 TreeIter iter;
349
350                 if (store.get_iter (out iter, path)) {
351                         Movie movie;
352                         store.get (iter, MovieListStore.Columns.MOVIE, out movie);
353                         movie_activated (movie);
354                 }
355         }
356
357         private void on_row_changed (TreePath path, TreeIter iter) {
358                 TreePath a;
359                 TreePath b;
360                 bool range;
361
362                 if (poster_mode_) {
363                         range = icons.get_visible_range (out a, out b);
364                 } else {
365                         range = tree.get_visible_range (out a, out b);
366                 }
367                 if (!range ||
368                     (range && path.compare (a) >= 0 && path.compare (b) <= 0)) {
369                         Movie movie;
370
371                         store.get (iter, MovieListStore.Columns.MOVIE, out movie);
372                         if (movie == null)
373                                 return;
374
375                         int i = path.get_indices ()[0];
376                         check_posters (i, i);
377                 }
378         }
379
380         private void receive_poster_small (Gdk.Pixbuf pixbuf, Movie movie) {
381                 Poster poster;
382                 if (movie.poster != null)
383                         poster = movie.poster;
384                 else
385                         poster = new Poster ();
386                 poster.small = pixbuf;
387                 // Notify store of the poster change
388                 movie.poster = poster;
389         }
390
391         private void receive_poster_icon (Gdk.Pixbuf pixbuf, Movie movie) {
392                 Poster poster;
393                 if (movie.poster != null)
394                         poster = movie.poster;
395                 else
396                         poster = new Poster ();
397                 poster.icon = pixbuf;
398                 // Notify store of the poster change
399                 movie.poster = poster;
400         }
401
402         private void on_search_finished (int movies) {
403                 more_movies_available = (movies > 100); // FIXME
404         }
405
406         private void title_data_func (CellLayout cell_layout, CellRenderer cell, TreeModel model, TreeIter iter) {
407                 Movie movie;
408                 string markup;
409
410                 model.get (iter, MovieListStore.Columns.MOVIE, out movie,
411                                  MovieListStore.Columns.MARKUP, out markup);
412                 title_renderer.markup = markup;
413                 secondary_renderer.text = movie.secondary;
414         }
415
416         private void rating_data_func (CellLayout cell_layout, CellRenderer cell, TreeModel model, TreeIter iter) {
417                 string rating;
418                 Movie movie;
419
420                 model.get (iter, MovieListStore.Columns.RATING, out rating,
421                                  MovieListStore.Columns.MOVIE, out movie);
422                 rating_renderer.text = rating;
423                 if (movie.julian_date != 0) {
424                         var date = Date ();
425                         date.set_julian (movie.julian_date);
426                         var s = new char[64];
427                         date.strftime (s, "%x");
428                         date_renderer.text = (string) s;
429                 } else {
430                         date_renderer.text = "";
431                 }
432         }
433 }