diff --git a/runners/kill/CMakeLists.txt b/runners/kill/CMakeLists.txt --- a/runners/kill/CMakeLists.txt +++ b/runners/kill/CMakeLists.txt @@ -1,7 +1,5 @@ add_definitions(-DTRANSLATION_DOMAIN=\"plasma_runner_kill\") -set(krunner_kill_SRCS killrunner.cpp) - set(kcm_krunner_kill_SRCS killrunner_config.cpp ) @@ -18,8 +16,8 @@ KF5::ConfigWidgets KF5::Runner ) - -add_library(krunner_kill MODULE ${krunner_kill_SRCS}) + +add_library(krunner_kill MODULE killrunner.cpp) target_link_libraries(krunner_kill KF5::I18n KF5::Completion @@ -30,8 +28,5 @@ ) add_dependencies(krunner_kill kcm_krunner_kill) -install(TARGETS krunner_kill kcm_krunner_kill - DESTINATION ${KDE_INSTALL_PLUGINDIR}) - -install(FILES plasma-runner-kill.desktop plasma-runner-kill_config.desktop - DESTINATION ${KDE_INSTALL_KSERVICES5DIR}) +install(TARGETS krunner_kill kcm_krunner_kill DESTINATION ${KDE_INSTALL_PLUGINDIR}) +install(FILES plasma-runner-kill.desktop plasma-runner-kill_config.desktop DESTINATION ${KDE_INSTALL_KSERVICES5DIR}) diff --git a/runners/kill/killrunner.h b/runners/kill/killrunner.h --- a/runners/kill/killrunner.h +++ b/runners/kill/killrunner.h @@ -1,4 +1,5 @@ /* Copyright 2009 + * Copyright 2020 * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -52,11 +53,6 @@ void cleanup(); 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 */ QString m_triggerWord; @@ -71,6 +67,12 @@ /** timer for retrying the cleanup due to lock contention */ QTimer m_delayedCleanupTimer; + + /** Reuse actions */ + QList m_actionList; + + /** Reuse value */ + bool m_hasTrigger; }; #endif diff --git a/runners/kill/killrunner.cpp b/runners/kill/killrunner.cpp --- a/runners/kill/killrunner.cpp +++ b/runners/kill/killrunner.cpp @@ -1,4 +1,5 @@ /* Copyright 2009 Jan Gerrit Marker + * Copyright 2020 Alexander Lohnau * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -25,22 +26,23 @@ #include #include -#include +#include -#include "processcore/processes.h" -#include "processcore/process.h" +#include +#include #include "killrunner_config.h" K_EXPORT_PLASMA_RUNNER(kill, KillRunner) KillRunner::KillRunner(QObject *parent, const QVariantList& args) - : Plasma::AbstractRunner(parent, args), - m_processes(nullptr) + : Plasma::AbstractRunner(parent, args), m_processes(nullptr) { - Q_UNUSED(args); - setObjectName( QLatin1String("Kill Runner") ); - reloadConfiguration(); + setObjectName(QStringLiteral("Kill Runner")); + + 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::teardown, this, &KillRunner::cleanup); @@ -62,6 +64,7 @@ if (grp.readEntry(CONFIG_USE_TRIGGERWORD, true)) { m_triggerWord = grp.readEntry(CONFIG_TRIGGERWORD, i18n("kill")) + QLatin1Char(' '); } + m_hasTrigger = !m_triggerWord.isEmpty(); m_sorting = (KillRunnerConfig::Sort) grp.readEntry(CONFIG_SORTING, 0); QList syntaxes; @@ -94,8 +97,7 @@ void KillRunner::match(Plasma::RunnerContext &context) { QString term = context.query(); - const bool hasTrigger = !m_triggerWord.isEmpty(); - if (hasTrigger && !term.startsWith(m_triggerWord, Qt::CaseInsensitive)) { + if (m_hasTrigger && !term.startsWith(m_triggerWord, Qt::CaseInsensitive)) { return; } @@ -114,35 +116,25 @@ term = term.right(term.length() - m_triggerWord.length()); - if (term.length() < 2) { + if (term.length() < 2 || !context.isValid()) { return; } QList matches; const QList processlist = m_processes->getAllProcesses(); for (const KSysGuard::Process *process : processlist) { - if (!context.isValid()) { - return; - } - const QString name = process->name(); if (!name.contains(term, Qt::CaseInsensitive)) { - //Process doesn't match the search term continue; } const quint64 pid = process->pid(); - const qlonglong uid = process->uid(); - const QString user = getUserName(uid); - - QVariantList data; - data << pid << user; Plasma::QueryMatch match(this); 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.setData(data); + match.setData(pid); match.setId(name); // Set the relevance @@ -161,32 +153,25 @@ matches << match; } - qDebug() << "match count is" << matches.count(); context.addMatches(matches); } void KillRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match) { Q_UNUSED(context) - QVariantList data = match.data().value(); - quint64 pid = data[0].toUInt(); -// QString user = data[1].toString(); + quint64 pid = match.data().toUInt(); int signal; - if (match.selectedAction() != nullptr) { + if (match.selectedAction()) { signal = match.selectedAction()->data().toInt(); } else { signal = 9; //default: SIGKILL } - QStringList args; - args << QStringLiteral("-%1").arg(signal) << QStringLiteral("%1").arg(pid); - KProcess process; - int returnCode = process.execute(QStringLiteral("kill"), args); - - if (returnCode == 0) - { + QStringList args = {QStringLiteral("-%1").arg(signal), QStringLiteral("%1").arg(pid)}; + int returnCode = KProcess::execute(QStringLiteral("kill"), args); + if (returnCode == 0) { return; } @@ -202,24 +187,7 @@ { Q_UNUSED(match) - QList ret; - - 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 + return m_actionList; } #include "killrunner.moc" diff --git a/runners/kill/killrunner_config.h b/runners/kill/killrunner_config.h --- a/runners/kill/killrunner_config.h +++ b/runners/kill/killrunner_config.h @@ -1,4 +1,5 @@ /* Copyright 2009 + * Copyright 2020 * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public diff --git a/runners/kill/killrunner_config.cpp b/runners/kill/killrunner_config.cpp --- a/runners/kill/killrunner_config.cpp +++ b/runners/kill/killrunner_config.cpp @@ -1,4 +1,5 @@ /* Copyright 2009 + * Copyright 2020 * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -19,21 +20,19 @@ //Project-Includes #include "killrunner_config.h" -#include //KDE-Includes #include +#include #include -#include K_PLUGIN_FACTORY(KillRunnerConfigFactory, registerPlugin(QStringLiteral("kcm_krunner_kill"));) KillRunnerConfigForm::KillRunnerConfigForm(QWidget* parent) : QWidget(parent) { setupUi(this); } -KillRunnerConfig::KillRunnerConfig(QWidget* parent, const QVariantList& args) : - KCModule(parent, args) +KillRunnerConfig::KillRunnerConfig(QWidget* parent, const QVariantList& args) : KCModule(parent, args) { m_ui = new KillRunnerConfigForm(this); @@ -43,15 +42,10 @@ m_ui->sorting->addItem(i18n("CPU usage"), CPU); m_ui->sorting->addItem(i18n("inverted CPU usage"), CPUI); 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::of(&QComboBox::currentIndexChanged), this, QOverload<>::of(&KillRunnerConfig::changed)); -#else + connect(m_ui->useTriggerWord, &QCheckBox::stateChanged, this, &KillRunnerConfig::markAsChanged); connect(m_ui->triggerWord, &KLineEdit::textChanged, this, &KillRunnerConfig::markAsChanged); connect(m_ui->sorting, QOverload::of(&QComboBox::currentIndexChanged), this, &KillRunnerConfig::markAsChanged); -#endif load(); } @@ -61,12 +55,11 @@ KCModule::load(); KSharedConfig::Ptr cfg = KSharedConfig::openConfig(QStringLiteral("krunnerrc")); - KConfigGroup grp = cfg->group("Runners"); - grp = KConfigGroup(&grp, "Kill Runner"); + const KConfigGroup grp = cfg->group("Runners").group("Kill Runner"); m_ui->useTriggerWord->setChecked(grp.readEntry(CONFIG_USE_TRIGGERWORD,true)); - m_ui->triggerWord->setText(grp.readEntry(CONFIG_TRIGGERWORD,i18n("kill"))); - m_ui->sorting->setCurrentIndex(m_ui->sorting->findData(grp.readEntry(CONFIG_SORTING, static_cast(NONE)))); + m_ui->triggerWord->setText(grp.readEntry(CONFIG_TRIGGERWORD, i18n("kill"))); + m_ui->sorting->setCurrentIndex(m_ui->sorting->findData(grp.readEntry(CONFIG_SORTING, NONE))); emit changed(false); } @@ -76,12 +69,11 @@ KCModule::save(); KSharedConfig::Ptr cfg = KSharedConfig::openConfig(QStringLiteral("krunnerrc")); - KConfigGroup grp = cfg->group("Runners"); - grp = KConfigGroup(&grp, "Kill Runner"); + KConfigGroup grp = cfg->group("Runners").group("Kill Runner"); - grp.writeEntry(CONFIG_USE_TRIGGERWORD,m_ui->useTriggerWord->isChecked()); - grp.writeEntry(CONFIG_TRIGGERWORD,m_ui->triggerWord->text()); - grp.writeEntry(CONFIG_SORTING,m_ui->sorting->itemData(m_ui->sorting->currentIndex())); + grp.writeEntry(CONFIG_USE_TRIGGERWORD, m_ui->useTriggerWord->isChecked()); + grp.writeEntry(CONFIG_TRIGGERWORD, m_ui->triggerWord->text()); + grp.writeEntry(CONFIG_SORTING, m_ui->sorting->itemData(m_ui->sorting->currentIndex())); emit changed(false); } @@ -94,7 +86,7 @@ m_ui->triggerWord->setText(i18n("kill")); m_ui->sorting->setCurrentIndex(m_ui->sorting->findData((int) NONE)); - emit changed(true); + emit markAsChanged(); } #include "killrunner_config.moc"