Commit 181137ac authored by Karel Slaný's avatar Karel Slaný
Browse files

Merge branch 'delete-ephemeral-leftovers' into 'develop'

Delete Database when Switching to Ephemeral

When having an account with databases in local storage. The databases in local storage are deleted when the user decides to switch to databases in memory.

Addresses issue #35.

See merge request !28
parents e1cd3879 be2c1901
......@@ -51,7 +51,8 @@ void Accounts::updateNewMessageCounter(const QString &userName)
qDebug("%s()", __func__);
MessageDb *msgDb = NULL;
msgDb = globMessageDbsPtr->accessMessageDb(globSet.dbsLocation, userName);
msgDb = globMessageDbsPtr->accessMessageDb(globSet.dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
if (msgDb == NULL) {
qDebug() << "ERROR: Message database cannot open!";
return;
......@@ -129,7 +130,8 @@ bool Accounts::removeAccount(const QString &userName, bool showDialog)
/* Delete file database */
FileDb *fDb = NULL;
fDb = globFileDbsPtr->accessFileDb(globSet.dbsLocation, userName);
fDb = globFileDbsPtr->accessFileDb(globSet.dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
if (fDb == NULL) {
qDebug() << "ERROR: File database open error!";
return false;
......@@ -141,7 +143,8 @@ bool Accounts::removeAccount(const QString &userName, bool showDialog)
/* Delete message database */
MessageDb *msgDb = NULL;
msgDb = globMessageDbsPtr->accessMessageDb(globSet.dbsLocation, userName);
msgDb = globMessageDbsPtr->accessMessageDb(globSet.dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
if (msgDb == NULL) {
qDebug() << "ERROR: Message database open error!";
return false;
......@@ -180,7 +183,7 @@ bool Accounts::removeAccount(const QString &userName, bool showDialog)
*/
void Accounts::updateAccount(const QString &loginMetod,
const QString &acntName, const QString &userName,
const QString &pwd, bool isTestAccount, bool rememberPwd,
const QString &pwd, bool isTestAccount, bool rememberPwd,
bool storeToDisk, const QString &certPath)
/* ========================================================================= */
{
......@@ -206,7 +209,42 @@ void Accounts::updateAccount(const QString &loginMetod,
acntData.setPassword(pwd.trimmed());
acntData.setTestAccount(isTestAccount);
acntData.setRememberPwd(rememberPwd);
acntData.setstoreToDisk(storeToDisk);
bool oldStoreToDisk = acntData.storeToDisk();
/* Reopen database if storeToDisk changed. */
if (acntData.storeToDisk() != storeToDisk) {
MessageDb *mDb = globMessageDbsPtr->accessMessageDb(
globSet.dbsLocation, userName, oldStoreToDisk);
if (mDb != NULL) {
QString mDbFileName(mDb->fileName());
/* Reopen database. */
globMessageDbsPtr->deleteDb(mDb);
globMessageDbsPtr->accessMessageDb(globSet.dbsLocation,
userName, storeToDisk);
if (oldStoreToDisk) {
/* Delete databases from storage. */
QFile::remove(mDbFileName);
}
}
FileDb *fDb = globFileDbsPtr->accessFileDb(
globSet.dbsLocation, userName, oldStoreToDisk);
if (fDb != NULL) {
QString fDbFileName(fDb->fileName());
/* Reopen database. */
globFileDbsPtr->deleteDb(fDb);
globFileDbsPtr->accessFileDb(globSet.dbsLocation,
userName, storeToDisk);
if (oldStoreToDisk) {
/* Delete databases from storage. */
QFile::remove(fDbFileName);
}
}
/* Inform the model that message counters have changed. */
globAccountsModelPtr->updateCounters(userName, 0, 0, 0, 0);
}
acntData.setStoreToDisk(storeToDisk);
QUrl url(certPath, QUrl::TolerantMode);
url.toLocalFile();
acntData.setCertPath(url.toLocalFile());
......@@ -249,7 +287,7 @@ bool Accounts::createAccount(const QString &loginMetod,
acntData.setLoginMethod(loginMetod);
acntData.setTestAccount(isTestAccount);
acntData.setRememberPwd(rememberPwd);
acntData.setstoreToDisk(storeToDisk);
acntData.setStoreToDisk(storeToDisk);
QUrl url(certPath, QUrl::TolerantMode);
url.toLocalFile();
acntData.setCertPath(url.toLocalFile());
......@@ -311,7 +349,8 @@ void Accounts::loadModelCounters(void)
}
MessageDb *msgDb = NULL;
msgDb = globMessageDbsPtr->accessMessageDb(globSet.dbsLocation, userName);
msgDb = globMessageDbsPtr->accessMessageDb(globSet.dbsLocation,
userName, AccountListModel::globAccounts[userName].storeToDisk());
if (msgDb == NULL) {
qWarning("Cannot open message database for '%s'.",
userName.toUtf8().constData());
......
......@@ -84,7 +84,7 @@ public:
*/
Q_INVOKABLE void updateAccount(const QString &loginMetod,
const QString &acntName, const QString &userName,
const QString &pwd, bool isTestAccount, bool rememberPwd,
const QString &pwd, bool isTestAccount, bool rememberPwd,
bool storeToDisk, const QString &certPath);
/*!
......
......@@ -146,7 +146,8 @@ void Files::deleteExpiredFilesFromDbs(int days)
QStringList userNameList = AccountListModel::globAccounts.keys();
foreach (const QString &userName, userNameList) {
FileDb *fDb = NULL;
fDb = globFileDbsPtr->accessFileDb(globSet.dbsLocation, userName);
fDb = globFileDbsPtr->accessFileDb(globSet.dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
if (fDb == NULL) {
qDebug() << "ERROR: File database cannot open!" << userName;
return;
......@@ -157,12 +158,12 @@ void Files::deleteExpiredFilesFromDbs(int days)
}
MessageDb *msgDb = NULL;
msgDb = globMessageDbsPtr->accessMessageDb(globSet.dbsLocation, userName);
msgDb = globMessageDbsPtr->accessMessageDb(globSet.dbsLocation,
userName, AccountListModel::globAccounts[userName].storeToDisk());
if (msgDb == NULL) {
qDebug() << "ERROR: Message database cannot open!";
return;
}
msgDb->beginTransaction();
foreach (const QString &msgId, msgIDList) {
msgDb->setAttachmentDownloaded(msgId.toLongLong(),
......@@ -183,7 +184,8 @@ void Files::fillFileList(const QString &userName, qint64 msgId)
qDebug("%s()", __func__);
FileDb *fDb = NULL;
fDb = globFileDbsPtr->accessFileDb(globSet.dbsLocation, userName);
fDb = globFileDbsPtr->accessFileDb(globSet.dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
if (fDb == NULL) {
qDebug() << "ERROR: File database cannot open!";
......@@ -208,7 +210,8 @@ void Files::openAttachnemt(const QString &userName, qint64 msgId, int fileId)
}
FileDb *fDb = NULL;
fDb = globFileDbsPtr->accessFileDb(globSet.dbsLocation, userName);
fDb = globFileDbsPtr->accessFileDb(globSet.dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
if (fDb == NULL) {
qDebug() << "ERROR: File database cannot open!";
......@@ -265,7 +268,8 @@ void Files::sendAttachmentsWithEmail(const QString &userName, qint64 msgId)
"dd.MM.yyyy-HH:mm:ss.zzz"));
MessageDb *msgDb = NULL;
msgDb = globMessageDbsPtr->accessMessageDb(globSet.dbsLocation, userName);
msgDb = globMessageDbsPtr->accessMessageDb(globSet.dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
if (msgDb == NULL) {
qDebug() << "ERROR: Message database cannot open!";
return;
......@@ -276,7 +280,8 @@ void Files::sendAttachmentsWithEmail(const QString &userName, qint64 msgId)
}
FileDb *fDb = NULL;
fDb = globFileDbsPtr->accessFileDb(globSet.dbsLocation, userName);
fDb = globFileDbsPtr->accessFileDb(globSet.dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
if (fDb == NULL) {
qDebug() << "ERROR: File database cannot open!";
......@@ -351,7 +356,8 @@ void Files::saveAttachmentsToDisk(const QString &userName, qint64 msgId)
}
FileDb *fDb = NULL;
fDb = globFileDbsPtr->accessFileDb(globSet.dbsLocation, userName);
fDb = globFileDbsPtr->accessFileDb(globSet.dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
if (fDb == NULL) {
qDebug() << "ERROR: File database cannot open!";
......@@ -433,7 +439,8 @@ void Files::deleteFileDb(const QString &userName)
}
FileDb *fDb = NULL;
fDb = globFileDbsPtr->accessFileDb(globSet.dbsLocation, userName);
fDb = globFileDbsPtr->accessFileDb(globSet.dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
if (fDb == NULL) {
qDebug() << "ERROR: File database open error!";
return;
......@@ -444,7 +451,8 @@ void Files::deleteFileDb(const QString &userName)
}
MessageDb *msgDb = NULL;
msgDb = globMessageDbsPtr->accessMessageDb(globSet.dbsLocation, userName);
msgDb = globMessageDbsPtr->accessMessageDb(globSet.dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
if (msgDb == NULL) {
qDebug() << "ERROR: Message database cannot open!";
return;
......@@ -470,7 +478,8 @@ void Files::vacuumFileDbs(void)
QStringList userNameList = AccountListModel::globAccounts.keys();
foreach (const QString &userName, userNameList) {
FileDb *fDb = NULL;
fDb = globFileDbsPtr->accessFileDb(globSet.dbsLocation, userName);
fDb = globFileDbsPtr->accessFileDb(globSet.dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
if (fDb == NULL) {
qDebug() << "ERROR: File database cannot open!" << userName;
return;
......@@ -492,7 +501,8 @@ bool Files::deleteAttachmentsFromDb(const QString &userName, qint64 msgId)
qDebug("%s()", __func__);
FileDb *fDb = NULL;
fDb = globFileDbsPtr->accessFileDb(globSet.dbsLocation, userName);
fDb = globFileDbsPtr->accessFileDb(globSet.dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
if (fDb == NULL) {
qDebug() << "ERROR: File database cannot open!" << userName;
return false;
......@@ -501,7 +511,7 @@ bool Files::deleteAttachmentsFromDb(const QString &userName, qint64 msgId)
if (fDb->deleteFilesFromDb(msgId)) {
MessageDb *msgDb = NULL;
msgDb = globMessageDbsPtr->accessMessageDb(globSet.dbsLocation,
userName);
userName, AccountListModel::globAccounts[userName].storeToDisk());
if (msgDb == NULL) {
qDebug() << "ERROR: Message database cannot open!";
return false;
......
......@@ -59,7 +59,8 @@ void Messages::fillMessageList(const QString &userName, int msgType)
qDebug("%s()", __func__);
MessageDb *msgDb = NULL;
msgDb = globMessageDbsPtr->accessMessageDb(globSet.dbsLocation, userName);
msgDb = globMessageDbsPtr->accessMessageDb(globSet.dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
if (msgDb == NULL) {
qDebug() << "ERROR: Message database cannot open!";
return;
......@@ -80,7 +81,8 @@ void Messages::fillMessageDetail(const QString &userName, qint64 msgId)
qDebug("%s()", __func__);
MessageDb *msgDb = NULL;
msgDb = globMessageDbsPtr->accessMessageDb(globSet.dbsLocation, userName);
msgDb = globMessageDbsPtr->accessMessageDb(globSet.dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
if (msgDb == NULL) {
qDebug() << "ERROR: Message database cannot open!";
return;
......@@ -100,7 +102,8 @@ void Messages::markMessageAsLocallyRead(const QString &userName, qint64 msgId,
qDebug("%s()", __func__);
MessageDb *msgDb = NULL;
msgDb = globMessageDbsPtr->accessMessageDb(globSet.dbsLocation, userName);
msgDb = globMessageDbsPtr->accessMessageDb(globSet.dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
if (msgDb == NULL) {
qDebug() << "ERROR: Message database cannot open!";
return;
......@@ -120,7 +123,8 @@ void Messages::markMessagesAsLocallyRead(const QString &userName, bool isRead)
qDebug("%s()", __func__);
MessageDb *msgDb = NULL;
msgDb = globMessageDbsPtr->accessMessageDb(globSet.dbsLocation, userName);
msgDb = globMessageDbsPtr->accessMessageDb(globSet.dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
if (msgDb == NULL) {
qDebug() << "ERROR: Message database cannot open!";
return;
......@@ -160,14 +164,16 @@ void Messages::deleteMessageFromDbs(const QString &userName, qint64 msgId)
}
MessageDb *msgDb = NULL;
msgDb = globMessageDbsPtr->accessMessageDb(globSet.dbsLocation, userName);
msgDb = globMessageDbsPtr->accessMessageDb(globSet.dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
if (msgDb == NULL) {
qDebug() << "ERROR: Message database cannot open!" << userName;
return;
}
FileDb *fDb = NULL;
fDb = globFileDbsPtr->accessFileDb(globSet.dbsLocation, userName);
fDb = globFileDbsPtr->accessFileDb(globSet.dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
if (fDb == NULL) {
qDebug() << "ERROR: File database cannot open!" << userName;
return;
......@@ -233,24 +239,28 @@ bool Messages::moveOrCopyDbToNewLocation(const QString &newLocation)
FileDb *fDb = NULL;
if (action == "new") {
msgDb = globMessageDbsPtr->accessMessageDb(newLocation, userName);
msgDb = globMessageDbsPtr->accessMessageDb(newLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
if (msgDb == NULL) {
qDebug() << "ERROR: Message database cannot open!" << userName;
return false;
}
fDb = globFileDbsPtr->accessFileDb(newLocation, userName);
fDb = globFileDbsPtr->accessFileDb(newLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
if (fDb == NULL) {
qDebug() << "ERROR: File database cannot open!" << userName;
return false;
}
} else {
msgDb = globMessageDbsPtr->accessMessageDb(globSet.dbsLocation, userName);
msgDb = globMessageDbsPtr->accessMessageDb(globSet.dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
if (msgDb == NULL) {
qDebug() << "ERROR: Message database cannot open!" << userName;
return false;
}
fDb = globFileDbsPtr->accessFileDb(globSet.dbsLocation, userName);
fDb = globFileDbsPtr->accessFileDb(globSet.dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
if (fDb == NULL) {
qDebug() << "ERROR: File database cannot open!" << userName;
return false;
......@@ -292,7 +302,8 @@ void Messages::deleteExpiredMessagesFromDbs(int days)
foreach (const QString &userName, userNameList) {
MessageDb *msgDb = NULL;
msgDb = globMessageDbsPtr->accessMessageDb(globSet.dbsLocation, userName);
msgDb = globMessageDbsPtr->accessMessageDb(globSet.dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
if (msgDb == NULL) {
qDebug() << "ERROR: Message database cannot open!" << userName;
return;
......@@ -304,7 +315,8 @@ void Messages::deleteExpiredMessagesFromDbs(int days)
}
FileDb *fDb = NULL;
fDb = globFileDbsPtr->accessFileDb(globSet.dbsLocation, userName);
fDb = globFileDbsPtr->accessFileDb(globSet.dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
if (fDb == NULL) {
qDebug() << "ERROR: File database cannot open!" << userName;
return;
......
......@@ -261,7 +261,7 @@ bool AcntData::storeToDisk(void) const
return m_storeToDisk;
}
void AcntData::setstoreToDisk(bool storeToDisk)
void AcntData::setStoreToDisk(bool storeToDisk)
{
m_storeToDisk = storeToDisk;
}
......@@ -396,7 +396,7 @@ AcntData getAcntData(const QSettings &settings, const QString &groupName)
groupName + "/" + TEST_ACCOUNT, "").toBool());
aData.setRememberPwd(settings.value(
groupName + "/" + REMEMBER_PWD, "").toBool());
aData.setstoreToDisk(settings.value(
aData.setStoreToDisk(settings.value(
groupName + "/" + USE_DB, "").toBool());
aData.setCertPath(settings.value(
groupName + "/" + CERT_PATH, "").toString());
......
......@@ -54,7 +54,7 @@ public:
bool rememberPwd(void) const;
void setRememberPwd(bool remember);
bool storeToDisk(void) const;
void setstoreToDisk(bool storeToDisk);
void setStoreToDisk(bool storeToDisk);
QString certPath(void) const;
void setCertPath(const QString &certPath);
QString _passPhrase(void) const;
......
......@@ -46,7 +46,8 @@ bool DbWrapper::insertMessageListToDb(const QString &userName, int messageType,
/* ========================================================================= */
{
MessageDb *msgDb = NULL;
msgDb = globMessageDbsPtr->accessMessageDb(globSet.dbsLocation, userName);
msgDb = globMessageDbsPtr->accessMessageDb(globSet.dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
if (msgDb == NULL) {
txt = tr("Cannot open message database!");
return false;
......@@ -105,7 +106,8 @@ bool DbWrapper::insertCompleteMessageToDb(const QString &userName,
/* Open files database from username */
FileDb *fDb = NULL;
fDb = globFileDbsPtr->accessFileDb(globSet.dbsLocation, userName);
fDb = globFileDbsPtr->accessFileDb(globSet.dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
if (fDb == NULL) {
txt = tr("Cannot open file database!");
return false;
......@@ -124,7 +126,8 @@ bool DbWrapper::insertCompleteMessageToDb(const QString &userName,
/* Open messages database from username */
MessageDb *msgDb = NULL;
msgDb = globMessageDbsPtr->accessMessageDb(globSet.dbsLocation, userName);
msgDb = globMessageDbsPtr->accessMessageDb(globSet.dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
if (msgDb == NULL) {
txt = tr("Cannot open message database!");
return false;
......@@ -198,7 +201,8 @@ bool DbWrapper::updateAuthorInfo(const QString &userName,
/* ========================================================================= */
{
MessageDb *msgDb = NULL;
msgDb = globMessageDbsPtr->accessMessageDb(globSet.dbsLocation, userName);
msgDb = globMessageDbsPtr->accessMessageDb(globSet.dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
if (msgDb == NULL) {
txt = tr("Cannot open message database!");
return false;
......@@ -236,7 +240,8 @@ bool DbWrapper::insertMesasgeDeliveryInfoToDb(const QString &userName,
bool ret = true;
MessageDb *msgDb = NULL;
msgDb = globMessageDbsPtr->accessMessageDb(globSet.dbsLocation, userName);
msgDb = globMessageDbsPtr->accessMessageDb(globSet.dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
if (msgDb == NULL) {
txt = tr("Cannot open message database!");
return false;
......
......@@ -26,8 +26,6 @@
#include <QFileInfo>
#include <QDir>
#include "src/settings.h"
#include "src/models/accountmodel.h"
#include "src/sqlite/file_db_container.h"
/* ========================================================================= */
......@@ -55,20 +53,14 @@ FileDbContainer::~FileDbContainer(void)
* Access/create+open message database related to item.
*/
FileDb *FileDbContainer::accessFileDb(const QString &locDir,
const QString &userName)
const QString &userName, bool storeLocally)
/* ========================================================================= */
{
FileDb *db = NULL;
bool open_ret;
bool storeToDisk = false;
if (AccountListModel::globAccounts.contains(userName)) {
storeToDisk = AccountListModel::globAccounts[userName].storeToDisk();
}
QString dbFileName = constructDbFileName(userName);
/* Already opened. */
if (this->find(dbFileName) != this->end()) {
return (*this)[dbFileName];
......@@ -83,7 +75,7 @@ FileDb *FileDbContainer::accessFileDb(const QString &locDir,
QString location = locDir + QDir::separator() +
QDir::toNativeSeparators(dbFileName);
open_ret = db->openDb(location, storeToDisk);
open_ret = db->openDb(location, storeLocally);
if (!open_ret) {
delete db;
return NULL;
......
......@@ -46,9 +46,11 @@ public:
*
* @param[in] locDir Directory where to search for the file.
* @param[in] userName ISDS user name.
* @param[in] storeLocally True if it should be stored in local storage.
* @return Pointer to database, zero pointer on error.
*/
FileDb *accessFileDb(const QString &locDir, const QString &userName);
FileDb *accessFileDb(const QString &locDir, const QString &userName,
bool storeLocally);
/*!
* @brief Delete all files related to dbset.
......
......@@ -26,7 +26,6 @@
#include <QFileInfo>
#include <QDir>
#include "src/models/accountmodel.h"
#include "src/sqlite/message_db_container.h"
/* ========================================================================= */
......@@ -54,16 +53,11 @@ MsgDbContainer::~MsgDbContainer(void)
* Access/create+open message database related to item.
*/
MessageDb *MsgDbContainer::accessMessageDb(const QString &locDir,
const QString &userName)
const QString &userName, bool storeLocally)
/* ========================================================================= */
{
MessageDb *db = NULL;
bool open_ret;
bool storeToDisk = false;
if (AccountListModel::globAccounts.contains(userName)) {
storeToDisk = AccountListModel::globAccounts[userName].storeToDisk();
}
QString dbFileName = constructDbFileName(userName);
......@@ -81,7 +75,7 @@ MessageDb *MsgDbContainer::accessMessageDb(const QString &locDir,
QString location = locDir + QDir::separator() +
QDir::toNativeSeparators(dbFileName);
open_ret = db->openDb(location, storeToDisk);
open_ret = db->openDb(location, storeLocally);
if (!open_ret) {
delete db;
return NULL;
......
......@@ -24,7 +24,6 @@
#ifndef _MESSAGE_DB_CONTAINER_H_
#define _MESSAGE_DB_CONTAINER_H_
#include <QMap>
#include <QString>
......@@ -46,10 +45,11 @@ public:
*
* @param[in] locDir Directory where to search for the file.
* @param[in] userName ISDS user name, usually the login.
* @param[in] storeLocally True if it should be stored in local storage.
* @return Pointer to database, zero pointer on error.
*/
MessageDb *accessMessageDb(const QString &locDir,
const QString &userName);
const QString &userName, bool storeLocally);
/*!
* @brief Delete all files related to dbset.
......
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