diff --git a/runners/katesessions/CMakeLists.txt b/runners/katesessions/CMakeLists.txt --- a/runners/katesessions/CMakeLists.txt +++ b/runners/katesessions/CMakeLists.txt @@ -1,11 +1,8 @@ add_definitions(-DTRANSLATION_DOMAIN=\"plasma_runner_katesessions\") -set(krunner_katesessions_SRCS - katesessions.cpp -) - - +set(krunner_katesessions_SRCS katesessions.cpp) add_library(krunner_katesessions MODULE ${krunner_katesessions_SRCS}) target_link_libraries(krunner_katesessions KF5::KIOCore KF5::I18n KF5::Runner) + install(TARGETS krunner_katesessions DESTINATION ${KDE_INSTALL_PLUGINDIR}) install(FILES plasma-runner-katesessions.desktop DESTINATION ${KDE_INSTALL_KSERVICES5DIR}) diff --git a/runners/katesessions/katesessions.h b/runners/katesessions/katesessions.h --- a/runners/katesessions/katesessions.h +++ b/runners/katesessions/katesessions.h @@ -1,6 +1,7 @@ /* * Copyright 2008 Sebastian Kügler * Copyright 2017 Kai Uwe Broulik + * Copyright 2020 Alexander Lohnau * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Library General Public License as @@ -21,29 +22,28 @@ #ifndef KATESESSIONS_H #define KATESESSIONS_H -#include +#include class KDirWatch; class KateSessions : public Plasma::AbstractRunner { Q_OBJECT public: - explicit KateSessions( QObject *parent, const QVariantList& args ); + explicit KateSessions(QObject *parent, const QVariantList& args); ~KateSessions() override; void match(Plasma::RunnerContext &context) override; void run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match) override; private Q_SLOTS: void loadSessions(); - void slotPrepare(); - void slotTeardown(); private: KDirWatch* m_sessionWatch = nullptr; QString m_sessionsFolderPath; QStringList m_sessions; + const QLatin1String m_triggerWord = QLatin1String("kate"); }; #endif diff --git a/runners/katesessions/katesessions.cpp b/runners/katesessions/katesessions.cpp --- a/runners/katesessions/katesessions.cpp +++ b/runners/katesessions/katesessions.cpp @@ -1,6 +1,7 @@ /* * Copyright 2008 Sebastian Kügler * Copyright 2017 Kai Uwe Broulik + * Copyright 2020 Alexander Lohnau * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Library General Public License as @@ -31,124 +32,76 @@ K_EXPORT_PLASMA_RUNNER(katesessionsrunner, KateSessions) -KateSessions::KateSessions(QObject *parent, const QVariantList& args) +KateSessions::KateSessions(QObject *parent, const QVariantList &args) : Plasma::AbstractRunner(parent, args) { - setObjectName(QLatin1String("Kate Sessions")); - setIgnoredTypes(Plasma::RunnerContext::File | Plasma::RunnerContext::Directory | Plasma::RunnerContext::NetworkLocation); + setObjectName(QStringLiteral("Kate Sessions")); + setIgnoredTypes(Plasma::RunnerContext::File | Plasma::RunnerContext::Directory + | Plasma::RunnerContext::NetworkLocation); - Plasma::RunnerSyntax s(QLatin1String(":q:"), i18n("Finds Kate sessions matching :q:.")); - s.addExampleQuery(QLatin1String("kate :q:")); + Plasma::RunnerSyntax s(QStringLiteral("kate :q:"), i18n("Finds Kate sessions matching :q:.")); addSyntax(s); - - setDefaultSyntax(Plasma::RunnerSyntax(QLatin1String("kate"), i18n("Lists all the Kate editor sessions in your account."))); + setDefaultSyntax(Plasma::RunnerSyntax(QStringLiteral("kate"), + i18n("Lists all the Kate editor sessions in your account."))); m_sessionsFolderPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) - + QLatin1String("/kate/sessions"); - - connect(this, SIGNAL(prepare()), SLOT(slotPrepare())); - connect(this, SIGNAL(teardown()), SLOT(slotTeardown())); -} - -KateSessions::~KateSessions() -{ -} - -void KateSessions::slotPrepare() -{ + + QStringLiteral("/kate/sessions"); + + // Initialize watchers and sessions + m_sessionWatch = new KDirWatch(this); + m_sessionWatch->addDir(m_sessionsFolderPath); + connect(m_sessionWatch, &KDirWatch::dirty, this, &KateSessions::loadSessions); + connect(m_sessionWatch, &KDirWatch::created, this, &KateSessions::loadSessions); + connect(m_sessionWatch, &KDirWatch::deleted, this, &KateSessions::loadSessions); loadSessions(); - - // listen for changes to the list of kate sessions - if (!m_sessionWatch) { - m_sessionWatch = new KDirWatch(this); - m_sessionWatch->addDir(m_sessionsFolderPath); - connect(m_sessionWatch, &KDirWatch::dirty, this, &KateSessions::loadSessions); - connect(m_sessionWatch, &KDirWatch::created, this, &KateSessions::loadSessions); - connect(m_sessionWatch, &KDirWatch::deleted, this, &KateSessions::loadSessions); - } } -void KateSessions::slotTeardown() +KateSessions::~KateSessions() { - delete m_sessionWatch; - m_sessionWatch = nullptr; - m_sessions.clear(); } void KateSessions::loadSessions() { QStringList sessions; + const QDir sessionsDir(m_sessionsFolderPath); - QDir sessionsDir(m_sessionsFolderPath); - - const auto &sessionFiles = sessionsDir.entryInfoList({QStringLiteral("*.katesession")}, QDir::Files); - + const auto &sessionFiles = sessionsDir.entryInfoList({QStringLiteral("*.katesession")}, QDir::Files, QDir::Name); for (const QFileInfo &sessionFile : sessionFiles) { - const QString name = QUrl::fromPercentEncoding(sessionFile.baseName().toLocal8Bit()); // is this the right encoding? - sessions.append(name); + sessions.append(QUrl::fromPercentEncoding(sessionFile.baseName().toLocal8Bit())); } - QCollator collator; - collator.setCaseSensitivity(Qt::CaseInsensitive); - std::sort(sessions.begin(), sessions.end(), [&collator](const QString &a, const QString &b) { - return collator.compare(a, b) < 0; - }); - m_sessions = sessions; } void KateSessions::match(Plasma::RunnerContext &context) { - if (m_sessions.isEmpty()) { + QString term = context.query(); + if (term.length() < 3 || m_sessions.isEmpty() || !context.isValid()) { return; } - - QString term = context.query(); - if (term.length() < 3) { + // Kate writes sessions as desktop actions in the local .desktop file => + // they are already available from the "Applications" Runner and in the normal launcher + if (!term.startsWith(m_triggerWord, Qt::CaseInsensitive)) { return; } bool listAll = false; - - if (term.startsWith(QLatin1String("kate"), Qt::CaseInsensitive)) { - if (term.trimmed().compare(QLatin1String("kate"), Qt::CaseInsensitive) == 0) { - listAll = true; - term.clear(); - } else if (term.at(4) == QLatin1Char(' ') ) { - term.remove(QLatin1String("kate"), Qt::CaseInsensitive); - term = term.trimmed(); - } else { - term.clear(); - } - } - - if (term.isEmpty() && !listAll) { + if (term.trimmed().compare(m_triggerWord, Qt::CaseInsensitive) == 0) { + listAll = true; + term.clear(); + } else if (term.at(4) == QLatin1Char(' ')) { + term = term.remove(m_triggerWord, Qt::CaseInsensitive).trimmed(); + } else { + // Prevent results for queries like "katee" return; } - foreach (const QString &session, m_sessions) { - if (!context.isValid()) { - return; - } - - if (listAll || (!term.isEmpty() && session.contains(term, Qt::CaseInsensitive))) { + for (const QString &session: qAsConst(m_sessions)) { + if (listAll || session.contains(term, Qt::CaseInsensitive)) { Plasma::QueryMatch match(this); - if (listAll) { - // All sessions listed, but with a low priority - match.setType(Plasma::QueryMatch::ExactMatch); - match.setRelevance(0.8); - } else { - if (session.compare(term, Qt::CaseInsensitive) == 0) { - // parameter to kate matches session exactly, bump it up! - match.setType(Plasma::QueryMatch::ExactMatch); - match.setRelevance(1.0); - } else { - // fuzzy match of the session in "kate $session" - match.setType(Plasma::QueryMatch::PossibleMatch); - match.setRelevance(0.8); - } - } - match.setIconName(QStringLiteral("kate")); + match.setType(Plasma::QueryMatch::ExactMatch); + match.setRelevance(session.compare(term, Qt::CaseInsensitive) == 0 ? 1 : 0.8); + match.setIconName(m_triggerWord); match.setData(session); match.setText(session); match.setSubtext(i18n("Open Kate Session")); @@ -160,13 +113,9 @@ void KateSessions::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match) { Q_UNUSED(context) - QString session = match.data().toString(); - if (!session.isEmpty()) { - QStringList args; - args << QLatin1String("--start") << session << QLatin1String("-n"); - KToolInvocation::kdeinitExec(QLatin1String("kate"), args); - } + KToolInvocation::kdeinitExec(QStringLiteral("kate"), {QStringLiteral("--start"), + match.data().toString(), QStringLiteral("-n")}); } #include "katesessions.moc"