X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fclient%2Fcoreaccountmodel.cpp;h=5ca81a9c438bcebb19fc022a13458d97c7f465a7;hp=1734c78894e91b2f46b597a6418999e14b915333;hb=78decd5f8d1a149fc0e62e01bd6b2886e0feadfe;hpb=0f87a72d470196f5781053927d9b91e52cc363f2 diff --git a/src/client/coreaccountmodel.cpp b/src/client/coreaccountmodel.cpp index 1734c788..5ca81a9c 100644 --- a/src/client/coreaccountmodel.cpp +++ b/src/client/coreaccountmodel.cpp @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2009 by the Quassel Project * + * Copyright (C) 2005-2016 by the Quassel Project * * devel@quassel-irc.org * * * * This program is free software; you can redistribute it and/or modify * @@ -15,7 +15,7 @@ * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * - * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * ***************************************************************************/ #include "coreaccountmodel.h" @@ -24,193 +24,234 @@ #include "quassel.h" CoreAccountModel::CoreAccountModel(QObject *parent) - : QAbstractListModel(parent), - _internalAccount(0) + : QAbstractListModel(parent), + _internalAccount(0) { - } + CoreAccountModel::CoreAccountModel(const CoreAccountModel *other, QObject *parent) - : QAbstractListModel(parent), - _internalAccount(0) -{ - update(other); -} - -void CoreAccountModel::update(const CoreAccountModel *other) { - clear(); - beginInsertRows(QModelIndex(), 0, other->_accounts.count() -1); - _internalAccount = other->internalAccount(); - _accounts = other->_accounts; - endInsertRows(); -} - -void CoreAccountModel::load() { - clear(); - CoreAccountSettings s; - foreach(AccountId accId, s.knownAccounts()) { - QVariantMap map = s.retrieveAccountData(accId); - CoreAccount acc; - acc.fromVariantMap(map); // TODO Hook into kwallet/password saving stuff - insertAccount(acc); - } - if(Quassel::runMode() == Quassel::Monolithic && !internalAccount().isValid()) { - // Make sure we have an internal account in monolithic mode - CoreAccount intAcc; - intAcc.setInternal(true); - intAcc.setAccountName(tr("Internal Core")); - _internalAccount = createOrUpdateAccount(intAcc); - } -} - -void CoreAccountModel::save() { - CoreAccountSettings s; - s.clearAccounts(); - foreach(const CoreAccount &acc, accounts()) { - QVariantMap map = acc.toVariantMap(false); // TODO Hook into kwallet/password saving stuff - s.storeAccountData(acc.accountId(), map); - } -} - -void CoreAccountModel::clear() { - if(rowCount()) { - beginRemoveRows(QModelIndex(), 0, rowCount()-1); - _internalAccount = 0; - _accounts.clear(); - endRemoveRows(); - } + : QAbstractListModel(parent), + _internalAccount(0) +{ + update(other); } -QVariant CoreAccountModel::data(const QModelIndex &index, int role) const { - if(!index.isValid() || index.row() >= rowCount() || index.column() >= 1) - return QVariant(); - const CoreAccount &acc = accounts().at(index.row()); +void CoreAccountModel::update(const CoreAccountModel *other) +{ + clear(); + if (other->_accounts.count() > 0) { + beginInsertRows(QModelIndex(), 0, other->_accounts.count() -1); + _accounts = other->_accounts; + endInsertRows(); + } + _internalAccount = other->internalAccount(); + _removedAccounts = other->_removedAccounts; +} - switch(role) { - case Qt::DisplayRole: - return acc.accountName(); - case AccountIdRole: - return QVariant::fromValue(acc.accountId()); - case UuidRole: - return acc.uuid().toString(); - default: - return QVariant(); +void CoreAccountModel::load() +{ + clear(); + CoreAccountSettings s; + foreach(AccountId accId, s.knownAccounts()) { + QVariantMap map = s.retrieveAccountData(accId); + CoreAccount acc; + acc.fromVariantMap(map); // TODO Hook into kwallet/password saving stuff + insertAccount(acc); + } + if (Quassel::runMode() == Quassel::Monolithic && !internalAccount().isValid()) { + // Make sure we have an internal account in monolithic mode + CoreAccount intAcc; + intAcc.setInternal(true); + intAcc.setAccountName(tr("Internal Core")); + _internalAccount = createOrUpdateAccount(intAcc); + } +} + - } +void CoreAccountModel::save() +{ + CoreAccountSettings s; + foreach(AccountId id, _removedAccounts) { + s.removeAccount(id); + } + _removedAccounts.clear(); + foreach(const CoreAccount &acc, accounts()) { + QVariantMap map = acc.toVariantMap(false); // TODO Hook into kwallet/password saving stuff + s.storeAccountData(acc.accountId(), map); + } } -CoreAccount CoreAccountModel::account(AccountId id) const { - int idx = findAccountIdx(id); - if(idx >= 0) - return _accounts.value(idx); - return CoreAccount(); + +void CoreAccountModel::clear() +{ + if (rowCount()) { + beginRemoveRows(QModelIndex(), 0, rowCount()-1); + _internalAccount = 0; + _accounts.clear(); + endRemoveRows(); + } } -CoreAccount CoreAccountModel::account(const QModelIndex &idx) const { - if(idx.isValid() && idx.row() < _accounts.count()) - return _accounts.value(idx.row()); - return CoreAccount(); + +QVariant CoreAccountModel::data(const QModelIndex &index, int role) const +{ + if (!index.isValid() || index.row() >= rowCount() || index.column() >= 1) + return QVariant(); + + const CoreAccount &acc = accounts().at(index.row()); + + switch (role) { + case Qt::DisplayRole: + return acc.accountName(); + case AccountIdRole: + return QVariant::fromValue(acc.accountId()); + case UuidRole: + return acc.uuid().toString(); + + default: + return QVariant(); + } } -QList CoreAccountModel::accounts() const { - return _accounts; + +CoreAccount CoreAccountModel::account(AccountId id) const +{ + int idx = findAccountIdx(id); + if (idx >= 0) + return _accounts.value(idx); + return CoreAccount(); } -QList CoreAccountModel::accountIds() const { - QList list; - foreach(const CoreAccount &acc, accounts()) + +CoreAccount CoreAccountModel::account(const QModelIndex &idx) const +{ + if (idx.isValid() && idx.row() < _accounts.count()) + return _accounts.value(idx.row()); + return CoreAccount(); +} + + +QList CoreAccountModel::accounts() const +{ + return _accounts; +} + + +QList CoreAccountModel::accountIds() const +{ + QList list; + foreach(const CoreAccount &acc, accounts()) list << acc.accountId(); - return list; + return list; } -bool CoreAccountModel::operator==(const CoreAccountModel &other) const { - return _accounts == other._accounts; + +bool CoreAccountModel::operator==(const CoreAccountModel &other) const +{ + return _accounts == other._accounts; } + // TODO with Qt 4.6, use QAbstractItemModel move semantics to properly do this -AccountId CoreAccountModel::createOrUpdateAccount(const CoreAccount &newAcc) { - CoreAccount acc = newAcc; - - if(acc.uuid().isNull()) - acc.setUuid(QUuid::createUuid()); - - if(!acc.accountId().isValid()) { - // find free Id - AccountId newId = 0; - const QList &ids = accountIds(); - for(int i = 1; ; i++) { - if(!ids.contains(i)) { - newId = i; - break; - } - } - acc.setAccountId(newId); - insertAccount(acc); - } else { - int idx = findAccountIdx(acc.accountId()); - if(idx >= 0) { - if(acc.accountName() == accounts().at(idx).accountName()) { - _accounts[idx] = acc; - emit dataChanged(index(idx, 0), index(idx, 0)); - } else { - takeAccount(acc.accountId()); +AccountId CoreAccountModel::createOrUpdateAccount(const CoreAccount &newAcc) +{ + CoreAccount acc = newAcc; + + if (acc.uuid().isNull()) + acc.setUuid(QUuid::createUuid()); + + if (!acc.accountId().isValid()) { + // find free Id + AccountId newId = 0; + const QList &ids = accountIds(); + for (int i = 1;; i++) { + if (!_removedAccounts.contains(i) && !ids.contains(i)) { + newId = i; + break; + } + } + acc.setAccountId(newId); insertAccount(acc); - } - } else - insertAccount(acc); - } - return acc.accountId(); -} - -void CoreAccountModel::insertAccount(const CoreAccount &acc) { - if(acc.isInternal()) { - if(internalAccount().isValid()) { - qWarning() << "Trying to insert a second internal account in CoreAccountModel, ignoring"; - return; } - _internalAccount = acc.accountId(); - } + else { + int idx = findAccountIdx(acc.accountId()); + if (idx >= 0) { + if (acc.accountName() == accounts().at(idx).accountName()) { + _accounts[idx] = acc; + emit dataChanged(index(idx, 0), index(idx, 0)); + } + else { + takeAccount(acc.accountId()); + insertAccount(acc); + } + } + else + insertAccount(acc); + } + return acc.accountId(); +} - // check for Quuid - int idx = 0; - while(idx < _accounts.count() && acc.accountName() > _accounts.at(idx).accountName() && !acc.isInternal()) - ++idx; - beginInsertRows(QModelIndex(), idx, idx); - _accounts.insert(idx, acc); - endInsertRows(); +void CoreAccountModel::insertAccount(const CoreAccount &acc) +{ + if (acc.isInternal()) { + if (internalAccount().isValid()) { + qWarning() << "Trying to insert a second internal account in CoreAccountModel, ignoring"; + return; + } + _internalAccount = acc.accountId(); + } + + // check for Quuid + int idx = 0; + while (idx<_accounts.count() && acc.accountName()> _accounts.at(idx).accountName() && !acc.isInternal()) + ++idx; + + beginInsertRows(QModelIndex(), idx, idx); + _accounts.insert(idx, acc); + endInsertRows(); } -CoreAccount CoreAccountModel::takeAccount(AccountId accId) { - int idx = findAccountIdx(accId); - if(idx < 0) - return CoreAccount(); - beginRemoveRows(QModelIndex(), idx, idx); - CoreAccount acc = _accounts.takeAt(idx); - endRemoveRows(); +CoreAccount CoreAccountModel::takeAccount(AccountId accId) +{ + int idx = findAccountIdx(accId); + if (idx < 0) + return CoreAccount(); + + beginRemoveRows(QModelIndex(), idx, idx); + CoreAccount acc = _accounts.takeAt(idx); + endRemoveRows(); - if(acc.isInternal()) - _internalAccount = 0; + if (acc.isInternal()) + _internalAccount = 0; - return acc; + return acc; } -void CoreAccountModel::removeAccount(AccountId accId) { - takeAccount(accId); + +void CoreAccountModel::removeAccount(AccountId accId) +{ + takeAccount(accId); + _removedAccounts.insert(accId); } -QModelIndex CoreAccountModel::accountIndex(AccountId accId) const { - for(int i = 0; i < _accounts.count(); i++) { - if(_accounts.at(i).accountId() == accId) - return index(i, 0); - } - return QModelIndex(); + +QModelIndex CoreAccountModel::accountIndex(AccountId accId) const +{ + for (int i = 0; i < _accounts.count(); i++) { + if (_accounts.at(i).accountId() == accId) + return index(i, 0); + } + return QModelIndex(); } -int CoreAccountModel::findAccountIdx(AccountId id) const { - QModelIndex idx = accountIndex(id); - return idx.isValid() ? idx.row() : -1; + +int CoreAccountModel::findAccountIdx(AccountId id) const +{ + QModelIndex idx = accountIndex(id); + return idx.isValid() ? idx.row() : -1; }