X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fclient%2Fcoreaccountmodel.cpp;h=8dc681ac49939e0318aff7507c24ac715b589121;hp=337334617006e9035bb94dc4b30d27fc97990de8;hb=HEAD;hpb=76db8cdfbeffaaba359c8e80cf2146da9e9e7f8a diff --git a/src/client/coreaccountmodel.cpp b/src/client/coreaccountmodel.cpp index 33733461..8dc681ac 100644 --- a/src/client/coreaccountmodel.cpp +++ b/src/client/coreaccountmodel.cpp @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2005-2013 by the Quassel Project * + * Copyright (C) 2005-2022 by the Quassel Project * * devel@quassel-irc.org * * * * This program is free software; you can redistribute it and/or modify * @@ -23,26 +23,23 @@ #include "clientsettings.h" #include "quassel.h" -CoreAccountModel::CoreAccountModel(QObject *parent) - : QAbstractListModel(parent), - _internalAccount(0) -{ -} - +CoreAccountModel::CoreAccountModel(QObject* parent) + : QAbstractListModel(parent) + , _internalAccount(0) +{} -CoreAccountModel::CoreAccountModel(const CoreAccountModel *other, QObject *parent) - : QAbstractListModel(parent), - _internalAccount(0) +CoreAccountModel::CoreAccountModel(const CoreAccountModel* other, QObject* parent) + : QAbstractListModel(parent) + , _internalAccount(0) { update(other); } - -void CoreAccountModel::update(const CoreAccountModel *other) +void CoreAccountModel::update(const CoreAccountModel* other) { clear(); if (other->_accounts.count() > 0) { - beginInsertRows(QModelIndex(), 0, other->_accounts.count() -1); + beginInsertRows(QModelIndex(), 0, other->_accounts.count() - 1); _accounts = other->_accounts; endInsertRows(); } @@ -50,15 +47,14 @@ void CoreAccountModel::update(const CoreAccountModel *other) _removedAccounts = other->_removedAccounts; } - void CoreAccountModel::load() { clear(); CoreAccountSettings s; - foreach(AccountId accId, s.knownAccounts()) { + foreach (AccountId accId, s.knownAccounts()) { QVariantMap map = s.retrieveAccountData(accId); CoreAccount acc; - acc.fromVariantMap(map); // TODO Hook into kwallet/password saving stuff + acc.fromVariantMap(map); // TODO Hook into kwallet/password saving stuff insertAccount(acc); } if (Quassel::runMode() == Quassel::Monolithic && !internalAccount().isValid()) { @@ -70,44 +66,39 @@ void CoreAccountModel::load() } } - void CoreAccountModel::save() { CoreAccountSettings s; - foreach(AccountId id, _removedAccounts) { + 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 + 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(); - } + beginResetModel(); + _internalAccount = 0; + _accounts.clear(); + endResetModel(); } - -QVariant CoreAccountModel::data(const QModelIndex &index, int role) const +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()); + const CoreAccount& acc = accounts().at(index.row()); switch (role) { case Qt::DisplayRole: return acc.accountName(); case AccountIdRole: - return QVariant::fromValue(acc.accountId()); + return QVariant::fromValue(acc.accountId()); case UuidRole: return acc.uuid().toString(); @@ -116,7 +107,6 @@ QVariant CoreAccountModel::data(const QModelIndex &index, int role) const } } - CoreAccount CoreAccountModel::account(AccountId id) const { int idx = findAccountIdx(id); @@ -125,38 +115,38 @@ CoreAccount CoreAccountModel::account(AccountId id) const return CoreAccount(); } - -CoreAccount CoreAccountModel::account(const QModelIndex &idx) const +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(); + foreach (const CoreAccount& acc, accounts()) + list << acc.accountId(); return list; } - -bool CoreAccountModel::operator==(const CoreAccountModel &other) const +bool CoreAccountModel::operator==(const CoreAccountModel& other) const { return _accounts == other._accounts; } +bool CoreAccountModel::operator!=(const CoreAccountModel& other) const +{ + return !(*this == other); +} // TODO with Qt 4.6, use QAbstractItemModel move semantics to properly do this -AccountId CoreAccountModel::createOrUpdateAccount(const CoreAccount &newAcc) +AccountId CoreAccountModel::createOrUpdateAccount(const CoreAccount& newAcc) { CoreAccount acc = newAcc; @@ -166,7 +156,7 @@ AccountId CoreAccountModel::createOrUpdateAccount(const CoreAccount &newAcc) if (!acc.accountId().isValid()) { // find free Id AccountId newId = 0; - const QList &ids = accountIds(); + const QList& ids = accountIds(); for (int i = 1;; i++) { if (!_removedAccounts.contains(i) && !ids.contains(i)) { newId = i; @@ -194,8 +184,7 @@ AccountId CoreAccountModel::createOrUpdateAccount(const CoreAccount &newAcc) return acc.accountId(); } - -void CoreAccountModel::insertAccount(const CoreAccount &acc) +void CoreAccountModel::insertAccount(const CoreAccount& acc) { if (acc.isInternal()) { if (internalAccount().isValid()) { @@ -207,7 +196,7 @@ void CoreAccountModel::insertAccount(const CoreAccount &acc) // check for Quuid int idx = 0; - while (idx<_accounts.count() && acc.accountName()> _accounts.at(idx).accountName() && !acc.isInternal()) + while (idx < _accounts.count() && acc.accountName() > _accounts.at(idx).accountName() && !acc.isInternal()) ++idx; beginInsertRows(QModelIndex(), idx, idx); @@ -215,7 +204,6 @@ void CoreAccountModel::insertAccount(const CoreAccount &acc) endInsertRows(); } - CoreAccount CoreAccountModel::takeAccount(AccountId accId) { int idx = findAccountIdx(accId); @@ -232,24 +220,21 @@ CoreAccount CoreAccountModel::takeAccount(AccountId accId) return acc; } - 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(); + return {}; } - int CoreAccountModel::findAccountIdx(AccountId id) const { QModelIndex idx = accountIndex(id);