File: windowedwidgetsrunner.cpp

package info (click to toggle)
plasma-workspace 4%3A5.27.5-2%2Bdeb12u2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 102,040 kB
  • sloc: cpp: 121,800; xml: 3,238; python: 645; perl: 586; sh: 254; javascript: 113; ruby: 62; makefile: 15; ansic: 13
file content (106 lines) | stat: -rw-r--r-- 3,684 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
    SPDX-FileCopyrightText: 2006 Aaron Seigo <aseigo@kde.org>
    SPDX-FileCopyrightText: 2010 Marco Martin <notmart@gmail.com>

    SPDX-License-Identifier: LGPL-2.0-only
*/

#include "windowedwidgetsrunner.h"

#include <QProcess>

#include <QIcon>
#include <QMimeData>

#include <KLocalizedString>
#include <QDebug>

#include <Plasma/Applet>
#include <Plasma/PluginLoader>
#include <QMutexLocker>

K_PLUGIN_CLASS_WITH_JSON(WindowedWidgetsRunner, "plasma-runner-windowedwidgets.json")

WindowedWidgetsRunner::WindowedWidgetsRunner(QObject *parent, const KPluginMetaData &metaData, const QVariantList &args)
    : Plasma::AbstractRunner(parent, metaData, args)
{
    setObjectName(QStringLiteral("WindowedWidgets"));
    setPriority(AbstractRunner::HighestPriority);

    addSyntax(Plasma::RunnerSyntax(QStringLiteral(":q:"), i18n("Finds Plasma widgets whose name or description match :q:")));
    addSyntax(Plasma::RunnerSyntax(i18nc("Note this is a KRunner keyword", "mobile applications"),
                                   i18n("list all Plasma widgets that can run as standalone applications")));
    setMinLetterCount(3);
    connect(this, &AbstractRunner::teardown, this, [this]() {
        m_applets.clear();
    });
}

WindowedWidgetsRunner::~WindowedWidgetsRunner()
{
}

void WindowedWidgetsRunner::match(Plasma::RunnerContext &context)
{
    loadMetadataList();
    const QString term = context.query();
    QList<Plasma::QueryMatch> matches;

    for (const KPluginMetaData &md : qAsConst(m_applets)) {
        if (((md.name().contains(term, Qt::CaseInsensitive) || md.value(QLatin1String("GenericName")).contains(term, Qt::CaseInsensitive)
              || md.description().contains(term, Qt::CaseInsensitive))
             || md.category().contains(term, Qt::CaseInsensitive) || term.startsWith(i18nc("Note this is a KRunner keyword", "mobile applications")))) {
            Plasma::QueryMatch match(this);
            match.setText(md.name());
            match.setSubtext(md.description());
            match.setIconName(md.iconName());
            match.setData(md.pluginId());
            if (md.name().compare(term, Qt::CaseInsensitive) == 0) {
                match.setType(Plasma::QueryMatch::ExactMatch);
                match.setRelevance(1);
            } else {
                match.setType(Plasma::QueryMatch::PossibleMatch);
                match.setRelevance(0.7);
            }
            matches << match;
        }
    }

    if (!context.isValid()) {
        return;
    }

    context.addMatches(matches);
}

void WindowedWidgetsRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match)
{
    Q_UNUSED(context);
    QProcess::startDetached(QStringLiteral("plasmawindowed"), {match.data().toString()});
}

QMimeData *WindowedWidgetsRunner::mimeDataForMatch(const Plasma::QueryMatch &match)
{
    QMimeData *data = new QMimeData();
    data->setData(QStringLiteral("text/x-plasmoidservicename"), match.data().toString().toUtf8());
    return data;
}

void WindowedWidgetsRunner::loadMetadataList()
{
    // We call this method in the match thread
    QMutexLocker locker(&m_mutex);
    // If the entries have already been loaded we reuse them for the same session
    if (!m_applets.isEmpty()) {
        return;
    }
    const auto &listMetadata = Plasma::PluginLoader::self()->listAppletMetaData(QString());
    for (const KPluginMetaData &md : listMetadata) {
        if (md.isValid() && !md.rawData().value(QStringLiteral("NoDisplay")).toBool()
            && md.rawData().value(QStringLiteral("X-Plasma-StandAloneApp")).toBool()) {
            m_applets << md;
        }
    }
}

#include "windowedwidgetsrunner.moc"