1 /***************************************************************************
2 * Copyright (C) 2005-2014 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()
91 beginRemoveRows(QModelIndex(), 0, rowCount()-1);
99 QVariant CoreAccountModel::data(const QModelIndex &index, int role) const
101 if (!index.isValid() || index.row() >= rowCount() || index.column() >= 1)
104 const CoreAccount &acc = accounts().at(index.row());
107 case Qt::DisplayRole:
108 return acc.accountName();
110 return QVariant::fromValue<AccountId>(acc.accountId());
112 return acc.uuid().toString();
120 CoreAccount CoreAccountModel::account(AccountId id) const
122 int idx = findAccountIdx(id);
124 return _accounts.value(idx);
125 return CoreAccount();
129 CoreAccount CoreAccountModel::account(const QModelIndex &idx) const
131 if (idx.isValid() && idx.row() < _accounts.count())
132 return _accounts.value(idx.row());
133 return CoreAccount();
137 QList<CoreAccount> CoreAccountModel::accounts() const
143 QList<AccountId> CoreAccountModel::accountIds() const
145 QList<AccountId> list;
146 foreach(const CoreAccount &acc, accounts())
147 list << acc.accountId();
152 bool CoreAccountModel::operator==(const CoreAccountModel &other) const
154 return _accounts == other._accounts;
158 // TODO with Qt 4.6, use QAbstractItemModel move semantics to properly do this
159 AccountId CoreAccountModel::createOrUpdateAccount(const CoreAccount &newAcc)
161 CoreAccount acc = newAcc;
163 if (acc.uuid().isNull())
164 acc.setUuid(QUuid::createUuid());
166 if (!acc.accountId().isValid()) {
169 const QList<AccountId> &ids = accountIds();
170 for (int i = 1;; i++) {
171 if (!_removedAccounts.contains(i) && !ids.contains(i)) {
176 acc.setAccountId(newId);
180 int idx = findAccountIdx(acc.accountId());
182 if (acc.accountName() == accounts().at(idx).accountName()) {
183 _accounts[idx] = acc;
184 emit dataChanged(index(idx, 0), index(idx, 0));
187 takeAccount(acc.accountId());
194 return acc.accountId();
198 void CoreAccountModel::insertAccount(const CoreAccount &acc)
200 if (acc.isInternal()) {
201 if (internalAccount().isValid()) {
202 qWarning() << "Trying to insert a second internal account in CoreAccountModel, ignoring";
205 _internalAccount = acc.accountId();
210 while (idx<_accounts.count() && acc.accountName()> _accounts.at(idx).accountName() && !acc.isInternal())
213 beginInsertRows(QModelIndex(), idx, idx);
214 _accounts.insert(idx, acc);
219 CoreAccount CoreAccountModel::takeAccount(AccountId accId)
221 int idx = findAccountIdx(accId);
223 return CoreAccount();
225 beginRemoveRows(QModelIndex(), idx, idx);
226 CoreAccount acc = _accounts.takeAt(idx);
229 if (acc.isInternal())
230 _internalAccount = 0;
236 void CoreAccountModel::removeAccount(AccountId accId)
239 _removedAccounts.insert(accId);
243 QModelIndex CoreAccountModel::accountIndex(AccountId accId) const
245 for (int i = 0; i < _accounts.count(); i++) {
246 if (_accounts.at(i).accountId() == accId)
249 return QModelIndex();
253 int CoreAccountModel::findAccountIdx(AccountId id) const
255 QModelIndex idx = accountIndex(id);
256 return idx.isValid() ? idx.row() : -1;