File indexing completed on 2024-04-21 04:56:56

0001 /**
0002  * SPDX-FileCopyrightText: 2018 Aleix Pol Gonzalez <aleixpol@kde.org>
0003  * SPDX-FileCopyrightText: 2018 Simon Redman <simon@ergotech.com>
0004  *
0005  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0006  */
0007 
0008 #include "conversationssortfilterproxymodel.h"
0009 #include "conversationlistmodel.h"
0010 #include "smshelper.h"
0011 
0012 #include <QLoggingCategory>
0013 #include <QString>
0014 
0015 Q_LOGGING_CATEGORY(KDECONNECT_SMS_CONVERSATIONS_SORT_FILTER_PROXY_MODEL, "kdeconnect.sms.conversations_sort_filter_proxy")
0016 
0017 #define INVALID_THREAD_ID -1
0018 
0019 ConversationsSortFilterProxyModel::ConversationsSortFilterProxyModel()
0020 {
0021     setFilterRole(ConversationListModel::ConversationIdRole);
0022 }
0023 
0024 ConversationsSortFilterProxyModel::~ConversationsSortFilterProxyModel()
0025 {
0026 }
0027 
0028 void ConversationsSortFilterProxyModel::setConversationsFilterRole(int role)
0029 {
0030     setFilterRole(role);
0031 }
0032 
0033 bool ConversationsSortFilterProxyModel::lessThan(const QModelIndex &leftIndex, const QModelIndex &rightIndex) const
0034 {
0035     qlonglong leftDataTimeStamp = sourceModel()->data(leftIndex, ConversationListModel::DateRole).toLongLong();
0036     qlonglong rightDataTimeStamp = sourceModel()->data(rightIndex, ConversationListModel::DateRole).toLongLong();
0037 
0038     if (leftDataTimeStamp == rightDataTimeStamp) {
0039         QVariant leftDataName = sourceModel()->data(leftIndex, Qt::DisplayRole);
0040         QVariant rightDataName = sourceModel()->data(rightIndex, Qt::DisplayRole);
0041         return leftDataName.toString().toLower() > rightDataName.toString().toLower();
0042     }
0043     return leftDataTimeStamp < rightDataTimeStamp;
0044 }
0045 
0046 bool ConversationsSortFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
0047 {
0048     QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
0049 
0050     if (filterRole() == ConversationListModel::ConversationIdRole) {
0051         return sourceModel()->data(index, ConversationListModel::ConversationIdRole) != INVALID_THREAD_ID;
0052     } else {
0053         if (sourceModel()->data(index, Qt::DisplayRole).toString().contains(filterRegularExpression())) {
0054             return true;
0055         }
0056 
0057         // This block of code compares each address in the multi target conversation to find a match
0058         const QList<ConversationAddress> addressList = sourceModel()->data(index, ConversationListModel::AddressesRole).value<QList<ConversationAddress>>();
0059         for (const ConversationAddress &address : addressList) {
0060             QString canonicalAddress = SmsHelper::canonicalizePhoneNumber(address.address());
0061 #if QT_VERSION_MAJOR < 6
0062             if (canonicalAddress.contains(filterRegExp())) {
0063 #else
0064             if (canonicalAddress.contains(filterRegularExpression())) {
0065 #endif
0066                 return true;
0067             }
0068         }
0069     }
0070     return false;
0071 }
0072 
0073 bool ConversationsSortFilterProxyModel::doesAddressExists(const QString &address)
0074 {
0075     for (int i = 0; i < rowCount(); ++i) {
0076         if (!data(index(i, 0), ConversationListModel::MultitargetRole).toBool()) {
0077             QVariant senderAddress = data(index(i, 0), ConversationListModel::SenderRole);
0078             if (SmsHelper::isPhoneNumberMatch(senderAddress.toString(), address)) {
0079                 return true;
0080             }
0081         }
0082     }
0083     return false;
0084 }
0085 
0086 #include "moc_conversationssortfilterproxymodel.cpp"