Skip to content

Commit

Permalink
Use XDG Desktop Portal to autostart the flatpak
Browse files Browse the repository at this point in the history
  • Loading branch information
moriol42 authored and droidmonkey committed Apr 26, 2024
1 parent 5b123e7 commit d0e9f13
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/core/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ static const QHash<Config::ConfigKey, ConfigDirective> 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}},
Expand Down
3 changes: 3 additions & 0 deletions src/core/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ class Config : public QObject
GUI_HidePreviewPanel,
GUI_AlwaysOnTop,
GUI_ToolButtonStyle,
#ifdef KEEPASSXC_DIST_FLATPAK
GUI_LaunchAtStartup,
#endif
GUI_ShowTrayIcon,
GUI_TrayIconAppearance,
GUI_MinimizeToTray,
Expand Down
52 changes: 52 additions & 0 deletions src/gui/osutils/nixutils/NixUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
#include <QStandardPaths>
#include <QStyle>
#include <QTextStream>
#ifdef KEEPASSXC_DIST_FLATPAK
#include "core/Config.h"
#include <QRandomGenerator>
#endif
#ifdef WITH_XC_X11
#include <QX11Info>

Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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<QString, QVariant> 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<QDBusObjectPath>();

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<bool>();
qDebug() << "The autostart value is set to:" << isLauchedAtStartup;
#if defined(KEEPASSXC_DIST_FLATPAK)
config()->set(Config::GUI_LaunchAtStartup, isLauchedAtStartup);
#endif
}

bool NixUtils::isCapslockEnabled()
Expand Down
1 change: 1 addition & 0 deletions src/gui/osutils/nixutils/NixUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class NixUtils : public OSUtilsBase, QAbstractNativeEventFilter
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);
Expand Down

0 comments on commit d0e9f13

Please sign in to comment.