smartlistmodel.cpp 11.1 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
 * 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"

22 23 24
#include "pixbufmanipulator.h"
#include "utils.h"
#include "lrcinstance.h"
25 26 27 28

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

29
#include <QDateTime>
30

31 32 33 34 35 36 37 38
SmartListModel::SmartListModel(const std::string& accId,
                               QObject *parent,
                               SmartListModel::Type listModelType,
                               const std::string& convUid)
    : QAbstractItemModel(parent)
    , accountId_(accId)
    , listModelType_(listModelType)
    , convUid_(convUid)
39
{
40 41 42
    if (listModelType_ == Type::CONFERENCE) {
        setConferenceableFilter();
    }
43 44 45 46 47
}

int SmartListModel::rowCount(const QModelIndex &parent) const
{
    if (!parent.isValid()) {
48 49 50
        auto& accInfo = LRCInstance::accountModel().getAccountInfo(accountId_);
        auto& convModel = accInfo.conversationModel;
        if (listModelType_ == Type::TRANSFER) {
51
            auto filterType = accInfo.profileInfo.type;
52 53 54 55 56 57 58 59 60 61 62
            return convModel->getFilteredConversations(filterType).size();
        } else if (listModelType_ == Type::CONFERENCE) {
            auto calls = conferenceables_.at(ConferenceableItem::CALL);
            auto contacts = conferenceables_.at(ConferenceableItem::CONTACT);
            auto rowCount = contacts.size();
            if (calls.size()) {
                rowCount = 2;
                rowCount += sectionState_[tr("Calls")] ? calls.size() : 0;
                rowCount += sectionState_[tr("Contacts")] ? contacts.size() : 0;
            }
            return rowCount;
63
        }
64
        return accInfo.conversationModel->allFilteredConversations().size();
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    }
    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();
    }

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    try {
        auto& accountInfo = LRCInstance::accountModel().getAccountInfo(accountId_);
        auto& convModel = accountInfo.conversationModel;
        lrc::api::conversation::Info item;
        if (listModelType_ == Type::TRANSFER) {
            auto filterType = accountInfo.profileInfo.type;
            item = convModel->getFilteredConversations(filterType).at(index.row());
            return getConversationItemData(item, accountInfo, role);
        } else if (listModelType_ == Type::CONFERENCE) {
            auto calls = conferenceables_.at(ConferenceableItem::CALL);
            auto contacts = conferenceables_.at(ConferenceableItem::CONTACT);
            std::string itemConvUid{}, itemAccId{};
            if (calls.size() == 0) {
                itemConvUid = contacts.at(index.row()).at(0).convId;
                itemAccId = contacts.at(index.row()).at(0).accountId;
            } else {
                bool callsOpen = sectionState_[tr("Calls")];
                bool contactsOpen = sectionState_[tr("Contacts")];
                auto callSectionEnd = callsOpen ? calls.size() + 1 : 1;
                auto contactSectionEnd = contactsOpen ?
                    callSectionEnd + contacts.size() + 1 :
                    callSectionEnd + 1;
                if (index.row() < callSectionEnd) {
                    if (index.row() == 0) {
                        return QVariant(role == Role::SectionName ?
                            (callsOpen ? "➖ " : "➕ ") + QString(tr("Calls")) :
                            "");
                    } else {
                        auto idx = index.row() - 1;
                        itemConvUid = calls.at(idx).at(0).convId;
                        itemAccId = calls.at(idx).at(0).accountId;
                    }
                } else if (index.row() < contactSectionEnd) {
                    if (index.row() == callSectionEnd) {
                        return QVariant(role == Role::SectionName ?
                            (contactsOpen ? "➖ " : "➕ ") + QString(tr("Contacts")) :
                            "");
                    } else {
                        auto idx = index.row() - (callSectionEnd + 1);
                        itemConvUid = contacts.at(idx).at(0).convId;
                        itemAccId = contacts.at(idx).at(0).accountId;
122 123 124
                    }
                }
            }
125 126
            if (role == Role::AccountId) {
                return QVariant(QString::fromStdString(itemAccId));
127
            }
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
            item = LRCInstance::getConversationFromConvUid(itemConvUid, itemAccId);
            auto& itemAccountInfo = LRCInstance::accountModel().getAccountInfo(itemAccId);
            return getConversationItemData(item, itemAccountInfo, role);
        } else if (listModelType_ == Type::CONVERSATION) {
            item = convModel->filteredConversation(index.row());
            return getConversationItemData(item, accountInfo, role);
        }
    } catch (const std::exception& e) {
        qWarning() << e.what();
    }
    return QVariant();
}

