File indexing completed on 2024-05-05 17:45:03

0001 /*
0002     SPDX-FileCopyrightText: 2006 Aaron Seigo <aseigo@kde.org>
0003     SPDX-FileCopyrightText: 2010 Marco Martin <notmart@gmail.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-only
0006 */
0007 
0008 #include "windowedwidgetsrunner.h"
0009 
0010 #include <QProcess>
0011 
0012 #include <QIcon>
0013 #include <QMimeData>
0014 
0015 #include <KLocalizedString>
0016 #include <QDebug>
0017 
0018 #include <Plasma/Applet>
0019 #include <Plasma/PluginLoader>
0020 #include <QMutexLocker>
0021 
0022 K_PLUGIN_CLASS_WITH_JSON(WindowedWidgetsRunner, "plasma-runner-windowedwidgets.json")
0023 
0024 WindowedWidgetsRunner::WindowedWidgetsRunner(QObject *parent, const KPluginMetaData &metaData, const QVariantList &args)
0025     : Plasma::AbstractRunner(parent, metaData, args)
0026 {
0027     setObjectName(QStringLiteral("WindowedWidgets"));
0028     setPriority(AbstractRunner::HighestPriority);
0029 
0030     addSyntax(Plasma::RunnerSyntax(QStringLiteral(":q:"), i18n("Finds Plasma widgets whose name or description match :q:")));
0031     addSyntax(Plasma::RunnerSyntax(i18nc("Note this is a KRunner keyword", "mobile applications"),
0032                                    i18n("list all Plasma widgets that can run as standalone applications")));
0033     setMinLetterCount(3);
0034     connect(this, &AbstractRunner::teardown, this, [this]() {
0035         m_applets.clear();
0036     });
0037 }
0038 
0039 WindowedWidgetsRunner::~WindowedWidgetsRunner()
0040 {
0041 }
0042 
0043 void WindowedWidgetsRunner::match(Plasma::RunnerContext &context)
0044 {
0045     loadMetadataList();
0046     const QString term = context.query();
0047     QList<Plasma::QueryMatch> matches;
0048 
0049     for (const KPluginMetaData &md : qAsConst(m_applets)) {
0050         if (((md.name().contains(term, Qt::CaseInsensitive) || md.value(QLatin1String("GenericName")).contains(term, Qt::CaseInsensitive)
0051               || md.description().contains(term, Qt::CaseInsensitive))
0052              || md.category().contains(term, Qt::CaseInsensitive) || term.startsWith(i18nc("Note this is a KRunner keyword", "mobile applications")))) {
0053             Plasma::QueryMatch match(this);
0054             match.setText(md.name());
0055             match.setSubtext(md.description());
0056             match.setIconName(md.iconName());
0057             match.setData(md.pluginId());
0058             if (md.name().compare(term, Qt::CaseInsensitive) == 0) {
0059                 match.setType(Plasma::QueryMatch::ExactMatch);
0060                 match.setRelevance(1);
0061             } else {
0062                 match.setType(Plasma::QueryMatch::PossibleMatch);
0063                 match.setRelevance(0.7);
0064             }
0065             matches << match;
0066         }
0067     }
0068 
0069     if (!context.isValid()) {
0070         return;
0071     }
0072 
0073     context.addMatches(matches);
0074 }
0075 
0076 void WindowedWidgetsRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match)
0077 {
0078     Q_UNUSED(context);
0079     QProcess::startDetached(QStringLiteral("plasmawindowed"), {match.data().toString()});
0080 }
0081 
0082 QMimeData *WindowedWidgetsRunner::mimeDataForMatch(const Plasma::QueryMatch &match)
0083 {
0084     QMimeData *data = new QMimeData();
0085     data->setData(QStringLiteral("text/x-plasmoidservicename"), match.data().toString().toUtf8());
0086     return data;
0087 }
0088 
0089 void WindowedWidgetsRunner::loadMetadataList()
0090 {
0091     // We call this method in the match thread
0092     QMutexLocker locker(&m_mutex);
0093     // If the entries have already been loaded we reuse them for the same session
0094     if (!m_applets.isEmpty()) {
0095         return;
0096     }
0097     const auto &listMetadata = Plasma::PluginLoader::self()->listAppletMetaData(QString());
0098     for (const KPluginMetaData &md : listMetadata) {
0099         if (md.isValid() && !md.rawData().value(QStringLiteral("NoDisplay")).toBool()
0100             && md.rawData().value(QStringLiteral("X-Plasma-StandAloneApp")).toBool()) {
0101             m_applets << md;
0102         }
0103     }
0104 }
0105 
0106 #include "windowedwidgetsrunner.moc"