Add a search thread to the movie list store
[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                 MOVIE,
28                 N_COLUMNS
29         }
30         private GLib.Type[] types = {
31                 typeof (string),
32                 typeof (int),
33                 typeof (int),
34                 typeof (Gdk.Pixbuf),
35                 typeof (Movie)
36         };
37         private GLib.Type[] base_type = {
38                 typeof (Movie)
39         };
40         private Gdk.Pixbuf no_poster;
41         public MovieSource source;
42         private string query;
43         public bool update_running;
44
45         construct {
46                 set_column_types (base_type);
47                 no_poster = null;
48                 source = null;
49                 update_running = false;
50         }
51
52         public void add (Movie movie, out TreeIter iter) {
53                 TreeIter iter1;
54
55                 append (out iter1);
56                 base.set (iter1, 0, movie);
57
58                 movie.notify.connect ((source, property) => { on_movie_changed(source); });
59
60                 iter = iter1;
61         }
62
63         private void on_movie_changed (GLib.Object source) {
64                 Movie movie = (Movie) source;
65
66                 TreeIter iter;
67                 if (get_iter_from_movie (out iter, movie)) {
68                         TreePath path = get_path (iter);
69                         base.row_changed (path, iter);
70                 }
71         }
72
73         public bool get_iter_from_movie (out TreeIter iter, Movie movie_a) {
74                 if (get_iter_first (out iter)) {
75                         do {
76                                 Movie movie_b;
77                                 get (iter, Columns.MOVIE, out movie_b);
78                                 if (movie_a == movie_b)
79                                         return true;
80                         } while (iter_next (ref iter));
81                 }
82                 return false;
83         }
84
85         public bool start_search (string _query) {
86                 if (update_running)
87                         return false;
88
89                 query = _query;
90                 try {
91                         Thread.create (search_thread, false);
92                         update_running = true;
93                 } catch (ThreadError e) {
94                         warning ("Failed to start search thread: %s", e.message);
95                 }
96                 return update_running;
97         }
98
99         // Update thread
100         private void* search_thread () {
101                 stdout.printf ("search thread started: \"%s\"\n", query);
102
103                 Gdk.threads_enter ();
104                 clear ();
105                 Gdk.threads_leave ();
106
107                 if (source != null)
108                         // FIXME - arbitrary limit
109                         source.get_movies (query, receive_movie, 100);
110
111                 update_running = false;
112
113                 stdout.printf ("search thread stopped\n");
114                 return null;
115         }
116
117         private void receive_movie (Movie movie) {
118                 TreeIter iter;
119
120                 Gdk.threads_enter ();
121                 add (movie, out iter);
122                 Gdk.threads_leave ();
123         }
124
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);
128
129                 return types[index_];
130         }
131
132         public virtual int get_n_columns () {
133                 return Columns.N_COLUMNS;
134         }
135
136         public virtual void get_value (TreeIter iter, int column, out GLib.Value value) {
137                 Movie movie;
138
139                 // FIXME
140                 if (no_poster == null) try {
141                         no_poster = new Gdk.Pixbuf.from_file ("/usr/share/icons/hicolor/64x64/hildon/general_video.png");
142                 } catch (Error e) {
143                         critical ("Missing general_video icon: %s\n", e.message);
144                 }
145
146                 return_if_fail (column >= 0 && column < Columns.N_COLUMNS);
147
148                 // Get the Movie from our parent's storage
149                 Value val;
150                 base.get_value (iter, 0, out val);
151                 movie = (Movie) val.get_object ();
152
153                 value.init (get_column_type (column));
154
155                 switch (column) {
156                 case Columns.TITLE:
157                         if (movie != null) {
158                                 value.set_string (movie.title);
159                         } else {
160                                 value.set_string ("");
161                         }
162                         break;
163
164                 case Columns.YEAR:
165                         if (movie != null) {
166                                 value.set_int (movie.year);
167                         } else {
168                                 value.set_int (-1);
169                         }
170                         break;
171
172                 case Columns.RATING:
173                         if (movie != null) {
174                                 value.set_int (movie.rating);
175                         } else {
176                                 value.set_int (-1);
177                         }
178                         break;
179
180                 case Columns.POSTER:
181                         if ((movie.poster != null) && (movie.poster.thumbnail != null))
182                                 value.set_object (movie.poster.thumbnail);
183                         else
184                                 value.set_object (no_poster);
185                         break;
186
187                 case Columns.MOVIE:
188                         value.set_object (movie);
189                         break;
190
191                 default:
192                         assert_not_reached ();
193                 }
194         }
195 }