Commit c1930e05 authored by Alexander Lohnau's avatar Alexander Lohnau 💬
Browse files

Port konsoleprofiles runner to profiles model

parent 9ae537f8
......@@ -51,16 +51,18 @@ void ProfilesModel::loadProfiles()
QFileInfo info(profilePath);
const QString profileIdentifier = info.baseName();
QString niceName = profileIdentifier;
QString iconName = QStringLiteral("utilities-terminal");
KConfig cfg(profilePath, KConfig::SimpleConfig);
if (cfg.hasGroup("General")) {
KConfigGroup grp(&cfg, "General");
iconName = grp.readEntry("Icon", iconName);
if (grp.hasKey("Name")) {
niceName = grp.readEntry("Name");
}
m_data.append(ProfileData{niceName, profileIdentifier});
m_data.append(ProfileData{niceName, profileIdentifier, iconName});
}
}
endResetModel();
......@@ -71,6 +73,7 @@ QHash<int, QByteArray> ProfilesModel::roleNames() const
return {
{NameRole, "name"},
{ProfileIdentifierRole, "profileIdentifier"},
{IconNameRole, "iconName"},
};
}
......@@ -82,6 +85,8 @@ QVariant ProfilesModel::data(const QModelIndex &index, int role) const
return data.name;
case ProfileIdentifierRole:
return data.profileIdentifier;
case IconNameRole:
return data.iconName;
default:
return QVariant();
}
......@@ -92,6 +97,7 @@ void ProfilesModel::openProfile(const QString profileIdentifier)
KIO::CommandLauncherJob *job;
if (m_appName == QLatin1String("konsole")) {
job = new KIO::CommandLauncherJob(m_appName, QStringList{QStringLiteral("--profile"), profileIdentifier});
job->setDesktopName(QStringLiteral("org.kde.konsole"));
} else {
Q_UNREACHABLE();
}
......
......@@ -12,6 +12,7 @@ class KDirWatch;
struct ProfileData {
QString name;
QString profileIdentifier;
QString iconName;
};
class ProfilesModel : public QAbstractListModel
......@@ -19,14 +20,14 @@ class ProfilesModel : public QAbstractListModel
Q_OBJECT
Q_PROPERTY(QString appName READ appName WRITE setAppName NOTIFY appNameChanged)
public:
explicit ProfilesModel(QObject *parent = nullptr);
enum Roles {
NameRole = Qt::DisplayRole,
ProfileIdentifierRole = Qt::UserRole,
IconNameRole,
};
public:
explicit ProfilesModel(QObject *parent = nullptr);
QHash<int, QByteArray> roleNames() const override;
QVariant data(const QModelIndex &index, int role) const override;
Q_INVOKABLE void openProfile(const QString profileIdentifier);
......
......@@ -3,7 +3,5 @@ add_definitions(-DTRANSLATION_DOMAIN="plasma_runner_konsoleprofiles")
kcoreaddons_add_plugin(krunner_konsoleprofiles SOURCES konsoleprofiles.cpp INSTALL_NAMESPACE "kf${QT_MAJOR_VERSION}/krunner")
target_link_libraries(krunner_konsoleprofiles
KF5::Runner
KF5::KIOGui
KF5::I18n
KF5::Notifications
profiles_utility_static
)
......@@ -12,10 +12,7 @@
#include <KConfig>
#include <KConfigGroup>
#include <KDirWatch>
#include <KIO/CommandLauncherJob>
#include <KLocalizedString>
#include <KNotificationJobUiDelegate>
// Qt
#include <QDir>
#include <QFileInfo>
#include <QStandardPaths>
......@@ -29,87 +26,37 @@ KonsoleProfiles::KonsoleProfiles(QObject *parent, const KPluginMetaData &metaDat
s.addExampleQuery(QStringLiteral("konsole :q:"));
addSyntax(s);
addSyntax(RunnerSyntax(QStringLiteral("konsole"), i18n("Lists all the Konsole profiles in your account.")));
}
KonsoleProfiles::~KonsoleProfiles() = default;
void KonsoleProfiles::init()
{
// Initialize profiles and file watcher
m_profileFilesWatch = new KDirWatch(this);
const QStringList konsoleDataBaseDirs = QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation);
for (const QString &konsoleDataBaseDir : konsoleDataBaseDirs) {
m_profileFilesWatch->addDir(konsoleDataBaseDir + QStringLiteral("/konsole"));
}
connect(m_profileFilesWatch, &KDirWatch::dirty, this, &KonsoleProfiles::loadProfiles);
connect(m_profileFilesWatch, &KDirWatch::created, this, &KonsoleProfiles::loadProfiles);
connect(m_profileFilesWatch, &KDirWatch::deleted, this, &KonsoleProfiles::loadProfiles);
loadProfiles();
setMinLetterCount(3);
}
void KonsoleProfiles::loadProfiles()
{
m_profiles.clear();
QStringList profilesPaths;
const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("konsole"), 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);
}
}
for (const auto &profilePath : std::as_const(profilesPaths)) {
const QString profileName = QFileInfo(profilePath).baseName();
const KConfig _config(profilePath, KConfig::SimpleConfig);
if (_config.hasGroup("General")) {
KonsoleProfileData profileData;
const KConfigGroup cfg = _config.group("General");
profileData.displayName = cfg.readEntry("Name", profileName);
profileData.iconName = cfg.readEntry("Icon", QStringLiteral("utilities-terminal"));
if (!profileData.displayName.isEmpty()) {
m_profiles.append(profileData);
}
}
}
suspendMatching(m_profiles.isEmpty());
m_model.setAppName(m_triggerWord);
suspendMatching(m_model.rowCount() == 0);
}
void KonsoleProfiles::match(RunnerContext &context)
{
QString term = context.query();
term = term.remove(m_triggerWord).simplified();
for (const KonsoleProfileData &data : std::as_const(m_profiles)) {
if (data.displayName.contains(term, Qt::CaseInsensitive)) {
for (int i = 0, count = m_model.rowCount(); i < count; ++i) {
QModelIndex idx = m_model.index(i);
const QString name = idx.data(ProfilesModel::NameRole).toString();
if (name.contains(term, Qt::CaseInsensitive)) {
const QString profileIdentifier = idx.data(ProfilesModel::ProfileIdentifierRole).toString();
QueryMatch match(this);
match.setType(QueryMatch::PossibleMatch);
match.setIconName(data.iconName);
match.setData(data.displayName);
match.setText(QStringLiteral("Konsole: ") + data.displayName);
match.setRelevance((float)term.length() / (float)data.displayName.length());
match.setIconName(idx.data(ProfilesModel::IconNameRole).toString());
match.setData(profileIdentifier);
match.setText(QStringLiteral("Konsole: ") + name);
match.setRelevance((float)term.length() / (float)name.length());
context.addMatch(match);
}
}
}
void KonsoleProfiles::run(const RunnerContext &context, const QueryMatch &match)
{
Q_UNUSED(context)
const QString profile = match.data().toString();
auto *job = new KIO::CommandLauncherJob(QStringLiteral("konsole"), {QStringLiteral("--profile"), profile});
job->setDesktopName(QStringLiteral("org.kde.konsole"));
auto *delegate = new KNotificationJobUiDelegate;
delegate->setAutoErrorHandlingEnabled(true);
job->setUiDelegate(delegate);
job->start();
m_model.openProfile(profile);
}
K_PLUGIN_CLASS_WITH_JSON(KonsoleProfiles, "plasma-runner-konsoleprofiles.json")
......
......@@ -8,17 +8,9 @@
#ifndef KONSOLEPROFILES_H
#define KONSOLEPROFILES_H
#include "profilesmodel.h"
#include <KRunner/AbstractRunner>
class KDirWatch;
struct KonsoleProfileData {
QString displayName;
QString iconName;
};
Q_DECLARE_TYPEINFO(KonsoleProfileData, Q_MOVABLE_TYPE);
using namespace Plasma;
class KonsoleProfiles : public AbstractRunner
......@@ -27,19 +19,13 @@ class KonsoleProfiles : public AbstractRunner
public:
explicit KonsoleProfiles(QObject *parent, const KPluginMetaData &metaData, const QVariantList &args);
~KonsoleProfiles() override;
void init() override;
void match(RunnerContext &context) override;
void run(const RunnerContext &context, const QueryMatch &match) override;
private Q_SLOTS:
void loadProfiles();
private:
KDirWatch *m_profileFilesWatch = nullptr;
QList<KonsoleProfileData> m_profiles;
QLatin1String m_triggerWord = QLatin1String("konsole");
ProfilesModel m_model;
const QLatin1String m_triggerWord = QLatin1String("konsole");
};
#endif
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