Use common open browser method for show website menu buttons
[beifahrer] / src / lift-list-window.vala
1 /* This file is part of Beifahrer.
2  *
3  * Copyright (C) 2010 Philipp Zabel
4  *
5  * Beifahrer 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  * Beifahrer 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 Beifahrer. If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 using Gtk;
20 using Hildon;
21
22 public class LiftListWindow : StackableWindow {
23         public const string GCONF_KEY_PRELOAD = "/apps/beifahrer/preload";
24
25         AdacMitfahrclub adac;
26         ListStore store;
27         Alignment alignment;
28         TreeView tree;
29         TreeViewColumn route_column;
30         Label no_lifts;
31         GConf.Client gconf;
32         bool preload = false;
33         string url;
34
35         Gtk.Button goto_website;
36
37         public LiftListWindow (AdacMitfahrclub _adac) {
38                 adac = _adac;
39         }
40
41         construct {
42                 set_title ("Beifahrer");
43
44                 var menu = new AppMenu ();
45                 goto_website = new Gtk.Button.with_label (_("Show website"));
46                 menu.append (goto_website);
47                 set_main_menu (menu);
48
49                 gconf = GConf.Client.get_default ();
50                 store = new ListStore (6, typeof (string), typeof (string), typeof (string), typeof (string), typeof (string), typeof (Lift));
51
52                 alignment = new Alignment (0.0f, 0.0f, 1.0f, 1.0f);
53                 alignment.top_padding = MARGIN_HALF;
54                 alignment.left_padding = MARGIN_DOUBLE;
55                 alignment.right_padding = MARGIN_DOUBLE;
56
57                 tree = (TreeView) Hildon.gtk_tree_view_new_with_model (UIMode.NORMAL, store);
58                 Hildon.gtk_widget_set_theme_size (tree, SizeType.FINGER_HEIGHT);
59
60                 tree.set_headers_visible (false);
61                 tree.set_rules_hint (true);
62
63                 // Tree selection object
64                 var selection = tree.get_selection ();
65                 selection.set_mode (SelectionMode.SINGLE);
66
67                 // Source and destination column
68                 route_column = new TreeViewColumn.with_attributes ("Route", new CellRendererText (), "markup", 0);
69                 route_column.set_reorderable (false);
70                 route_column.set_sizing (TreeViewColumnSizing.AUTOSIZE);
71                 route_column.set_expand (true);
72                 tree.append_column (route_column);
73
74                 // Date and time column
75                 var datetime_column = new TreeViewColumn.with_attributes ("DateTime", new CellRendererText (), "text", 1);
76                 datetime_column.set_reorderable (false);
77                 datetime_column.set_sizing (TreeViewColumnSizing.AUTOSIZE);
78                 datetime_column.set_expand (true);
79                 tree.append_column (datetime_column);
80
81                 // Free places column
82                 var places_column = new TreeViewColumn.with_attributes ("Places", new CellRendererText (), "text", 2);
83                 places_column.set_reorderable (false);
84                 places_column.set_sizing (TreeViewColumnSizing.AUTOSIZE);
85                 places_column.set_expand (true);
86                 tree.append_column (places_column);
87
88                 // Price column
89                 var price_column = new TreeViewColumn.with_attributes ("Price", new CellRendererText (), "text", 3);
90                 price_column.set_reorderable (false);
91                 price_column.set_sizing (TreeViewColumnSizing.AUTOSIZE);
92                 price_column.set_expand (true);
93                 tree.append_column (price_column);
94
95                 // Smoker/Non-smoker column
96                 var smoke_column = new TreeViewColumn.with_attributes ("Smoker", new CellRendererPixbuf (), "icon-name", 4);
97                 smoke_column.set_reorderable (false);
98                 smoke_column.set_sizing (TreeViewColumnSizing.AUTOSIZE);
99                 smoke_column.set_expand (false);
100                 tree.append_column (smoke_column);
101
102                 var pannable = new PannableArea ();
103                 pannable.add (tree);
104
105                 no_lifts = new Label (_("No lifts"));
106                 Hildon.helper_set_logical_font (no_lifts, "LargeSystemFont");
107                 Hildon.helper_set_logical_color (no_lifts, RcFlags.FG, StateType.NORMAL, "SecondaryTextColor");
108                 no_lifts.set_size_request (-1, 6 * 70);
109                 no_lifts.set_alignment ((float) 0.5, (float) 0.42);
110
111                 var vbox = new VBox (false, 0);
112                 vbox.pack_start (pannable, true, true, 0);
113                 vbox.pack_start (no_lifts, false, false, 0);
114
115                 alignment.add (vbox);
116                 add (alignment);
117
118                 alignment.show_all ();
119                 no_lifts.hide ();
120                 route_column.set_visible (!BeifahrerProgram.orientation.portrait);
121
122                 tree.row_activated.connect (this.on_row_activated);
123                 Gdk.Screen.get_default ().size_changed.connect (this.on_orientation_changed);
124                 goto_website.clicked.connect (on_goto_website_clicked);
125         }
126
127         public async void find_lifts (string city_from, int radius_from, string city_to, int radius_to, Date date, int tolerance = 0) {
128                 TreeIter iter;
129                 set_title ("%s - %s".printf (city_from, city_to));
130                 Hildon.gtk_window_set_progress_indicator (this, 1);
131
132                 url = adac.get_lift_list_url (city_from, radius_from, city_to, radius_to, date, tolerance);
133                 if (url == null)
134                         return;
135                 goto_website.show ();
136                 var lift_list = yield adac.get_lift_list (city_from, radius_from, city_to, radius_to, date, tolerance);
137                 foreach (Lift lift in lift_list) {
138                         string icon_name = null;
139                         if (LiftFlags.SMOKER in lift.flags)
140                                 icon_name = "beifahrer_smoker";
141                         else if (LiftFlags.NON_SMOKER in lift.flags)
142                                 icon_name = "beifahrer_non_smoker";
143                         string datetime = "%02d.%02d.%04d".printf (lift.time.day, lift.time.month, lift.time.year);
144                         if (lift.time.hour >= 0)
145                                 datetime += ", %d:%02d".printf (lift.time.hour, lift.time.minute);
146                         store.insert_with_values (out iter, -1, 0, lift.city_from + " - " + lift.city_to,
147                                                                 1, datetime,
148                                                                 2, _("%d pl.").printf (lift.places),
149                                                                 3, lift.price,
150                                                                 4, icon_name,
151                                                                 5, lift);
152                         // (LiftFlags.WOMEN_ONLY,ADAC_MEMBER in lift.flags)
153                 }
154                 if (lift_list.length () > 6)
155                         alignment.right_padding = MARGIN_DEFAULT;
156                 if (lift_list.length () == 0)
157                         no_lifts.show ();
158
159                 bool preload = false;
160                 try {
161                         preload = gconf.get_bool (GCONF_KEY_PRELOAD);
162                 } catch (Error e) {
163                 }
164                 if (preload) {
165                         if (store.get_iter_first (out iter)) do {
166                                 Lift lift2;
167                                 store.@get (iter, 5, out lift2);
168                                 if (lift2 == null)
169                                         continue;
170                                 if (lift2.description == null) {
171                                         yield adac.update_lift_details (lift2);
172                                         store.@set (iter, 0, lift2.city_from + " - " + lift2.city_to + "\n<small>" + lift2.name + "</small>");
173                                 }
174                         } while (store.iter_next (ref iter));
175                 }
176
177                 Hildon.gtk_window_set_progress_indicator (this, 0);
178         }
179
180         void on_goto_website_clicked () {
181                 BeifahrerProgram.open_browser (this, url);
182         }
183
184         private void on_row_activated (TreeView tree, TreePath /*_*/path, TreeViewColumn column) {
185                 TreeModel model = tree.model;
186                 TreeIter iter;
187                 Lift lift;
188
189                 if (model.get_iter (out iter, path)) {
190                         model.get (iter, 5, out lift);
191
192                         var window = new LiftDetailWindow (adac, lift);
193                         window.show ();
194                 }
195         }
196
197         private void on_orientation_changed (Gdk.Screen screen) {
198                 route_column.set_visible (!BeifahrerProgram.orientation.portrait);
199         }
200 }