[evolution-data-server/meego-eds] Move the FILE_AS generation to a contact getter



commit d3ac11182b9cb0c8a88af94bb5d12717090d4bb4
Author: Christophe Dumez <christophe dumez intel com>
Date:   Wed Jun 22 14:00:43 2011 +0300

    Move the FILE_AS generation to a contact getter
    
    The vCard should be kept RFC compliant.

 addressbook/libebook/e-contact.c |   71 ++++++++++++++++++++++++++++++++++++--
 addressbook/libebook/e-vcard.c   |   47 -------------------------
 2 files changed, 68 insertions(+), 50 deletions(-)
---
diff --git a/addressbook/libebook/e-contact.c b/addressbook/libebook/e-contact.c
index 6a2b39d..a6d6675 100644
--- a/addressbook/libebook/e-contact.c
+++ b/addressbook/libebook/e-contact.c
@@ -88,6 +88,8 @@ static gpointer  fn_getter (EContact *contact, EVCardAttribute *attr);
 static void fn_setter (EContact *contact, EVCardAttribute *attr, gpointer data);
 static gpointer  n_getter (EContact *contact, EVCardAttribute *attr);
 static void n_setter (EContact *contact, EVCardAttribute *attr, gpointer data);
+static gpointer  fileas_getter (EContact *contact, EVCardAttribute *attr);
+static void fileas_setter (EContact *contact, EVCardAttribute *attr, gpointer data);
 static gpointer  adr_getter (EContact *contact, EVCardAttribute *attr);
 static void adr_setter (EContact *contact, EVCardAttribute *attr, gpointer data);
 static gpointer  date_getter (EContact *contact, EVCardAttribute *attr);
@@ -113,7 +115,9 @@ static void cert_setter (EContact *contact, EVCardAttribute *attr, gpointer data
 static const EContactFieldInfo field_info[] = {
 	{0,}, /* Dummy row as EContactField starts from 1 */
 	STRING_FIELD (E_CONTACT_UID,        EVC_UID,       "id",         N_("Unique ID"),  FALSE),
-	STRING_FIELD (E_CONTACT_FILE_AS,    EVC_X_FILE_AS, "file_as",    N_("File Under"),    FALSE),
+	/* FILE_AS is not really a structured field - we use a getter/setter
+           so we can generate its value if necessary in the getter */
+	GETSET_FIELD (E_CONTACT_FILE_AS,    EVC_X_FILE_AS, "file_as",    N_("File Under"),    FALSE, fileas_getter, fileas_setter),
 	/* URI of the book to which the contact belongs to */
 	STRING_FIELD (E_CONTACT_BOOK_URI, EVC_X_BOOK_URI, "book_uri", N_("Book URI"), FALSE),
 
@@ -541,6 +545,68 @@ fn_setter (EContact *contact, EVCardAttribute *attr, gpointer data)
 	}
 }
 
+static gpointer
+fileas_getter (EContact *contact, EVCardAttribute *attr)
+{
+	if (!attr) {
+		/* Generate a FILE_AS field */
+		EContactName *name;
+		gchar* new_file_as = NULL;
+
+		name = e_contact_get (contact, E_CONTACT_NAME);
+
+		/* Use name if available */
+		if (name) {
+			gchar *strings[3], **stringptr;
+
+			stringptr = strings;
+			if (name->family && *name->family)
+				*(stringptr++) = name->family;
+			if (name->given && *name->given)
+				*(stringptr++) = name->given;
+			if (stringptr != strings) {
+				*stringptr = NULL;
+				new_file_as = g_strjoinv(", ", strings);
+			}
+
+			e_contact_name_free (name);
+		}
+
+		/* Use org as fallback */
+		if (!new_file_as) {
+			const gchar *org = e_contact_get_const (contact, E_CONTACT_ORG);
+
+			if (org && *org) {
+				new_file_as = strdup(org);
+			}
+		}
+
+		/* Add the FILE_AS attribute to the vcard */
+		if (new_file_as) {
+			attr = e_vcard_attribute_new (NULL, EVC_X_FILE_AS);
+			e_vcard_add_attribute_with_value (E_VCARD (contact), attr, new_file_as);
+
+			g_free (new_file_as);
+		}
+	}
+
+	if (attr) {
+		GList *p = e_vcard_attribute_get_values (attr);
+
+		return p && p->data ? p->data : (gpointer) "";
+	} else {
+		return NULL;
+	}
+}
+
+static void
+fileas_setter (EContact *contact, EVCardAttribute *attr, gpointer data)
+{
+	/* Default implementation */
+	const gchar* file_as = data;
+	e_vcard_attribute_add_value (attr, file_as ? : "");
+}
+
 
 
 static gpointer
@@ -1451,8 +1517,7 @@ e_contact_get (EContact *contact, EContactField field_id)
 		EVCardAttribute *attr = e_contact_get_first_attr (contact, info->vcard_field_name);
 		gpointer rv = NULL;
 
-		if (attr)
-			rv = info->struct_getter (contact, attr);
+		rv = info->struct_getter (contact, attr);
 
 		if (info->t & E_CONTACT_FIELD_TYPE_STRUCT)
 			return (gpointer)info->boxed_type_getter();
diff --git a/addressbook/libebook/e-vcard.c b/addressbook/libebook/e-vcard.c
index 589b37f..90597a6 100644
--- a/addressbook/libebook/e-vcard.c
+++ b/addressbook/libebook/e-vcard.c
@@ -660,7 +660,6 @@ static GList*
 e_vcard_ensure_attributes (EVCard *evc)
 {
         if (evc->priv->vcard) {
-		EVCardAttribute *fileas_attr;
                 gchar *vcs = evc->priv->vcard;
 
                 /* detach vCard to avoid loops */
@@ -669,52 +668,6 @@ e_vcard_ensure_attributes (EVCard *evc)
                 /* Parse the vCard */
                 parse (evc, vcs);
                 g_free (vcs);
-
-		/* Generate a FILE_AS field if needed */
-		fileas_attr = e_vcard_get_attribute (evc, EVC_X_FILE_AS);
-		if (!fileas_attr) {
-			EVCardAttribute *name_attr = NULL;
-			gchar *file_as_new = NULL;
-			gchar *strings[4];
-			gchar **strings_p = strings;
-
-			/* Get name attribute */
-			name_attr = e_vcard_get_attribute (evc, EVC_N);
-
-			if (name_attr) {
-				GList *p = e_vcard_attribute_get_values (name_attr);
-
-				/* Check if family name is present */
-				if (p && p->data)
-					*(strings_p++) = p->data; p = p->next;
-
-				/* Check if given name is present */
-				if (p && p->data)
-					*(strings_p++) = p->data;
-
-				/* Generate new FILE_AS */
-				if (strings_p != strings) {
-					*strings_p = NULL;
-					file_as_new = g_strjoinv (", ", strings);
-				}
-			}
-
-			/* Use org as fallback */
-			if (!file_as_new) {
-				EVCardAttribute *org_attr = e_vcard_get_attribute (evc, EVC_ORG);
-				if (org_attr) {
-					const gchar *org = e_vcard_attribute_get_value (org_attr);
-					file_as_new = g_strdup (org);
-				}
-			}
-
-			if (file_as_new) {
-				EVCardAttribute *fileas_attr_new = e_vcard_attribute_new ("", EVC_X_FILE_AS);
-				e_vcard_add_attribute_with_value (evc, fileas_attr_new, file_as_new);
-
-				g_free (file_as_new);
-			}
-		}
         }
 
         return evc->priv->attributes;



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