Debian packaging: 0.0.4-1
[beifahrer] / src / my-offers-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 MyOffersWindow : StackableWindow {
23         AdacMitfahrclub adac;
24         ListStore store;
25         Alignment alignment;
26         TreeView tree;
27         TreeViewColumn route_column;
28         Label no_offers;
29         bool preload = false;
30
31         public MyOffersWindow (AdacMitfahrclub _adac) {
32                 adac = _adac;
33         }
34
35         construct {
36                 set_title ("My offers");
37
38                 var menu = new AppMenu ();
39                 var goto_website = new Gtk.Button.with_label (_("Show website"));
40                 goto_website.show ();
41                 menu.append (goto_website);
42                 set_main_menu (menu);
43
44                 store = new ListStore (4, typeof (string), typeof (string), typeof (string), typeof (Lift));
45
46                 alignment = new Alignment (0.0f, 0.0f, 1.0f, 1.0f);
47                 alignment.top_padding = MARGIN_HALF;
48                 alignment.left_padding = MARGIN_DOUBLE;
49                 alignment.right_padding = MARGIN_DOUBLE;
50
51                 tree = (TreeView) Hildon.gtk_tree_view_new_with_model (UIMode.NORMAL, store);
52                 Hildon.gtk_widget_set_theme_size (tree, SizeType.FINGER_HEIGHT);
53
54                 tree.set_headers_visible (false);
55                 tree.set_rules_hint (true);
56
57                 // Tree selection object
58                 var selection = tree.get_selection ();
59                 selection.set_mode (SelectionMode.SINGLE);
60
61                 // Source and destination column
62                 route_column = new TreeViewColumn.with_attributes ("Route", new CellRendererText (), "markup", 0);
63                 route_column.set_reorderable (false);
64                 route_column.set_sizing (TreeViewColumnSizing.AUTOSIZE);
65                 route_column.set_expand (true);
66                 tree.append_column (route_column);
67
68                 // Date and time column
69                 var datetime_column = new TreeViewColumn.with_attributes ("DateTime", new CellRendererText (), "text", 1);
70                 datetime_column.set_reorderable (false);
71                 datetime_column.set_sizing (TreeViewColumnSizing.AUTOSIZE);
72                 datetime_column.set_expand (true);
73                 tree.append_column (datetime_column);
74
75                 // Active column
76                 var renderer = new CellRendererText ();
77                 Pango.AttrList attr_list = new Pango.AttrList ();
78                 var style = Gtk.rc_get_style_by_paths (Gtk.Settings.get_default (), "SmallSystemFont", null, typeof (void));
79                 if (style != null) {
80                         var attr_font_desc = new Pango.AttrFontDesc (style.font_desc.copy ());
81                         attr_list.insert ((owned) attr_font_desc);
82                 }
83                 Gdk.Color color;
84                 this.ensure_style ();
85                 if (this.style.lookup_color ("ActiveTextColor", out color)) {
86                         Pango.Attribute attr_color = Pango.attr_foreground_new (color.red, color.green, color.blue);
87                         attr_list.insert ((owned) attr_color);
88                 }
89                 renderer.attributes = attr_list;
90                 var active_column = new TreeViewColumn.with_attributes ("Active", renderer, "text", 2);
91                 active_column.set_reorderable (false);
92                 active_column.set_sizing (TreeViewColumnSizing.AUTOSIZE);
93                 active_column.set_expand (false);
94                 tree.append_column (active_column);
95
96                 var pannable = new PannableArea ();
97                 pannable.add (tree);
98
99                 no_offers = new Label (_("No offers"));
100                 Hildon.helper_set_logical_font (no_offers, "LargeSystemFont");
101                 Hildon.helper_set_logical_color (no_offers, RcFlags.FG, StateType.NORMAL, "SecondaryTextColor");
102                 no_offers.set_size_request (-1, 6 * 70);
103                 no_offers.set_alignment ((float) 0.5, (float) 0.42);
104
105                 var vbox = new VBox (false, 0);
106                 vbox.pack_start (pannable, true, true, 0);
107                 vbox.pack_start (no_offers, false, false, 0);
108
109                 alignment.add (vbox);
110                 add (alignment);
111
112                 alignment.show_all ();
113                 no_offers.hide ();
114                 route_column.set_visible (!BeifahrerProgram.orientation.portrait);
115
116                 tree.row_activated.connect (this.on_row_activated);
117                 Gdk.Screen.get_default ().size_changed.connect (this.on_orientation_changed);
118                 goto_website.clicked.connect (on_goto_website_clicked);
119         }
120
121         public async void get_my_offers () {
122                 TreeIter iter;
123                 Hildon.gtk_window_set_progress_indicator (this, 1);
124                 bool logged_in = yield adac.login_async ();
125                 if (!logged_in) {
126                         Banner.show_information (this, null, "Login failed.");
127                         return;
128                 }
129
130                 var lift_list = yield adac.get_my_offers ();
131                 foreach (Lift lift in lift_list) {
132                         string datetime = "%02d.%02d.%04d".printf (lift.time.day, lift.time.month, lift.time.year);
133                         if (lift.time.hour >= 0)
134                                 datetime += ", %d:%02d".printf (lift.time.hour, lift.time.minute);
135                         store.insert_with_values (out iter, -1, 0, lift.city_from + " - " + lift.city_to,
136                                                                 1, datetime,
137                                                                 2, (LiftFlags.ACTIVE in lift.flags) ? _("Active") : _("Inactive"),
138                                                                 3, lift);
139                 }
140
141                 if (lift_list.length () > 6)
142                         alignment.right_padding = MARGIN_DEFAULT;
143                 if (lift_list.length () == 0)
144                         no_offers.show ();
145
146                 Hildon.gtk_window_set_progress_indicator (this, 0);
147         }
148
149         void on_goto_website_clicked () {
150                 BeifahrerProgram.open_browser (this, adac.get_my_offers_url ());
151         }
152
153         private void on_row_activated (TreeView tree, TreePath path, TreeViewColumn column) {
154                 TreeModel model = tree.model;
155                 TreeIter iter;
156                 Lift lift;
157
158                 if (model.get_iter (out iter, path)) {
159                         model.get (iter, 3, out lift);
160
161                         var dialog = new OfferEditDialog (this, adac, lift);
162                         dialog.show ();
163                 }
164         }
165
166         private void on_orientation_changed (Gdk.Screen screen) {
167         //      route_column.set_visible (!BeifahrerProgram.orientation.portrait);
168         }
169 }