Add movie filter class
[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 MovieFilter filter;
43         public bool update_running { get; set; }
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 (MovieFilter _filter) {
86                 if (update_running)
87                         return false;
88
89                 filter = _filter;
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", filter.title);
102
103                 Gdk.threads_enter ();
104                 clear ();
105                 Gdk.threads_leave ();
106
107                 if (source != null)
108                         // FIXME - arbitrary limit
109                         source.get_movies (filter, receive_movie, 100);
110
111                 Gdk.threads_enter ();
112                 update_running = false;
113                 Gdk.threads_leave ();
114
115                 stdout.printf ("search thread stopped\n");
116                 return null;
117         }
118
119         private void receive_movie (Movie movie) {
120                 TreeIter iter;
121
122                 Gdk.threads_enter ();
123                 add (movie, out iter);
124                 Gdk.threads_leave ();
125         }
126
127         // Implement TreeModel interface
128         public virtual GLib.Type get_column_type (int index_) {
129                 return_val_if_fail (index_ >= 0 && index_ < Columns.N_COLUMNS, 0);
130
131                 return types[index_];
132         }
133
134         public virtual int get_n_columns () {
135                 return Columns.N_COLUMNS;
136         }
137
138         public virtual void get_value (TreeIter iter, int column, out GLib.Value value) {
139                 Movie movie;
140
141                 // FIXME
142                 if (no_poster == null) try {
143                         no_poster = new Gdk.Pixbuf.from_file ("/usr/share/icons/hicolor/64x64/hildon/general_video.png");
144                 } catch (Error e) {
145                         critical ("Missing general_video icon: %s\n", e.message);
146                 }
147
148                 return_if_fail (column >= 0 && column < Columns.N_COLUMNS);
149
150                 // Get the Movie from our parent's storage
151                 Value val;
152                 base.get_value (iter, 0, out val);
153                 movie = (Movie) val.get_object ();
154
155                 value.init (get_column_type (column));
156
157                 switch (column) {
158                 case Columns.TITLE:
159                         if (movie != null) {
160                                 value.set_string (movie.title);
161                         } else {
162                                 value.set_string ("");
163                         }
164                         break;
165
166                 case Columns.YEAR:
167                         if (movie != null) {
168                                 value.set_int (movie.year);
169                         } else {
170                                 value.set_int (-1);
171                         }
172                         break;
173
174                 case Columns.RATING:
175                         if (movie != null) {
176                                 value.set_int (movie.rating);
177                         } else {
178                                 value.set_int (-1);
179                         }
180                         break;
181
182                 case Columns.POSTER:
183                         if ((movie.poster != null) && (movie.poster.thumbnail != null))
184                                 value.set_object (movie.poster.thumbnail);
185                         else
186                                 value.set_object (no_poster);
187                         break;
188
189                 case Columns.MOVIE:
190                         value.set_object (movie);
191                         break;
192
193                 default:
194                         assert_not_reached ();
195                 }
196         }
197 }