Migrate the data to new window's dataDirectory if needed.

This commit is contained in:
Matthieu Gautier 2020-04-14 19:13:04 +02:00
parent a9a424a2e2
commit 1951084ad8

View File

@ -124,13 +124,38 @@ KiwixApp::~KiwixApp()
QString KiwixApp::findLibraryDirectory()
{
QString currentPathLibrary = QString::fromStdString(appendToDirectory(removeLastPathElement(getExecutablePath()),"library.xml"));
QFileInfo libraryFile(currentPathLibrary);
// Check for library.xml in the same directory than the executable (portable kiwix-desktop)
auto currentDataDir = QString::fromStdString(removeLastPathElement(getExecutablePath()));
auto libraryFile = QFileInfo(currentDataDir, "library.xml");
if (libraryFile.exists())
return currentDataDir;
if (libraryFile.exists())
return QString::fromStdString(removeLastPathElement(getExecutablePath()));
else
return QString::fromStdString(getDataDirectory());
// Check for default dataDirectory.
currentDataDir = QString::fromStdString(getDataDirectory());
libraryFile = QFileInfo(currentDataDir, "library.xml");
if (libraryFile.exists())
return currentDataDir;
// There is no library.xml in default dataDirectory.
// Either, it is a first launch, or user used a pre-release version with wrong directory.
// Let's try to move data from old directory to new one if needed.
auto oldDataDir = QDir(currentDataDir);
oldDataDir.cdUp();
libraryFile = QFileInfo(oldDataDir, "library.xml");
if (libraryFile.exists()) {
// We have to move all zims file and xml file to the new dataDir
for (auto& fileInfo: oldDataDir.entryInfoList({"*.zim", "library*.xml"})) {
auto newFileInfo = QFileInfo(currentDataDir, fileInfo.fileName());
QFile::rename(fileInfo.absoluteFilePath(), newFileInfo.absoluteFilePath());
}
// Aria2 store informations about the current download using absolute path.
// Let's remove everything. User will loose its ongoing download but it should be pretty rare.
for (auto& fileInfo: oldDataDir.entryInfoList({"*.aria2", "*.meta4", "kiwix.session"})) {
QFile::remove(fileInfo.absoluteFilePath());
}
}
return currentDataDir;
}
KiwixApp *KiwixApp::instance()