[evolution-patches] some improvements to libebook



e-contact/e-vcard patch here, just adding some more convenience methods
so we can make use of them in the contact editor.

Chris
? libebook/e-contact.c.foo
Index: ChangeLog
===================================================================
RCS file: /cvs/gnome/evolution-data-server/addressbook/ChangeLog,v
retrieving revision 1.93
diff -u -r1.93 ChangeLog
--- ChangeLog	24 Feb 2004 23:44:37 -0000	1.93
+++ ChangeLog	24 Feb 2004 23:51:11 -0000
@@ -1,3 +1,31 @@
+2004-02-24  Chris Toshok  <toshok ximian com>
+
+	* libebook/e-vcard.c (e_vcard_attribute_is_single_valued):
+	predicate returning TRUE if there's only one value for the
+	attribute.
+	(e_vcard_attribute_get_value): return the first value in the list,
+	if there is one.  also, warn if called on a multi-valued
+	attribute.
+	(e_vcard_attribute_get_value_decoded): same, but return a GString.
+	(e_vcard_attribute_has_type): predicate to check for TYPE= typestr
+	in the parameter list for an attribute.
+
+	* libebook/e-vcard.h: add prototypes for
+	e_vcard_attribute_is_single_valued and
+	e_vcard_attribute_get_value{_decoded}.  Also, add
+	e_vcard_attribute_has_type.
+
+	* libebook/e-contact.h: add prototypes for
+	e_contact_{get,set}_attributes, and e_contact_vcard_attribute.
+
+	* libebook/e-contact.c (e_contact_vcard_attribute): new function,
+	return the vcard attribute for a given EContactField id.
+	(e_contact_get_attributes): get a GList* of EVCardAttributes for
+	the given EContactField.  This allows you to get at the parameters
+	to the attribute, which you can't get with e_contact_get.
+	(e_contact_set_attributes): an analogous setter for
+	e_contact_get_attributes.
+
 2004-02-23  Chris Toshok  <toshok ximian com>
 
 	* libebook/e-vcard.c (parse): wrap spew in "#define d(x)".
Index: libebook/e-contact.c
===================================================================
RCS file: /cvs/gnome/evolution-data-server/addressbook/libebook/e-contact.c,v
retrieving revision 1.23
diff -u -r1.23 e-contact.c
--- libebook/e-contact.c	24 Feb 2004 23:44:39 -0000	1.23
+++ libebook/e-contact.c	24 Feb 2004 23:51:12 -0000
@@ -1324,6 +1324,22 @@
 	return "";
 }
 
+const char*
+e_contact_vcard_attribute  (EContactField field_id)
+{
+	int i;
+
+	g_return_val_if_fail (field_id >= 1 && field_id <= E_CONTACT_FIELD_LAST, "");
+
+	for (i = 0; i < G_N_ELEMENTS (field_info); i ++) {
+		if (field_id == field_info[i].field_id)
+			return field_info[i].vcard_field_name;
+	}
+
+	g_warning ("unknown field id %d", field_id);
+	return NULL;
+}
+
 EContactField
 e_contact_field_id (const char *field_name)
 {
@@ -1387,6 +1403,75 @@
 		      NULL);
 }
 
+GList*
+e_contact_get_attributes (EContact *contact, EContactField field_id)
+{
+	GList *l = NULL;
+	GList *attrs, *a;
+	int i;
+	EContactFieldInfo *info = NULL;
+
+	g_return_val_if_fail (contact && E_IS_CONTACT (contact), NULL);
+	g_return_val_if_fail (field_id >= 1 && field_id <= E_CONTACT_FIELD_LAST, NULL);
+
+	for (i = 0; i < G_N_ELEMENTS (field_info); i++) {
+		if (field_info[i].field_id == field_id) {
+			info = &field_info[i];
+			break;
+		}
+	}
+
+	if (!info) {
+		g_warning ("unknown field %d", field_id);
+		return NULL;
+	}
+
+	attrs = e_vcard_get_attributes (E_VCARD (contact));
+
+	for (a = attrs; a; a = a->next) {
+		EVCardAttribute *attr = a->data;
+		const char *name, *group;
+
+		group = e_vcard_attribute_get_group (attr);
+		name = e_vcard_attribute_get_name (attr);
+
+		if ((!group || !*group) && !strcasecmp (name, info->vcard_field_name)) {
+			l = g_list_append (l, e_vcard_attribute_copy (attr));
+		}
+	}
+
+	return l;
+}
+
+void
+e_contact_set_attributes (EContact *contact, EContactField field_id, GList *attributes)
+{
+	EContactFieldInfo *info = NULL;
+	GList *l;
+	int i;
+
+	g_return_if_fail (contact && E_IS_CONTACT (contact));
+	g_return_if_fail (field_id >= 1 && field_id <= E_CONTACT_FIELD_LAST);
+
+	for (i = 0; i < G_N_ELEMENTS (field_info); i++) {
+		if (field_info[i].field_id == field_id) {
+			info = &field_info[i];
+			break;
+		}
+	}
+
+	if (!info) {
+		g_warning ("unknown field %d", field_id);
+		return;
+	}
+
+	e_vcard_remove_attributes (E_VCARD (contact), NULL, info->vcard_field_name);
+
+	for (l = attributes; l; l = l->next)
+		e_vcard_add_attribute (E_VCARD (contact),
+				       e_vcard_attribute_copy ((EVCardAttribute*)l->data));
+}
+
 EContactName*
 e_contact_name_new ()
 {
Index: libebook/e-contact.h
===================================================================
RCS file: /cvs/gnome/evolution-data-server/addressbook/libebook/e-contact.h,v
retrieving revision 1.10
diff -u -r1.10 e-contact.h
--- libebook/e-contact.h	21 Feb 2004 01:20:46 -0000	1.10
+++ libebook/e-contact.h	24 Feb 2004 23:51:12 -0000
@@ -254,6 +254,11 @@
 const gpointer          e_contact_get_const        (EContact *contact, EContactField field_id);
 void                    e_contact_set              (EContact *contact, EContactField field_id, gpointer value);
 
+/* the following two calls return and take a GList of
+   EVCardAttribute*'s. */
+GList*                  e_contact_get_attributes   (EContact *contact, EContactField field_id);
+void                    e_contact_set_attributes   (EContact *contact, EContactField field_id, GList *attributes);
+
 /* misc functions for structured values */
 GType                   e_contact_date_get_type    (void);
 EContactDate           *e_contact_date_new         (void);
@@ -283,6 +288,7 @@
 
 const char*             e_contact_field_name       (EContactField field_id);
 const char*             e_contact_pretty_name      (EContactField field_id);
+const char*             e_contact_vcard_attribute  (EContactField field_id);
 EContactField           e_contact_field_id         (const char *field_name);
 
 #endif /* __E_CONTACT_H__ */
Index: libebook/e-vcard.c
===================================================================
RCS file: /cvs/gnome/evolution-data-server/addressbook/libebook/e-vcard.c,v
retrieving revision 1.9
diff -u -r1.9 e-vcard.c
--- libebook/e-vcard.c	24 Feb 2004 23:44:39 -0000	1.9
+++ libebook/e-vcard.c	24 Feb 2004 23:51:13 -0000
@@ -1189,6 +1189,63 @@
 	return attr->decoded_values;
 }
 
