Commit a4e96351 authored by Karel Slaný's avatar Karel Slaný

Accounts map is no longer a static global instance.

parent 3a1aa267
......@@ -79,7 +79,8 @@ void Accounts::updateNewMessageCounter(const QVariant &acntModelVariant,
qDebug("%s()", __func__);
if (Q_UNLIKELY((GlobInstcs::setPtr == Q_NULLPTR) ||
(GlobInstcs::messageDbsPtr == Q_NULLPTR))) {
(GlobInstcs::messageDbsPtr == Q_NULLPTR) ||
(GlobInstcs::acntMapPtr == Q_NULLPTR))) {
Q_ASSERT(0);
return;
}
......@@ -94,7 +95,7 @@ void Accounts::updateNewMessageCounter(const QVariant &acntModelVariant,
MessageDb *msgDb = GlobInstcs::messageDbsPtr->accessMessageDb(
GlobInstcs::setPtr->dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
(*GlobInstcs::acntMapPtr)[userName].storeToDisk());
if (msgDb == Q_NULLPTR) {
qCritical("%s", "Cannot open message database.");
return;
......@@ -121,11 +122,16 @@ void Accounts::getAccountData(const QString &userName)
{
qDebug("%s()", __func__);
if (!AccountListModel::globAccounts.contains(userName)) {
if (Q_UNLIKELY(GlobInstcs::acntMapPtr == Q_NULLPTR)) {
Q_ASSERT(0);
return;
}
if (!GlobInstcs::acntMapPtr->contains(userName)) {
return;
}
const AcntData &acntData(AccountListModel::globAccounts[userName]);
const AcntData &acntData((*GlobInstcs::acntMapPtr)[userName]);
emit sendAccountData(acntData.accountName(), userName,
acntData.loginMethod(), acntData.password(), acntData.isTestAccount(),
......@@ -185,7 +191,8 @@ bool Accounts::updateAccount(const QVariant &acntModelVariant,
if (Q_UNLIKELY((GlobInstcs::setPtr == Q_NULLPTR) ||
(GlobInstcs::messageDbsPtr == Q_NULLPTR) ||
(GlobInstcs::fileDbsPtr == Q_NULLPTR))) {
(GlobInstcs::fileDbsPtr == Q_NULLPTR) ||
(GlobInstcs::acntMapPtr == Q_NULLPTR))) {
Q_ASSERT(0);
return false;
}
......@@ -206,11 +213,11 @@ bool Accounts::updateAccount(const QVariant &acntModelVariant,
return false;
}
if (!AccountListModel::globAccounts.contains(userName)) {
if (!GlobInstcs::acntMapPtr->contains(userName)) {
return false;
}
AcntData &acntData(AccountListModel::globAccounts[userName]);
AcntData &acntData((*GlobInstcs::acntMapPtr)[userName]);
/* update account map */
acntData.setLoginMethod(loginMetod);
......@@ -259,7 +266,7 @@ bool Accounts::updateAccount(const QVariant &acntModelVariant,
acntData.setCertPath(certPath);
/* Model data have changed. */
emit AccountListModel::globAccounts.accountDataChanged(userName);
emit GlobInstcs::acntMapPtr->accountDataChanged(userName);
/* Delete isds session context */
emit removeIsdsCtx(userName);
......@@ -275,7 +282,8 @@ bool Accounts::removeAccount(const QVariant &acntModelVariant,
if (Q_UNLIKELY((GlobInstcs::setPtr == Q_NULLPTR) ||
(GlobInstcs::accountDbPtr == Q_NULLPTR) ||
(GlobInstcs::messageDbsPtr == Q_NULLPTR) ||
(GlobInstcs::fileDbsPtr == Q_NULLPTR))) {
(GlobInstcs::fileDbsPtr == Q_NULLPTR) ||
(GlobInstcs::acntMapPtr == Q_NULLPTR))) {
Q_ASSERT(0);
return false;
}
......@@ -299,14 +307,14 @@ bool Accounts::removeAccount(const QVariant &acntModelVariant,
}
}
if (!AccountListModel::globAccounts.contains(userName)) {
if (!GlobInstcs::acntMapPtr->contains(userName)) {
return false;
}
/* Delete file database */
FileDb *fDb = GlobInstcs::fileDbsPtr->accessFileDb(
GlobInstcs::setPtr->dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
(*GlobInstcs::acntMapPtr)[userName].storeToDisk());
if (fDb == Q_NULLPTR) {
qCritical("%s", "Cannot open file database.");
return false;
......@@ -319,7 +327,7 @@ bool Accounts::removeAccount(const QVariant &acntModelVariant,
/* Delete message database */
MessageDb *msgDb = GlobInstcs::messageDbsPtr->accessMessageDb(
GlobInstcs::setPtr->dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
(*GlobInstcs::acntMapPtr)[userName].storeToDisk());
if (msgDb == Q_NULLPTR) {
qCritical("%s", "Cannot open message database.");
return false;
......@@ -355,7 +363,8 @@ void Accounts::loadModelCounters(AccountListModel *accountModel)
qDebug("%s()", __func__);
if (Q_UNLIKELY((GlobInstcs::setPtr == Q_NULLPTR) ||
(GlobInstcs::messageDbsPtr == Q_NULLPTR))) {
(GlobInstcs::messageDbsPtr == Q_NULLPTR) ||
(GlobInstcs::acntMapPtr == Q_NULLPTR))) {
Q_ASSERT(0);
return;
}
......@@ -376,7 +385,7 @@ void Accounts::loadModelCounters(AccountListModel *accountModel)
MessageDb *msgDb = GlobInstcs::messageDbsPtr->accessMessageDb(
GlobInstcs::setPtr->dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
(*GlobInstcs::acntMapPtr)[userName].storeToDisk());
if (msgDb == Q_NULLPTR) {
qWarning("Cannot open message database for '%s'.",
userName.toUtf8().constData());
......
......@@ -72,17 +72,18 @@ void Files::deleteExpiredFilesFromDbs(int days)
if (Q_UNLIKELY((GlobInstcs::setPtr == Q_NULLPTR) ||
(GlobInstcs::messageDbsPtr == Q_NULLPTR) ||
(GlobInstcs::fileDbsPtr == Q_NULLPTR))) {
(GlobInstcs::fileDbsPtr == Q_NULLPTR) ||
(GlobInstcs::acntMapPtr == Q_NULLPTR))) {
Q_ASSERT(0);
return;
}
QStringList msgIDList;
QStringList userNameList = AccountListModel::globAccounts.keys();
QStringList userNameList(GlobInstcs::acntMapPtr->keys());
foreach (const QString &userName, userNameList) {
FileDb *fDb = GlobInstcs::fileDbsPtr->accessFileDb(
GlobInstcs::setPtr->dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
(*GlobInstcs::acntMapPtr)[userName].storeToDisk());
if (fDb == Q_NULLPTR) {
qCritical() << "ERROR: File database cannot open!"
<< userName;
......@@ -95,7 +96,7 @@ void Files::deleteExpiredFilesFromDbs(int days)
MessageDb *msgDb = GlobInstcs::messageDbsPtr->accessMessageDb(
GlobInstcs::setPtr->dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
(*GlobInstcs::acntMapPtr)[userName].storeToDisk());
if (msgDb == Q_NULLPTR) {
qCritical() << "ERROR: Message database cannot open!";
return;
......@@ -125,14 +126,15 @@ QByteArray Files::getFileRawContentFromDb(const QString &userName, int fileId)
qDebug("%s()", __func__);
if (Q_UNLIKELY((GlobInstcs::setPtr == Q_NULLPTR) ||
(GlobInstcs::fileDbsPtr == Q_NULLPTR))) {
(GlobInstcs::fileDbsPtr == Q_NULLPTR) ||
(GlobInstcs::acntMapPtr == Q_NULLPTR))) {
Q_ASSERT(0);
return QByteArray();
}
FileDb *fDb = GlobInstcs::fileDbsPtr->accessFileDb(
GlobInstcs::setPtr->dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
(*GlobInstcs::acntMapPtr)[userName].storeToDisk());
if (fDb == Q_NULLPTR) {
qCritical() << "Cannot open file database!";
......@@ -149,14 +151,15 @@ void Files::openAttachmentFromDb(const QString &userName, int fileId)
qDebug("%s()", __func__);
if (Q_UNLIKELY((GlobInstcs::setPtr == Q_NULLPTR) ||
(GlobInstcs::fileDbsPtr == Q_NULLPTR))) {
(GlobInstcs::fileDbsPtr == Q_NULLPTR) ||
(GlobInstcs::acntMapPtr == Q_NULLPTR))) {
Q_ASSERT(0);
return;
}
FileDb *fDb = GlobInstcs::fileDbsPtr->accessFileDb(
GlobInstcs::setPtr->dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
(*GlobInstcs::acntMapPtr)[userName].storeToDisk());
if (fDb == Q_NULLPTR) {
qCritical() << "Cannot open file database!";
......@@ -238,7 +241,8 @@ void Files::sendAttachmentsWithEmail(const QString &userName, qint64 msgId)
if (Q_UNLIKELY((GlobInstcs::setPtr == Q_NULLPTR) ||
(GlobInstcs::messageDbsPtr == Q_NULLPTR) ||
(GlobInstcs::fileDbsPtr == Q_NULLPTR))) {
(GlobInstcs::fileDbsPtr == Q_NULLPTR) ||
(GlobInstcs::acntMapPtr == Q_NULLPTR))) {
Q_ASSERT(0);
return;
}
......@@ -253,7 +257,7 @@ void Files::sendAttachmentsWithEmail(const QString &userName, qint64 msgId)
MessageDb *msgDb = GlobInstcs::messageDbsPtr->accessMessageDb(
GlobInstcs::setPtr->dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
(*GlobInstcs::acntMapPtr)[userName].storeToDisk());
if (msgDb == Q_NULLPTR) {
qCritical() << "ERROR: Message database cannot open!";
return;
......@@ -265,7 +269,7 @@ void Files::sendAttachmentsWithEmail(const QString &userName, qint64 msgId)
FileDb *fDb = GlobInstcs::fileDbsPtr->accessFileDb(
GlobInstcs::setPtr->dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
(*GlobInstcs::acntMapPtr)[userName].storeToDisk());
if (fDb == Q_NULLPTR) {
qCritical() << "ERROR: File database cannot open!";
return;
......@@ -307,7 +311,8 @@ void Files::deleteFileDb(const QString &userName)
if (Q_UNLIKELY((GlobInstcs::setPtr == Q_NULLPTR) ||
(GlobInstcs::messageDbsPtr == Q_NULLPTR) ||
(GlobInstcs::fileDbsPtr == Q_NULLPTR))) {
(GlobInstcs::fileDbsPtr == Q_NULLPTR) ||
(GlobInstcs::acntMapPtr == Q_NULLPTR))) {
Q_ASSERT(0);
return;
}
......@@ -323,7 +328,7 @@ void Files::deleteFileDb(const QString &userName)
FileDb *fDb = GlobInstcs::fileDbsPtr->accessFileDb(
GlobInstcs::setPtr->dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
(*GlobInstcs::acntMapPtr)[userName].storeToDisk());
if (fDb == Q_NULLPTR) {
qCritical() << "ERROR: File database open error!";
return;
......@@ -335,7 +340,7 @@ void Files::deleteFileDb(const QString &userName)
MessageDb *msgDb = GlobInstcs::messageDbsPtr->accessMessageDb(
GlobInstcs::setPtr->dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
(*GlobInstcs::acntMapPtr)[userName].storeToDisk());
if (msgDb == Q_NULLPTR) {
qCritical() << "ERROR: Message database cannot open!";
return;
......@@ -351,18 +356,19 @@ void Files::vacuumFileDbs(void)
qDebug("%s()", __func__);
if (Q_UNLIKELY((GlobInstcs::setPtr == Q_NULLPTR) ||
(GlobInstcs::fileDbsPtr == Q_NULLPTR))) {
(GlobInstcs::fileDbsPtr == Q_NULLPTR) ||
(GlobInstcs::acntMapPtr == Q_NULLPTR))) {
Q_ASSERT(0);
return;
}
emit statusBarTextChanged(tr("Vacuum databases"), true);
QStringList userNameList = AccountListModel::globAccounts.keys();
QStringList userNameList(GlobInstcs::acntMapPtr->keys());
foreach (const QString &userName, userNameList) {
FileDb *fDb = GlobInstcs::fileDbsPtr->accessFileDb(
GlobInstcs::setPtr->dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
(*GlobInstcs::acntMapPtr)[userName].storeToDisk());
if (fDb == Q_NULLPTR) {
qCritical() << "ERROR: File database cannot open!"
<< userName;
......@@ -380,14 +386,15 @@ bool Files::deleteAttachmentsFromDb(const QString &userName, qint64 msgId)
if (Q_UNLIKELY((GlobInstcs::setPtr == Q_NULLPTR) ||
(GlobInstcs::messageDbsPtr == Q_NULLPTR) ||
(GlobInstcs::fileDbsPtr == Q_NULLPTR))) {
(GlobInstcs::fileDbsPtr == Q_NULLPTR) ||
(GlobInstcs::acntMapPtr == Q_NULLPTR))) {
Q_ASSERT(0);
return false;
}
FileDb *fDb = GlobInstcs::fileDbsPtr->accessFileDb(
GlobInstcs::setPtr->dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
(*GlobInstcs::acntMapPtr)[userName].storeToDisk());
if (fDb == Q_NULLPTR) {
qCritical() << "ERROR: File database cannot open!" << userName;
return false;
......@@ -396,7 +403,7 @@ bool Files::deleteAttachmentsFromDb(const QString &userName, qint64 msgId)
if (fDb->deleteFilesFromDb(msgId)) {
MessageDb *msgDb = GlobInstcs::messageDbsPtr->accessMessageDb(
GlobInstcs::setPtr->dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
(*GlobInstcs::acntMapPtr)[userName].storeToDisk());
if (msgDb == Q_NULLPTR) {
qCritical() << "ERROR: Message database cannot open!";
return false;
......@@ -476,14 +483,15 @@ bool Files::setAttachmentModel(FileListModel &attachModel,
qDebug("%s()", __func__);
if (Q_UNLIKELY((GlobInstcs::setPtr == Q_NULLPTR) ||
(GlobInstcs::fileDbsPtr == Q_NULLPTR))) {
(GlobInstcs::fileDbsPtr == Q_NULLPTR) ||
(GlobInstcs::acntMapPtr == Q_NULLPTR))) {
Q_ASSERT(0);
return false;
}
FileDb *fDb = GlobInstcs::fileDbsPtr->accessFileDb(
GlobInstcs::setPtr->dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
(*GlobInstcs::acntMapPtr)[userName].storeToDisk());
if (fDb == Q_NULLPTR) {
qCritical() << "Cannot open file database!";
......@@ -549,7 +557,8 @@ void Files::saveAttachmentsToDiskDb(const QString &userName,
qDebug("%s()", __func__);
if (Q_UNLIKELY((GlobInstcs::setPtr == Q_NULLPTR) ||
(GlobInstcs::fileDbsPtr == Q_NULLPTR))) {
(GlobInstcs::fileDbsPtr == Q_NULLPTR) ||
(GlobInstcs::acntMapPtr == Q_NULLPTR))) {
Q_ASSERT(0);
return;
}
......@@ -569,7 +578,7 @@ void Files::saveAttachmentsToDiskDb(const QString &userName,
FileDb *fDb = GlobInstcs::fileDbsPtr->accessFileDb(
GlobInstcs::setPtr->dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
(*GlobInstcs::acntMapPtr)[userName].storeToDisk());
if (fDb == Q_NULLPTR) {
qCritical() << "Cannot open file database!";
......
......@@ -37,4 +37,6 @@ class FileDbContainer *GlobInstcs::fileDbsPtr = Q_NULLPTR;
class ZfoDb *GlobInstcs::zfoDbPtr = Q_NULLPTR;
class RecordsManagementDb *GlobInstcs::recMgmtDbPtr = Q_NULLPTR;
class AccountsMap *GlobInstcs::acntMapPtr = Q_NULLPTR;
class ImageProvider *GlobInstcs::imgProvPtr = Q_NULLPTR;
......@@ -50,6 +50,9 @@ public:
static
class RecordsManagementDb *recMgmtDbPtr; /*!< Records management database. */
static
class AccountsMap *acntMapPtr; /*!< Account container. */
static
class ImageProvider *imgProvPtr; /*!< Image provider. */
......
......@@ -352,15 +352,7 @@ int main(int argc, char *argv[])
&locker, SLOT(processNewState(Qt::ApplicationState)));
app.installEventFilter(&locker);
/* Create and init account model. */
AccountListModel *accountModelPtr = new (std::nothrow) AccountListModel;
if (Q_NULLPTR == accountModelPtr) {
qCritical("Cannot create account model.");
return EXIT_FAILURE;
}
accountModelPtr->setObjectName("mainAccountModel");
/* get main handle of appliaction and QML */
/* Get main handle of the application and the QML engine. */
QQmlApplicationEngine engine;
QQmlContext *ctx = engine.rootContext();
......@@ -407,9 +399,6 @@ int main(int argc, char *argv[])
ctx->setContextProperty("zfo", &zfo);
ctx->setContextProperty("recordsManagement", &recordsManagement);
/* register and set models in QML */
ctx->setContextProperty(accountModelPtr->objectName(), accountModelPtr);
/* Localise description in tables. */
accntinfTbl.reloadLocalisedDescription();
userinfTbl.reloadLocalisedDescription();
......@@ -418,7 +407,7 @@ int main(int argc, char *argv[])
evntsTbl.reloadLocalisedDescription();
msgZfoTbl.reloadLocalisedDescription();
/* Init and open account database. */
/* Initialise and open account database. */
AccountDb accountDb("ACCOUNTS", false);
MsgDbContainer messageDbs("MESSAGES");
FileDbContainer fileDbs("FILES");
......@@ -437,6 +426,12 @@ int main(int argc, char *argv[])
return EXIT_FAILURE;
}
GlobInstcs::acntMapPtr = new (std::nothrow) AccountsMap;
if (GlobInstcs::acntMapPtr == Q_NULLPTR) {
qCritical("Cannot allocate account map.");
return EXIT_FAILURE;
}
GlobInstcs::imgProvPtr = new (std::nothrow) ImageProvider;
if (GlobInstcs::imgProvPtr == Q_NULLPTR) {
qCritical("Cannot create image provider.");
......@@ -444,6 +439,18 @@ int main(int argc, char *argv[])
}
}
/* Create and initialise account model. */
AccountListModel *accountModelPtr = new (std::nothrow) AccountListModel;
if (Q_NULLPTR == accountModelPtr) {
qCritical("Cannot create account model.");
return EXIT_FAILURE;
}
accountModelPtr->setObjectName("mainAccountModel");
accountModelPtr->setAccounts(GlobInstcs::acntMapPtr);
/* Register and set account model in QML. */
ctx->setContextProperty(accountModelPtr->objectName(), accountModelPtr);
engine.addImageProvider(IMAGE_PROVIDER_ID, GlobInstcs::imgProvPtr);
QString dirName(
......@@ -581,13 +588,14 @@ int main(int argc, char *argv[])
GlobalSettingsQmlWrapper::saveAllSettings(accountModelPtr);
}
delete accountModelPtr;
accountModelPtr = Q_NULLPTR;
delete accountModelPtr; accountModelPtr = Q_NULLPTR;
/* Destroy globally accessible objects. */
{
GlobInstcs::imgProvPtr = Q_NULLPTR; /* Is owned by engine. */
delete GlobInstcs::acntMapPtr; GlobInstcs::acntMapPtr = Q_NULLPTR;
delete GlobInstcs::recMgmtDbPtr; GlobInstcs::recMgmtDbPtr = Q_NULLPTR;
GlobInstcs::zfoDbPtr = Q_NULLPTR;
GlobInstcs::fileDbsPtr = Q_NULLPTR;
......
This diff is collapsed.
......@@ -532,7 +532,13 @@ void AccountsMap::loadAccountsFromSettings(const QSettings &settings)
}
}
AccountsMap AccountListModel::globAccounts;
void AccountsMap::decryptAllPwds(const QString &pinVal)
{
for (AccountsMap::iterator it = this->begin();
it != this->end(); ++it) {
it->recomputePwd(pinVal, QString());
}
}
void AccountListModel::declareQML(void)
{
......@@ -543,21 +549,33 @@ void AccountListModel::declareQML(void)
AccountListModel::AccountListModel(QObject *parent)
: QAbstractListModel(parent),
m_userNames()
m_userNames(),
m_accountsPtr(Q_NULLPTR)
{
/* Automatically handle signalled changes. */
connect(&globAccounts, SIGNAL(accountDataChanged(QString)),
this, SLOT(handleAccountDataChange(QString)));
}
AccountListModel::AccountListModel(const AccountListModel &model,
QObject *parent)
: QAbstractListModel(parent),
m_userNames(model.m_userNames)
m_userNames(model.m_userNames),
m_accountsPtr(Q_NULLPTR)
{
setAccounts(model.m_accountsPtr);
}
void AccountListModel::setAccounts(AccountsMap *accounts)
{
if (m_accountsPtr != Q_NULLPTR) {
disconnect(m_accountsPtr, SIGNAL(accountDataChanged(QString)),
this, SLOT(handleAccountDataChange(QString)));
m_accountsPtr = Q_NULLPTR;
}
/* Automatically handle signalled changes. */
connect(&globAccounts, SIGNAL(accountDataChanged(QString)),
this, SLOT(handleAccountDataChange(QString)));
if (accounts != Q_NULLPTR) {
m_accountsPtr = accounts;
connect(m_accountsPtr, SIGNAL(accountDataChanged(QString)),
this, SLOT(handleAccountDataChange(QString)));
}
}
int AccountListModel::rowCount(const QModelIndex &parent) const
......@@ -584,6 +602,11 @@ QHash<int, QByteArray> AccountListModel::roleNames(void) const
QVariant AccountListModel::data(const QModelIndex &index, int role) const
{
if (Q_UNLIKELY(m_accountsPtr == Q_NULLPTR)) {
Q_ASSERT(0);
return QVariant();
}
if ((index.row() < 0) || (index.row() >= m_userNames.size())) {
return QVariant();
}
......@@ -594,11 +617,11 @@ QVariant AccountListModel::data(const QModelIndex &index, int role) const
return QVariant();
}
if (!globAccounts.contains(userName)) {
if (!m_accountsPtr->contains(userName)) {
Q_ASSERT(0);
return QVariant();
}
const AcntData &acntData(globAccounts[userName]);
const AcntData &acntData((*m_accountsPtr)[userName]);
switch (role) {
case ROLE_ACCOUNT_NAME:
......@@ -642,7 +665,12 @@ void AccountListModel::loadAccountsFromSettings(const QSettings &settings)
{
qDebug("%s()", __func__);
globAccounts.loadAccountsFromSettings(settings);
if (Q_UNLIKELY(m_accountsPtr == Q_NULLPTR)) {
Q_ASSERT(0);
return;
}
m_accountsPtr->loadAccountsFromSettings(settings);
QStringList groups = settings.childGroups();
QRegExp credRe(CREDENTIALS ".*");
......@@ -679,12 +707,17 @@ void AccountListModel::loadAccountsFromSettings(const QSettings &settings)
void AccountListModel::saveAccountsToSettings(const QString &pinVal,
QSettings &settings) const
{
if (Q_UNLIKELY(m_accountsPtr == Q_NULLPTR)) {
Q_ASSERT(0);
return;
}
QString groupName;
for (int row = 0; row < m_userNames.size(); ++row) {
const QString &userName(m_userNames.at(row));
Q_ASSERT(!userName.isEmpty());
const AcntData &acntData(globAccounts[userName]);
const AcntData &acntData((*m_accountsPtr)[userName]);
Q_ASSERT(userName == acntData.userName());
groupName = CREDENTIALS;
......@@ -696,6 +729,11 @@ void AccountListModel::saveAccountsToSettings(const QString &pinVal,
int AccountListModel::addAccount(const AcntData &acntData, QModelIndex *idx)
{
if (Q_UNLIKELY(m_accountsPtr == Q_NULLPTR)) {
Q_ASSERT(0);
return -1;
}
const QString userName(acntData.userName());
if (userName.isEmpty()) {
......@@ -703,7 +741,7 @@ int AccountListModel::addAccount(const AcntData &acntData, QModelIndex *idx)
return -1;
}
if (globAccounts.contains(userName)) {
if (m_accountsPtr->contains(userName)) {
qWarning("Account with user name '%s' already exists.",
userName.toUtf8().constData());
return -2;
......@@ -713,7 +751,7 @@ int AccountListModel::addAccount(const AcntData &acntData, QModelIndex *idx)
beginInsertRows(QModelIndex(), rowCount(), rowCount());
globAccounts[userName] = acntData;
(*m_accountsPtr)[userName] = acntData;
m_userNames.append(userName);
......@@ -728,6 +766,11 @@ int AccountListModel::addAccount(const AcntData &acntData, QModelIndex *idx)
void AccountListModel::deleteAccount(const QString &userName)
{
if (Q_UNLIKELY(m_accountsPtr == Q_NULLPTR)) {
Q_ASSERT(0);
return;
}
QModelIndex acntIdx(acntIndex(userName));
if (!acntIdx.isValid()) {
......@@ -740,7 +783,7 @@ void AccountListModel::deleteAccount(const QString &userName)
m_userNames.removeAt(row);
globAccounts.remove(userName);
m_accountsPtr->remove(userName);
endRemoveRows();
}
......@@ -762,9 +805,14 @@ QString AccountListModel::userName(const QModelIndex &index) const
QModelIndex AccountListModel::acntIndex(const QString &userName) const
{
if (Q_UNLIKELY(m_accountsPtr == Q_NULLPTR)) {
Q_ASSERT(0);
return QModelIndex();
}
for (int row = 0; row < m_userNames.size(); ++row) {
if (m_userNames.at(row) == userName) {
Q_ASSERT(globAccounts.contains(userName));
Q_ASSERT(m_accountsPtr->contains(userName));
return index(row, 0, QModelIndex());
}
}
......@@ -775,6 +823,11 @@ QModelIndex AccountListModel::acntIndex(const QString &userName) const
void AccountListModel::updateCounters(const QString &userName, int recNew,
int recTot, int sntNew, int sntTot)
{
if (Q_UNLIKELY(m_accountsPtr == Q_NULLPTR)) {
Q_ASSERT(0);
return;
}
if (userName.isEmpty()) {
Q_ASSERT(0);
return;
......@@ -785,38 +838,30 @@ void AccountListModel::updateCounters(const QString &userName, int recNew,
}
if (!m_userNames.contains(userName) ||
!globAccounts.contains(userName)) {
!m_accountsPtr->contains(userName)) {
Q_ASSERT(0);
return;
}
if (recNew >= 0) {
globAccounts[userName]._receivedNew = recNew;
(*m_accountsPtr)[userName]._receivedNew = recNew;
}
if (recTot >= 0) {
globAccounts[userName]._receivedTotal = recTot;
(*m_accountsPtr)[userName]._receivedTotal = recTot;
}
if (sntNew >= 0) {
globAccounts[userName]._sentNew = sntNew;
(*m_accountsPtr)[userName]._sentNew = sntNew;
}
if (sntTot >= 0) {
globAccounts[userName]._sentTotal = sntTot;
(*m_accountsPtr)[userName]._sentTotal = sntTot;
}
handleAccountDataChange(userName);
}
void AccountListModel::decryptAllPwds(const QString &pinVal)
{
for (AccountsMap::iterator it = globAccounts.begin();
it != globAccounts.end(); ++it) {
it->recomputePwd(pinVal, QString());
}
}
AccountListModel *AccountListModel::fromVariant(const QVariant &modelVariant)
{
if (!modelVariant.canConvert<QObject *>()) {
......
/*
* Copyright (C) 2014-2017 CZ.NIC
* Copyright (C) 2014-2018 CZ.NIC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
......@@ -113,6 +113,13 @@ public:
*/
void loadAccountsFromSettings(const QSettings &settings);
/*!
* @brief Decrypts all encrypted passwords.
*
* @param[in] pinVal Pin value.
*/
void decryptAllPwds(const QString &pinVal);
signals:
/*!
* @brief Notifies that account data have changed.
......@@ -131,14 +138,6 @@ class AccountListModel : public QAbstractListModel {
Q_OBJECT
public:
/*!
* @brief Holds account data related to account.
*
* @note Key is userName. The user name is held by the user name list.
*/
static
AccountsMap globAccounts;
/*!
* @brief Roles which this model supports.
*/
......@@ -176,6 +175,13 @@ public:
explicit AccountListModel(const AccountListModel &model,
QObject *parent = Q_NULLPTR);
/*!
* @brief Set account map to be the source of data for the model.
*
* @param[in] accounts Pointer to account map.
*/
void setAccounts(AccountsMap *accounts);
/*!
* @brief Return number of rows under the given parent.
*
......@@ -279,14 +285,6 @@ public:
void updateCounters(const QString &userName, int recNew, int recTot,
int sntNew, int sntTot);
/*!
* @brief Decrypts all encrypted passwords.
*
* @param[in] pinVal Pin value.
*/
static
void decryptAllPwds(const QString &pinVal);
/*!
* @brief Converts QVariant obtained from QML into model pointer.
*
......@@ -314,6 +312,7 @@ private:
* List of user names,
* defines the ordering of the model.
*/
AccountsMap *m_accountsPtr; /*!< Pointer to account data container. */
};
/* QML passes its arguments via QVariant. */
......
......@@ -113,14 +113,15 @@ bool DbWrapper::insertMessageListToDb(const QString &userName,
QList<qint64> &listOfNewMsgIds)
{
if (Q_UNLIKELY((GlobInstcs::setPtr == Q_NULLPTR) ||
(GlobInstcs::messageDbsPtr == Q_NULLPTR))) {
(GlobInstcs::messageDbsPtr == Q_NULLPTR) ||
(GlobInstcs::acntMapPtr == Q_NULLPTR))) {
Q_ASSERT(0);
return false;
}
MessageDb *msgDb = GlobInstcs::messageDbsPtr->accessMessageDb(