Commit 1d909d13 authored by Jan Grulich's avatar Jan Grulich

Add settings portal

Missing to implement setting changes
parent 16bc9430
......@@ -63,6 +63,7 @@ find_package(Qt5 ${QT_MIN_VERSION} CONFIG REQUIRED COMPONENTS
find_package(KF5 ${KF5_MIN_VERSION} REQUIRED
CoreAddons
Config
I18n
Notifications
Wayland
......
[portal]
DBusName=org.freedesktop.impl.portal.desktop.kde
Interfaces=org.freedesktop.impl.portal.Access;org.freedesktop.impl.portal.AppChooser;org.freedesktop.impl.portal.Email;org.freedesktop.impl.portal.FileChooser;org.freedesktop.impl.portal.Inhibit;org.freedesktop.impl.portal.Notification;org.freedesktop.impl.portal.Print;org.freedesktop.impl.portal.ScreenCast;org.freedesktop.impl.portal.Screenshot;org.freedesktop.impl.portal.RemoteDesktop
Interfaces=org.freedesktop.impl.portal.Access;org.freedesktop.impl.portal.AppChooser;org.freedesktop.impl.portal.Email;org.freedesktop.impl.portal.FileChooser;org.freedesktop.impl.portal.Inhibit;org.freedesktop.impl.portal.Notification;org.freedesktop.impl.portal.Print;org.freedesktop.impl.portal.ScreenCast;org.freedesktop.impl.portal.Screenshot;org.freedesktop.impl.portal.RemoteDesktop;org.freedesktop.impl.portal.Settings
UseIn=KDE
......@@ -19,6 +19,7 @@ set(xdg_desktop_portal_kde_SRCS
session.cpp
screenshot.cpp
screenshotdialog.cpp
settings.cpp
)
if (SCREENCAST_ENABLED)
......@@ -51,6 +52,7 @@ target_link_libraries(xdg-desktop-portal-kde
Qt5::PrintSupport
Qt5::Widgets
KF5::CoreAddons
KF5::ConfigCore
KF5::I18n
KF5::Notifications
KF5::WaylandClient
......
......@@ -42,6 +42,7 @@ DesktopPortal::DesktopPortal(QObject *parent)
, m_remoteDesktop(new RemoteDesktopPortal(this))
#endif
, m_screenshot(new ScreenshotPortal(this))
, m_settings(new SettingsPortal(this))
{
#if SCREENCAST_ENABLED
WaylandIntegration::init();
......
......@@ -23,6 +23,7 @@
#include <QObject>
#include <QDBusVirtualObject>
#include <QDBusContext>
#include "access.h"
#include "appchooser.h"
......@@ -37,8 +38,9 @@
#include "waylandintegration.h"
#endif
#include "screenshot.h"
#include "settings.h"
class DesktopPortal : public QObject
class DesktopPortal : public QObject, public QDBusContext
{
Q_OBJECT
public:
......@@ -58,6 +60,7 @@ private:
RemoteDesktopPortal *m_remoteDesktop;
#endif
ScreenshotPortal *m_screenshot;
SettingsPortal *m_settings;
};
#endif // XDG_DESKTOP_PORTAL_KDE_DESKTOP_PORTAL_H
......
/*
* Copyright © 2018 Red Hat, Inc
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Jan Grulich <jgrulich@redhat.com>
*/
#include "settings.h"
#include <QDBusMetaType>
#include <QDBusContext>
#include <QDBusMessage>
#include <QDBusConnection>
#include <QLoggingCategory>
#include <KConfigCore/KConfigGroup>
Q_LOGGING_CATEGORY(XdgDesktopPortalKdeSettings, "xdp-kde-settings")
Q_DECLARE_METATYPE(SettingsPortal::VariantMapMap)
QDBusArgument &operator<<(QDBusArgument &argument, const SettingsPortal::VariantMapMap &mymap)
{
argument.beginMap(QVariant::String, QVariant::Map);
QMapIterator<QString, QVariantMap> i(mymap);
while (i.hasNext()) {
i.next();
argument.beginMapEntry();
argument << i.key() << i.value();
argument.endMapEntry();
}
argument.endMap();
return argument;
}
const QDBusArgument &operator>>(const QDBusArgument &argument, SettingsPortal::VariantMapMap &mymap)
{
argument.beginMap();
mymap.clear();
while (!argument.atEnd()) {
QString key;
QVariantMap value;
argument.beginMapEntry();
argument >> key >> value;
argument.endMapEntry();
mymap.insert(key, value);
}
argument.endMap();
return argument;
}
static bool groupMatches(const QString &group, const QStringList &patterns)
{
for (const QString &pattern : patterns) {
if (pattern.isEmpty()) {
return true;
}
if (pattern == group) {
return true;
}
if (pattern.endsWith(QLatin1Char('*')) && group.startsWith(pattern.left(pattern.length() - 1))) {
return true;
}
}
return false;
}
SettingsPortal::SettingsPortal(QObject *parent)
: QDBusAbstractAdaptor(parent)
{
qDBusRegisterMetaType<VariantMapMap>();
m_kdeglobals = KSharedConfig::openConfig();
}
SettingsPortal::~SettingsPortal()
{
}
void SettingsPortal::ReadAll(const QStringList &groups)
{
qCDebug(XdgDesktopPortalKdeSettings) << "ReadAll called with parameters:";
qCDebug(XdgDesktopPortalKdeSettings) << " groups: " << groups;
//FIXME this is super ugly, but I was unable to make it properly return VariantMapMap
QObject *obj = QObject::parent();
if (!obj) {
qCWarning(XdgDesktopPortalKdeSettings) << "Failed to get dbus context";
return;
}
void *ptr = obj->qt_metacast("QDBusContext");
QDBusContext *q_ptr = reinterpret_cast<QDBusContext *>(ptr);
if (!q_ptr) {
qCWarning(XdgDesktopPortalKdeSettings) << "Failed to get dbus context";
return;
}
VariantMapMap result;
for (const QString &settingGroupName : m_kdeglobals->groupList()) {
//NOTE: use org.kde.kdeglobals prefix
QString uniqueGroupName = QStringLiteral("org.kde.kdeglobals.") + settingGroupName;
if (!groupMatches(uniqueGroupName, groups)) {
continue;
}
QVariantMap map;
KConfigGroup configGroup(m_kdeglobals, settingGroupName);
for (const QString &key : configGroup.keyList()) {
map.insert(key, configGroup.readEntry(key));
}
result.insert(uniqueGroupName, map);
}
QDBusMessage message = q_ptr->message();
QDBusMessage reply = message.createReply(QVariant::fromValue(result));
QDBusConnection::sessionBus().send(reply);
}
QDBusVariant SettingsPortal::Read(const QString &group, const QString &key)
{
qCDebug(XdgDesktopPortalKdeSettings) << "Read called with parameters:";
qCDebug(XdgDesktopPortalKdeSettings) << " group: " << group;
qCDebug(XdgDesktopPortalKdeSettings) << " key: " << key;
// All our namespaces start with this prefix
if (!group.startsWith(QStringLiteral("org.kde.kdeglobals"))) {
qCWarning(XdgDesktopPortalKdeSettings) << "Namespace " << group << " is not supported";
return QDBusVariant();
}
QString groupName = group.right(group.length() - QStringLiteral("org.kde.kdeglobals.").length());
if (!m_kdeglobals->hasGroup(groupName)) {
qCWarning(XdgDesktopPortalKdeSettings) << "Group " << group << " doesn't exist";
return QDBusVariant();
}
KConfigGroup configGroup(m_kdeglobals, groupName);
if (!configGroup.hasKey(key)) {
qCWarning(XdgDesktopPortalKdeSettings) << "Key " << key << " doesn't exist";
return QDBusVariant();
}
return QDBusVariant(configGroup.readEntry(key));
}
/*
* Copyright © 2018 Red Hat, Inc
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Jan Grulich <jgrulich@redhat.com>
*/
#ifndef XDG_DESKTOP_PORTAL_KDE_SETTINGS_H
#define XDG_DESKTOP_PORTAL_KDE_SETTINGS_H
#include <QDBusAbstractAdaptor>
#include <QDBusObjectPath>
#include <KConfigCore/KConfig>
#include <KConfigCore/KSharedConfig>
class SettingsPortal : public QDBusAbstractAdaptor
{
Q_OBJECT
Q_CLASSINFO("D-Bus Interface", "org.freedesktop.impl.portal.Settings")
public:
explicit SettingsPortal(QObject *parent);
~SettingsPortal();
typedef QMap<QString, QMap<QString, QVariant> > VariantMapMap;
uint version() const { return 1; }
public Q_SLOTS:
void ReadAll(const QStringList &groups);
QDBusVariant Read(const QString &group, const QString &keys);
Q_SIGNALS:
// TODO not implemented yet
void SettingChanged(const QString &group, const QString &key, const QVariant &value);
private:
KSharedConfigPtr m_kdeglobals;
};
#endif // XDG_DESKTOP_PORTAL_KDE_SETTINGS_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment