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