2019-10-27 17:08:51 +00:00
|
|
|
/**
|
|
|
|
* SPDX-FileCopyrightText: 2019 Nicolas Fella <nicolas.fella@gmx.de>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "pluginmodel.h"
|
|
|
|
|
|
|
|
#include <QDebug>
|
2020-11-15 23:28:44 +00:00
|
|
|
|
2023-04-16 08:21:13 +01:00
|
|
|
#include "kcoreaddons_version.h"
|
2020-11-15 23:28:44 +00:00
|
|
|
#include <KConfigGroup>
|
2023-04-16 08:21:13 +01:00
|
|
|
#if KCOREADDONS_VERSION < QT_VERSION_CHECK(5, 86, 0)
|
2020-11-15 23:28:44 +00:00
|
|
|
#include <KPluginLoader>
|
2023-04-16 08:21:13 +01:00
|
|
|
#endif
|
2019-10-27 17:08:51 +00:00
|
|
|
|
|
|
|
PluginModel::PluginModel(QObject *parent)
|
|
|
|
: QAbstractListModel(parent)
|
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
connect(this, &QAbstractItemModel::rowsInserted, this, &PluginModel::rowsChanged);
|
|
|
|
connect(this, &QAbstractItemModel::rowsRemoved, this, &PluginModel::rowsChanged);
|
2019-10-27 17:08:51 +00:00
|
|
|
|
|
|
|
beginResetModel();
|
2023-04-16 08:21:13 +01:00
|
|
|
#if KCOREADDONS_VERSION < QT_VERSION_CHECK(5, 84, 0)
|
2020-11-15 23:28:44 +00:00
|
|
|
m_plugins = KPluginLoader::findPlugins(QStringLiteral("kdeconnect/"));
|
2023-04-16 08:21:13 +01:00
|
|
|
#else
|
|
|
|
m_plugins = KPluginMetaData::findPlugins(QStringLiteral("kdeconnect/"));
|
|
|
|
#endif
|
2019-10-27 17:08:51 +00:00
|
|
|
endResetModel();
|
|
|
|
}
|
|
|
|
|
|
|
|
PluginModel::~PluginModel()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void PluginModel::setDeviceId(const QString &deviceId)
|
2019-10-27 17:08:51 +00:00
|
|
|
{
|
|
|
|
if (deviceId == m_deviceId)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_deviceId = deviceId;
|
|
|
|
DeviceDbusInterface *device = new DeviceDbusInterface(m_deviceId);
|
|
|
|
m_config = KSharedConfig::openConfig(device->pluginsConfigFile());
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant PluginModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
if (!index.isValid()) {
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2020-11-15 23:28:44 +00:00
|
|
|
const KPluginMetaData &pluginEntry = m_plugins[index.row()];
|
2019-10-27 17:08:51 +00:00
|
|
|
|
|
|
|
switch (role) {
|
2022-09-10 22:23:52 +01:00
|
|
|
case Qt::CheckStateRole: {
|
2020-11-15 23:29:29 +00:00
|
|
|
const QString def = pluginEntry.isEnabledByDefault() ? QStringLiteral("true") : QStringLiteral("false");
|
2020-11-15 23:28:44 +00:00
|
|
|
return m_config->group("Plugins").readEntry(QStringLiteral("%1Enabled").arg(pluginEntry.pluginId()), def) == QStringLiteral("true");
|
2019-10-27 17:08:51 +00:00
|
|
|
}
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
return pluginEntry.name();
|
|
|
|
case IconRole:
|
2020-11-15 23:28:44 +00:00
|
|
|
return pluginEntry.iconName();
|
2019-10-27 17:08:51 +00:00
|
|
|
case IdRole:
|
2020-11-15 23:28:44 +00:00
|
|
|
return pluginEntry.pluginId();
|
2022-09-10 22:23:52 +01:00
|
|
|
case ConfigSourceRole: {
|
|
|
|
const QString configFile =
|
|
|
|
QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("kdeconnect/%1_config.qml").arg(pluginEntry.pluginId()));
|
2019-10-27 17:08:51 +00:00
|
|
|
if (configFile.isEmpty())
|
|
|
|
return QUrl();
|
|
|
|
|
|
|
|
return QUrl::fromLocalFile(configFile);
|
|
|
|
}
|
2020-11-15 23:39:23 +00:00
|
|
|
case DescriptionRole:
|
|
|
|
return pluginEntry.description();
|
2019-10-27 17:08:51 +00:00
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QHash<int, QByteArray> PluginModel::roleNames() const
|
|
|
|
{
|
|
|
|
QHash<int, QByteArray> roles = QAbstractItemModel::roleNames();
|
|
|
|
roles[Qt::DisplayRole] = "name";
|
|
|
|
roles[Qt::CheckStateRole] = "isChecked";
|
|
|
|
roles[IconRole] = "iconName";
|
|
|
|
roles[IdRole] = "pluginId";
|
|
|
|
roles[ConfigSourceRole] = "configSource";
|
2020-11-15 23:39:23 +00:00
|
|
|
roles[DescriptionRole] = "description";
|
2019-10-27 17:08:51 +00:00
|
|
|
return roles;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PluginModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
|
|
|
{
|
|
|
|
if (!index.isValid()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ret = false;
|
|
|
|
|
|
|
|
if (role == Qt::CheckStateRole) {
|
2020-11-15 23:28:44 +00:00
|
|
|
const KPluginMetaData &pluginEntry = m_plugins[index.row()];
|
|
|
|
m_config->group("Plugins").writeEntry(QStringLiteral("%1Enabled").arg(pluginEntry.pluginId()), value);
|
2019-10-27 17:08:51 +00:00
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_config->sync();
|
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
Q_EMIT dataChanged(index, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int PluginModel::rowCount(const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
if (parent.isValid()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_plugins.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString PluginModel::deviceId()
|
|
|
|
{
|
|
|
|
return m_deviceId;
|
|
|
|
}
|