fix(src/components/smart-search-bar/SmartSearchBarModel): use contact (if exists) username on search

This commit is contained in:
Ronan Abhamon 2017-01-05 14:01:45 +01:00
parent 735554d421
commit 7821bf168d
2 changed files with 15 additions and 3 deletions

View file

@ -36,7 +36,7 @@ void SmartSearchBarModel::setFilter (const QString &pattern) {
bool SmartSearchBarModel::filterAcceptsRow (int source_row, const QModelIndex &source_parent) const {
const QModelIndex &index = sourceModel()->index(source_row, 0, source_parent);
return computeStringWeight(index.data().toMap()["sipAddress"].toString()) > 0;
return computeEntryWeight(index.data().toMap()) > 0;
}
bool SmartSearchBarModel::lessThan (const QModelIndex &left, const QModelIndex &right) const {
@ -46,8 +46,9 @@ bool SmartSearchBarModel::lessThan (const QModelIndex &left, const QModelIndex &
const QString &sip_address_a = map_a["sipAddress"].toString();
const QString &sip_address_b = map_b["sipAddress"].toString();
int weight_a = computeStringWeight(sip_address_a);
int weight_b = computeStringWeight(sip_address_b);
// TODO: Use a cache, do not compute the same value as `filterAcceptsRow`.
int weight_a = computeEntryWeight(map_a);
int weight_b = computeEntryWeight(map_b);
// 1. Not the same weight.
if (weight_a != weight_b)
@ -77,6 +78,16 @@ bool SmartSearchBarModel::lessThan (const QModelIndex &left, const QModelIndex &
return sip_address_a <= sip_address_b;
}
int SmartSearchBarModel::computeEntryWeight (const QVariantMap &entry) const {
int weight = computeStringWeight(entry["sipAddress"].toString().mid(4));
const ContactModel *contact = entry.value("contact").value<ContactModel *>();
if (contact)
weight += computeStringWeight(contact->getVcardModel()->getUsername());
return weight;
}
int SmartSearchBarModel::computeStringWeight (const QString &string) const {
int index = -1;
int offset = -1;

View file

@ -23,6 +23,7 @@ protected:
bool lessThan (const QModelIndex &left, const QModelIndex &right) const override;
private:
int computeEntryWeight (const QVariantMap &entry) const;
int computeStringWeight (const QString &string) const;
QString m_filter;