2015-03-14 04:19:39 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2015 Albert Vaca <albertvaka@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of
|
|
|
|
* the License or (at your option) version 3 or any later version
|
|
|
|
* accepted by the membership of KDE e.V. (or its successor approved
|
|
|
|
* by the membership of KDE e.V.), which shall act as a proxy
|
|
|
|
* defined in Section 14 of version 3 of the license.
|
|
|
|
*
|
|
|
|
* This program 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2019-03-23 16:29:26 +00:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
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>
|
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
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
QVariant KdeConnectPluginConfig::get(const QString& key, const QVariant& defaultValue)
|
|
|
|
{
|
2017-09-03 20:39:44 +01:00
|
|
|
d->m_config->sync();
|
|
|
|
return d->m_config->value(key, defaultValue);
|
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
|
|
|
}
|