+gboolean
+e_vcard_attribute_is_single_valued (EVCardAttribute *attr)
+{
+	if (attr->values == NULL
+	    || attr->values->next != NULL)
+		return FALSE;
+
+	return TRUE;
+}
+
+char*
+e_vcard_attribute_get_value (EVCardAttribute *attr)
+{
+	GList *values = e_vcard_attribute_get_values (attr);
+
+	if (!e_vcard_attribute_is_single_valued (attr))
+		g_warning ("e_vcard_attribute_get_value called on multivalued attribute");
+
+	return values ? g_strdup ((char*)values->data) : NULL;
+}
+
+GString*
+e_vcard_attribute_get_value_decoded (EVCardAttribute *attr)
+{
+	GList *values = e_vcard_attribute_get_values_decoded (attr);
+	GString *str = NULL;
+
+	if (!e_vcard_attribute_is_single_valued (attr))
+		g_warning ("e_vcard_attribute_get_value_decoded called on multivalued attribute");
+
+	if (values)
+		str = values->data;
+
+	return str ? g_string_new_len (str->str, str->len) : NULL;
+}
+
+gboolean
+e_vcard_attribute_has_type (EVCardAttribute *attr, const char *typestr)
+{
+	GList *params = e_vcard_attribute_get_params (attr);
+	GList *p;
+	for (p = params; p; p = p->next) {
+		EVCardAttributeParam *param = p->data;
+
+		if (!strcasecmp (e_vcard_attribute_param_get_name (param), EVC_TYPE)) {
+			GList *values = e_vcard_attribute_param_get_values (param);
+			GList *v;
+			for (v = values; v; v = v->next) {
+				if (!strcasecmp ((char*)v->data, typestr))
+					return TRUE;
+			}
+		}
+	}
+
+	return FALSE;
+}
+
 GList*
 e_vcard_attribute_get_params (EVCardAttribute *attr)
 {
Index: libebook/e-vcard.h
===================================================================
RCS file: /cvs/gnome/evolution-data-server/addressbook/libebook/e-vcard.h,v
retrieving revision 1.4
diff -u -r1.4 e-vcard.h
--- libebook/e-vcard.h	23 Jan 2004 20:16:12 -0000	1.4
+++ libebook/e-vcard.h	24 Feb 2004 23:51:13 -0000
@@ -163,10 +163,18 @@
 GList*           e_vcard_attribute_get_values (EVCardAttribute *attr);  /* GList elements are of type char* */
 GList*           e_vcard_attribute_get_values_decoded (EVCardAttribute *attr); /* GList elements are of type GString* */
 
+/* special accessors for single valued attributes */
+gboolean              e_vcard_attribute_is_single_valued      (EVCardAttribute *attr);
+char*                 e_vcard_attribute_get_value             (EVCardAttribute *attr);
+GString*              e_vcard_attribute_get_value_decoded     (EVCardAttribute *attr);
+
 GList*           e_vcard_attribute_get_params       (EVCardAttribute *attr);
 const char*      e_vcard_attribute_param_get_name   (EVCardAttributeParam *param);
 GList*           e_vcard_attribute_param_get_values (EVCardAttributeParam *param);
 
+/* special TYPE= parameter predicate (checks for TYPE= typestr */
+gboolean         e_vcard_attribute_has_type         (EVCardAttribute *attr, const char *typestr);
+
 /* Utility functions. */
 char*            e_vcard_escape_string (const char *str);
 char*            e_vcard_unescape_string (const char *str);


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