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