Commit 5eedfcd7 authored by Martin Straka's avatar Martin Straka Committed by Karel Slaný

Change database location update (issue #31)

parent 139ecd7a
......@@ -244,9 +244,7 @@ Component {
}
}
Column {
/* TODO - deactivate this option */
visible: false
//----------------------------------
id: changeDatabaseLocation
spacing: 1
Label {
id: dbPathLabel
......@@ -270,8 +268,10 @@ Component {
var newLocation = settings.changeDbPath(dbPathText.text.toString(), false)
if (newLocation != "") {
if (newLocation != dbPathText.text) {
if (messages.moveOrCopyDbToNewLocation(newLocation)) {
var allDbsSizeInBytes = messages.getAllDbsSizeInBytes();
if (messages.moveOrCreateNewDbsToNewLocation(newLocation, allDbsSizeInBytes)) {
dbPathText.text = newLocation;
dbResetPathButton.visible = true
}
}
}
......@@ -284,8 +284,10 @@ Component {
var newLocation = settings.changeDbPath(dbPathText.text.toString(), true)
if (newLocation != "") {
if (newLocation != dbPathText.text) {
if (messages.moveOrCopyDbToNewLocation(newLocation)) {
var allDbsSizeInBytes = messages.getAllDbsSizeInBytes();
if (messages.moveOrCreateNewDbsToNewLocation(newLocation, allDbsSizeInBytes)) {
dbPathText.text = newLocation;
dbResetPathButton.visible = false
}
}
}
......@@ -314,7 +316,7 @@ Component {
color: datovkaPalette.mid
width: myWidht
wrapMode: Text.Wrap
text: "The action allows clean up local databases and reduce their size. Note: The action can take some time."
text: qsTr("The action allows clean up local databases and reduce their size. Note: The action can take some time.")
}
}
Connections {
......@@ -323,6 +325,8 @@ Component {
messageLifeSpinBox.setVal(msgLifeDays)
attachLifeSpinBox.setVal(fileLifeDays)
dbPathText.text = dbPath
changeDatabaseLocation.visible = showChangeDbLocation
dbResetPathButton.visible = showSetDefaulButton
}
}
Connections {
......
......@@ -29,6 +29,7 @@
#define APP_ORG_DOMAIN "cz.nic"
#define DEFAULT_FONT_FAMILY "System"
#define DEFAULT_FONT_SIZE 16
#define INITIAL_DB_SIZE_BYTES 1000000 /* 1 MB free disk space is required for new database */
/* HTML INFO MACRO */
......
......@@ -61,6 +61,13 @@ QString getBasePathBasedOnPlatform(void)
}
QString getDefaultDbAndConfigLocation(void)
{
return existingWritableLocation(QStandardPaths::AppDataLocation);
}
QString setFilePath(const QString &basePath, const QString &destDir)
{
QString newPath = basePath;
......
......@@ -41,6 +41,14 @@
QString getBasePathBasedOnPlatform(void);
/*!
* @brief Returns default location for database files and datovka.conf.
*
* @return Location path or empty string on error.
*/
QString getDefaultDbAndConfigLocation(void);
/*!
* @brief Set file destination path and dir.
*
......
......@@ -24,6 +24,7 @@
#include <QDebug>
#include <QMessageBox>
#include <QPushButton>
#include <QStorageInfo>
#include "src/common.h"
#include "src/messages.h"
......@@ -195,27 +196,32 @@ void Messages::deleteMessageFromDbs(const QString &userName, qint64 msgId)
/* ========================================================================= */
/*
* Slot: Move or copy databases to new location.
* Slot: Move or create new databases to new location.
*/
bool Messages::moveOrCopyDbToNewLocation(const QString &newLocation)
bool Messages::moveOrCreateNewDbsToNewLocation(const QString &newLocation,
int currentDbsSizeInBytes)
/* ========================================================================= */
{
qDebug("%s()", __func__);
QString action = "";
bool success = true;
int currentDbSize = currentDbsSizeInBytes;
/*
* select action what do you want to do
* with the currently used databases
*/
QMessageBox msgBox;
msgBox.setWindowTitle(tr("Change database location"));
msgBox.setIcon(QMessageBox::Question);
msgBox.setText(tr(
"You can move or copy the current databases to a new location or create new empty databases."));
msgBox.setInformativeText(tr(
"What do you want to do with the currently used databases?"));
msgBox.setInformativeText(tr(
"You can move current databases to a new location or you can "
"delete it and create new empty databases."));
QAbstractButton *moveButton = msgBox.addButton(tr("Move"),
QMessageBox::ActionRole);
QAbstractButton *copyButton = msgBox.addButton(tr("Copy"),
QMessageBox::ActionRole);
QAbstractButton *newButton = msgBox.addButton(tr("Create new"),
QMessageBox::ActionRole);
msgBox.setStandardButtons(QMessageBox::Cancel);
......@@ -224,59 +230,93 @@ bool Messages::moveOrCopyDbToNewLocation(const QString &newLocation)
if (msgBox.clickedButton() == moveButton) {
action = "move";
} else if (msgBox.clickedButton() == copyButton) {
action = "copy";
} else if (msgBox.clickedButton() == newButton) {
action = "new";
currentDbSize = INITIAL_DB_SIZE_BYTES;
} else {
return false;
}
/* check if new location has required size for database storing */
QStorageInfo si(newLocation);
if (si.bytesAvailable() < currentDbSize) {
QMessageBox msgBox;
msgBox.setWindowTitle(tr("New location error"));
msgBox.setIcon(QMessageBox::Critical);
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setDefaultButton(QMessageBox::Ok);
msgBox.setText(tr("It is not possible to store databases in "
"the new location because there is enough disk space."));
msgBox.setInformativeText(tr("Databases size is %1 "
"MB.").arg(currentDbsSizeInBytes/1000000));
msgBox.exec();
return false;
}
QStringList userNameList = AccountListModel::globAccounts.keys();
foreach (const QString &userName, userNameList) {
/* if account has data in memmory do nothing */
AcntData &acntData(AccountListModel::globAccounts[userName]);
if (!acntData.storeToDisk()) {
continue;
}
MessageDb *msgDb = NULL;
FileDb *fDb = NULL;
if (action == "new") {
msgDb = globMessageDbsPtr->accessMessageDb(newLocation, userName,
/* TODO - delete dbs */
msgDb = globMessageDbsPtr->accessMessageDb(newLocation,
userName,
AccountListModel::globAccounts[userName].storeToDisk());
if (msgDb == NULL) {
qDebug() << "ERROR: Message database cannot open!" << userName;
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;
qDebug() << "ERROR: File database "
"cannot open!" << userName;
return false;
}
} else {
msgDb = globMessageDbsPtr->accessMessageDb(globSet.dbsLocation, userName,
} else if (action == "move") {
msgDb = globMessageDbsPtr->accessMessageDb(
globSet.dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
if (msgDb == NULL) {
qDebug() << "ERROR: Message database cannot open!" << userName;
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;
}
if (action == "move") {
success = success && msgDb->moveDb(newLocation);
success = success && fDb->moveDb(newLocation);
} else if (action == "copy") {
success = success && msgDb->copyDb(newLocation);
success = success && fDb->copyDb(newLocation);
} else {
qDebug() << "ERROR: File database "
"cannot open!" << userName;
return false;
}
success = success && msgDb->moveDb(newLocation);
success = success && fDb->moveDb(newLocation);
} else {
return false;
}
}
/*
* TODO - if success is false,
* revert all changes and do not store new path.
*/
if (success) {
globSet.dbsLocation = newLocation;
saveAllSettings();
......@@ -333,3 +373,36 @@ void Messages::deleteExpiredMessagesFromDbs(int days)
msgDb->commitTransaction();
}
}
/* ========================================================================= */
/*
* Slot: Return size of all databases in bytes.
*/
int Messages::getAllDbsSizeInBytes(void)
/* ========================================================================= */
{
int size = 0;
QStringList userNameList = AccountListModel::globAccounts.keys();
foreach (const QString &userName, userNameList) {
MessageDb *msgDb = NULL;
msgDb = globMessageDbsPtr->accessMessageDb(globSet.dbsLocation, userName);
if (msgDb == NULL) {
qDebug() << "ERROR: Message database cannot open!" << userName;
return size;
}
size += msgDb->getDbSizeInBytes();
FileDb *fDb = NULL;
fDb = globFileDbsPtr->accessFileDb(globSet.dbsLocation, userName);
if (fDb == NULL) {
qDebug() << "ERROR: File database cannot open!" << userName;
return size;
}
size += fDb->getDbSizeInBytes();
}
return size;
}
......@@ -74,10 +74,14 @@ public:
qint64 msgId);
/*!
* @brief Move or copy databases to new location.
* @brief Move or create new databases to new location.
*
* @param[in] newLocation - New databases location.
* @param[in] currentDbsSizeInBytes - Total size of all databases.
*/
Q_INVOKABLE
bool moveOrCopyDbToNewLocation(const QString &newLocation);
bool moveOrCreateNewDbsToNewLocation(const QString &newLocation,
int currentDbsSizeInBytes);
/*!
* @brief Delete messages from all message databases together with files
......@@ -87,6 +91,12 @@ public:
*/
void deleteExpiredMessagesFromDbs(int days);
/*!
* @brief Return size of all databases in bytes.
*
*/
Q_INVOKABLE int getAllDbsSizeInBytes(void);
class Message {
public:
Message(void)
......
......@@ -45,7 +45,7 @@ Settings::Settings(void)
pinSalt(),
pinCode(),
lastUpdate(""),
dbsLocation("")
dbsLocation(getDefaultDbAndConfigLocation())
{
}
......@@ -84,10 +84,11 @@ void Settings::saveToSettings(QSettings &settings) const
}
settings.setValue(SETTINGS_LAST_UPDATE, globSet.lastUpdate);
/*
* TODO - temporarily disabled
settings.setValue(SETTINGS_DBS_LOCATION, globSet.dbsLocation);
*/
if (globSet.dbsLocation != getDefaultDbAndConfigLocation()) {
settings.setValue(SETTINGS_DBS_LOCATION, globSet.dbsLocation);
}
settings.endGroup();
}
......@@ -130,14 +131,12 @@ void Settings::loadFromSettings(const QSettings &settings)
globSet.lastUpdate = settings.value(
SETTINGS_GLOBAL_GROUP "/" SETTINGS_LAST_UPDATE).toString();
/*
* TODO - temporarily disabled
globSet.dbsLocation = settings.value(
SETTINGS_GLOBAL_GROUP "/" SETTINGS_DBS_LOCATION).toString();
*/
if (globSet.dbsLocation.isEmpty()) {
globSet.dbsLocation =
existingWritableLocation(QStandardPaths::AppDataLocation);
globSet.dbsLocation = getDefaultDbAndConfigLocation();
}
}
......@@ -149,8 +148,7 @@ void Settings::loadFromSettings(const QSettings &settings)
QString Settings::settingsPath(void)
/* ========================================================================= */
{
QString settingsPath(existingWritableLocation(
QStandardPaths::AppLocalDataLocation));
QString settingsPath(getDefaultDbAndConfigLocation());
if (settingsPath.isEmpty()) {
return QString();
......
......@@ -23,7 +23,7 @@
#include <QtDebug> // qCritical
#include <QFileDialog>
#include <QStandardPaths>
#include <QMessageBox>
#include "src/crypto/pin.h"
#include "src/crypto/wrapped.h"
......@@ -87,9 +87,18 @@ void GlobalSettingsQmlWrapper::loadSettings(const QString &section)
} else if (section == "storage") {
emit sendSettingsStorageData(globSet.msgLifeTimeInDays,
globSet.fileLifeTimeInDays, globSet.dbsLocation);
bool showSetDefaulButton =
(globSet.dbsLocation != getDefaultDbAndConfigLocation());
#if defined Q_OS_IOS
emit sendSettingsStorageData(globSet.msgLifeTimeInDays,
globSet.fileLifeTimeInDays, globSet.dbsLocation,
false, showSetDefaulButton);
#else
emit sendSettingsStorageData(globSet.msgLifeTimeInDays,
globSet.fileLifeTimeInDays, globSet.dbsLocation,
true, showSetDefaulButton);
#endif
}
}
......@@ -254,15 +263,37 @@ QString GlobalSettingsQmlWrapper::changeDbPath(const QString &currentLocation,
bool setDefaultLocation)
/* ========================================================================= */
{
QString newLocation;
if (setDefaultLocation) {
newLocation =
existingWritableLocation(QStandardPaths::AppDataLocation);
} else {
QString newLocation = getDefaultDbAndConfigLocation();
if (!setDefaultLocation) {
newLocation = QFileDialog::getExistingDirectory(0,
tr("Select directory"), currentLocation,
QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
QFileDialog::ShowDirsOnly
| QFileDialog::DontResolveSymlinks);
if (newLocation.isEmpty()) {
return QString();
}
} else {
if (currentLocation == newLocation) {
return QString();
}
}
/* check if new location is writable */
QFileInfo fi(newLocation);
if (!fi.isWritable()) {
QMessageBox msgBox;
msgBox.setWindowTitle(tr("New location error"));
msgBox.setIcon(QMessageBox::Critical);
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setDefaultButton(QMessageBox::Ok);
msgBox.setText(tr("It is not possible to store databases in the new "
"location. Write permission denied."));
msgBox.setInformativeText(tr("Action will be canceled."));
msgBox.exec();
return QString();
}
return newLocation;
}
......@@ -134,8 +134,8 @@ public:
/*!
* @brief Returns new database location string to QML.
*
* @param[in] currentLocation - Current databases location.
* @param[in] setDefaultLocation - Default databases location.
* @param[in] currentLocation - Current databases location.
* @param[in] setDefaultLocation - Default databases location.
*/
Q_INVOKABLE
QString changeDbPath(const QString &currentLocation,
......@@ -173,9 +173,12 @@ signals:
* @param[in] msgLifeDays - How many days are messages stored in db.
* @param[in] fileLifeDays - How many days are attachments stored in db.
* @param[in] dbPath - Current path to databases for QML.
* @param[in] showChangeDbLocation - true = Allow changing of database location.
* @param[in] showSetDefaulButton - true = Show setDefaultLocation button.
*/
void sendSettingsStorageData(int msgLifeDays,
int fileLifeDays, QString dbPath);
int fileLifeDays, QString dbPath, bool showChangeDbLocation,
bool showSetDefaulButton);
/*!
* @brief Send current PIN settings to QML.
......
......@@ -450,3 +450,15 @@ bool FileDb::vacuumFileDb(void)
return false;
}
/* ========================================================================= */
/*
* Get file database size in byte.
*/
int FileDb::getDbSizeInBytes(void)
/* ========================================================================= */
{
QFileInfo fi(m_db.databaseName());
return fi.size();
}
......@@ -137,6 +137,13 @@ public:
*/
bool vacuumFileDb(void);
/*!
* @brief Get file database size in bytes.
*
* @return True size in bytes.
*/
int getDbSizeInBytes(void);
private:
/*!
* @brief Returns list of tables.
......
......@@ -1024,3 +1024,15 @@ bool MessageDb::deleteMsgFromDb(qint64 msgId)
return true;
}
/* ========================================================================= */
/*
* Get message database size in byte.
*/
int MessageDb::getDbSizeInBytes(void)
/* ========================================================================= */
{
QFileInfo fi(m_db.databaseName());
return fi.size();
}
......@@ -217,6 +217,13 @@ public:
*/
bool deleteMsgFromDb(qint64 msgId);
/*!
* @brief Get message database size in bytes.
*
* @return True size in bytes.
*/
int getDbSizeInBytes(void);
private:
static
const QVector<QString> msgPrintedAttribs;
......
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