[gnome-contacts/wip/mjog/extended-actions: 2/2] App: Add create-contact action



commit 82c8a98e0bd7142f0ae8d9421e5dadf593a01926
Author: Michael Gratton <mike vee net>
Date:   Sun Mar 17 00:09:26 2019 +1100

    App: Add create-contact action
    
    Like the existing new-contact action, but provides a way for the caller
    to prefil values and let people edit the contact before actually adding
    it to the default store.

 data/ui/contacts-window.ui       |  2 +-
 src/contacts-app.vala            | 21 ++++++++++++++++
 src/contacts-contact-editor.vala | 52 +++++++++++++++++++++++++++++++++-------
 src/contacts-contact-pane.vala   |  8 +++----
 src/contacts-window.vala         | 10 +++++---
 5 files changed, 76 insertions(+), 17 deletions(-)
---
diff --git a/data/ui/contacts-window.ui b/data/ui/contacts-window.ui
index 597b5c8..48dacc7 100644
--- a/data/ui/contacts-window.ui
+++ b/data/ui/contacts-window.ui
@@ -128,7 +128,7 @@
                     <property name="halign">center</property>
                     <property name="valign">center</property>
                     <property name="tooltip_text" translatable="yes">Create new contact</property>
-                    <signal name="clicked" handler="new_contact" object="ContactsWindow" after="no" 
swapped="no"/>
+                    <signal name="clicked" handler="on_new_contact" object="ContactsWindow" after="no" 
swapped="no"/>
                     <child internal-child="accessible">
                       <object class="AtkObject" id="add_button_atkobject">
                         <property name="AtkObject::accessible-name" translatable="yes">Add contact</property>
diff --git a/src/contacts-app.vala b/src/contacts-app.vala
index 46182e8..d9018be 100644
--- a/src/contacts-app.vala
+++ b/src/contacts-app.vala
@@ -35,6 +35,7 @@ public class Contacts.App : Gtk.Application {
     { "change-book",     change_address_book },
     { "online-accounts", online_accounts     },
     { "new-contact",     new_contact         },
+    { "create-contact",  on_create_contact, "a(ss)" },
     { "show-contact",    on_show_contact,   "s"     }
   };
 
