File indexing completed on 2024-04-28 16:54:56

0001 /*
0002     SPDX-FileCopyrightText: 2014 Marco Martin <mart@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "alternativeshelper.h"
0008 
0009 #include <QQmlContext>
0010 #include <QQmlEngine>
0011 
0012 #include <KConfigLoader>
0013 
0014 #include <Plasma/Containment>
0015 #include <Plasma/PluginLoader>
0016 
0017 AlternativesHelper::AlternativesHelper(Plasma::Applet *applet, QObject *parent)
0018     : QObject(parent)
0019     , m_applet(applet)
0020 {
0021 }
0022 
0023 AlternativesHelper::~AlternativesHelper()
0024 {
0025 }
0026 
0027 QStringList AlternativesHelper::appletProvides() const
0028 {
0029     return m_applet->pluginMetaData().value(QStringLiteral("X-Plasma-Provides"), QStringList());
0030 }
0031 
0032 QString AlternativesHelper::currentPlugin() const
0033 {
0034     return m_applet->pluginMetaData().pluginId();
0035 }
0036 
0037 QQuickItem *AlternativesHelper::applet() const
0038 {
0039     return m_applet->property("_plasma_graphicObject").value<QQuickItem *>();
0040 }
0041 
0042 void AlternativesHelper::loadAlternative(const QString &plugin)
0043 {
0044     if (plugin == m_applet->pluginMetaData().pluginId() || m_applet->isContainment()) {
0045         return;
0046     }
0047 
0048     Plasma::Containment *cont = m_applet->containment();
0049     if (!cont) {
0050         return;
0051     }
0052 
0053     QQuickItem *appletItem = m_applet->property("_plasma_graphicObject").value<QQuickItem *>();
0054     QQuickItem *contItem = cont->property("_plasma_graphicObject").value<QQuickItem *>();
0055     if (!appletItem || !contItem) {
0056         return;
0057     }
0058 
0059     // ensure the global shortcut is moved to the new applet
0060     const QKeySequence &shortcut = m_applet->globalShortcut();
0061     m_applet->setGlobalShortcut(QKeySequence()); // need to unmap the old one first
0062 
0063     const QPoint newPos = appletItem->mapToItem(contItem, QPointF(0, 0)).toPoint();
0064 
0065     Plasma::Applet *newApplet = nullptr;
0066     QMetaObject::invokeMethod(contItem,
0067                               "createApplet",
0068                               Q_RETURN_ARG(Plasma::Applet *, newApplet),
0069                               Q_ARG(QString, plugin),
0070                               Q_ARG(QVariantList, QVariantList()),
0071                               Q_ARG(QPoint, newPos));
0072 
0073     if (newApplet) {
0074         newApplet->setGlobalShortcut(shortcut);
0075         KConfigGroup newCg(newApplet->config());
0076         m_applet->config().copyTo(&newCg);
0077         // To let ConfigPropertyMap reload its config
0078         Q_EMIT newApplet->configScheme()->configChanged();
0079     }
0080 
0081     m_applet->destroy();
0082 }
0083 
0084 #include "moc_alternativeshelper.cpp"