Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 1 | /*************************************************************************** |
Sébastien Blin | 68abac9 | 2019-01-02 17:41:31 -0500 | [diff] [blame] | 2 | * Copyright (C) 2017-2019 by Savoir-faire Linux * |
| 3 | * Author: Anthony L�onard <anthony.leonard@savoirfairelinux.com> * |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 4 | * Author: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com> * |
| 5 | * * |
| 6 | * This program is free software; you can redistribute it and/or modify * |
| 7 | * it under the terms of the GNU General Public License as published by * |
| 8 | * the Free Software Foundation; either version 3 of the License, or * |
| 9 | * (at your option) any later version. * |
| 10 | * * |
| 11 | * This program is distributed in the hope that it will be useful, * |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 | * GNU General Public License for more details. * |
| 15 | * * |
| 16 | * You should have received a copy of the GNU General Public License * |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. * |
| 18 | **************************************************************************/ |
| 19 | |
| 20 | #include "smartlistmodel.h" |
| 21 | |
| 22 | // Qt |
| 23 | #include <QDateTime> |
| 24 | |
| 25 | // LRC |
| 26 | #include "globalinstances.h" |
| 27 | #include "api/contactmodel.h" |
| 28 | #include "api/conversationmodel.h" |
| 29 | |
| 30 | // Client |
| 31 | #include "pixbufmanipulator.h" |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 32 | #include "utils.h" |
Andreas Traczyk | 6b5ad3e | 2019-01-02 17:04:36 -0500 | [diff] [blame] | 33 | #include "lrcinstance.h" |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 34 | |
Andreas Traczyk | 6b5ad3e | 2019-01-02 17:04:36 -0500 | [diff] [blame] | 35 | SmartListModel::SmartListModel(const std::string& accId, QObject *parent) |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 36 | : QAbstractItemModel(parent), |
Andreas Traczyk | 6b5ad3e | 2019-01-02 17:04:36 -0500 | [diff] [blame] | 37 | accId_(accId) |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 38 | { |
| 39 | } |
| 40 | |
| 41 | int SmartListModel::rowCount(const QModelIndex &parent) const |
| 42 | { |
| 43 | if (!parent.isValid()) { |
Andreas Traczyk | 6b5ad3e | 2019-01-02 17:04:36 -0500 | [diff] [blame] | 44 | auto& accInfo = LRCInstance::accountModel().getAccountInfo(accId_); |
| 45 | return accInfo.conversationModel->allFilteredConversations().size(); |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 46 | } |
| 47 | return 0; // A valid QModelIndex returns 0 as no entry has sub-elements |
| 48 | } |
| 49 | |
| 50 | int SmartListModel::columnCount(const QModelIndex &parent) const |
| 51 | { |
| 52 | Q_UNUSED(parent); |
| 53 | return 1; |
| 54 | } |
| 55 | |
| 56 | QVariant SmartListModel::data(const QModelIndex &index, int role) const |
| 57 | { |
| 58 | if (!index.isValid()) { |
| 59 | return QVariant(); |
| 60 | } |
| 61 | |
Andreas Traczyk | 6b5ad3e | 2019-01-02 17:04:36 -0500 | [diff] [blame] | 62 | auto& accInfo = LRCInstance::accountModel().getAccountInfo(accId_); |
| 63 | const auto& item = accInfo.conversationModel->filteredConversation(index.row()); |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 64 | if (item.participants.size() > 0) { |
| 65 | try { |
| 66 | switch (role) { |
| 67 | case Role::Picture: |
| 68 | case Qt::DecorationRole: |
Andreas Traczyk | 6b5ad3e | 2019-01-02 17:04:36 -0500 | [diff] [blame] | 69 | return GlobalInstances::pixmapManipulator().decorationRole(item, accInfo); |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 70 | case Role::DisplayName: |
| 71 | case Qt::DisplayRole: |
| 72 | { |
Andreas Traczyk | 6b5ad3e | 2019-01-02 17:04:36 -0500 | [diff] [blame] | 73 | auto& contact = accInfo.contactModel->getContact(item.participants[0]); |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 74 | return QVariant(QString::fromStdString(Utils::bestNameForContact(contact))); |
| 75 | } |
| 76 | case Role::DisplayID: |
| 77 | { |
Andreas Traczyk | 6b5ad3e | 2019-01-02 17:04:36 -0500 | [diff] [blame] | 78 | auto& contact = accInfo.contactModel->getContact(item.participants[0]); |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 79 | return QVariant(QString::fromStdString(Utils::bestIdForContact(contact))); |
| 80 | } |
| 81 | case Role::Presence: |
| 82 | { |
Andreas Traczyk | 6b5ad3e | 2019-01-02 17:04:36 -0500 | [diff] [blame] | 83 | auto& contact = accInfo.contactModel->getContact(item.participants[0]); |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 84 | return QVariant(contact.isPresent); |
| 85 | } |
| 86 | case Role::URI: |
| 87 | { |
Andreas Traczyk | 6b5ad3e | 2019-01-02 17:04:36 -0500 | [diff] [blame] | 88 | auto& contact = accInfo.contactModel->getContact(item.participants[0]); |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 89 | return QVariant(QString::fromStdString(contact.profileInfo.uri)); |
| 90 | } |
| 91 | case Role::UnreadMessagesCount: |
| 92 | return QVariant(item.unreadMessages); |
| 93 | case Role::LastInteractionDate: |
| 94 | { |
| 95 | auto& date = item.interactions.at(item.lastMessageUid).timestamp; |
| 96 | return QVariant(QString::fromStdString(Utils::formatTimeString(date))); |
| 97 | } |
| 98 | case Role::LastInteraction: |
| 99 | return QVariant(QString::fromStdString(item.interactions.at(item.lastMessageUid).body)); |
Andreas Traczyk | 43c0823 | 2018-10-31 13:42:09 -0400 | [diff] [blame] | 100 | case Role::LastInteractionType: |
| 101 | return QVariant(Utils::toUnderlyingValue(item.interactions.at(item.lastMessageUid).type)); |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 102 | case Role::ContactType: |
| 103 | { |
Andreas Traczyk | 6b5ad3e | 2019-01-02 17:04:36 -0500 | [diff] [blame] | 104 | auto& contact = accInfo.contactModel->getContact(item.participants[0]); |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 105 | return QVariant(Utils::toUnderlyingValue(contact.profileInfo.type)); |
| 106 | } |
| 107 | case Role::UID: |
| 108 | return QVariant(QString::fromStdString(item.uid)); |
| 109 | case Role::ContextMenuOpen: |
Andreas Traczyk | 43c0823 | 2018-10-31 13:42:09 -0400 | [diff] [blame] | 110 | return QVariant(isContextMenuOpen); |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 111 | } |
| 112 | } catch (...) {} |
| 113 | } |
| 114 | return QVariant(); |
| 115 | } |
| 116 | |
| 117 | QModelIndex SmartListModel::index(int row, int column, const QModelIndex &parent) const |
| 118 | { |
| 119 | Q_UNUSED(parent); |
| 120 | if (column != 0) { |
| 121 | return QModelIndex(); |
| 122 | } |
| 123 | |
| 124 | if (row >= 0 && row < rowCount()) { |
| 125 | return createIndex(row, column); |
| 126 | } |
| 127 | return QModelIndex(); |
| 128 | } |
| 129 | |
| 130 | QModelIndex SmartListModel::parent(const QModelIndex &child) const |
| 131 | { |
| 132 | Q_UNUSED(child); |
| 133 | return QModelIndex(); |
| 134 | } |
| 135 | |
| 136 | Qt::ItemFlags SmartListModel::flags(const QModelIndex &index) const |
| 137 | { |
| 138 | auto flags = QAbstractItemModel::flags(index) | Qt::ItemNeverHasChildren | Qt::ItemIsSelectable; |
| 139 | auto type = Utils::toEnum<lrc::api::profile::Type>(data(index, Role::ContactType).value<int>()); |
| 140 | auto displayName = data(index, Role::DisplayName).value<QString>(); |
| 141 | auto uid = data(index, Role::UID).value<QString>(); |
| 142 | if (!index.isValid()) { |
| 143 | return QAbstractItemModel::flags(index); |
| 144 | } else if ( type == lrc::api::profile::Type::TEMPORARY && |
| 145 | uid.isEmpty()) { |
| 146 | flags &= ~(Qt::ItemIsSelectable); |
| 147 | } |
| 148 | return flags; |
| 149 | } |
Andreas Traczyk | 6b5ad3e | 2019-01-02 17:04:36 -0500 | [diff] [blame] | 150 | |
| 151 | void |
| 152 | SmartListModel::setAccount(const std::string& accId) |
| 153 | { |
Andreas Traczyk | cce9d61 | 2019-01-03 10:05:31 -0500 | [diff] [blame] | 154 | beginResetModel(); |
Andreas Traczyk | 6b5ad3e | 2019-01-02 17:04:36 -0500 | [diff] [blame] | 155 | accId_ = accId; |
Andreas Traczyk | cce9d61 | 2019-01-03 10:05:31 -0500 | [diff] [blame] | 156 | endResetModel(); |
Andreas Traczyk | 6b5ad3e | 2019-01-02 17:04:36 -0500 | [diff] [blame] | 157 | } |