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

Improve Portability Check #10760

Merged
merged 1 commit into from
May 27, 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
5 changes: 3 additions & 2 deletions src/browser/NativeMessageInstaller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "NativeMessageInstaller.h"
#include "BrowserSettings.h"
#include "config-keepassx.h"
#include "core/Config.h"

#include <QCoreApplication>
#include <QDebug>
Expand Down Expand Up @@ -209,8 +210,8 @@ QString NativeMessageInstaller::getNativeMessagePath(SupportedBrowsers browser)
QString basePath;
#if defined(Q_OS_WIN)
// If portable settings file exists save the JSON scripts to the application folder
if (QFile::exists(QCoreApplication::applicationDirPath() + QStringLiteral("/keepassxc.ini"))) {
basePath = QCoreApplication::applicationDirPath();
if (Config::isPortable()) {
basePath = Config::portableConfigDir();
} else {
basePath = QStandardPaths::writableLocation(QStandardPaths::DataLocation);
}
Expand Down
37 changes: 23 additions & 14 deletions src/core/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -512,20 +512,8 @@ void Config::init(const QString& configFileName, const QString& localConfigFileN
QPair<QString, QString> Config::defaultConfigFiles()
{
// Check if we are running in portable mode, if so store the config files local to the app
#ifdef Q_OS_WIN
// Enable QFileInfo::isWritable check on Windows
extern Q_CORE_EXPORT int qt_ntfs_permission_lookup;
qt_ntfs_permission_lookup++;
#endif
auto portablePath = QCoreApplication::applicationDirPath().append("/%1");
auto portableFile = portablePath.arg(".portable");
bool isPortable = QFile::exists(portableFile) && QFileInfo(portableFile).isWritable();
#ifdef Q_OS_WIN
qt_ntfs_permission_lookup--;
#endif

if (isPortable) {
return {portablePath.arg("config/keepassxc.ini"), portablePath.arg("config/keepassxc_local.ini")};
if (isPortable()) {
return {portableConfigDir().append("/keepassxc.ini"), portableConfigDir().append("/keepassxc_local.ini")};
}

QString configPath;
Expand Down Expand Up @@ -602,6 +590,27 @@ void Config::createTempFileInstance()
tmpFile->setParent(m_instance);
}

bool Config::isPortable()
{
#ifdef Q_OS_WIN
// Enable QFileInfo::isWritable check on Windows
extern Q_CORE_EXPORT int qt_ntfs_permission_lookup;
qt_ntfs_permission_lookup++;
#endif
auto portablePath = QCoreApplication::applicationDirPath().append("/%1");
auto portableFile = portablePath.arg(".portable");
auto isPortable = QFile::exists(portableFile) && QFileInfo(portableFile).isWritable();
#ifdef Q_OS_WIN
qt_ntfs_permission_lookup--;
#endif
return isPortable;
}

QString Config::portableConfigDir()
{
return QCoreApplication::applicationDirPath().append("/config");
}

QList<Config::ShortcutEntry> Config::getShortcuts() const
{
m_settings->beginGroup("Shortcuts");
Expand Down
2 changes: 2 additions & 0 deletions src/core/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ class Config : public QObject
static Config* instance();
static void createConfigFromFile(const QString& configFileName, const QString& localConfigFileName = {});
static void createTempFileInstance();
static bool isPortable();
static QString portableConfigDir();

signals:
void changed(ConfigKey key);
Expand Down
Loading