[california/wip/740088-invite] First stab at using default mail client to send invitation



commit da06d1959d48804bb9645b6691710440251195d6
Author: Jim Nelson <jim yorba org>
Date:   Tue Nov 18 16:30:44 2014 -0800

    First stab at using default mail client to send invitation
    
    Would like to embed invitation in the body as well as offer as an
    attachment, so this isn't perfect yet.

 src/component/component-icalendar.vala |    2 +-
 src/host/host-create-update-event.vala |   56 ++++++++++++++++++++++++++++++++
 2 files changed, 57 insertions(+), 1 deletions(-)
---
diff --git a/src/component/component-icalendar.vala b/src/component/component-icalendar.vala
index c629f8b..b02af1e 100644
--- a/src/component/component-icalendar.vala
+++ b/src/component/component-icalendar.vala
@@ -85,7 +85,7 @@ public class iCalendar : BaseObject {
      * later modifications will allow for Instances to be added and removed dynamically.
      */
     public iCalendar(iCal.icalproperty_method method, string? prodid, string? version, string? calscale,
-        Gee.List<Instance>? instances) {
+        Gee.Collection<Instance>? instances) {
         this.prodid = prodid;
         this.version = version;
         this.calscale = calscale;
diff --git a/src/host/host-create-update-event.vala b/src/host/host-create-update-event.vala
index dd38e62..902d0eb 100644
--- a/src/host/host-create-update-event.vala
+++ b/src/host/host-create-update-event.vala
@@ -338,6 +338,8 @@ public class CreateUpdateEvent : Gtk.Grid, Toolkit.Card {
         
         Toolkit.set_unbusy(this, cursor);
         
+        invite_attendees(target);
+        
         if (create_err == null)
             notify_success();
         else
@@ -389,12 +391,66 @@ public class CreateUpdateEvent : Gtk.Grid, Toolkit.Card {
         
         Toolkit.set_unbusy(this, cursor);
         
+        invite_attendees(target);
+        
         if (update_err == null)
             notify_success();
         else
             report_error(_("Unable to update event: %s").printf(update_err.message));
     }
     
+    private void invite_attendees(Component.Event event) {
+        Component.iCalendar ics = new Component.iCalendar(iCal.icalproperty_method.REQUEST,
+            Component.ICAL_PRODID, Component.ICAL_VERSION, null, event.attendees);
+        
+        string temporary_filename;
+        try {
+            int temporary_handle = FileUtils.open_tmp("california-invite-XXXXXX.ics",
+                out temporary_filename);
+            FileUtils.set_contents(temporary_filename, ics.source);
+            FileUtils.close(temporary_handle);
+            
+            // ensure this file is only readable by the user ... this needs to be done after the
+            // file is closed
+            FileUtils.chmod(temporary_filename, (int) (Posix.S_IRUSR | Posix.S_IWUSR));
+        } catch (Error err) {
+            Application.instance.error_message(deck.get_toplevel() as Gtk.Window,
+                _("Unable to export .ics: %s").printf(err.message));
+        }
+        
+        // build the mailto: for the default mail application
+        StringBuilder mailto = new StringBuilder("mailto:";);
+        mailto.append(traverse<Component.Person>(event.attendees)
+            .sort()
+            .to_string(attendee_to_uri)
+        );
+        
+        if (!String.is_empty(event.summary))
+            mailto.append_printf("?subject=%s", GLib.Uri.escape_string(_("Invitation: 
%s").printf(event.summary)));
+        else
+            mailto.append_printf("?subject=%s", GLib.Uri.escape_string(_("Invitation")));
+        
+        mailto.append_printf("&body=%s", GLib.Uri.escape_string(
+            _("Attached is an invitation to an event")
+        ));
+        
+        mailto.append_printf("&attachment=%s", GLib.Uri.escape_string(temporary_filename));
+        
+        debug("%s", mailto.str);
+        
+        try {
+            Gtk.show_uri(deck.get_screen(), mailto.str, Gdk.CURRENT_TIME);
+        } catch (Error err) {
+            Application.instance.error_message(deck.get_toplevel() as Gtk.Window,
+                _("Unable to launch mail client: %s").printf(err.message));
+        }
+    }
+    
+    private string? attendee_to_uri(Component.Person attendee, bool is_first, bool is_last) {
+        string to_mailbox = is_last ? attendee.mailbox : "%s,".printf(attendee.mailbox);
+        
+        return GLib.Uri.escape_string(to_mailbox, "@,");
+    }
 }
 
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]