From ac53ec420a4c893e76507ee373a4d356278eb001 Mon Sep 17 00:00:00 2001 From: Chris Toshok Date: Thu, 26 Feb 2004 15:54:10 +0000 Subject: [PATCH] predicate returning TRUE if there's only one value for the attribute. 2004-02-25 Chris Toshok * 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. --- addressbook/ChangeLog | 31 ++++++++++++++- addressbook/libebook/e-contact.c | 85 ++++++++++++++++++++++++++++++++++++++++ addressbook/libebook/e-contact.h | 6 +++ addressbook/libebook/e-vcard.c | 59 ++++++++++++++++++++++++++++ addressbook/libebook/e-vcard.h | 8 ++++ 5 files changed, 188 insertions(+), 1 deletion(-) diff --git a/addressbook/ChangeLog b/addressbook/ChangeLog index faf9cdd..9eddc93 100644 --- a/addressbook/ChangeLog +++ b/addressbook/ChangeLog @@ -1,9 +1,38 @@ +2004-02-25 Chris Toshok + + * 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-25 Sivaiah Nallagatla + * libebook/e-vcard.h : added EVC_X_GROUPWISE definition * libebook/e-contact.[ch] : added E_CONTACT_IM_GROUPWISE field to EContact to store groupwise im ids -2dd E_004-02-24 Hans Petter Jansson +2004-02-24 Hans Petter Jansson * libebook/e-book-async.c (_default_book_response_dtor): Don't unref the *new* book after returning it to user. This was causing diff --git a/addressbook/libebook/e-contact.c b/addressbook/libebook/e-contact.c index 786e8c3..3440c8a 100644 --- a/addressbook/libebook/e-contact.c +++ b/addressbook/libebook/e-contact.c @@ -1325,6 +1325,22 @@ e_contact_pretty_name (EContactField field_id) 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) { @@ -1388,6 +1404,75 @@ e_contact_set (EContact *contact, EContactField field_id, gpointer value) 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 () { diff --git a/addressbook/libebook/e-contact.h b/addressbook/libebook/e-contact.h index fb51637..ed83f3c 100644 --- a/addressbook/libebook/e-contact.h +++ b/addressbook/libebook/e-contact.h @@ -255,6 +255,11 @@ gpointer e_contact_get (EContact *contact, EContactF 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); @@ -284,6 +289,7 @@ void e_contact_address_free (EContactAddress *address); 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__ */ diff --git a/addressbook/libebook/e-vcard.c b/addressbook/libebook/e-vcard.c index 45e0bbc..fcfaf72 100644 --- a/addressbook/libebook/e-vcard.c +++ b/addressbook/libebook/e-vcard.c @@ -1189,6 +1189,65 @@ e_vcard_attribute_get_values_decoded (EVCardAttribute *attr) 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) { diff --git a/addressbook/libebook/e-vcard.h b/addressbook/libebook/e-vcard.h index 2289d23..f3692c9 100644 --- a/addressbook/libebook/e-vcard.h +++ b/addressbook/libebook/e-vcard.h @@ -164,10 +164,18 @@ const char* e_vcard_attribute_get_name (EVCardAttribute *attr); 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); -- 2.7.4