void
SmartListModel::setConferenceableFilter(const std::string& filter)
{
    beginResetModel();
    auto& accountInfo = LRCInstance::accountModel().getAccountInfo(accountId_);
    auto& convModel = accountInfo.conversationModel;
    conferenceables_ = convModel->getConferenceableConversations(convUid_, filter);
    sectionState_[tr("Calls")] = true;
    sectionState_[tr("Contacts")] = true;
    endResetModel();
}

void
SmartListModel::toggleSection(const QString& section)
{
    beginResetModel();
    if (section.contains(tr("Calls"))) {
        sectionState_[tr("Calls")] ^= true;
    } else if (section.contains(tr("Contacts"))) {
        sectionState_[tr("Contacts")] ^= true;
    }
    endResetModel();
}

QVariant
SmartListModel::getConversationItemData(const conversation::Info& item,
                                        const account::Info& accountInfo,
                                        int role) const
{
    if (item.participants.size() <= 0) {
        return QVariant();
    }
    auto& contactModel = accountInfo.contactModel;
    switch (role) {
    case Role::Picture:
    case Qt::DecorationRole:
        return GlobalInstances::pixmapManipulator().decorationRole(item, accountInfo);
    case Role::DisplayName:
    case Qt::DisplayRole:
    {
        auto& contact = contactModel->getContact(item.participants[0]);
        return QVariant(QString::fromStdString(Utils::bestNameForContact(contact)));
    }
    case Role::DisplayID:
    {
        auto& contact = contactModel->getContact(item.participants[0]);
        return QVariant(QString::fromStdString(Utils::bestIdForContact(contact)));
    }
    case Role::Presence:
    {
        auto& contact = contactModel->getContact(item.participants[0]);
        return QVariant(contact.isPresent);
    }
    case Role::URI:
    {
        auto& contact = contactModel->getContact(item.participants[0]);
        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));
    case Role::LastInteractionType:
        return QVariant(Utils::toUnderlyingValue(item.interactions.at(item.lastMessageUid).type));
    case Role::ContactType:
    {
        auto& contact = contactModel->getContact(item.participants[0]);
        return QVariant(Utils::toUnderlyingValue(contact.profileInfo.type));
    }
    case Role::UID:
        return QVariant(QString::fromStdString(item.uid));
    case Role::ContextMenuOpen:
        return QVariant(isContextMenuOpen);
    case Role::InCall:
    {
        auto& convInfo = LRCInstance::getConversationFromConvUid(item.uid);
        if (!convInfo.uid.empty()) {
            auto callModel = LRCInstance::getCurrentCallModel();
            return QVariant(callModel->hasCall(convInfo.callId));
        }
        return QVariant(false);
    }
    case Role::CallStateStr:
    {
        auto& convInfo = LRCInstance::getConversationFromConvUid(item.uid);
        if (!convInfo.uid.empty()) {
            auto call = LRCInstance::getCallInfoForConversation(convInfo);
            if (call) {
                auto statusString = call::to_string(call->status);
                return QVariant(QString::fromStdString(statusString));
            }
        }
        return QVariant();
    }
    case Role::SectionName:
        return QVariant(QString());
242 243
    case Role::Draft:
        return LRCInstance::getContentDraft(item.uid.c_str(), accountInfo.id.c_str());
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
    }
    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 uid = data(index, Role::UID).value<QString>();
    if (!index.isValid()) {
        return QAbstractItemModel::flags(index);
274 275
    } else if ( (type == lrc::api::profile::Type::TEMPORARY &&
                uid.isEmpty())) {
276 277 278 279
        flags &= ~(Qt::ItemIsSelectable);
    }
    return flags;
}
280 281

void
282
SmartListModel::setAccount(const std::string& accountId)
283
{
284
    beginResetModel();
285
    accountId_ = accountId;
286
    endResetModel();
287
}