+ icons.item_activated.connect (on_item_activated);
+ store.row_changed.connect (on_row_changed);
+ store.search_finished.connect (on_search_finished);
+ }
+
+ construct {
+ hscrollbar_policy = Gtk.PolicyType.NEVER;
+
+ poster_factory = MoviePoster.Factory.get_instance ();
+ }
+
+ public void set_hildon_ui_mode (UIMode mode) {
+ var selection = tree.get_selection ();
+
+ if (mode == UIMode.NORMAL) {
+ selection.set_mode (SelectionMode.NONE);
+ icons.set_selection_mode (SelectionMode.NONE);
+ }
+ Hildon.gtk_tree_view_set_ui_mode (tree, mode);
+ Hildon.gtk_icon_view_set_ui_mode (icons, mode);
+ if (mode == UIMode.EDIT) {
+ selection.set_mode (SelectionMode.MULTIPLE);
+ icons.set_selection_mode (SelectionMode.MULTIPLE);
+ }
+ }
+
+ private Pango.AttrList get_attributes (Gtk.Window window, string font_name, string color_name) {
+ Pango.AttrList attr_list = new Pango.AttrList ();
+ var style = Gtk.rc_get_style_by_paths (Gtk.Settings.get_default (), font_name, null, typeof (void));
+ if (style != null) {
+ var attr_font_desc = new Pango.AttrFontDesc (style.font_desc.copy ());
+ attr_list.insert ((owned) attr_font_desc);
+ }
+ Gdk.Color color;
+ window.ensure_style ();
+ if (window.style.lookup_color (color_name, out color)) {
+ Pango.Attribute attr_color = Pango.attr_foreground_new (color.red, color.green, color.blue);
+ attr_list.insert ((owned) attr_color);
+ }
+ return attr_list;
+ }
+
+ public void unselect_all () {
+ tree.get_selection ().unselect_all ();
+ icons.unselect_all ();
+ }
+
+ public List<Movie> get_selected_movies () {
+ var movies = new List<Movie> ();
+ List<TreePath> paths;
+
+ if (poster_mode_)
+ paths = icons.get_selected_items ();
+ else
+ paths = tree.get_selection ().get_selected_rows (null);
+
+ // get selected movies from the store
+ foreach (TreePath path in paths) {
+ TreeIter iter;
+
+ if (store.get_iter (out iter, path)) {
+ Movie movie;
+
+ store.get (iter, MovieListStore.Columns.MOVIE, out movie);
+ if (movie != null) {
+ movies.append (movie);
+ }
+ }
+ }
+
+ return movies;
+ }
+
+ // TODO: after scrolling down 80% of the list, load more
+ // results if available.
+ private void on_adjustment_value_changed () {
+ if (more_movies_available) {
+ var vadj = get_vadjustment ();
+ if (vadj.value > 0.8 * vadj.upper) {
+ Banner.show_information (this, null, _("More results available - refine search to reduce the dataset"));
+ more_movies_available = false;
+ }
+ }
+
+ TreePath a_;
+ TreePath b_;
+ bool range;
+ if (poster_mode_) {
+ range = icons.get_visible_range (out a_, out b_);
+ } else {
+ range = tree.get_visible_range (out a_, out b_);
+ }
+ if (range) {
+ // We know the list store is flat
+ int a = a_.get_indices ()[0];
+ int b = b_.get_indices ()[0];
+ assert (a <= b);
+
+ if (a == last_a && b == last_b)
+ return;
+
+ if (a > last_b || b < last_a) {
+ check_posters (a, b);
+ } else if (a >= last_a && b > last_b) {
+ check_posters (last_b + 1, b);
+ } else if (b <= last_b && a < last_a) {
+ check_posters (a, last_a - 1);
+ }
+
+ last_a = a;
+ last_b = b;
+ }
+ }
+
+ private void check_posters (int a, int b) {
+ for (int i = a; i <= b; i++) {
+ var path = new TreePath.from_indices (i);
+ TreeIter iter;
+ if (store.get_iter (out iter, path)) {
+ Movie movie;
+ store.get (iter, MovieListStore.Columns.MOVIE, out movie);
+ if (movie != null) {
+ if (poster_mode_) {
+ if (movie.poster == null || movie.poster.small == null) try {
+ poster_factory.queue_thumbnail (movie, Poster.SMALL_WIDTH, Poster.SMALL_HEIGHT, false, receive_poster_small);
+ } catch (Error e) {
+ warning ("Failed to queue poster request: %s\n", e.message);
+ }
+ } else {
+ if (movie.poster == null || movie.poster.icon == null) try {
+ poster_factory.queue_thumbnail (movie, Poster.ICON_WIDTH, Poster.ICON_HEIGHT, false, receive_poster_icon);
+ } catch (Error e) {
+ warning ("Failed to queue poster request: %s\n", e.message);
+ }
+ }
+ }
+ }
+ }
+ }
+
+ private void on_row_activated (TreeView tree, TreePath path, TreeViewColumn column) {
+ on_item_activated (path);