|
| 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" |
0 commit comments