Changeset View
Changeset View
Standalone View
Standalone View
runners/katesessions/katesessions.cpp
1 | /* | 1 | /* | ||
---|---|---|---|---|---|
2 | * Copyright 2008 Sebastian Kügler <sebas@kde.org> | 2 | * Copyright 2008 Sebastian Kügler <sebas@kde.org> | ||
3 | * Copyright 2017 Kai Uwe Broulik <kde@privat.broulik.de> | 3 | * Copyright 2017 Kai Uwe Broulik <kde@privat.broulik.de> | ||
4 | * Copyright 2020 Alexander Lohnau <alexander.lohnau@gmx.de> | ||||
4 | * | 5 | * | ||
5 | * This program is free software; you can redistribute it and/or modify | 6 | * This program is free software; you can redistribute it and/or modify | ||
6 | * it under the terms of the GNU Library General Public License as | 7 | * it under the terms of the GNU Library General Public License as | ||
7 | * published by the Free Software Foundation; either version 2, or | 8 | * published by the Free Software Foundation; either version 2, or | ||
8 | * (at your option) any later version. | 9 | * (at your option) any later version. | ||
9 | * | 10 | * | ||
10 | * This program is distributed in the hope that it will be useful, | 11 | * This program is distributed in the hope that it will be useful, | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
Show All 14 Lines | |||||
26 | #include <QStandardPaths> | 27 | #include <QStandardPaths> | ||
27 | 28 | | |||
28 | #include <KDirWatch> | 29 | #include <KDirWatch> | ||
29 | #include <KLocalizedString> | 30 | #include <KLocalizedString> | ||
30 | #include <KToolInvocation> | 31 | #include <KToolInvocation> | ||
31 | 32 | | |||
32 | K_EXPORT_PLASMA_RUNNER(katesessionsrunner, KateSessions) | 33 | K_EXPORT_PLASMA_RUNNER(katesessionsrunner, KateSessions) | ||
33 | 34 | | |||
34 | KateSessions::KateSessions(QObject *parent, const QVariantList& args) | 35 | KateSessions::KateSessions(QObject *parent, const QVariantList &args) | ||
35 | : Plasma::AbstractRunner(parent, args) | 36 | : Plasma::AbstractRunner(parent, args) | ||
36 | { | 37 | { | ||
37 | setObjectName(QLatin1String("Kate Sessions")); | 38 | setObjectName(QStringLiteral("Kate Sessions")); | ||
38 | setIgnoredTypes(Plasma::RunnerContext::File | Plasma::RunnerContext::Directory | Plasma::RunnerContext::NetworkLocation); | 39 | setIgnoredTypes(Plasma::RunnerContext::File | Plasma::RunnerContext::Directory | ||
40 | | Plasma::RunnerContext::NetworkLocation); | ||||
39 | 41 | | |||
40 | Plasma::RunnerSyntax s(QLatin1String(":q:"), i18n("Finds Kate sessions matching :q:.")); | 42 | Plasma::RunnerSyntax s(QStringLiteral("kate :q:"), i18n("Finds Kate sessions matching :q:.")); | ||
41 | s.addExampleQuery(QLatin1String("kate :q:")); | | |||
42 | addSyntax(s); | 43 | addSyntax(s); | ||
43 | 44 | setDefaultSyntax(Plasma::RunnerSyntax(QStringLiteral("kate"), | |||
44 | setDefaultSyntax(Plasma::RunnerSyntax(QLatin1String("kate"), i18n("Lists all the Kate editor sessions in your account."))); | 45 | i18n("Lists all the Kate editor sessions in your account."))); | ||
45 | 46 | | |||
46 | m_sessionsFolderPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) | 47 | m_sessionsFolderPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) | ||
47 | + QLatin1String("/kate/sessions"); | 48 | + QStringLiteral("/kate/sessions"); | ||
48 | | ||||
49 | connect(this, SIGNAL(prepare()), SLOT(slotPrepare())); | | |||
50 | connect(this, SIGNAL(teardown()), SLOT(slotTeardown())); | | |||
51 | } | | |||
52 | 49 | | |||
53 | KateSessions::~KateSessions() | 50 | // Initialize watchers and sessions | ||
54 | { | | |||
55 | } | | |||
56 | | ||||
57 | void KateSessions::slotPrepare() | | |||
58 | { | | |||
59 | loadSessions(); | | |||
60 | | ||||
61 | // listen for changes to the list of kate sessions | | |||
62 | if (!m_sessionWatch) { | | |||
63 | m_sessionWatch = new KDirWatch(this); | 51 | m_sessionWatch = new KDirWatch(this); | ||
64 | m_sessionWatch->addDir(m_sessionsFolderPath); | 52 | m_sessionWatch->addDir(m_sessionsFolderPath); | ||
65 | connect(m_sessionWatch, &KDirWatch::dirty, this, &KateSessions::loadSessions); | 53 | connect(m_sessionWatch, &KDirWatch::dirty, this, &KateSessions::loadSessions); | ||
66 | connect(m_sessionWatch, &KDirWatch::created, this, &KateSessions::loadSessions); | 54 | connect(m_sessionWatch, &KDirWatch::created, this, &KateSessions::loadSessions); | ||
67 | connect(m_sessionWatch, &KDirWatch::deleted, this, &KateSessions::loadSessions); | 55 | connect(m_sessionWatch, &KDirWatch::deleted, this, &KateSessions::loadSessions); | ||
68 | } | 56 | loadSessions(); | ||
69 | } | 57 | } | ||
70 | 58 | | |||
71 | void KateSessions::slotTeardown() | 59 | KateSessions::~KateSessions() | ||
72 | { | 60 | { | ||
73 | delete m_sessionWatch; | | |||
74 | m_sessionWatch = nullptr; | | |||
75 | m_sessions.clear(); | | |||
76 | } | 61 | } | ||
77 | 62 | | |||
78 | void KateSessions::loadSessions() | 63 | void KateSessions::loadSessions() | ||
79 | { | 64 | { | ||
80 | QStringList sessions; | 65 | QStringList sessions; | ||
66 | const QDir sessionsDir(m_sessionsFolderPath); | ||||
81 | 67 | | |||
82 | QDir sessionsDir(m_sessionsFolderPath); | 68 | const auto &sessionFiles = sessionsDir.entryInfoList( | ||
83 | 69 | {QStringLiteral("*.katesession")}, QDir::Files, QDir::Name); | |||
84 | const auto &sessionFiles = sessionsDir.entryInfoList({QStringLiteral("*.katesession")}, QDir::Files); | | |||
85 | | ||||
86 | for (const QFileInfo &sessionFile : sessionFiles) { | 70 | for (const QFileInfo &sessionFile : sessionFiles) { | ||
87 | const QString name = QUrl::fromPercentEncoding(sessionFile.baseName().toLocal8Bit()); // is this the right encoding? | 71 | sessions.append(QUrl::fromPercentEncoding(sessionFile.baseName().toLocal8Bit())); | ||
88 | sessions.append(name); | | |||
89 | } | 72 | } | ||
90 | 73 | | |||
91 | QCollator collator; | | |||
92 | collator.setCaseSensitivity(Qt::CaseInsensitive); | | |||
93 | std::sort(sessions.begin(), sessions.end(), [&collator](const QString &a, const QString &b) { | | |||
94 | return collator.compare(a, b) < 0; | | |||
95 | }); | | |||
96 | | ||||
97 | m_sessions = sessions; | 74 | m_sessions = sessions; | ||
98 | } | 75 | } | ||
99 | 76 | | |||
100 | void KateSessions::match(Plasma::RunnerContext &context) | 77 | void KateSessions::match(Plasma::RunnerContext &context) | ||
101 | { | 78 | { | ||
102 | if (m_sessions.isEmpty()) { | 79 | QString term = context.query(); | ||
80 | if (term.length() < 3 || m_sessions.isEmpty() || !context.isValid()) { | ||||
103 | return; | 81 | return; | ||
104 | } | 82 | } | ||
105 | 83 | // Kate writes sessions as desktop actions in the local .desktop file => | |||
106 | QString term = context.query(); | 84 | // they are already available from the "Applications" Runner and in the normal launcher | ||
107 | if (term.length() < 3) { | 85 | if (!term.startsWith(m_triggerWord, Qt::CaseInsensitive)) { | ||
108 | return; | 86 | return; | ||
109 | } | 87 | } | ||
110 | 88 | | |||
111 | bool listAll = false; | 89 | bool listAll = false; | ||
112 | 90 | if (term.trimmed().compare(m_triggerWord, Qt::CaseInsensitive) == 0) { | |||
113 | if (term.startsWith(QLatin1String("kate"), Qt::CaseInsensitive)) { | | |||
114 | if (term.trimmed().compare(QLatin1String("kate"), Qt::CaseInsensitive) == 0) { | | |||
115 | listAll = true; | 91 | listAll = true; | ||
116 | term.clear(); | 92 | term.clear(); | ||
117 | } else if (term.at(4) == QLatin1Char(' ') ) { | 93 | } else if (term.at(4) == QLatin1Char(' ')) { | ||
118 | term.remove(QLatin1String("kate"), Qt::CaseInsensitive); | 94 | term = term.remove(m_triggerWord, Qt::CaseInsensitive).trimmed(); | ||
119 | term = term.trimmed(); | | |||
120 | } else { | 95 | } else { | ||
121 | term.clear(); | 96 | // Prevent results for queries like "katee" | ||
122 | } | | |||
123 | } | | |||
124 | | ||||
125 | if (term.isEmpty() && !listAll) { | | |||
126 | return; | | |||
127 | } | | |||
128 | | ||||
129 | foreach (const QString &session, m_sessions) { | | |||
130 | if (!context.isValid()) { | | |||
131 | return; | 97 | return; | ||
132 | } | 98 | } | ||
133 | 99 | | |||
134 | if (listAll || (!term.isEmpty() && session.contains(term, Qt::CaseInsensitive))) { | 100 | for (const QString &session: qAsConst(m_sessions)) { | ||
101 | if (listAll || session.contains(term, Qt::CaseInsensitive)) { | ||||
135 | Plasma::QueryMatch match(this); | 102 | Plasma::QueryMatch match(this); | ||
136 | if (listAll) { | | |||
137 | // All sessions listed, but with a low priority | | |||
138 | match.setType(Plasma::QueryMatch::ExactMatch); | | |||
139 | match.setRelevance(0.8); | | |||
140 | } else { | | |||
141 | if (session.compare(term, Qt::CaseInsensitive) == 0) { | | |||
142 | // parameter to kate matches session exactly, bump it up! | | |||
143 | match.setType(Plasma::QueryMatch::ExactMatch); | 103 | match.setType(Plasma::QueryMatch::ExactMatch); | ||
144 | match.setRelevance(1.0); | 104 | match.setRelevance(session.compare(term, Qt::CaseInsensitive) == 0 ? 1 : 0.8); | ||
145 | } else { | 105 | match.setIconName(m_triggerWord); | ||
146 | // fuzzy match of the session in "kate $session" | | |||
147 | match.setType(Plasma::QueryMatch::PossibleMatch); | | |||
148 | match.setRelevance(0.8); | | |||
149 | } | | |||
150 | } | | |||
151 | match.setIconName(QStringLiteral("kate")); | | |||
152 | match.setData(session); | 106 | match.setData(session); | ||
153 | match.setText(session); | 107 | match.setText(session); | ||
154 | match.setSubtext(i18n("Open Kate Session")); | 108 | match.setSubtext(i18n("Open Kate Session")); | ||
155 | context.addMatch(match); | 109 | context.addMatch(match); | ||
156 | } | 110 | } | ||
157 | } | 111 | } | ||
158 | } | 112 | } | ||
159 | 113 | | |||
160 | void KateSessions::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match) | 114 | void KateSessions::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match) | ||
161 | { | 115 | { | ||
162 | Q_UNUSED(context) | 116 | Q_UNUSED(context) | ||
163 | QString session = match.data().toString(); | | |||
164 | 117 | | |||
165 | if (!session.isEmpty()) { | 118 | KToolInvocation::kdeinitExec(QStringLiteral("kate"), {QStringLiteral("--start"), | ||
166 | QStringList args; | 119 | match.data().toString(), QStringLiteral("-n")}); | ||
167 | args << QLatin1String("--start") << session << QLatin1String("-n"); | | |||
168 | KToolInvocation::kdeinitExec(QLatin1String("kate"), args); | | |||
169 | } | | |||
170 | } | 120 | } | ||
171 | 121 | | |||
172 | #include "katesessions.moc" | 122 | #include "katesessions.moc" |