[OpenDocString] kdeconnect-kde (cpp)
conversationssortfilterproxymodel.cpp
ConversationsSortFilterProxyModel::ConversationsSortFilterProxyModel()
{
    setFilterRole(ConversationListModel::ConversationIdRole);
}
Sets the filter role on the conversations list model.
ConversationsSortFilterProxyModel::~ConversationsSortFilterProxyModel()
{
}
This implements the sorting feature of the conversations model.
void ConversationsSortFilterProxyModel::setConversationsFilterRole(int role)
{
    setFilterRole(role);
}
Sets the role for the filter.
bool ConversationsSortFilterProxyModel::lessThan(const QModelIndex &leftIndex, const QModelIndex &rightIndex) const
{
    qlonglong leftDataTimeStamp = sourceModel()->data(leftIndex, ConversationListModel::DateRole).toLongLong();
    qlonglong rightDataTimeStamp = sourceModel()->data(rightIndex, ConversationListModel::DateRole).toLongLong();

    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;
}
This implements the less than operator to determine if the first item in the left index is less than the given right item. The function first retrieves the timestamp of the left item and if it is the timestamp of the right item, and if it is the timestamp of the right item, and if it is the timestamp of the left item, the function returns true. Otherwise, the function retrieves the data name of the left item and sets the value to its lower representation.
bool ConversationsSortFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
{
    QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);

    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;
        }

        // This block of code compares each address in the multi target conversation to find a match
        const QList addressList = sourceModel()->data(index, ConversationListModel::AddressesRole).value>();
        for (const ConversationAddress &address : addressList) {
            QString canonicalAddress = SmsHelper::canonicalizePhoneNumber(address.address());
            if (canonicalAddress.contains(filterRegExp())) {
                return true;
            }
        }
    }
    return false;
}
This implements checking if the given row is accepted by the filter. It first checks if the source row is of the correct type, then checks if the source row contains the filter regex. If the filter is of the typeConversationIdRole, the source row is of the typeConversationIdRole, the source model is of the typeConversationListModel::ConversationIdRole, the source model is of the typeConversationListModel::ConversationIdRole, the source model is of the typeConversationListModel::AddressesRole, the filter is of the typeConversationListModel::AddressesRole, the function checks if the source row contains the filter regex, if the filter is of the typeConversationIdRole, the source model contains the invalid_thread_id, the source row is of the typeConversationIdRole, the source model is of the typeConversationListModel::DisplayRole, the source model is of the typeConversationListModel::AddressesRole, the filter is of the typeConversationListModel::AddressesRole, the filter is of the typeConversationListModel::The filter is of the typeConversationListModel::TheFilterRegExp() function, which checks if the filter is of the typeConversationIdRole, the filter is true, and the filter is not a valid_thread_id, the source model is of the typeConversationIdRole, the filter is of the typeConversationList, the filter is of the typeConversationList, the filter is of the typeConversationList, the filter is
bool ConversationsSortFilterProxyModel::doesAddressExists(const QString &address)
{
    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;
}
This checks if the address exists in the list. It first checks if the sender role is a multicast role. If it is it checks if the sender phone number matches.