Skip to content

Commit a5a5c76

Browse files
committed
Init.
0 parents  commit a5a5c76

File tree

4 files changed

+160
-0
lines changed

4 files changed

+160
-0
lines changed

CMakeLists.txt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
cmake_minimum_required(VERSION 2.8.12)
2+
project(Runnerlinuxcommands)
3+
4+
find_package(ECM 5.12.0 REQUIRED NO_MODULE)
5+
set (CMAKE_MODULE_PATH
6+
${ECM_MODULE_PATH} ${ECM_KDE_MODULE_DIR} ${CMAKE_MODULE_PATH}
7+
)
8+
9+
10+
find_package (Qt5 ${QT_MIN_VERSION} REQUIRED CONFIG COMPONENTS Widgets Core Network Quick QuickWidgets)
11+
find_package (KF5 ${KF5_MIN_VERSION} REQUIRED COMPONENTS I18n Service Runner TextWidgets ConfigWidgets PlasmaQuick)
12+
13+
include(KDEInstallDirs)
14+
include(KDECMakeSettings)
15+
include(KDECompilerSettings NO_POLICY_SCOPE)
16+
17+
set(krunner_linuxcommands_SRCS
18+
linux_commands.cpp
19+
)
20+
21+
add_library(krunner_linuxcommands MODULE ${krunner_linuxcommands_SRCS})
22+
target_link_libraries(krunner_linuxcommands KF5::Runner Qt5::Widgets Qt5::Network
23+
KF5::I18n
24+
KF5::Service
25+
KF5::ConfigWidgets
26+
KF5::Plasma)
27+
28+
#add_dependencies(krunner_linuxcommands)
29+
30+
install(TARGETS krunner_linuxcommands DESTINATION ${PLUGIN_INSTALL_DIR})
31+
install(FILES plasma-runner-linuxcommands.desktop DESTINATION ${SERVICES_INSTALL_DIR})

linux_commands.cpp

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#include "linux_commands.h"
2+
3+
#include <KLocalizedString>
4+
#include <QApplication>
5+
#include <QClipboard>
6+
#include <QFileInfo>
7+
#include <QJsonDocument>
8+
9+
LinuxCommandsRunner::LinuxCommandsRunner(QObject *parent, const QVariantList &args)
10+
: Plasma::AbstractRunner(parent, args)
11+
{
12+
Q_UNUSED(args);
13+
14+
setObjectName(QLatin1String("LinuxCommandsRunner"));
15+
reloadConfiguration();
16+
setHasRunOptions(true);
17+
setIgnoredTypes(Plasma::RunnerContext::Directory |
18+
Plasma::RunnerContext::File |
19+
Plasma::RunnerContext::NetworkLocation);
20+
setSpeed(AbstractRunner::SlowSpeed);
21+
setPriority(HighestPriority);
22+
setDefaultSyntax(Plasma::RunnerSyntax(QString("lc :q:"), i18n("List linux commands :q:.")));
23+
linux_command_index_path = "/home/roach/data.json";
24+
}
25+
26+
LinuxCommandsRunner::~LinuxCommandsRunner()
27+
{
28+
}
29+
30+
void LinuxCommandsRunner::match(Plasma::RunnerContext &context)
31+
{
32+
QString text = context.query();
33+
34+
if (!context.isValid() || !fileExists(linux_command_index_path)) return;
35+
36+
if (text.contains(" ")) {
37+
QJsonObject json = readJson(linux_command_index_path);
38+
QEventLoop loop;
39+
QList<Plasma::QueryMatch> matches;
40+
float relevance = 1;
41+
QString kw = text.replace("lc", "", Qt::CaseInsensitive);
42+
if (kw.size() > 0) {
43+
foreach(const QString& key, json.keys()) {
44+
if (key.contains(kw, Qt::CaseInsensitive)) {
45+
relevance -= 0.01;
46+
Plasma::QueryMatch match(this);
47+
match.setType(Plasma::QueryMatch::InformationalMatch);
48+
match.setIcon(QIcon::fromTheme("konversation"));
49+
match.setText(key);
50+
match.setRelevance(relevance);
51+
matches.append(match);
52+
}
53+
}
54+
}
55+
context.addMatches(matches);
56+
loop.exec();
57+
}
58+
}
59+
60+
void LinuxCommandsRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match)
61+
{
62+
Q_UNUSED(context);
63+
QApplication::clipboard()->setText(match.text());
64+
}
65+
66+
void LinuxCommandsRunner::reloadConfiguration()
67+
{
68+
//nothing
69+
}
70+
71+
bool LinuxCommandsRunner::fileExists(QString& path) {
72+
QFileInfo check_file(path);
73+
// check if file exists and if yes: Is it really a file and no directory?
74+
if (check_file.exists() && check_file.isFile()) {
75+
return true;
76+
} else {
77+
return false;
78+
}
79+
}
80+
QJsonObject LinuxCommandsRunner::readJson(QString& path)
81+
{
82+
QFile file(path);
83+
file.open(QIODevice::ReadOnly | QIODevice::Text);
84+
QByteArray rawData = file.readAll();
85+
file.close();
86+
return QJsonDocument::fromJson(rawData).object();
87+
}
88+
#include "moc_linux_commands.cpp"

linux_commands.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#ifndef LINUX_COMMANDS_H
2+
#define LINUX_COMMANDS_H
3+
4+
#include <KRunner/AbstractRunner>
5+
6+
class LinuxCommandsRunner : public Plasma::AbstractRunner
7+
{
8+
Q_OBJECT
9+
10+
public:
11+
LinuxCommandsRunner(QObject *parent, const QVariantList &args);
12+
~LinuxCommandsRunner();
13+
14+
void match(Plasma::RunnerContext &context) override;
15+
void run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match) override;
16+
void createRunOptions(QWidget *widget) override;
17+
void reloadConfiguration() override;
18+
19+
private:
20+
QString linux_command_index_path;
21+
bool fileExists(QString& path);
22+
QJsonObject readJson(QString& path);
23+
};
24+
25+
K_EXPORT_PLASMA_RUNNER(linuxcommands, LinuxCommandsRunner)
26+
27+
#endif
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[Desktop Entry]
2+
Name=Linux Commands
3+
Comment=Search through the Linux command.
4+
X-KDE-ServiceTypes=Plasma/Runner
5+
Type=Service
6+
Icon=konversation
7+
X-KDE-Library=krunner_linuxcommands
8+
X-KDE-PluginInfo-Author=Roach Sinai
9+
X-KDE-PluginInfo-Email=roachsinai@qq.com
10+
X-KDE-PluginInfo-Name=linuxcommands
11+
X-KDE-PluginInfo-Version= 1.0
12+
X-KDE-PluginInfo-License=GPL
13+
X-KDE-PluginInfo-EnabledByDefault=true
14+
X-Plasma-AdvertiseSingleRunnerQueryMode=true

0 commit comments

Comments
 (0)