Commit 5554f374 authored by Sylvain Berfini's avatar Sylvain Berfini 🐮
Browse files

Added methods to find friend by phone number + tests

parent 3f5e30fd
......@@ -467,6 +467,39 @@ bctbx_list_t* linphone_friend_get_phone_numbers(const LinphoneFriend *lf) {
return NULL;
}
bool_t linphone_friend_has_phone_number(const LinphoneFriend *lf, const char *phoneNumber) {
if (!lf || !phoneNumber) return FALSE;
LinphoneProxyConfig *cfg = linphone_core_get_default_proxy_config(lf->lc);
if (phoneNumber == NULL || !linphone_proxy_config_is_phone_number(cfg, phoneNumber)) {
ms_warning("Phone number [%s] isn't valid", phoneNumber);
return FALSE;
}
char *normalized_phone_number = linphone_proxy_config_normalize_phone_number(cfg, phoneNumber);
bool_t found = FALSE;
if (linphone_core_vcard_supported()) {
bctbx_list_t *numbers = linphone_friend_get_phone_numbers(lf);
bctbx_list_t *it = NULL;
for (it = numbers; it != NULL; it = bctbx_list_next(it)) {
const char *value = (const char *)bctbx_list_get_data(it);
char *normalized_value = linphone_proxy_config_normalize_phone_number(cfg, value);
if (normalized_value && strcmp(normalized_value, normalized_phone_number) == 0) {
found = TRUE;
ms_free(normalized_value);
break;
}
if (normalized_value) ms_free(normalized_value);
}
bctbx_list_free(numbers);
}
if (normalized_phone_number) ms_free(normalized_phone_number);
return found;
}
void linphone_friend_remove_phone_number(LinphoneFriend *lf, const char *phone) {
if (!lf || !phone || !lf->vcard) return;
......@@ -995,6 +1028,17 @@ LinphoneFriend *linphone_core_find_friend(const LinphoneCore *lc, const Linphone
return lf;
}
LinphoneFriend *linphone_core_find_friend_by_phone_number(const LinphoneCore *lc, const char* phoneNumber) {
bctbx_list_t *lists = lc->friends_lists;
LinphoneFriend *lf = NULL;
while (lists && !lf) {
LinphoneFriendList *list = (LinphoneFriendList *)bctbx_list_get_data(lists);
lf = linphone_friend_list_find_friend_by_phone_number(list, phoneNumber);
lists = bctbx_list_next(lists);
}
return lf;
}
bctbx_list_t *linphone_core_find_friends(const LinphoneCore *lc, const LinphoneAddress *addr) {
bctbx_list_t *result = NULL;
bctbx_list_t *lists = lc->friends_lists;
......
......@@ -877,6 +877,21 @@ LinphoneFriend * linphone_friend_list_find_friend_by_address(const LinphoneFrien
return lf;
}
LinphoneFriend * linphone_friend_list_find_friend_by_phone_number(const LinphoneFriendList *list, const char *phoneNumber) {
LinphoneFriend *result = NULL;
const bctbx_list_t *elem;
for (elem = list->friends; elem != NULL; elem = bctbx_list_next(elem)) {
LinphoneFriend *lf = (LinphoneFriend *)bctbx_list_get_data(elem);
if (linphone_friend_has_phone_number(lf, phoneNumber)) {
result = lf;
break;
}
}
return result;
}
bctbx_list_t * linphone_friend_list_find_friends_by_address(const LinphoneFriendList *list, const LinphoneAddress *address) {
LinphoneAddress *clean_addr = linphone_address_clone(address);
bctbx_list_t *result = NULL;
......
......@@ -744,7 +744,7 @@ bool_t linphone_proxy_config_is_phone_number(LinphoneProxyConfig *proxy, const c
}
//remove anything but [0-9] and +
static char *flatten_number(const char *number){
static char *linphone_proxy_config_flatten_phone_number(LinphoneProxyConfig *proxy, const char *number) {
char *result=reinterpret_cast<char *>(ms_malloc0(strlen(number)+1));
char *w=result;
const char *r;
......@@ -782,7 +782,7 @@ char* linphone_proxy_config_normalize_phone_number(LinphoneProxyConfig *proxy, c
int ccc = -1;
if (linphone_proxy_config_is_phone_number(tmpproxy, username)){
char * flatten=flatten_number(username);
char * flatten = linphone_proxy_config_flatten_phone_number(proxy, username);
ms_debug("Flattened number is '%s' for '%s'",flatten, username);
ccc = DialPlan::lookupCccFromE164(flatten);
......
......@@ -6059,10 +6059,20 @@ LINPHONE_PUBLIC LINPHONE_DEPRECATED LinphoneFriend *linphone_core_get_friend_by_
* Search a #LinphoneFriend by its address.
* @param[in] lc #LinphoneCore object.
* @param[in] addr The address to use to search the friend.
* @return The #LinphoneFriend object corresponding to the given address.
* @return The #LinphoneFriend object corresponding to the given address or NULL if not found.
* @maybenil
*/
LINPHONE_PUBLIC LinphoneFriend *linphone_core_find_friend(const LinphoneCore *lc, const LinphoneAddress *addr);
/**
* Search a #LinphoneFriend by its phone number.
* @param[in] lc #LinphoneCore object.
* @param[in] phoneNumber The phone number to use to search the friend.
* @return The #LinphoneFriend object corresponding to the given phone number or NULL if not found.
* @maybenil
*/
LINPHONE_PUBLIC LinphoneFriend *linphone_core_find_friend_by_phone_number(const LinphoneCore *lc, const char* phoneNumber);
/**
* Search all #LinphoneFriend matching an address.
* @param[in] lc #LinphoneCore object.
......
......@@ -119,6 +119,14 @@ LINPHONE_PUBLIC void linphone_friend_add_phone_number(LinphoneFriend *lf, const
*/
LINPHONE_PUBLIC bctbx_list_t* linphone_friend_get_phone_numbers(const LinphoneFriend *lf);
/**
* Returns whether a friend contains the given phone number
* @param lf #LinphoneFriend object
* @param phoneNumber the phone number to search for
* @return TRUE if found, FALSE otherwise
*/
LINPHONE_PUBLIC bool_t linphone_friend_has_phone_number(const LinphoneFriend *lf, const char *phoneNumber);
/**
* Removes a phone number in this friend
* @param lf #LinphoneFriend object
......
......@@ -140,9 +140,19 @@ LINPHONE_PUBLIC const bctbx_list_t * linphone_friend_list_get_friends(const Linp
* @param[in] list #LinphoneFriendList object.
* @param[in] address #LinphoneAddress object of the friend we want to search for.
* @return A #LinphoneFriend if found, NULL otherwise.
* @maybenil
**/
LINPHONE_PUBLIC LinphoneFriend * linphone_friend_list_find_friend_by_address(const LinphoneFriendList *list, const LinphoneAddress *address);
/**
* Find a friend in the friend list using a phone number.
* @param[in] list #LinphoneFriendList object.
* @param[in] phoneNumber a string of the phone number for which we want to find a friend.
* @return A #LinphoneFriend if found, NULL otherwise.
* @maybenil
**/
LINPHONE_PUBLIC LinphoneFriend * linphone_friend_list_find_friend_by_phone_number(const LinphoneFriendList *list, const char *phoneNumber);
/**
* Find all friends in the friend list using a LinphoneAddress.
* @param[in] list #LinphoneFriendList object.
......
......@@ -980,6 +980,131 @@ static void search_friend_with_phone_number(void) {
linphone_core_manager_destroy(manager);
}
static void search_friend_with_phone_number_2(void) {
LinphoneCoreManager* manager = linphone_core_manager_new2("chloe_rc", FALSE);
LinphoneFriendList *lfl = linphone_core_get_default_friend_list(manager->lc);
LinphoneFriend *stephanieFriend = linphone_core_create_friend(manager->lc);
LinphoneFriend *laureFriend = linphone_core_create_friend(manager->lc);
LinphoneVcard *stephanieVcard = linphone_factory_create_vcard(linphone_factory_get());
LinphoneVcard *laureVcard = linphone_factory_create_vcard(linphone_factory_get());
const char* stephanieName = {"stephanie de monaco"};
const char* laureName = {"Laure"};
const char* stephaniePhoneNumber = {"0633889977"};
const char* laurePhoneNumber = {"+33641424344"};
linphone_vcard_set_full_name(stephanieVcard, stephanieName);
linphone_vcard_add_phone_number(stephanieVcard, stephaniePhoneNumber);
linphone_friend_set_vcard(stephanieFriend, stephanieVcard);
linphone_core_add_friend(manager->lc, stephanieFriend);
linphone_vcard_set_full_name(laureVcard, laureName);
linphone_vcard_add_phone_number(laureVcard, laurePhoneNumber);
linphone_friend_set_vcard(laureFriend, laureVcard);
linphone_core_add_friend(manager->lc, laureFriend);
LinphoneProxyConfig *lpc = linphone_core_get_default_proxy_config(manager->lc);
BC_ASSERT_PTR_NOT_NULL(lpc);
if (lpc) {
const char *prefix = linphone_proxy_config_get_dial_prefix(lpc);
BC_ASSERT_PTR_NULL(prefix);
}
// Exists as-is
LinphoneFriend *lf = linphone_friend_list_find_friend_by_phone_number(lfl, "+33641424344");
BC_ASSERT_PTR_NOT_NULL(lf);
if (lf) {
BC_ASSERT_PTR_EQUAL(lf, laureFriend);
}
lf = linphone_friend_list_find_friend_by_phone_number(lfl, "+(33) 6 41 42 43 44");
BC_ASSERT_PTR_NOT_NULL(lf);
if (lf) {
BC_ASSERT_PTR_EQUAL(lf, laureFriend);
}
// Can be found by remove the prefix if it is known
lf = linphone_friend_list_find_friend_by_phone_number(lfl, "0641424344");
BC_ASSERT_PTR_NULL(lf);
// Exists as-is
lf = linphone_friend_list_find_friend_by_phone_number(lfl, "0633889977");
BC_ASSERT_PTR_NOT_NULL(lf);
if (lf) {
BC_ASSERT_PTR_EQUAL(lf, stephanieFriend);
}
lf = linphone_friend_list_find_friend_by_phone_number(lfl, "06 33 88 99 77");
BC_ASSERT_PTR_NOT_NULL(lf);
if (lf) {
BC_ASSERT_PTR_EQUAL(lf, stephanieFriend);
}
// Can be found by adding the prefix if it is known
lf = linphone_friend_list_find_friend_by_phone_number(lfl, "+33633889977");
BC_ASSERT_PTR_NULL(lf);
// Doesn't exists
lf = linphone_friend_list_find_friend_by_phone_number(lfl, "0612131415");
BC_ASSERT_PTR_NULL(lf);
lf = linphone_friend_list_find_friend_by_phone_number(lfl, "+33612131415");
BC_ASSERT_PTR_NULL(lf);
lf = linphone_friend_list_find_friend_by_phone_number(lfl, "+ (33) 6 12 13 14 15");
BC_ASSERT_PTR_NULL(lf);
if (lpc) {
linphone_proxy_config_set_dial_prefix(lpc, "33");
const char *prefix = linphone_proxy_config_get_dial_prefix(lpc);
BC_ASSERT_PTR_NOT_NULL(prefix);
if (prefix) {
BC_ASSERT_STRING_EQUAL(prefix, "33");
}
}
// Exists as-is
lf = linphone_core_find_friend_by_phone_number(manager->lc, "+33641424344");
BC_ASSERT_PTR_NOT_NULL(lf);
if (lf) {
BC_ASSERT_PTR_EQUAL(lf, laureFriend);
}
// Can be found by remove the prefix if it is known
lf = linphone_core_find_friend_by_phone_number(manager->lc, "0641424344");
BC_ASSERT_PTR_NOT_NULL(lf);
if (lf) {
BC_ASSERT_PTR_EQUAL(lf, laureFriend);
}
// Exists as-is
lf = linphone_core_find_friend_by_phone_number(manager->lc, "0633889977");
BC_ASSERT_PTR_NOT_NULL(lf);
if (lf) {
BC_ASSERT_PTR_EQUAL(lf, stephanieFriend);
}
// Can be found by adding the prefix if it is known
lf = linphone_core_find_friend_by_phone_number(manager->lc, "+33633889977");
BC_ASSERT_PTR_NOT_NULL(lf);
if (lf) {
BC_ASSERT_PTR_EQUAL(lf, stephanieFriend);
}
// Doesn't exists
lf = linphone_friend_list_find_friend_by_phone_number(lfl, "0612131415");
BC_ASSERT_PTR_NULL(lf);
lf = linphone_friend_list_find_friend_by_phone_number(lfl, "+33612131415");
BC_ASSERT_PTR_NULL(lf);
lf = linphone_friend_list_find_friend_by_phone_number(lfl, "+ (33) 6 12 13 14 15");
BC_ASSERT_PTR_NULL(lf);
linphone_friend_list_remove_friend(lfl, stephanieFriend);
if (stephanieFriend) linphone_friend_unref(stephanieFriend);
if (stephanieVcard) linphone_vcard_unref(stephanieVcard);
linphone_friend_list_remove_friend(lfl, laureFriend);
if (laureFriend) linphone_friend_unref(laureFriend);
if (laureVcard) linphone_vcard_unref(laureVcard);
linphone_core_manager_destroy(manager);
}
static void search_friend_with_presence(void) {
LinphoneMagicSearch *magicSearch = NULL;
bctbx_list_t *resultList = NULL;
......@@ -2005,6 +2130,7 @@ test_t setup_tests[] = {
TEST_ONE_TAG("Multiple looking for friends with the same cache", search_friend_research_estate, "MagicSearch"),
TEST_ONE_TAG("Multiple looking for friends with cache resetting", search_friend_research_estate_reset, "MagicSearch"),
TEST_ONE_TAG("Search friend with phone number", search_friend_with_phone_number, "MagicSearch"),
TEST_NO_TAG("Search friend with phone number 2", search_friend_with_phone_number_2),
TEST_ONE_TAG("Search friend and find it with its presence", search_friend_with_presence, "MagicSearch"),
TEST_ONE_TAG("Search friend in call log", search_friend_in_call_log, "MagicSearch"),
TEST_ONE_TAG("Search friend in call log but don't add address which already exist", search_friend_in_call_log_already_exist, "MagicSearch"),
......
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