Turn lift date and time strings into struct Time
[beifahrer] / src / lift-detail-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 LiftDetailWindow : StackableWindow {
23         private const string BROWSER_SERVICE = "com.nokia.osso_browser";
24         private const string BROWSER_PATH = "/com/nokia/osso_browser";
25         private const string BROWSER_IF = "com.nokia.osso_browser";
26
27         private const string CALENDAR_SERVICE = "com.nokia.calendar";
28         private const string CALENDAR_PATH = "/com/nokia/calendar";
29         private const string CALENDAR_IF = "com.nokia.calendar";
30
31         AdacMitfahrclub adac;
32         Lift lift;
33         OssoABook.Button button_route;
34         OssoABook.Button button_calendar;
35         Label label_driver;
36         OssoABook.Button button_phone;
37         OssoABook.Button button_sms;
38         Image image_smoke;
39         Label label;
40
41         public LiftDetailWindow (AdacMitfahrclub _adac, Lift _lift) {
42                 adac = _adac;
43                 lift = _lift;
44
45                 update_lift_details ();
46
47                 if (lift.description == null)
48                         get_lift_details.begin ();
49         }
50
51         construct {
52                 set_title ("Beifahrer");
53
54                 var menu = new AppMenu ();
55                 var add_contact = new Gtk.Button.with_label (_("New contact"));
56                 add_contact.show ();
57                 menu.append (add_contact);
58                 var goto_website = new Gtk.Button.with_label (_("Show website"));
59                 goto_website.show ();
60                 menu.append (goto_website);
61                 set_main_menu (menu);
62
63                 var pannable = new PannableArea ();
64                 pannable.hscrollbar_policy = PolicyType.NEVER;
65
66                 var vbox = new VBox (false, 0);
67
68                 button_route = new OssoABook.Button (SizeType.FINGER_HEIGHT);
69                 button_route.style = OssoABook.ButtonStyle.LABEL;
70
71                 var table = new Table (5, 2, false);
72
73                 button_calendar = new OssoABook.Button (SizeType.FINGER_HEIGHT);
74                 button_calendar.set_icon_name ("general_calendar");
75                 button_calendar.set_icon_visible (true);
76                 button_calendar.style = OssoABook.ButtonStyle.LABEL;
77                 button_calendar.title = _("Departure time");
78
79                 label_driver = new Label (_("Driver:"));
80                 label_driver.set_alignment (0.0f, 0.5f);
81
82                 button_phone = new OssoABook.Button (SizeType.FINGER_HEIGHT);
83                 button_phone.set_icon_name ("general_call");
84                 button_phone.set_icon_visible (true);
85                 button_phone.title = _("Phone");
86
87                 button_sms = new OssoABook.Button (SizeType.FINGER_HEIGHT);
88                 button_sms.set_icon_name ("general_sms");
89                 button_sms.set_icon_visible (true);
90                 button_sms.title = _("SMS");
91
92                 image_smoke = new Image ();
93
94                 label = new Label ("");
95                 label.set_alignment (0.0f, 0.5f);
96                 label.set_line_wrap (true);
97
98                 table.attach (button_calendar, 1, 2, 0, 1, AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.FILL, 0, 0);
99                 table.attach (label_driver, 0, 1, 1, 2, AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.FILL, MARGIN_DEFAULT, 0);
100                 table.attach (button_phone, 1, 2, 1, 2, AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.FILL, 0, 0);
101                 table.attach (button_sms, 1, 2, 2, 3, AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.FILL, 0, 0);
102                 table.attach (image_smoke, 1, 2, 3, 4, AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.FILL, 0, 0);
103                 table.attach (label, 0, 2, 4, 5, AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.FILL, MARGIN_DEFAULT, 0);
104
105                 vbox.pack_start (button_route, true, true, 0);
106                 vbox.pack_start (table, true, true, 0);
107
108                 pannable.add_with_viewport (vbox);
109
110                 add (pannable);
111
112                 add_contact.clicked.connect (on_add_contact_clicked);
113                 goto_website.clicked.connect (on_goto_website_clicked);
114                 button_phone.clicked.connect (on_button_phone_clicked);
115                 button_sms.clicked.connect (on_button_sms_clicked);
116         }
117
118         private async void get_lift_details () {
119                 Hildon.gtk_window_set_progress_indicator (this, 1);
120                 yield adac.update_lift_details (lift);
121                 Hildon.gtk_window_set_progress_indicator (this, 0);
122
123                 update_lift_details ();
124         }
125
126         private void update_lift_details () {
127                 button_route.title = _("From %s to %s").printf (lift.city_from, lift.city_to);
128                 if (lift.city_via.length () > 0) {
129                         string route = _("via ");
130                         foreach (string via in lift.city_via)
131                                 route += via + " ";
132                         button_route.value = route;
133                 } else {
134                         button_route.value = "";
135                 }
136
137                 button_calendar.value = "%02d.%02d.%04d, %d:%02d".printf (lift.time.day, lift.time.month, lift.time.year, lift.time.hour, lift.time.minute);
138
139                 label_driver.set_text (_("Driver: ") + lift.name);
140                 if (lift.cell != null) {
141                         button_phone.title = _("Cell");
142                         string cell_number = E.normalize_phone_number (lift.cell);
143                         button_phone.value = cell_number;
144                         button_sms.value = cell_number;
145                 } else if (lift.phone != null) {
146                         button_phone.title = _("Phone");
147                         string phone_number = E.normalize_phone_number (lift.phone);
148                         button_phone.value = phone_number;
149                         button_sms.value = phone_number;
150                 }
151
152                 string lift_text = _("Free places: %d\n").printf (lift.places);
153                 lift_text += (lift.price != null) ? _("Price: %s\n").printf (lift.price) : _("(no price given)\n");
154                 if (LiftFlags.SMOKER in lift.flags) try {
155                         var pixbuf = IconTheme.get_default ().load_icon ("beifahrer_smoker", 32, IconLookupFlags.NO_SVG);
156                         image_smoke.pixbuf = pixbuf;
157                         lift_text += _("smoker ");
158                 } catch (Error e) {
159                 } else if (LiftFlags.NON_SMOKER in lift.flags) try {
160                         var pixbuf = IconTheme.get_default ().load_icon ("beifahrer_non_smoker", 32, IconLookupFlags.NO_SVG);
161                         image_smoke.pixbuf = pixbuf;
162                         lift_text += _("non-smoker ");
163                 } catch (Error e) {
164                 }
165                 if (LiftFlags.ADAC_MEMBER in lift.flags)
166                         lift_text += _("ADAC member ");
167                 if (LiftFlags.WOMEN_ONLY in lift.flags)
168                         lift_text += _("only women ");
169                 lift_text += "\n\n" + lift.description;
170
171                 if (lift.modified != null)
172                         lift_text += _("\nLast changed: ") + lift.modified;
173
174                 label.set_text (lift_text);
175         }
176
177         void on_button_phone_clicked () {
178                 var uri = "tel://";
179                 if (lift.cell != null)
180                         uri += E.normalize_phone_number (lift.cell);
181                 else if (lift.phone != null)
182                         uri += E.normalize_phone_number (lift.phone);
183                 try {
184                         var action = URIAction.get_default_action_by_uri (uri);
185                         if (action != null) {
186                                 action.open (uri);
187                         } else {
188                                 
189                                 Banner.show_information (this, null, _("Couldn't call %s URI").printf (uri));
190                         }
191                 } catch (Error e) {
192                         if (e is URIError) {
193                                 Banner.show_information (this, null, _("Error: %s").printf (e.message));
194                         }
195                 }
196         }
197
198         void on_button_sms_clicked () {
199                 var uri = "sms://";
200                 if (lift.cell != null)
201                         uri += E.normalize_phone_number (lift.cell);
202                 else if (lift.phone != null)
203                         uri += E.normalize_phone_number (lift.phone);
204                 try {
205                         var action = URIAction.get_default_action_by_uri (uri);
206                         if (action != null) {
207                                 action.open (uri);
208                         } else {
209                                 
210                                 Banner.show_information (this, null, _("Couldn't open %s URI").printf (uri));
211                         }
212                 } catch (Error e) {
213                         if (e is URIError) {
214                                 Banner.show_information (this, null, _("Error: %s").printf (e.message));
215                         }
216                 }
217         }
218
219         void on_add_contact_clicked () {
220                 var contact = new OssoABook.Contact ();
221                 var attr_fn = new E.VCardAttribute (null, E.EVC_FN);
222                 contact.add_attribute_with_value ((owned) attr_fn, lift.name);
223                 if (lift.cell != null) {
224                         var attr_cell = new E.VCardAttribute (null, E.EVC_TEL);
225                         var param = new E.VCardAttributeParam (E.EVC_TYPE);
226                         attr_cell.add_param_with_value ((owned) param, "CELL");
227                         contact.add_attribute_with_value ((owned) attr_cell, E.normalize_phone_number (lift.cell));
228                 }
229                 if (lift.phone != null) {
230                         var attr_phone = new E.VCardAttribute (null, E.EVC_TEL);
231                         var param = new E.VCardAttributeParam (E.EVC_TYPE);
232                         attr_phone.add_param_with_value ((owned) param, "VOICE");
233                         contact.add_attribute_with_value ((owned) attr_phone, E.normalize_phone_number (lift.phone));
234                 }
235                 var dialog = new OssoABook.TemporaryContactDialog.with_contact (this, contact);
236                 dialog.show ();
237         }
238
239         void on_goto_website_clicked () {
240                 var url = "http://mitfahrclub.adac.de" + lift.href;
241
242                 try {
243                         var conn = DBus.Bus.get (DBus.BusType.SESSION);
244
245                         dynamic DBus.Object browser = conn.get_object (BROWSER_SERVICE, BROWSER_PATH, BROWSER_IF);
246                         browser.open_new_window (url, false);
247                 } catch (Error e) {
248                         stderr.printf ("Error: %s\n", e.message);
249                         Banner.show_information (this, null, _("Failed to open browser."));
250                 }
251         }
252 }