Skip to content

Commit

Permalink
Merge pull request #1179 from kiwix/feature/portable-kiwix
Browse files Browse the repository at this point in the history
Implement portable mode
  • Loading branch information
kelson42 authored Sep 10, 2024
2 parents f383bea + 78a50cf commit 5e5bcdc
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 32 deletions.
62 changes: 32 additions & 30 deletions src/kiwixapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,38 +142,40 @@ void KiwixApp::newTab()

QString KiwixApp::findLibraryDirectory()
{
// Check for library.xml in the same directory than the executable (portable kiwix-desktop)
auto currentDataDir = QString::fromStdString(kiwix::removeLastPathElement(kiwix::getExecutablePath()));
auto libraryFile = QFileInfo(currentDataDir, "library.xml");
if (libraryFile.exists())
return currentDataDir;

// Check for default dataDirectory.
currentDataDir = 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());
auto currentDataDir = QString::fromStdString(kiwix::removeLastPathElement(kiwix::getExecutablePath()));

if (isPortableMode())
return currentDataDir + QDir::separator() + "data";

// Check for library.xml in the same directory as the executable.
auto libraryFile = QFileInfo(currentDataDir, "library.xml");
if (libraryFile.exists())
return currentDataDir;

// Check for default dataDirectory.
currentDataDir = getDataDirectory();
libraryFile = QFileInfo(currentDataDir, "library.xml");
if (libraryFile.exists())
return currentDataDir;

// Check if this is a pre-release version with wrong directory.
auto oldDataDir = QDir(currentDataDir);
oldDataDir.cdUp();
libraryFile = QFileInfo(oldDataDir, "library.xml");
if (libraryFile.exists()) {
// We have to move all zim files 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 stores information about the current download using absolute path.
// Let's remove everything. User will lose its ongoing download but it should be pretty rare.
for (auto& fileInfo: oldDataDir.entryInfoList({"*.aria2", "*.meta4", "kiwix.session"}))
QFile::remove(fileInfo.absoluteFilePath());
}
}

return currentDataDir;
// This is a first launch
return currentDataDir;
}

void KiwixApp::restoreTabs()
Expand Down
31 changes: 29 additions & 2 deletions src/settingsmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@

QString getDataDirectory()
{
if (isPortableMode()) {
auto currentDataDir = QString::fromStdString(kiwix::removeLastPathElement(kiwix::getExecutablePath()));
return currentDataDir + QDir::separator() + "data";
}

QString dataDir = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);

if (!dataDir.isEmpty() && QDir().mkpath(dataDir))
Expand All @@ -17,9 +22,30 @@ QString getDataDirectory()
return QString::fromStdString(kiwix::getCurrentDirectory());
}

bool isPortableMode()
{
auto currentDataDir = QString::fromStdString(kiwix::removeLastPathElement(kiwix::getExecutablePath()));
auto portableFile = QFileInfo(currentDataDir, ".portable");

return portableFile.exists();
}

namespace {

QString getSettingsConfPath()
{
QString confDirectory = isPortableMode() ?
getDataDirectory() :
QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);

return confDirectory + QDir::separator() + "Kiwix-desktop.conf";
}

}

SettingsManager::SettingsManager(QObject *parent)
: QObject(parent),
m_settings("Kiwix", "Kiwix-desktop"),
m_settings(getSettingsConfPath(), QSettings::NativeFormat),
m_view(nullptr)
{
initSettings();
Expand Down Expand Up @@ -162,7 +188,8 @@ void SettingsManager::initSettings()
{
m_kiwixServerPort = m_settings.value("localKiwixServer/port", 8080).toInt();
m_zoomFactor = m_settings.value("view/zoomFactor", 1).toDouble();
m_downloadDir = m_settings.value("download/dir", getDataDirectory()).toString();
QString dataDir = getDataDirectory();
m_downloadDir = isPortableMode() ? dataDir : m_settings.value("download/dir", dataDir).toString();
m_kiwixServerIpAddress = m_settings.value("localKiwixServer/ipAddress", QString("0.0.0.0")).toString();
m_monitorDir = m_settings.value("monitor/dir", QString("")).toString();
m_moveToTrash = m_settings.value("moveToTrash", true).toBool();
Expand Down
2 changes: 2 additions & 0 deletions src/settingsmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,6 @@ public slots:
};

QString getDataDirectory();
bool isPortableMode();

#endif // SETTINGSMANAGER_H

0 comments on commit 5e5bcdc

Please sign in to comment.