Commit 382f60d9 authored by Mickaël Turnel's avatar Mickaël Turnel

Fix contact duplication in search bar

parent 893b80a5
......@@ -227,6 +227,44 @@ list<SearchResult> MagicSearch::getAddressFromCallLog (
return resultList;
}
static string getAddressFromSearchResult (const SearchResult &sr, const shared_ptr<Core> lc) {
string sAddress;
if (!sr.getAddress() && sr.getFriend()) {
const LinphonePresenceModel *presenceModel = linphone_friend_get_presence_model(sr.getFriend());
char *contactPresence = presenceModel ? linphone_presence_model_get_contact(presenceModel) : nullptr;
LinphoneAddress *addrPresence = nullptr;
if (contactPresence) {
addrPresence = linphone_core_create_address(lc->getCCore(), contactPresence);
if (addrPresence) {
char *tmp = linphone_address_as_string_uri_only(addrPresence);
sAddress = tmp;
if (tmp) bctbx_free(tmp);
linphone_address_unref(addrPresence);
}
bctbx_free(contactPresence);
}
} else {
char *tmp = linphone_address_as_string_uri_only(sr.getAddress());
sAddress = tmp;
if (tmp) bctbx_free(tmp);
}
return sAddress;
}
static string getDisplayNameFromSearchResult (const SearchResult &sr) {
string name;
if (sr.getFriend()) {
name = linphone_friend_get_name(sr.getFriend());
} else if (sr.getAddress()){
name = linphone_address_get_display_name(sr.getAddress()) ?
linphone_address_get_display_name(sr.getAddress()) : linphone_address_get_username(sr.getAddress());
}
return name;
}
list<SearchResult> MagicSearch::getFriends (const string &withDomain) const {
list<SearchResult> returnList;
list<SearchResult> clResults;
......@@ -266,23 +304,31 @@ list<SearchResult> MagicSearch::getFriends (const string &withDomain) const {
clResults = getAddressFromCallLog("", withDomain, clResults);
addResultsToResultsList(clResults, returnList);
returnList.sort([](const SearchResult& lsr, const SearchResult& rsr){
auto lc = this->getCore();
returnList.sort([lc](const SearchResult& lsr, const SearchResult& rsr){
unsigned int cpt = 0;
string name1 = "";
string name2 = "";
if (lsr.getFriend()) {
name1 = linphone_friend_get_name(lsr.getFriend());
} else if (lsr.getAddress()){
name1 = linphone_address_get_display_name(lsr.getAddress()) ?
linphone_address_get_display_name(lsr.getAddress()) : linphone_address_get_username(lsr.getAddress());
}
string addr1 = getAddressFromSearchResult(lsr, lc);
string addr2 = getAddressFromSearchResult(rsr, lc);
if (rsr.getFriend()) {
name2 = linphone_friend_get_name(rsr.getFriend());
} else if (rsr.getAddress()){
name2 = linphone_address_get_display_name(rsr.getAddress()) ?
linphone_address_get_display_name(rsr.getAddress()) : linphone_address_get_username(rsr.getAddress());
while (addr1.size() > cpt && addr2.size() > cpt) {
int char1 = tolower(addr1.at(cpt));
int char2 = tolower(addr2.at(cpt));
if (char1 > char2) {
return true;
} else if (char1 < char2) {
return false;
}
cpt++;
}
return addr1.size() > addr2.size();
});
returnList = *uniqueItemsList(returnList);
returnList.sort([](const SearchResult& lsr, const SearchResult& rsr){
unsigned int cpt = 0;
string name1 = getDisplayNameFromSearchResult(lsr);
string name2 = getDisplayNameFromSearchResult(rsr);
while (name1.size() > cpt && name2.size() > cpt) {
int char1 = tolower(name1.at(cpt));
......@@ -297,7 +343,7 @@ list<SearchResult> MagicSearch::getFriends (const string &withDomain) const {
return name1.size() < name2.size();
});
return *uniqueItemsList(returnList);
return returnList;
}
list<SearchResult> *MagicSearch::beginNewSearch (const string &filter, const string &withDomain) const {
......@@ -502,38 +548,18 @@ void MagicSearch::addResultsToResultsList (std::list<SearchResult> &results, std
}
}
static string getAddressFromSearchResult (const SearchResult &sr, const shared_ptr<Core> lc) {
string sAddress = "";
if (!sr.getAddress() && sr.getFriend()) {
const LinphonePresenceModel *presenceModel = linphone_friend_get_presence_model(sr.getFriend());
char *contactPresence = presenceModel ? linphone_presence_model_get_contact(presenceModel) : nullptr;
LinphoneAddress *addrPresence = nullptr;
if (contactPresence) {
addrPresence = linphone_core_create_address(lc->getCCore(), contactPresence);
if (addrPresence) {
char *tmp = linphone_address_as_string_uri_only(addrPresence);
sAddress = tmp;
if (tmp) bctbx_free(tmp);
linphone_address_unref(addrPresence);
}
bctbx_free(contactPresence);
}
} else {
char *tmp = linphone_address_as_string_uri_only(sr.getAddress());
sAddress = tmp;
if (tmp) bctbx_free(tmp);
}
return sAddress;
}
list<SearchResult> *MagicSearch::uniqueItemsList (list<SearchResult> &list) const {
auto lc = this->getCore();
list.unique([lc](const SearchResult& lsr, const SearchResult& rsr){
string left = getAddressFromSearchResult(lsr, lc);
string right = getAddressFromSearchResult(rsr, lc);
const char phonePattern[] = ";user=phone";
size_t i = left.find(phonePattern);
if (i != string::npos) left.erase(i, sizeof phonePattern - 1);
i = right.find(phonePattern);
if (i != string::npos) right.erase(i, sizeof phonePattern - 1);
return (!left.empty() || !right.empty()) && left == right;
});
return &list;
......
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