[gnome-contacts/new-design] Support actually creating new contacts with the new contact dialog



commit 7bec500650317cda021d2f19f36738b9a64b6eec
Author: Alexander Larsson <alexl redhat com>
Date:   Fri Dec 16 18:00:42 2011 +0100

    Support actually creating new contacts with the new contact dialog

 src/contacts-app.vala                |    7 ++-
 src/contacts-new-contact-dialog.vala |  150 +++++++++++++++++++++++++++++++++-
 2 files changed, 155 insertions(+), 2 deletions(-)
---
diff --git a/src/contacts-app.vala b/src/contacts-app.vala
index cbb6775..f004997 100644
--- a/src/contacts-app.vala
+++ b/src/contacts-app.vala
@@ -56,6 +56,11 @@ public class Contacts.App : Gtk.Application {
     contacts_pane.show_contact (new_selection);
   }
 
+  public void show_contact (Contact? contact) {
+    list_pane.select_contact (contact);
+    contacts_pane.show_contact (contact);
+  }
+
   public async void show_individual (string id) {
     var contact = yield contacts_store.find_contact ( (c) => {
 	return c.individual.id == id;
@@ -117,7 +122,7 @@ public class Contacts.App : Gtk.Application {
     list_pane = new ListPane (contacts_store);
     list_pane.selection_changed.connect (selection_changed);
     list_pane.create_new.connect ( () => {
-	var dialog = new NewContactDialog (window);
+	var dialog = new NewContactDialog (contacts_store, window);
 	dialog.show_all ();
       });
 
diff --git a/src/contacts-new-contact-dialog.vala b/src/contacts-new-contact-dialog.vala
index 348166f..2825bec 100644
--- a/src/contacts-new-contact-dialog.vala
+++ b/src/contacts-new-contact-dialog.vala
@@ -21,6 +21,7 @@ using Folks;
 using Gee;
 
 public class Contacts.NewContactDialog : Dialog {
+  Store contacts_store;
   Grid grid;
   Entry name_entry;
   ArrayList<Entry> email_entries;
@@ -30,8 +31,9 @@ public class Contacts.NewContactDialog : Dialog {
   ArrayList<Grid> address_entries;
   ArrayList<TypeCombo> address_combos;
 
-  public NewContactDialog(Window parent) {
+  public NewContactDialog(Store contacts_store, Window? parent) {
     set_title (_("New contact"));
+    this.contacts_store = contacts_store;
     set_destroy_with_parent (true);
     set_transient_for (parent);
 
@@ -212,7 +214,153 @@ public class Contacts.NewContactDialog : Dialog {
 	  });
 	return;
       }
+
+      var details = get_details ();
+      create_persona (details);
     }
+
     this.destroy ();
   }
+
+  public HashTable<string, Value?> get_details () {
+    var details = new HashTable<string, Value?> (str_hash, str_equal);
+
+    var v = Value (typeof (string));
+    v.set_string (name_entry.get_text ());
+    details.set ("full-name", v);
+
+    var email_details = new HashSet<EmailFieldDetails>();
+    for (int i = 0; i < email_entries.size; i++) {
+      Entry entry = email_entries[i];
+      TypeCombo combo = email_combos[i];
+
+      if (entry.get_text () == "")
+	continue;
+
+      var d = new EmailFieldDetails(entry.get_text ());
+      combo.update_details (d);
+
+      email_details.add (d);
+    }
+
+    if (!email_details.is_empty) {
+      v = Value(email_details.get_type ());
+      v.set_object (email_details);
+      details.set ("email-addresses", v);
+    }
+
+    var phone_details = new HashSet<PhoneFieldDetails>();
+    for (int i = 0; i < phone_entries.size; i++) {
+      Entry entry = phone_entries[i];
+      TypeCombo combo = phone_combos[i];
+
+      if (entry.get_text () == "")
+	continue;
+
+      var d = new PhoneFieldDetails(entry.get_text ());
+      combo.update_details (d);
+
+      phone_details.add (d);
+    }
+
+    if (!phone_details.is_empty) {
+      v = Value(phone_details.get_type ());
+      v.set_object (phone_details);
+      details.set ("phone-numbers", v);
+    }
+
+    var address_details = new HashSet<PostalAddressFieldDetails>();
+    for (int i = 0; i < address_entries.size; i++) {
+      Grid a_grid = address_entries[i];
+      TypeCombo combo = address_combos[i];
+
+      Entry? entries[8];
+
+      bool all_empty = true;
+      for (int j = 0; j < Contact.postal_element_props.length; j++) {
+	entries[j] = (Entry) a_grid.get_child_at (0, j);
+	if (entries[j].get_text () != "")
+	  all_empty  = false;
+      }
+
+      if (all_empty)
+	continue;
+
+      var p = new PostalAddress (null, null, null, null,
+				 null, null, null, null,
+				 null);
+      for (int j = 0; j < Contact.postal_element_props.length; j++)
+	p.set (Contact.postal_element_props[j], entries[j].get_text ());
+      var d = new PostalAddressFieldDetails(p, null);
+      combo.update_details (d);
+
+      address_details.add (d);
+    }
+
+    if (!address_details.is_empty) {
+      v = Value(address_details.get_type ());
+      v.set_object (address_details);
+      details.set ("postal-addresses", v);
+    }
+
+
+    return details;
+  }
+
+  public void create_persona (HashTable<string, Value?> details) {
+    if (contacts_store.aggregator.primary_store == null) {
+      Dialog dialog = new MessageDialog (this,
+					 DialogFlags.DESTROY_WITH_PARENT |
+					 DialogFlags.MODAL,
+					 MessageType.ERROR,
+					 ButtonsType.OK,
+					 _("No primary addressbook configured\n"));
+      dialog.show ();
+      dialog.response.connect ( () => {
+	  dialog.destroy ();
+	});
+      return;
+    }
+
+    contacts_store.aggregator.primary_store.add_persona_from_details.begin (details, (obj, res) => {
+	var store = obj as PersonaStore;
+	Persona? persona = null;
+	Dialog dialog = null;
+
+	try {
+	  persona = store.add_persona_from_details.end (res);
+	} catch (Error e) {
+	  dialog = new MessageDialog (this.get_toplevel () as Window,
+				      DialogFlags.DESTROY_WITH_PARENT |
+				      DialogFlags.MODAL,
+				      MessageType.ERROR,
+				      ButtonsType.OK,
+				      _("Unable to create new contacts: %s\n"), e.message);
+	}
+
+	var contact = contacts_store.find_contact_with_persona (persona);
+	if (contact == null) {
+	  dialog = new MessageDialog (this.get_toplevel () as Window,
+				      DialogFlags.DESTROY_WITH_PARENT |
+				      DialogFlags.MODAL,
+				      MessageType.ERROR,
+				      ButtonsType.OK,
+				      "%s",
+				      _("Unable to find newly created contact\n"));
+	}
+
+	if (dialog != null) {
+	  dialog.show ();
+	  dialog.response.connect ( () => {
+	      dialog.destroy ();
+	    });
+
+	  return;
+	}
+
+	App.app.show_contact (contact);
+      });
+
+
+  }
 }



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