1 /***************************************************************************
2 * Copyright (C) 2005-2018 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),
33 CoreAccountModel::CoreAccountModel(const CoreAccountModel *other, QObject *parent)
34 : QAbstractListModel(parent),
41 void CoreAccountModel::update(const CoreAccountModel *other)
44 if (other->_accounts.count() > 0) {
45 beginInsertRows(QModelIndex(), 0, other->_accounts.count() -1);
46 _accounts = other->_accounts;
49 _internalAccount = other->internalAccount();
50 _removedAccounts = other->_removedAccounts;
54 void CoreAccountModel::load()
57 CoreAccountSettings s;
58 foreach(AccountId accId, s.knownAccounts()) {
59 QVariantMap map = s.retrieveAccountData(accId);
61 acc.fromVariantMap(map); // TODO Hook into kwallet/password saving stuff
64 if (Quassel::runMode() == Quassel::Monolithic && !internalAccount().isValid()) {
65 // Make sure we have an internal account in monolithic mode
67 intAcc.setInternal(true);
68 intAcc.setAccountName(tr("Internal Core"));
69 _internalAccount = createOrUpdateAccount(intAcc);
74 void CoreAccountModel::save()
76 CoreAccountSettings s;
77 foreach(AccountId id, _removedAccounts) {
80 _removedAccounts.clear();
81 foreach(const CoreAccount &acc, accounts()) {
82 QVariantMap map = acc.toVariantMap(false); // TODO Hook into kwallet/password saving stuff
83 s.storeAccountData(acc.accountId(), map);
88 void CoreAccountModel::clear()
97 QVariant CoreAccountModel::data(const QModelIndex &index, int role) const
99 if (!index.isValid() || index.row() >= rowCount() || index.column() >= 1)
102 const CoreAccount &acc = accounts().at(index.row());
105 case Qt::DisplayRole:
106 return acc.accountName();
108 return QVariant::fromValue<AccountId>(acc.accountId());
110 return acc.uuid().toString();
118 CoreAccount CoreAccountModel::account(AccountId id) const
120 int idx = findAccountIdx(id);
122 return _accounts.value(idx);
123 return CoreAccount();
127 CoreAccount CoreAccountModel::account(const QModelIndex &idx) const
129 if (idx.isValid() && idx.row() < _accounts.count())
130 return _accounts.value(idx.row());
131 return CoreAccount();
135 QList<CoreAccount> CoreAccountModel::accounts() const
141 QList<AccountId> CoreAccountModel::accountIds() const
143 QList<AccountId> list;
144 foreach(const CoreAccount &acc, accounts())
145 list << acc.accountId();
150 bool CoreAccountModel::operator==(const CoreAccountModel &other) const
152 return _accounts == other._accounts;
156 bool CoreAccountModel::operator!=(const CoreAccountModel &other) const
158 return !(*this == other);
162 // TODO with Qt 4.6, use QAbstractItemModel move semantics to properly do this
163 AccountId CoreAccountModel::createOrUpdateAccount(const CoreAccount &newAcc)
165 CoreAccount acc = newAcc;
167 if (acc.uuid().isNull())
168 acc.setUuid(QUuid::createUuid());
170 if (!acc.accountId().isValid()) {
173 const QList<AccountId> &ids = accountIds();
174 for (int i = 1;; i++) {
175 if (!_removedAccounts.contains(i) && !ids.contains(i)) {
180 acc.setAccountId(newId);
184 int idx = findAccountIdx(acc.accountId());
186 if (acc.accountName() == accounts().at(idx).accountName()) {
187 _accounts[idx] = acc;
188 emit dataChanged(index(idx, 0), index(idx, 0));
191 takeAccount(acc.accountId());
198 return acc.accountId();
202 void CoreAccountModel::insertAccount(const CoreAccount &acc)
204 if (acc.isInternal()) {
205 if (internalAccount().isValid()) {
206 qWarning() << "Trying to insert a second internal account in CoreAccountModel, ignoring";
209 _internalAccount = acc.accountId();
214 while (idx<_accounts.count() && acc.accountName()> _accounts.at(idx).accountName() && !acc.isInternal())
217 beginInsertRows(QModelIndex(), idx, idx);
218 _accounts.insert(idx, acc);
223 CoreAccount CoreAccountModel::takeAccount(AccountId accId)
225 int idx = findAccountIdx(accId);
227 return CoreAccount();
229 beginRemoveRows(QModelIndex(), idx, idx);
230 CoreAccount acc = _accounts.takeAt(idx);
233 if (acc.isInternal())
234 _internalAccount = 0;
240 void CoreAccountModel::removeAccount(AccountId accId)
243 _removedAccounts.insert(accId);
247 QModelIndex CoreAccountModel::accountIndex(AccountId accId) const
249 for (int i = 0; i < _accounts.count(); i++) {
250 if (_accounts.at(i).accountId() == accId)
253 return QModelIndex();
257 int CoreAccountModel::findAccountIdx(AccountId id) const
259 QModelIndex idx = accountIndex(id);
260 return idx.isValid() ? idx.row() : -1;