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