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

Modified SQLiteDb::copyDb() to accept file name instead of directory name.

parent 88b62d99
......@@ -238,9 +238,16 @@ fail:
return false;
}
bool SQLiteDb::copyDb(const QString &newLocDir,
bool SQLiteDb::copyDb(const QString &newFileName,
const QList<class SQLiteTbl *> &tables)
{
if (fileName() == memoryLocation) {
logErrorNL("%s",
"Trying to copy database that resides i memory.");
Q_ASSERT(0);
return false;
}
bool copy_ret, open_ret;
/* Close database. */
......@@ -248,14 +255,16 @@ bool SQLiteDb::copyDb(const QString &newLocDir,
/* Backup old file name. */
QString oldFileName = fileName();
QFileInfo fi(oldFileName);
QString fileName = fi.fileName();
QString newFileName =
newLocDir + QDir::separator() + QDir::toNativeSeparators(fileName);
logInfoNL("Copying database file '%s' to location '%s'.",
oldFileName.toUtf8().constData(),
newFileName.toUtf8().constData());
/* Fail if target equals the source. */
/* TODO -- Perform a more reliable check than string comparison. */
if (oldFileName == newFileName) {
logWarningNL(
"Copying of database file '%s' aborted. Target and source are equal.",
oldFileName.toUtf8().constData());
return false;
}
......@@ -266,9 +275,14 @@ bool SQLiteDb::copyDb(const QString &newLocDir,
copy_ret = QFile::copy(oldFileName, newFileName);
/* Open database. */
open_ret = openDb(newFileName, true, tables);
open_ret = openDb(copy_ret ? newFileName : oldFileName, false, tables);
if (!open_ret) {
Q_ASSERT(0);
logErrorNL("File '%s' could not be opened.",
copy_ret ?
newFileName.toUtf8().constData() :
oldFileName.toUtf8().constData());
/* TODO -- qFatal() ? */
return false;
}
......
......@@ -75,7 +75,7 @@ public:
/*!
* @brief End named transaction.
*
* @param[in] savePointName Name of the save point.
* @param[in] savePointName Name of the save point.
* @return True on success.
*/
bool releaseSavePoint(const QString &savePointName);
......@@ -83,7 +83,7 @@ public:
/*!
* @brief Roll back transaction.
*
* @param[in] savePointName Name of the save point.
* @param[in] savePointName Name of the save point.
* @return True on success.
*
* @note If no save-point name is supplied then a complete roll-back is
......@@ -127,14 +127,14 @@ protected:
/*!
* @brief Copy db.
*
* @param[in] newLocDir New location name.
* @param[in] newFileName New file path.
* @param[in] tables List of table prototypes that should be created
* if missing.
* @return True on success.
*
* @note The copy is continued to be used. Original is closed.
*/
bool copyDb(const QString &newLocDir,
bool copyDb(const QString &newFileName,
const QList<class SQLiteTbl *> &tables);
/*!
......@@ -181,11 +181,11 @@ protected:
/*!
* @brief Open database file.
*
* @param[in] fileName File name.
* @param[in] fileName File name.
* @param[in] forceInMemory True if the message should be stored in
* memory only.
* @param[in] tables List of table prototypes that should be
* created if missing.
* @param[in] tables List of table prototypes that should be created
* if missing.
* @return True on success, false on any error.
*/
bool openDb(const QString &fileName, bool forceInMemory,
......
......@@ -449,7 +449,7 @@ int Messages::estimateAllDbSize(void)
return 2 * AccountListModel::globAccounts.size() * 1000000;
}
bool Messages::relocateDatabases(const QString &newLocation,
bool Messages::relocateDatabases(const QString &newLocationDir,
enum ReloactionAction action)
{
if ((action != RA_RELOCATE) && (action != RA_CREATE_NEW)) {
......@@ -490,16 +490,18 @@ bool Messages::relocateDatabases(const QString &newLocation,
break; /* Break the for cycle. */
}
QString oldLocation;
QString oldPath, oldFileName;
/* Message database file. */
oldLocation = mDb->fileName();
oldPath = mDb->fileName();
oldFileName = QFileInfo(oldPath).fileName();
switch (action) {
case RA_RELOCATE:
relocationSucceeded = mDb->copyDb(newLocation);
relocationSucceeded = mDb->copyDb(
newLocationDir + QDir::separator() + oldFileName);
break;
case RA_CREATE_NEW:
relocationSucceeded = mDb->createDb(newLocation);
relocationSucceeded = mDb->createDb(newLocationDir);
break;
default:
Q_ASSERT(0);
......@@ -512,19 +514,21 @@ bool Messages::relocateDatabases(const QString &newLocation,
* exists.
*/
relocatedMsgDbs.append(
MsgDbListEntry(mDb, oldLocation));
MsgDbListEntry(mDb, oldPath));
} else {
break; /* Break the for cycle. */
}
/* File database file. */
oldLocation = fDb->fileName();
oldPath = fDb->fileName();
oldFileName = QFileInfo(oldPath).fileName();
switch (action) {
case RA_RELOCATE:
relocationSucceeded = fDb->copyDb(newLocation);
relocationSucceeded = fDb->copyDb(
newLocationDir + QDir::separator() + oldFileName);
break;
case RA_CREATE_NEW:
relocationSucceeded = fDb->createDb(newLocation);
relocationSucceeded = fDb->createDb(newLocationDir);
break;
default:
Q_ASSERT(0);
......@@ -537,7 +541,7 @@ bool Messages::relocateDatabases(const QString &newLocation,
* exists.
*/
relocatedFileDbs.append(
FileDbListEntry(fDb, oldLocation));
FileDbListEntry(fDb, oldPath));
} else {
break; /* Break the for cycle. */
}
......
......@@ -217,12 +217,12 @@ private:
/*!
* @brief Relocates account database files.
*
* @param[in] newLocation New location (folder).
* @param[in] newLocationDir New location directory.
* @param[in] action Desired action to preform.
* @return True on success, false of failure.
*/
static
bool relocateDatabases(const QString &newLocation,
bool relocateDatabases(const QString &newLocationDir,
enum ReloactionAction action);
};
......
......@@ -85,9 +85,9 @@ QStringList FileDb::cleanFilesInDb(int days)
return msgIDList;
}
bool FileDb::copyDb(const QString &newLocDir)
bool FileDb::copyDb(const QString &newFileName)
{
return SQLiteDb::copyDb(newLocDir, listOfTables());
return SQLiteDb::copyDb(newFileName, listOfTables());
}
int FileDb::countFilesInDb(void)
......
......@@ -61,12 +61,12 @@ public:
/*!
* @brief Copy db.
*
* @param[in] newLocDir New location name.
* @param[in] newFileName New file path.
* @return True on success.
*
* @note The copy is continued to be used. Original is closed.
*/
bool copyDb(const QString &newLocDir);
bool copyDb(const QString &newFileName);
/*!
* @brief Get count of records in files db.
......
......@@ -56,9 +56,9 @@ MessageDb::MessageDb(const QString &connectionName)
{
}
bool MessageDb::copyDb(const QString &newLocDir)
bool MessageDb::copyDb(const QString &newFileName)
{
return SQLiteDb::copyDb(newLocDir, listOfTables());
return SQLiteDb::copyDb(newFileName, listOfTables());
}
bool MessageDb::createDb(const QString &newLocDir)
......
......@@ -56,12 +56,12 @@ public:
/*!
* @brief Copy db.
*
* @param[in] newLocDir New location name.
* @param[in] newFileName New file path.
* @return True on success.
*
* @note The copy is continued to be used. Original is closed.
*/
bool copyDb(const QString &newLocDir);
bool copyDb(const QString &newFileName);
/*!
* @brief Create new database in the new location.
......
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