Add VBox cell renderer to display driver name in lift list
[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         enum Columns {
26                 ROUTE,
27                 SECONDARY,
28                 DATETIME,
29                 PLACES,
30                 PRICE,
31                 SMOKER,
32                 LIFT
33         }
34
35         AdacMitfahrclub adac;
36         ListStore store;
37         TreeSortable sortable;
38         Alignment alignment;
39         TreeView tree;
40         TreeViewColumn route_column;
41         Label no_lifts;
42         GConf.Client gconf;
43         bool preload = false;
44         string url;
45
46         Gtk.Button goto_website;
47
48         public LiftListWindow (AdacMitfahrclub _adac) {
49                 adac = _adac;
50         }
51
52         construct {
53                 set_title ("Beifahrer");
54
55                 var menu = new AppMenu ();
56                 var sort_by_time = new RadioButton.with_label (null, _("Time"));
57                 sort_by_time.set_mode (false);
58                 sort_by_time.show ();
59                 var sort_by_price = new RadioButton.with_label_from_widget (sort_by_time, _("Price"));
60                 sort_by_price.set_mode (false);
61                 sort_by_price.show ();
62                 menu.add_filter (sort_by_time);
63                 menu.add_filter (sort_by_price);
64                 goto_website = new Gtk.Button.with_label (_("Show website"));
65                 menu.append (goto_website);
66                 set_main_menu (menu);
67
68                 gconf = GConf.Client.get_default ();
69                 store = new ListStore (7, typeof (string), typeof (string), typeof (string), typeof (string), typeof (string), typeof (string), typeof (Lift));
70                 sortable = new TreeModelSort.with_model (store);
71
72                 alignment = new Alignment (0.0f, 0.0f, 1.0f, 1.0f);
73                 alignment.top_padding = MARGIN_HALF;
74                 alignment.left_padding = MARGIN_DOUBLE;
75                 alignment.right_padding = MARGIN_DOUBLE;
76
77                 tree = (TreeView) Hildon.gtk_tree_view_new_with_model (UIMode.NORMAL, sortable);
78                 Hildon.gtk_widget_set_theme_size (tree, SizeType.FINGER_HEIGHT);
79
80                 tree.set_headers_visible (false);
81                 tree.set_rules_hint (true);
82
83                 // Tree selection object
84                 var selection = tree.get_selection ();
85                 selection.set_mode (SelectionMode.SINGLE);
86
87                 // Source - destination / driver column
88                 var route_column = new TreeViewColumn ();
89                 route_column.set_title (_("Route"));
90                 route_column.set_reorderable (false);
91                 route_column.set_sizing (TreeViewColumnSizing.AUTOSIZE);
92                 route_column.set_expand (true);
93                 tree.append_column (route_column);
94
95                 var vbox_renderer = new CellRendererVBox ();
96
97                 var renderer = new CellRendererText ();
98                 renderer.yalign = 1.0f;
99                 renderer.set_fixed_size (-1, 32);
100                 vbox_renderer.append (renderer, true);
101                 vbox_renderer.set_data ("route-renderer", renderer);
102
103                 // Add secondary text to column (driver name, route details)
104                 renderer = new CellRendererText ();
105                 renderer.yalign = 0;
106                 renderer.set_fixed_size (-1, 32);
107                 renderer.ellipsize = Pango.EllipsizeMode.END;
108                 Pango.AttrList attr_list = new Pango.AttrList ();
109                 var style = Gtk.rc_get_style_by_paths (Gtk.Settings.get_default (), "SmallSystemFont", null, typeof (void));
110                 if (style != null) {
111                         var attr_font_desc = new Pango.AttrFontDesc (style.font_desc.copy ());
112                         attr_list.insert ((owned) attr_font_desc);
113                 }
114                 Gdk.Color color;
115                 this.ensure_style ();
116                 if (this.style.lookup_color ("SecondaryTextColor", out color)) {
117                         Pango.Attribute attr_color = Pango.attr_foreground_new (color.red, color.green, color.blue);
118                         attr_list.insert ((owned) attr_color);
119                 }
120                 renderer.attributes = attr_list;
121                 vbox_renderer.append (renderer, true);
122                 vbox_renderer.set_data ("secondary-renderer", renderer);
123
124                 route_column.pack_start (vbox_renderer, true);
125                 route_column.set_cell_data_func (vbox_renderer, vbox_data_func);
126
127                 // Date and time column
128                 var datetime_column = new TreeViewColumn.with_attributes ("DateTime", new CellRendererText (), "text", Columns.DATETIME);
129                 datetime_column.set_reorderable (false);
130                 datetime_column.set_sizing (TreeViewColumnSizing.AUTOSIZE);
131                 datetime_column.set_expand (true);
132                 tree.append_column (datetime_column);
133
134                 // Free places column
135                 var places_column = new TreeViewColumn.with_attributes ("Places", new CellRendererText (), "text", Columns.PLACES);
136                 places_column.set_reorderable (false);
137                 places_column.set_sizing (TreeViewColumnSizing.AUTOSIZE);
138                 places_column.set_expand (true);
139                 tree.append_column (places_column);
140
141                 // Price column
142                 var price_column = new TreeViewColumn.with_attributes ("Price", new CellRendererText (), "text", Columns.PRICE);
143                 price_column.set_reorderable (false);
144                 price_column.set_sizing (TreeViewColumnSizing.AUTOSIZE);
145                 price_column.set_expand (true);
146                 tree.append_column (price_column);
147
148                 // Smoker/Non-smoker column
149                 var smoke_column = new TreeViewColumn.with_attributes ("Smoker", new CellRendererPixbuf (), "icon-name", Columns.SMOKER);
150                 smoke_column.set_reorderable (false);
151                 smoke_column.set_sizing (TreeViewColumnSizing.AUTOSIZE);
152                 smoke_column.set_expand (false);
153                 tree.append_column (smoke_column);
154
155                 var pannable = new PannableArea ();
156                 pannable.add (tree);
157
158                 no_lifts = new Label (_("No lifts"));
159                 Hildon.helper_set_logical_font (no_lifts, "LargeSystemFont");
160                 Hildon.helper_set_logical_color (no_lifts, RcFlags.FG, StateType.NORMAL, "SecondaryTextColor");
161                 no_lifts.set_size_request (-1, 6 * 70);
162                 no_lifts.set_alignment ((float) 0.5, (float) 0.42);
163
164                 var vbox = new VBox (false, 0);
165                 vbox.pack_start (pannable, true, true, 0);
166                 vbox.pack_start (no_lifts, false, false, 0);
167
168                 alignment.add (vbox);
169                 add (alignment);
170
171                 alignment.show_all ();
172                 no_lifts.hide ();
173                 route_column.set_visible (!BeifahrerProgram.orientation.portrait);
174
175                 tree.row_activated.connect (this.on_row_activated);
176                 Gdk.Screen.get_default ().size_changed.connect (this.on_orientation_changed);
177                 goto_website.clicked.connect (on_goto_website_clicked);
178                 sort_by_time.toggled.connect (button => {
179                         if (button.get_active ())
180                                 sortable.set_sort_column_id (Columns.DATETIME, Gtk.SortType.ASCENDING);
181                 });
182                 sort_by_price.toggled.connect (button => {
183                         if (button.get_active ())
184                                 sortable.set_sort_column_id (Columns.PRICE, Gtk.SortType.ASCENDING);
185                 });
186         }
187
188         private void vbox_data_func (CellLayout cell_layout, CellRenderer cell, TreeModel model, TreeIter iter) {
189                 unowned string text;
190                 CellRendererText renderer;
191
192                 model.get (iter, Columns.ROUTE, out text);
193                 renderer = cell.get_data ("route-renderer");
194                 renderer.text = text;
195
196                 model.get (iter, Columns.SECONDARY, out text);
197                 renderer = cell.get_data ("secondary-renderer");
198                 renderer.text = text;
199         }
200
201         public async void find_lifts (string city_from, int radius_from, string city_to, int radius_to, Date date, int tolerance = 0) {
202                 TreeIter iter;
203                 set_title ("%s - %s".printf (city_from, city_to));
204                 Hildon.gtk_window_set_progress_indicator (this, 1);
205
206                 url = adac.get_lift_list_url (city_from, radius_from, city_to, radius_to, date, tolerance);
207                 if (url == null)
208                         return;
209                 goto_website.show ();
210                 var lift_list = yield adac.get_lift_list (city_from, radius_from, city_to, radius_to, date, tolerance);
211                 foreach (Lift lift in lift_list) {
212                         string icon_name = null;
213                         if (LiftFlags.SMOKER in lift.flags)
214                                 icon_name = "beifahrer_smoker";
215                         else if (LiftFlags.NON_SMOKER in lift.flags)
216                                 icon_name = "beifahrer_non_smoker";
217                         string datetime = "%02d.%02d.%04d".printf (lift.time.day, lift.time.month, lift.time.year);
218                         if (lift.time.hour >= 0)
219                                 datetime += ", %d:%02d".printf (lift.time.hour, lift.time.minute);
220                         store.insert_with_values (out iter, -1, Columns.ROUTE, lift.city_from + " - " + lift.city_to,
221                                                                 Columns.SECONDARY, "",
222                                                                 Columns.DATETIME, datetime,
223                                                                 Columns.PLACES, _("%d pl.").printf (lift.places),
224                                                                 Columns.PRICE, lift.price,
225                                                                 Columns.SMOKER, icon_name,
226                                                                 Columns.LIFT, lift);
227                         // (LiftFlags.WOMEN_ONLY,ADAC_MEMBER in lift.flags)
228                 }
229                 if (lift_list.length () > 6)
230                         alignment.right_padding = MARGIN_DEFAULT;
231                 if (lift_list.length () == 0)
232                         no_lifts.show ();
233
234                 bool preload = false;
235                 try {
236                         preload = gconf.get_bool (GCONF_KEY_PRELOAD);
237                 } catch (Error e) {
238                 }
239                 if (preload) {
240                         if (store.get_iter_first (out iter)) do {
241                                 Lift lift2;
242                                 store.@get (iter, Columns.LIFT, out lift2);
243                                 if (lift2 == null)
244                                         continue;
245                                 if (lift2.description == null) {
246                                         yield adac.update_lift_details (lift2);
247                                         store.@set (iter, Columns.SECONDARY, lift2.name);
248                                 }
249                         } while (store.iter_next (ref iter));
250                 }
251
252                 Hildon.gtk_window_set_progress_indicator (this, 0);
253         }
254
255         void on_goto_website_clicked () {
256                 BeifahrerProgram.open_browser (this, url);
257         }
258
259         private void on_row_activated (TreeView tree, TreePath /*_*/path, TreeViewColumn column) {
260                 TreeModel model = tree.model;
261                 TreeIter iter;
262                 Lift lift;
263
264                 if (model.get_iter (out iter, path)) {
265                         model.get (iter, Columns.LIFT, out lift);
266
267                         var window = new LiftDetailWindow (adac, lift);
268                         window.show ();
269                 }
270         }
271
272         private void on_orientation_changed (Gdk.Screen screen) {
273                 route_column.set_visible (!BeifahrerProgram.orientation.portrait);
274         }
275 }