Commit 4a48fe00 authored by Laurent Montel's avatar Laurent Montel 😁
Browse files

Improve search element

parent dce31b90
Pipeline #147198 passed with stage
in 2 minutes and 29 seconds
......@@ -19,6 +19,32 @@ using namespace Akonadi;
static bool contactMatchesFilter(const KContacts::Addressee &contact, const QString &filterString, ContactsFilterProxyModel::MatchFilterContactFlag flag);
static bool contactGroupMatchesFilter(const KContacts::ContactGroup &group, const QString &filterString);
static QString normalize(QStringView str)
{
QString out;
out.reserve(str.size());
for (const auto c : str) {
// case folding
const auto n = c.toCaseFolded();
// if the character has a canonical decomposition use that and skip the
// combining diacritic markers following it
// see https://en.wikipedia.org/wiki/Unicode_equivalence
// see https://en.wikipedia.org/wiki/Combining_character
if (n.decompositionTag() == QChar::Canonical) {
out.push_back(n.decomposition().at(0));
}
// handle compatibility compositions such as ligatures
// see https://en.wikipedia.org/wiki/Unicode_compatibility_characters
else if (n.decompositionTag() == QChar::Compat && n.isLetter() && n.script() == QChar::Script_Latin) {
out.append(n.decomposition());
} else {
out.push_back(n);
}
}
return out;
}
class Akonadi::ContactsFilterProxyModelPrivate
{
public:
......@@ -63,7 +89,7 @@ bool ContactsFilterProxyModel::filterAcceptsRow(int row, const QModelIndex &pare
if ((d->mFilter.isEmpty()) && (!(d->flags & ContactsFilterProxyModel::HasEmail))) {
return true;
}
const QString filterStr = normalize(d->mFilter);
const auto item = index.data(Akonadi::EntityTreeModel::ItemRole).value<Akonadi::Item>();
if (item.hasPayload<KContacts::Addressee>()) {
......@@ -74,13 +100,13 @@ bool ContactsFilterProxyModel::filterAcceptsRow(int row, const QModelIndex &pare
}
}
if (!d->mFilter.isEmpty()) {
return contactMatchesFilter(contact, d->mFilter, d->matchFilterFlag);
return contactMatchesFilter(contact, filterStr, d->matchFilterFlag);
}
} else {
if (!d->mFilter.isEmpty()) {
if (item.hasPayload<KContacts::ContactGroup>()) {
const auto group = item.payload<KContacts::ContactGroup>();
return contactGroupMatchesFilter(group, d->mFilter);
return contactGroupMatchesFilter(group, filterStr);
}
}
}
......@@ -173,15 +199,15 @@ static bool addressMatchesFilter(const KContacts::Address &address, const QStrin
static bool contactMatchesFilter(const KContacts::Addressee &contact, const QString &filterString, ContactsFilterProxyModel::MatchFilterContactFlag flag)
{
if (contact.assembledName().contains(filterString, Qt::CaseInsensitive)) {
if (normalize(contact.assembledName()).contains(filterString, Qt::CaseInsensitive)) {
return true;
}
if (contact.formattedName().contains(filterString, Qt::CaseInsensitive)) {
if (normalize(contact.formattedName()).contains(filterString, Qt::CaseInsensitive)) {
return true;
}
if (contact.nickName().contains(filterString, Qt::CaseInsensitive)) {
if (normalize(contact.nickName()).contains(filterString, Qt::CaseInsensitive)) {
return true;
}
......@@ -210,7 +236,7 @@ static bool contactMatchesFilter(const KContacts::Addressee &contact, const QStr
const QStringList emails = contact.emails();
count = emails.count();
for (int i = 0; i < count; ++i) {
if (emails.at(i).contains(filterString, Qt::CaseInsensitive)) {
if (normalize(emails.at(i)).contains(filterString, Qt::CaseInsensitive)) {
return true;
}
}
......@@ -227,7 +253,7 @@ static bool contactMatchesFilter(const KContacts::Addressee &contact, const QStr
return true;
}
if (contact.title().contains(filterString, Qt::CaseInsensitive)) {
if (normalize(contact.title()).contains(filterString, Qt::CaseInsensitive)) {
return true;
}
......@@ -235,15 +261,15 @@ static bool contactMatchesFilter(const KContacts::Addressee &contact, const QStr
return true;
}
if (contact.organization().contains(filterString, Qt::CaseInsensitive)) {
if (normalize(contact.organization()).contains(filterString, Qt::CaseInsensitive)) {
return true;
}
if (contact.department().contains(filterString, Qt::CaseInsensitive)) {
if (normalize(contact.department()).contains(filterString, Qt::CaseInsensitive)) {
return true;
}
if (contact.note().contains(filterString, Qt::CaseInsensitive)) {
if (normalize(contact.note()).contains(filterString, Qt::CaseInsensitive)) {
return true;
}
......@@ -265,16 +291,16 @@ static bool contactMatchesFilter(const KContacts::Addressee &contact, const QStr
bool contactGroupMatchesFilter(const KContacts::ContactGroup &group, const QString &filterString)
{
if (group.name().contains(filterString, Qt::CaseInsensitive)) {
if (normalize(group.name()).contains(filterString, Qt::CaseInsensitive)) {
return true;
}
const int count = group.dataCount();
for (int i = 0; i < count; ++i) {
if (group.data(i).name().contains(filterString, Qt::CaseInsensitive)) {
if (normalize(group.data(i).name()).contains(filterString, Qt::CaseInsensitive)) {
return true;
}
if (group.data(i).email().contains(filterString, Qt::CaseInsensitive)) {
if (normalize(group.data(i).email()).contains(filterString, Qt::CaseInsensitive)) {
return true;
}
}
......
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