File indexing completed on 2023-05-30 09:17:32
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(filterRegExp())) { 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 (canonicalAddress.contains(filterRegExp())) { 0062 return true; 0063 } 0064 } 0065 } 0066 return false; 0067 } 0068 0069 bool ConversationsSortFilterProxyModel::doesAddressExists(const QString &address) 0070 { 0071 for (int i = 0; i < rowCount(); ++i) { 0072 if (!data(index(i, 0), ConversationListModel::MultitargetRole).toBool()) { 0073 QVariant senderAddress = data(index(i, 0), ConversationListModel::SenderRole); 0074 if (SmsHelper::isPhoneNumberMatch(senderAddress.toString(), address)) { 0075 return true; 0076 } 0077 } 0078 } 0079 return false; 0080 }