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