Kill Runner: Remove duplicate function calls and refactor

Summary:
Duplicate function calls to load the config in the runner/config have been removed.
The actions of the runner are also refactored, similar to https://phabricator.kde.org/D3688.
Also the user (owner of process) has been removed from the subtext, because the second line is not shown and adding the user info to the same line would be too long.

Test Plan: Should compile and kill processes as before.

Reviewers: davidedmundson, ngraham, broulik, meven, apol

Reviewed By: meven

Subscribers: apol, plasma-devel

Tags: #plasma

Differential Revision: https://phabricator.kde.org/D27513
wilder-portage-prov
Alexander Lohnau 6 years ago
parent 683aedfc01
commit 6f103ebd7b
  1. 13
      runners/kill/CMakeLists.txt
  2. 77
      runners/kill/killrunner.cpp
  3. 14
      runners/kill/killrunner.h
  4. 37
      runners/kill/killrunner_config.cpp
  5. 7
      runners/kill/killrunner_config.h

@ -1,7 +1,5 @@
add_definitions(-DTRANSLATION_DOMAIN=\"plasma_runner_kill\") add_definitions(-DTRANSLATION_DOMAIN=\"plasma_runner_kill\")
set(krunner_kill_SRCS killrunner.cpp)
set(kcm_krunner_kill_SRCS set(kcm_krunner_kill_SRCS
killrunner_config.cpp killrunner_config.cpp
) )
@ -18,8 +16,8 @@ target_link_libraries(kcm_krunner_kill
KF5::ConfigWidgets KF5::ConfigWidgets
KF5::Runner KF5::Runner
) )
add_library(krunner_kill MODULE ${krunner_kill_SRCS}) add_library(krunner_kill MODULE killrunner.cpp)
target_link_libraries(krunner_kill target_link_libraries(krunner_kill
KF5::I18n KF5::I18n
KF5::Completion KF5::Completion
@ -30,8 +28,5 @@ target_link_libraries(krunner_kill
) )
add_dependencies(krunner_kill kcm_krunner_kill) add_dependencies(krunner_kill kcm_krunner_kill)
install(TARGETS krunner_kill kcm_krunner_kill install(TARGETS krunner_kill kcm_krunner_kill DESTINATION ${KDE_INSTALL_PLUGINDIR})
DESTINATION ${KDE_INSTALL_PLUGINDIR}) install(FILES plasma-runner-kill.desktop plasma-runner-kill_config.desktop DESTINATION ${KDE_INSTALL_KSERVICES5DIR})
install(FILES plasma-runner-kill.desktop plasma-runner-kill_config.desktop
DESTINATION ${KDE_INSTALL_KSERVICES5DIR})

@ -1,4 +1,5 @@
/* Copyright 2009 Jan Gerrit Marker <jangerrit@weiler-marker.com> /* Copyright 2009 Jan Gerrit Marker <jangerrit@weiler-marker.com>
* Copyright 2020 Alexander Lohnau <alexander.lohnau@gmx.de>
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
@ -24,22 +25,22 @@
#include <QIcon> #include <QIcon>
#include <KProcess> #include <KProcess>
#include <KUser> #include <KAuth>
#include <kauth.h>
#include <KLocalizedString> #include <KLocalizedString>
#include "processcore/processes.h" #include <processcore/processes.h>
#include "processcore/process.h" #include <processcore/process.h>
K_EXPORT_PLASMA_RUNNER(kill, KillRunner) K_EXPORT_PLASMA_RUNNER(kill, KillRunner)
KillRunner::KillRunner(QObject *parent, const QVariantList& args) KillRunner::KillRunner(QObject *parent, const QVariantList &args)
: Plasma::AbstractRunner(parent, args), : Plasma::AbstractRunner(parent, args), m_processes(nullptr)
m_processes(nullptr)
{ {
Q_UNUSED(args); setObjectName(QStringLiteral("Kill Runner"));
setObjectName( QLatin1String("Kill Runner") );
reloadConfiguration(); addAction(QStringLiteral("SIGTERM"), QIcon::fromTheme(QStringLiteral("application-exit")), i18n("Send SIGTERM"))->setData(15);
addAction(QStringLiteral("SIGKILL"), QIcon::fromTheme(QStringLiteral("process-stop")), i18n("Send SIGKILL"))->setData(9);
m_actionList = {action(QStringLiteral("SIGTERM")), action(QStringLiteral("SIGKILL"))};
connect(this, &Plasma::AbstractRunner::prepare, this, &KillRunner::prep); connect(this, &Plasma::AbstractRunner::prepare, this, &KillRunner::prep);
connect(this, &Plasma::AbstractRunner::teardown, this, &KillRunner::cleanup); connect(this, &Plasma::AbstractRunner::teardown, this, &KillRunner::cleanup);
@ -49,9 +50,7 @@ KillRunner::KillRunner(QObject *parent, const QVariantList& args)
connect(&m_delayedCleanupTimer, &QTimer::timeout, this, &KillRunner::cleanup); connect(&m_delayedCleanupTimer, &QTimer::timeout, this, &KillRunner::cleanup);
} }
KillRunner::~KillRunner() KillRunner::~KillRunner() = default;
{
}
void KillRunner::reloadConfiguration() void KillRunner::reloadConfiguration()
@ -61,6 +60,7 @@ void KillRunner::reloadConfiguration()
if (grp.readEntry(CONFIG_USE_TRIGGERWORD, true)) { if (grp.readEntry(CONFIG_USE_TRIGGERWORD, true)) {
m_triggerWord = grp.readEntry(CONFIG_TRIGGERWORD, i18n("kill")) + QLatin1Char(' '); m_triggerWord = grp.readEntry(CONFIG_TRIGGERWORD, i18n("kill")) + QLatin1Char(' ');
} }
m_hasTrigger = !m_triggerWord.isEmpty();
m_sorting = (Sort) grp.readEntry(CONFIG_SORTING, static_cast<int>(Sort::NONE)); m_sorting = (Sort) grp.readEntry(CONFIG_SORTING, static_cast<int>(Sort::NONE));
QList<Plasma::RunnerSyntax> syntaxes; QList<Plasma::RunnerSyntax> syntaxes;
@ -93,8 +93,7 @@ void KillRunner::cleanup()
void KillRunner::match(Plasma::RunnerContext &context) void KillRunner::match(Plasma::RunnerContext &context)
{ {
QString term = context.query(); QString term = context.query();
const bool hasTrigger = !m_triggerWord.isEmpty(); if (m_hasTrigger && !term.startsWith(m_triggerWord, Qt::CaseInsensitive)) {
if (hasTrigger && !term.startsWith(m_triggerWord, Qt::CaseInsensitive)) {
return; return;
} }
@ -123,25 +122,17 @@ void KillRunner::match(Plasma::RunnerContext &context)
if (!context.isValid()) { if (!context.isValid()) {
return; return;
} }
const QString name = process->name(); const QString name = process->name();
if (!name.contains(term, Qt::CaseInsensitive)) { if (!name.contains(term, Qt::CaseInsensitive)) {
//Process doesn't match the search term
continue; continue;
} }
const quint64 pid = process->pid(); const quint64 pid = process->pid();
const qlonglong uid = process->uid();
const QString user = getUserName(uid);
QVariantList data;
data << pid << user;
Plasma::QueryMatch match(this); Plasma::QueryMatch match(this);
match.setText(i18n("Terminate %1", name)); match.setText(i18n("Terminate %1", name));
match.setSubtext(i18n("Process ID: %1\nRunning as user: %2", QString::number(pid), user)); match.setSubtext(i18n("Process ID: %1", QString::number(pid)));
match.setIconName(QStringLiteral("application-exit")); match.setIconName(QStringLiteral("application-exit"));
match.setData(data); match.setData(pid);
match.setId(name); match.setId(name);
// Set the relevance // Set the relevance
@ -160,7 +151,6 @@ void KillRunner::match(Plasma::RunnerContext &context)
matches << match; matches << match;
} }
qDebug() << "match count is" << matches.count();
context.addMatches(matches); context.addMatches(matches);
} }
@ -168,24 +158,18 @@ void KillRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMa
{ {
Q_UNUSED(context) Q_UNUSED(context)
QVariantList data = match.data().value<QVariantList>(); const quint64 pid = match.data().toUInt();
quint64 pid = data[0].toUInt();
// QString user = data[1].toString();
int signal; int signal;
if (match.selectedAction() != nullptr) { if (match.selectedAction()) {
signal = match.selectedAction()->data().toInt(); signal = match.selectedAction()->data().toInt();
} else { } else {
signal = 9; //default: SIGKILL signal = 9; //default: SIGKILL
} }
QStringList args; const QStringList args = {QStringLiteral("-%1").arg(signal), QString::number(pid)};
args << QStringLiteral("-%1").arg(signal) << QStringLiteral("%1").arg(pid); int returnCode = KProcess::execute(QStringLiteral("kill"), args);
KProcess process; if (returnCode == 0) {
int returnCode = process.execute(QStringLiteral("kill"), args);
if (returnCode == 0)
{
return; return;
} }
@ -201,24 +185,7 @@ QList<QAction*> KillRunner::actionsForMatch(const Plasma::QueryMatch &match)
{ {
Q_UNUSED(match) Q_UNUSED(match)
QList<QAction*> ret; return m_actionList;
if (!action(QStringLiteral("SIGTERM"))) {
(addAction(QStringLiteral("SIGTERM"), QIcon::fromTheme(QStringLiteral("application-exit")), i18n("Send SIGTERM")))->setData(15);
(addAction(QStringLiteral("SIGKILL"), QIcon::fromTheme(QStringLiteral("process-stop")), i18n("Send SIGKILL")))->setData(9);
}
ret << action(QStringLiteral("SIGTERM")) << action(QStringLiteral("SIGKILL"));
return ret;
}
QString KillRunner::getUserName(qlonglong uid)
{
KUser user(uid);
if (user.isValid()) {
return user.loginName();
}
qDebug() << QStringLiteral("No user with UID %1 was found").arg(uid);
return QStringLiteral("root");//No user with UID uid was found, so root is used
} }
#include "killrunner.moc" #include "killrunner.moc"

@ -1,4 +1,5 @@
/* Copyright 2009 <Jan Gerrit Marker> <jangerrit@weiler-marker.com> /* Copyright 2009 <Jan Gerrit Marker> <jangerrit@weiler-marker.com>
* Copyright 2020 <Alexander Lohnau> <alexander.lohnau@gmx.de>
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
@ -39,7 +40,7 @@ class KillRunner : public Plasma::AbstractRunner
Q_OBJECT Q_OBJECT
public: public:
KillRunner(QObject *parent, const QVariantList& args); KillRunner(QObject *parent, const QVariantList &args);
~KillRunner() override; ~KillRunner() override;
void match(Plasma::RunnerContext &context) override; void match(Plasma::RunnerContext &context) override;
@ -52,11 +53,6 @@ private Q_SLOTS:
void cleanup(); void cleanup();
private: private:
/** @param uid the uid of the user
* @return the username of the user with the UID uid
*/
QString getUserName(qlonglong uid);
/** The trigger word */ /** The trigger word */
QString m_triggerWord; QString m_triggerWord;
@ -71,6 +67,12 @@ private:
/** timer for retrying the cleanup due to lock contention */ /** timer for retrying the cleanup due to lock contention */
QTimer m_delayedCleanupTimer; QTimer m_delayedCleanupTimer;
/** Reuse actions */
QList<QAction *> m_actionList;
/** Reuse value */
bool m_hasTrigger;
}; };
#endif #endif

