Movie list window: add title markup for filtered views
[cinaest] / src / movie-list-window.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 using Hildon;
21
22 public class MovieListWindow : StackableWindow {
23         private MovieListMenu menu;
24         private Hildon.EditToolbar edit_toolbar;
25         private Hildon.Entry search_field;
26         private Toolbar search_bar;
27         private uint source_id;
28         private MovieListView movie_list;
29         private MovieFilter filter;
30         private MovieListStore store;
31         private Label no_movies;
32         private bool search_bar_visible;
33         private MovieWindow movie_window;
34         private Alignment alignment;
35         private int count;
36         private MovieSource source;
37
38         public MovieListWindow (MovieSource source_) {
39                 source = source_;
40                 set_title (source.get_description ());
41
42                 // View menu
43                 menu = new MovieListMenu (this);
44                 menu.source = source;
45
46                 set_main_menu (menu);
47
48                 // Search bar
49                 search_field = new Hildon.Entry (SizeType.FINGER_HEIGHT);
50
51                 var search_field_item = new ToolItem ();
52                 search_field_item.set_expand (true);
53                 search_field_item.add (search_field);
54
55                 var icon_theme = Gtk.IconTheme.get_default ();
56                 var pixbuf = icon_theme.load_icon ("general_close",
57                                                    48,
58                                                    Gtk.IconLookupFlags.NO_SVG);
59                 var close_image = new Image.from_pixbuf (pixbuf);
60                 var close_button = new ToolButton (close_image, _("Close"));
61
62                 search_bar = new Toolbar ();
63                 search_bar.insert (search_field_item, 0);
64                 search_bar.insert (close_button, 1);
65                 search_bar.show_all ();
66
67                 add_toolbar (search_bar);
68
69                 // Movie list - connected to menu for sorting
70                 movie_list = new MovieListView (this, source.get_name () == _("Watched movies"));
71                 menu.sortable = movie_list.store;
72                 store = movie_list.store;
73                 store.source = source;
74
75                 no_movies = new Label (_("No movies"));
76                 Hildon.helper_set_logical_font (no_movies, "LargeSystemFont");
77                 Hildon.helper_set_logical_color (no_movies, RcFlags.FG, StateType.NORMAL, "SecondaryTextColor");
78                 no_movies.set_size_request (-1, 6 * 70);
79                 no_movies.set_alignment ((float) 0.5, (float) 0.42);
80
81                 var vbox = new VBox (false, 0);
82                 vbox.pack_start (movie_list, true, true, 0);
83                 vbox.pack_start (no_movies, false, false, 0);
84
85                 alignment = new Alignment (0.0f, 0.0f, 1.0f, 1.0f);
86                 alignment.top_padding = MARGIN_HALF;
87                 alignment.left_padding = MARGIN_DOUBLE;
88                 alignment.right_padding = MARGIN_DOUBLE;
89
90                 alignment.add (vbox);
91                 add (alignment);
92
93                 edit_toolbar = new Hildon.EditToolbar.with_text (_("Select movies"), _("Delete"));
94                 set_edit_toolbar (edit_toolbar);
95
96                 // Connect signals
97                 menu.filter_changed.connect (on_filter_changed);
98                 edit_toolbar.button_clicked.connect (on_delete_button_clicked);
99                 edit_toolbar.arrow_clicked.connect (leave_edit_mode); 
100                 search_field.changed.connect (on_search_field_changed);
101                 close_button.clicked.connect (on_close_button_clicked);
102                 key_press_event.connect (on_key_press_event);
103                 movie_list.movie_activated.connect (on_movie_activated);
104                 store.row_deleted.connect (on_row_deleted);
105                 store.row_inserted.connect (on_row_inserted);
106                 store.search_finished.connect (on_search_finished);
107
108                 store.notify["update-running"].connect (on_update_running_changed);
109
110                 search_field.set_flags (WidgetFlags.CAN_DEFAULT);
111                 search_field.grab_default ();
112
113                 alignment.show_all ();
114                 edit_toolbar.hide ();
115                 search_bar_visible = false;
116                 search_bar.hide ();
117
118                 filter = new MovieFilter ();
119                 menu.filter = filter;
120                 filter.title = "";
121                 if (SourceFlags.ONLINE in source.get_flags ()) {
122                         no_movies.hide ();
123                         search_bar_visible = true;
124                         search_bar.show ();
125                         search_field.grab_focus ();
126                         return;
127                 }
128                 if (store.start_search (filter)) {
129                         no_movies.hide ();
130                 } else {
131                         movie_list.hide ();
132                 }
133         }
134
135         private void on_filter_changed () {
136                 var markup = new StringBuilder ();
137                 if (filter.year_min != 0 || filter.year_max != 0)
138                         markup.append (_(" - years"));
139                 if (filter.rating_min != 0)
140                         markup.append (_(" - rating"));
141                 if (filter.genres.field != 0)
142                         markup.append (_(" - genres"));
143                 if (markup.str.length > 0) {
144                         Gdk.Color color;
145                         this.ensure_style ();
146                         if (this.style.lookup_color ("ActiveTextColor", out color)) {
147                                 markup.prepend ("<span fgcolor=\"%s\">".printf (color.to_string ()));
148                                 markup.append ("</span>");
149                         }
150                         markup.prepend (source.get_description ());
151                         set_markup (markup.str);
152                 } else {
153                         set_markup (source.get_description ());
154                 }
155
156                 start_search ();
157         }
158
159         public void on_delete_movies_clicked () {
160                 fullscreen ();
161                 edit_toolbar.show ();
162                 movie_list.set_hildon_ui_mode (UIMode.EDIT);
163
164                 var selection = movie_list.get_selection ();
165                 selection.unselect_all ();
166         }
167
168         private void on_delete_button_clicked () {
169                 var selection = movie_list.get_selection ();
170                 int count = selection.count_selected_rows ();
171                 if (count == 0) {
172                         Banner.show_information (this, null, _("No movies selected"));
173                         leave_edit_mode ();
174                         return;
175                 }
176
177                 var dialog = new Note.confirmation (this, _("Delete %d movies?").printf (count));
178                 var res = dialog.run ();
179
180                 if (res == Gtk.ResponseType.OK) {
181                         weak TreeModel model;
182                         var rows = selection.get_selected_rows (out model);
183
184                         var movies = new List<Movie> ();
185
186                         // get selected movies from the store
187                         foreach (TreePath path in rows) {
188                                 TreeIter iter;
189
190                                 if (model.get_iter (out iter, path)) {
191                                         Movie movie;
192
193                                         model.get (iter, MovieListStore.Columns.MOVIE, out movie);
194                                         if (movie != null) {
195                                                 movies.append (movie);
196                                         }
197                                 }
198                         }
199                         // and remove them
200                         foreach (Movie movie in movies) {
201                                 store.remove (movie);
202                         }
203
204                         // Switch to "No movies" label if the store is emptied
205                         TreeIter iter;
206                         if (!store.get_iter_first (out iter)) {
207                                 movie_list.hide ();
208                                 no_movies.show ();
209                         }
210                 }
211                 dialog.destroy ();
212                 leave_edit_mode ();
213         }
214
215         private void leave_edit_mode () {
216                 movie_list.set_hildon_ui_mode (UIMode.NORMAL);
217                 edit_toolbar.hide ();
218                 unfullscreen ();
219         }
220
221         private void on_close_button_clicked () {
222                 search_field.set_text ("");
223                 search_bar_visible = false;
224                 search_bar.hide ();
225         }
226
227         private bool on_key_press_event (Widget widget, Gdk.EventKey event) {
228                 if (event.str != "") {
229                         if (!search_bar_visible) {
230                                 search_bar_visible = true;
231                                 search_bar.show ();
232                         }
233                         search_field.grab_focus ();
234                 }
235
236                 return false;
237         }
238
239         private void on_search_field_changed () {
240                 // With every change we reset the timer to 500ms
241                 if (source_id != 0) {
242                         Source.remove (source_id);
243                 }
244                 source_id = Timeout.add (500, start_search);
245         }
246
247         private bool start_search () {
248                 filter.title = search_field.get_text ();
249                 if (store.start_search (filter)) {
250                         movie_list.show ();
251                         no_movies.hide ();
252                 }
253
254                 // One-shot only
255                 return false;
256         }
257
258         private void on_search_finished (int movies) {
259                 if (movies > 6)
260                         alignment.right_padding = MARGIN_DEFAULT;
261                 else
262                         alignment.right_padding = MARGIN_DOUBLE;
263         }
264
265         private void on_movie_activated (Movie movie) {
266                 if (movie_window != null)
267                         return;
268
269                 movie_window = new MovieWindow.with_movie (movie, store);
270                 movie_window.destroy.connect (() => { movie_window = null; });
271                 movie_window.show ();
272         }
273
274         private void on_row_deleted (TreePath path) {
275                 if (--count == 0) {
276                         no_movies.show ();
277                         movie_list.hide ();
278                 }
279         }
280
281         private void on_row_inserted (TreePath path, TreeIter iter) {
282                 if (count++ == 0) {
283                         no_movies.hide ();
284                         movie_list.show ();
285                 }
286         }
287
288         private void on_update_running_changed (GLib.Object source, ParamSpec spec) {
289                 TreeIter iter;
290
291                 Hildon.gtk_window_set_progress_indicator (this, (int) store.update_running);
292                 // Update finished, but store still empty?
293                 if (!store.update_running && !store.get_iter_first (out iter)) {
294                         movie_list.hide ();
295                         no_movies.show ();
296                 }
297         }
298 }
299