IMDb downloader: optimize SQLlite database for the common case
[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         private MoviePoster.Factory poster_factory;
42         public MovieSource source;
43         private MovieFilter filter;
44         public bool update_running { get; set; }
45         private Cancellable cancellable;
46
47         construct {
48                 set_column_types (base_type);
49                 no_poster = null;
50                 source = null;
51                 update_running = false;
52
53                 poster_factory = MoviePoster.Factory.get_instance ();
54         }
55
56         public void add (Movie movie, out TreeIter iter) {
57                 TreeIter iter1;
58
59                 append (out iter1);
60                 base.set (iter1, 0, movie);
61
62                 movie.notify.connect (this.on_movie_changed);
63
64                 iter = iter1;
65         }
66
67         public new bool remove (Movie movie) {
68                 TreeIter iter;
69
70                 if (get_iter_from_movie (out iter, movie)) {
71                         movie.notify.disconnect (this.on_movie_changed);
72                         base.remove (iter);
73
74                         if (source.get_editable ()) {
75                                 source.delete_movie (movie);
76                         }
77
78                         return true;
79                 }
80
81                 return false;
82         }
83
84         private void on_movie_changed (GLib.Object source, GLib.ParamSpec spec) {
85                 var movie = (Movie) source;
86
87                 TreeIter iter;
88                 if (get_iter_from_movie (out iter, movie)) {
89                         TreePath path = get_path (iter);
90                         base.row_changed (path, iter);
91                 }
92         }
93
94         public bool get_editable () {
95                 return source.get_editable ();
96         }
97
98         public bool get_iter_from_movie (out TreeIter iter, Movie movie_a) {
99                 if (get_iter_first (out iter)) {
100                         do {
101                                 Movie movie_b;
102                                 get (iter, Columns.MOVIE, out movie_b);
103                                 if (movie_a == movie_b)
104                                         return true;
105                         } while (iter_next (ref iter));
106                 }
107                 return false;
108         }
109
110         public bool start_search (MovieFilter _filter) {
111                 if (update_running) {
112                         stdout.printf ("aborting search ...\n");
113                         cancellable.cancel ();
114                         poster_factory.clear_queue ();
115                         return false;
116                 }
117                 if (cancellable == null || cancellable.is_cancelled ())
118                         cancellable = new Cancellable ();
119
120                 filter = _filter;
121                 stdout.printf ("begin search\n");
122                 search_async.begin ();
123                 update_running = true;
124                 return true;
125         }
126
127         // Asynchronous update method
128         private async void search_async () {
129                 stdout.printf ("search started: \"%s\"\n", filter.title);
130
131                 clear ();
132
133                 if (source != null)
134                         // FIXME - arbitrary limit
135                         yield source.get_movies (filter, receive_movie, 100, cancellable);
136
137                 update_running = false;
138                 if (cancellable.is_cancelled ()) {
139                         stdout.printf ("search aborted, starting new\n");
140                         cancellable.reset ();
141                         if (cancellable.is_cancelled ()) {
142                                 stdout.printf ("OW WEY\n");
143                         }
144                         start_search (filter);
145                 } else {
146                         stdout.printf ("search stopped\n");
147                 }
148         }
149
150         private void receive_movie (Movie movie) {
151                 TreeIter iter;
152
153                 if (cancellable.is_cancelled ())
154                         return;
155
156                 add (movie, out iter);
157                 try {
158                         poster_factory.queue_thumbnail (movie, 64, 64, false, receive_poster_thumbnail);
159                 } catch (Error e) {
160                         warning ("Failed to queue poster request: %s\n", e.message);
161                 }
162         }
163
164         private void receive_poster_thumbnail (Gdk.Pixbuf pixbuf, Movie movie) {
165                 var poster = new Poster ();
166                 poster.thumbnail = pixbuf;
167                 movie.poster = poster;
168         }
169
170         // Implement TreeModel interface
171         public virtual GLib.Type get_column_type (int index_) {
172                 return_val_if_fail (index_ >= 0 && index_ < Columns.N_COLUMNS, 0);
173
174                 return types[index_];
175         }
176
177         public virtual int get_n_columns () {
178                 return Columns.N_COLUMNS;
179         }
180
181         public virtual void get_value (TreeIter iter, int column, out GLib.Value value) {
182                 Movie movie;
183
184                 // FIXME
185                 if (no_poster == null) try {
186                         no_poster = new Gdk.Pixbuf.from_file ("/usr/share/icons/hicolor/64x64/hildon/general_video.png");
187                 } catch (Error e) {
188                         critical ("Missing general_video icon: %s\n", e.message);
189                 }
190
191                 return_if_fail (column >= 0 && column < Columns.N_COLUMNS);
192
193                 // Get the Movie from our parent's storage
194                 Value val;
195                 base.get_value (iter, 0, out val);
196                 movie = (Movie) val.get_object ();
197
198                 value.init (get_column_type (column));
199
200                 switch (column) {
201                 case Columns.TITLE:
202                         if (movie != null) {
203                                 value.set_string (movie.title);
204                         } else {
205                                 value.set_string ("");
206                         }
207                         break;
208
209                 case Columns.YEAR:
210                         if (movie != null) {
211                                 value.set_int (movie.year);
212                         } else {
213                                 value.set_int (-1);
214                         }
215                         break;
216
217                 case Columns.RATING:
218                         if (movie != null) {
219                                 value.set_int (movie.rating);
220                         } else {
221                                 value.set_int (-1);
222                         }
223                         break;
224
225                 case Columns.POSTER:
226                         if ((movie.poster != null) && (movie.poster.thumbnail != null))
227                                 value.set_object (movie.poster.thumbnail);
228                         else
229                                 value.set_object (no_poster);
230                         break;
231
232                 case Columns.MOVIE:
233                         value.set_object (movie);
234                         break;
235
236                 default:
237                         assert_not_reached ();
238                 }
239         }
240 }