summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/contacts-contact-editor.vala196
-rw-r--r--src/contacts-contact-pane.vala68
2 files changed, 126 insertions, 138 deletions
diff --git a/src/contacts-contact-editor.vala b/src/contacts-contact-editor.vala
index a311525..8e224e6 100644
--- a/src/contacts-contact-editor.vala
+++ b/src/contacts-contact-editor.vala
@@ -1,4 +1,3 @@
-/* -*- Mode: vala; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 8 -*- */
/*
* Copyright (C) 2011 Alexander Larsson <alexl@redhat.com>
*
@@ -60,6 +59,9 @@ public class Contacts.AddressEditor : Box {
}
}
+/**
+ * A widget that allows the user to edit a given {@link Contact}.
+ */
[GtkTemplate (ui = "/org/gnome/Contacts/ui/contacts-contact-editor.ui")]
public class Contacts.ContactEditor : Grid {
@@ -108,7 +110,7 @@ public class Contacts.ContactEditor : Grid {
private int last_row;
/* the key of the hash_map is the uid of the persona */
- private HashMap<string, HashMap<string, Field?> > writable_personas;
+ private HashMap<string, HashMap<string, Field?>> writable_personas;
public bool has_birthday_row {
get; private set; default = false;
@@ -122,6 +124,90 @@ public class Contacts.ContactEditor : Grid {
get; private set; default = false;
}
+ construct {
+ this.writable_personas = new HashMap<string, HashMap<string, Field?>> ();
+
+ this.container_grid.set_focus_vadjustment (this.main_sw.get_vadjustment ());
+
+ this.main_sw.get_style_context ().add_class ("contacts-main-view");
+ this.main_sw.get_style_context ().add_class ("view");
+ }
+
+ public ContactEditor (Contact? contact, Store store, GLib.ActionGroup editor_actions) {
+ this.store = store;
+ this.contact = contact;
+
+ this.add_detail_button.get_popover ().insert_action_group ("edit", editor_actions);
+
+ if (contact != null) {
+ this.remove_button.sensitive = contact.can_remove_personas ();
+ this.linked_button.sensitive = contact.individual.personas.size > 1;
+ } else {
+ this.remove_button.hide ();
+ this.linked_button.hide ();
+ }
+
+ create_avatar_button ();
+ create_name_entry ();
+
+ if (contact != null)
+ fill_in_contact ();
+ else
+ fill_in_empty ();
+
+ show_all ();
+ }
+
+ private void fill_in_contact () {
+ int i = 3;
+ int last_store_position = 0;
+ bool is_first_persona = true;
+
+ var personas = this.contact.get_personas_for_display ();
+ foreach (var p in personas) {
+ if (!is_first_persona) {
+ var store_name = new Label("");
+ store_name.set_markup (Markup.printf_escaped ("<span font='16px bold'>%s</span>",
+ Contact.format_persona_store_name_for_contact (p)));
+ store_name.set_halign (Align.START);
+ store_name.xalign = 0.0f;
+ store_name.margin_start = 6;
+ container_grid.attach (store_name, 0, i, 2, 1);
+ last_store_position = ++i;
+ }
+
+ var rw_props = Contact.sort_persona_properties (p.writeable_properties);
+ if (rw_props.length != 0) {
+ this.writable_personas[p.uid] = new HashMap<string, Field?> ();
+ foreach (var prop in rw_props)
+ add_edit_row (p, prop, ref i);
+ }
+
+ if (is_first_persona)
+ this.last_row = i - 1;
+
+ if (i != 3)
+ is_first_persona = false;
+
+ if (i == last_store_position) {
+ i--;
+ this.container_grid.get_child_at (0, i).destroy ();
+ }
+ }
+ }
+
+ 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 ());
+ }
+
+ this.focus_widget = this.name_entry;
+ }
+
Value get_value_from_emails (HashMap<int, RowData?> rows) {
var new_details = new HashSet<EmailFieldDetails>();
@@ -763,112 +849,6 @@ public class Contacts.ContactEditor : Grid {
}
}
- public ContactEditor (Store store, SimpleActionGroup editor_actions) {
- this.store = store;
-
- this.container_grid.set_focus_vadjustment (this.main_sw.get_vadjustment ());
-
- this.main_sw.get_style_context ().add_class ("contacts-main-view");
- this.main_sw.get_style_context ().add_class ("view");
-
- this.add_detail_button.get_popover ().insert_action_group ("edit", editor_actions);
-
- this.writable_personas = new HashMap<string, HashMap<string, Field?>> ();
- }
-
- /**
- * Adjusts the ContactEditor to the given contact.
- * Use clear() to make sure nothing is lingering from the previous one.
- */
- public void edit (Contact c) {
- contact = c;
-
- remove_button.show ();
- remove_button.sensitive = contact.can_remove_personas ();
- linked_button.show ();
- linked_button.sensitive = contact.individual.personas.size > 1;
-
- create_avatar_button ();
- create_name_entry ();
-
- int i = 3;
- int last_store_position = 0;
- bool is_first_persona = true;
-
- var personas = c.get_personas_for_display ();
- foreach (var p in personas) {
- if (!is_first_persona) {
- var store_name = new Label("");
- store_name.set_markup (Markup.printf_escaped ("<span font='16px bold'>%s</span>",
- Contact.format_persona_store_name_for_contact (p)));
- store_name.set_halign (Align.START);
- store_name.xalign = 0.0f;
- store_name.margin_start = 6;
- container_grid.attach (store_name, 0, i, 2, 1);
- last_store_position = ++i;
- }
-
- var rw_props = Contact.sort_persona_properties (p.writeable_properties);
- if (rw_props.length != 0) {
- writable_personas.set (p.uid, new HashMap<string, Field?> ());
- foreach (var prop in rw_props) {
- add_edit_row (p, prop, ref i);
- }
- }
-
- if (is_first_persona) {
- last_row = i - 1;
- }
-
- if (i != 3) {
- is_first_persona = false;
- }
-
- if (i == last_store_position) {
- i--;
- container_grid.get_child_at (0, i).destroy ();
- }
- }
- }
-
- /**
- * Adjusts the ContactEditor for a new contact.
- * Use clear() to make sure nothing is lingering from the previous one.
- */
- public void set_new_contact () {
- remove_button.hide ();
- linked_button.hide ();
-
- create_avatar_button ();
- create_name_entry ();
- this.last_row = 2;
-
- 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 ());
- }
-
- this.focus_widget = this.name_entry;
- }
-
- public void clear () {
- foreach (var w in container_grid.get_children ()) {
- w.destroy ();
- }
-
- remove_button.set_sensitive (false);
- linked_button.set_sensitive (false);
-
- /* clean metadata as well */
- has_birthday_row = false;
- has_nickname_row = false;
- has_notes_row = false;
-
- writable_personas.clear ();
- contact = null;
- }
-
public HashMap<string, PropertyData?> properties_changed () {
var props_set = new HashMap<string, PropertyData?> ();
diff --git a/src/contacts-contact-pane.vala b/src/contacts-contact-pane.vala
index e2ee36e..ffb5d62 100644
--- a/src/contacts-contact-pane.vala
+++ b/src/contacts-contact-pane.vala
@@ -46,7 +46,7 @@ public class Contacts.ContactPane : Stack {
[GtkChild]
private Box contact_editor_page;
- private ContactEditor editor;
+ private ContactEditor? editor = null;
private SimpleActionGroup edit_contact_actions = new SimpleActionGroup ();
private const GLib.ActionEntry[] action_entries = {
@@ -80,25 +80,6 @@ public class Contacts.ContactPane : Stack {
this.store = contacts_store;
this.edit_contact_actions.add_action_entries (action_entries, this);
-
- // Contact editor
- this.editor = new ContactEditor (this.store, this.edit_contact_actions);
- this.editor.linked_button.clicked.connect (linked_accounts);
- this.editor.remove_button.clicked.connect (delete_contact);
- this.contact_editor_page.add (this.editor);
-
- /* enable/disable actions*/
- var birthday_action = this.edit_contact_actions.lookup_action ("add.birthday") as SimpleAction;
- this.editor.bind_property ("has-birthday-row", birthday_action, "enabled",
- BindingFlags.SYNC_CREATE | BindingFlags.INVERT_BOOLEAN);
-
- var nickname_action = this.edit_contact_actions.lookup_action ("add.nickname") as SimpleAction;
- this.editor.bind_property ("has-nickname-row", nickname_action, "enabled",
- BindingFlags.SYNC_CREATE | BindingFlags.INVERT_BOOLEAN);
-
- var notes_action = this.edit_contact_actions.lookup_action ("add.notes") as SimpleAction;
- this.editor.bind_property ("has-notes-row", notes_action, "enabled",
- BindingFlags.SYNC_CREATE | BindingFlags.INVERT_BOOLEAN);
}
public void add_suggestion (Contact c) {
@@ -169,6 +150,38 @@ public class Contacts.ContactPane : Stack {
this.sheet = null;
}
+ private void create_contact_editor () {
+ if (this.editor != null)
+ remove_contact_editor ();
+
+ this.editor = new ContactEditor (this.contact, this.store, this.edit_contact_actions);
+ this.editor.linked_button.clicked.connect (linked_accounts);
+ this.editor.remove_button.clicked.connect (delete_contact);
+
+ /* enable/disable actions*/
+ var birthday_action = this.edit_contact_actions.lookup_action ("add.birthday") as SimpleAction;
+ this.editor.bind_property ("has-birthday-row", birthday_action, "enabled",
+ BindingFlags.SYNC_CREATE | BindingFlags.INVERT_BOOLEAN);
+
+ var nickname_action = this.edit_contact_actions.lookup_action ("add.nickname") as SimpleAction;
+ this.editor.bind_property ("has-nickname-row", nickname_action, "enabled",
+ BindingFlags.SYNC_CREATE | BindingFlags.INVERT_BOOLEAN);
+
+ var notes_action = this.edit_contact_actions.lookup_action ("add.notes") as SimpleAction;
+ this.editor.bind_property ("has-notes-row", notes_action, "enabled",
+ BindingFlags.SYNC_CREATE | BindingFlags.INVERT_BOOLEAN);
+
+ this.contact_editor_page.add (this.editor);
+ }
+
+ private void remove_contact_editor () {
+ if (this.editor == null)
+ return;
+
+ this.contact_editor_page.remove (this.editor);
+ this.editor = null;
+ }
+
void on_add_detail (GLib.SimpleAction action, GLib.Variant? parameter) {
var tok = action.name.split (".");
@@ -203,10 +216,7 @@ public class Contacts.ContactPane : Stack {
this.on_edit_mode = true;
remove_contact_sheet ();
-
- this.editor.clear ();
- this.editor.edit (this.contact);
- this.editor.show_all ();
+ create_contact_editor ();
set_visible_child (this.contact_editor_page);
}
@@ -219,7 +229,7 @@ public class Contacts.ContactPane : Stack {
if (!drop_changes)
save_editor_changes.begin ();
- this.editor.clear ();
+ remove_contact_editor ();
if (this.contact != null)
show_contact_sheet ();
@@ -257,12 +267,10 @@ public class Contacts.ContactPane : Stack {
}
public void new_contact () {
- on_edit_mode = true;
-
+ this.on_edit_mode = true;
+ this.contact = null;
remove_contact_sheet ();
-
- editor.set_new_contact ();
-
+ create_contact_editor ();
set_visible_child (this.contact_editor_page);
}