Add SMS button to the lift detail window
[beifahrer] / src / lift-detail-window.vala
index 4b997eb..de6b8c8 100644 (file)
@@ -34,6 +34,7 @@ public class LiftDetailWindow : StackableWindow {
        OssoABook.Button button_calendar;
        Label label_driver;
        OssoABook.Button button_phone;
+       OssoABook.Button button_sms;
        Image image_smoke;
        Label label;
 
@@ -67,7 +68,7 @@ public class LiftDetailWindow : StackableWindow {
                button_route = new OssoABook.Button (SizeType.FINGER_HEIGHT);
                button_route.style = OssoABook.ButtonStyle.LABEL;
 
-               var table = new Table (2, 4, false);
+               var table = new Table (5, 2, false);
 
                button_calendar = new OssoABook.Button (SizeType.FINGER_HEIGHT);
                button_calendar.set_icon_name ("general_calendar");
@@ -83,6 +84,11 @@ public class LiftDetailWindow : StackableWindow {
                button_phone.set_icon_visible (true);
                button_phone.title = _("Phone");
 
+               button_sms = new OssoABook.Button (SizeType.FINGER_HEIGHT);
+               button_sms.set_icon_name ("general_sms");
+               button_sms.set_icon_visible (true);
+               button_sms.title = _("SMS");
+
                image_smoke = new Image ();
 
                label = new Label ("");
@@ -92,8 +98,9 @@ public class LiftDetailWindow : StackableWindow {
                table.attach (button_calendar, 1, 2, 0, 1, AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.FILL, 0, 0);
                table.attach (label_driver, 0, 1, 1, 2, AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.FILL, MARGIN_DEFAULT, 0);
                table.attach (button_phone, 1, 2, 1, 2, AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.FILL, 0, 0);
-               table.attach (image_smoke, 1, 2, 2, 3, AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.FILL, 0, 0);
-               table.attach (label, 0, 2, 3, 4, AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.FILL, MARGIN_DEFAULT, 0);
+               table.attach (button_sms, 1, 2, 2, 3, AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.FILL, 0, 0);
+               table.attach (image_smoke, 1, 2, 3, 4, AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.FILL, 0, 0);
+               table.attach (label, 0, 2, 4, 5, AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.FILL, MARGIN_DEFAULT, 0);
 
                vbox.pack_start (button_route, true, true, 0);
                vbox.pack_start (table, true, true, 0);
@@ -105,6 +112,7 @@ public class LiftDetailWindow : StackableWindow {
                add_contact.clicked.connect (on_add_contact_clicked);
                goto_website.clicked.connect (on_goto_website_clicked);
                button_phone.clicked.connect (on_button_phone_clicked);
+               button_sms.clicked.connect (on_button_sms_clicked);
        }
 
        private async void get_lift_details () {
@@ -131,10 +139,14 @@ public class LiftDetailWindow : StackableWindow {
                label_driver.set_text (_("Driver: ") + lift.name);
                if (lift.cell != null) {
                        button_phone.title = _("Cell");
-                       button_phone.value = E.normalize_phone_number (lift.cell);
+                       string cell_number = E.normalize_phone_number (lift.cell);
+                       button_phone.value = cell_number;
+                       button_sms.value = cell_number;
                } else if (lift.phone != null) {
                        button_phone.title = _("Phone");
-                       button_phone.value = E.normalize_phone_number (lift.phone);
+                       string phone_number = E.normalize_phone_number (lift.phone);
+                       button_phone.value = phone_number;
+                       button_sms.value = phone_number;
                }
 
                string lift_text = _("Free places: %d\n").printf (lift.places);
@@ -183,6 +195,27 @@ public class LiftDetailWindow : StackableWindow {
                }
        }
 
+       void on_button_sms_clicked () {
+               var uri = "sms://";
+               if (lift.cell != null)
+                       uri += E.normalize_phone_number (lift.cell);
+               else if (lift.phone != null)
+                       uri += E.normalize_phone_number (lift.phone);
+               try {
+                       var action = URIAction.get_default_action_by_uri (uri);
+                       if (action != null) {
+                               action.open (uri);
+                       } else {
+                               
+                               Banner.show_information (this, null, _("Couldn't open %s URI").printf (uri));
+                       }
+               } catch (Error e) {
+                       if (e is URIError) {
+                               Banner.show_information (this, null, _("Error: %s").printf (e.message));
+                       }
+               }
+       }
+
        void on_add_contact_clicked () {
                var contact = new OssoABook.Contact ();
                var attr_fn = new E.VCardAttribute (null, E.EVC_FN);