Movie list menu / window: add settable source property to the menu
[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.Entry search_field;
25         private Toolbar search_bar;
26         private uint source_id;
27         private MovieListView movie_list;
28         private MovieFilter filter;
29         private MovieListStore store;
30         private Label no_movies;
31         private bool search_bar_visible;
32
33         construct {
34                 // View menu
35                 menu = new MovieListMenu (this);
36
37                 set_main_menu (menu);
38
39                 // Search bar
40                 search_field = new Hildon.Entry (SizeType.FINGER_HEIGHT);
41
42                 var search_field_item = new ToolItem ();
43                 search_field_item.set_expand (true);
44                 search_field_item.add (search_field);
45
46                 var close_image = new Image.from_file("/usr/share/icons/hicolor/48x48/hildon/general_close.png");
47                 var close_button = new ToolButton (close_image, _("Close"));
48
49                 search_bar = new Toolbar ();
50                 search_bar.insert (search_field_item, 0);
51                 search_bar.insert (close_button, 1);
52
53                 add_toolbar (search_bar);
54
55                 // Movie list - connected to menu for sorting
56                 movie_list = new MovieListView ();
57                 menu.sortable = movie_list.sorted_store;
58                 store = movie_list.store;
59
60                 no_movies = new Label (_("No movies"));
61                 Hildon.helper_set_logical_font (no_movies, "LargeSystemFont");
62                 Hildon.helper_set_logical_color (no_movies, RcFlags.FG, StateType.NORMAL, "SecondaryTextColor");
63                 no_movies.set_size_request (-1, 6 * 70);
64                 no_movies.set_alignment ((float) 0.5, (float) 0.42);
65
66                 var vbox = new VBox (false, 0);
67                 vbox.pack_start (movie_list, true, true, 0);
68                 vbox.pack_start (no_movies, false, false, 0);
69
70                 add (vbox);
71
72                 // Connect signals
73                 search_field.changed.connect (on_search_field_changed);
74                 close_button.clicked.connect (on_close_button_clicked);
75                 key_press_event.connect (on_key_press_event);
76
77                 store.notify["update-running"].connect (on_update_running_changed);
78
79                 search_field.set_flags (WidgetFlags.CAN_DEFAULT);
80                 search_field.grab_default ();
81
82                 show_all ();
83                 search_bar_visible = false;
84                 search_bar.hide ();
85                 movie_list.hide ();
86
87                 filter = new MovieFilter ();
88         }
89
90         public MovieSource source {
91                 get { return store.source; }
92                 set {
93                         store.source = value;
94                         menu.source = value;
95                         set_title ("Cinæst: " + value.get_description ());
96                         filter.title = search_field.get_text ();
97                         if (store.start_search (filter)) {
98                                 movie_list.show ();
99                                 no_movies.hide ();
100                         }
101                 }
102         }
103
104         private void on_close_button_clicked () {
105                 search_field.set_text ("");
106                 search_bar_visible = false;
107                 search_bar.hide ();
108         }
109
110         private bool on_key_press_event (Widget widget, Gdk.EventKey event) {
111                 if (event.str != "") {
112                         if (!search_bar_visible) {
113                                 search_bar_visible = true;
114                                 search_bar.show ();
115                         }
116                         search_field.grab_focus ();
117                 }
118
119                 return false;
120         }
121
122         private void on_search_field_changed () {
123                 if (search_field.get_text () == "") {
124                         store.clear ();
125                         movie_list.hide ();
126                         no_movies.show ();
127                         return;
128                 }
129
130                 // With every change we reset the timer to 500ms
131                 if (source_id != 0) {
132                         Source.remove (source_id);
133                 }
134                 source_id = Timeout.add (500, start_search);
135         }
136
137         private bool start_search () {
138                 filter.title = search_field.get_text ();
139                 if (store.start_search (filter)) {
140                         movie_list.show ();
141                         no_movies.hide ();
142                 }
143
144                 // One-shot only
145                 return false;
146         }
147
148         private void on_update_running_changed (GLib.Object source, ParamSpec spec) {
149                 TreeIter iter;
150
151                 Hildon.gtk_window_set_progress_indicator (this, (int) store.update_running);
152                 // Update finished, but store still empty?
153                 if (!store.update_running && !store.get_iter_first (out iter)) {
154                         movie_list.hide ();
155                         no_movies.show ();
156                 }
157         }
158 }
159