From 0a59544a446f3bf244b88d9bb2cac27269d6eb3c Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Wed, 16 Dec 2015 10:51:17 +0100 Subject: [PATCH] Added checks for null values in vcard.cc and added new methods to manipulate sip addresses in vcard from friend --- coreapi/friend.c | 24 ++++++++++++++---------- coreapi/vcard.cc | 37 +++++++++++++++++++++++++++++++++++++ coreapi/vcard.h | 14 ++++++++++++++ 3 files changed, 65 insertions(+), 10 deletions(-) diff --git a/coreapi/friend.c b/coreapi/friend.c index 1c173aba9..17ab1fda7 100644 --- a/coreapi/friend.c +++ b/coreapi/friend.c @@ -199,10 +199,12 @@ void linphone_core_interpret_friend_uri(LinphoneCore *lc, const char *uri, char } int linphone_friend_set_address(LinphoneFriend *lf, const LinphoneAddress *addr){ - LinphoneAddress *fr=linphone_address_clone(addr); + LinphoneAddress *fr = linphone_address_clone(addr); + linphone_address_clean(fr); - if (lf->uri!=NULL) linphone_address_destroy(lf->uri); - lf->uri=fr; + if (lf->uri != NULL) linphone_address_destroy(lf->uri); + lf->uri = fr; + return 0; } @@ -211,17 +213,19 @@ int linphone_friend_set_name(LinphoneFriend *lf, const char *name){ LinphoneVCard *vcard = NULL; if (fr == NULL){ - ms_error("linphone_friend_set_sip_addr() must be called before linphone_friend_set_name()."); + ms_error("linphone_friend_set_address() must be called before linphone_friend_set_name()."); return -1; } - linphone_address_set_display_name(fr,name); - + linphone_address_set_display_name(fr, name); vcard = linphone_friend_get_vcard(lf); if (!vcard) { linphone_friend_create_vcard(lf, name); - } else { + vcard = linphone_friend_get_vcard(lf); + } + if (vcard) { linphone_vcard_set_full_name(vcard, name); + linphone_vcard_edit_main_sip_address(vcard, linphone_address_as_string_uri_only(fr)); } return 0; @@ -771,10 +775,10 @@ bool_t linphone_friend_create_vcard(LinphoneFriend *fr, const char *name) { } else { const char *displayName = linphone_address_get_display_name(addr); if (!displayName) { - ms_error("Friend's URI doesn't have a display name"); - return FALSE; + fullName = linphone_address_get_username(addr); + } else { + fullName = displayName; } - fullName = displayName; } if (!fullName) { diff --git a/coreapi/vcard.cc b/coreapi/vcard.cc index 7e049f87d..2cdc8a677 100644 --- a/coreapi/vcard.cc +++ b/coreapi/vcard.cc @@ -32,6 +32,8 @@ extern "C" LinphoneVCard* linphone_vcard_new(void) { } extern "C" void linphone_vcard_free(LinphoneVCard *vCard) { + if (!vCard) return; + vCard->belCard.reset(); ms_free(vCard); } @@ -84,28 +86,63 @@ extern "C" LinphoneVCard* linphone_vcard_new_from_vcard4_buffer(const char *buff } extern "C" const char * linphone_vcard_as_vcard4_string(LinphoneVCard *vCard) { + if (!vCard) return NULL; + return vCard->belCard->toFoldedString().c_str(); } extern "C" void linphone_vcard_set_full_name(LinphoneVCard *vCard, const char *name) { + if (!vCard || !name) return; + shared_ptr fn = belcard::BelCardGeneric::create(); fn->setValue(name); vCard->belCard->setFullName(fn); } extern "C" const char* linphone_vcard_get_full_name(const LinphoneVCard *vCard) { + if (!vCard) return NULL; + const char *result = vCard->belCard->getFullName() ? vCard->belCard->getFullName()->getValue().c_str() : NULL; return result; } extern "C" void linphone_vcard_add_sip_address(LinphoneVCard *vCard, const char *sip_address) { + if (!vCard || !sip_address) return; + shared_ptr impp = belcard::BelCardGeneric::create(); impp->setValue(sip_address); vCard->belCard->addImpp(impp); } +extern "C" void linphone_vcard_remove_sip_address(LinphoneVCard *vCard, const char *sip_address) { + if (!vCard) return; + + for (auto it = vCard->belCard->getImpp().begin(); it != vCard->belCard->getImpp().end(); ++it) { + const char *value = (*it)->getValue().c_str(); + if (strcmp(value, sip_address) == 0) { + vCard->belCard->removeImpp(*it); + break; + } + } +} + +extern "C" void linphone_vcard_edit_main_sip_address(LinphoneVCard *vCard, const char *sip_address) { + if (!vCard || !sip_address) return; + + if (vCard->belCard->getImpp().size() > 0) { + const shared_ptr impp = vCard->belCard->getImpp().front(); + impp->setValue(sip_address); + } else { + shared_ptr impp = belcard::BelCardGeneric::create(); + impp->setValue(sip_address); + vCard->belCard->addImpp(impp); + } +} + extern "C" MSList* linphone_vcard_get_sip_addresses(const LinphoneVCard *vCard) { MSList *result = NULL; + if (!vCard) return NULL; + for (auto it = vCard->belCard->getImpp().begin(); it != vCard->belCard->getImpp().end(); ++it) { const char *value = (*it)->getValue().c_str(); if (strncmp(value, "sip:", 4) == 0) { diff --git a/coreapi/vcard.h b/coreapi/vcard.h index ac3f4d296..4ad094915 100644 --- a/coreapi/vcard.h +++ b/coreapi/vcard.h @@ -98,6 +98,20 @@ LINPHONE_PUBLIC const char* linphone_vcard_get_full_name(const LinphoneVCard *vC */ LINPHONE_PUBLIC void linphone_vcard_add_sip_address(LinphoneVCard *vCard, const char *sip_address); +/** + * Removes a SIP address in the vCard (if it exists), using the IMPP property + * @param[in] vCard the LinphoneVCard + * @param[in] sip_address the SIP address to remove + */ +LINPHONE_PUBLIC void linphone_vcard_remove_sip_address(LinphoneVCard *vCard, const char *sip_address); + +/** + * Edits the preferred SIP address in the vCard (or the first one), using the IMPP property + * @param[in] vCard the LinphoneVCard + * @param[in] sip_address the new SIP address + */ +LINPHONE_PUBLIC void linphone_vcard_edit_main_sip_address(LinphoneVCard *vCard, const char *sip_address); + /** * Returns the list of SIP addresses (as const char *) in the vCard (all the IMPP attributes that has an URI value starting by "sip:") or NULL * @param[in] vCard the LinphoneVCard