forked from KDE/kdeconnect-kde
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpluginmodel.cpp
121 lines (98 loc) · 3.17 KB
/
pluginmodel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
* SPDX-FileCopyrightText: 2019 Nicolas Fella <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
#include "pluginmodel.h"
#include <QDebug>
#include "kcoreaddons_version.h"
#include <KConfigGroup>
PluginModel::PluginModel(QObject *parent)
: QAbstractListModel(parent)
{
connect(this, &QAbstractItemModel::rowsInserted, this, &PluginModel::rowsChanged);
connect(this, &QAbstractItemModel::rowsRemoved, this, &PluginModel::rowsChanged);
m_plugins = KPluginMetaData::findPlugins(QStringLiteral("kdeconnect"));
}
PluginModel::~PluginModel()
{
}
void PluginModel::setDeviceId(const QString &deviceId)
{
if (deviceId == m_deviceId)
return;
m_deviceId = deviceId;
DeviceDbusInterface *device = new DeviceDbusInterface(m_deviceId);
m_config = KSharedConfig::openConfig(device->pluginsConfigFile());
Q_EMIT deviceIdChanged(deviceId);
}
QVariant PluginModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid()) {
return QVariant();
}
const KPluginMetaData &pluginEntry = m_plugins[index.row()];
switch (role) {
case Qt::CheckStateRole: {
const QString def = pluginEntry.isEnabledByDefault() ? QStringLiteral("true") : QStringLiteral("false");
return m_config->group(QStringLiteral("Plugins")).readEntry(QStringLiteral("%1Enabled").arg(pluginEntry.pluginId()), def) == QStringLiteral("true");
}
case Qt::DisplayRole:
return pluginEntry.name();
case IconRole:
return pluginEntry.iconName();
case IdRole:
return pluginEntry.pluginId();
case ConfigSourceRole: {
const QString configFile =
QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("kdeconnect/%1_config.qml").arg(pluginEntry.pluginId()));
if (configFile.isEmpty())
return QUrl();
return QUrl::fromLocalFile(configFile);
}
case DescriptionRole:
return pluginEntry.description();
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";
roles[DescriptionRole] = "description";
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) {
const KPluginMetaData &pluginEntry = m_plugins[index.row()];
m_config->group(QStringLiteral("Plugins")).writeEntry(QStringLiteral("%1Enabled").arg(pluginEntry.pluginId()), value);
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;
}
#include "moc_pluginmodel.cpp"