blob: db26c0686e6ecfa126e8fc9560aef530bf517434 [file] [log] [blame]
Andreas Traczykb8b13ba2018-08-21 16:30:16 -04001/***************************************************************************
Sébastien Blin68abac92019-01-02 17:41:31 -05002 * Copyright (C) 2017-2019 by Savoir-faire Linux *
3 * Author: Anthony L�onard <anthony.leonard@savoirfairelinux.com> *
Andreas Traczykb8b13ba2018-08-21 16:30:16 -04004 * 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 Traczykb8b13ba2018-08-21 16:30:16 -040032#include "utils.h"
Andreas Traczyk6b5ad3e2019-01-02 17:04:36 -050033#include "lrcinstance.h"
Andreas Traczykb8b13ba2018-08-21 16:30:16 -040034
Andreas Traczyk6b5ad3e2019-01-02 17:04:36 -050035SmartListModel::SmartListModel(const std::string& accId, QObject *parent)
Andreas Traczykb8b13ba2018-08-21 16:30:16 -040036 : QAbstractItemModel(parent),
Andreas Traczyk6b5ad3e2019-01-02 17:04:36 -050037 accId_(accId)
Andreas Traczykb8b13ba2018-08-21 16:30:16 -040038{
39}
40
41int SmartListModel::rowCount(const QModelIndex &parent) const
42{
43 if (!parent.isValid()) {
Andreas Traczyk6b5ad3e2019-01-02 17:04:36 -050044 auto& accInfo = LRCInstance::accountModel().getAccountInfo(accId_);
45 return accInfo.conversationModel->allFilteredConversations().size();
Andreas Traczykb8b13ba2018-08-21 16:30:16 -040046 }
47 return 0; // A valid QModelIndex returns 0 as no entry has sub-elements
48}
49
50int SmartListModel::columnCount(const QModelIndex &parent) const
51{
52 Q_UNUSED(parent);
53 return 1;
54}
55
56QVariant SmartListModel::data(const QModelIndex &index, int role) const
57{
58 if (!index.isValid()) {
59 return QVariant();
60 }
61
Andreas Traczyk6b5ad3e2019-01-02 17:04:36 -050062 auto& accInfo = LRCInstance::accountModel().getAccountInfo(accId_);
63 const auto& item = accInfo.conversationModel->filteredConversation(index.row());
Andreas Traczykb8b13ba2018-08-21 16:30:16 -040064 if (item.participants.size() > 0) {
65 try {
66 switch (role) {
67 case Role::Picture:
68 case Qt::DecorationRole:
Andreas Traczyk6b5ad3e2019-01-02 17:04:36 -050069 return GlobalInstances::pixmapManipulator().decorationRole(item, accInfo);
Andreas Traczykb8b13ba2018-08-21 16:30:16 -040070 case Role::DisplayName:
71 case Qt::DisplayRole:
72 {
Andreas Traczyk6b5ad3e2019-01-02 17:04:36 -050073 auto& contact = accInfo.contactModel->getContact(item.participants[0]);
Andreas Traczykb8b13ba2018-08-21 16:30:16 -040074 return QVariant(QString::fromStdString(Utils::bestNameForContact(contact)));
75 }
76 case Role::DisplayID:
77 {
Andreas Traczyk6b5ad3e2019-01-02 17:04:36 -050078 auto& contact = accInfo.contactModel->getContact(item.participants[0]);
Andreas Traczykb8b13ba2018-08-21 16:30:16 -040079 return QVariant(QString::fromStdString(Utils::bestIdForContact(contact)));
80 }
81 case Role::Presence:
82 {
Andreas Traczyk6b5ad3e2019-01-02 17:04:36 -050083 auto& contact = accInfo.contactModel->getContact(item.participants[0]);
Andreas Traczykb8b13ba2018-08-21 16:30:16 -040084 return QVariant(contact.isPresent);
85 }
86 case Role::URI:
87 {
Andreas Traczyk6b5ad3e2019-01-02 17:04:36 -050088 auto& contact = accInfo.contactModel->getContact(item.participants[0]);
Andreas Traczykb8b13ba2018-08-21 16:30:16 -040089 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 Traczyk43c08232018-10-31 13:42:09 -0400100 case Role::LastInteractionType:
101 return QVariant(Utils::toUnderlyingValue(item.interactions.at(item.lastMessageUid).type));
Andreas Traczykb8b13ba2018-08-21 16:30:16 -0400102 case Role::ContactType:
103 {
Andreas Traczyk6b5ad3e2019-01-02 17:04:36 -0500104 auto& contact = accInfo.contactModel->getContact(item.participants[0]);
Andreas Traczykb8b13ba2018-08-21 16:30:16 -0400105 return QVariant(Utils::toUnderlyingValue(contact.profileInfo.type));
106 }
107 case Role::UID:
108 return QVariant(QString::fromStdString(item.uid));
109 case Role::ContextMenuOpen:
Andreas Traczyk43c08232018-10-31 13:42:09 -0400110 return QVariant(isContextMenuOpen);
Andreas Traczykb8b13ba2018-08-21 16:30:16 -0400111 }
112 } catch (...) {}
113 }
114 return QVariant();
115}
116
117QModelIndex 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
130QModelIndex SmartListModel::parent(const QModelIndex &child) const
131{
132 Q_UNUSED(child);
133 return QModelIndex();
134}
135
136Qt::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 Traczyk6b5ad3e2019-01-02 17:04:36 -0500150
151void
152SmartListModel::setAccount(const std::string& accId)
153{
Andreas Traczykcce9d612019-01-03 10:05:31 -0500154 beginResetModel();
Andreas Traczyk6b5ad3e2019-01-02 17:04:36 -0500155 accId_ = accId;
Andreas Traczykcce9d612019-01-03 10:05:31 -0500156 endResetModel();
Andreas Traczyk6b5ad3e2019-01-02 17:04:36 -0500157}