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