kdevelopsessions.cpp 4.77 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
 *   Copyright 2008,2011 Sebastian Kügler <sebas@kde.org>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Library General Public License as
 *   published by the Free Software Foundation; either version 2, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details
 *
 *   You should have received a copy of the GNU Library General Public
 *   License along with this program; if not, write to the
 *   Free Software Foundation, Inc.,
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "kdevelopsessions.h"
21

22
23
24
// KDevelopSessionsWatch
#include <kdevelopsessionswatch.h>
// KF
25
#include <KLocalizedString>
26
27
28
// Qt
#include <QDebug>
#include <QCollator>
29

30
31
32
#if KRUNNER_VERSION >= QT_VERSION_CHECK(5, 72, 0)
K_EXPORT_PLASMA_RUNNER_WITH_JSON(KDevelopSessions, "kdevelopsessions.json")
#else
33
K_EXPORT_PLASMA_RUNNER(kdevelopsessions, KDevelopSessions)
34
#endif
35

36
37
38
39
#if KRUNNER_VERSION >= QT_VERSION_CHECK(5, 77, 0)
KDevelopSessions::KDevelopSessions(QObject* parent, const KPluginMetaData& metaData, const QVariantList& args)
    : Plasma::AbstractRunner(parent, metaData, args)
#else
40
41
KDevelopSessions::KDevelopSessions(QObject *parent, const QVariantList& args)
    : Plasma::AbstractRunner(parent, args)
42
#endif
43
{
Milian Wolff's avatar
Milian Wolff committed
44
    setObjectName(QStringLiteral("KDevelop Sessions"));
45

Milian Wolff's avatar
Milian Wolff committed
46
47
    Plasma::RunnerSyntax s(QStringLiteral(":q:"), i18n("Finds KDevelop sessions matching :q:."));
    s.addExampleQuery(QStringLiteral("kdevelop :q:"));
48
49
    addSyntax(s);

50
51
52
#if KRUNNER_VERSION >= QT_VERSION_CHECK(5, 76, 0)
    addSyntax(Plasma::RunnerSyntax(QStringLiteral("kdevelop"), i18n("Lists all the KDevelop editor sessions in your account.")));
#else
Milian Wolff's avatar
Milian Wolff committed
53
    setDefaultSyntax(Plasma::RunnerSyntax(QStringLiteral("kdevelop"), i18n("Lists all the KDevelop editor sessions in your account.")));
54
#endif
55
56
}

57
58
59
60
KDevelopSessions::~KDevelopSessions()
{
    KDevelopSessionsWatch::unregisterObserver(this);
}
61

62
void KDevelopSessions::init()
63
{
64
65
66
67
68
69
70
71
    KDevelopSessionsWatch::registerObserver(this);

    Plasma::AbstractRunner::init();
}

void KDevelopSessions::setSessionDataList(const QVector<KDevelopSessionData>& sessionDataList)
{
    m_sessionDataList = sessionDataList;
72
73
74
75
76
}

void KDevelopSessions::match(Plasma::RunnerContext &context)
{
    QString term = context.query();
77
    if (term.size() < 3) {
78
79
80
81
        return;
    }

    bool listAll = false;
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
    if (term.startsWith(QLatin1String("kdevelop"), Qt::CaseInsensitive)) {
        const QStringRef trimmedStrippedTerm = term.midRef(8).trimmed();
        // "kdevelop" -> list all sessions
        if (trimmedStrippedTerm.isEmpty()) {
            listAll = true;
            term.clear();
        }
        // "kdevelop X" -> list all sessions with "X"
        else if (term.at(8) == QLatin1Char(' ') ) {
            term = trimmedStrippedTerm.toString();
        }
        // "kdevelopX" -> list all sessions with "kdevelopX"
        else {
            term = term.trimmed();
        }
    }

    if (term.isEmpty() && !listAll) {
100
101
102
        return;
    }

103
    for (const auto& session : qAsConst(m_sessionDataList)) {
104
105
106
107
        if (!context.isValid()) {
            return;
        }

108
        if (listAll || (!term.isEmpty() && session.description.contains(term, Qt::CaseInsensitive))) {
109
110
111
112
113
114
            Plasma::QueryMatch match(this);
            if (listAll) {
                // All sessions listed, but with a low priority
                match.setType(Plasma::QueryMatch::ExactMatch);
                match.setRelevance(0.8);
            } else {
115
                if (session.description.compare(term, Qt::CaseInsensitive) == 0) {
116
117
118
119
120
121
122
123
124
                    // parameter to kdevelop matches session exactly, bump it up!
                    match.setType(Plasma::QueryMatch::ExactMatch);
                    match.setRelevance(1.0);
                } else {
                    // fuzzy match of the session in "kdevelop $session"
                    match.setType(Plasma::QueryMatch::PossibleMatch);
                    match.setRelevance(0.8);
                }
            }
Kevin Funk's avatar
Kevin Funk committed
125
            match.setIconName(QStringLiteral("kdevelop"));
126
            match.setData(session.id);
127
            match.setText(session.description);
128
            match.setSubtext(i18n("Open KDevelop Session"));
129
            context.addMatch(match);
130
131
132
133
134
135
136
        }
    }
}

void KDevelopSessions::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match)
{
    Q_UNUSED(context)
137
    const QString sessionId = match.data().toString();
138
    qDebug() << "Open KDevelop session" << sessionId;
139
    KDevelopSessionsWatch::openSession(sessionId);
140
141
}

142
#include "kdevelopsessions.moc"