Use common open browser method for show website menu buttons
[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 CALENDAR_SERVICE = "com.nokia.calendar";
24         private const string CALENDAR_PATH = "/com/nokia/calendar";
25         private const string CALENDAR_IF = "com.nokia.calendar";
26
27         AdacMitfahrclub adac;
28         Lift lift;
29         Alignment alignment;
30         PannableArea pannable;
31         VBox vbox;
32         OssoABook.Button button_route;
33         OssoABook.Button button_calendar;
34         Label label_driver;
35         OssoABook.Button button_phone;
36         OssoABook.Button button_sms;
37         OssoABook.Button button_email;
38         Image image_smoke;
39         Label label;
40         Label label_changed;
41         Table table;
42         string ics_filename = null;
43
44         public LiftDetailWindow (AdacMitfahrclub _adac, Lift _lift) {
45                 adac = _adac;
46                 lift = _lift;
47
48                 update_lift_details ();
49
50                 if (lift.description == null)
51                         get_lift_details.begin ();
52         }
53
54         construct {
55                 set_title ("Beifahrer");
56
57                 var menu = new AppMenu ();
58                 var add_contact = new Gtk.Button.with_label (_("New contact"));
59                 add_contact.show ();
60                 menu.append (add_contact);
61                 var goto_website = new Gtk.Button.with_label (_("Show website"));
62                 goto_website.show ();
63                 menu.append (goto_website);
64                 set_main_menu (menu);
65
66                 alignment = new Alignment (0.0f, 0.0f, 1.0f, 1.0f);
67                 alignment.top_padding = MARGIN_HALF;
68                 alignment.left_padding = MARGIN_DOUBLE;
69                 alignment.right_padding = MARGIN_DOUBLE;
70
71                 pannable = new PannableArea ();
72                 pannable.hscrollbar_policy = PolicyType.NEVER;
73
74                 vbox = new VBox (false, 0);
75
76                 var hbox = new HBox (false, 0);
77
78                 button_route = new OssoABook.Button (SizeType.FINGER_HEIGHT);
79                 button_route.style = OssoABook.ButtonStyle.LABEL;
80
81                 var image_eventbox = new EventBox ();
82                 image_smoke = new Image ();
83                 image_eventbox.add (image_smoke);
84
85                 hbox.pack_start (button_route, true, true, 0);
86                 hbox.pack_start (image_eventbox, false, false, MARGIN_DEFAULT);
87
88                 table = new Table (7, 2, false);
89
90                 button_calendar = new OssoABook.Button (SizeType.FINGER_HEIGHT);
91                 button_calendar.set_icon_name ("general_calendar");
92                 button_calendar.set_icon_visible (true);
93                 button_calendar.title = _("Departure time");
94
95                 label_driver = new Label (_("Driver:"));
96                 label_driver.set_alignment (0.0f, 0.5f);
97                 Hildon.gtk_widget_set_theme_size (label_driver, SizeType.FINGER_HEIGHT);
98
99                 button_phone = new OssoABook.Button (SizeType.FINGER_HEIGHT);
100                 button_phone.set_icon_name ("general_call");
101                 button_phone.set_icon_visible (true);
102                 button_phone.title = _("Phone");
103
104                 button_sms = new OssoABook.Button (SizeType.FINGER_HEIGHT);
105                 button_sms.set_icon_name ("general_sms");
106                 button_sms.set_icon_visible (true);
107                 button_sms.title = _("SMS");
108
109                 button_email = new OssoABook.Button (SizeType.FINGER_HEIGHT);
110                 button_email.set_icon_name ("general_email");
111                 button_email.set_icon_visible (true);
112                 button_email.title = _("E-mail");
113
114                 label = new Label ("");
115                 label.set_alignment (0.0f, 0.5f);
116                 label.set_line_wrap (true);
117
118                 label_changed = new Label ("");
119                 label_changed.set_alignment (1.0f, 0.0f);
120                 Hildon.helper_set_logical_color (label_changed, RcFlags.FG, StateType.NORMAL, "SecondaryTextColor");
121                 Hildon.helper_set_logical_font (label_changed, "SmallSystemFont");
122
123                 if (BeifahrerProgram.orientation.portrait) {
124                         table.attach (button_calendar, 0, 2, 0, 1, AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.FILL, 0, 0);
125                         table.attach (label_driver, 0, 2, 1, 2, AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.FILL, MARGIN_DEFAULT, 0);
126                         table.attach (button_phone, 0, 2, 2, 3, AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.FILL, 0, 0);
127                         table.attach (button_sms, 0, 2, 3, 4, AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.FILL, 0, 0);
128                         table.attach (button_email, 0, 2, 4, 5, AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.FILL, 0, 0);
129                 } else {
130                         table.attach (button_calendar, 1, 2, 0, 1, AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.FILL, 0, 0);
131                         table.attach (label_driver, 0, 1, 1, 2, AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.FILL, MARGIN_DEFAULT, 0);
132                         table.attach (button_phone, 1, 2, 1, 2, AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.FILL, 0, 0);
133                         table.attach (button_sms, 1, 2, 2, 3, AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.FILL, 0, 0);
134                         table.attach (button_email, 1, 2, 3, 4, AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.FILL, 0, 0);
135                 }
136                 table.attach (label, 0, 2, 5, 6, AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.FILL, MARGIN_DEFAULT, 0);
137                 table.attach (label_changed, 0, 2, 6, 7, AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.FILL, MARGIN_DEFAULT, 0);
138
139                 vbox.pack_start (hbox, true, true, 0);
140                 vbox.pack_start (table, true, true, 0);
141
142                 pannable.add_with_viewport (vbox);
143                 alignment.add (pannable);
144
145                 alignment.show_all ();
146                 button_email.hide ();
147                 add (alignment);
148
149                 Gdk.Screen.get_default ().size_changed.connect (on_orientation_changed);
150                 add_contact.clicked.connect (on_add_contact_clicked);
151                 goto_website.clicked.connect (on_goto_website_clicked);
152                 image_eventbox.button_press_event.connect (on_image_eventbox_button_pressed);
153                 button_calendar.clicked.connect (on_button_calendar_clicked);
154                 button_phone.clicked.connect (on_button_phone_clicked);
155                 button_sms.clicked.connect (on_button_sms_clicked);
156                 button_email.clicked.connect (on_button_email_clicked);
157                 destroy.connect (on_destroy);
158                 map_event.connect (on_map_event);
159         }
160
161         private async void get_lift_details () {
162                 Hildon.gtk_window_set_progress_indicator (this, 1);
163                 yield adac.update_lift_details (lift);
164                 Hildon.gtk_window_set_progress_indicator (this, 0);
165
166                 update_lift_details ();
167         }
168
169         private void update_lift_details () {
170                 button_route.title = _("From %s to %s").printf (lift.city_from, lift.city_to);
171                 if (lift.city_via.length () > 0) {
172                         string route = _("via ");
173                         foreach (string via in lift.city_via)
174                                 route += via + " ";
175                         button_route.value = route;
176                 } else {
177                         button_route.value = "";
178                 }
179
180                 button_calendar.value = "%02d.%02d.%04d, %d:%02d".printf (lift.time.day, lift.time.month, lift.time.year, lift.time.hour, lift.time.minute);
181
182                 label_driver.set_text (_("Driver: ") + lift.name);
183                 if (lift.cell != null) {
184                         button_phone.title = _("Cell");
185                         string cell_number = E.normalize_phone_number (lift.cell);
186                         button_phone.value = cell_number;
187                         button_sms.value = cell_number;
188                 } else if (lift.phone != null) {
189                         button_phone.title = _("Phone");
190                         string phone_number = E.normalize_phone_number (lift.phone);
191                         button_phone.value = phone_number;
192                         button_sms.value = phone_number;
193                 }
194
195                 if (lift.email_image_uri != null) {
196                         button_email.value = _("Click to view E-mail image");
197                         button_email.show ();
198                 }
199
200                 string lift_text = _("Free places: %d\n").printf (lift.places);
201                 lift_text += (lift.price != null) ? _("Price: %s\n").printf (lift.price) : _("(no price given)\n");
202                 if (LiftFlags.SMOKER in lift.flags) try {
203                         var pixbuf = IconTheme.get_default ().load_icon ("beifahrer_smoker", 32, IconLookupFlags.NO_SVG);
204                         image_smoke.pixbuf = pixbuf;
205                 } catch (Error e) {
206                 } else if (LiftFlags.NON_SMOKER in lift.flags) try {
207                         var pixbuf = IconTheme.get_default ().load_icon ("beifahrer_non_smoker", 32, IconLookupFlags.NO_SVG);
208                         image_smoke.pixbuf = pixbuf;
209                 } catch (Error e) {
210                 }
211                 if (LiftFlags.ADAC_MEMBER in lift.flags)
212                         lift_text += _("ADAC member ");
213                 if (LiftFlags.WOMEN_ONLY in lift.flags)
214                         lift_text += _("only women ");
215                 lift_text += "\n\n" + lift.description;
216
217                 label.set_text (lift_text);
218
219                 if (lift.modified != null)
220                         label_changed.set_text (_("\nLast changed: ") + lift.modified);
221
222                 // Reduce right margin for scrollbar, if update_lift_details is called after mapping the window
223                 if (vbox.allocation.height > pannable.allocation.height)
224                         alignment.right_padding = MARGIN_DEFAULT;
225         }
226
227         void on_button_calendar_clicked () {
228                 var ical = new StringBuilder ("BEGIN:VCALENDAR\nVERSION:2.0\nMETHOD:PUBLISH\nBEGIN:VEVENT\nUID:");
229                 ical.append ("beifahrer" + lift.href); // UID
230                 ical.append ("\nCLASS:PUBLIC\nSUMMARY:");
231                 ical.append_printf (_("From %s to %s with %s"), lift.city_from, lift.city_to, lift.name);
232                 ical.append ("\nDTSTART;TZID=Europe/Berlin:");
233                 ical.append_printf ("%04d%02d%02dT%02d%02d00", lift.time.year + 2000, lift.time.month, lift.time.day, lift.time.hour, lift.time.minute);
234                 ical.append ("\nDTEND;TZID=Europe/Berlin:");
235                 ical.append_printf ("%04d%02d%02dT%02d%02d00", lift.time.year + 2000, lift.time.month, lift.time.day, lift.time.hour, lift.time.minute);
236                 ical.append ("\nDESCRIPTION:");
237                 ical.append (lift.description.replace ("\n", "\\n"));
238                 ical.append ("\nEND:VEVENT\nEND:VCALENDAR\n");
239
240                 ics_filename = Path.build_path ("/", Environment.get_tmp_dir (),
241                                                 "beifahrer.ics");
242                 try {
243                         FileUtils.set_contents (ics_filename, ical.str);
244                 } catch (FileError e) {
245                         stderr.printf ("Failed to write %s: %s\n", ics_filename, e.message);
246                         return;
247                 }
248
249                 var error = DBus.RawError ();
250                 var bus = DBus.RawBus.get (DBus.BusType.SESSION, ref error);
251                 var res = HildonMime.open_file (bus, "file://" + ics_filename);
252         }
253
254         void on_destroy () {
255                 if (ics_filename != null)
256                         FileUtils.remove (ics_filename);
257         }
258
259         bool on_map_event (Gdk.Event event) {
260                 // Reduce right margin for scrollbar, for preloaded lift
261                 if (vbox.allocation.height > pannable.allocation.height)
262                         alignment.right_padding = MARGIN_DEFAULT;
263                 return true;
264         }
265
266         void on_button_phone_clicked () {
267                 var uri = "tel://";
268                 if (lift.cell != null)
269                         uri += E.normalize_phone_number (lift.cell);
270                 else if (lift.phone != null)
271                         uri += E.normalize_phone_number (lift.phone);
272                 try {
273                         var action = URIAction.get_default_action_by_uri (uri);
274                         if (action != null) {
275                                 action.open (uri);
276                         } else {
277                                 
278                                 Banner.show_information (this, null, _("Couldn't call %s URI").printf (uri));
279                         }
280                 } catch (Error e) {
281                         if (e is URIError) {
282                                 Banner.show_information (this, null, _("Error: %s").printf (e.message));
283                         }
284                 }
285         }
286
287         void on_button_sms_clicked () {
288                 var uri = "sms://";
289                 if (lift.cell != null)
290                         uri += E.normalize_phone_number (lift.cell);
291                 else if (lift.phone != null)
292                         uri += E.normalize_phone_number (lift.phone);
293                 try {
294                         var action = URIAction.get_default_action_by_uri (uri);
295                         if (action != null) {
296                                 action.open (uri);
297                         } else {
298                                 
299                                 Banner.show_information (this, null, _("Couldn't open %s URI").printf (uri));
300                         }
301                 } catch (Error e) {
302                         if (e is URIError) {
303                                 Banner.show_information (this, null, _("Error: %s").printf (e.message));
304                         }
305                 }
306         }
307
308         void on_button_email_clicked () {
309                 var uri = "http://mitfahrclub.adac.de";
310                 uri += lift.email_image_uri;
311                 try {
312                         var action = URIAction.get_default_action_by_uri (uri);
313                         if (action != null) {
314                                 action.open (uri);
315                         } else {
316
317                                 Banner.show_information (this, null, _("Couldn't open %s URI").printf (uri));
318                         }
319                 } catch (Error e) {
320                         if (e is URIError) {
321                                 Banner.show_information (this, null, _("Error: %s").printf (e.message));
322                         }
323                 }
324         }
325
326         void on_add_contact_clicked () {
327                 var contact = new OssoABook.Contact ();
328                 var attr_fn = new E.VCardAttribute (null, E.EVC_FN);
329                 contact.add_attribute_with_value ((owned) attr_fn, lift.name);
330                 if (lift.cell != null) {
331                         var attr_cell = new E.VCardAttribute (null, E.EVC_TEL);
332                         var param = new E.VCardAttributeParam (E.EVC_TYPE);
333                         attr_cell.add_param_with_value ((owned) param, "CELL");
334                         contact.add_attribute_with_value ((owned) attr_cell, E.normalize_phone_number (lift.cell));
335                 }
336                 if (lift.phone != null) {
337                         var attr_phone = new E.VCardAttribute (null, E.EVC_TEL);
338                         var param = new E.VCardAttributeParam (E.EVC_TYPE);
339                         attr_phone.add_param_with_value ((owned) param, "VOICE");
340                         contact.add_attribute_with_value ((owned) attr_phone, E.normalize_phone_number (lift.phone));
341                 }
342                 var dialog = new OssoABook.TemporaryContactDialog.with_contact (this, contact);
343                 dialog.show ();
344         }
345
346         void on_goto_website_clicked () {
347                 BeifahrerProgram.open_browser (this, adac.get_lift_details_url (lift));
348         }
349
350         bool on_image_eventbox_button_pressed (Gdk.EventButton event) {
351                 if (event.button != 1)
352                         return true;
353                 if (LiftFlags.SMOKER in lift.flags)
354                         Banner.show_information (this, null, _("Smoker"));
355                 if (LiftFlags.NON_SMOKER in lift.flags)
356                         Banner.show_information (this, null, _("Non-smoker"));
357                 return false;
358         }
359
360         void on_orientation_changed (Gdk.Screen screen) {
361                 // Maybe this will be fixed directly in Maemo-GTK, see
362                 // http://lists.maemo.org/pipermail/maemo-developers/2010-May/026332.html
363                 vbox.queue_resize ();
364
365                 table.remove (button_calendar);
366                 table.remove (label_driver);
367                 table.remove (button_phone);
368                 table.remove (button_sms);
369                 table.remove (button_email);
370                 if (BeifahrerProgram.orientation.portrait) {
371                         table.attach (button_calendar, 0, 2, 0, 1, AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.FILL, 0, 0);
372                         table.attach (label_driver, 0, 2, 1, 2, AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.FILL, MARGIN_DEFAULT, 0);
373                         table.attach (button_phone, 0, 2, 2, 3, AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.FILL, 0, 0);
374                         table.attach (button_sms, 0, 2, 3, 4, AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.FILL, 0, 0);
375                         table.attach (button_email, 0, 2, 4, 5, AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.FILL, 0, 0);
376                 } else {
377                         table.attach (button_calendar, 1, 2, 0, 1, AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.FILL, 0, 0);
378                         table.attach (label_driver, 0, 1, 1, 2, AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.FILL, MARGIN_DEFAULT, 0);
379                         table.attach (button_phone, 1, 2, 1, 2, AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.FILL, 0, 0);
380                         table.attach (button_sms, 1, 2, 2, 3, AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.FILL, 0, 0);
381                         table.attach (button_email, 1, 2, 3, 4, AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.FILL, 0, 0);
382                 }
383         }
384 }