Movie list window: do not ask to "Delete 0 movies?" if no movie is selected
[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                 edit_toolbar.button_clicked.connect (on_delete_button_clicked);
78                 edit_toolbar.arrow_clicked.connect (leave_edit_mode); 
79                 search_field.changed.connect (on_search_field_changed);
80                 close_button.clicked.connect (on_close_button_clicked);
81                 key_press_event.connect (on_key_press_event);
82                 movie_list.movie_activated.connect (on_movie_activated);
83
84                 store.notify["update-running"].connect (on_update_running_changed);
85
86                 search_field.set_flags (WidgetFlags.CAN_DEFAULT);
87                 search_field.grab_default ();
88
89                 show_all ();
90                 edit_toolbar.hide ();
91                 search_bar_visible = false;
92                 search_bar.hide ();
93                 movie_list.hide ();
94
95                 filter = new MovieFilter ();
96         }
97
98         public MovieSource source {
99                 get { return store.source; }
100                 set {
101                         store.source = value;
102                         menu.source = value;
103                         set_title (value.get_description ());
104                         filter.title = search_field.get_text ();
105                         if (store.start_search (filter)) {
106                                 movie_list.show ();
107                                 no_movies.hide ();
108                         }
109                 }
110         }
111
112         public void on_delete_movies_clicked () {
113                 fullscreen ();
114                 edit_toolbar.show ();
115                 movie_list.set_hildon_ui_mode (UIMode.EDIT);
116
117                 var selection = movie_list.get_selection ();
118                 selection.unselect_all ();
119         }
120
121         private void on_delete_button_clicked () {
122                 var selection = movie_list.get_selection ();
123                 int count = selection.count_selected_rows ();
124                 if (count == 0) {
125                         Banner.show_information (this, null, _("No movies selected"));
126                         leave_edit_mode ();
127                         return;
128                 }
129
130                 var dialog = new Note.confirmation (this, _("Delete %d movies?").printf (count));
131                 var res = dialog.run ();
132
133                 if (res == Gtk.ResponseType.OK) {
134                         weak TreeModel model;
135                         var rows = selection.get_selected_rows (out model);
136
137                         var movies = new List<Movie> ();
138
139                         // get selected movies from the store
140                         foreach (TreePath path in rows) {
141                                 TreeIter iter;
142
143                                 if (model.get_iter (out iter, path)) {
144                                         Movie movie;
145
146                                         model.get (iter, MovieListStore.Columns.MOVIE, out movie);
147                                         if (movie != null) {
148                                                 movies.append (movie);
149                                         }
150                                 }
151                         }
152                         // and remove them
153                         foreach (Movie movie in movies) {
154                                 store.remove (movie);
155                         }
156                 }
157                 dialog.destroy ();
158                 leave_edit_mode ();
159         }
160
161         private void leave_edit_mode () {
162                 movie_list.set_hildon_ui_mode (UIMode.NORMAL);
163                 edit_toolbar.hide ();
164                 unfullscreen ();
165         }
166
167         private void on_close_button_clicked () {
168                 search_field.set_text ("");
169                 search_bar_visible = false;
170                 search_bar.hide ();
171         }
172
173         private bool on_key_press_event (Widget widget, Gdk.EventKey event) {
174                 if (event.str != "") {
175                         if (!search_bar_visible) {
176                                 search_bar_visible = true;
177                                 search_bar.show ();
178                         }
179                         search_field.grab_focus ();
180                 }
181
182                 return false;
183         }
184
185         private void on_search_field_changed () {
186                 // With every change we reset the timer to 500ms
187                 if (source_id != 0) {
188                         Source.remove (source_id);
189                 }
190                 source_id = Timeout.add (500, start_search);
191         }
192
193         private bool start_search () {
194                 filter.title = search_field.get_text ();
195                 if (store.start_search (filter)) {
196                         movie_list.show ();
197                         no_movies.hide ();
198                 }
199
200                 // One-shot only
201                 return false;
202         }
203
204         private void on_movie_activated (Movie movie) {
205                 var window = new MovieWindow.with_movie (movie, store);
206                 window.show_all ();
207
208         }
209
210         private void on_update_running_changed (GLib.Object source, ParamSpec spec) {
211                 TreeIter iter;
212
213                 Hildon.gtk_window_set_progress_indicator (this, (int) store.update_running);
214                 // Update finished, but store still empty?
215                 if (!store.update_running && !store.get_iter_first (out iter)) {
216                         movie_list.hide ();
217                         no_movies.show ();
218                 }
219         }
220 }
221