diff --git a/runners/dictionary/config_keys.h b/runners/dictionary/config_keys.h new file mode 100644 --- /dev/null +++ b/runners/dictionary/config_keys.h @@ -0,0 +1,6 @@ +/* + * Copyright (C) 2010, 2012 Jason A. Donenfeld + * Copyright (C) 2020 Alexander Lohnau + */ + +static const char CONFIG_TRIGGERWORD[] = "triggerWord"; diff --git a/runners/dictionary/dictionarymatchengine.cpp b/runners/dictionary/dictionarymatchengine.cpp --- a/runners/dictionary/dictionarymatchengine.cpp +++ b/runners/dictionary/dictionarymatchengine.cpp @@ -1,5 +1,6 @@ /* * Copyright (C) 2010, 2012 Jason A. Donenfeld + * Copyright (C) 2020 Alexander Lohnau */ #include "dictionarymatchengine.h" @@ -18,7 +19,7 @@ * how the connection is made based on data availability. There are two cases, * and this extra connection handles the second case. */ Q_ASSERT(m_dictionaryEngine); - connect(m_dictionaryEngine, SIGNAL(sourceAdded(QString)), this, SLOT(sourceAdded(QString))); + connect(m_dictionaryEngine, &Plasma::DataEngine::sourceAdded, this, &DictionaryMatchEngine::sourceAdded); } /* This function should be called from a different thread. */ @@ -74,13 +75,14 @@ void DictionaryMatchEngine::dataUpdated(const QString &source, const Plasma::DataEngine::Data &result) { - if (!result.contains(QLatin1String("text"))) + if (!result.contains(QStringLiteral("text"))){ return; + } - QString definition(result[QLatin1String("text")].toString()); + QString definition(result[QStringLiteral("text")].toString()); m_wordLock.lockForRead(); - foreach (ThreadData *data, m_lockers.values(source)) { + for (ThreadData *data: m_lockers.values(source)) { QMutexLocker locker(&data->mutex); /* Because of QString's CoW semantics, we don't have to worry about * the overhead of assigning this to every item. */ diff --git a/runners/dictionary/dictionaryrunner.h b/runners/dictionary/dictionaryrunner.h --- a/runners/dictionary/dictionaryrunner.h +++ b/runners/dictionary/dictionaryrunner.h @@ -1,11 +1,13 @@ /* * Copyright (C) 2010, 2012 Jason A. Donenfeld + * Copyright (C) 2020 Alexander Lohnau */ #ifndef DICTIONARYRUNNER_H #define DICTIONARYRUNNER_H #include +#include #include "dictionarymatchengine.h" class DictionaryRunner : public Plasma::AbstractRunner @@ -20,10 +22,8 @@ private: QString m_triggerWord; DictionaryMatchEngine *m_engine; - -protected Q_SLOTS: - void init() override; - + QRegularExpression m_removeHtmlRegex; + QRegularExpression m_partOfSpeechRegex; }; #endif diff --git a/runners/dictionary/dictionaryrunner.cpp b/runners/dictionary/dictionaryrunner.cpp --- a/runners/dictionary/dictionaryrunner.cpp +++ b/runners/dictionary/dictionaryrunner.cpp @@ -1,83 +1,93 @@ /* * Copyright (C) 2010, 2012 Jason A. Donenfeld + * Copyright (C) 2020 Alexander Lohnau */ #include "dictionaryrunner.h" +#include "config_keys.h" #include -#include - -static const char CONFIG_TRIGGERWORD[] = "triggerWord"; +#include DictionaryRunner::DictionaryRunner(QObject *parent, const QVariantList &args) : AbstractRunner(parent, args) { m_engine = new DictionaryMatchEngine(dataEngine(QStringLiteral("dict")), this); setSpeed(SlowSpeed); setPriority(LowPriority); - setObjectName(QLatin1String("Dictionary")); + setObjectName(QStringLiteral("Dictionary")); setIgnoredTypes(Plasma::RunnerContext::Directory | Plasma::RunnerContext::File | - Plasma::RunnerContext::NetworkLocation | Plasma::RunnerContext::Executable | - Plasma::RunnerContext::ShellCommand); -} + Plasma::RunnerContext::NetworkLocation | Plasma::RunnerContext::Executable | + Plasma::RunnerContext::ShellCommand); -void DictionaryRunner::init() -{ - reloadConfiguration(); + m_removeHtmlRegex = QRegularExpression(QStringLiteral("<[^>]*>")); + m_partOfSpeechRegex = QRegularExpression(QStringLiteral("(?: ([a-z]{1,5})){0,1} [0-9]{1,2}: (.*)")); + m_removeHtmlRegex.optimize(); + m_partOfSpeechRegex.optimize(); } void DictionaryRunner::reloadConfiguration() { - KConfigGroup c = config(); + const KConfigGroup c = config(); m_triggerWord = c.readEntry(CONFIG_TRIGGERWORD, i18nc("Trigger word before word to define", "define")); - if (!m_triggerWord.isEmpty()) - m_triggerWord.append(QLatin1Char(' ')); - setSyntaxes(QList() << Plasma::RunnerSyntax(Plasma::RunnerSyntax(i18nc("Dictionary keyword", "%1:q:", m_triggerWord), i18n("Finds the definition of :q:.")))); + if (!m_triggerWord.isEmpty()) { + m_triggerWord = m_triggerWord.simplified().append(QLatin1Char(' ')); + } + setSyntaxes({Plasma::RunnerSyntax(Plasma::RunnerSyntax(i18nc("Dictionary keyword", "%1:q:", m_triggerWord), + i18n("Finds the definition of :q:.")))}); } void DictionaryRunner::match(Plasma::RunnerContext &context) { QString query = context.query(); - if (!query.startsWith(m_triggerWord, Qt::CaseInsensitive)) + if (!query.startsWith(m_triggerWord, Qt::CaseInsensitive)) { return; + } query.remove(0, m_triggerWord.length()); - if (query.isEmpty()) + if (query.isEmpty()) { return; + } QString returnedQuery = m_engine->lookupWord(query); - if (!context.isValid()) + if (!context.isValid()) { return; + } - static const QRegExp removeHtml(QLatin1String("<[^>]*>")); QString definitions(returnedQuery); - definitions.remove(QLatin1Char('\r')).remove(removeHtml); - while (definitions.contains(QLatin1String(" "))) + definitions.remove(QLatin1Char('\r')).remove(m_removeHtmlRegex); + while (definitions.contains(QLatin1String(" "))) { definitions.replace(QLatin1String(" "), QLatin1String(" ")); + } #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) QStringList lines = definitions.split(QLatin1Char('\n'), QString::SkipEmptyParts); #else QStringList lines = definitions.split(QLatin1Char('\n'), Qt::SkipEmptyParts); #endif - if (lines.length() < 2) + if (lines.length() < 2) { return; + } lines.removeFirst(); QList matches; int item = 0; - static const QRegExp partOfSpeech(QLatin1String("(?: ([a-z]{1,5})){0,1} [0-9]{1,2}: (.*)")); QString lastPartOfSpeech; - foreach (const QString &line, lines) { - if (partOfSpeech.indexIn(line) == -1) + for (const QString &line: qAsConst(lines)) { + const QRegularExpressionMatch partOfSpeechMatch = m_partOfSpeechRegex.match(line); + if (!partOfSpeechMatch.hasMatch()) { continue; - if (!partOfSpeech.cap(1).isEmpty()) - lastPartOfSpeech = partOfSpeech.cap(1); + } + if (!partOfSpeechMatch.captured(1).isEmpty()) { + lastPartOfSpeech = partOfSpeechMatch.captured(1); + } Plasma::QueryMatch match(this); match.setText(query + QLatin1String(": ") + lastPartOfSpeech); match.setRelevance(1 - (static_cast(++item) / static_cast(lines.length()))); match.setType(Plasma::QueryMatch::InformationalMatch); match.setIconName(QStringLiteral("accessories-dictionary")); - match.setSubtext(partOfSpeech.cap(2)); + match.setSubtext(partOfSpeechMatch.captured(2)); + // TODO What to set as data? + //match.setData(QVariant()); matches.append(match); } context.addMatches(matches); diff --git a/runners/dictionary/dictionaryrunner_config.h b/runners/dictionary/dictionaryrunner_config.h --- a/runners/dictionary/dictionaryrunner_config.h +++ b/runners/dictionary/dictionaryrunner_config.h @@ -1,5 +1,6 @@ /* * Copyright (C) 2010, 2012 Jason A. Donenfeld + * Copyright (C) 2020 Alexander Lohnau */ #ifndef DICTIONARYRUNNERCONFIG_H @@ -9,8 +10,6 @@ #include class QLineEdit; -static const char CONFIG_TRIGGERWORD[] = "triggerWord"; - class DictionaryRunnerConfig : public KCModule { Q_OBJECT diff --git a/runners/dictionary/dictionaryrunner_config.cpp b/runners/dictionary/dictionaryrunner_config.cpp --- a/runners/dictionary/dictionaryrunner_config.cpp +++ b/runners/dictionary/dictionaryrunner_config.cpp @@ -1,52 +1,46 @@ /* * Copyright (C) 2010, 2012 Jason A. Donenfeld + * Copyright (C) 2020 Alexander Lohnau */ #include "dictionaryrunner_config.h" +#include "config_keys.h" #include #include #include #include -#include +#include #include K_PLUGIN_FACTORY(DictionaryRunnerConfigFactory, registerPlugin(QStringLiteral("kcm_krunner_dictionary"));) DictionaryRunnerConfig::DictionaryRunnerConfig(QWidget* parent, const QVariantList& args) : KCModule(parent, args) { - QFormLayout *layout = new QFormLayout; + auto *layout = new QFormLayout; m_triggerWord = new QLineEdit; layout->addRow(i18nc("@label:textbox", "Trigger word:"), m_triggerWord); setLayout(layout); - connect(m_triggerWord, SIGNAL(textChanged(QString)), this, SLOT(changed())); - load(); + connect(m_triggerWord, &QLineEdit::textChanged, this, &DictionaryRunnerConfig::markAsChanged); } void DictionaryRunnerConfig::load() { - KCModule::load(); - KSharedConfig::Ptr cfg = KSharedConfig::openConfig(QLatin1String("krunnerrc")); - KConfigGroup grp = cfg->group("Runners"); - grp = KConfigGroup(&grp, "Dictionary"); + KConfigGroup grp = KSharedConfig::openConfig(QStringLiteral("krunnerrc"))->group("Runners").group("Dictionary"); m_triggerWord->setText(grp.readEntry(CONFIG_TRIGGERWORD, i18nc("Trigger word before word to define", "define"))); emit changed(false); } void DictionaryRunnerConfig::save() { - KCModule::save(); - KSharedConfig::Ptr cfg = KSharedConfig::openConfig(QLatin1String("krunnerrc")); - KConfigGroup grp = cfg->group("Runners"); - grp = KConfigGroup(&grp, "Dictionary"); + KConfigGroup grp = KSharedConfig::openConfig(QStringLiteral("krunnerrc"))->group("Runners").group("Dictionary"); grp.writeEntry(CONFIG_TRIGGERWORD, m_triggerWord->text()); emit changed(false); } void DictionaryRunnerConfig::defaults() { - KCModule::defaults(); m_triggerWord->setText(i18nc("Trigger word before word to define", "define")); emit changed(true); }