diff --git a/include/linphone/api/c-search-result.h b/include/linphone/api/c-search-result.h index 7a4a62485..8730d90ec 100644 --- a/include/linphone/api/c-search-result.h +++ b/include/linphone/api/c-search-result.h @@ -51,6 +51,11 @@ LINPHONE_PUBLIC const LinphoneFriend* linphone_search_result_get_friend(const Li **/ LINPHONE_PUBLIC const LinphoneAddress* linphone_search_result_get_address(const LinphoneSearchResult *searchResult); +/** + * @return Phone Number associed + **/ +LINPHONE_PUBLIC const char* linphone_search_result_get_phone_number(const LinphoneSearchResult *searchResult); + /** * @return the result weight **/ diff --git a/src/c-wrapper/api/c-search-result.cpp b/src/c-wrapper/api/c-search-result.cpp index 77cd38c13..295c0edeb 100644 --- a/src/c-wrapper/api/c-search-result.cpp +++ b/src/c-wrapper/api/c-search-result.cpp @@ -39,6 +39,10 @@ const LinphoneAddress* linphone_search_result_get_address(const LinphoneSearchRe return L_GET_CPP_PTR_FROM_C_OBJECT(searchResult)->getAddress(); } +const char* linphone_search_result_get_phone_number(const LinphoneSearchResult *searchResult) { + return L_STRING_TO_C(L_GET_CPP_PTR_FROM_C_OBJECT(searchResult)->getPhoneNumber()); +} + unsigned int linphone_search_result_get_weight(const LinphoneSearchResult *searchResult) { return L_GET_CPP_PTR_FROM_C_OBJECT(searchResult)->getWeight(); } diff --git a/src/search/magic-search.cpp b/src/search/magic-search.cpp index 65499075c..3c8c92379 100644 --- a/src/search/magic-search.cpp +++ b/src/search/magic-search.cpp @@ -26,6 +26,7 @@ #include "linphone/utils/utils.h" #include "linphone/core.h" #include "linphone/types.h" +#include "logger/logger.h" #include "private.h" using namespace std; @@ -149,7 +150,7 @@ 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) returnList.push_back(SearchResult(0, lastResult, nullptr)); + if (lastResult) returnList.push_back(SearchResult(0, lastResult, "", nullptr)); } } @@ -178,9 +179,11 @@ list MagicSearch::getAllFriends() { 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); + 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, lFriend)); + returnList.push_back(SearchResult(1, lAddress, lPhoneNumber, lFriend)); } return returnList; @@ -195,7 +198,9 @@ list *MagicSearch::beginNewSearch(const string &filter, const stri 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); + if (result.getAddress() || !result.getPhoneNumber().empty()) { + resultList->push_back(result); + } } } @@ -209,7 +214,7 @@ list *MagicSearch::beginNewSearch(const string &filter, const stri 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)); + resultList->push_back(SearchResult(weight, addr, "", nullptr)); } } } @@ -222,10 +227,17 @@ list *MagicSearch::continueSearch(const string &filter, const stri const list *cacheList = getSearchCache(); for (const auto sr : *cacheList) { - if (sr.getFriend()) { - SearchResult result = searchInFriend(sr.getFriend(), filter, withDomain); - if (result.getWeight() > getMinWeight()) { - resultList->push_back(result); + if (sr.getAddress() || !sr.getPhoneNumber().empty()) { + if (sr.getFriend()) { + SearchResult result = searchInFriend(sr.getFriend(), filter, withDomain); + if (result.getWeight() > getMinWeight()) { + resultList->push_back(result); + } + } else { + unsigned int weight = searchInAddress(sr.getAddress(), filter, withDomain); + if (weight > getMinWeight()) { + resultList->push_back(SearchResult(weight, sr.getAddress(), sr.getPhoneNumber(), nullptr)); + } } } } @@ -234,11 +246,12 @@ list *MagicSearch::continueSearch(const string &filter, const stri } SearchResult MagicSearch::searchInFriend(const LinphoneFriend *lFriend, const string &filter, const string &withDomain) { + 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); + if (!withDomain.empty()) return SearchResult(weight, nullptr, ""); } // NAME @@ -252,12 +265,17 @@ SearchResult MagicSearch::searchInFriend(const LinphoneFriend *lFriend, const st weight += searchInAddress(lAddress, filter, withDomain) * 1; // PHONE NUMBER + LinphoneProxyConfig *proxy = linphone_core_get_default_proxy_config(this->getCore()->getCCore()); bctbx_list_t *begin, *phoneNumbers = linphone_friend_get_phone_numbers(lFriend); begin = phoneNumbers; while (phoneNumbers && phoneNumbers->data) { string number = static_cast(phoneNumbers->data); const LinphonePresenceModel *presence = linphone_friend_get_presence_model_for_uri_or_tel(lFriend, number.c_str()); - weight += getWeight(number, filter); + phoneNumber = number; + if (proxy) { + phoneNumber = linphone_proxy_config_normalize_phone_number(proxy, phoneNumber.c_str()); + } + weight += getWeight(phoneNumber.c_str(), filter); if (presence) { char *contact = linphone_presence_model_get_contact(presence); weight += getWeight(contact, filter) * 2; @@ -269,7 +287,7 @@ SearchResult MagicSearch::searchInFriend(const LinphoneFriend *lFriend, const st // FIXME: Ugly temporary workaround to solve weak. Remove me later. if (lAddress) linphone_address_ref(const_cast(lAddress)); - return SearchResult(weight, lAddress, lFriend); + return SearchResult(weight, lAddress, phoneNumber, lFriend); } unsigned int MagicSearch::searchInAddress(const LinphoneAddress *lAddress, const string &filter, const string &withDomain) { @@ -328,7 +346,7 @@ unsigned int MagicSearch::getWeight(const string &stringWords, const string &fil return (weight != string::npos) ? (unsigned int)(weight) : getMinWeight(); } -bool MagicSearch::checkDomain (const LinphoneFriend *lFriend, const LinphoneAddress *lAddress, const string &withDomain) const { +bool MagicSearch::checkDomain(const LinphoneFriend *lFriend, const LinphoneAddress *lAddress, const string &withDomain) const { bool onlySipUri = !withDomain.empty() && withDomain.compare("*") != 0; const LinphonePresenceModel *presenceModel = lFriend ? linphone_friend_get_presence_model(lFriend) : nullptr; char *contactPresence = presenceModel ? linphone_presence_model_get_contact(presenceModel) : nullptr; diff --git a/src/search/search-result.cpp b/src/search/search-result.cpp index 8f5b3a10c..d8b78adea 100644 --- a/src/search/search-result.cpp +++ b/src/search/search-result.cpp @@ -30,59 +30,69 @@ class SearchResultPrivate : public ClonableObjectPrivate { private: const LinphoneFriend *mFriend; const LinphoneAddress *mAddress; + std::string mPhoneNumber; unsigned int mWeight; L_DECLARE_PUBLIC(SearchResult); }; -SearchResult::SearchResult (const unsigned int weight, const LinphoneAddress *a, const LinphoneFriend *f) : ClonableObject(*new SearchResultPrivate) { +using namespace std; + +SearchResult::SearchResult(const unsigned int weight, const LinphoneAddress *a, const string &pn, const LinphoneFriend *f) : ClonableObject(*new SearchResultPrivate) { L_D(); d->mWeight = weight; d->mAddress = a; d->mFriend = f; + d->mPhoneNumber = pn; } -SearchResult::SearchResult (const SearchResult &sr) : ClonableObject(*new SearchResultPrivate) { +SearchResult::SearchResult(const SearchResult &sr) : ClonableObject(*new SearchResultPrivate) { L_D(); d->mWeight = sr.getWeight(); d->mAddress = sr.getAddress(); if (d->mAddress) linphone_address_ref(const_cast(d->mAddress)); d->mFriend = sr.getFriend(); + d->mPhoneNumber = sr.getPhoneNumber(); } -SearchResult::~SearchResult () { +SearchResult::~SearchResult() { L_D(); // FIXME: Ugly temporary workaround to solve weak. Remove me later. if (d->mAddress) linphone_address_unref(const_cast(d->mAddress)); }; -bool SearchResult::operator< (const SearchResult &other) const { +bool SearchResult::operator<(const SearchResult &other) const { return getWeight() < other.getWeight(); } -bool SearchResult::operator> (const SearchResult &other) const { +bool SearchResult::operator>(const SearchResult &other) const { return getWeight() > other.getWeight(); } -bool SearchResult::operator>= (const SearchResult &other) const { +bool SearchResult::operator>=(const SearchResult &other) const { return getWeight() >= other.getWeight(); } -bool SearchResult::operator= (const SearchResult &other) const { +bool SearchResult::operator=(const SearchResult &other) const { return getWeight() == other.getWeight(); } -const LinphoneFriend *SearchResult::getFriend () const { +const LinphoneFriend *SearchResult::getFriend() const { L_D(); return d->mFriend; } -const LinphoneAddress *SearchResult::getAddress () const { +const LinphoneAddress *SearchResult::getAddress() const { L_D(); return d->mAddress; } -unsigned int SearchResult::getWeight () const { +const string &SearchResult::getPhoneNumber() const { + L_D(); + return d->mPhoneNumber; +} + +unsigned int SearchResult::getWeight() const { L_D(); return d->mWeight; } diff --git a/src/search/search-result.h b/src/search/search-result.h index e6aa7b56f..cee73aafc 100644 --- a/src/search/search-result.h +++ b/src/search/search-result.h @@ -34,29 +34,34 @@ class SearchResultPrivate; class LINPHONE_PUBLIC SearchResult : public ClonableObject { public: // TODO: Use C++ Address! Not LinphoneAddress. - SearchResult (const unsigned int weight, const LinphoneAddress *a, const LinphoneFriend *f = nullptr); - SearchResult (const SearchResult &other); - ~SearchResult (); + SearchResult(const unsigned int weight, const LinphoneAddress *a, const std::string &pn, const LinphoneFriend *f = nullptr); + SearchResult(const SearchResult &other); + ~SearchResult(); - bool operator< (const SearchResult &other) const; - bool operator> (const SearchResult &other) const; - bool operator>= (const SearchResult &other) const; - bool operator= (const SearchResult &other) const; + bool operator<(const SearchResult &other) const; + bool operator>(const SearchResult &other) const; + bool operator>=(const SearchResult &other) const; + bool operator=(const SearchResult &other) const; /** * @return LinphoneFriend associed **/ - const LinphoneFriend *getFriend() const; + const LinphoneFriend *getFriend()const; /** * @return LinphoneAddress associed **/ - const LinphoneAddress *getAddress () const; + const LinphoneAddress *getAddress() const; + + /** + * @return Phone Number associed + **/ + const std::string &getPhoneNumber() const; /** * @return the result weight **/ - unsigned int getWeight () const; + unsigned int getWeight() const; private: L_DECLARE_PRIVATE(SearchResult);