1 /* This file is part of Beifahrer.
3 * Copyright (C) 2010 Philipp Zabel
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.
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.
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/>.
22 public class LiftListWindow : StackableWindow {
23 public const string GCONF_KEY_PRELOAD = "/apps/beifahrer/preload";
37 TreeSortable sortable;
40 TreeViewColumn route_column;
46 Gtk.Button goto_website;
48 public LiftListWindow (AdacMitfahrclub _adac) {
53 set_title ("Beifahrer");
55 var menu = new AppMenu ();
56 var sort_by_time = new RadioButton.with_label (null, _("Time"));
57 sort_by_time.set_mode (false);
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);
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);
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;
77 tree = (TreeView) Hildon.gtk_tree_view_new_with_model (UIMode.NORMAL, sortable);
78 Hildon.gtk_widget_set_theme_size (tree, SizeType.FINGER_HEIGHT);
80 tree.set_headers_visible (false);
81 tree.set_rules_hint (true);
83 // Tree selection object
84 var selection = tree.get_selection ();
85 selection.set_mode (SelectionMode.SINGLE);
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);
95 var vbox_renderer = new CellRendererVBox ();
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);
103 // Add secondary text to column (driver name, route details)
104 renderer = new CellRendererText ();
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));
111 var attr_font_desc = new Pango.AttrFontDesc (style.font_desc.copy ());
112 attr_list.insert ((owned) attr_font_desc);
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);
120 renderer.attributes = attr_list;
121 vbox_renderer.append (renderer, true);
122 vbox_renderer.set_data ("secondary-renderer", renderer);
124 route_column.pack_start (vbox_renderer, true);
125 route_column.set_cell_data_func (vbox_renderer, vbox_data_func);
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);
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);
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);
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);
155 var pannable = new PannableArea ();
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);
164 var vbox = new VBox (false, 0);
165 vbox.pack_start (pannable, true, true, 0);
166 vbox.pack_start (no_lifts, false, false, 0);
168 alignment.add (vbox);
171 alignment.show_all ();
173 route_column.set_visible (!BeifahrerProgram.orientation.portrait);
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);
182 sort_by_price.toggled.connect (button => {
183 if (button.get_active ())
184 sortable.set_sort_column_id (Columns.PRICE, Gtk.SortType.ASCENDING);
188 private void vbox_data_func (CellLayout cell_layout, CellRenderer cell, TreeModel model, TreeIter iter) {
190 CellRendererText renderer;
192 model.get (iter, Columns.ROUTE, out text);
193 renderer = cell.get_data ("route-renderer");
194 renderer.text = text;
196 model.get (iter, Columns.SECONDARY, out text);
197 renderer = cell.get_data ("secondary-renderer");
198 renderer.text = text;
201 public async void find_lifts (string city_from, int radius_from, string city_to, int radius_to, Date date, int tolerance = 0) {
203 set_title ("%s - %s".printf (city_from, city_to));
204 Hildon.gtk_window_set_progress_indicator (this, 1);
206 url = adac.get_lift_list_url (city_from, radius_from, city_to, radius_to, date, tolerance);
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,
227 // (LiftFlags.WOMEN_ONLY,ADAC_MEMBER in lift.flags)
229 if (lift_list.length () > 6)
230 alignment.right_padding = MARGIN_DEFAULT;
231 if (lift_list.length () == 0)
234 bool preload = false;
236 preload = gconf.get_bool (GCONF_KEY_PRELOAD);
240 if (store.get_iter_first (out iter)) do {
242 store.@get (iter, Columns.LIFT, out lift2);
245 if (lift2.description == null) {
246 yield adac.update_lift_details (lift2);
247 store.@set (iter, Columns.SECONDARY, lift2.name);
249 } while (store.iter_next (ref iter));
252 Hildon.gtk_window_set_progress_indicator (this, 0);
255 void on_goto_website_clicked () {
256 BeifahrerProgram.open_browser (this, url);
259 private void on_row_activated (TreeView tree, TreePath /*_*/path, TreeViewColumn column) {
260 TreeModel model = tree.model;
264 if (model.get_iter (out iter, path)) {
265 model.get (iter, Columns.LIFT, out lift);
267 var window = new LiftDetailWindow (adac, lift);
272 private void on_orientation_changed (Gdk.Screen screen) {
273 route_column.set_visible (!BeifahrerProgram.orientation.portrait);