Clickable calendar button: add lift to calendar
[beifahrer] / src / lift-detail-window.vala
index c354383..35b859d 100644 (file)
@@ -37,6 +37,7 @@ public class LiftDetailWindow : StackableWindow {
        OssoABook.Button button_sms;
        Image image_smoke;
        Label label;
+       string ics_filename = null;
 
        public LiftDetailWindow (AdacMitfahrclub _adac, Lift _lift) {
                adac = _adac;
@@ -73,7 +74,6 @@ public class LiftDetailWindow : StackableWindow {
                button_calendar = new OssoABook.Button (SizeType.FINGER_HEIGHT);
                button_calendar.set_icon_name ("general_calendar");
                button_calendar.set_icon_visible (true);
-               button_calendar.style = OssoABook.ButtonStyle.LABEL;
                button_calendar.title = _("Departure time");
 
                label_driver = new Label (_("Driver:"));
@@ -111,8 +111,10 @@ public class LiftDetailWindow : StackableWindow {
 
                add_contact.clicked.connect (on_add_contact_clicked);
                goto_website.clicked.connect (on_goto_website_clicked);
+               button_calendar.clicked.connect (on_button_calendar_clicked);
                button_phone.clicked.connect (on_button_phone_clicked);
                button_sms.clicked.connect (on_button_sms_clicked);
+               destroy.connect (on_destroy);
        }
 
        private async void get_lift_details () {
@@ -174,6 +176,38 @@ public class LiftDetailWindow : StackableWindow {
                label.set_text (lift_text);
        }
 
+       void on_button_calendar_clicked () {
+               var ical = new StringBuilder ("BEGIN:VCALENDAR\nVERSION:2.0\nMETHOD:PUBLISH\nBEGIN:VEVENT\nUID:");
+               ical.append ("beifahrer" + lift.href); // UID
+               ical.append ("\nCLASS:PUBLIC\nSUMMARY:");
+               ical.append_printf (_("From %s to %s with %s"), lift.city_from, lift.city_to, lift.name);
+               ical.append ("\nDTSTART;TZID=Europe/Berlin:");
+               ical.append_printf ("%04d%02d%02dT%02d%02d00", lift.time.year + 2000, lift.time.month, lift.time.day, lift.time.hour, lift.time.minute);
+               ical.append ("\nDTEND;TZID=Europe/Berlin:");
+               ical.append_printf ("%04d%02d%02dT%02d%02d00", lift.time.year + 2000, lift.time.month, lift.time.day, lift.time.hour, lift.time.minute);
+               ical.append ("\nDESCRIPTION:");
+               ical.append (lift.description.replace ("\n", "\\n"));
+               ical.append ("\nEND:VEVENT\nEND:VCALENDAR\n");
+
+               ics_filename = Path.build_path ("/", Environment.get_tmp_dir (),
+                                               "beifahrer.ics");
+               try {
+                       FileUtils.set_contents (ics_filename, ical.str);
+               } catch (FileError e) {
+                       stderr.printf ("Failed to write %s: %s\n", ics_filename, e.message);
+                       return;
+               }
+
+               var error = DBus.RawError ();
+               var bus = DBus.RawBus.get (DBus.BusType.SESSION, ref error);
+               var res = HildonMime.open_file (bus, "file://" + ics_filename);
+       }
+
+       void on_destroy () {
+               if (ics_filename != null)
+                       FileUtils.remove (ics_filename);
+       }
+
        void on_button_phone_clicked () {
                var uri = "tel://";
                if (lift.cell != null)