[geary/wip/alt-contact-save-action] Workaround lack of way to save contacts via Contacts right now



commit 15c106a501a6cb514ead147c1344678b1431582f
Author: Michael Gratton <mike vee net>
Date:   Sun Jun 23 11:59:45 2019 +1000

    Workaround lack of way to save contacts via Contacts right now
    
    We want to propmt people to save contacts in the Contacts app by opening
    the contact in the app and prompting them to save it there, but
    functionality for this hasn't landed in Contacts yet. So for the moment,
    just save directly via Folks, then open in Contacts.
    
    See #133, GNOME/gnome-contacts!66

 src/client/application/application-contact.vala | 113 ++++++++++++++++++------
 1 file changed, 86 insertions(+), 27 deletions(-)
---
diff --git a/src/client/application/application-contact.vala b/src/client/application/application-contact.vala
index fa2389d5..2cd9a254 100644
--- a/src/client/application/application-contact.vala
+++ b/src/client/application/application-contact.vala
@@ -150,36 +150,95 @@ public class Application.Contact : Geary.BaseObject {
     /** Invokes the desktop contacts application to save this contact. */
     public async void save_to_desktop(GLib.Cancellable? cancellable)
         throws GLib.Error {
-        GLib.DBusConnection dbus = yield GLib.Bus.get(
-            GLib.BusType.SESSION, cancellable
-        );
-        GLib.DBusActionGroup contacts = DBusActionGroup.get(
-            dbus, "org.gnome.Contacts", "/org/gnome/Contacts"
-        );
+        Folks.Persona? persona = null;
+        ContactStore? contacts = this.store;
+        if (contacts != null) {
+            Folks.PersonaStore? personas = contacts.individuals.primary_store;
+            if (personas != null && personas.can_add_personas == TRUE) {
+                GLib.HashTable<string,GLib.Value?> details =
+                    new GLib.HashTable<string,GLib.Value?>(GLib.str_hash,
+                                                           GLib.str_equal);
+
+                GLib.Value name_value = GLib.Value(typeof(string));
+                name_value.set_string(this.display_name);
+                details.insert(
+                    Folks.PersonaStore.detail_key(
+                        Folks.PersonaDetail.FULL_NAME
+                    ),
+                    name_value
+                );
+
+                Gee.Set<Folks.EmailFieldDetails> email_addresses =
+                    new Gee.HashSet<Folks.EmailFieldDetails>();
+                GLib.Value email_value = GLib.Value(typeof(Gee.Set));
+                foreach (Geary.RFC822.MailboxAddress addr
+                         in this.email_addresses) {
+                    email_addresses.add(
+                        new Folks.EmailFieldDetails(addr.address)
+                    );
+                }
+                email_value.set_object(email_addresses);
+                details.insert(
+                    Folks.PersonaStore.detail_key(
+                        Folks.PersonaDetail.EMAIL_ADDRESSES
+                    ),
+                    email_value
+                );
 
-        GLib.Variant param = new GLib.Variant.array(
-            new GLib.VariantType("(ss)"),
-            new GLib.Variant[] {
-                new GLib.Variant.tuple(
-                    new GLib.Variant[] {
-                        Folks.PersonaStore.detail_key(
-                            Folks.PersonaDetail.FULL_NAME
-                        ),
-                        this.display_name ?? ""
-                    }
-                ),
-                new GLib.Variant.tuple(
-                    new GLib.Variant[] {
-                        Folks.PersonaStore.detail_key(
-                            Folks.PersonaDetail.EMAIL_ADDRESSES
-                        ),
-                        Geary.Collection.get_first(this.email_addresses).address
-                    }
-                )
+                persona = yield personas.add_persona_from_details(details);
             }
-        );
+        }
+        if (persona == null) {
+            throw new Geary.EngineError.UNSUPPORTED(
+                "Supported persona store not found"
+            );
+        }
+
+        Folks.Individual? individual = persona.individual;
+        if (individual == null) {
+            throw new Geary.EngineError.UNSUPPORTED(
+                "Individual not created for persona"
+            );
+        }
+
+        update_individual(individual);
+        update();
+        changed();
 
-        contacts.activate_action("create-contact", param);
+        open_on_desktop(cancellable);
+
+        // XXX Un-comment and use the section below instead of the
+        // code above when something has been done about
+        // https://gitlab.gnome.org/GNOME/gnome-contacts/merge_requests/66
+
+        // GLib.DBusConnection dbus = yield GLib.Bus.get(
+        // GLib.BusType.SESSION, cancellable ); GLib.DBusActionGroup
+        // contacts = DBusActionGroup.get( dbus, "org.gnome.Contacts",
+        // "/org/gnome/Contacts" );
+
+        // GLib.Variant param = new GLib.Variant.array(
+        //     new GLib.VariantType("(ss)"),
+        //     new GLib.Variant[] {
+        //         new GLib.Variant.tuple(
+        //             new GLib.Variant[] {
+        //                 Folks.PersonaStore.detail_key(
+        //                     Folks.PersonaDetail.FULL_NAME
+        //                 ),
+        //                 this.display_name ?? ""
+        //             }
+        //         ),
+        //         new GLib.Variant.tuple(
+        //             new GLib.Variant[] {
+        //                 Folks.PersonaStore.detail_key(
+        //                     Folks.PersonaDetail.EMAIL_ADDRESSES
+        //                 ),
+        //                 this.contact.email
+        //             }
+        //         )
+        //     }
+        // );
+
+        // contacts.activate_action("create-contact", param);
     }
 
     /** Invokes the desktop contacts application to open this contact. */


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