From 7f735fbdfda8b10cd9789a68457643b7049fe64c Mon Sep 17 00:00:00 2001 From: Vinicius Costa Gomes Date: Mon, 19 Apr 2010 20:20:42 -0300 Subject: [PATCH] obexd: Add the concept of phonebook_contact This way it is easy to deal with many contact fields. --- obexd/plugins/phonebook-tracker.c | 22 ++++++++----- obexd/plugins/vcard.c | 53 +++++++++++++++++++++---------- obexd/plugins/vcard.h | 19 +++++++++-- 3 files changed, 66 insertions(+), 28 deletions(-) diff --git a/obexd/plugins/phonebook-tracker.c b/obexd/plugins/phonebook-tracker.c index f88eb6c85..82068dc30 100644 --- a/obexd/plugins/phonebook-tracker.c +++ b/obexd/plugins/phonebook-tracker.c @@ -467,8 +467,8 @@ static void pull_contacts(char **reply, int num_fields, void *user_data) { struct phonebook_data *data = user_data; const struct apparam_field *params = data->params; + struct phonebook_contact *contact; GString *vcards = data->vcards; - char *formatted; int last_index; if (reply == NULL) @@ -485,13 +485,19 @@ static void pull_contacts(char **reply, int num_fields, void *user_data) if (data->index < params->liststartoffset || data->index > last_index) return; - formatted = g_strdup_printf("%s;%s;%s;%s;%s", reply[2], reply[3], - reply[4], reply[5], reply[6]); - - phonebook_add_entry(vcards, reply[0], TEL_TYPE_HOME, formatted, - reply[6], reply[1], params->filter); - - g_free(formatted); + contact = g_new0(struct phonebook_contact, 1); + contact->tel = g_strdup(reply[0]); + contact->tel_type = 1; /* HOME */ + contact->fullname = g_strdup(reply[1]); + contact->family = g_strdup(reply[2]); + contact->given = g_strdup(reply[3]); + contact->additional = g_strdup(reply[4]); + contact->prefix = g_strdup(reply[5]); + contact->suffix = g_strdup(reply[6]); + contact->email = g_strdup(reply[7]); + + phonebook_add_contact(vcards, contact, params->filter); + phonebook_contact_free(contact); return; diff --git a/obexd/plugins/vcard.c b/obexd/plugins/vcard.c index 3b833ef9b..a9d464f27 100644 --- a/obexd/plugins/vcard.c +++ b/obexd/plugins/vcard.c @@ -126,18 +126,19 @@ static void vcard_printf_begin(GString *vcards) vcard_printf(vcards, "VERSION:3.0"); } -static void vcard_printf_fullname(GString *vcards, const char *text) +static void vcard_printf_name(GString *vcards, + struct phonebook_contact *contact) { - char field[LEN_MAX]; - add_slash(field, text, LEN_MAX, strlen(text)); - vcard_printf(vcards, "FN:%s", field); + vcard_printf(vcards, "N:%s;%s;%s;%s;%s", contact->family, + contact->given, contact->additional, + contact->prefix, contact->suffix); } -static void vcard_printf_name(GString *vcards, const char *text) +static void vcard_printf_fullname(GString *vcards, const char *text) { char field[LEN_MAX]; add_slash(field, text, LEN_MAX, strlen(text)); - vcard_printf(vcards, "N:%s", field); + vcard_printf(vcards, "FN:%s", field); } static void vcard_printf_number(GString *vcards, const char *number, int type, @@ -196,13 +197,13 @@ static void vcard_printf_end(GString *vcards) vcard_printf(vcards, ""); } -void phonebook_add_entry(GString *vcards, const char *number, int type, - const char *name, const char *fullname, - const char *email, uint64_t filter) +void phonebook_add_contact(GString *vcards, struct phonebook_contact *contact, + uint64_t filter) { /* There's really nothing to do */ - if ((number == NULL || number[0] == '\0') && - (fullname == NULL || fullname[0] == '\0')) + if ((contact->tel == NULL || contact->tel[0] == '\0') && + (contact->fullname == NULL || + contact->fullname[0] == '\0')) return; filter |= (FILTER_VERSION | FILTER_FN | FILTER_N | FILTER_TEL); @@ -210,20 +211,38 @@ void phonebook_add_entry(GString *vcards, const char *number, int type, vcard_printf_begin(vcards); if (filter & FILTER_FN) { - if (fullname == NULL || fullname[0] == '\0') - vcard_printf_fullname(vcards, number); + if (contact->fullname == NULL || contact->fullname[0] == '\0') + vcard_printf_fullname(vcards, contact->tel); else - vcard_printf_fullname(vcards, fullname); + vcard_printf_fullname(vcards, contact->fullname); } if (filter & FILTER_TEL) - vcard_printf_number(vcards, number, type, TEL_TYPE_OTHER); + vcard_printf_number(vcards, contact->tel, contact->tel_type, + TEL_TYPE_OTHER); if (filter & FILTER_N) - vcard_printf_name(vcards, name); + vcard_printf_name(vcards, contact); if (filter & FILTER_EMAIL) - vcard_printf_email(vcards, email); + vcard_printf_email(vcards, contact->email); vcard_printf_end(vcards); } + +void phonebook_contact_free(struct phonebook_contact *contact) +{ + if (contact == NULL) + return; + + g_free(contact->fullname); + g_free(contact->given); + g_free(contact->family); + g_free(contact->additional); + g_free(contact->tel); + g_free(contact->email); + g_free(contact->prefix); + g_free(contact->suffix); + g_free(contact); + +} diff --git a/obexd/plugins/vcard.h b/obexd/plugins/vcard.h index d9171ccd6..f015e9480 100644 --- a/obexd/plugins/vcard.h +++ b/obexd/plugins/vcard.h @@ -27,6 +27,19 @@ enum phonebook_number_type { TEL_TYPE_OTHER, }; -void phonebook_add_entry(GString *vcards, const char *number, int type, - const char *name, const char *fullname, - const char *email, uint64_t filter); +struct phonebook_contact { + char *fullname; + char *given; + char *family; + char *additional; + char *tel; + int tel_type; + char *email; + char *prefix; + char *suffix; +}; + +void phonebook_add_contact(GString *vcards, struct phonebook_contact *contact, + uint64_t filter); + +void phonebook_contact_free(struct phonebook_contact *contact); -- 2.47.3