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
if (other->_accounts.count() > 0) {
43
beginInsertRows(QModelIndex(), 0, other->_accounts.count() -1);
44
_accounts = other->_accounts;
47
_internalAccount = other->internalAccount();
48
_removedAccounts = other->_removedAccounts;
51
void CoreAccountModel::load() {
53
CoreAccountSettings s;
54
foreach(AccountId accId, s.knownAccounts()) {
55
QVariantMap map = s.retrieveAccountData(accId);
57
acc.fromVariantMap(map); // TODO Hook into kwallet/password saving stuff
60
if(Quassel::runMode() == Quassel::Monolithic && !internalAccount().isValid()) {
61
// Make sure we have an internal account in monolithic mode
63
intAcc.setInternal(true);
64
intAcc.setAccountName(tr("Internal Core"));
65
_internalAccount = createOrUpdateAccount(intAcc);
69
void CoreAccountModel::save() {
70
CoreAccountSettings s;
71
foreach(AccountId id, _removedAccounts) {
74
_removedAccounts.clear();
75
foreach(const CoreAccount &acc, accounts()) {
76
QVariantMap map = acc.toVariantMap(false); // TODO Hook into kwallet/password saving stuff
77
s.storeAccountData(acc.accountId(), map);
81
void CoreAccountModel::clear() {
83
beginRemoveRows(QModelIndex(), 0, rowCount()-1);
90
QVariant CoreAccountModel::data(const QModelIndex &index, int role) const {
91
if(!index.isValid() || index.row() >= rowCount() || index.column() >= 1)
94
const CoreAccount &acc = accounts().at(index.row());
98
return acc.accountName();
100
return QVariant::fromValue<AccountId>(acc.accountId());
102
return acc.uuid().toString();
110
CoreAccount CoreAccountModel::account(AccountId id) const {
111
int idx = findAccountIdx(id);
113
return _accounts.value(idx);
114
return CoreAccount();
117
CoreAccount CoreAccountModel::account(const QModelIndex &idx) const {
118
if(idx.isValid() && idx.row() < _accounts.count())
119
return _accounts.value(idx.row());
120
return CoreAccount();
123
QList<CoreAccount> CoreAccountModel::accounts() const {
127
QList<AccountId> CoreAccountModel::accountIds() const {
128
QList<AccountId> list;
129
foreach(const CoreAccount &acc, accounts())
130
list << acc.accountId();
134
bool CoreAccountModel::operator==(const CoreAccountModel &other) const {
135
return _accounts == other._accounts;
138
// TODO with Qt 4.6, use QAbstractItemModel move semantics to properly do this
139
AccountId CoreAccountModel::createOrUpdateAccount(const CoreAccount &newAcc) {
140
CoreAccount acc = newAcc;
142
if(acc.uuid().isNull())
143
acc.setUuid(QUuid::createUuid());
145
if(!acc.accountId().isValid()) {
148
const QList<AccountId> &ids = accountIds();
149
for(int i = 1; ; i++) {
150
if(!_removedAccounts.contains(i) && !ids.contains(i)) {
155
acc.setAccountId(newId);
158
int idx = findAccountIdx(acc.accountId());
160
if(acc.accountName() == accounts().at(idx).accountName()) {
161
_accounts[idx] = acc;
162
emit dataChanged(index(idx, 0), index(idx, 0));
164
takeAccount(acc.accountId());
170
return acc.accountId();
173
void CoreAccountModel::insertAccount(const CoreAccount &acc) {
174
if(acc.isInternal()) {
175
if(internalAccount().isValid()) {
176
qWarning() << "Trying to insert a second internal account in CoreAccountModel, ignoring";
179
_internalAccount = acc.accountId();
184
while(idx < _accounts.count() && acc.accountName() > _accounts.at(idx).accountName() && !acc.isInternal())
187
beginInsertRows(QModelIndex(), idx, idx);
188
_accounts.insert(idx, acc);
192
CoreAccount CoreAccountModel::takeAccount(AccountId accId) {
193
int idx = findAccountIdx(accId);
195
return CoreAccount();
197
beginRemoveRows(QModelIndex(), idx, idx);
198
CoreAccount acc = _accounts.takeAt(idx);
202
_internalAccount = 0;
207
void CoreAccountModel::removeAccount(AccountId accId) {
209
_removedAccounts.insert(accId);
212
QModelIndex CoreAccountModel::accountIndex(AccountId accId) const {
213
for(int i = 0; i < _accounts.count(); i++) {
214
if(_accounts.at(i).accountId() == accId)
217
return QModelIndex();
220
int CoreAccountModel::findAccountIdx(AccountId id) const {
221
QModelIndex idx = accountIndex(id);
222
return idx.isValid() ? idx.row() : -1;