File indexing completed on 2024-12-08 12:53:34

0001 /*
0002    SPDX-FileCopyrightText: 2022-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "notificationhistorymodelfilterproxymodel.h"
0008 #include "notificationhistorymodel.h"
0009 
0010 #include "config-ruqola.h"
0011 #if HAVE_TEXT_UTILS
0012 #include <TextUtils/ConvertText>
0013 #endif
0014 
0015 NotificationHistoryModelFilterProxyModel::NotificationHistoryModelFilterProxyModel(QObject *parent)
0016     : SortFilterProxyModelBase{parent}
0017 {
0018 }
0019 
0020 NotificationHistoryModelFilterProxyModel::~NotificationHistoryModelFilterProxyModel() = default;
0021 
0022 bool NotificationHistoryModelFilterProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
0023 {
0024     const QModelIndex modelIndex = sourceModel()->index(source_row, 0, source_parent);
0025 
0026     if (!mAccountNameFilter.isEmpty()) {
0027         if (!modelIndex.data(NotificationHistoryModel::AccountName).toString().contains(mAccountNameFilter, Qt::CaseInsensitive)) {
0028             return false;
0029         }
0030     }
0031     auto match = [&](int role) {
0032         if (mFilterString.isEmpty()) {
0033             return true;
0034         };
0035 #if HAVE_TEXT_UTILS
0036         const QString str = TextUtils::ConvertText::normalize(modelIndex.data(role).toString());
0037         return str.contains(mFilterString, Qt::CaseInsensitive);
0038 #else
0039         const QString str = modelIndex.data(role).toString();
0040         return str.contains(mFilterString, Qt::CaseInsensitive);
0041 #endif
0042     };
0043     if (!match(NotificationHistoryModel::RoomName) && !match(NotificationHistoryModel::AccountName) && !match(NotificationHistoryModel::SenderName)
0044         && !match(NotificationHistoryModel::MessageStr)) {
0045         return false;
0046     }
0047     return true;
0048 }
0049 
0050 void NotificationHistoryModelFilterProxyModel::setAccountNameFilter(const QString &newAccountNameFilter)
0051 {
0052     if (mAccountNameFilter != newAccountNameFilter) {
0053         mAccountNameFilter = newAccountNameFilter;
0054         invalidate();
0055     }
0056 }
0057 
0058 #include "moc_notificationhistorymodelfilterproxymodel.cpp"