Commit ec8f737d authored by Martin Straka's avatar Martin Straka Committed by Karel Slaný
Browse files

Change database location update (issue #31)

parent 5eedfcd7
......@@ -253,6 +253,12 @@ bool Messages::moveOrCreateNewDbsToNewLocation(const QString &newLocation,
return false;
}
QStringList userNameMsgDone; /* username where message db location change was success */
QStringList userNameFileDone; /* username where files db location change was success */
userNameMsgDone.clear();
userNameFileDone.clear();
/* For all accounts do */
QStringList userNameList = AccountListModel::globAccounts.keys();
foreach (const QString &userName, userNameList) {
......@@ -267,24 +273,36 @@ bool Messages::moveOrCreateNewDbsToNewLocation(const QString &newLocation,
if (action == "new") {
/* TODO - delete dbs */
msgDb = globMessageDbsPtr->accessMessageDb(newLocation,
userName,
msgDb = globMessageDbsPtr->accessMessageDb(
globSet.dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
if (msgDb == NULL) {
qDebug() << "ERROR: Message database "
"cannot open!" << userName;
return false;
success = false;
break;
}
fDb = globFileDbsPtr->accessFileDb(newLocation,
userName,
fDb = globFileDbsPtr->accessFileDb(
globSet.dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
if (fDb == NULL) {
qDebug() << "ERROR: File database "
"cannot open!" << userName;
return false;
success = false;
break;
}
if (msgDb->createDb(newLocation)) {
success = false;
break;
}
userNameMsgDone.append(userName);
if (fDb->createDb(newLocation)) {
success = false;
break;
}
userNameFileDone.append(userName);
} else if (action == "move") {
......@@ -294,39 +312,72 @@ bool Messages::moveOrCreateNewDbsToNewLocation(const QString &newLocation,
if (msgDb == NULL) {
qDebug() << "ERROR: Message database "
"cannot open!" << userName;
return false;
success = false;
break;
}
fDb = globFileDbsPtr->accessFileDb(
globSet.dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
if (fDb == NULL) {
qDebug() << "ERROR: File database "
"cannot open!" << userName;
return false;
success = false;
break;
}
success = success && msgDb->moveDb(newLocation);
success = success && fDb->moveDb(newLocation);
if (msgDb->moveDb(newLocation)) {
success = false;
break;
}
userNameMsgDone.append(userName);
if (fDb->moveDb(newLocation)) {
success = false;
break;
}
userNameFileDone.append(userName);
} else {
return false;
}
}
/*
* TODO - if success is false,
* revert all changes and do not store new path.
*/
/* all databases have creaated/moved successly */
if (success) {
if (action == "new") {
/*
* TODO - here delete all databases from old location
* Use userNameMsgDone.list and userNameFileDone.list
*/
}
/* save new location to settings */
globSet.dbsLocation = newLocation;
saveAllSettings();
} else {
/* an problem has occurred */
if (action == "new") {
/*
* TODO - open back old databases in the old locationo
* and delete new databases.
* Use userNameMsgDone.list and userNameFileDone.list
*/
}
if (action == "move") {
/*
* TODO - move back databases form new loaction
* to old location
* Use userNameMsgDone.list and userNameFileDone.list
*/
}
}
return success;
}
/* ========================================================================= */
/*
* Func: Delete messages from all message databases together with files
......
......@@ -62,6 +62,103 @@ bool FileDb::openDb(const QString &fileName, bool storeToDisk)
}
/* ========================================================================= */
/*
* Delete current file db and create new in the new location.
*/
bool FileDb::deleteCurrentDbCreateNew(const QString &newLocDir)
/* ========================================================================= */
{
bool open_ret;
/* Close database. */
m_db.close();
/* Backup old file name. */
QString oldFileName = fileName();
QFileInfo fi(oldFileName);
QString fileName = fi.fileName();
QString newFileName =
newLocDir + QDir::separator() + QDir::toNativeSeparators(fileName);
/* Fail if target equals the source. */
/* TODO -- Perform a more reliable check than string comparison. */
if (oldFileName == newFileName) {
return false;
}
/* Erase target if exists. */
QFile::remove(newFileName);
/*
* Open database in the new location.
* If error durring opening of new dabatase, open back old database.
*/
open_ret = openDb(newFileName, true);
if (!open_ret) {
open_ret = openDb(oldFileName, true);
return false;
}
/* Delete file. */
return QFile::remove(oldFileName);
}
/* ========================================================================= */
/*
* Create new database in the new location.
*/
bool FileDb::createDb(const QString &newLocDir)
/* ========================================================================= */
{
/* Close database. */
m_db.close();
/* Backup old file name. */
QString oldFileName = fileName();
QFileInfo fi(oldFileName);
QString fileName = fi.fileName();
QString newFileName =
newLocDir + QDir::separator() + QDir::toNativeSeparators(fileName);
/* Fail if target equals the source. */
/* TODO -- Perform a more reliable check than string comparison. */
if (oldFileName == newFileName) {
return false;
}
/* Erase target if exists. */
QFile::remove(newFileName);
/* Open new database in the new location.*/
return openDb(newFileName, true);
}
/* ========================================================================= */
/*
* Create new database in the new location.
*/
bool FileDb::deleteDb(const QString &LocDir)
/* ========================================================================= */
{
/* Close database. */
m_db.close();
/* Backup old file name. */
QString oldFileName = fileName();
QFileInfo fi(oldFileName);
QString fileName = fi.fileName();
QString newFileName =
LocDir + QDir::separator() + QDir::toNativeSeparators(fileName);
/* Erase target if exists. */
return QFile::remove(newFileName);
}
/* ========================================================================= */
/*
* Copy db.
......@@ -91,10 +188,10 @@ bool FileDb::copyDb(const QString &newLocDir)
QFile::remove(newFileName);
/* Copy database file. */
copy_ret = QFile::copy(oldFileName, newFileName);
//copy_ret = QFile::copy(oldFileName, newFileName);
/* Open database. */
open_ret = openDb(copy_ret ? newFileName : oldFileName, true);
open_ret = openDb(newFileName, true);
if (!open_ret) {
Q_ASSERT(0);
return false;
......
......@@ -58,6 +58,30 @@ public:
*/
bool openDb(const QString &fileName, bool storeToDisk);
/*!
* @brief Delete current file db and create new in new location.
*
* @param[in] newLocDir New location name.
* @return True on success.
*/
bool deleteCurrentDbCreateNew(const QString &newLocDir);
/*!
* @brief Create new database in the new location.
*
* @param[in] newLocDir New location name.
* @return True on success.
*/
bool createDb(const QString &newLocDir);
/*!
* @brief Delete database from location.
*
* @param[in] LocDir New location name.
* @return True on success.
*/
bool deleteDb(const QString &LocDir);
/*!
* @brief Copy db.
*
......
......@@ -157,6 +157,103 @@ bool MessageDb::openDb(const QString &fileName, bool storeToDisk)
}
/* ========================================================================= */
/*
* Delete current message db and create new in the new location.
*/
bool MessageDb::deleteCurrentDbCreateNew(const QString &newLocDir)
/* ========================================================================= */
{
bool open_ret;
/* Close database. */
m_db.close();
/* Backup old file name. */
QString oldFileName = fileName();
QFileInfo fi(oldFileName);
QString fileName = fi.fileName();
QString newFileName =
newLocDir + QDir::separator() + QDir::toNativeSeparators(fileName);
/* Fail if target equals the source. */
/* TODO -- Perform a more reliable check than string comparison. */
if (oldFileName == newFileName) {
return false;
}
/* Erase target if exists. */
QFile::remove(newFileName);
/*
* Open database in the new location.
* If error durring opening of new dabatase, open back old database.
*/
open_ret = openDb(newFileName, true);
if (!open_ret) {
open_ret = openDb(oldFileName, true);
return false;
}
/* Delete file. */
return QFile::remove(oldFileName);
}
/* ========================================================================= */
/*
* Create new database in the new location.
*/
bool MessageDb::createDb(const QString &newLocDir)
/* ========================================================================= */
{
/* Close database. */
m_db.close();
/* Backup old file name. */
QString oldFileName = fileName();
QFileInfo fi(oldFileName);
QString fileName = fi.fileName();
QString newFileName =
newLocDir + QDir::separator() + QDir::toNativeSeparators(fileName);
/* Fail if target equals the source. */
/* TODO -- Perform a more reliable check than string comparison. */
if (oldFileName == newFileName) {
return false;
}
/* Erase target if exists. */
QFile::remove(newFileName);
/* Open new database in the new location.*/
return openDb(newFileName, true);
}
/* ========================================================================= */
/*
* Create new database in the new location.
*/
bool MessageDb::deleteDb(const QString &LocDir)
/* ========================================================================= */
{
/* Close database. */
m_db.close();
/* Backup old file name. */
QString oldFileName = fileName();
QFileInfo fi(oldFileName);
QString fileName = fi.fileName();
QString newFileName =
LocDir + QDir::separator() + QDir::toNativeSeparators(fileName);
/* Erase target if exists. */
return QFile::remove(newFileName);
}
/* ========================================================================= */
/*
* Copy db.
......@@ -189,7 +286,7 @@ bool MessageDb::copyDb(const QString &newLocDir)
copy_ret = QFile::copy(oldFileName, newFileName);
/* Open database. */
open_ret = openDb(copy_ret ? newFileName : oldFileName, true);
open_ret = openDb(newFileName, true);
if (!open_ret) {
Q_ASSERT(0);
return false;
......@@ -228,7 +325,7 @@ bool MessageDb::moveDb(const QString &newLocDir)
/* Erase target if exists. */
QFile::remove(newFileName);
/* Copy database file. */
/* Move database file. */
move_ret = QFile::rename(oldFileName, newFileName);
/* Open database. */
......
......@@ -61,6 +61,30 @@ public:
*/
bool openDb(const QString &fileName, bool storeToDisk);
/*!
* @brief Delete current message db and create new in new location.
*
* @param[in] newLocDir New location name.
* @return True on success.
*/
bool deleteCurrentDbCreateNew(const QString &newLocDir);
/*!
* @brief Create new database in the new location.
*
* @param[in] newLocDir New location name.
* @return True on success.
*/
bool createDb(const QString &newLocDir);
/*!
* @brief Delete database from location.
*
* @param[in] LocDir New location name.
* @return True on success.
*/
bool deleteDb(const QString &LocDir);
/*!
* @brief Copy db.
*
......
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