Add E-mail button
[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
34         public LiftListWindow (AdacMitfahrclub _adac) {
35                 adac = _adac;
36         }
37
38         construct {
39                 set_title ("Beifahrer");
40
41                 gconf = GConf.Client.get_default ();
42                 store = new ListStore (6, typeof (string), typeof (string), typeof (string), typeof (string), typeof (string), typeof (Lift));
43
44                 alignment = new Alignment (0.0f, 0.0f, 1.0f, 1.0f);
45                 alignment.top_padding = MARGIN_HALF;
46                 alignment.left_padding = MARGIN_DOUBLE;
47                 alignment.right_padding = MARGIN_DOUBLE;
48
49                 tree = (TreeView) Hildon.gtk_tree_view_new_with_model (UIMode.NORMAL, store);
50                 Hildon.gtk_widget_set_theme_size (tree, SizeType.FINGER_HEIGHT);
51
52                 tree.set_headers_visible (false);
53                 tree.set_rules_hint (true);
54
55                 // Tree selection object
56                 var selection = tree.get_selection ();
57                 selection.set_mode (SelectionMode.SINGLE);
58
59                 // Source and destination column
60                 route_column = new TreeViewColumn.with_attributes ("Route", new CellRendererText (), "text", 0);
61                 route_column.set_reorderable (false);
62                 route_column.set_sizing (TreeViewColumnSizing.AUTOSIZE);
63                 route_column.set_expand (true);
64                 tree.append_column (route_column);
65
66                 // Date and time column
67                 var datetime_column = new TreeViewColumn.with_attributes ("DateTime", new CellRendererText (), "text", 1);
68                 datetime_column.set_reorderable (false);
69                 datetime_column.set_sizing (TreeViewColumnSizing.AUTOSIZE);
70                 datetime_column.set_expand (true);
71                 tree.append_column (datetime_column);
72
73                 // Free places column
74                 var places_column = new TreeViewColumn.with_attributes ("Places", new CellRendererText (), "text", 2);
75                 places_column.set_reorderable (false);
76                 places_column.set_sizing (TreeViewColumnSizing.AUTOSIZE);
77                 places_column.set_expand (true);
78                 tree.append_column (places_column);
79
80                 // Price column
81                 var price_column = new TreeViewColumn.with_attributes ("Price", new CellRendererText (), "text", 3);
82                 price_column.set_reorderable (false);
83                 price_column.set_sizing (TreeViewColumnSizing.AUTOSIZE);
84                 price_column.set_expand (true);
85                 tree.append_column (price_column);
86
87                 // Smoker/Non-smoker column
88                 var smoke_column = new TreeViewColumn.with_attributes ("Smoker", new CellRendererPixbuf (), "icon-name", 4);
89                 smoke_column.set_reorderable (false);
90                 smoke_column.set_sizing (TreeViewColumnSizing.AUTOSIZE);
91                 smoke_column.set_expand (false);
92                 tree.append_column (smoke_column);
93
94                 var pannable = new PannableArea ();
95                 pannable.add (tree);
96
97                 no_lifts = new Label (_("No lifts"));
98                 Hildon.helper_set_logical_font (no_lifts, "LargeSystemFont");
99                 Hildon.helper_set_logical_color (no_lifts, RcFlags.FG, StateType.NORMAL, "SecondaryTextColor");
100                 no_lifts.set_size_request (-1, 6 * 70);
101                 no_lifts.set_alignment ((float) 0.5, (float) 0.42);
102
103                 var vbox = new VBox (false, 0);
104                 vbox.pack_start (pannable, true, true, 0);
105                 vbox.pack_start (no_lifts, false, false, 0);
106
107                 alignment.add (vbox);
108                 add (alignment);
109
110                 alignment.show_all ();
111                 no_lifts.hide ();
112                 route_column.set_visible (!BeifahrerProgram.orientation.portrait);
113
114                 tree.row_activated.connect (this.on_row_activated);
115                 BeifahrerProgram.orientation.changed.connect (this.on_orientation_changed);
116         }
117
118         public async void find_lifts (string city_from, int radius_from, string city_to, int radius_to, Date date, int tolerance = 0) {
119                 TreeIter iter;
120                 set_title ("%s - %s".printf (city_from, city_to));
121                 Hildon.gtk_window_set_progress_indicator (this, 1);
122
123                 var lift_list = yield adac.get_lift_list (city_from, radius_from, city_to, radius_to, date, tolerance);
124                 foreach (Lift lift in lift_list) {
125                         string icon_name = null;
126                         if (LiftFlags.SMOKER in lift.flags)
127                                 icon_name = "beifahrer_smoker";
128                         else if (LiftFlags.NON_SMOKER in lift.flags)
129                                 icon_name = "beifahrer_non_smoker";
130                         string datetime = "%02d.%02d.%04d".printf (lift.time.day, lift.time.month, lift.time.year);
131                         if (lift.time.hour >= 0)
132                                 datetime += ", %d:%02d".printf (lift.time.hour, lift.time.minute);
133                         store.insert_with_values (out iter, -1, 0, lift.city_from + " - " + lift.city_to,
134                                                                 1, datetime,
135                                                                 2, _("%d pl.").printf (lift.places),
136                                                                 3, lift.price,
137                                                                 4, icon_name,
138                                                                 5, lift);
139                         // (LiftFlags.WOMEN_ONLY,ADAC_MEMBER in lift.flags)
140                 }
141                 if (lift_list.length () > 6)
142                         alignment.right_padding = MARGIN_DEFAULT;
143                 if (lift_list.length () == 0)
144                         no_lifts.show ();
145
146                 bool preload = false;
147                 try {
148                         preload = gconf.get_bool (GCONF_KEY_PRELOAD);
149                 } catch (Error e) {
150                 }
151                 if (preload) {
152                         if (store.get_iter_first (out iter)) do {
153                                 Lift lift2;
154                                 store.@get (iter, 5, out lift2);
155                                 if (lift2 == null)
156                                         continue;
157                                 if (lift2.description == null)
158                                         yield adac.update_lift_details (lift2);
159                         } while (store.iter_next (ref iter));
160                 }
161
162                 Hildon.gtk_window_set_progress_indicator (this, 0);
163         }
164
165         private void on_row_activated (TreeView tree, TreePath /*_*/path, TreeViewColumn column) {
166                 TreeModel model = tree.model;
167                 TreeIter iter;
168                 Lift lift;
169
170                 if (model.get_iter (out iter, path)) {
171                         model.get (iter, 5, out lift);
172
173                         var window = new LiftDetailWindow (adac, lift);
174                         window.show ();
175                 }
176         }
177
178         private void on_orientation_changed () {
179                 route_column.set_visible (!BeifahrerProgram.orientation.portrait);
180         }
181 }