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 intAcc.setAccountName(tr("Internal Core"));
62 _internalAccount = createOrUpdateAccount(intAcc);
66 void CoreAccountModel::save() {
67 CoreAccountSettings s;
69 foreach(const CoreAccount &acc, accounts()) {
70 QVariantMap map = acc.toVariantMap(false); // TODO Hook into kwallet/password saving stuff
71 s.storeAccountData(acc.accountId(), map);
75 void CoreAccountModel::clear() {
77 beginRemoveRows(QModelIndex(), 0, rowCount()-1);
84 QVariant CoreAccountModel::data(const QModelIndex &index, int role) const {
85 if(!index.isValid() || index.row() >= rowCount() || index.column() >= 1)
88 const CoreAccount &acc = accounts().at(index.row());
92 return acc.accountName();
94 return QVariant::fromValue<AccountId>(acc.accountId());
96 return acc.uuid().toString();
104 CoreAccount CoreAccountModel::account(AccountId id) const {
105 int idx = findAccountIdx(id);
107 return _accounts.value(idx);
108 return CoreAccount();
111 CoreAccount CoreAccountModel::account(const QModelIndex &idx) const {
112 if(idx.isValid() && idx.row() < _accounts.count())
113 return _accounts.value(idx.row());
114 return CoreAccount();
117 QList<CoreAccount> CoreAccountModel::accounts() const {
121 QList<AccountId> CoreAccountModel::accountIds() const {
122 QList<AccountId> list;
123 foreach(const CoreAccount &acc, accounts())
124 list << acc.accountId();
128 bool CoreAccountModel::operator==(const CoreAccountModel &other) const {
129 return _accounts == other._accounts;
132 // TODO with Qt 4.6, use QAbstractItemModel move semantics to properly do this
133 AccountId CoreAccountModel::createOrUpdateAccount(const CoreAccount &newAcc) {
134 CoreAccount acc = newAcc;
136 if(acc.uuid().isNull())
137 acc.setUuid(QUuid::createUuid());
139 if(!acc.accountId().isValid()) {
142 const QList<AccountId> &ids = accountIds();
143 for(int i = 1; ; i++) {
144 if(!ids.contains(i)) {
149 acc.setAccountId(newId);
152 int idx = findAccountIdx(acc.accountId());
154 if(acc.accountName() == accounts().at(idx).accountName()) {
155 _accounts[idx] = acc;
156 emit dataChanged(index(idx, 0), index(idx, 0));
158 takeAccount(acc.accountId());
164 return acc.accountId();
167 void CoreAccountModel::insertAccount(const CoreAccount &acc) {
168 if(acc.isInternal()) {
169 if(internalAccount().isValid()) {
170 qWarning() << "Trying to insert a second internal account in CoreAccountModel, ignoring";
173 _internalAccount = acc.accountId();
178 while(idx < _accounts.count() && acc.accountName() > _accounts.at(idx).accountName() && !acc.isInternal())
181 beginInsertRows(QModelIndex(), idx, idx);
182 _accounts.insert(idx, acc);
186 CoreAccount CoreAccountModel::takeAccount(AccountId accId) {
187 int idx = findAccountIdx(accId);
189 return CoreAccount();
191 beginRemoveRows(QModelIndex(), idx, idx);
192 CoreAccount acc = _accounts.takeAt(idx);
196 _internalAccount = 0;
201 void CoreAccountModel::removeAccount(AccountId accId) {
205 QModelIndex CoreAccountModel::accountIndex(AccountId accId) const {
206 for(int i = 0; i < _accounts.count(); i++) {
207 if(_accounts.at(i).accountId() == accId)
210 return QModelIndex();
213 int CoreAccountModel::findAccountIdx(AccountId id) const {
214 QModelIndex idx = accountIndex(id);
215 return idx.isValid() ? idx.row() : -1;