File indexing completed on 2024-05-12 05:36:51
0001 /* 0002 * SPDX-FileCopyrightText: 2020 David Redondo <kde@david-redondo.de> 0003 * 0004 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0005 */ 0006 0007 #include "WidgetExporter.h" 0008 0009 #include "FaceLoader.h" 0010 #include "PageDataObject.h" 0011 0012 #include <KConfigGroup> 0013 #include <KSharedConfig> 0014 0015 #include <QDBusConnection> 0016 #include <QDBusConnectionInterface> 0017 #include <QDBusServiceWatcher> 0018 0019 static const QString plasmashellService = QStringLiteral("org.kde.plasmashell"); 0020 0021 WidgetExporter::WidgetExporter(QObject *parent) 0022 : QObject(parent) 0023 { 0024 m_plasmashellAvailable = QDBusConnection::sessionBus().interface()->isServiceRegistered(plasmashellService); 0025 auto serviceWatcher = new QDBusServiceWatcher(plasmashellService, QDBusConnection::sessionBus(), QDBusServiceWatcher::WatchForOwnerChange, this); 0026 connect(serviceWatcher, &QDBusServiceWatcher::serviceRegistered, this, [this] { 0027 m_plasmashellAvailable = true; 0028 Q_EMIT plasmashellAvailableChanged(); 0029 }); 0030 connect(serviceWatcher, &QDBusServiceWatcher::serviceUnregistered, this, [this] { 0031 m_plasmashellAvailable = false; 0032 Q_EMIT plasmashellAvailableChanged(); 0033 }); 0034 } 0035 0036 void WidgetExporter::exportAsWidget(FaceLoader *loader) const 0037 { 0038 const PageDataObject *data = loader->dataObject(); 0039 const QString group = data->value(QStringLiteral("face")).toString(); 0040 const KConfigGroup configGroup = data->config()->group(group); 0041 0042 QString script = QStringLiteral(R"(a = currentActivity() 0043 d = desktopsForActivity(a)[0] 0044 w = d.addWidget('org.kde.plasma.systemmonitor'))"); 0045 script += QChar('\n'); 0046 script += configEntriesScript(configGroup); 0047 script += QStringLiteral("w.reloadConfig\n"); 0048 0049 auto call = QDBusMessage::createMethodCall(plasmashellService, 0050 QStringLiteral("/PlasmaShell"), 0051 QStringLiteral("org.kde.PlasmaShell"), 0052 QStringLiteral("evaluateScript")); 0053 call.setArguments({script}); 0054 QDBusConnection::sessionBus().asyncCall(call); 0055 } 0056 0057 QString WidgetExporter::configEntriesScript(const KConfigGroup &group, const QStringList &path) const 0058 { 0059 QString script = QStringLiteral("w.currentConfigGroup = new Array(%1)\n").arg(path.join(',')); 0060 for (const auto &entry : group.keyList()) { 0061 script += QStringLiteral("w.writeConfig('%1','%2')\n").arg(entry, group.readEntry(entry)); 0062 } 0063 for (const auto &groupName : group.groupList()) { 0064 QStringList groupPath = path; 0065 groupPath.append(QStringLiteral("'%1'").arg(groupName)); 0066 script += configEntriesScript(group.group(groupName), groupPath); 0067 } 0068 return script; 0069 } 0070 0071 #include "moc_WidgetExporter.cpp"