File indexing completed on 2024-05-12 04:37:32
0001 /* 0002 SPDX-FileCopyrightText: 2008, 2011 Sebastian Kügler <sebas@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "kdevelopsessions.h" 0008 0009 // KDevelopSessionsWatch 0010 #include <kdevelopsessionswatch.h> 0011 // KF 0012 #include <KLocalizedString> 0013 #include <KRunner/krunner_version.h> 0014 // Qt 0015 #include <QDebug> 0016 #include <QCollator> 0017 0018 K_PLUGIN_CLASS_WITH_JSON(KDevelopSessions, "kdevelopsessions.json") 0019 0020 KDevelopSessions::KDevelopSessions(QObject* parent, const KPluginMetaData& metaData, const QVariantList& args) 0021 : Plasma::AbstractRunner(parent, metaData, args) 0022 { 0023 setObjectName(QStringLiteral("KDevelop Sessions")); 0024 0025 #if KRUNNER_VERSION < QT_VERSION_CHECK(5, 106, 0) 0026 Plasma::RunnerSyntax s(QStringLiteral(":q:"), i18n("Finds KDevelop sessions matching :q:.")); 0027 s.addExampleQuery(QStringLiteral("kdevelop :q:")); 0028 #else 0029 Plasma::RunnerSyntax s({QStringLiteral(":q:"), QStringLiteral("kdevelop :q:")}, 0030 i18n("Finds KDevelop sessions matching :q:.")); 0031 #endif 0032 addSyntax(s); 0033 0034 addSyntax(Plasma::RunnerSyntax(QStringLiteral("kdevelop"), i18n("Lists all the KDevelop editor sessions in your account."))); 0035 } 0036 0037 KDevelopSessions::~KDevelopSessions() 0038 { 0039 KDevelopSessionsWatch::unregisterObserver(this); 0040 } 0041 0042 void KDevelopSessions::init() 0043 { 0044 KDevelopSessionsWatch::registerObserver(this); 0045 0046 Plasma::AbstractRunner::init(); 0047 } 0048 0049 void KDevelopSessions::setSessionDataList(const QVector<KDevelopSessionData>& sessionDataList) 0050 { 0051 m_sessionDataList = sessionDataList; 0052 } 0053 0054 void KDevelopSessions::match(Plasma::RunnerContext &context) 0055 { 0056 QString term = context.query(); 0057 if (term.size() < 3) { 0058 return; 0059 } 0060 0061 bool listAll = false; 0062 if (term.startsWith(QLatin1String("kdevelop"), Qt::CaseInsensitive)) { 0063 const QStringRef trimmedStrippedTerm = term.midRef(8).trimmed(); 0064 // "kdevelop" -> list all sessions 0065 if (trimmedStrippedTerm.isEmpty()) { 0066 listAll = true; 0067 term.clear(); 0068 } 0069 // "kdevelop X" -> list all sessions with "X" 0070 else if (term.at(8) == QLatin1Char(' ') ) { 0071 term = trimmedStrippedTerm.toString(); 0072 } 0073 // "kdevelopX" -> list all sessions with "kdevelopX" 0074 else { 0075 term = term.trimmed(); 0076 } 0077 } 0078 0079 if (term.isEmpty() && !listAll) { 0080 return; 0081 } 0082 0083 for (const auto& session : qAsConst(m_sessionDataList)) { 0084 if (!context.isValid()) { 0085 return; 0086 } 0087 0088 if (listAll || (!term.isEmpty() && session.description.contains(term, Qt::CaseInsensitive))) { 0089 Plasma::QueryMatch match(this); 0090 if (listAll) { 0091 // All sessions listed, but with a low priority 0092 match.setType(Plasma::QueryMatch::ExactMatch); 0093 match.setRelevance(0.8); 0094 } else { 0095 if (session.description.compare(term, Qt::CaseInsensitive) == 0) { 0096 // parameter to kdevelop matches session exactly, bump it up! 0097 match.setType(Plasma::QueryMatch::ExactMatch); 0098 match.setRelevance(1.0); 0099 } else { 0100 // fuzzy match of the session in "kdevelop $session" 0101 match.setType(Plasma::QueryMatch::PossibleMatch); 0102 match.setRelevance(0.8); 0103 } 0104 } 0105 match.setIconName(QStringLiteral("kdevelop")); 0106 match.setData(session.id); 0107 match.setText(session.description); 0108 match.setSubtext(i18n("Open KDevelop Session")); 0109 context.addMatch(match); 0110 } 0111 } 0112 } 0113 0114 void KDevelopSessions::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match) 0115 { 0116 Q_UNUSED(context) 0117 const QString sessionId = match.data().toString(); 0118 qDebug() << "Open KDevelop session" << sessionId; 0119 KDevelopSessionsWatch::openSession(sessionId); 0120 } 0121 0122 #include "kdevelopsessions.moc" 0123 #include "moc_kdevelopsessions.cpp"