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;
48 void CoreAccountModel::load() {
50 CoreAccountSettings s;
51 foreach(AccountId accId, s.knownAccounts()) {
52 QVariantMap map = s.retrieveAccountData(accId);
54 acc.fromVariantMap(map); // TODO Hook into kwallet/password saving stuff
57 if(Quassel::runMode() == Quassel::Monolithic && !internalAccount().isValid()) {
58 // Make sure we have an internal account in monolithic mode
60 intAcc.setInternal(true);
61 insertAccount(intAcc);
65 void CoreAccountModel::save() {
66 CoreAccountSettings s;
67 foreach(const CoreAccount &acc, accounts()) {
69 continue; // FIXME don't save internal for now - but make sure to handle this correctly once mono can do remotes!
70 // we'll have to ensure that autoConnectAccount works with internal as well then
71 QVariantMap map = acc.toVariantMap(true); // TODO Hook into kwallet/password saving stuff
72 s.storeAccountData(acc.accountId(), map);
76 void CoreAccountModel::clear() {
78 beginRemoveRows(QModelIndex(), 0, rowCount()-1);
85 QVariant CoreAccountModel::data(const QModelIndex &index, int role) const {
86 if(!index.isValid() || index.row() >= rowCount() || index.column() >= 1)
89 const CoreAccount &acc = accounts().at(index.row());
93 return acc.accountName();
95 return QVariant::fromValue<AccountId>(acc.accountId());
97 return acc.uuid().toString();
105 CoreAccount CoreAccountModel::account(AccountId id) const {
106 int idx = findAccountIdx(id);
108 return _accounts.value(idx);
109 return CoreAccount();
112 CoreAccount CoreAccountModel::account(const QModelIndex &idx) const {
113 if(idx.isValid() && idx.row() < _accounts.count())
114 return _accounts.value(idx.row());
115 return CoreAccount();
118 QList<CoreAccount> CoreAccountModel::accounts() const {
122 QList<AccountId> CoreAccountModel::accountIds() const {
123 QList<AccountId> list;
124 foreach(const CoreAccount &acc, accounts())
125 list << acc.accountId();
129 bool CoreAccountModel::operator==(const CoreAccountModel &other) const {
130 return _accounts == other._accounts;
133 // TODO with Qt 4.6, use QAbstractItemModel move semantics to properly do this
134 AccountId CoreAccountModel::createOrUpdateAccount(const CoreAccount &newAcc) {
135 CoreAccount acc = newAcc;
137 if(acc.uuid().isNull())
138 acc.setUuid(QUuid::createUuid());
140 if(!acc.accountId().isValid()) {
143 const QList<AccountId> &ids = accountIds();
144 for(int i = 1; ; i++) {
145 if(!ids.contains(i)) {
150 acc.setAccountId(newId);
153 int idx = findAccountIdx(acc.accountId());
155 if(acc.accountName() == accounts().at(idx).accountName()) {
156 _accounts[idx] = acc;
157 emit dataChanged(index(idx, 0), index(idx, 0));
159 takeAccount(acc.accountId());
165 return acc.accountId();
168 void CoreAccountModel::insertAccount(const CoreAccount &acc) {
169 if(acc.isInternal()) {
170 if(Quassel::runMode() == Quassel::Monolithic)
172 if(internalAccount().isValid()) {
173 qWarning() << "Trying to insert a second internal account in CoreAccountModel, ignoring";
176 _internalAccount = acc.accountId();
181 while(idx < _accounts.count() && acc.accountName() > _accounts.at(idx).accountName() && !acc.isInternal())
184 beginInsertRows(QModelIndex(), idx, idx);
185 _accounts.insert(idx, acc);
189 CoreAccount CoreAccountModel::takeAccount(AccountId accId) {
190 int idx = findAccountIdx(accId);
192 return CoreAccount();
194 beginRemoveRows(QModelIndex(), idx, idx);
195 CoreAccount acc = _accounts.takeAt(idx);
199 _internalAccount = 0;
204 void CoreAccountModel::removeAccount(AccountId accId) {
208 QModelIndex CoreAccountModel::accountIndex(AccountId accId) const {
209 for(int i = 0; i < _accounts.count(); i++) {
210 if(_accounts.at(i).accountId() == accId)
213 return QModelIndex();
216 int CoreAccountModel::findAccountIdx(AccountId id) const {
217 QModelIndex idx = accountIndex(id);
218 return idx.isValid() ? idx.row() : -1;