From 411273845489b21c1dd3bde1bd718bff9414f74d Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Fri, 25 Mar 2016 14:59:28 +0100 Subject: [PATCH] Added methods to friend to manipulate phone numbers and sip addresses --- coreapi/friend.c | 106 +++++++++++++++++++++++++++++++++++++-- coreapi/linphonefriend.h | 44 +++++++++++++++- coreapi/vcard.cc | 31 ++++++++++++ coreapi/vcard.h | 28 +++++++++++ coreapi/vcard_stubs.c | 12 +++++ 5 files changed, 216 insertions(+), 5 deletions(-) diff --git a/coreapi/friend.c b/coreapi/friend.c index 41d191535..2025a67af 100644 --- a/coreapi/friend.c +++ b/coreapi/friend.c @@ -195,6 +195,10 @@ void linphone_core_interpret_friend_uri(LinphoneCore *lc, const char *uri, char } } +const LinphoneAddress *linphone_friend_get_address(const LinphoneFriend *lf){ + return lf->uri; +} + int linphone_friend_set_address(LinphoneFriend *lf, const LinphoneAddress *addr){ LinphoneAddress *fr = linphone_address_clone(addr); LinphoneVcard *vcard = NULL; @@ -211,6 +215,103 @@ int linphone_friend_set_address(LinphoneFriend *lf, const LinphoneAddress *addr) return 0; } +void linphone_friend_add_address(LinphoneFriend *lf, const LinphoneAddress *addr) { + LinphoneVcard *vcard = NULL; + if (!lf || !addr) { + return; + } + + vcard = linphone_friend_get_vcard(lf); + if (!vcard) { + return; + } + + linphone_vcard_add_sip_address(vcard, linphone_address_as_string_uri_only(addr)); +} + +const MSList* linphone_friend_get_addresses(LinphoneFriend *lf) { + LinphoneVcard *vcard = NULL; + MSList *sipAddresses = NULL; + MSList *addresses = NULL; + + if (!lf) { + return NULL; + } + + vcard = linphone_friend_get_vcard(lf); + if (!vcard) { + return NULL; + } + + sipAddresses = linphone_vcard_get_sip_addresses(vcard); + if (sipAddresses) { + const char *sipAddress = (const char *)sipAddresses->data; + LinphoneAddress *addr = linphone_address_new(sipAddress); + if (addr) { + ms_list_append(addresses, addr); + } + ms_list_free(sipAddresses); + } + return addresses; +} + +void linphone_friend_remove_address(LinphoneFriend *lf, const LinphoneAddress *addr) { + LinphoneVcard *vcard = NULL; + if (!lf || !addr) { + return; + } + + vcard = linphone_friend_get_vcard(lf); + if (!vcard) { + return; + } + + linphone_vcard_remove_sip_address(vcard, linphone_address_as_string_uri_only(addr)); +} + +void linphone_friend_add_phone_number(LinphoneFriend *lf, const char *phone) { + LinphoneVcard *vcard = NULL; + if (!lf || !phone) { + return; + } + + vcard = linphone_friend_get_vcard(lf); + if (!vcard) { + return; + } + + linphone_vcard_add_phone_number(vcard, phone); +} + +const MSList* linphone_friend_get_phone_numbers(LinphoneFriend *lf) { + LinphoneVcard *vcard = NULL; + + if (!lf) { + return NULL; + } + + vcard = linphone_friend_get_vcard(lf); + if (!vcard) { + return NULL; + } + + return linphone_vcard_get_phone_numbers(vcard); +} + +void linphone_friend_remove_phone_number(LinphoneFriend *lf, const char *phone) { + LinphoneVcard *vcard = NULL; + if (!lf || !phone) { + return; + } + + vcard = linphone_friend_get_vcard(lf); + if (!vcard) { + return; + } + + linphone_vcard_remove_phone_number(vcard, phone); +} + int linphone_friend_set_name(LinphoneFriend *lf, const char *name){ LinphoneAddress *fr = lf->uri; LinphoneVcard *vcard = NULL; @@ -334,10 +435,6 @@ static belle_sip_error_code _linphone_friend_marshall(belle_sip_object_t *obj, c return err; } -const LinphoneAddress *linphone_friend_get_address(const LinphoneFriend *lf){ - return lf->uri; -} - const char * linphone_friend_get_name(const LinphoneFriend *lf) { if (lf && lf->vcard) { return linphone_vcard_get_full_name(lf->vcard); @@ -915,6 +1012,7 @@ LinphoneFriend *linphone_friend_new_from_vcard(LinphoneVcard *vcard) { linphone_friend_set_address(fr, linphone_address); linphone_address_unref(linphone_address); } + ms_free(sipAddresses); } if (name) { linphone_friend_set_name(fr, name); diff --git a/coreapi/linphonefriend.h b/coreapi/linphonefriend.h index 5488a1358..4cbffbbfd 100644 --- a/coreapi/linphonefriend.h +++ b/coreapi/linphonefriend.h @@ -162,7 +162,49 @@ LINPHONE_PUBLIC int linphone_friend_set_address(LinphoneFriend *fr, const Linpho * @param lf #LinphoneFriend object * @return #LinphoneAddress */ -LINPHONE_PUBLIC const LinphoneAddress *linphone_friend_get_address(const LinphoneFriend *lf); +LINPHONE_PUBLIC const LinphoneAddress *linphone_friend_get_address(const LinphoneFriend *lf); + +/** + * Adds an address in this friend + * @param lf #LinphoneFriend object + * @param addr #LinphoneAddress object + */ +LINPHONE_PUBLIC void linphone_friend_add_address(LinphoneFriend *lf, const LinphoneAddress *addr); + +/** + * Returns a list of #LinphoneAddress for this friend + * @param lf #LinphoneFriend object + * @return \mslist{LinphoneAddress} + */ +LINPHONE_PUBLIC const MSList* linphone_friend_get_addresses(LinphoneFriend *lf); + +/** + * Removes an address in this friend + * @param lf #LinphoneFriend object + * @param addr #LinphoneAddress object + */ +LINPHONE_PUBLIC void linphone_friend_remove_address(LinphoneFriend *lf, const LinphoneAddress *addr); + +/** + * Adds a phone number in this friend + * @param lf #LinphoneFriend object + * @param phone number to add + */ +LINPHONE_PUBLIC void linphone_friend_add_phone_number(LinphoneFriend *lf, const char *phone); + +/** + * Returns a list of phone numbers for this friend + * @param lf #LinphoneFriend object + * @return a MSList of phone numbers + */ +LINPHONE_PUBLIC const MSList* linphone_friend_get_phone_numbers(LinphoneFriend *lf); + +/** + * Removes a phone number in this friend + * @param lf #LinphoneFriend object + * @param phone number to remove + */ +LINPHONE_PUBLIC void linphone_friend_remove_phone_number(LinphoneFriend *lf, const char *phone); /** * Set the display name for this friend diff --git a/coreapi/vcard.cc b/coreapi/vcard.cc index 635312069..2623c241f 100644 --- a/coreapi/vcard.cc +++ b/coreapi/vcard.cc @@ -163,6 +163,37 @@ MSList* linphone_vcard_get_sip_addresses(const LinphoneVcard *vCard) { return result; } +void linphone_vcard_add_phone_number(LinphoneVcard *vCard, const char *phone) { + if (!vCard || !phone) return; + + shared_ptr phone_number = belcard::BelCardGeneric::create(); + phone_number->setValue(phone); + vCard->belCard->addPhoneNumber(phone_number); +} + +void linphone_vcard_remove_phone_number(LinphoneVcard *vCard, const char *phone) { + if (!vCard) return; + + for (auto it = vCard->belCard->getPhoneNumbers().begin(); it != vCard->belCard->getPhoneNumbers().end(); ++it) { + const char *value = (*it)->getValue().c_str(); + if (strcmp(value, phone) == 0) { + vCard->belCard->removePhoneNumber(*it); + break; + } + } +} + +MSList* linphone_vcard_get_phone_numbers(const LinphoneVcard *vCard) { + MSList *result = NULL; + if (!vCard) return NULL; + + for (auto it = vCard->belCard->getPhoneNumbers().begin(); it != vCard->belCard->getPhoneNumbers().end(); ++it) { + const char *value = (*it)->getValue().c_str(); + result = ms_list_append(result, (char *)value); + } + return result; +} + void linphone_vcard_set_organization(LinphoneVcard *vCard, const char *organization) { if (!vCard) return; diff --git a/coreapi/vcard.h b/coreapi/vcard.h index 71e5440e7..7948fbd11 100644 --- a/coreapi/vcard.h +++ b/coreapi/vcard.h @@ -122,6 +122,34 @@ void linphone_vcard_edit_main_sip_address(LinphoneVcard *vCard, const char *sip_ */ LINPHONE_PUBLIC MSList* linphone_vcard_get_sip_addresses(const LinphoneVcard *vCard); +/** + * Adds a phone number in the vCard, using the TEL property + * @param[in] vCard the LinphoneVcard + * @param[in] sip_address the phone number to add + */ +void linphone_vcard_add_phone_number(LinphoneVcard *vCard, const char *phone); + +/** + * Removes a phone number in the vCard (if it exists), using the TEL property + * @param[in] vCard the LinphoneVcard + * @param[in] sip_address the phone number to remove + */ +void linphone_vcard_remove_phone_number(LinphoneVcard *vCard, const char *phone); + +/** + * Returns the list of phone numbers (as string) in the vCard (all the TEL attributes) or NULL + * @param[in] vCard the LinphoneVcard + * @return \mslist{const char *} + */ +LINPHONE_PUBLIC MSList* linphone_vcard_get_phone_numbers(const LinphoneVcard *vCard); + +/** + * Returns the list of SIP addresses (as string) in the vCard (all the IMPP attributes that has an URI value starting by "sip:") or NULL + * @param[in] vCard the LinphoneVcard + * @return \mslist{const char *} + */ +LINPHONE_PUBLIC MSList* linphone_vcard_get_sip_addresses(const LinphoneVcard *vCard); + /** * Fills the Organization field of the vCard * @param[in] vCard the LinphoneVcard diff --git a/coreapi/vcard_stubs.c b/coreapi/vcard_stubs.c index 6e9a33a3b..0a0fa20ae 100644 --- a/coreapi/vcard_stubs.c +++ b/coreapi/vcard_stubs.c @@ -71,6 +71,18 @@ MSList* linphone_vcard_get_sip_addresses(const LinphoneVcard *vCard) { return NULL; } +void linphone_vcard_add_phone_number(LinphoneVcard *vCard, const char *phone) { + +} + +void linphone_vcard_remove_phone_number(LinphoneVcard *vCard, const char *phone) { + +} + +MSList* linphone_vcard_get_phone_numbers(const LinphoneVcard *vCard) { + return NULL; +} + void linphone_vcard_set_organization(LinphoneVcard *vCard, const char *organization) { }