smartlistmodel.cpp 5.85 KB
Newer Older
1
/***************************************************************************
2 3
 * Copyright (C) 2017-2019 by Savoir-faire Linux                                *
 * Author: Anthony L�onard <anthony.leonard@savoirfairelinux.com>          *
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
 * Author: Andreas Traczyk <andreas.traczyk@savoirfairelinux.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 3 of the License, or       *
 * (at your option) any later version.                                     *
 *                                                                         *
 * 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 <http://www.gnu.org/licenses/>.   *
 **************************************************************************/

#include "smartlistmodel.h"

// Qt
#include <QDateTime>

// LRC
#include "globalinstances.h"
#include "api/contactmodel.h"
#include "api/conversationmodel.h"

// Client
#include "pixbufmanipulator.h"
#include "utils.h"
33
#include "lrcinstance.h"
34

35
SmartListModel::SmartListModel(const std::string& accId, QObject *parent)
36
    : QAbstractItemModel(parent),
37
    accId_(accId)
38 39 40 41 42 43
{
}

int SmartListModel::rowCount(const QModelIndex &parent) const
{
    if (!parent.isValid()) {
44 45
        auto& accInfo = LRCInstance::accountModel().getAccountInfo(accId_);
        return accInfo.conversationModel->allFilteredConversations().size();
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
    }
    return 0; // A valid QModelIndex returns 0 as no entry has sub-elements
}

int SmartListModel::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return 1;
}

QVariant SmartListModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid()) {
        return QVariant();
    }

62 63
    auto& accInfo = LRCInstance::accountModel().getAccountInfo(accId_);
    const auto& item = accInfo.conversationModel->filteredConversation(index.row());
64 65 66 67 68
    if (item.participants.size() > 0) {
        try {
            switch (role) {
            case Role::Picture:
            case Qt::DecorationRole:
69
                return GlobalInstances::pixmapManipulator().decorationRole(item, accInfo);
70 71 72
            case Role::DisplayName:
            case Qt::DisplayRole:
            {
73
                auto& contact = accInfo.contactModel->getContact(item.participants[0]);
74 75 76 77
                return QVariant(QString::fromStdString(Utils::bestNameForContact(contact)));
            }
            case Role::DisplayID:
            {
78
                auto& contact = accInfo.contactModel->getContact(item.participants[0]);
79 80 81 82
                return QVariant(QString::fromStdString(Utils::bestIdForContact(contact)));
            }
            case Role::Presence:
            {
83
                auto& contact = accInfo.contactModel->getContact(item.participants[0]);
84 85 86 87
                return QVariant(contact.isPresent);
            }
            case Role::URI:
            {
88
                auto& contact = accInfo.contactModel->getContact(item.participants[0]);
89 90 91 92 93 94 95 96 97 98 99
                return QVariant(QString::fromStdString(contact.profileInfo.uri));
            }
            case Role::UnreadMessagesCount:
                return QVariant(item.unreadMessages);
            case Role::LastInteractionDate:
            {
                auto& date = item.interactions.at(item.lastMessageUid).timestamp;
                return QVariant(QString::fromStdString(Utils::formatTimeString(date)));
            }
            case Role::LastInteraction:
                return QVariant(QString::fromStdString(item.interactions.at(item.lastMessageUid).body));
100 101
            case Role::LastInteractionType:
                return QVariant(Utils::toUnderlyingValue(item.interactions.at(item.lastMessageUid).type));
102 103
            case Role::ContactType:
            {
104
                auto& contact = accInfo.contactModel->getContact(item.participants[0]);
105 106 107 108 109
                return QVariant(Utils::toUnderlyingValue(contact.profileInfo.type));
            }
            case Role::UID:
                return QVariant(QString::fromStdString(item.uid));
            case Role::ContextMenuOpen:
110
                return QVariant(isContextMenuOpen);
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
            }
        } catch (...) {}
    }
    return QVariant();
}

QModelIndex SmartListModel::index(int row, int column, const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    if (column != 0) {
        return QModelIndex();
    }

    if (row >= 0 && row < rowCount()) {
        return createIndex(row, column);
    }
    return QModelIndex();
}

QModelIndex SmartListModel::parent(const QModelIndex &child) const
{
    Q_UNUSED(child);
    return QModelIndex();
}

Qt::ItemFlags SmartListModel::flags(const QModelIndex &index) const
{
    auto flags = QAbstractItemModel::flags(index) | Qt::ItemNeverHasChildren | Qt::ItemIsSelectable;
    auto type = Utils::toEnum<lrc::api::profile::Type>(data(index, Role::ContactType).value<int>());
    auto displayName = data(index, Role::DisplayName).value<QString>();
    auto uid = data(index, Role::UID).value<QString>();
    if (!index.isValid()) {
        return QAbstractItemModel::flags(index);
    } else if ( type == lrc::api::profile::Type::TEMPORARY &&
                uid.isEmpty()) {
        flags &= ~(Qt::ItemIsSelectable);
    }
    return flags;
}
150 151 152 153

void
SmartListModel::setAccount(const std::string& accId)
{
154
    beginResetModel();
155
    accId_ = accId;
156
    endResetModel();
157
}