File indexing completed on 2024-04-21 05:31:14

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 // Qt
0010 #include <QQmlEngine>
0011 #include <QQmlContext>
0012 
0013 // KDE
0014 #include <KPackage/Package>
0015 #include <kconfig_version.h>
0016 
0017 // Plasma
0018 #include <Plasma/Containment>
0019 #include <Plasma/PluginLoader>
0020 
0021 AlternativesHelper::AlternativesHelper(Plasma::Applet *applet, QObject *parent)
0022     : QObject(parent),
0023       m_applet(applet)
0024 {
0025 }
0026 
0027 AlternativesHelper::~AlternativesHelper()
0028 {
0029 }
0030 
0031 QStringList AlternativesHelper::appletProvides() const
0032 {
0033 #if KCONFIG_VERSION_MINOR >= 27
0034     return KPluginMetaData::readStringList(m_applet->pluginMetaData().rawData(), QStringLiteral("X-Plasma-Provides"));
0035 #else
0036     return KPluginMetaData::readStringList(m_applet->pluginInfo().toMetaData().rawData(), QStringLiteral("X-Plasma-Provides"));
0037 #endif
0038 }
0039 
0040 QString AlternativesHelper::currentPlugin() const
0041 {
0042 #if KCONFIG_VERSION_MINOR >= 27
0043     return m_applet->pluginMetaData().pluginId();
0044 #else
0045     return m_applet->pluginInfo().toMetaData().pluginId();
0046 #endif
0047 }
0048 
0049 QQuickItem *AlternativesHelper::applet() const
0050 {
0051     return m_applet->property("_plasma_graphicObject").value<QQuickItem *>();
0052 }
0053 
0054 void AlternativesHelper::loadAlternative(const QString &plugin)
0055 {
0056     if (plugin == currentPlugin() || m_applet->isContainment()) {
0057         return;
0058     }
0059 
0060     Plasma::Containment *cont = m_applet->containment();
0061 
0062     if (!cont) {
0063         return;
0064     }
0065 
0066     QQuickItem *appletItem = m_applet->property("_plasma_graphicObject").value<QQuickItem *>();
0067     QQuickItem *contItem = cont->property("_plasma_graphicObject").value<QQuickItem *>();
0068 
0069     if (!appletItem || !contItem) {
0070         return;
0071     }
0072 
0073     // ensure the global shortcut is moved to the new applet
0074     const QKeySequence &shortcut = m_applet->globalShortcut();
0075     m_applet->setGlobalShortcut(QKeySequence()); // need to unmap the old one first
0076 
0077     const QPoint newPos = appletItem->mapToItem(contItem, QPointF(0, 0)).toPoint();
0078 
0079     m_applet->destroy();
0080 
0081     connect(m_applet, &QObject::destroyed, [ = ]() {
0082         Plasma::Applet *newApplet = Q_NULLPTR;
0083         QMetaObject::invokeMethod(contItem, "createApplet", Q_RETURN_ARG(Plasma::Applet *, newApplet), Q_ARG(QString, plugin), Q_ARG(QVariantList, QVariantList()), Q_ARG(QPoint, newPos));
0084 
0085         if (newApplet) {
0086             newApplet->setGlobalShortcut(shortcut);
0087         }
0088     });
0089 }
0090 
0091 #include "moc_alternativeshelper.cpp"
0092