Commit 323ac0ff authored by Alexander Lohnau's avatar Alexander Lohnau 💬
Browse files

Port kate sesseions runner to profiles model

While we need some extra tweaks in the model, most of the logic can be reused.
This will make porting it to QML easier.
parent c1930e05
......@@ -9,6 +9,7 @@
#include <KConfig>
#include <KConfigGroup>
#include <KDirWatch>
#include <KFileUtils>
// Qt
#include <KIO/CommandLauncherJob>
#include <QDebug>
......@@ -36,13 +37,12 @@ void ProfilesModel::init()
void ProfilesModel::loadProfiles()
{
QStringList profilesPaths;
const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, m_appName, QStandardPaths::LocateDirectory);
for (const auto &dir : dirs) {
const QStringList fileNames = QDir(dir).entryList({QStringLiteral("*.profile")});
for (const QString &fileName : fileNames) {
profilesPaths.append(dir + QLatin1Char('/') + fileName);
}
if (m_appName == QLatin1String("kate")) {
const QDir sessionsDir(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/kate/sessions"));
profilesPaths = sessionsDir.entryList({QStringLiteral("*.katesession")}, QDir::Files, QDir::Name);
} else {
const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, m_appName, QStandardPaths::LocateDirectory);
profilesPaths = KFileUtils::findAllUniqueFiles(dirs, {QStringLiteral("*.profile")});
}
beginResetModel();
......@@ -52,18 +52,14 @@ void ProfilesModel::loadProfiles()
const QString profileIdentifier = info.baseName();
QString niceName = profileIdentifier;
QString iconName = QStringLiteral("utilities-terminal");
KConfig cfg(profilePath, KConfig::SimpleConfig);
if (cfg.hasGroup("General")) {
if (m_appName == QLatin1String("konsole")) {
KConfig cfg(profilePath, KConfig::SimpleConfig);
KConfigGroup grp(&cfg, "General");
iconName = grp.readEntry("Icon", iconName);
if (grp.hasKey("Name")) {
niceName = grp.readEntry("Name");
}
m_data.append(ProfileData{niceName, profileIdentifier, iconName});
niceName = grp.readEntry("Name", niceName);
}
m_data.append(ProfileData{niceName, profileIdentifier, iconName});
}
endResetModel();
}
......@@ -98,6 +94,9 @@ void ProfilesModel::openProfile(const QString profileIdentifier)
if (m_appName == QLatin1String("konsole")) {
job = new KIO::CommandLauncherJob(m_appName, QStringList{QStringLiteral("--profile"), profileIdentifier});
job->setDesktopName(QStringLiteral("org.kde.konsole"));
} else if (m_appName == QLatin1String("kate")) {
job = new KIO::CommandLauncherJob(m_appName, {QStringLiteral("--start"), profileIdentifier, QStringLiteral("-n")});
job->setDesktopName(QStringLiteral("org.kde.kate"));
} else {
Q_UNREACHABLE();
}
......
add_definitions(-DTRANSLATION_DOMAIN=\"plasma_runner_katesessions\")
kcoreaddons_add_plugin(krunner_katesessions SOURCES katesessions.cpp INSTALL_NAMESPACE "kf${QT_MAJOR_VERSION}/krunner")
target_link_libraries(krunner_katesessions KF5::KIOGui KF5::Notifications KF5::I18n KF5::Runner)
target_link_libraries(krunner_katesessions KF5::KIOGui KF5::Notifications KF5::I18n KF5::Runner profiles_utility_static)
......@@ -30,27 +30,10 @@ KateSessions::KateSessions(QObject *parent, const KPluginMetaData &metaData, con
addSyntax(RunnerSyntax(QStringLiteral("kate :q:"), i18n("Finds Kate sessions matching :q:.")));
addSyntax(RunnerSyntax(QStringLiteral("kate"), i18n("Lists all the Kate editor sessions in your account.")));
m_sessionsFolderPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/kate/sessions");
// Initialize watchers and sessions
setTriggerWords({m_triggerWord});
}
KateSessions::~KateSessions()
{
}
QStringList KateSessions::loadSessions()
{
QStringList sessions;
const QDir sessionsDir(m_sessionsFolderPath);
const auto &sessionFiles = sessionsDir.entryInfoList({QStringLiteral("*.katesession")}, QDir::Files, QDir::Name);
for (const QFileInfo &sessionFile : sessionFiles) {
sessions.append(QUrl::fromPercentEncoding(sessionFile.baseName().toLocal8Bit()));
}
return sessions;
m_model.setAppName(m_triggerWord);
}
void KateSessions::match(RunnerContext &context)
......@@ -67,19 +50,13 @@ void KateSessions::match(RunnerContext &context)
return;
}
// we got here, load sessions now
const auto sessions = loadSessions();
if (sessions.isEmpty()) {
suspendMatching(true);
return;
}
QList<QueryMatch> matches;
int maxScore = 0;
for (const QString &session : std::as_const(sessions)) {
for (int i = 0, count = m_model.rowCount(); i < count; ++i) {
// Does the query match exactly?
// no query = perfect match => list everything
QString session = m_model.index(i).data(ProfilesModel::NameRole).toString();
if (listAll || session.compare(term, Qt::CaseInsensitive) == 0) {
QueryMatch match(this);
match.setType(QueryMatch::ExactMatch);
......@@ -118,14 +95,7 @@ void KateSessions::run(const RunnerContext &context, const QueryMatch &match)
{
Q_UNUSED(context)
auto *job = new KIO::CommandLauncherJob(QStringLiteral("kate"), {QStringLiteral("--start"), match.data().toString(), QStringLiteral("-n")});
job->setDesktopName(QStringLiteral("org.kde.kate"));
auto *delegate = new KNotificationJobUiDelegate;
delegate->setAutoErrorHandlingEnabled(true);
job->setUiDelegate(delegate);
job->start();
m_model.openProfile(match.data().toString());
}
#include "katesessions.moc"
......@@ -9,10 +9,9 @@
#ifndef KATESESSIONS_H
#define KATESESSIONS_H
#include "profilesmodel.h"
#include <KRunner/AbstractRunner>
class KDirWatch;
using namespace Plasma;
class KateSessions : public AbstractRunner
......@@ -21,17 +20,13 @@ class KateSessions : public AbstractRunner
public:
explicit KateSessions(QObject *parent, const KPluginMetaData &metaData, const QVariantList &args);
~KateSessions() override;
void match(RunnerContext &context) override;
void run(const RunnerContext &context, const QueryMatch &match) override;
private Q_SLOTS:
QStringList loadSessions();
private:
QString m_sessionsFolderPath;
const QLatin1String m_triggerWord = QLatin1String("kate");
ProfilesModel m_model;
};
#endif
......@@ -29,7 +29,6 @@ KonsoleProfiles::KonsoleProfiles(QObject *parent, const KPluginMetaData &metaDat
setMinLetterCount(3);
m_model.setAppName(m_triggerWord);
suspendMatching(m_model.rowCount() == 0);
}
void KonsoleProfiles::match(RunnerContext &context)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment