1 /***************************************************************************
2 * Copyright (C) 2009 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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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),
40 void CoreAccountModel::update(const CoreAccountModel *other) {
42 beginInsertRows(QModelIndex(), 0, other->_accounts.count() -1);
43 _internalAccount = other->internalAccount();
44 _accounts = other->_accounts;
45 _removedAccounts = other->_removedAccounts;
49 void CoreAccountModel::load() {
51 CoreAccountSettings s;
52 foreach(AccountId accId, s.knownAccounts()) {
53 QVariantMap map = s.retrieveAccountData(accId);
55 acc.fromVariantMap(map); // TODO Hook into kwallet/password saving stuff
58 if(Quassel::runMode() == Quassel::Monolithic && !internalAccount().isValid()) {
59 // Make sure we have an internal account in monolithic mode
61 intAcc.setInternal(true);
62 intAcc.setAccountName(tr("Internal Core"));
63 _internalAccount = createOrUpdateAccount(intAcc);
67 void CoreAccountModel::save() {
68 CoreAccountSettings s;
69 foreach(AccountId id, _removedAccounts) {
72 _removedAccounts.clear();
73 foreach(const CoreAccount &acc, accounts()) {
74 QVariantMap map = acc.toVariantMap(false); // TODO Hook into kwallet/password saving stuff
75 s.storeAccountData(acc.accountId(), map);
79 void CoreAccountModel::clear() {
81 beginRemoveRows(QModelIndex(), 0, rowCount()-1);
88 QVariant CoreAccountModel::data(const QModelIndex &index, int role) const {
89 if(!index.isValid() || index.row() >= rowCount() || index.column() >= 1)
92 const CoreAccount &acc = accounts().at(index.row());
96 return acc.accountName();
98 return QVariant::fromValue<AccountId>(acc.accountId());
100 return acc.uuid().toString();
108 CoreAccount CoreAccountModel::account(AccountId id) const {
109 int idx = findAccountIdx(id);
111 return _accounts.value(idx);
112 return CoreAccount();
115 CoreAccount CoreAccountModel::account(const QModelIndex &idx) const {
116 if(idx.isValid() && idx.row() < _accounts.count())
117 return _accounts.value(idx.row());
118 return CoreAccount();
121 QList<CoreAccount> CoreAccountModel::accounts() const {
125 QList<AccountId> CoreAccountModel::accountIds() const {
126 QList<AccountId> list;
127 foreach(const CoreAccount &acc, accounts())
128 list << acc.accountId();
132 bool CoreAccountModel::operator==(const CoreAccountModel &other) const {
133 return _accounts == other._accounts;
136 // TODO with Qt 4.6, use QAbstractItemModel move semantics to properly do this
137 AccountId CoreAccountModel::createOrUpdateAccount(const CoreAccount &newAcc) {
138 CoreAccount acc = newAcc;
140 if(acc.uuid().isNull())
141 acc.setUuid(QUuid::createUuid());
143 if(!acc.accountId().isValid()) {
146 const QList<AccountId> &ids = accountIds();
147 for(int i = 1; ; i++) {
148 if(!_removedAccounts.contains(i) && !ids.contains(i)) {
153 acc.setAccountId(newId);
156 int idx = findAccountIdx(acc.accountId());
158 if(acc.accountName() == accounts().at(idx).accountName()) {
159 _accounts[idx] = acc;
160 emit dataChanged(index(idx, 0), index(idx, 0));
162 takeAccount(acc.accountId());
168 return acc.accountId();
171 void CoreAccountModel::insertAccount(const CoreAccount &acc) {
172 if(acc.isInternal()) {
173 if(internalAccount().isValid()) {
174 qWarning() << "Trying to insert a second internal account in CoreAccountModel, ignoring";
177 _internalAccount = acc.accountId();
182 while(idx < _accounts.count() && acc.accountName() > _accounts.at(idx).accountName() && !acc.isInternal())
185 beginInsertRows(QModelIndex(), idx, idx);
186 _accounts.insert(idx, acc);
190 CoreAccount CoreAccountModel::takeAccount(AccountId accId) {
191 int idx = findAccountIdx(accId);
193 return CoreAccount();
195 beginRemoveRows(QModelIndex(), idx, idx);
196 CoreAccount acc = _accounts.takeAt(idx);
200 _internalAccount = 0;
205 void CoreAccountModel::removeAccount(AccountId accId) {
207 _removedAccounts.insert(accId);
210 QModelIndex CoreAccountModel::accountIndex(AccountId accId) const {
211 for(int i = 0; i < _accounts.count(); i++) {
212 if(_accounts.at(i).accountId() == accId)
215 return QModelIndex();
218 int CoreAccountModel::findAccountIdx(AccountId id) const {
219 QModelIndex idx = accountIndex(id);
220 return idx.isValid() ? idx.row() : -1;