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