Warning, file /plasma/kdeplasma-addons/runners/katesessions/katesessions.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* 0002 * SPDX-FileCopyrightText: 2008 Sebastian Kügler <sebas@kde.org> 0003 * SPDX-FileCopyrightText: 2017 Kai Uwe Broulik <kde@privat.broulik.de> 0004 * SPDX-FileCopyrightText: 2020 Alexander Lohnau <alexander.lohnau@gmx.de> 0005 * 0006 * SPDX-License-Identifier: LGPL-2.0-or-later 0007 */ 0008 0009 #include "katesessions.h" 0010 0011 #include <QCollator> 0012 #include <QDir> 0013 #include <QFileInfo> 0014 #include <QStandardPaths> 0015 0016 #include <KDirWatch> 0017 #include <KFuzzyMatcher> 0018 #include <KLocalizedString> 0019 #include <KNotificationJobUiDelegate> 0020 0021 #include <KIO/CommandLauncherJob> 0022 0023 K_PLUGIN_CLASS_WITH_JSON(KateSessions, "plasma-runner-katesessions.json") 0024 0025 KateSessions::KateSessions(QObject *parent, const KPluginMetaData &metaData, const QVariantList &args) 0026 : AbstractRunner(parent, metaData, args) 0027 { 0028 setObjectName(QStringLiteral("Kate Sessions")); 0029 0030 addSyntax(RunnerSyntax(QStringLiteral("kate :q:"), i18n("Finds Kate sessions matching :q:."))); 0031 addSyntax(RunnerSyntax(QStringLiteral("kate"), i18n("Lists all the Kate editor sessions in your account."))); 0032 0033 // Initialize watchers and sessions 0034 setTriggerWords({m_triggerWord}); 0035 } 0036 0037 void KateSessions::match(RunnerContext &context) 0038 { 0039 QString term = context.query(); 0040 bool listAll = false; 0041 if (term.trimmed().compare(m_triggerWord, Qt::CaseInsensitive) == 0) { 0042 listAll = true; 0043 term.clear(); 0044 } else if (term.at(4) == QLatin1Char(' ')) { 0045 term = term.remove(m_triggerWord, Qt::CaseInsensitive).trimmed(); 0046 } else { 0047 // Prevent results for queries like "katee" 0048 return; 0049 } 0050 0051 QList<QueryMatch> matches; 0052 int maxScore = 0; 0053 0054 for (int i = 0, count = m_model->rowCount(); i < count; ++i) { 0055 // Does the query match exactly? 0056 // no query = perfect match => list everything 0057 QString session = m_model->index(i).data(ProfilesModel::NameRole).toString(); 0058 if (listAll || session.compare(term, Qt::CaseInsensitive) == 0) { 0059 QueryMatch match(this); 0060 match.setType(QueryMatch::ExactMatch); 0061 match.setRelevance(session.compare(term, Qt::CaseInsensitive) == 0 ? 1 : 0.8); 0062 match.setIconName(m_triggerWord); 0063 match.setData(session); 0064 match.setText(session); 0065 match.setSubtext(i18n("Open Kate Session")); 0066 context.addMatch(match); 0067 } else { 0068 // Do fuzzy matching 0069 const auto res = KFuzzyMatcher::match(term, session); 0070 if (res.matched) { 0071 QueryMatch match(this); 0072 match.setRelevance(res.score); // store the score here for now 0073 match.setIconName(m_triggerWord); 0074 match.setData(session); 0075 match.setText(session); 0076 match.setSubtext(i18n("Open Kate Session")); 0077 matches.push_back(match); 0078 maxScore = std::max(res.score, maxScore); 0079 } 0080 } 0081 } 0082 0083 auto calculate_relevance = [maxScore](double score) { 0084 return score / maxScore; 0085 }; 0086 for (auto &match : matches) { 0087 match.setRelevance(calculate_relevance(match.relevance())); 0088 } 0089 context.addMatches(matches); 0090 } 0091 0092 void KateSessions::run(const RunnerContext &context, const QueryMatch &match) 0093 { 0094 Q_UNUSED(context) 0095 0096 m_model->openProfile(match.data().toString()); 0097 } 0098 0099 void KateSessions::init() 0100 { 0101 // Only create this in the correct thread. Inside we use KDirWatch which is thread sensitive. 0102 m_model = new ProfilesModel(this); 0103 m_model->setAppName(m_triggerWord); 0104 } 0105 0106 #include "katesessions.moc"