blob: be10454b28c79c5bfc1e0b85fefe75962432c05e [file] [log] [blame]
Andreas Traczykb8b13ba2018-08-21 16:30:16 -04001/***************************************************************************
2 * Copyright (C) 2017 by Savoir-faire Linux *
3 * Author: Anthony LĂ©onard <anthony.leonard@savoirfairelinux.com> *
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 Traczykb8b13ba2018-08-21 16:30:16 -040032#include "utils.h"
33
34SmartListModel::SmartListModel(const lrc::api::account::Info &acc, QObject *parent)
35 : QAbstractItemModel(parent),
36 acc_(acc)
37{
38}
39
40int SmartListModel::rowCount(const QModelIndex &parent) const
41{
42 if (!parent.isValid()) {
43 return acc_.conversationModel->allFilteredConversations().size();
44 }
45 return 0; // A valid QModelIndex returns 0 as no entry has sub-elements
46}
47
48int SmartListModel::columnCount(const QModelIndex &parent) const
49{
50 Q_UNUSED(parent);
51 return 1;
52}
53
54QVariant SmartListModel::data(const QModelIndex &index, int role) const
55{
56 if (!index.isValid()) {
57 return QVariant();
58 }
59
60 const auto& item = acc_.conversationModel->filteredConversation(index.row());
61 if (item.participants.size() > 0) {
62 try {
63 switch (role) {
64 case Role::Picture:
65 case Qt::DecorationRole:
66 return GlobalInstances::pixmapManipulator().decorationRole(item, acc_);
67 case Role::DisplayName:
68 case Qt::DisplayRole:
69 {
70 auto& contact = acc_.contactModel->getContact(item.participants[0]);
71 return QVariant(QString::fromStdString(Utils::bestNameForContact(contact)));
72 }
73 case Role::DisplayID:
74 {
75 auto& contact = acc_.contactModel->getContact(item.participants[0]);
76 return QVariant(QString::fromStdString(Utils::bestIdForContact(contact)));
77 }
78 case Role::Presence:
79 {
80 auto& contact = acc_.contactModel->getContact(item.participants[0]);
81 return QVariant(contact.isPresent);
82 }
83 case Role::URI:
84 {
85 auto& contact = acc_.contactModel->getContact(item.participants[0]);
86 return QVariant(QString::fromStdString(contact.profileInfo.uri));
87 }
88 case Role::UnreadMessagesCount:
89 return QVariant(item.unreadMessages);
90 case Role::LastInteractionDate:
91 {
92 auto& date = item.interactions.at(item.lastMessageUid).timestamp;
93 return QVariant(QString::fromStdString(Utils::formatTimeString(date)));
94 }
95 case Role::LastInteraction:
96 return QVariant(QString::fromStdString(item.interactions.at(item.lastMessageUid).body));
Andreas Traczyk43c08232018-10-31 13:42:09 -040097 case Role::LastInteractionType:
98 return QVariant(Utils::toUnderlyingValue(item.interactions.at(item.lastMessageUid).type));
Andreas Traczykb8b13ba2018-08-21 16:30:16 -040099 case Role::ContactType:
100 {
101 auto& contact = acc_.contactModel->getContact(item.participants[0]);
102 return QVariant(Utils::toUnderlyingValue(contact.profileInfo.type));
103 }
104 case Role::UID:
105 return QVariant(QString::fromStdString(item.uid));
106 case Role::ContextMenuOpen:
Andreas Traczyk43c08232018-10-31 13:42:09 -0400107 return QVariant(isContextMenuOpen);
Andreas Traczykb8b13ba2018-08-21 16:30:16 -0400108 }
109 } catch (...) {}
110 }
111 return QVariant();
112}
113
114QModelIndex SmartListModel::index(int row, int column, const QModelIndex &parent) const
115{
116 Q_UNUSED(parent);
117 if (column != 0) {
118 return QModelIndex();
119 }
120
121 if (row >= 0 && row < rowCount()) {
122 return createIndex(row, column);
123 }
124 return QModelIndex();
125}
126
127QModelIndex SmartListModel::parent(const QModelIndex &child) const
128{
129 Q_UNUSED(child);
130 return QModelIndex();
131}
132
133Qt::ItemFlags SmartListModel::flags(const QModelIndex &index) const
134{
135 auto flags = QAbstractItemModel::flags(index) | Qt::ItemNeverHasChildren | Qt::ItemIsSelectable;
136 auto type = Utils::toEnum<lrc::api::profile::Type>(data(index, Role::ContactType).value<int>());
137 auto displayName = data(index, Role::DisplayName).value<QString>();
138 auto uid = data(index, Role::UID).value<QString>();
139 if (!index.isValid()) {
140 return QAbstractItemModel::flags(index);
141 } else if ( type == lrc::api::profile::Type::TEMPORARY &&
142 uid.isEmpty()) {
143 flags &= ~(Qt::ItemIsSelectable);
144 }
145 return flags;
146}