diff --git a/src/core/Config.cpp b/src/core/Config.cpp index cc9971bec..fa5e1443e 100644 --- a/src/core/Config.cpp +++ b/src/core/Config.cpp @@ -97,6 +97,9 @@ static const QHash configStrings = { {Config::GUI_HidePreviewPanel, {QS("GUI/HidePreviewPanel"), Roaming, false}}, {Config::GUI_AlwaysOnTop, {QS("GUI/GUI_AlwaysOnTop"), Local, false}}, {Config::GUI_ToolButtonStyle, {QS("GUI/ToolButtonStyle"), Roaming, Qt::ToolButtonIconOnly}}, +#ifdef KEEPASSXC_DIST_FLATPAK + {Config::GUI_LaunchAtStartup, {QS("GUI/LaunchAtStartup"), Roaming, false}}, +#endif {Config::GUI_ShowTrayIcon, {QS("GUI/ShowTrayIcon"), Roaming, false}}, {Config::GUI_TrayIconAppearance, {QS("GUI/TrayIconAppearance"), Roaming, {}}}, {Config::GUI_MinimizeToTray, {QS("GUI/MinimizeToTray"), Roaming, false}}, diff --git a/src/core/Config.h b/src/core/Config.h index b4ebc5036..b7a202574 100644 --- a/src/core/Config.h +++ b/src/core/Config.h @@ -79,6 +79,9 @@ public: GUI_HidePreviewPanel, GUI_AlwaysOnTop, GUI_ToolButtonStyle, +#ifdef KEEPASSXC_DIST_FLATPAK + GUI_LaunchAtStartup, +#endif GUI_ShowTrayIcon, GUI_TrayIconAppearance, GUI_MinimizeToTray, diff --git a/src/gui/osutils/nixutils/NixUtils.cpp b/src/gui/osutils/nixutils/NixUtils.cpp index a2a034205..b2b70ea72 100644 --- a/src/gui/osutils/nixutils/NixUtils.cpp +++ b/src/gui/osutils/nixutils/NixUtils.cpp @@ -27,6 +27,10 @@ #include #include #include +#ifdef KEEPASSXC_DIST_FLATPAK +#include "core/Config.h" +#include +#endif #ifdef WITH_XC_X11 #include @@ -123,12 +127,17 @@ QString NixUtils::getAutostartDesktopFilename(bool createDirs) const bool NixUtils::isLaunchAtStartupEnabled() const { +#if !defined(KEEPASSXC_DIST_FLATPAK) return QFile::exists(getAutostartDesktopFilename()); ; +#else + return config()->get(Config::GUI_LaunchAtStartup).toBool(); +#endif } void NixUtils::setLaunchAtStartup(bool enable) { +#if !defined(KEEPASSXC_DIST_FLATPAK) if (enable) { QFile desktopFile(getAutostartDesktopFilename(true)); if (!desktopFile.open(QIODevice::WriteOnly)) { @@ -163,6 +172,49 @@ void NixUtils::setLaunchAtStartup(bool enable) } else if (isLaunchAtStartupEnabled()) { QFile::remove(getAutostartDesktopFilename()); } +#else + QDBusConnection sessionBus = QDBusConnection::sessionBus(); + QDBusMessage msg = QDBusMessage::createMethodCall("org.freedesktop.portal.Desktop", + "/org/freedesktop/portal/desktop", + "org.freedesktop.portal.Background", + "RequestBackground"); + + QMap options; + options["autostart"] = QVariant(enable); + options["reason"] = QVariant("Launch KeePassXC at startup"); + int token = QRandomGenerator::global()->bounded(1000, 9999); + options["handle_token"] = QVariant(QString("org/keepassxc/KeePassXC/%1").arg(token)); + + msg << "" << options; + + QDBusMessage response = sessionBus.call(msg); + + QDBusObjectPath handle = response.arguments().at(0).value(); + + bool res = sessionBus.connect("org.freedesktop.portal.Desktop", + handle.path(), + "org.freedesktop.portal.Request", + "Response", + this, + SLOT(launchAtStartupRequested(uint, QVariantMap))); + + if (!res) { + qDebug() << "Could not connect to org.freedesktop.portal.Request::Response signal"; + } +#endif +} + +void NixUtils::launchAtStartupRequested(uint response, const QVariantMap& results) +{ + if (response > 0) { + qDebug() << "The interaction was cancelled"; + return; + } + bool isLauchedAtStartup = results["autostart"].value(); + qDebug() << "The autostart value is set to:" << isLauchedAtStartup; +#if defined(KEEPASSXC_DIST_FLATPAK) + config()->set(Config::GUI_LaunchAtStartup, isLauchedAtStartup); +#endif } bool NixUtils::isCapslockEnabled() diff --git a/src/gui/osutils/nixutils/NixUtils.h b/src/gui/osutils/nixutils/NixUtils.h index 571af1a3a..c3caca6b7 100644 --- a/src/gui/osutils/nixutils/NixUtils.h +++ b/src/gui/osutils/nixutils/NixUtils.h @@ -54,6 +54,7 @@ public: private slots: void handleColorSchemeRead(QDBusVariant value); void handleColorSchemeChanged(QString ns, QString key, QDBusVariant value); + void launchAtStartupRequested(uint response, const QVariantMap& results); private: explicit NixUtils(QObject* parent = nullptr);