From ce6e236b5b81773f270605a55b74cbf395542639 Mon Sep 17 00:00:00 2001 From: Erwan Croze Date: Mon, 21 May 2018 14:01:48 +0200 Subject: [PATCH] Changing some method in the magic search bar --- src/search/magic-search.cpp | 88 +++++++++++++++------- src/search/magic-search.h | 5 +- src/search/search-result.cpp | 1 + tester/setup_tester.c | 141 ++++++++++++++++++++++++++++++++++- 4 files changed, 205 insertions(+), 30 deletions(-) diff --git a/src/search/magic-search.cpp b/src/search/magic-search.cpp index 3c8c92379..ec051baca 100644 --- a/src/search/magic-search.cpp +++ b/src/search/magic-search.cpp @@ -151,6 +151,7 @@ list MagicSearch::getContactListFromFilter(const string &filter, c string filterAddress = "sip:" + filter + "@" + domain; LinphoneAddress *lastResult = linphone_core_create_address(this->getCore()->getCCore(), filterAddress.c_str()); if (lastResult) returnList.push_back(SearchResult(0, lastResult, "", nullptr)); + linphone_address_unref(lastResult); } } @@ -182,26 +183,39 @@ list MagicSearch::getAllFriends() { bctbx_list_t *lPhoneNumbers = linphone_friend_get_phone_numbers(lFriend); string lPhoneNumber = (lPhoneNumbers != nullptr) ? static_cast(lPhoneNumbers->data) : ""; - if (lAddress) linphone_address_ref(const_cast(lAddress)); returnList.push_back(SearchResult(1, lAddress, lPhoneNumber, lFriend)); } + returnList.sort([](const SearchResult& lsr, const SearchResult& rsr){ + unsigned int cpt = 0; + string name1 = linphone_friend_get_name(lsr.getFriend()); + string name2 = linphone_friend_get_name(rsr.getFriend()); + unsigned int char1 = tolower(name1.at(cpt)); + unsigned int char2 = tolower(name2.at(cpt)); + + while (name1.size() > cpt && name2.size() > cpt) { + if (char1 < char2) { + return true; + } else if (char1 > char2) { + return false; + } + cpt++; + } + return name1.size() < name2.size(); + }); + return returnList; } list *MagicSearch::beginNewSearch(const string &filter, const string &withDomain) { list *resultList = new list(); - LinphoneFriendList *list = linphone_core_get_default_friend_list(this->getCore()->getCCore()); + LinphoneFriendList *fList = linphone_core_get_default_friend_list(this->getCore()->getCCore()); const bctbx_list_t *callLog = linphone_core_get_call_logs(this->getCore()->getCCore()); // For all friends or when we reach the search limit - for (bctbx_list_t *f = list->friends ; f != nullptr ; f = bctbx_list_next(f)) { - SearchResult result = searchInFriend(reinterpret_cast(f->data), filter, withDomain); - if (result.getWeight() > getMinWeight()) { - if (result.getAddress() || !result.getPhoneNumber().empty()) { - resultList->push_back(result); - } - } + for (bctbx_list_t *f = fList->friends ; f != nullptr ; f = bctbx_list_next(f)) { + list results = searchInFriend(reinterpret_cast(f->data), filter, withDomain); + addResultsToResultsList(results, *resultList); } // For all call log or when we reach the search limit @@ -212,8 +226,6 @@ list *MagicSearch::beginNewSearch(const string &filter, const stri if (addr) { unsigned int weight = searchInAddress(addr, filter, withDomain); if (weight > getMinWeight()) { - // FIXME: Ugly temporary workaround to solve weak. Remove me later. - linphone_address_ref(const_cast(addr)); resultList->push_back(SearchResult(weight, addr, "", nullptr)); } } @@ -229,10 +241,8 @@ list *MagicSearch::continueSearch(const string &filter, const stri for (const auto sr : *cacheList) { if (sr.getAddress() || !sr.getPhoneNumber().empty()) { if (sr.getFriend()) { - SearchResult result = searchInFriend(sr.getFriend(), filter, withDomain); - if (result.getWeight() > getMinWeight()) { - resultList->push_back(result); - } + list results = searchInFriend(sr.getFriend(), filter, withDomain); + addResultsToResultsList(results, *resultList); } else { unsigned int weight = searchInAddress(sr.getAddress(), filter, withDomain); if (weight > getMinWeight()) { @@ -245,14 +255,10 @@ list *MagicSearch::continueSearch(const string &filter, const stri return resultList; } -SearchResult MagicSearch::searchInFriend(const LinphoneFriend *lFriend, const string &filter, const string &withDomain) { +list MagicSearch::searchInFriend(const LinphoneFriend *lFriend, const string &filter, const string &withDomain) { + list friendResult; string phoneNumber = ""; unsigned int weight = getMinWeight(); - const LinphoneAddress* lAddress = linphone_friend_get_address(lFriend); - - if (!checkDomain(lFriend, lAddress, withDomain)) { - if (!withDomain.empty()) return SearchResult(weight, nullptr, ""); - } // NAME if (linphone_core_vcard_supported()) { @@ -262,7 +268,22 @@ SearchResult MagicSearch::searchInFriend(const LinphoneFriend *lFriend, const st } //SIP URI - weight += searchInAddress(lAddress, filter, withDomain) * 1; + for (const bctbx_list_t *listAddress = linphone_friend_get_addresses(lFriend); + listAddress != nullptr && listAddress->data != nullptr; + listAddress = listAddress->next) { + const LinphoneAddress *lAddress = static_cast(listAddress->data); + if (!checkDomain(lFriend, lAddress, withDomain)) { + if (!withDomain.empty()) { + continue; + } + } + + unsigned int weightAddress = searchInAddress(lAddress, filter, withDomain) * 1; + + if ((weightAddress + weight) > getMinWeight()) { + friendResult.push_back(SearchResult(weight + weightAddress, lAddress, phoneNumber, lFriend)); + } + } // PHONE NUMBER LinphoneProxyConfig *proxy = linphone_core_get_default_proxy_config(this->getCore()->getCCore()); @@ -273,21 +294,24 @@ SearchResult MagicSearch::searchInFriend(const LinphoneFriend *lFriend, const st const LinphonePresenceModel *presence = linphone_friend_get_presence_model_for_uri_or_tel(lFriend, number.c_str()); phoneNumber = number; if (proxy) { - phoneNumber = linphone_proxy_config_normalize_phone_number(proxy, phoneNumber.c_str()); + char * buff = linphone_proxy_config_normalize_phone_number(proxy, phoneNumber.c_str()); + phoneNumber = buff; + bctbx_free(buff); } - weight += getWeight(phoneNumber.c_str(), filter); + unsigned int weightNumber = getWeight(phoneNumber.c_str(), filter); if (presence) { char *contact = linphone_presence_model_get_contact(presence); - weight += getWeight(contact, filter) * 2; + weightNumber += getWeight(contact, filter) * 2; bctbx_free(contact); } + if (weightNumber > getMinWeight()) { + friendResult.push_back(SearchResult(weight + weightNumber, linphone_friend_get_address(lFriend), phoneNumber, lFriend)); + } phoneNumbers = phoneNumbers->next; } if (begin) bctbx_list_free(begin); - // FIXME: Ugly temporary workaround to solve weak. Remove me later. - if (lAddress) linphone_address_ref(const_cast(lAddress)); - return SearchResult(weight, lAddress, phoneNumber, lFriend); + return friendResult; } unsigned int MagicSearch::searchInAddress(const LinphoneAddress *lAddress, const string &filter, const string &withDomain) { @@ -371,4 +395,12 @@ bool MagicSearch::checkDomain(const LinphoneFriend *lFriend, const LinphoneAddre return soFarSoGood; } +void MagicSearch::addResultsToResultsList(std::list &results, std::list &srL) { + if (results.size() > 0) { + for (auto r : results) { + srL.push_back(r); + } + } +} + LINPHONE_END_NAMESPACE diff --git a/src/search/magic-search.h b/src/search/magic-search.h index 7020405c0..ac6a30e30 100644 --- a/src/search/magic-search.h +++ b/src/search/magic-search.h @@ -168,9 +168,10 @@ private: * @param[in] lFriend friend whose informations will be check * @param[in] filter word we search * @param[in] withDomain domain which we want to search only + * @return list of result from friend * @private **/ - SearchResult searchInFriend(const LinphoneFriend* lFriend, const std::string &filter, const std::string &withDomain); + std::list searchInFriend(const LinphoneFriend* lFriend, const std::string &filter, const std::string &withDomain); /** * Search informations in address given @@ -199,6 +200,8 @@ private: **/ bool checkDomain(const LinphoneFriend* lFriend, const LinphoneAddress *lAddress, const std::string &withDomain) const; + void addResultsToResultsList(std::list &results, std::list &srL); + L_DECLARE_PRIVATE(MagicSearch); }; diff --git a/src/search/search-result.cpp b/src/search/search-result.cpp index d8b78adea..c7494768a 100644 --- a/src/search/search-result.cpp +++ b/src/search/search-result.cpp @@ -42,6 +42,7 @@ SearchResult::SearchResult(const unsigned int weight, const LinphoneAddress *a, L_D(); d->mWeight = weight; d->mAddress = a; + if (d->mAddress) linphone_address_ref(const_cast(d->mAddress)); d->mFriend = f; d->mPhoneNumber = pn; } diff --git a/tester/setup_tester.c b/tester/setup_tester.c index dfd74b6ec..abf5f3fe9 100644 --- a/tester/setup_tester.c +++ b/tester/setup_tester.c @@ -69,7 +69,8 @@ static void _check_friend_result_list(LinphoneCore *lc, const bctbx_list_t *resu const LinphoneSearchResult *sr = bctbx_list_nth_data(resultList, index); const LinphoneFriend *lf = linphone_search_result_get_friend(sr); if (lf) { - const LinphoneAddress *la = linphone_friend_get_address(lf); + const LinphoneAddress *la = (linphone_search_result_get_address(sr)) ? + linphone_search_result_get_address(sr) : linphone_friend_get_address(lf); if (la) { char* fa = linphone_address_as_string(la); BC_ASSERT_STRING_EQUAL(fa , uri); @@ -482,6 +483,91 @@ static void custom_tones_setup(void){ linphone_core_manager_destroy(mgr); } +static void search_friend_in_alphabetical_order(void) { + LinphoneMagicSearch *magicSearch = NULL; + bctbx_list_t *resultList = NULL; + LinphoneCoreManager* manager = linphone_core_manager_new2("empty_rc", FALSE); + LinphoneFriendList *lfl = linphone_core_get_default_friend_list(manager->lc); + + const char *name1SipUri = {"sip:toto@sip.example.org"}; + const char *name2SipUri = {"sip:stephanie@sip.example.org"}; + const char *name3SipUri = {"sip:alber@sip.example.org"}; + const char *name4SipUri = {"sip:gauthier@sip.example.org"}; + + LinphoneFriend *friend1 = linphone_core_create_friend(manager->lc); + LinphoneFriend *friend2 = linphone_core_create_friend(manager->lc); + LinphoneFriend *friend3 = linphone_core_create_friend(manager->lc); + LinphoneFriend *friend4 = linphone_core_create_friend(manager->lc); + + LinphoneVcard *vcard1 = linphone_factory_create_vcard(linphone_factory_get()); + LinphoneVcard *vcard2 = linphone_factory_create_vcard(linphone_factory_get()); + LinphoneVcard *vcard3 = linphone_factory_create_vcard(linphone_factory_get()); + LinphoneVcard *vcard4 = linphone_factory_create_vcard(linphone_factory_get()); + + const char *name1 = {"STEPHANIE delarue"}; + const char *name2 = {"alias delarue"}; + const char *name3 = {"Alber josh"}; + const char *name4 = {"gauthier wei"}; + + linphone_vcard_set_full_name(vcard1, name1); // STEPHANIE delarue + linphone_vcard_set_url(vcard1, name1SipUri); //sip:toto@sip.example.org + linphone_vcard_add_sip_address(vcard1, name1SipUri); + linphone_friend_set_vcard(friend1, vcard1); + linphone_core_add_friend(manager->lc, friend1); + + linphone_vcard_set_full_name(vcard2, name2); // alias delarue + linphone_vcard_set_url(vcard2, name2SipUri); //sip:stephanie@sip.example.org + linphone_vcard_add_sip_address(vcard2, name2SipUri); + linphone_friend_set_vcard(friend2, vcard2); + linphone_core_add_friend(manager->lc, friend2); + + linphone_vcard_set_full_name(vcard3, name3); // Alber josh + linphone_vcard_set_url(vcard3, name3SipUri); //sip:alber@sip.example.org + linphone_vcard_add_sip_address(vcard3, name3SipUri); + linphone_friend_set_vcard(friend3, vcard3); + linphone_core_add_friend(manager->lc, friend3); + + linphone_vcard_set_full_name(vcard4, name4); // gauthier wei + linphone_vcard_set_url(vcard4, name4SipUri); //sip:gauthier@sip.example.org + linphone_vcard_add_sip_address(vcard4, name4SipUri); + linphone_friend_set_vcard(friend4, vcard4); + linphone_core_add_friend(manager->lc, friend4); + + magicSearch = linphone_magic_search_new(manager->lc); + + resultList = linphone_magic_search_get_contact_list_from_filter(magicSearch, "", ""); + + if (BC_ASSERT_PTR_NOT_NULL(resultList)) { + BC_ASSERT_EQUAL(bctbx_list_size(resultList), 4, int, "%d"); + _check_friend_result_list(manager->lc, resultList, 0, name3SipUri, NULL);//"sip:stephanie@sip.example.org" + _check_friend_result_list(manager->lc, resultList, 1, name2SipUri, NULL);//"sip:alber@sip.example.org" + _check_friend_result_list(manager->lc, resultList, 2, name4SipUri, NULL);//"sip:gauthier@sip.example.org" + _check_friend_result_list(manager->lc, resultList, 3, name1SipUri, NULL);//"sip:toto@sip.example.org" + bctbx_list_free_with_data(resultList, (bctbx_list_free_func)linphone_magic_search_unref); + } + + linphone_magic_search_reset_search_cache(magicSearch); + + linphone_friend_list_remove_friend(lfl, friend1); + linphone_friend_list_remove_friend(lfl, friend2); + linphone_friend_list_remove_friend(lfl, friend3); + linphone_friend_list_remove_friend(lfl, friend4); + + if (friend1) linphone_friend_unref(friend1); + if (friend2) linphone_friend_unref(friend2); + if (friend3) linphone_friend_unref(friend3); + if (friend4) linphone_friend_unref(friend4); + + if (vcard1) linphone_vcard_unref(vcard1); + if (vcard2) linphone_vcard_unref(vcard2); + if (vcard3) linphone_vcard_unref(vcard3); + if (vcard4) linphone_vcard_unref(vcard4); + + linphone_magic_search_unref(magicSearch); + linphone_core_manager_destroy(manager); + +} + static void search_friend_without_filter(void) { LinphoneMagicSearch *magicSearch = NULL; bctbx_list_t *resultList = NULL; @@ -968,6 +1054,57 @@ static void search_friend_with_name_with_uppercase(void) { linphone_core_manager_destroy(manager); } +static void search_friend_with_multiple_sip_address(void) { + LinphoneMagicSearch *magicSearch = NULL; + bctbx_list_t *resultList = NULL; + LinphoneCoreManager* manager = linphone_core_manager_new2("empty_rc", FALSE); + LinphoneFriendList *lfl = linphone_core_get_default_friend_list(manager->lc); + const char *stephanieSipUri1 = {"sip:toto@sip.example.org"}; + const char *stephanieSipUri2 = {"sip:stephanie@sip.example.org"}; + LinphoneFriend *stephanieFriend = linphone_core_create_friend(manager->lc); + LinphoneVcard *stephanieVcard = linphone_factory_create_vcard(linphone_factory_get()); + const char *stephanieName = {"stephanie delarue"}; + + _create_friends_from_tab(manager->lc, lfl, sFriends, sSizeFriend); + + linphone_vcard_set_full_name(stephanieVcard, stephanieName); // stephanie delarue + linphone_vcard_set_url(stephanieVcard, stephanieSipUri1); //sip:toto@sip.example.org + linphone_vcard_add_sip_address(stephanieVcard, stephanieSipUri1); + linphone_vcard_add_sip_address(stephanieVcard, stephanieSipUri2); + linphone_friend_set_vcard(stephanieFriend, stephanieVcard); + linphone_core_add_friend(manager->lc, stephanieFriend); + + magicSearch = linphone_magic_search_new(manager->lc); + + resultList = linphone_magic_search_get_contact_list_from_filter(magicSearch, "stephanie", ""); + + if (BC_ASSERT_PTR_NOT_NULL(resultList)) { + BC_ASSERT_EQUAL(bctbx_list_size(resultList), 2, int, "%d"); + _check_friend_result_list(manager->lc, resultList, 0, stephanieSipUri2, NULL);//"sip:stephanie@sip.example.org" + _check_friend_result_list(manager->lc, resultList, 1, stephanieSipUri1, NULL);//"sip:toto@sip.example.org" + bctbx_list_free_with_data(resultList, (bctbx_list_free_func)linphone_magic_search_unref); + } + + linphone_magic_search_reset_search_cache(magicSearch); + + resultList = linphone_magic_search_get_contact_list_from_filter(magicSearch, "delarue", ""); + + if (BC_ASSERT_PTR_NOT_NULL(resultList)) { + BC_ASSERT_EQUAL(bctbx_list_size(resultList), 2, int, "%d"); + _check_friend_result_list(manager->lc, resultList, 0, stephanieSipUri2, NULL);//"sip:stephanie@sip.example.org" + _check_friend_result_list(manager->lc, resultList, 1, stephanieSipUri1, NULL);//"sip:toto@sip.example.org" + bctbx_list_free_with_data(resultList, (bctbx_list_free_func)linphone_magic_search_unref); + } + + _remove_friends_from_list(lfl, sFriends, sSizeFriend); + linphone_friend_list_remove_friend(lfl, stephanieFriend); + if (stephanieFriend) linphone_friend_unref(stephanieFriend); + if (stephanieVcard) linphone_vcard_unref(stephanieVcard); + + linphone_magic_search_unref(magicSearch); + linphone_core_manager_destroy(manager); +} + static void search_friend_large_database(void) { char *dbPath = bc_tester_res("db/friends.db"); char *searchedFriend = "6295103032641994169"; @@ -1013,6 +1150,7 @@ test_t setup_tests[] = { TEST_NO_TAG("Codec usability", codec_usability_test), TEST_NO_TAG("Codec setup", codec_setup), TEST_NO_TAG("Custom tones setup", custom_tones_setup), + TEST_ONE_TAG("Return friend list in alphabetical order", search_friend_in_alphabetical_order, "MagicSearch"), TEST_ONE_TAG("Search friend without filter", search_friend_without_filter, "MagicSearch"), TEST_ONE_TAG("Search friend from all domains", search_friend_all_domains, "MagicSearch"), TEST_ONE_TAG("Search friend from one domain", search_friend_one_domain, "MagicSearch"), @@ -1024,6 +1162,7 @@ test_t setup_tests[] = { TEST_ONE_TAG("Search friend last item is the filter", search_friend_last_item_is_filter, "MagicSearch"), TEST_ONE_TAG("Search friend with name", search_friend_with_name, "MagicSearch"), TEST_ONE_TAG("Search friend with uppercase name", search_friend_with_name_with_uppercase, "MagicSearch"), + TEST_ONE_TAG("Search friend with multiple sip address", search_friend_with_multiple_sip_address, "MagicSearch"), TEST_ONE_TAG("Search friend in large friends database", search_friend_large_database, "MagicSearch") };