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

Removed globAccountsModelPtr from accounts.cpp.

parent 5cda2373
......@@ -165,7 +165,7 @@ Component {
} else if (index == 3) {
isds.changePassword(userName, acntName)
} else if (index == 4) {
if (accounts.removeAccount(userName, true)) {
if (accounts.removeAccount(accountListModel, userName, true)) {
pageView.pop(StackView.Immediate)
}
} else {
......
......@@ -89,7 +89,7 @@ Component {
MouseArea {
anchors.fill: parent
onClicked: {
accounts.updateNewMessageCounter(userName)
accounts.updateNewMessageCounter(accountListModel, userName)
statusBar.visible = false
pageView.pop(StackView.Immediate)
}
......
......@@ -312,7 +312,7 @@ Component {
/* TODO - Must be replaced via better solution in the future. */
onSaveAccountResponse: {
if (isNewAccount) {
if (accounts.createAccount(sLoginMethod,
if (accounts.createAccount(accountListModel, sLoginMethod,
accountNameTextField.text.toString(),
userNameTextField.text.toString(),
passwordTextField.text.toString(),
......@@ -322,11 +322,11 @@ Component {
accounts.saveAccountToSettings()
pageView.pop(StackView.Immediate)
} else {
accounts.removeAccount(userNameTextField.text.toString(), false)
accounts.removeAccount(accountListModel, userNameTextField.text.toString(), false)
}
}
} else {
accounts.updateAccount(sLoginMethod,
accounts.updateAccount(accountListModel, sLoginMethod,
accountNameTextField.text.toString(),
userNameTextField.text.toString(),
passwordTextField.text.toString(),
......
......@@ -21,7 +21,6 @@
* the two.
*/
#include <QDebug>
#include <QSettings>
#include <QUrl>
......@@ -41,35 +40,33 @@ Accounts::Accounts(QObject *parent)
{
}
/* ========================================================================= */
/*
* Slot: Update new message counter.
*/
void Accounts::updateNewMessageCounter(const QString &userName)
/* ========================================================================= */
void Accounts::updateNewMessageCounter(const QVariant &acntModelVariant,
const QString &userName)
{
qDebug("%s()", __func__);
MessageDb *msgDb = NULL;
msgDb = globMessageDbsPtr->accessMessageDb(globSet.dbsLocation, userName,
AccountListModel *accountModel =
AccountListModel::fromVariant(acntModelVariant);
if (accountModel == Q_NULLPTR) {
Q_ASSERT(0);
qCritical("%s", "Cannot access account model.");
return;
}
MessageDb *msgDb = globMessageDbsPtr->accessMessageDb(
globSet.dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
if (msgDb == NULL) {
qDebug() << "ERROR: Message database cannot open!";
if (msgDb == Q_NULLPTR) {
qCritical("%s", "Cannot open message database.");
return;
}
globAccountsModelPtr->updateCounters(userName,
accountModel->updateCounters(userName,
msgDb->getCntOfNewMsgs(MessageDb::TYPE_RECEIVED), -1,
msgDb->getCntOfNewMsgs(MessageDb::TYPE_SENT), -1);
}
/* ========================================================================= */
/*
* Slot: Remove account.
*/
void Accounts::getAccountData(const QString &userName)
/* ========================================================================= */
{
qDebug("%s()", __func__);
......@@ -100,17 +97,20 @@ void Accounts::getAccountData(const QString &userName)
acntData.rememberPwd(), acntData.storeToDisk(), acntData.certPath());
}
/* ========================================================================= */
/*
* Slot: Remove account.
*/
bool Accounts::removeAccount(const QString &userName, bool showDialog)
/* ========================================================================= */
bool Accounts::removeAccount(const QVariant &acntModelVariant,
const QString &userName, bool showDialogue)
{
qDebug("%s()", __func__);
if (showDialog) {
AccountListModel *accountModel =
AccountListModel::fromVariant(acntModelVariant);
if (accountModel == Q_NULLPTR) {
Q_ASSERT(0);
qCritical("%s", "Cannot access account model.");
return false;
}
if (showDialogue) {
int msgResponse = Dialogues::message(Dialogues::QUESTION,
tr("Remove account: %1").arg(userName),
tr("Do you want to remove the account '%1'?").arg(userName),
......@@ -126,40 +126,39 @@ bool Accounts::removeAccount(const QString &userName, bool showDialog)
}
/* Delete file database */
FileDb *fDb = NULL;
fDb = globFileDbsPtr->accessFileDb(globSet.dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
if (fDb == NULL) {
qDebug() << "ERROR: File database open error!";
FileDb *fDb = globFileDbsPtr->accessFileDb(globSet.dbsLocation,
userName, AccountListModel::globAccounts[userName].storeToDisk());
if (fDb == Q_NULLPTR) {
qCritical("%s", "Cannot open file database.");
return false;
}
if (!globFileDbsPtr->deleteDb(fDb)) {
qDebug() << "ERROR: File database could not delete!";
qCritical("%s", "Could not delete file database.");
return false;
}
/* Delete message database */
MessageDb *msgDb = NULL;
msgDb = globMessageDbsPtr->accessMessageDb(globSet.dbsLocation, userName,
MessageDb *msgDb = globMessageDbsPtr->accessMessageDb(
globSet.dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
if (msgDb == NULL) {
qDebug() << "ERROR: Message database open error!";
if (msgDb == Q_NULLPTR) {
qCritical("%s", "Cannot open message database.");
return false;
}
if (!globMessageDbsPtr->deleteDb(msgDb)) {
qDebug() << "ERROR: Message database could not delete!";
qCritical("%s", "Could not delete message database.");
return false;
}
/* Delete account info from account database */
if (!globAccountDbPtr->deleteAccountInfoFromDb(userName)) {
qDebug() << "ERROR: Account info could not delete!";
qCritical("%s", "Could not delete account info.");
return false;
}
/* Delete user info from account database */
if (!globAccountDbPtr->deleteUserInfoFromDb(userName)) {
qDebug() << "ERROR: User info could not delete!";
qCritical("%s", "Could not delete user info.");
return false;
}
......@@ -167,25 +166,27 @@ bool Accounts::removeAccount(const QString &userName, bool showDialog)
emit removeIsdsCtx(userName);
/* Delete account from settings */
globAccountsModelPtr->deleteAccount(userName);
accountModel->deleteAccount(userName);
saveAllSettings();
return true;
}
/* ========================================================================= */
/*
* Slot: Update account.
*/
void Accounts::updateAccount(const QString &loginMetod,
const QString &acntName, const QString &userName,
const QString &pwd, bool isTestAccount, bool rememberPwd,
bool storeToDisk, const QString &certPath)
/* ========================================================================= */
void Accounts::updateAccount(const QVariant &acntModelVariant,
const QString &loginMetod, const QString &acntName, const QString &userName,
const QString &pwd, bool isTestAccount, bool rememberPwd, bool storeToDisk,
const QString &certPath)
{
qDebug("%s()", __func__);
AccountListModel *accountModel =
AccountListModel::fromVariant(acntModelVariant);
if (accountModel == Q_NULLPTR) {
Q_ASSERT(0);
qCritical("%s", "Cannot access account model.");
return;
}
if (acntName.isEmpty()) {
Dialogues::errorMessage(Dialogues::CRITICAL,
tr("Problem while updating account"),
......@@ -212,7 +213,7 @@ void Accounts::updateAccount(const QString &loginMetod,
if (acntData.storeToDisk() != storeToDisk) {
MessageDb *mDb = globMessageDbsPtr->accessMessageDb(
globSet.dbsLocation, userName, oldStoreToDisk);
if (mDb != NULL) {
if (mDb != Q_NULLPTR) {
QString mDbFileName(mDb->fileName());
/* Reopen database. */
globMessageDbsPtr->deleteDb(mDb);
......@@ -225,7 +226,7 @@ void Accounts::updateAccount(const QString &loginMetod,
}
FileDb *fDb = globFileDbsPtr->accessFileDb(
globSet.dbsLocation, userName, oldStoreToDisk);
if (fDb != NULL) {
if (fDb != Q_NULLPTR) {
QString fDbFileName(fDb->fileName());
/* Reopen database. */
globFileDbsPtr->deleteDb(fDb);
......@@ -238,7 +239,7 @@ void Accounts::updateAccount(const QString &loginMetod,
}
/* Inform the model that message counters have changed. */
globAccountsModelPtr->updateCounters(userName, 0, 0, 0, 0);
accountModel->updateCounters(userName, 0, 0, 0, 0);
}
acntData.setStoreToDisk(storeToDisk);
......@@ -256,19 +257,21 @@ void Accounts::updateAccount(const QString &loginMetod,
saveAllSettings();
}
/* ========================================================================= */
/*
* Slot: Create account.
*/
bool Accounts::createAccount(const QString &loginMetod,
const QString &acntName, const QString &userName,
const QString &pwd, bool isTestAccount, bool rememberPwd,
bool storeToDisk, const QString &certPath)
/* ========================================================================= */
bool Accounts::createAccount(const QVariant &acntModelVariant,
const QString &loginMetod, const QString &acntName, const QString &userName,
const QString &pwd, bool isTestAccount, bool rememberPwd, bool storeToDisk,
const QString &certPath)
{
qDebug("%s()", __func__);
AccountListModel *accountModel =
AccountListModel::fromVariant(acntModelVariant);
if (accountModel == Q_NULLPTR) {
Q_ASSERT(0);
qCritical("%s", "Cannot access account model.");
return false;
}
if (userName.isEmpty() || acntName.isEmpty()) {
Dialogues::errorMessage(Dialogues::CRITICAL,
tr("Problem while creating account"),
......@@ -289,7 +292,7 @@ bool Accounts::createAccount(const QString &loginMetod,
url.toLocalFile();
acntData.setCertPath(url.toLocalFile());
if (globAccountsModelPtr->addAccount(acntData) == -2) {
if (accountModel->addAccount(acntData) == -2) {
Dialogues::errorMessage(Dialogues::CRITICAL,
tr("Creating account: %1").arg(userName),
tr("Account '%1' could not be created.").arg(acntName),
......@@ -300,7 +303,6 @@ bool Accounts::createAccount(const QString &loginMetod,
return true;
}
/* ========================================================================= */
/*
* Slot: Load account info from databse and fill QML page.
......@@ -319,7 +321,7 @@ void Accounts::saveAccountToSettings(void)
void Accounts::fillAccountInfo(const QString &userName)
/* ========================================================================= */
{
qDebug("%s()", __func__);
qDebug("%s()", __func__);
QString html = globAccountDbPtr->getAccountDetail(userName);
if (!html.isEmpty()) {
......@@ -327,34 +329,34 @@ void Accounts::fillAccountInfo(const QString &userName)
}
}
/* ========================================================================= */
/*
* Slot: Load model counters.
*/
void Accounts::loadModelCounters(void)
/* ========================================================================= */
void Accounts::loadModelCounters(AccountListModel *accountModel)
{
qDebug("%s()", __func__);
qDebug("%s()", __func__);
if (accountModel == Q_NULLPTR) {
Q_ASSERT(0);
qCritical("%s", "Cannot access account model.");
return;
}
for (int row = 0; row < globAccountsModelPtr->rowCount(); ++row) {
const QString userName(globAccountsModelPtr->userName(
globAccountsModelPtr->index(row, 0)));
for (int row = 0; row < accountModel->rowCount(); ++row) {
const QString userName(accountModel->userName(
accountModel->index(row, 0)));
if (userName.isEmpty()) {
Q_ASSERT(0);
continue;
}
MessageDb *msgDb = NULL;
msgDb = globMessageDbsPtr->accessMessageDb(globSet.dbsLocation,
userName, AccountListModel::globAccounts[userName].storeToDisk());
if (msgDb == NULL) {
MessageDb *msgDb = globMessageDbsPtr->accessMessageDb(
globSet.dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
if (msgDb == Q_NULLPTR) {
qWarning("Cannot open message database for '%s'.",
userName.toUtf8().constData());
continue;
}
globAccountsModelPtr->updateCounters(userName,
accountModel->updateCounters(userName,
msgDb->getCntOfNewMsgs(MessageDb::TYPE_RECEIVED),
msgDb->getMessageCount(MessageDb::TYPE_RECEIVED),
msgDb->getCntOfNewMsgs(MessageDb::TYPE_SENT),
......@@ -370,7 +372,6 @@ void Accounts::loadModelCounters(void)
const QString Accounts::getStorageText(bool toDisk)
/* ========================================================================= */
{
if (toDisk) return tr("local storage");
return tr("data in memory");
}
......@@ -383,7 +384,6 @@ const QString Accounts::getStorageText(bool toDisk)
const QString Accounts::getAccountTypeText(bool isTest)
/* ========================================================================= */
{
if (isTest) return tr("Test account");
return tr("Standard account");
}
......
......@@ -40,6 +40,8 @@
/* ACCOUNT DB filename */
#define ACNT_DB_NAME "accounts.db"
class AccountListModel; /* Forward declaration. */
/*
* Class Accounts provides interface between QML and account database, settings.
* Class is initialised in the main function (main.cpp)
......@@ -52,8 +54,14 @@ public:
/*!
* @brief Update new message counter.
*
* @param[in,out] acntModelVariant QVariant holding account model
* to be altered.
* @param[in] userName User name identifying account.
*/
Q_INVOKABLE void updateNewMessageCounter(const QString &userName);
Q_INVOKABLE static
void updateNewMessageCounter(const QVariant &acntModelVariant,
const QString &userName);
/*!
* @brief Load account info from databse and fill QML page.
......@@ -63,10 +71,11 @@ public:
/*!
* @brief Create account.
*/
Q_INVOKABLE bool createAccount(const QString &loginMetod,
const QString &acntName, const QString &userName,
const QString &pwd, bool isTestAccount, bool rememberPwd,
bool storeToDisk, const QString &certPath);
Q_INVOKABLE static
bool createAccount(const QVariant &acntModelVariant,
const QString &loginMetod, const QString &acntName,
const QString &userName, const QString &pwd, bool isTestAccount,
bool rememberPwd, bool storeToDisk, const QString &certPath);
/*!
* @brief Save account data to settings.
......@@ -81,21 +90,32 @@ public:
/*!
* @brief Update account.
*/
Q_INVOKABLE void updateAccount(const QString &loginMetod,
const QString &acntName, const QString &userName,
const QString &pwd, bool isTestAccount, bool rememberPwd,
bool storeToDisk, const QString &certPath);
Q_INVOKABLE
void updateAccount(const QVariant &acntModelVariant,
const QString &loginMetod, const QString &acntName,
const QString &userName, const QString &pwd, bool isTestAccount,
bool rememberPwd, bool storeToDisk, const QString &certPath);
/*!
* @brief Remove account.
*
* @param[in,out] acntModelVariant QVariant holding account model
* to be altered.
* @param[in] userName User name identifying account.
* @param[in] showDialogue Whether to notify the user using
* a dialogue.
*/
Q_INVOKABLE bool removeAccount(const QString &userName, bool showDialog);
Q_INVOKABLE
bool removeAccount(const QVariant &acntModelVariant,
const QString &userName, bool showDialogue);
/*!
* @brief Loads model counters from database.
*
* @param[in,out] accountModel Model whose counters should be updated.
*/
static
void loadModelCounters(void);
void loadModelCounters(AccountListModel *accountModel);
/*!
* @brief Received save account request from QML.
......
......@@ -376,7 +376,7 @@ int main(int argc, char *argv[])
}
/* Load counters. */
Accounts::loadModelCounters();
Accounts::loadModelCounters(globAccountsModelPtr);
/*
* Show PIN screen if needed. Encoded PIN is checked because it hasn't
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment