Add genre filter dialog
[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
34         construct {
35                 // View menu
36                 menu = new MovieListMenu (this);
37
38                 set_main_menu (menu);
39
40                 // Search bar
41                 search_field = new Hildon.Entry (SizeType.FINGER_HEIGHT);
42
43                 var search_field_item = new ToolItem ();
44                 search_field_item.set_expand (true);
45                 search_field_item.add (search_field);
46
47                 var close_image = new Image.from_file("/usr/share/icons/hicolor/48x48/hildon/general_close.png");
48                 var close_button = new ToolButton (close_image, _("Close"));
49
50                 search_bar = new Toolbar ();
51                 search_bar.insert (search_field_item, 0);
52                 search_bar.insert (close_button, 1);
53
54                 add_toolbar (search_bar);
55
56                 // Movie list - connected to menu for sorting
57                 movie_list = new MovieListView ();
58                 menu.sortable = movie_list.sorted_store;
59                 store = movie_list.store;
60
61                 no_movies = new Label (_("No movies"));
62                 Hildon.helper_set_logical_font (no_movies, "LargeSystemFont");
63                 Hildon.helper_set_logical_color (no_movies, RcFlags.FG, StateType.NORMAL, "SecondaryTextColor");
64                 no_movies.set_size_request (-1, 6 * 70);
65                 no_movies.set_alignment ((float) 0.5, (float) 0.42);
66
67                 var vbox = new VBox (false, 0);
68                 vbox.pack_start (movie_list, true, true, 0);
69                 vbox.pack_start (no_movies, false, false, 0);
70
71                 add (vbox);
72
73                 edit_toolbar = new Hildon.EditToolbar.with_text (_("Select movies"), _("Delete"));
74                 set_edit_toolbar (edit_toolbar);
75
76                 // Connect signals
77                 menu.filter_changed.connect (() => { start_search (); });
78                 edit_toolbar.button_clicked.connect (on_delete_button_clicked);
79                 edit_toolbar.arrow_clicked.connect (leave_edit_mode); 
80                 search_field.changed.connect (on_search_field_changed);
81                 close_button.clicked.connect (on_close_button_clicked);
82                 key_press_event.connect (on_key_press_event);
83                 movie_list.movie_activated.connect (on_movie_activated);
84
85                 store.notify["update-running"].connect (on_update_running_changed);
86
87                 search_field.set_flags (WidgetFlags.CAN_DEFAULT);
88                 search_field.grab_default ();
89
90                 show_all ();
91                 edit_toolbar.hide ();
92                 search_bar_visible = false;
93                 search_bar.hide ();
94                 movie_list.hide ();
95
96                 filter = new MovieFilter ();
97                 menu.filter = filter;
98         }
99
100         public MovieSource source {
101                 get { return store.source; }
102                 set {
103                         store.source = value;
104                         menu.source = value;
105                         set_title (value.get_description ());
106                         filter.title = search_field.get_text ();
107                         if (store.start_search (filter)) {
108                                 movie_list.show ();
109                                 no_movies.hide ();
110                         }
111                 }
112         }
113
114         public void on_delete_movies_clicked () {
115                 fullscreen ();
116                 edit_toolbar.show ();
117                 movie_list.set_hildon_ui_mode (UIMode.EDIT);
118
119                 var selection = movie_list.get_selection ();
120                 selection.unselect_all ();
121         }
122
123         private void on_delete_button_clicked () {
124                 var selection = movie_list.get_selection ();
125                 int count = selection.count_selected_rows ();
126                 if (count == 0) {
127                         Banner.show_information (this, null, _("No movies selected"));
128                         leave_edit_mode ();
129                         return;
130                 }
131
132                 var dialog = new Note.confirmation (this, _("Delete %d movies?").printf (count));
133                 var res = dialog.run ();
134
135                 if (res == Gtk.ResponseType.OK) {
136                         weak TreeModel model;
137                         var rows = selection.get_selected_rows (out model);
138
139                         var movies = new List<Movie> ();
140
141                         // get selected movies from the store
142                         foreach (TreePath path in rows) {
143                                 TreeIter iter;
144
145                                 if (model.get_iter (out iter, path)) {
146                                         Movie movie;
147
148                                         model.get (iter, MovieListStore.Columns.MOVIE, out movie);
149                                         if (movie != null) {
150                                                 movies.append (movie);
151                                         }
152                                 }
153                         }
154                         // and remove them
155                         foreach (Movie movie in movies) {
156                                 store.remove (movie);
157                         }
158
159                         // Switch to "No movies" label if the store is emptied
160                         TreeIter iter;
161                         if (!store.get_iter_first (out iter)) {
162                                 movie_list.hide ();
163                                 no_movies.show ();
164                         }
165                 }
166                 dialog.destroy ();
167                 leave_edit_mode ();
168         }
169
170         private void leave_edit_mode () {
171                 movie_list.set_hildon_ui_mode (UIMode.NORMAL);
172                 edit_toolbar.hide ();
173                 unfullscreen ();
174         }
175
176         private void on_close_button_clicked () {
177                 search_field.set_text ("");
178                 search_bar_visible = false;
179                 search_bar.hide ();
180         }
181
182         private bool on_key_press_event (Widget widget, Gdk.EventKey event) {
183                 if (event.str != "") {
184                         if (!search_bar_visible) {
185                                 search_bar_visible = true;
186                                 search_bar.show ();
187                         }
188                         search_field.grab_focus ();
189                 }
190
191                 return false;
192         }
193
194         private void on_search_field_changed () {
195                 // With every change we reset the timer to 500ms
196                 if (source_id != 0) {
197                         Source.remove (source_id);
198                 }
199                 source_id = Timeout.add (500, start_search);
200         }
201
202         private bool start_search () {
203                 filter.title = search_field.get_text ();
204                 if (store.start_search (filter)) {
205                         movie_list.show ();
206                         no_movies.hide ();
207                 }
208
209                 // One-shot only
210                 return false;
211         }
212
213         private void on_movie_activated (Movie movie) {
214                 var window = new MovieWindow.with_movie (movie, store);
215                 window.show_all ();
216
217         }
218
219         private void on_update_running_changed (GLib.Object source, ParamSpec spec) {
220                 TreeIter iter;
221
222                 Hildon.gtk_window_set_progress_indicator (this, (int) store.update_running);
223                 // Update finished, but store still empty?
224                 if (!store.update_running && !store.get_iter_first (out iter)) {
225                         movie_list.hide ();
226                         no_movies.show ();
227                 }
228         }
229 }
230