File indexing completed on 2024-10-13 04:27:35
0001 /** 0002 * SPDX-FileCopyrightText: 2019 Nicolas Fella <nicolas.fella@gmx.de> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0005 */ 0006 0007 #include "pluginmodel.h" 0008 0009 #include <QDebug> 0010 0011 #include "kcoreaddons_version.h" 0012 #include <KConfigGroup> 0013 0014 PluginModel::PluginModel(QObject *parent) 0015 : QAbstractListModel(parent) 0016 { 0017 connect(this, &QAbstractItemModel::rowsInserted, this, &PluginModel::rowsChanged); 0018 connect(this, &QAbstractItemModel::rowsRemoved, this, &PluginModel::rowsChanged); 0019 m_plugins = KPluginMetaData::findPlugins(QStringLiteral("kdeconnect")); 0020 } 0021 0022 PluginModel::~PluginModel() 0023 { 0024 } 0025 0026 void PluginModel::setDeviceId(const QString &deviceId) 0027 { 0028 if (deviceId == m_deviceId) 0029 return; 0030 0031 m_deviceId = deviceId; 0032 DeviceDbusInterface *device = new DeviceDbusInterface(m_deviceId); 0033 m_config = KSharedConfig::openConfig(device->pluginsConfigFile()); 0034 0035 Q_EMIT deviceIdChanged(deviceId); 0036 } 0037 0038 QVariant PluginModel::data(const QModelIndex &index, int role) const 0039 { 0040 if (!index.isValid()) { 0041 return QVariant(); 0042 } 0043 0044 const KPluginMetaData &pluginEntry = m_plugins[index.row()]; 0045 0046 switch (role) { 0047 case Qt::CheckStateRole: { 0048 const QString def = pluginEntry.isEnabledByDefault() ? QStringLiteral("true") : QStringLiteral("false"); 0049 return m_config->group(QStringLiteral("Plugins")).readEntry(QStringLiteral("%1Enabled").arg(pluginEntry.pluginId()), def) == QStringLiteral("true"); 0050 } 0051 case Qt::DisplayRole: 0052 return pluginEntry.name(); 0053 case IconRole: 0054 return pluginEntry.iconName(); 0055 case IdRole: 0056 return pluginEntry.pluginId(); 0057 case ConfigSourceRole: { 0058 const QString configFile = 0059 QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("kdeconnect/%1_config.qml").arg(pluginEntry.pluginId())); 0060 if (configFile.isEmpty()) 0061 return QUrl(); 0062 0063 return QUrl::fromLocalFile(configFile); 0064 } 0065 case DescriptionRole: 0066 return pluginEntry.description(); 0067 default: 0068 return QVariant(); 0069 } 0070 } 0071 0072 QHash<int, QByteArray> PluginModel::roleNames() const 0073 { 0074 QHash<int, QByteArray> roles = QAbstractItemModel::roleNames(); 0075 roles[Qt::DisplayRole] = "name"; 0076 roles[Qt::CheckStateRole] = "isChecked"; 0077 roles[IconRole] = "iconName"; 0078 roles[IdRole] = "pluginId"; 0079 roles[ConfigSourceRole] = "configSource"; 0080 roles[DescriptionRole] = "description"; 0081 return roles; 0082 } 0083 0084 bool PluginModel::setData(const QModelIndex &index, const QVariant &value, int role) 0085 { 0086 if (!index.isValid()) { 0087 return false; 0088 } 0089 0090 bool ret = false; 0091 0092 if (role == Qt::CheckStateRole) { 0093 const KPluginMetaData &pluginEntry = m_plugins[index.row()]; 0094 m_config->group(QStringLiteral("Plugins")).writeEntry(QStringLiteral("%1Enabled").arg(pluginEntry.pluginId()), value); 0095 ret = true; 0096 } 0097 0098 m_config->sync(); 0099 0100 if (ret) { 0101 Q_EMIT dataChanged(index, index); 0102 } 0103 0104 return ret; 0105 } 0106 0107 int PluginModel::rowCount(const QModelIndex &parent) const 0108 { 0109 if (parent.isValid()) { 0110 return 0; 0111 } 0112 0113 return m_plugins.size(); 0114 } 0115 0116 QString PluginModel::deviceId() 0117 { 0118 return m_deviceId; 0119 } 0120 0121 #include "moc_pluginmodel.cpp"