@ -1,4 +1,5 @@
/* Copyright 2009 <Jan Gerrit Marker> <jangerrit@weiler-marker.com> /* Copyright 2009 <Jan Gerrit Marker> <jangerrit@weiler-marker.com>
* Copyright 2020 <Alexander Lohnau> <alexander.lohnau@gmx.de>
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
@ -19,40 +20,34 @@
//Project-Includes //Project-Includes
#include "killrunner_config.h" #include "killrunner_config.h"
#include <kconfigwidgets_version.h>
//KDE-Includes //KDE-Includes
#include <KSharedConfig> #include <KSharedConfig>
#include <KConfigGroup>
#include <KPluginFactory> #include <KPluginFactory>
#include <krunner/abstractrunner.h>
#include "config_keys.h" #include "config_keys.h"
K_PLUGIN_FACTORY(KillRunnerConfigFactory, registerPlugin<KillRunnerConfig>(QStringLiteral("kcm_krunner_kill"));) K_PLUGIN_FACTORY(KillRunnerConfigFactory, registerPlugin<KillRunnerConfig>(QStringLiteral("kcm_krunner_kill"));)
KillRunnerConfigForm::KillRunnerConfigForm(QWidget* parent) : QWidget(parent) KillRunnerConfigForm::KillRunnerConfigForm(QWidget *parent) : QWidget(parent)
{ {
setupUi(this); setupUi(this);
} }
KillRunnerConfig::KillRunnerConfig(QWidget* parent, const QVariantList& args) : KillRunnerConfig::KillRunnerConfig(QWidget *parent, const QVariantList &args)
KCModule(parent, args) : KCModule(parent, args)
{ {
m_ui = new KillRunnerConfigForm(this); m_ui = new KillRunnerConfigForm(this);
QGridLayout* layout = new QGridLayout(this); QGridLayout *layout = new QGridLayout(this);
layout->addWidget(m_ui, 0, 0); layout->addWidget(m_ui, 0, 0);
m_ui->sorting->addItem(i18n("CPU usage"), CPU); m_ui->sorting->addItem(i18n("CPU usage"), CPU);
m_ui->sorting->addItem(i18n("inverted CPU usage"), CPUI); m_ui->sorting->addItem(i18n("inverted CPU usage"), CPUI);
m_ui->sorting->addItem(i18n("nothing"), NONE); m_ui->sorting->addItem(i18n("nothing"), NONE);
#if KCONFIGWIDGETS_VERSION < QT_VERSION_CHECK(5, 64, 0)
connect(m_ui->useTriggerWord, &QCheckBox::stateChanged, this, QOverload<>::of(&KillRunnerConfig::changed));
connect(m_ui->triggerWord, &KLineEdit::textChanged, this, QOverload<>::of(&KillRunnerConfig::changed));
connect(m_ui->sorting, QOverload<int>::of(&QComboBox::currentIndexChanged), this, QOverload<>::of(&KillRunnerConfig::changed));
#else
connect(m_ui->useTriggerWord, &QCheckBox::stateChanged, this, &KillRunnerConfig::markAsChanged); connect(m_ui->useTriggerWord, &QCheckBox::stateChanged, this, &KillRunnerConfig::markAsChanged);
connect(m_ui->triggerWord, &KLineEdit::textChanged, this, &KillRunnerConfig::markAsChanged); connect(m_ui->triggerWord, &KLineEdit::textChanged, this, &KillRunnerConfig::markAsChanged);
connect(m_ui->sorting, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &KillRunnerConfig::markAsChanged); connect(m_ui->sorting, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &KillRunnerConfig::markAsChanged);
#endif
load(); load();
} }
@ -62,12 +57,11 @@ void KillRunnerConfig::load()
KCModule::load(); KCModule::load();
KSharedConfig::Ptr cfg = KSharedConfig::openConfig(QStringLiteral("krunnerrc")); KSharedConfig::Ptr cfg = KSharedConfig::openConfig(QStringLiteral("krunnerrc"));
KConfigGroup grp = cfg->group("Runners"); const KConfigGroup grp = cfg->group("Runners").group("Kill Runner");
grp = KConfigGroup(&grp, "Kill Runner");
m_ui->useTriggerWord->setChecked(grp.readEntry(CONFIG_USE_TRIGGERWORD,true)); m_ui->useTriggerWord->setChecked(grp.readEntry(CONFIG_USE_TRIGGERWORD,true));
m_ui->triggerWord->setText(grp.readEntry(CONFIG_TRIGGERWORD,i18n("kill"))); m_ui->triggerWord->setText(grp.readEntry(CONFIG_TRIGGERWORD, i18n("kill")));
m_ui->sorting->setCurrentIndex(m_ui->sorting->findData(grp.readEntry<int>(CONFIG_SORTING, static_cast<int>(NONE)))); m_ui->sorting->setCurrentIndex(m_ui->sorting->findData(grp.readEntry<int>(CONFIG_SORTING, NONE)));
emit changed(false); emit changed(false);
} }
@ -77,12 +71,11 @@ void KillRunnerConfig::save()
KCModule::save(); KCModule::save();
KSharedConfig::Ptr cfg = KSharedConfig::openConfig(QStringLiteral("krunnerrc")); KSharedConfig::Ptr cfg = KSharedConfig::openConfig(QStringLiteral("krunnerrc"));
KConfigGroup grp = cfg->group("Runners"); KConfigGroup grp = cfg->group("Runners").group("Kill Runner");
grp = KConfigGroup(&grp, "Kill Runner");
grp.writeEntry(CONFIG_USE_TRIGGERWORD,m_ui->useTriggerWord->isChecked()); grp.writeEntry(CONFIG_USE_TRIGGERWORD, m_ui->useTriggerWord->isChecked());
grp.writeEntry(CONFIG_TRIGGERWORD,m_ui->triggerWord->text()); grp.writeEntry(CONFIG_TRIGGERWORD, m_ui->triggerWord->text());
grp.writeEntry(CONFIG_SORTING,m_ui->sorting->itemData(m_ui->sorting->currentIndex())); grp.writeEntry(CONFIG_SORTING, m_ui->sorting->itemData(m_ui->sorting->currentIndex()));
emit changed(false); emit changed(false);
} }
@ -95,7 +88,7 @@ void KillRunnerConfig::defaults()
m_ui->triggerWord->setText(i18n("kill")); m_ui->triggerWord->setText(i18n("kill"));
m_ui->sorting->setCurrentIndex(m_ui->sorting->findData((int) NONE)); m_ui->sorting->setCurrentIndex(m_ui->sorting->findData((int) NONE));
emit changed(true); emit markAsChanged();
} }
#include "killrunner_config.moc" #include "killrunner_config.moc"

@ -1,4 +1,5 @@
/* Copyright 2009 <Jan Gerrit Marker> <jangerrit@weiler-marker.com> /* Copyright 2009 <Jan Gerrit Marker> <jangerrit@weiler-marker.com>
* Copyright 2020 <Alexander Lohnau> <alexander.lohnau@gmx.de>
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
@ -31,7 +32,7 @@ class KillRunnerConfigForm : public QWidget, public Ui::KillRunnerConfigUi
Q_OBJECT Q_OBJECT
public: public:
explicit KillRunnerConfigForm(QWidget* parent); explicit KillRunnerConfigForm(QWidget *parent);
}; };
class KillRunnerConfig : public KCModule class KillRunnerConfig : public KCModule
@ -39,7 +40,7 @@ class KillRunnerConfig : public KCModule
Q_OBJECT Q_OBJECT
public: public:
explicit KillRunnerConfig(QWidget* parent = nullptr, const QVariantList& args = QVariantList()); explicit KillRunnerConfig(QWidget *parent = nullptr, const QVariantList &args = QVariantList());
public Q_SLOTS: public Q_SLOTS:
void save() override; void save() override;
@ -47,6 +48,6 @@ public Q_SLOTS:
void defaults() override; void defaults() override;
private: private:
KillRunnerConfigForm* m_ui; KillRunnerConfigForm *m_ui;
}; };
#endif #endif

Loading…
Cancel
Save