2015-03-14 04:19:39 +00:00
|
|
|
/**
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-FileCopyrightText: 2015 Albert Vaca <albertvaka@gmail.com>
|
2015-03-14 04:19:39 +00:00
|
|
|
*
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
2015-03-14 04:19:39 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "kdeconnectpluginconfig.h"
|
|
|
|
|
2019-10-27 17:08:51 +00:00
|
|
|
#include <QDBusConnection>
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <QDBusMessage>
|
2019-10-27 17:08:51 +00:00
|
|
|
#include <QDebug>
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <QDir>
|
|
|
|
#include <QSettings>
|
2015-03-14 04:19:39 +00:00
|
|
|
|
2019-06-09 16:28:49 +01:00
|
|
|
#include "dbushelper.h"
|
2022-09-10 22:23:52 +01:00
|
|
|
#include "kdeconnectconfig.h"
|
2015-03-14 04:19:39 +00:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
struct KdeConnectPluginConfigPrivate {
|
2017-09-03 20:39:44 +01:00
|
|
|
QDir m_configDir;
|
2022-09-10 22:23:52 +01:00
|
|
|
QSettings *m_config;
|
2017-09-03 20:39:44 +01:00
|
|
|
QDBusMessage m_signal;
|
2015-03-14 04:19:39 +00:00
|
|
|
};
|
|
|
|
|
2019-10-27 17:08:51 +00:00
|
|
|
KdeConnectPluginConfig::KdeConnectPluginConfig()
|
|
|
|
: d(new KdeConnectPluginConfigPrivate())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
KdeConnectPluginConfig::KdeConnectPluginConfig(const QString &deviceId, const QString &pluginName)
|
2015-03-14 04:19:39 +00:00
|
|
|
: d(new KdeConnectPluginConfigPrivate())
|
|
|
|
{
|
2019-09-08 16:09:52 +01:00
|
|
|
d->m_configDir = KdeConnectConfig::instance().pluginConfigDir(deviceId, pluginName);
|
2017-09-03 20:39:44 +01:00
|
|
|
QDir().mkpath(d->m_configDir.path());
|
2015-03-14 04:19:39 +00:00
|
|
|
|
2017-09-03 20:39:44 +01:00
|
|
|
d->m_config = new QSettings(d->m_configDir.absoluteFilePath(QStringLiteral("config")), QSettings::IniFormat);
|
2015-09-12 12:46:37 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
d->m_signal = QDBusMessage::createSignal(QStringLiteral("/kdeconnect/") + deviceId + QStringLiteral("/") + pluginName,
|
|
|
|
QStringLiteral("org.kde.kdeconnect.config"),
|
|
|
|
QStringLiteral("configChanged"));
|
|
|
|
QDBusConnection::sessionBus().connect(QLatin1String(""),
|
|
|
|
QStringLiteral("/kdeconnect/") + deviceId + QStringLiteral("/") + pluginName,
|
|
|
|
QStringLiteral("org.kde.kdeconnect.config"),
|
|
|
|
QStringLiteral("configChanged"),
|
|
|
|
this,
|
|
|
|
SLOT(slotConfigChanged()));
|
2015-03-14 04:19:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
KdeConnectPluginConfig::~KdeConnectPluginConfig()
|
|
|
|
{
|
2017-09-03 20:39:44 +01:00
|
|
|
delete d->m_config;
|
2015-03-14 04:19:39 +00:00
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
QString KdeConnectPluginConfig::getString(const QString &key, const QString &defaultValue)
|
2019-10-27 17:08:51 +00:00
|
|
|
{
|
|
|
|
if (!d->m_config) {
|
|
|
|
loadConfig();
|
|
|
|
}
|
|
|
|
|
|
|
|
d->m_config->sync();
|
|
|
|
return d->m_config->value(key, defaultValue).toString();
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
bool KdeConnectPluginConfig::getBool(const QString &key, const bool defaultValue)
|
2019-10-27 17:08:51 +00:00
|
|
|
{
|
|
|
|
if (!d->m_config) {
|
|
|
|
loadConfig();
|
|
|
|
}
|
|
|
|
|
|
|
|
d->m_config->sync();
|
|
|
|
return d->m_config->value(key, defaultValue).toBool();
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
int KdeConnectPluginConfig::getInt(const QString &key, const int defaultValue)
|
2015-03-14 04:19:39 +00:00
|
|
|
{
|
2019-10-27 17:08:51 +00:00
|
|
|
if (!d->m_config) {
|
|
|
|
loadConfig();
|
|
|
|
}
|
|
|
|
|
2017-09-03 20:39:44 +01:00
|
|
|
d->m_config->sync();
|
2019-10-27 17:08:51 +00:00
|
|
|
return d->m_config->value(key, defaultValue).toInt();
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
QByteArray KdeConnectPluginConfig::getByteArray(const QString &key, const QByteArray defaultValue)
|
2019-10-27 17:08:51 +00:00
|
|
|
{
|
|
|
|
if (!d->m_config) {
|
|
|
|
loadConfig();
|
|
|
|
}
|
|
|
|
|
|
|
|
d->m_config->sync();
|
|
|
|
return d->m_config->value(key, defaultValue).toByteArray();
|
2015-03-14 04:19:39 +00:00
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
QVariantList KdeConnectPluginConfig::getList(const QString &key, const QVariantList &defaultValue)
|
2015-12-05 22:11:57 +00:00
|
|
|
{
|
|
|
|
QVariantList list;
|
2022-09-10 22:23:52 +01:00
|
|
|
d->m_config->sync(); // note: need sync() to get recent changes signalled from other process
|
2017-09-03 20:39:44 +01:00
|
|
|
int size = d->m_config->beginReadArray(key);
|
2015-12-05 22:11:57 +00:00
|
|
|
if (size < 1) {
|
2017-09-03 20:39:44 +01:00
|
|
|
d->m_config->endArray();
|
2015-12-05 22:11:57 +00:00
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < size; ++i) {
|
2017-09-03 20:39:44 +01:00
|
|
|
d->m_config->setArrayIndex(i);
|
|
|
|
list << d->m_config->value(QStringLiteral("value"));
|
2015-12-05 22:11:57 +00:00
|
|
|
}
|
2017-09-03 20:39:44 +01:00
|
|
|
d->m_config->endArray();
|
2015-12-05 22:11:57 +00:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void KdeConnectPluginConfig::set(const QString &key, const QVariant &value)
|
2015-03-14 04:19:39 +00:00
|
|
|
{
|
2017-09-03 20:39:44 +01:00
|
|
|
d->m_config->setValue(key, value);
|
|
|
|
d->m_config->sync();
|
2022-04-12 06:40:03 +01:00
|
|
|
QDBusConnection::sessionBus().send(d->m_signal);
|
2015-09-12 12:46:37 +01:00
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void KdeConnectPluginConfig::setList(const QString &key, const QVariantList &list)
|
2015-12-05 22:11:57 +00:00
|
|
|
{
|
2017-09-03 20:39:44 +01:00
|
|
|
d->m_config->beginWriteArray(key);
|
2015-12-05 22:11:57 +00:00
|
|
|
for (int i = 0; i < list.size(); ++i) {
|
2017-09-03 20:39:44 +01:00
|
|
|
d->m_config->setArrayIndex(i);
|
|
|
|
d->m_config->setValue(QStringLiteral("value"), list.at(i));
|
2015-12-05 22:11:57 +00:00
|
|
|
}
|
2017-09-03 20:39:44 +01:00
|
|
|
d->m_config->endArray();
|
|
|
|
d->m_config->sync();
|
2022-04-12 06:40:03 +01:00
|
|
|
QDBusConnection::sessionBus().send(d->m_signal);
|
2015-12-05 22:11:57 +00:00
|
|
|
}
|
|
|
|
|
2015-09-12 12:46:37 +01:00
|
|
|
void KdeConnectPluginConfig::slotConfigChanged()
|
|
|
|
{
|
|
|
|
Q_EMIT configChanged();
|
2015-03-14 04:19:39 +00:00
|
|
|
}
|
2019-10-27 17:08:51 +00:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void KdeConnectPluginConfig::setDeviceId(const QString &deviceId)
|
2019-10-27 17:08:51 +00:00
|
|
|
{
|
|
|
|
if (deviceId != m_deviceId) {
|
|
|
|
m_deviceId = deviceId;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!m_deviceId.isEmpty() && !m_pluginName.isEmpty()) {
|
|
|
|
loadConfig();
|
|
|
|
}
|
2023-07-20 18:24:41 +01:00
|
|
|
|
|
|
|
Q_EMIT deviceIdChanged(deviceId);
|
2019-10-27 17:08:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString KdeConnectPluginConfig::deviceId()
|
|
|
|
{
|
|
|
|
return m_deviceId;
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void KdeConnectPluginConfig::setPluginName(const QString &pluginName)
|
2019-10-27 17:08:51 +00:00
|
|
|
{
|
|
|
|
if (pluginName != m_pluginName) {
|
|
|
|
m_pluginName = pluginName;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!m_deviceId.isEmpty() && !m_pluginName.isEmpty()) {
|
|
|
|
loadConfig();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString KdeConnectPluginConfig::pluginName()
|
|
|
|
{
|
|
|
|
return m_pluginName;
|
|
|
|
}
|
|
|
|
|
|
|
|
void KdeConnectPluginConfig::loadConfig()
|
|
|
|
{
|
|
|
|
d->m_configDir = KdeConnectConfig::instance().pluginConfigDir(m_deviceId, m_pluginName);
|
|
|
|
QDir().mkpath(d->m_configDir.path());
|
|
|
|
|
|
|
|
d->m_config = new QSettings(d->m_configDir.absoluteFilePath(QStringLiteral("config")), QSettings::IniFormat);
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
d->m_signal = QDBusMessage::createSignal(QStringLiteral("/kdeconnect/") + m_deviceId + QStringLiteral("/") + m_pluginName,
|
|
|
|
QStringLiteral("org.kde.kdeconnect.config"),
|
|
|
|
QStringLiteral("configChanged"));
|
|
|
|
QDBusConnection::sessionBus().connect(QLatin1String(""),
|
|
|
|
QStringLiteral("/kdeconnect/") + m_deviceId + QStringLiteral("/") + m_pluginName,
|
|
|
|
QStringLiteral("org.kde.kdeconnect.config"),
|
|
|
|
QStringLiteral("configChanged"),
|
|
|
|
this,
|
|
|
|
SLOT(slotConfigChanged()));
|
2019-10-27 17:08:51 +00:00
|
|
|
Q_EMIT configChanged();
|
|
|
|
}
|