File indexing completed on 2024-12-08 13:22:27

0001 /*
0002  *   Copyright 2014 Marco Martin <mart@kde.org>
0003  *
0004  *   This program is free software; you can redistribute it and/or modify
0005  *   it under the terms of the GNU Library General Public License as
0006  *   published by the Free Software Foundation; either version 2, or
0007  *   (at your option) any later version.
0008  *
0009  *   This program is distributed in the hope that it will be useful,
0010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012  *   GNU General Public License for more details
0013  *
0014  *   You should have received a copy of the GNU Library General Public
0015  *   License along with this program; if not, write to the
0016  *   Free Software Foundation, Inc.,
0017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
0018  */
0019 
0020 #include "alternativeshelper.h"
0021 
0022 // Qt
0023 #include <QQmlEngine>
0024 #include <QQmlContext>
0025 
0026 // KDE
0027 #include <KPackage/Package>
0028 #include <kconfig_version.h>
0029 
0030 // Plasma
0031 #include <Plasma/Containment>
0032 #include <Plasma/PluginLoader>
0033 
0034 AlternativesHelper::AlternativesHelper(Plasma::Applet *applet, QObject *parent)
0035     : QObject(parent),
0036       m_applet(applet)
0037 {
0038 }
0039 
0040 AlternativesHelper::~AlternativesHelper()
0041 {
0042 }
0043 
0044 QStringList AlternativesHelper::appletProvides() const
0045 {
0046 #if KCONFIG_VERSION_MINOR >= 27
0047     return KPluginMetaData::readStringList(m_applet->pluginMetaData().rawData(), QStringLiteral("X-Plasma-Provides"));
0048 #else
0049     return KPluginMetaData::readStringList(m_applet->pluginInfo().toMetaData().rawData(), QStringLiteral("X-Plasma-Provides"));
0050 #endif
0051 }
0052 
0053 QString AlternativesHelper::currentPlugin() const
0054 {
0055 #if KCONFIG_VERSION_MINOR >= 27
0056     return m_applet->pluginMetaData().pluginId();
0057 #else
0058     return m_applet->pluginInfo().toMetaData().pluginId();
0059 #endif
0060 }
0061 
0062 QQuickItem *AlternativesHelper::applet() const
0063 {
0064     return m_applet->property("_plasma_graphicObject").value<QQuickItem *>();
0065 }
0066 
0067 void AlternativesHelper::loadAlternative(const QString &plugin)
0068 {
0069     if (plugin == currentPlugin() || m_applet->isContainment()) {
0070         return;
0071     }
0072 
0073     Plasma::Containment *cont = m_applet->containment();
0074 
0075     if (!cont) {
0076         return;
0077     }
0078 
0079     QQuickItem *appletItem = m_applet->property("_plasma_graphicObject").value<QQuickItem *>();
0080     QQuickItem *contItem = cont->property("_plasma_graphicObject").value<QQuickItem *>();
0081 
0082     if (!appletItem || !contItem) {
0083         return;
0084     }
0085 
0086     // ensure the global shortcut is moved to the new applet
0087     const QKeySequence &shortcut = m_applet->globalShortcut();
0088     m_applet->setGlobalShortcut(QKeySequence()); // need to unmap the old one first
0089 
0090     const QPoint newPos = appletItem->mapToItem(contItem, QPointF(0, 0)).toPoint();
0091 
0092     m_applet->destroy();
0093 
0094     connect(m_applet, &QObject::destroyed, [ = ]() {
0095         Plasma::Applet *newApplet = Q_NULLPTR;
0096         QMetaObject::invokeMethod(contItem, "createApplet", Q_RETURN_ARG(Plasma::Applet *, newApplet), Q_ARG(QString, plugin), Q_ARG(QVariantList, QVariantList()), Q_ARG(QPoint, newPos));
0097 
0098         if (newApplet) {
0099             newApplet->setGlobalShortcut(shortcut);
0100         }
0101     });
0102 }
0103 
0104 #include "moc_alternativeshelper.cpp"
0105