1 /* This file is part of Cinaest.
3 * Copyright (C) 2009 Philipp Zabel
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.
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.
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/>.
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;
37 public MovieListWindow (MovieSource source) {
38 set_title (source.get_description ());
41 menu = new MovieListMenu (this);
47 search_field = new Hildon.Entry (SizeType.FINGER_HEIGHT);
49 var search_field_item = new ToolItem ();
50 search_field_item.set_expand (true);
51 search_field_item.add (search_field);
53 var icon_theme = Gtk.IconTheme.get_default ();
54 var pixbuf = icon_theme.load_icon ("general_close",
56 Gtk.IconLookupFlags.NO_SVG);
57 var close_image = new Image.from_pixbuf (pixbuf);
58 var close_button = new ToolButton (close_image, _("Close"));
60 search_bar = new Toolbar ();
61 search_bar.insert (search_field_item, 0);
62 search_bar.insert (close_button, 1);
63 search_bar.show_all ();
65 add_toolbar (search_bar);
67 // Movie list - connected to menu for sorting
68 movie_list = new MovieListView (this, source.get_name () == _("Watched movies"));
69 menu.sortable = movie_list.sorted_store;
70 store = movie_list.store;
71 store.source = source;
73 no_movies = new Label (_("No movies"));
74 Hildon.helper_set_logical_font (no_movies, "LargeSystemFont");
75 Hildon.helper_set_logical_color (no_movies, RcFlags.FG, StateType.NORMAL, "SecondaryTextColor");
76 no_movies.set_size_request (-1, 6 * 70);
77 no_movies.set_alignment ((float) 0.5, (float) 0.42);
79 var vbox = new VBox (false, 0);
80 vbox.pack_start (movie_list, true, true, 0);
81 vbox.pack_start (no_movies, false, false, 0);
83 alignment = new Alignment (0.0f, 0.0f, 1.0f, 1.0f);
84 alignment.top_padding = MARGIN_HALF;
85 alignment.left_padding = MARGIN_DOUBLE;
86 alignment.right_padding = MARGIN_DOUBLE;
91 edit_toolbar = new Hildon.EditToolbar.with_text (_("Select movies"), _("Delete"));
92 set_edit_toolbar (edit_toolbar);
95 menu.filter_changed.connect (() => { start_search (); });
96 edit_toolbar.button_clicked.connect (on_delete_button_clicked);
97 edit_toolbar.arrow_clicked.connect (leave_edit_mode);
98 search_field.changed.connect (on_search_field_changed);
99 close_button.clicked.connect (on_close_button_clicked);
100 key_press_event.connect (on_key_press_event);
101 movie_list.movie_activated.connect (on_movie_activated);
102 store.row_deleted.connect (on_row_deleted);
103 store.row_inserted.connect (on_row_inserted);
104 store.search_finished.connect (on_search_finished);
106 store.notify["update-running"].connect (on_update_running_changed);
108 search_field.set_flags (WidgetFlags.CAN_DEFAULT);
109 search_field.grab_default ();
111 alignment.show_all ();
112 edit_toolbar.hide ();
113 search_bar_visible = false;
116 filter = new MovieFilter ();
117 menu.filter = filter;
119 if (SourceFlags.ONLINE in source.get_flags ()) {
121 search_bar_visible = true;
123 search_field.grab_focus ();
126 if (store.start_search (filter)) {
133 public void on_delete_movies_clicked () {
135 edit_toolbar.show ();
136 movie_list.set_hildon_ui_mode (UIMode.EDIT);
138 var selection = movie_list.get_selection ();
139 selection.unselect_all ();
142 private void on_delete_button_clicked () {
143 var selection = movie_list.get_selection ();
144 int count = selection.count_selected_rows ();
146 Banner.show_information (this, null, _("No movies selected"));
151 var dialog = new Note.confirmation (this, _("Delete %d movies?").printf (count));
152 var res = dialog.run ();
154 if (res == Gtk.ResponseType.OK) {
155 weak TreeModel model;
156 var rows = selection.get_selected_rows (out model);
158 var movies = new List<Movie> ();
160 // get selected movies from the store
161 foreach (TreePath path in rows) {
164 if (model.get_iter (out iter, path)) {
167 model.get (iter, MovieListStore.Columns.MOVIE, out movie);
169 movies.append (movie);
174 foreach (Movie movie in movies) {
175 store.remove (movie);
178 // Switch to "No movies" label if the store is emptied
180 if (!store.get_iter_first (out iter)) {
189 private void leave_edit_mode () {
190 movie_list.set_hildon_ui_mode (UIMode.NORMAL);
191 edit_toolbar.hide ();
195 private void on_close_button_clicked () {
196 search_field.set_text ("");
197 search_bar_visible = false;
201 private bool on_key_press_event (Widget widget, Gdk.EventKey event) {
202 if (event.str != "") {
203 if (!search_bar_visible) {
204 search_bar_visible = true;
207 search_field.grab_focus ();
213 private void on_search_field_changed () {
214 // With every change we reset the timer to 500ms
215 if (source_id != 0) {
216 Source.remove (source_id);
218 source_id = Timeout.add (500, start_search);
221 private bool start_search () {
222 filter.title = search_field.get_text ();
223 if (store.start_search (filter)) {
232 private void on_search_finished (int movies) {
234 alignment.right_padding = MARGIN_DEFAULT;
236 alignment.right_padding = MARGIN_DOUBLE;
239 private void on_movie_activated (Movie movie) {
240 if (movie_window != null)
243 movie_window = new MovieWindow.with_movie (movie, store);
244 movie_window.destroy.connect (() => { movie_window = null; });
245 movie_window.show ();
248 private void on_row_deleted (TreePath path) {
255 private void on_row_inserted (TreePath path, TreeIter iter) {
262 private void on_update_running_changed (GLib.Object source, ParamSpec spec) {
265 Hildon.gtk_window_set_progress_indicator (this, (int) store.update_running);
266 // Update finished, but store still empty?
267 if (!store.update_running && !store.get_iter_first (out iter)) {