1 /* This file is part of Cinaest.
3 * Copyright (C) 2009 Philipp Zabel
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.
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.
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/>.
21 public class MovieListStore : ListStore, TreeModel {
30 private GLib.Type[] types = {
37 private GLib.Type[] base_type = {
40 private Gdk.Pixbuf no_poster;
41 public MovieSource source;
43 public bool update_running;
46 set_column_types (base_type);
49 update_running = false;
52 public void add (Movie movie, out TreeIter iter) {
56 base.set (iter1, 0, movie);
58 movie.notify.connect ((source, property) => { on_movie_changed(source); });
63 private void on_movie_changed (GLib.Object source) {
64 Movie movie = (Movie) source;
67 if (get_iter_from_movie (out iter, movie)) {
68 TreePath path = get_path (iter);
69 base.row_changed (path, iter);
73 public bool get_iter_from_movie (out TreeIter iter, Movie movie_a) {
74 if (get_iter_first (out iter)) {
77 get (iter, Columns.MOVIE, out movie_b);
78 if (movie_a == movie_b)
80 } while (iter_next (ref iter));
85 public bool start_search (string _query) {
91 Thread.create (search_thread, false);
92 update_running = true;
93 } catch (ThreadError e) {
94 warning ("Failed to start search thread: %s", e.message);
96 return update_running;
100 private void* search_thread () {
101 stdout.printf ("search thread started: \"%s\"\n", query);
103 Gdk.threads_enter ();
105 Gdk.threads_leave ();
108 // FIXME - arbitrary limit
109 source.get_movies (query, receive_movie, 100);
111 update_running = false;
113 stdout.printf ("search thread stopped\n");
117 private void receive_movie (Movie movie) {
120 Gdk.threads_enter ();
121 add (movie, out iter);
122 Gdk.threads_leave ();
125 // Implement TreeModel interface
126 public virtual GLib.Type get_column_type (int index_) {
127 return_val_if_fail (index_ >= 0 && index_ < Columns.N_COLUMNS, 0);
129 return types[index_];
132 public virtual int get_n_columns () {
133 return Columns.N_COLUMNS;
136 public virtual void get_value (TreeIter iter, int column, out GLib.Value value) {
140 if (no_poster == null) try {
141 no_poster = new Gdk.Pixbuf.from_file ("/usr/share/icons/hicolor/64x64/hildon/general_video.png");
143 critical ("Missing general_video icon: %s\n", e.message);
146 return_if_fail (column >= 0 && column < Columns.N_COLUMNS);
148 // Get the Movie from our parent's storage
150 base.get_value (iter, 0, out val);
151 movie = (Movie) val.get_object ();
153 value.init (get_column_type (column));
158 value.set_string (movie.title);
160 value.set_string ("");
166 value.set_int (movie.year);
174 value.set_int (movie.rating);
181 if ((movie.poster != null) && (movie.poster.thumbnail != null))
182 value.set_object (movie.poster.thumbnail);
184 value.set_object (no_poster);
188 value.set_object (movie);
192 assert_not_reached ();