conversationssortfilterproxymodel.cpp 4.33 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/**
 * Copyright (C) 2018 Aleix Pol Gonzalez <aleixpol@kde.org>
 * Copyright (C) 2018 Simon Redman <simon@ergotech.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License or (at your option) version 3 or any later version
 * accepted by the membership of KDE e.V. (or its successor approved
 * by the membership of KDE e.V.), which shall act as a proxy
 * defined in Section 14 of version 3 of the license.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "conversationssortfilterproxymodel.h"
#include "conversationlistmodel.h"
24
#include "smshelper.h"
25 26 27 28 29 30 31 32 33 34

#include <QString>
#include <QLoggingCategory>

Q_LOGGING_CATEGORY(KDECONNECT_SMS_CONVERSATIONS_SORT_FILTER_PROXY_MODEL, "kdeconnect.sms.conversations_sort_filter_proxy")

#define INVALID_THREAD_ID -1

ConversationsSortFilterProxyModel::ConversationsSortFilterProxyModel()
{
35
    setFilterRole(ConversationListModel::ConversationIdRole);
36 37
}

Aniket Kumar's avatar
Aniket Kumar committed
38 39
ConversationsSortFilterProxyModel::~ConversationsSortFilterProxyModel() {}

40
void ConversationsSortFilterProxyModel::setConversationsFilterRole(int role)
41 42 43 44 45 46
{
    setFilterRole(role);
}

bool ConversationsSortFilterProxyModel::lessThan(const QModelIndex& leftIndex, const QModelIndex& rightIndex) const
{
47 48
    // This if block checks for multitarget conversations and sorts it at bottom of the list when the filtring is done on the basis of SenderRole
    // This keeps the individual contacts with matching address at the top of the list
49
    if (filterRole() == ConversationListModel::AddressesRole) {
50
        const bool isLeftMultitarget = sourceModel()->data(leftIndex, ConversationListModel::MultitargetRole).toBool();
51
        const bool isRightMultitarget = sourceModel()->data(rightIndex, ConversationListModel::MultitargetRole).toBool();
52
        if (isLeftMultitarget && !isRightMultitarget) {
53
            return true;
54 55 56 57 58 59
        }
        if (!isLeftMultitarget && isRightMultitarget) {
            return true;
        }
    }

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    QVariant leftDataTimeStamp = sourceModel()->data(leftIndex, ConversationListModel::DateRole);
    QVariant rightDataTimeStamp = sourceModel()->data(rightIndex, ConversationListModel::DateRole);

    if (leftDataTimeStamp == rightDataTimeStamp) {
        QVariant leftDataName = sourceModel()->data(leftIndex, Qt::DisplayRole);
        QVariant rightDataName = sourceModel()->data(rightIndex, Qt::DisplayRole);
        return leftDataName.toString().toLower() > rightDataName.toString().toLower();
    }
    return leftDataTimeStamp < rightDataTimeStamp;
}

bool ConversationsSortFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
{
    QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);

75 76 77 78 79 80
    if (filterRole() == ConversationListModel::ConversationIdRole) {
        return sourceModel()->data(index, ConversationListModel::ConversationIdRole) != INVALID_THREAD_ID;
    } else {
        if (sourceModel()->data(index, Qt::DisplayRole).toString().contains(filterRegExp())) {
           return true;
        }
81

82 83 84 85 86
        // This block of code compares each address in the multi target conversation to find a match
        QList<ConversationAddress> addressList = sourceModel()->data(index, ConversationListModel::AddressesRole).value<QList<ConversationAddress>>();
        for (const ConversationAddress address : addressList) {
            if (address.address().contains(filterRegExp())) {
                return true;
87 88
            }
        }
89
    }
90
    return false;
91 92
}

93
bool ConversationsSortFilterProxyModel::doesAddressExists(const QString &address)
94 95 96 97 98 99 100 101 102 103
{
    for(int i = 0; i < rowCount(); ++i) {
        if (!data(index(i, 0), ConversationListModel::MultitargetRole).toBool()) {
            QVariant senderAddress = data(index(i, 0), ConversationListModel::SenderRole);
            if (SmsHelper::isPhoneNumberMatch(senderAddress.toString(), address)) {
                return true;
            }
        }
    }
    return false;
104
}