Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement portable mode #1179

Merged
merged 2 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
sgourdas marked this conversation as resolved.
Show resolved Hide resolved
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();
kelson42 marked this conversation as resolved.
Show resolved Hide resolved
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
Loading