@@ -386,6 +387,26 @@ public class Contacts.App : Gtk.Application {
   public void new_contact () {
     window.new_contact ();
   }
+
+  private void on_create_contact(SimpleAction action, Variant? param) {
+    Gee.MultiMap<string,string>? values = null;
+    if (param != null) {
+      values = new Gee.HashMultiMap<string,string>();
+      for (int i = 0; i < param.n_children(); i++) {
+        Variant entry = param.get_child_value(i);
+        string? key = entry.get_child_value(0) as string;
+        string? value = entry.get_child_value(1) as string;
+        if (key != null && value != null) {
+          values.set(key, value);
+        }
+      }
+    }
+    if (window != null) {
+      window.present ();
+      window.new_contact (values);
+    }
+  }
+
   private void on_show_contact(SimpleAction action, Variant? param) {
     var individual = param as string;
     if (window != null)
diff --git a/src/contacts-contact-editor.vala b/src/contacts-contact-editor.vala
index 3398bf0..7de44c7 100644
--- a/src/contacts-contact-editor.vala
+++ b/src/contacts-contact-editor.vala
@@ -120,7 +120,7 @@ public class Contacts.ContactEditor : ContactForm {
     this.container_grid.size_allocate.connect(on_container_grid_size_allocate);
   }
 
-  public ContactEditor (Contact? contact, Store store, GLib.ActionGroup editor_actions) {
+  public ContactEditor (Contact? contact, Store store, GLib.ActionGroup editor_actions, 
MultiMap<string,string>? values = null) {
     this.store = store;
     this.contact = contact;
 
@@ -135,10 +135,24 @@ public class Contacts.ContactEditor : ContactForm {
     }
 
     create_avatar_button ();
-    create_name_entry ();
+
+    string name = "";
+    if (contact != null)
+      name = contact.individual.display_name;
+    else if (values != null) {
+      string key = PersonaStore.detail_key(PersonaDetail.FULL_NAME);
+      Collection<string>? names = values.get(key);
+      if (names != null && !names.is_empty) {
+        name = Utils.get_first(names);
+      }
+      values.remove_all(key);
+    }
+    create_name_entry (name);
 
     if (contact != null)
       fill_in_contact ();
+    else if (values != null)
+      fill_in_values (values);
     else
       fill_in_empty ();
 
@@ -177,13 +191,30 @@ public class Contacts.ContactEditor : ContactForm {
     }
   }
 
+  private void fill_in_values (MultiMap<string,string>? values = null) {
+    this.last_row = 2;
+
+    int row = 3;
+    this.writable_personas["null-persona.hack"] = new HashMap<string, Field?> ();
+    foreach (string key in values.get_keys()) {
+      switch (key) {
+      case "email-addresses":
+        foreach (string value in values.get(key))
+          add_edit_row (null, key, ref row, false, null, value);
+        break;
+      }
+    }
+
+    this.focus_widget = this.name_entry;
+  }
+
   private void fill_in_empty () {
     this.last_row = 2;
 
     this.writable_personas["null-persona.hack"] = new HashMap<string, Field?> ();
     foreach (var prop in DEFAULT_PROPS_NEW_CONTACT) {
       var tok = prop.split (".");
-      add_new_row_for_property (null, tok[0], tok[1].up ());
+      add_new_row_for_property (null, tok[0], null, tok[1].up ());
     }
 
     this.focus_widget = this.name_entry;
@@ -572,7 +603,7 @@ public class Contacts.ContactEditor : ContactForm {
     focus_widget = value_address;
   }
 
-  void add_edit_row (Persona? p, string prop_name, ref int row, bool add_empty = false, string? type = null) 
{
+  void add_edit_row (Persona? p, string prop_name, ref int row, bool add_empty = false, string? type = null, 
string? value = null) {
     /* Here, we will need to add manually every type of field,
      * we're planning to allow editing on */
     string persona_uid = p != null ? p.uid : "null-persona.hack";
@@ -584,6 +615,11 @@ public class Contacts.ContactEditor : ContactForm {
        attach_row_with_entry (row, TypeSet.email, detail_field, "", type);
        rows.set (row, { detail_field });
        row++;
+      } else if (value != null){
+       var detail_field = new EmailFieldDetails ("");
+       attach_row_with_entry (row, TypeSet.email, detail_field, value, type);
+       rows.set (row, { detail_field });
+       row++;
       } else {
        var details = p as EmailDetails;
        if (details != null) {
@@ -872,7 +908,7 @@ public class Contacts.ContactEditor : ContactForm {
     return props_set;
   }
 
-  public void add_new_row_for_property (Persona? p, string prop_name, string? type = null) {
+  public void add_new_row_for_property (Persona? p, string prop_name, string? value, string? type = null) {
     /* Somehow, I need to ensure that p is the main/default/first persona */
     Persona persona = null;
     if (contact != null) {
@@ -944,15 +980,13 @@ public class Contacts.ContactEditor : ContactForm {
   }
 
   // Creates the big name entry on the top
-  private void create_name_entry () {
+  private void create_name_entry (string name) {
     this.name_entry = new Entry ();
     this.name_entry.hexpand = true;
     this.name_entry.valign = Align.CENTER;
     this.name_entry.placeholder_text = _("Add name");
     this.name_entry.set_data ("changed", false);
-
-    if (this.contact != null)
-        this.name_entry.text = this.contact.individual.display_name;
+    this.name_entry.text = name;
 
     /* structured name change */
     this.name_entry.changed.connect (() => {
diff --git a/src/contacts-contact-pane.vala b/src/contacts-contact-pane.vala
index 1fabeff..280f50e 100644
--- a/src/contacts-contact-pane.vala
+++ b/src/contacts-contact-pane.vala
@@ -147,11 +147,11 @@ public class Contacts.ContactPane : Stack {
     this.sheet = null;
   }
 
-  private void create_contact_editor () {
+  private void create_contact_editor (Gee.MultiMap<string,string>? values = null) {
     if (this.editor != null)
       remove_contact_editor ();
 
-    this.editor = new ContactEditor (this.contact, this.store, this.edit_contact_actions);
+    this.editor = new ContactEditor (this.contact, this.store, this.edit_contact_actions, values);
     this.editor.linked_button.clicked.connect (linked_accounts);
     this.editor.remove_button.clicked.connect (delete_contact);
 
@@ -263,11 +263,11 @@ public class Contacts.ContactPane : Stack {
     }
   }
 
-  public void new_contact () {
+  public void new_contact (MultiMap<string,string>? values = null) {
     this.on_edit_mode = true;
     this.contact = null;
     remove_contact_sheet ();
-    create_contact_editor ();
+    create_contact_editor (values);
     set_visible_child (this.contact_editor_page);
   }
 
diff --git a/src/contacts-window.vala b/src/contacts-window.vala
index 738228d..0b88e83 100644
--- a/src/contacts-window.vala
+++ b/src/contacts-window.vala
@@ -311,8 +311,7 @@ public class Contacts.Window : Gtk.ApplicationWindow {
     }
   }
 
-  [GtkCallback]
-  public void new_contact () {
+  public void new_contact (Gee.MultiMap<string,string>? values = null) {
     if (this.state == UiState.UPDATING || this.state == UiState.CREATING)
       return;
 
@@ -322,10 +321,15 @@ public class Contacts.Window : Gtk.ApplicationWindow {
 
     this.right_header.title = _("New Contact");
 
-    this.contact_pane.new_contact ();
+    this.contact_pane.new_contact (values);
     show_contact_pane ();
   }
 
+  [GtkCallback]
+  public void on_new_contact () {
+    new_contact ();
+  }
+
   [GtkCallback]
   private void on_cancel_visible () {
     update ();


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