blob: 79fecd436f6dc42aba93de5f4c4016240448c069 [file] [log] [blame]
Sébastien Blin1f915762020-08-03 13:27:42 -04001/*
2 * Copyright (C) 2019-2020 by Savoir-faire Linux
3 * Author: Isa Nanic <isa.nanic@savoirfairelinux.com>
4 * Author: Yang Wang <yang.wang@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 "bannedlistmodel.h"
21#include "lrcinstance.h"
22
23BannedListModel::BannedListModel(QObject *parent)
24 : QAbstractListModel(parent)
25{}
26
27BannedListModel::~BannedListModel() {}
28
29int
30BannedListModel::rowCount(const QModelIndex &parent) const
31{
32 if (!parent.isValid()) {
33 return LRCInstance::getCurrentAccountInfo().contactModel->getBannedContacts().size();
34 }
35 return 0;
36}
37
38int
39BannedListModel::columnCount(const QModelIndex &parent) const
40{
41 Q_UNUSED(parent);
42 /*
43 * Only need one column.
44 */
45 return 1;
46}
47
48QVariant
49BannedListModel::data(const QModelIndex &index, int role) const
50{
51 auto contactList = LRCInstance::getCurrentAccountInfo().contactModel->getBannedContacts();
52 if (!index.isValid() || contactList.size() <= index.row()) {
53 return QVariant();
54 }
55
56 auto contactInfo = LRCInstance::getCurrentAccountInfo().contactModel->getContact(
57 contactList.at(index.row()));
58
59 switch (role) {
60 case Role::ContactName:
61 return QVariant(contactInfo.registeredName);
62 case Role::ContactID:
63 return QVariant(contactInfo.profileInfo.uri);
64 case Role::ContactPicture:
65 QImage avatarImage = Utils::fallbackAvatar(QSize(48, 48),
66 contactInfo.profileInfo.uri,
67 contactInfo.registeredName);
68
69 return QString::fromLatin1(Utils::QImageToByteArray(avatarImage).toBase64().data());
70 }
71 return QVariant();
72}
73
74QHash<int, QByteArray>
75BannedListModel::roleNames() const
76{
77 QHash<int, QByteArray> roles;
78 roles[ContactName] = "ContactName";
79 roles[ContactID] = "ContactID";
80 roles[ContactPicture] = "ContactPicture";
81 return roles;
82}
83
84QModelIndex
85BannedListModel::index(int row, int column, const QModelIndex &parent) const
86{
87 Q_UNUSED(parent);
88 if (column != 0) {
89 return QModelIndex();
90 }
91
92 if (row >= 0 && row < rowCount()) {
93 return createIndex(row, column);
94 }
95 return QModelIndex();
96}
97
98QModelIndex
99BannedListModel::parent(const QModelIndex &child) const
100{
101 Q_UNUSED(child);
102 return QModelIndex();
103}
104
105Qt::ItemFlags
106BannedListModel::flags(const QModelIndex &index) const
107{
108 auto flags = QAbstractItemModel::flags(index) | Qt::ItemNeverHasChildren | Qt::ItemIsSelectable;
109 if (!index.isValid()) {
110 return QAbstractItemModel::flags(index);
111 }
112 return flags;
113}
114
115void
116BannedListModel::reset()
117{
118 beginResetModel();
119 endResetModel();
120}