1 /***************************************************************************
2 * Copyright (C) 2005-2020 by the Quassel Project *
3 * devel@quassel-irc.org *
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 2 of the License, or *
8 * (at your option) version 3. *
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. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include "coreaccountmodel.h"
23 #include "clientsettings.h"
26 CoreAccountModel::CoreAccountModel(QObject* parent)
27 : QAbstractListModel(parent)
31 CoreAccountModel::CoreAccountModel(const CoreAccountModel* other, QObject* parent)
32 : QAbstractListModel(parent)
38 void CoreAccountModel::update(const CoreAccountModel* other)
41 if (other->_accounts.count() > 0) {
42 beginInsertRows(QModelIndex(), 0, other->_accounts.count() - 1);
43 _accounts = other->_accounts;
46 _internalAccount = other->internalAccount();
47 _removedAccounts = other->_removedAccounts;
50 void CoreAccountModel::load()
53 CoreAccountSettings s;
54 foreach (AccountId accId, s.knownAccounts()) {
55 QVariantMap map = s.retrieveAccountData(accId);
57 acc.fromVariantMap(map); // TODO Hook into kwallet/password saving stuff
60 if (Quassel::runMode() == Quassel::Monolithic && !internalAccount().isValid()) {
61 // Make sure we have an internal account in monolithic mode
63 intAcc.setInternal(true);
64 intAcc.setAccountName(tr("Internal Core"));
65 _internalAccount = createOrUpdateAccount(intAcc);
69 void CoreAccountModel::save()
71 CoreAccountSettings s;
72 foreach (AccountId id, _removedAccounts) {
75 _removedAccounts.clear();
76 foreach (const CoreAccount& acc, accounts()) {
77 QVariantMap map = acc.toVariantMap(false); // TODO Hook into kwallet/password saving stuff
78 s.storeAccountData(acc.accountId(), map);
82 void CoreAccountModel::clear()
90 QVariant CoreAccountModel::data(const QModelIndex& index, int role) const
92 if (!index.isValid() || index.row() >= rowCount() || index.column() >= 1)
95 const CoreAccount& acc = accounts().at(index.row());
99 return acc.accountName();
101 return QVariant::fromValue(acc.accountId());
103 return acc.uuid().toString();
110 CoreAccount CoreAccountModel::account(AccountId id) const
112 int idx = findAccountIdx(id);
114 return _accounts.value(idx);
115 return CoreAccount();
118 CoreAccount CoreAccountModel::account(const QModelIndex& idx) const
120 if (idx.isValid() && idx.row() < _accounts.count())
121 return _accounts.value(idx.row());
122 return CoreAccount();
125 QList<CoreAccount> CoreAccountModel::accounts() const
130 QList<AccountId> CoreAccountModel::accountIds() const
132 QList<AccountId> list;
133 foreach (const CoreAccount& acc, accounts())
134 list << acc.accountId();
138 bool CoreAccountModel::operator==(const CoreAccountModel& other) const
140 return _accounts == other._accounts;
143 bool CoreAccountModel::operator!=(const CoreAccountModel& other) const
145 return !(*this == other);
148 // TODO with Qt 4.6, use QAbstractItemModel move semantics to properly do this
149 AccountId CoreAccountModel::createOrUpdateAccount(const CoreAccount& newAcc)
151 CoreAccount acc = newAcc;
153 if (acc.uuid().isNull())
154 acc.setUuid(QUuid::createUuid());
156 if (!acc.accountId().isValid()) {
159 const QList<AccountId>& ids = accountIds();
160 for (int i = 1;; i++) {
161 if (!_removedAccounts.contains(i) && !ids.contains(i)) {
166 acc.setAccountId(newId);
170 int idx = findAccountIdx(acc.accountId());
172 if (acc.accountName() == accounts().at(idx).accountName()) {
173 _accounts[idx] = acc;
174 emit dataChanged(index(idx, 0), index(idx, 0));
177 takeAccount(acc.accountId());
184 return acc.accountId();
187 void CoreAccountModel::insertAccount(const CoreAccount& acc)
189 if (acc.isInternal()) {
190 if (internalAccount().isValid()) {
191 qWarning() << "Trying to insert a second internal account in CoreAccountModel, ignoring";
194 _internalAccount = acc.accountId();
199 while (idx < _accounts.count() && acc.accountName() > _accounts.at(idx).accountName() && !acc.isInternal())
202 beginInsertRows(QModelIndex(), idx, idx);
203 _accounts.insert(idx, acc);
207 CoreAccount CoreAccountModel::takeAccount(AccountId accId)
209 int idx = findAccountIdx(accId);
211 return CoreAccount();
213 beginRemoveRows(QModelIndex(), idx, idx);
214 CoreAccount acc = _accounts.takeAt(idx);
217 if (acc.isInternal())
218 _internalAccount = 0;
223 void CoreAccountModel::removeAccount(AccountId accId)
226 _removedAccounts.insert(accId);
229 QModelIndex CoreAccountModel::accountIndex(AccountId accId) const
231 for (int i = 0; i < _accounts.count(); i++) {
232 if (_accounts.at(i).accountId() == accId)
238 int CoreAccountModel::findAccountIdx(AccountId id) const
240 QModelIndex idx = accountIndex(id);
241 return idx.isValid() ? idx.row() : -1;