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"
|
|
|
|
|
|
|
|
#include <QDir>
|
2015-03-24 03:44:09 +00:00
|
|
|
#include <QSettings>
|
2015-09-12 12:46:37 +01:00
|
|
|
#include <QDBusMessage>
|
2019-10-27 17:08:51 +00:00
|
|
|
#include <QDBusConnection>
|
|
|
|
#include <QDebug>
|
2015-03-14 04:19:39 +00:00
|
|
|
|
|
|
|
#include "kdeconnectconfig.h"
|
2019-06-09 16:28:49 +01:00
|
|
|
#include "dbushelper.h"
|
2015-03-14 04:19:39 +00:00
|
|
|
|
|
|
|
struct KdeConnectPluginConfigPrivate
|
|
|
|
{
|
2017-09-03 20:39:44 +01:00
|
|
|
QDir m_configDir;
|
|
|
|
QSettings* m_config;
|
|
|
|
QDBusMessage m_signal;
|
2015-03-14 04:19:39 +00:00
|
|
|
};
|
|
|
|
|
2019-10-27 17:08:51 +00:00
|
|
|
KdeConnectPluginConfig::KdeConnectPluginConfig()
|
|
|
|
: d(new KdeConnectPluginConfigPrivate())
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-03-14 04:19:39 +00:00
|
|
|
KdeConnectPluginConfig::KdeConnectPluginConfig(const QString& deviceId, const QString& pluginName)
|
|
|
|
: 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
|
|
|
|
2019-06-10 15:40:28 +01:00
|
|
|
d->m_signal = QDBusMessage::createSignal(QStringLiteral("/kdeconnect/") + deviceId + QStringLiteral("/") + pluginName, QStringLiteral("org.kde.kdeconnect.config"), QStringLiteral("configChanged"));
|
2019-08-14 16:36:19 +01:00
|
|
|
DBusHelper::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
|
|
|
}
|
|
|
|
|
2019-10-27 17:08:51 +00:00
|
|
|
QString KdeConnectPluginConfig::getString(const QString& key, const QString& defaultValue)
|
|
|
|
{
|
|
|
|
if (!d->m_config) {
|
|
|
|
loadConfig();
|
|
|
|
}
|
|
|
|
|
|
|
|
d->m_config->sync();
|
|
|
|
return d->m_config->value(key, defaultValue).toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool KdeConnectPluginConfig::getBool(const QString& key, const bool defaultValue)
|
|
|
|
{
|
|
|
|
if (!d->m_config) {
|
|
|
|
loadConfig();
|
|
|
|
}
|
|
|
|
|
|
|
|
d->m_config->sync();
|
|
|
|
return d->m_config->value(key, defaultValue).toBool();
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray KdeConnectPluginConfig::getByteArray(const QString& key, const QByteArray defaultValue)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-12-05 22:11:57 +00:00
|
|
|
QVariantList KdeConnectPluginConfig::getList(const QString& key,
|
|
|
|
const QVariantList& defaultValue)
|
|
|
|
{
|
|
|
|
QVariantList list;
|
2017-09-03 20:39:44 +01:00
|
|
|
d->m_config->sync(); // note: need sync() to get recent changes signalled from other process
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-03-14 04:19:39 +00:00
|
|
|
void KdeConnectPluginConfig::set(const QString& key, const QVariant& value)
|
|
|
|
{
|
2017-09-03 20:39:44 +01:00
|
|
|
d->m_config->setValue(key, value);
|
|
|
|
d->m_config->sync();
|
2019-08-14 16:36:19 +01:00
|
|
|
DBusHelper::sessionBus().send(d->m_signal);
|
2015-09-12 12:46:37 +01:00
|
|
|
}
|
|
|
|
|
2015-12-05 22:11:57 +00:00
|
|
|
void KdeConnectPluginConfig::setList(const QString& key, const QVariantList& list)
|
|
|
|
{
|
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();
|
2019-08-14 16:36:19 +01:00
|
|
|
DBusHelper::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
|
|
|
|
|
|
|
void KdeConnectPluginConfig::setDeviceId(const QString& deviceId)
|
|
|
|
{
|
|
|
|
if (deviceId != m_deviceId) {
|
|
|
|
m_deviceId = deviceId;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!m_deviceId.isEmpty() && !m_pluginName.isEmpty()) {
|
|
|
|
loadConfig();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString KdeConnectPluginConfig::deviceId()
|
|
|
|
{
|
|
|
|
return m_deviceId;
|
|
|
|
}
|
|
|
|
|
|
|
|
void KdeConnectPluginConfig::setPluginName(const QString& pluginName)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
|
|
|
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()));
|
|
|
|
Q_EMIT configChanged();
|
|
|
|
}
|