diff --git a/src/search/magic-search.cpp b/src/search/magic-search.cpp index a5333c79a..d2d6982a9 100644 --- a/src/search/magic-search.cpp +++ b/src/search/magic-search.cpp @@ -20,6 +20,7 @@ #include "magic-search-p.h" #include +#include #include "c-wrapper/internal/c-tools.h" #include "linphone/utils/utils.h" @@ -119,7 +120,7 @@ list MagicSearch::getContactListFromFilter(const string &filter, c list returnList; LinphoneProxyConfig *proxy = nullptr; - if (filter.empty()) return list(); + if (filter.empty()) return getAllFriends(); if (getSearchCache() != nullptr) { resultList = continueSearch(filter, withDomain); @@ -170,6 +171,21 @@ void MagicSearch::setSearchCache(list *cache) { d->mCacheResult = cache; } +list MagicSearch::getAllFriends() { + list returnList; + LinphoneFriendList *list = linphone_core_get_default_friend_list(this->getCore()->getCCore()); + + for (bctbx_list_t *f = list->friends ; f != nullptr ; f = bctbx_list_next(f)) { + const LinphoneFriend *lFriend = reinterpret_cast(f->data); + const LinphoneAddress* lAddress = linphone_friend_get_address(lFriend); + + if (lAddress) linphone_address_ref(const_cast(lAddress)); + returnList.push_back(SearchResult(1, lAddress, lFriend)); + } + + 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()); @@ -270,12 +286,18 @@ unsigned int MagicSearch::searchInAddress(const LinphoneAddress *lAddress, const } unsigned int MagicSearch::getWeight(const string &stringWords, const string &filter) const { + locale loc; + string filterLC = filter; + string stringWordsLC = stringWords; size_t weight = string::npos; - // Finding all occurrences of "filter" in "stringWords" - for (size_t w = stringWords.find(filter); + transform(stringWordsLC.begin(), stringWordsLC.end(), stringWordsLC.begin(), [](unsigned char c){ return tolower(c); }); + transform(filterLC.begin(), filterLC.end(), filterLC.begin(), [](unsigned char c){ return tolower(c); }); + + // Finding all occurrences of "filterLC" in "stringWordsLC" + for (size_t w = stringWordsLC.find(filterLC); w != string::npos; - w = stringWords.find(filter, w + filter.length()) + w = stringWordsLC.find(filterLC, w + filterLC.length()) ) { // weight max if occurence find at beginning if (w == 0) { @@ -283,8 +305,8 @@ unsigned int MagicSearch::getWeight(const string &stringWords, const string &fil } else { bool isDelimiter = false; if (getUseDelimiter()) { - // get the char before the matched filter - const char l = stringWords.at(w - 1); + // get the char before the matched filterLC + const char l = stringWordsLC.at(w - 1); // Check if it's a delimiter for (const char d : getDelimiter()) { if (l == d) { @@ -296,7 +318,7 @@ unsigned int MagicSearch::getWeight(const string &stringWords, const string &fil unsigned int newWeight = getMaxWeight() - (unsigned int)((isDelimiter) ? 1 : w + 1); weight = (weight != string::npos) ? weight + newWeight : newWeight; } - // Only one search on the stringWords for the moment + // Only one search on the stringWordsLC for the moment // due to weight calcul which dos not take into the case of multiple occurence break; } diff --git a/src/search/magic-search.h b/src/search/magic-search.h index 7fa34f294..7020405c0 100644 --- a/src/search/magic-search.h +++ b/src/search/magic-search.h @@ -140,6 +140,13 @@ private: **/ void setSearchCache(std::list *cache); + /** + * Get all friends as SearchResult + * @return all friends in a SearchResult list + * @private + **/ + std::list getAllFriends(); + /** * Begin the search from friend list * @param[in] filter word we search diff --git a/tester/setup_tester.c b/tester/setup_tester.c index 111a1136c..9cfe33e81 100644 --- a/tester/setup_tester.c +++ b/tester/setup_tester.c @@ -482,6 +482,29 @@ static void custom_tones_setup(void){ linphone_core_manager_destroy(mgr); } +static void search_friend_without_filter(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); + + _create_friends_from_tab(manager->lc, lfl, sFriends, sSizeFriend); + + 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), S_SIZE_FRIEND, int, "%d"); + bctbx_list_free_with_data(resultList, (bctbx_list_free_func)linphone_magic_search_unref); + } + + _remove_friends_from_list(lfl, sFriends, sSizeFriend); + + linphone_magic_search_unref(magicSearch); + linphone_core_manager_destroy(manager); +} + static void search_friend_all_domains(void) { LinphoneMagicSearch *magicSearch = NULL; bctbx_list_t *resultList = NULL; @@ -846,6 +869,59 @@ static void search_friend_with_name(void) { linphone_core_manager_destroy(manager); } +static void search_friend_with_name_with_uppercase(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 *stephanie1SipUri = {"sip:toto@sip.example.org"}; + const char *stephanie2SipUri = {"sip:stephanie@sip.example.org"}; + LinphoneFriend *stephanie1Friend = linphone_core_create_friend(manager->lc); + LinphoneFriend *stephanie2Friend = linphone_core_create_friend(manager->lc); + LinphoneVcard *stephanie1Vcard = linphone_factory_create_vcard(linphone_factory_get()); + LinphoneVcard *stephanie2Vcard = linphone_factory_create_vcard(linphone_factory_get()); + const char *stephanie1Name = {"STEPHANIE delarue"}; + const char *stephanie2Name = {"alias delarue"}; + + _create_friends_from_tab(manager->lc, lfl, sFriends, sSizeFriend); + + linphone_vcard_set_full_name(stephanie1Vcard, stephanie1Name); // STEPHANIE delarue + linphone_vcard_set_url(stephanie1Vcard, stephanie1SipUri); //sip:toto@sip.example.org + linphone_vcard_add_sip_address(stephanie1Vcard, stephanie1SipUri); + linphone_friend_set_vcard(stephanie1Friend, stephanie1Vcard); + linphone_core_add_friend(manager->lc, stephanie1Friend); + + linphone_vcard_set_full_name(stephanie2Vcard, stephanie2Name); // alias delarue + linphone_vcard_set_url(stephanie2Vcard, stephanie2SipUri); //sip:stephanie@sip.example.org + linphone_vcard_add_sip_address(stephanie2Vcard, stephanie2SipUri); + linphone_friend_set_vcard(stephanie2Friend, stephanie2Vcard); + linphone_core_add_friend(manager->lc, stephanie2Friend); + + 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, stephanie1SipUri, NULL);//"sip:toto@sip.example.org" + _check_friend_result_list(manager->lc, resultList, 1, stephanie2SipUri, NULL);//"sip:stephanie@sip.example.org" + bctbx_list_free_with_data(resultList, (bctbx_list_free_func)linphone_magic_search_unref); + } + + linphone_magic_search_reset_search_cache(magicSearch); + + _remove_friends_from_list(lfl, sFriends, sSizeFriend); + linphone_friend_list_remove_friend(lfl, stephanie1Friend); + linphone_friend_list_remove_friend(lfl, stephanie2Friend); + if (stephanie1Friend) linphone_friend_unref(stephanie1Friend); + if (stephanie2Friend) linphone_friend_unref(stephanie2Friend); + if (stephanie1Vcard) linphone_vcard_unref(stephanie1Vcard); + if (stephanie2Vcard) linphone_vcard_unref(stephanie2Vcard); + + 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"; @@ -891,6 +967,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("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"), TEST_ONE_TAG("Multiple looking for friends with the same cache", search_friend_research_estate, "MagicSearch"), @@ -900,6 +977,7 @@ test_t setup_tests[] = { TEST_ONE_TAG("Search friend in call log", search_friend_in_call_log, "MagicSearch"), 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 in large friends database", search_friend_large_database, "MagicSearch") };