Commit c7e09290 authored by Sylvain Berfini's avatar Sylvain Berfini 🎩

Cache vCards SIP addresses for faster getters

parent 5cbf79d6
...@@ -264,16 +264,9 @@ void linphone_core_interpret_friend_uri(LinphoneCore *lc, const char *uri, char ...@@ -264,16 +264,9 @@ void linphone_core_interpret_friend_uri(LinphoneCore *lc, const char *uri, char
const LinphoneAddress * linphone_friend_get_address(const LinphoneFriend *lf) { const LinphoneAddress * linphone_friend_get_address(const LinphoneFriend *lf) {
if (linphone_core_vcard_supported()) { if (linphone_core_vcard_supported()) {
if (lf->vcard) { if (lf->vcard) {
bctbx_list_t *sip_addresses = linphone_vcard_get_sip_addresses(lf->vcard); const bctbx_list_t *sip_addresses = linphone_vcard_get_sip_addresses(lf->vcard);
if (sip_addresses) { if (sip_addresses) {
const char *uri = (const char *)bctbx_list_nth_data(sip_addresses, 0); LinphoneAddress *addr = (LinphoneAddress *)bctbx_list_nth_data(sip_addresses, 0);
LinphoneAddress *addr = NULL;
if (uri) addr = linphone_address_new(uri);
bctbx_list_free(sip_addresses);
if (lf->uri){
linphone_address_unref(lf->uri);
}
((LinphoneFriend*)lf)->uri = addr;
return addr; return addr;
} }
} }
...@@ -327,27 +320,20 @@ void linphone_friend_add_address(LinphoneFriend *lf, const LinphoneAddress *addr ...@@ -327,27 +320,20 @@ void linphone_friend_add_address(LinphoneFriend *lf, const LinphoneAddress *addr
} }
bctbx_list_t* linphone_friend_get_addresses(const LinphoneFriend *lf) { bctbx_list_t* linphone_friend_get_addresses(const LinphoneFriend *lf) {
bctbx_list_t *sip_addresses = NULL;
bctbx_list_t *addresses = NULL;
bctbx_list_t *iterator = NULL;
if (!lf) return NULL; if (!lf) return NULL;
if (linphone_core_vcard_supported()) { if (linphone_core_vcard_supported()) {
sip_addresses = linphone_vcard_get_sip_addresses(lf->vcard); bctbx_list_t *result = NULL;
iterator = sip_addresses; const bctbx_list_t * addresses = linphone_vcard_get_sip_addresses(lf->vcard);
while (iterator) { while (addresses) {
const char *sip_address = (const char *)bctbx_list_get_data(iterator); LinphoneAddress *addr = (LinphoneAddress *)addresses->data;
LinphoneAddress *addr = linphone_address_new(sip_address); result = bctbx_list_append(result, linphone_address_clone(addr));
if (addr) { addresses = bctbx_list_next(addresses);
addresses = bctbx_list_append(addresses, addr);
}
iterator = bctbx_list_next(iterator);
} }
if (sip_addresses) bctbx_list_free(sip_addresses); return result;
return addresses;
} else { } else {
return lf->uri ? bctbx_list_append(addresses, linphone_address_clone(lf->uri)) : NULL; bctbx_list_t *addresses = NULL;
return lf->uri ? bctbx_list_append(addresses, lf->uri) : NULL;
} }
} }
...@@ -771,14 +757,17 @@ void linphone_friend_edit(LinphoneFriend *fr) { ...@@ -771,14 +757,17 @@ void linphone_friend_edit(LinphoneFriend *fr) {
void linphone_friend_done(LinphoneFriend *fr) { void linphone_friend_done(LinphoneFriend *fr) {
ms_return_if_fail(fr); ms_return_if_fail(fr);
if (!fr->lc || !fr->friend_list) return; if (!fr->lc) return;
linphone_friend_apply(fr, fr->lc); linphone_friend_apply(fr, fr->lc);
linphone_friend_save(fr, fr->lc); linphone_friend_save(fr, fr->lc);
if (fr && linphone_core_vcard_supported() && fr->vcard) { if (fr && linphone_core_vcard_supported() && fr->vcard) {
if (linphone_vcard_compare_md5_hash(fr->vcard) != 0) { if (linphone_vcard_compare_md5_hash(fr->vcard) != 0) {
ms_debug("vCard's md5 has changed, mark friend as dirty"); ms_debug("vCard's md5 has changed, mark friend as dirty and clear sip addresses list cache");
fr->friend_list->dirty_friends_to_update = bctbx_list_append(fr->friend_list->dirty_friends_to_update, linphone_friend_ref(fr)); linphone_vcard_clean_cache(fr->vcard);
if (fr->friend_list) {
fr->friend_list->dirty_friends_to_update = bctbx_list_append(fr->friend_list->dirty_friends_to_update, linphone_friend_ref(fr));
}
} }
} }
} }
......
...@@ -37,6 +37,7 @@ struct _LinphoneVcard { ...@@ -37,6 +37,7 @@ struct _LinphoneVcard {
char *etag; char *etag;
char *url; char *url;
unsigned char md5[VCARD_MD5_HASH_SIZE]; unsigned char md5[VCARD_MD5_HASH_SIZE];
bctbx_list_t *sip_addresses_cache;
}; };
#ifdef __cplusplus #ifdef __cplusplus
...@@ -82,6 +83,7 @@ void linphone_vcard_free(LinphoneVcard *vCard) { ...@@ -82,6 +83,7 @@ void linphone_vcard_free(LinphoneVcard *vCard) {
if (!vCard) return; if (!vCard) return;
if (vCard->etag) ms_free(vCard->etag); if (vCard->etag) ms_free(vCard->etag);
if (vCard->url) ms_free(vCard->url); if (vCard->url) ms_free(vCard->url);
linphone_vcard_clean_cache(vCard);
vCard->belCard.reset(); vCard->belCard.reset();
ms_free(vCard); ms_free(vCard);
} }
...@@ -238,17 +240,17 @@ void linphone_vcard_edit_main_sip_address(LinphoneVcard *vCard, const char *sip_ ...@@ -238,17 +240,17 @@ void linphone_vcard_edit_main_sip_address(LinphoneVcard *vCard, const char *sip_
} }
} }
bctbx_list_t* linphone_vcard_get_sip_addresses(const LinphoneVcard *vCard) { const bctbx_list_t* linphone_vcard_get_sip_addresses(LinphoneVcard *vCard) {
bctbx_list_t *result = NULL;
if (!vCard) return NULL; if (!vCard) return NULL;
if (!vCard->sip_addresses_cache) {
for (auto it = vCard->belCard->getImpp().begin(); it != vCard->belCard->getImpp().end(); ++it) { for (auto it = vCard->belCard->getImpp().begin(); it != vCard->belCard->getImpp().end(); ++it) {
const char *value = (*it)->getValue().c_str(); LinphoneAddress* addr = linphone_address_new((*it)->getValue().c_str());
if (strncmp(value, "sip:", 4) == 0) { if (addr) {
result = bctbx_list_append(result, (char *)value); vCard->sip_addresses_cache = bctbx_list_append(vCard->sip_addresses_cache, addr);
}
} }
} }
return result; return vCard->sip_addresses_cache;
} }
void linphone_vcard_add_phone_number(LinphoneVcard *vCard, const char *phone) { void linphone_vcard_add_phone_number(LinphoneVcard *vCard, const char *phone) {
...@@ -390,6 +392,11 @@ bool_t linphone_core_vcard_supported(void) { ...@@ -390,6 +392,11 @@ bool_t linphone_core_vcard_supported(void) {
return TRUE; return TRUE;
} }
void linphone_vcard_clean_cache(LinphoneVcard *vCard) {
if (vCard->sip_addresses_cache) bctbx_list_free_with_data(vCard->sip_addresses_cache, (void (*)(void*))linphone_address_unref);
vCard->sip_addresses_cache = NULL;
}
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
\ No newline at end of file
...@@ -179,11 +179,11 @@ void linphone_vcard_remove_sip_address(LinphoneVcard *vCard, const char *sip_add ...@@ -179,11 +179,11 @@ void linphone_vcard_remove_sip_address(LinphoneVcard *vCard, const char *sip_add
void linphone_vcard_edit_main_sip_address(LinphoneVcard *vCard, const char *sip_address); void linphone_vcard_edit_main_sip_address(LinphoneVcard *vCard, const char *sip_address);
/** /**
* 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 * Returns the list of SIP addresses (as LinphoneAddress) in the vCard (all the IMPP attributes that has an URI value starting by "sip:") or NULL
* @param[in] vCard the LinphoneVcard * @param[in] vCard the LinphoneVcard
* @return \mslist{const char *} * @return const \mslist{LinphoneAddress *}
*/ */
LINPHONE_PUBLIC bctbx_list_t* linphone_vcard_get_sip_addresses(const LinphoneVcard *vCard); LINPHONE_PUBLIC const bctbx_list_t* linphone_vcard_get_sip_addresses(LinphoneVcard *vCard);
/** /**
* Adds a phone number in the vCard, using the TEL property * Adds a phone number in the vCard, using the TEL property
...@@ -283,6 +283,8 @@ void linphone_vcard_compute_md5_hash(LinphoneVcard *vCard); ...@@ -283,6 +283,8 @@ void linphone_vcard_compute_md5_hash(LinphoneVcard *vCard);
*/ */
bool_t linphone_vcard_compare_md5_hash(LinphoneVcard *vCard); bool_t linphone_vcard_compare_md5_hash(LinphoneVcard *vCard);
void linphone_vcard_clean_cache(LinphoneVcard *vCard);
/** /**
* @} * @}
*/ */
......
...@@ -158,3 +158,6 @@ bool_t linphone_vcard_compare_md5_hash(LinphoneVcard *vCard) { ...@@ -158,3 +158,6 @@ bool_t linphone_vcard_compare_md5_hash(LinphoneVcard *vCard) {
bool_t linphone_core_vcard_supported(void) { bool_t linphone_core_vcard_supported(void) {
return FALSE; return FALSE;
} }
void linphone_vcard_clean_cache(LinphoneVcard *vCard) {
}
...@@ -157,6 +157,7 @@ static void linphone_vcard_phone_numbers_and_sip_addresses(void) { ...@@ -157,6 +157,7 @@ static void linphone_vcard_phone_numbers_and_sip_addresses(void) {
lvc = linphone_vcard_context_get_vcard_from_buffer(manager->lc->vcard_context, "BEGIN:VCARD\r\nVERSION:4.0\r\nFN:Sylvain Berfini\r\nTEL;TYPE=work:0952636505\r\nTEL:0476010203\r\nEND:VCARD\r\n"); lvc = linphone_vcard_context_get_vcard_from_buffer(manager->lc->vcard_context, "BEGIN:VCARD\r\nVERSION:4.0\r\nFN:Sylvain Berfini\r\nTEL;TYPE=work:0952636505\r\nTEL:0476010203\r\nEND:VCARD\r\n");
lf = linphone_friend_new_from_vcard(lvc); lf = linphone_friend_new_from_vcard(lvc);
lf->lc = manager->lc;
sip_addresses = linphone_friend_get_addresses(lf); sip_addresses = linphone_friend_get_addresses(lf);
phone_numbers = linphone_friend_get_phone_numbers(lf); phone_numbers = linphone_friend_get_phone_numbers(lf);
...@@ -167,7 +168,6 @@ static void linphone_vcard_phone_numbers_and_sip_addresses(void) { ...@@ -167,7 +168,6 @@ static void linphone_vcard_phone_numbers_and_sip_addresses(void) {
addr = linphone_address_new("sip:sylvain@sip.linphone.org"); addr = linphone_address_new("sip:sylvain@sip.linphone.org");
linphone_friend_add_address(lf, addr); linphone_friend_add_address(lf, addr);
linphone_address_unref(addr);
sip_addresses = linphone_friend_get_addresses(lf); sip_addresses = linphone_friend_get_addresses(lf);
BC_ASSERT_EQUAL((unsigned int)bctbx_list_size(sip_addresses), 1, unsigned int, "%u"); BC_ASSERT_EQUAL((unsigned int)bctbx_list_size(sip_addresses), 1, unsigned int, "%u");
if (sip_addresses) bctbx_list_free_with_data(sip_addresses, (void (*)(void *))linphone_address_unref); if (sip_addresses) bctbx_list_free_with_data(sip_addresses, (void (*)(void *))linphone_address_unref);
...@@ -182,9 +182,9 @@ static void linphone_vcard_phone_numbers_and_sip_addresses(void) { ...@@ -182,9 +182,9 @@ static void linphone_vcard_phone_numbers_and_sip_addresses(void) {
BC_ASSERT_EQUAL((unsigned int)bctbx_list_size(phone_numbers), 0, unsigned int, "%u"); BC_ASSERT_EQUAL((unsigned int)bctbx_list_size(phone_numbers), 0, unsigned int, "%u");
if (phone_numbers) bctbx_list_free(phone_numbers); if (phone_numbers) bctbx_list_free(phone_numbers);
addr = linphone_address_new("sip:sylvain@sip.linphone.org"); linphone_friend_edit(lf);
linphone_friend_remove_address(lf, addr); linphone_friend_remove_address(lf, addr);
linphone_address_unref(addr); linphone_friend_done(lf);
sip_addresses = linphone_friend_get_addresses(lf); sip_addresses = linphone_friend_get_addresses(lf);
BC_ASSERT_EQUAL((unsigned int)bctbx_list_size(sip_addresses), 0, unsigned int, "%u"); BC_ASSERT_EQUAL((unsigned int)bctbx_list_size(sip_addresses), 0, unsigned int, "%u");
if (sip_addresses) bctbx_list_free_with_data(sip_addresses, (void (*)(void *))linphone_address_unref); if (sip_addresses) bctbx_list_free_with_data(sip_addresses, (void (*)(void *))linphone_address_unref);
...@@ -194,6 +194,7 @@ static void linphone_vcard_phone_numbers_and_sip_addresses(void) { ...@@ -194,6 +194,7 @@ static void linphone_vcard_phone_numbers_and_sip_addresses(void) {
BC_ASSERT_EQUAL((unsigned int)bctbx_list_size(phone_numbers), 1, unsigned int, "%u"); BC_ASSERT_EQUAL((unsigned int)bctbx_list_size(phone_numbers), 1, unsigned int, "%u");
if (phone_numbers) bctbx_list_free(phone_numbers); if (phone_numbers) bctbx_list_free(phone_numbers);
linphone_address_unref(addr);
linphone_friend_unref(lf); linphone_friend_unref(lf);
lf = NULL; lf = NULL;
lvc = NULL; lvc = NULL;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment