diff --git a/include/linphone/api/c-magic-search.h b/include/linphone/api/c-magic-search.h index 1bd04a72f..e45bc2605 100644 --- a/include/linphone/api/c-magic-search.h +++ b/include/linphone/api/c-magic-search.h @@ -121,7 +121,7 @@ LINPHONE_PUBLIC void linphone_magic_search_reset_search_cache(LinphoneMagicSearc * Create a sorted list of SearchResult from SipUri, Contact name, * Contact displayname, Contact phone number, which match with a filter word * The last item list will be an address formed with "filter" if a proxy config exist - * During the first search, a cache is created and used for the next search and so on + * During the first search, a cache is created and used for the next search * Use linphone_magic_search_reset_search_cache() to begin a new search * @param[in] filter word we search * @param[in] withDomain domain which we want to search only diff --git a/src/search/magic-search.cpp b/src/search/magic-search.cpp index 13ebc6e41..a6a4e197e 100644 --- a/src/search/magic-search.cpp +++ b/src/search/magic-search.cpp @@ -116,15 +116,14 @@ void MagicSearch::resetSearchCache() { list MagicSearch::getContactListFromFilter(const string &filter, const string &withDomain) { list *resultList; + list returnList; LinphoneProxyConfig *proxy = nullptr; if (filter.empty()) return list(); - // We begin a new search if the last result size is superior or equal of the search limit size - if (getSearchCache() != nullptr && - (getLimitedSearch() && getSearchLimit() > getSearchCache()->size()) - ) { + if (getSearchCache() != nullptr) { resultList = continueSearch(filter, withDomain); + resetSearchCache(); } else { resultList = beginNewSearch(filter, withDomain); } @@ -133,6 +132,15 @@ list MagicSearch::getContactListFromFilter(const string &filter, c return (!rsr.getFriend() && lsr.getFriend()) || lsr >= rsr; }); + setSearchCache(resultList); + returnList = *resultList; + + if (getLimitedSearch() && returnList.size() > getSearchLimit()) { + auto limitIterator = returnList.begin(); + advance(limitIterator, getSearchLimit()); + returnList.erase(limitIterator, returnList.cend()); + } + proxy = linphone_core_get_default_proxy_config(this->getCore()->getCCore()); // Adding last item if proxy exist if (proxy) { @@ -140,12 +148,11 @@ list MagicSearch::getContactListFromFilter(const string &filter, c if (domain) { string filterAddress = "sip:" + filter + "@" + domain; LinphoneAddress *lastResult = linphone_core_create_address(this->getCore()->getCCore(), filterAddress.c_str()); - if (lastResult) resultList->push_back(SearchResult(0, lastResult, nullptr)); + if (lastResult) returnList.push_back(SearchResult(0, lastResult, nullptr)); } } - setSearchCache(resultList); - return *resultList; + return returnList; } ///////////////////// @@ -169,10 +176,7 @@ list *MagicSearch::beginNewSearch(const string &filter, const stri 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 && (!getLimitedSearch() || resultList->size() < getSearchLimit()); - f = bctbx_list_next(f) - ) { + 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()) { resultList->push_back(result); @@ -180,10 +184,7 @@ list *MagicSearch::beginNewSearch(const string &filter, const stri } // For all call log or when we reach the search limit - for (const bctbx_list_t *f = callLog; - f != nullptr && (!getLimitedSearch() || resultList->size() < getSearchLimit()); - f = bctbx_list_next(f) - ) { + for (const bctbx_list_t *f = callLog ; f != nullptr ; f = bctbx_list_next(f)) { LinphoneCallLog *log = reinterpret_cast(f->data); const LinphoneAddress *addr = (linphone_call_log_get_dir(log) == LinphoneCallDir::LinphoneCallIncoming) ? linphone_call_log_get_from_address(log) : linphone_call_log_get_to_address(log); @@ -269,7 +270,7 @@ unsigned int MagicSearch::getWeight(const string &stringWords, const string &fil for (size_t w = stringWords.find(filter); w != string::npos; w = stringWords.find(filter, w + filter.length()) - ) { + ) { // weight max if occurence find at beginning if (w == 0) { weight = getMaxWeight(); diff --git a/src/search/magic-search.h b/src/search/magic-search.h index 3c7331fef..7fa34f294 100644 --- a/src/search/magic-search.h +++ b/src/search/magic-search.h @@ -114,7 +114,7 @@ public: * Create a sorted list of SearchResult from SipUri, Contact name, * Contact displayname, Contact phone number, which match with a filter word * The last item list will be an address formed with "filter" if a proxy config exist - * During the first search, a cache is created and used for the next search and so on + * During the first search, a cache is created and used for the next search * Use resetSearchCache() to begin a new search * @param[in] filter word we search * @param[in] withDomain