-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlinux_commands.cpp
111 lines (98 loc) · 3.48 KB
/
linux_commands.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "linux_commands.h"
#include "config/linux_commands_config.h"
#include <KLocalizedString>
#include <QApplication>
#include <QClipboard>
#include <QFileInfo>
#include <QJsonDocument>
#include <QDir>
#include <QDesktopServices>
LinuxCommandsRunner::LinuxCommandsRunner(QObject *parent, const QVariantList &args)
: Plasma::AbstractRunner(parent, args)
{
Q_UNUSED(args);
setObjectName(QLatin1String("LinuxCommandsRunner"));
setHasRunOptions(true);
setIgnoredTypes(Plasma::RunnerContext::Directory |
Plasma::RunnerContext::File |
Plasma::RunnerContext::NetworkLocation);
setSpeed(AbstractRunner::SlowSpeed);
setPriority(HighestPriority);
setDefaultSyntax(Plasma::RunnerSyntax(QString("lc :q:"), i18n("List linux commands :q:.")));
url_prefix = "https://wangchujiang.com/linux-command/c";
reloadConfiguration();
}
LinuxCommandsRunner::~LinuxCommandsRunner()
{
}
void LinuxCommandsRunner::match(Plasma::RunnerContext &context)
{
QString text = context.query();
if (!context.isValid() || !fileExists(morphFile(m_jsonLocation))) return;
if (text.contains(" ")) {
QJsonObject json = readJson(morphFile(m_jsonLocation));
QList<Plasma::QueryMatch> matches;
float relevance = 1;
QString kw = text.replace("lc ", "", Qt::CaseInsensitive);
if (kw.size() > 0) {
foreach(const QString& key, json.keys()) {
if (key.contains(kw, Qt::CaseInsensitive)) {
QJsonObject inner_json = json.value(key).toObject();
QString desc = inner_json.value("d").toString();
QUrl url(url_prefix + inner_json.value("p").toString() + ".html");
QList<QUrl> urls;
urls.append(url);
relevance -= 0.01;
Plasma::QueryMatch match(this);
match.setType(Plasma::QueryMatch::HelperMatch);
match.setIcon(QIcon::fromTheme("internet-web-browser"));
match.setText(key);
match.setSubtext(desc);
match.setUrls(urls);
match.setEnabled(true);
match.setRelevance(relevance);
matches.append(match);
}
}
}
if (!matches.isEmpty())
context.addMatches(matches);
}
}
void LinuxCommandsRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match)
{
Q_UNUSED(context);
QApplication::clipboard()->setText(match.text());
foreach(const QUrl& url, match.urls()) {
QDesktopServices::openUrl(url);
}
}
void LinuxCommandsRunner::reloadConfiguration()
{
KConfigGroup grp = config();
m_jsonLocation= grp.readEntry(CONFIG_JSON_LOCATION);
}
bool LinuxCommandsRunner::fileExists(QString& path) {
QFileInfo check_file(path);
// check if file exists and if yes: Is it really a file and no directory?
if (check_file.exists() && check_file.isFile()) {
return true;
} else {
return false;
}
}
QJsonObject LinuxCommandsRunner::readJson(QString& path)
{
QFile file(path);
file.open(QIODevice::ReadOnly | QIODevice::Text);
QByteArray rawData = file.readAll();
file.close();
return QJsonDocument::fromJson(rawData).object();
}
QString& LinuxCommandsRunner::morphFile(QString& s)
{
if (s.startsWith ("~/"))
s.replace (0, 1, QDir::homePath());
return s;
}
#include "moc_linux_commands.cpp"