Changeset View
Changeset View
Standalone View
Standalone View
runners/shell/shellrunner.cpp
Show All 14 Lines | |||||
15 | * License along with this program; if not, write to the | 15 | * License along with this program; if not, write to the | ||
16 | * Free Software Foundation, Inc., | 16 | * Free Software Foundation, Inc., | ||
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
18 | */ | 18 | */ | ||
19 | 19 | | |||
20 | #include "shellrunner.h" | 20 | #include "shellrunner.h" | ||
21 | 21 | | |||
22 | #include <KAuthorized> | 22 | #include <KAuthorized> | ||
23 | #include <QDebug> | | |||
24 | #include <KLocalizedString> | 23 | #include <KLocalizedString> | ||
25 | #include <KRun> | 24 | #include <KRun> | ||
26 | #include <KShell> | | |||
27 | #include <QStandardPaths> | | |||
28 | #include <KToolInvocation> | 25 | #include <KToolInvocation> | ||
29 | 26 | | |||
30 | #include <Plasma/Theme> | | |||
31 | | ||||
32 | #include "shell_config.h" | | |||
33 | | ||||
34 | K_EXPORT_PLASMA_RUNNER(shell, ShellRunner) | 27 | K_EXPORT_PLASMA_RUNNER(shell, ShellRunner) | ||
35 | 28 | | |||
36 | static QString s_runInTerminalId = QStringLiteral("runInTerminal"); | | |||
37 | | ||||
38 | ShellRunner::ShellRunner(QObject *parent, const QVariantList &args) | 29 | ShellRunner::ShellRunner(QObject *parent, const QVariantList &args) | ||
39 | : Plasma::AbstractRunner(parent, args) | 30 | : Plasma::AbstractRunner(parent, args) | ||
40 | { | 31 | { | ||
41 | setObjectName( QLatin1String("Command" )); | 32 | setObjectName(QStringLiteral("Command")); | ||
42 | setPriority(AbstractRunner::HighestPriority); | 33 | setPriority(AbstractRunner::HighestPriority); | ||
43 | setHasRunOptions(true); | | |||
44 | m_enabled = KAuthorized::authorize(QStringLiteral("run_command")) && KAuthorized::authorize(QStringLiteral("shell_access")); | 34 | m_enabled = KAuthorized::authorize(QStringLiteral("run_command")) && KAuthorized::authorize(QStringLiteral("shell_access")); | ||
45 | setIgnoredTypes(Plasma::RunnerContext::Directory | Plasma::RunnerContext::File | | 35 | setIgnoredTypes(Plasma::RunnerContext::Directory | Plasma::RunnerContext::File | | ||
46 | Plasma::RunnerContext::NetworkLocation | Plasma::RunnerContext::UnknownType | | 36 | Plasma::RunnerContext::NetworkLocation | Plasma::RunnerContext::UnknownType | | ||
47 | Plasma::RunnerContext::Help); | 37 | Plasma::RunnerContext::Help); | ||
48 | 38 | | |||
49 | addSyntax(Plasma::RunnerSyntax(QStringLiteral(":q:"), i18n("Finds commands that match :q:, using common shell syntax"))); | 39 | addSyntax(Plasma::RunnerSyntax(QStringLiteral(":q:"), i18n("Finds commands that match :q:, using common shell syntax"))); | ||
50 | addAction(s_runInTerminalId, QIcon::fromTheme(QStringLiteral("utilities-terminal")), i18n("Run in Terminal Window")); | 40 | m_actionList = {addAction(QStringLiteral("runInTerminal"), | ||
41 | QIcon::fromTheme(QStringLiteral("utilities-terminal")), | ||||
42 | i18n("Run in Terminal Window"))}; | ||||
43 | m_matchIcon = QIcon::fromTheme(QStringLiteral("system-run")); | ||||
51 | } | 44 | } | ||
52 | 45 | | |||
53 | ShellRunner::~ShellRunner() | 46 | ShellRunner::~ShellRunner() | ||
54 | { | 47 | { | ||
55 | } | 48 | } | ||
56 | 49 | | |||
57 | void ShellRunner::match(Plasma::RunnerContext &context) | 50 | void ShellRunner::match(Plasma::RunnerContext &context) | ||
58 | { | 51 | { | ||
59 | if (!m_enabled) { | 52 | if (!context.isValid() || !m_enabled) { | ||
60 | return; | 53 | return; | ||
61 | } | 54 | } | ||
62 | 55 | | |||
63 | if (context.type() == Plasma::RunnerContext::Executable || | | |||
64 | context.type() == Plasma::RunnerContext::ShellCommand) { | | |||
65 | const QString term = context.query(); | 56 | const QString term = context.query(); | ||
66 | Plasma::QueryMatch match(this); | 57 | Plasma::QueryMatch match(this); | ||
67 | match.setId(term); | 58 | match.setId(term); | ||
68 | match.setType(Plasma::QueryMatch::ExactMatch); | 59 | match.setType(Plasma::QueryMatch::ExactMatch); | ||
69 | match.setIconName(QStringLiteral("system-run")); | 60 | match.setIcon(m_matchIcon); | ||
70 | match.setText(i18n("Run %1", term)); | 61 | match.setText(i18n("Run %1", term)); | ||
71 | match.setRelevance(0.7); | 62 | match.setRelevance(0.7); | ||
72 | context.addMatch(match); | 63 | context.addMatch(match); | ||
73 | } | 64 | } | ||
74 | } | | |||
75 | 65 | | |||
76 | void ShellRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match) | 66 | void ShellRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match) | ||
77 | { | 67 | { | ||
78 | Q_UNUSED(match); | 68 | if (match.selectedAction()) { | ||
meven: you can remove this line | |||||
79 | | ||||
80 | if (m_enabled) { | | |||
81 | if (match.selectedAction() && match.selectedAction() == action(s_runInTerminalId)) { | | |||
82 | KToolInvocation::invokeTerminal(context.query()); | 69 | KToolInvocation::invokeTerminal(context.query()); | ||
83 | } else { | 70 | } else { | ||
84 | KRun::runCommand(context.query(), nullptr); | 71 | KRun::runCommand(context.query(), nullptr); | ||
85 | } | 72 | } | ||
86 | } | 73 | } | ||
87 | } | | |||
88 | 74 | | |||
89 | QList<QAction *> ShellRunner::actionsForMatch(const Plasma::QueryMatch &match) | 75 | QList<QAction *> ShellRunner::actionsForMatch(const Plasma::QueryMatch &match) | ||
90 | { | 76 | { | ||
91 | Q_UNUSED(match) | 77 | Q_UNUSED(match) | ||
92 | 78 | | |||
93 | return {action(s_runInTerminalId)}; | 79 | return m_actionList; | ||
94 | } | 80 | } | ||
95 | 81 | | |||
96 | #include "shellrunner.moc" | 82 | #include "shellrunner.moc" |
you can remove this line