From 1951084ad82afcd72e013042c31e3bf0d632ca89 Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Tue, 14 Apr 2020 19:13:04 +0200 Subject: [PATCH] Migrate the data to new window's dataDirectory if needed. --- src/kiwixapp.cpp | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/src/kiwixapp.cpp b/src/kiwixapp.cpp index c354425..fe515fa 100644 --- a/src/kiwixapp.cpp +++ b/src/kiwixapp.cpp @@ -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()