[gnome-contacts] Implement type editing



commit 2b0c3e7128afcee8ada10f69e9933f7b3611e18c
Author: Alexander Larsson <alexl redhat com>
Date:   Thu Jun 23 19:47:21 2011 +0200

    Implement type editing

 src/contacts-contact-pane.vala |   28 +++++++++++++-
 src/contacts-types.vala        |   80 ++++++++++++++++++++++++++++++++++-----
 2 files changed, 96 insertions(+), 12 deletions(-)
---
diff --git a/src/contacts-contact-pane.vala b/src/contacts-contact-pane.vala
index 7ee2113..1dfd06c 100644
--- a/src/contacts-contact-pane.vala
+++ b/src/contacts-contact-pane.vala
@@ -224,7 +224,29 @@ public class Contacts.ContactPane : EventBox {
     return image_frame;
   }
 
-  private void update_edit_detail_value (Set<FieldDetails> detail_set, 
+  private void update_edit_detail_type (Set<FieldDetails> detail_set,
+					TypeCombo combo,
+					string property_name) {
+    FieldDetails? old_detail = null;
+    foreach (var detail in detail_set) {
+      if (detail.get_data<TypeCombo> ("combo") == combo) {
+	old_detail = detail;
+	break;
+      }
+    }
+    assert (old_detail != null);
+
+    var new_detail = combo.update_details (old_detail);
+    new_detail.set_data ("entry", old_detail.get_data<Entry> ("entry"));
+    new_detail.set_data ("combo", old_detail.get_data<TypeCombo> ("combo"));
+
+    detail_set.remove (old_detail);
+    detail_set.add (new_detail);
+      
+    editing_persona.set (property_name, detail_set);
+  }
+
+  private void update_edit_detail_value (Set<FieldDetails> detail_set,
 					 Entry entry,
 					 string property_name) {
     FieldDetails? old_detail = null;
@@ -265,6 +287,10 @@ public class Contacts.ContactPane : EventBox {
 	update_edit_detail_value (detail_set, entry, property_name);
 	return false;
       });
+
+    combo.changed.connect ( () => {
+	update_edit_detail_type (detail_set, combo, property_name);
+      });
   }
   private void update_edit_details (Frame image_frame, Persona persona) {
     layout.reset (false);
diff --git a/src/contacts-types.vala b/src/contacts-types.vala
index 4bb6624..67710f2 100644
--- a/src/contacts-types.vala
+++ b/src/contacts-types.vala
@@ -22,6 +22,7 @@ using Gee;
 using Folks;
 
 public class Contacts.TypeSet : Object  {
+  const string X_GOOGLE_LABEL = "x-google-label";
   const int MAX_TYPES = 3;
   private struct InitData {
     unowned string display_name_u;
@@ -172,8 +173,8 @@ public class Contacts.TypeSet : Object  {
 
   // Looks up (and creates if necessary) the type in the store
   public void lookup_type (FieldDetails detail, out TreeIter iter) {
-    if (detail.parameters.contains ("x-google-label")) {
-      var label = Utils.get_first<string> (detail.parameters.get ("x-google-label"));
+    if (detail.parameters.contains (X_GOOGLE_LABEL)) {
+      var label = Utils.get_first<string> (detail.parameters.get (X_GOOGLE_LABEL));
       add_custom_label (label, out iter);
       return;
     }
@@ -192,8 +193,8 @@ public class Contacts.TypeSet : Object  {
   }
 
   public string format_type (FieldDetails detail) {
-    if (detail.parameters.contains ("x-google-label")) {
-      return Utils.get_first<string> (detail.parameters.get ("x-google-label"));
+    if (detail.parameters.contains (X_GOOGLE_LABEL)) {
+      return Utils.get_first<string> (detail.parameters.get (X_GOOGLE_LABEL));
     }
 
     unowned Data? d = lookup_data (detail);
@@ -204,8 +205,48 @@ public class Contacts.TypeSet : Object  {
     return _("Other");
   }
 
+  public FieldDetails update_details (FieldDetails old_details, TreeIter iter) {
+    FieldDetails details = new FieldDetails(old_details.value);
+    bool has_pref = false;
+    foreach (var value in old_details.parameters.get ("type")) {
+      if (value.ascii_casecmp ("PREF") == 0) {
+	has_pref = true;
+	break;
+      }
+    }
+    foreach (var param in old_details.parameters.get_keys()) {
+      if (param != "type" && param != X_GOOGLE_LABEL) {
+	foreach (var value in old_details.parameters.get (param)) {
+	  details.parameters.set (param, value);
+	}
+      }
+    }
+    
+    Data data;
+    string display_name;
+    store.get (iter, 0, out display_name, 1, out data);
+    
+    assert (display_name != null); // Not separator
+    assert (data != custom_dummy); // Not custom...
+    
+    if (data == null) { // A custom label
+      details.parameters.set ("type", "OTHER");
+      details.parameters.set (X_GOOGLE_LABEL, display_name);
+    } else {
+      InitData *init_data = data.init_data.data;
+      for (int j = 0; j < MAX_TYPES && init_data.types[j] != null; j++) {
+	details.parameters.set ("type", init_data.types[j]);
+      }
+    }
+
+    if (has_pref)
+      details.parameters.set ("type", "PREF");
+    
+    return details;
+  }
+
   public bool is_custom (TreeIter iter) {
-    InitData *data;
+    Data data;
     store.get (iter, 1, out data);
     return data == custom_dummy;
   }
@@ -282,6 +323,9 @@ public class Contacts.TypeCombo : Grid  {
   ComboBox combo;
   Entry entry;
   bool custom_mode;
+  bool in_manual_change;
+
+  public signal void changed ();
 
   public TypeCombo (TypeSet type_set) {
     this.type_set = type_set;
@@ -340,19 +384,33 @@ public class Contacts.TypeCombo : Grid  {
   }
 
   private void combo_changed (ComboBox combo) {
+    if (in_manual_change)
+      return;
+
     TreeIter iter;
-    if (combo.get_active_iter (out iter) &&
-	type_set.is_custom (iter)) {
-      custom_mode = true;
-      combo.hide ();
-      entry.show ();
-      entry.grab_focus ();
+    if (combo.get_active_iter (out iter)) {
+      if (type_set.is_custom (iter)) {
+	custom_mode = true;
+	combo.hide ();
+	entry.show ();
+	entry.grab_focus ();
+      } else {
+	this.changed ();
+      }
     }
   }
 
   public void set_active (FieldDetails details) {
     TreeIter iter;
     type_set.lookup_type (details, out iter);
+    in_manual_change = true;
     combo.set_active_iter (iter);
+    in_manual_change = false;
+  }
+
+  public FieldDetails update_details (FieldDetails old_details) {
+    TreeIter iter;
+    combo.get_active_iter (out iter);
+    return type_set.update_details (old_details, iter);
   }
 }



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