blob: 8123eec1052e4dac29ef875fe87e7cc32127bbb2 [file] [log] [blame]
Sébastien Blin1f915762020-08-03 13:27:42 -04001/*
2 * Copyright (C) 2019-2020 by Savoir-faire Linux
3 * Author: Yang Wang <yang.wang@savoirfairelinux.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include "accountstomigratelistmodel.h"
20
21AccountsToMigrateListModel::AccountsToMigrateListModel(QObject *parent)
22 : QAbstractListModel(parent)
23{}
24
25AccountsToMigrateListModel::~AccountsToMigrateListModel() {}
26
27int
28AccountsToMigrateListModel::rowCount(const QModelIndex &parent) const
29{
30 if (!parent.isValid()) {
31 /*
32 * Count.
33 */
34 auto accountList = LRCInstance::accountModel().getAccountList();
35
36 int countAccountToMigrate = 0;
37
38 for (const QString &i : accountList) {
39 auto accountStatus = LRCInstance::accountModel().getAccountInfo(i).status;
40 if (accountStatus == lrc::api::account::Status::ERROR_NEED_MIGRATION) {
41 countAccountToMigrate++;
42 }
43 }
44
45 return countAccountToMigrate;
46 }
47 /*
48 * A valid QModelIndex returns 0 as no entry has sub-elements.
49 */
50 return 0;
51}
52
53int
54AccountsToMigrateListModel::columnCount(const QModelIndex &parent) const
55{
56 Q_UNUSED(parent);
57 /*
58 * Only need one column.
59 */
60 return 1;
61}
62
63QVariant
64AccountsToMigrateListModel::data(const QModelIndex &index, int role) const
65{
66 auto accountList = LRCInstance::accountModel().getAccountList();
67 if (!index.isValid() || accountList.size() <= index.row()) {
68 return QVariant();
69 }
70
71 QList<QString> accountToMigrateList;
72
73 for (QString i : accountList) {
74 auto accountStatus = LRCInstance::accountModel().getAccountInfo(i).status;
75 if (accountStatus == lrc::api::account::Status::ERROR_NEED_MIGRATION) {
76 accountToMigrateList.append(i);
77 }
78 }
79
80 QString accountId = accountToMigrateList.at(index.row());
81
82 auto &avatarInfo = LRCInstance::accountModel().getAccountInfo(accountId);
83
84 switch (role) {
85 case Role::Account_ID:
86 return QVariant(accountId);
87 case Role::ManagerUsername:
88 return QVariant(avatarInfo.confProperties.managerUsername);
89 case Role::ManagerUri:
90 return QVariant(avatarInfo.confProperties.managerUri);
91 case Role::Username:
92 return QVariant(avatarInfo.confProperties.username);
93 case Role::Alias:
94 return QVariant(LRCInstance::accountModel().getAccountInfo(accountId).profileInfo.alias);
95 case Role::Picture:
96 return QString::fromLatin1(
97 Utils::QImageToByteArray(Utils::accountPhoto(avatarInfo)).toBase64().data());
98 }
99 return QVariant();
100}
101
102QHash<int, QByteArray>
103AccountsToMigrateListModel::roleNames() const
104{
105 QHash<int, QByteArray> roles;
106 roles[Account_ID] = "Account_ID";
107 roles[ManagerUsername] = "ManagerUsername";
108 roles[ManagerUri] = "ManagerUri";
109 roles[Username] = "Username";
110 roles[Alias] = "Alias";
111 roles[Picture] = "Picture";
112 return roles;
113}
114
115QModelIndex
116AccountsToMigrateListModel::index(int row, int column, const QModelIndex &parent) const
117{
118 Q_UNUSED(parent);
119 if (column != 0) {
120 return QModelIndex();
121 }
122
123 if (row >= 0 && row < rowCount()) {
124 return createIndex(row, column);
125 }
126 return QModelIndex();
127}
128
129QModelIndex
130AccountsToMigrateListModel::parent(const QModelIndex &child) const
131{
132 Q_UNUSED(child);
133 return QModelIndex();
134}
135
136Qt::ItemFlags
137AccountsToMigrateListModel::flags(const QModelIndex &index) const
138{
139 auto flags = QAbstractItemModel::flags(index) | Qt::ItemNeverHasChildren | Qt::ItemIsSelectable;
140 if (!index.isValid()) {
141 return QAbstractItemModel::flags(index);
142 }
143 return flags;
144}
145
146void
147AccountsToMigrateListModel::reset()
148{
149 beginResetModel();
150 endResetModel();
151}