Commit 8dd8fe55 authored by Alexander Lohnau's avatar Alexander Lohnau 💬 Committed by Harald Sitter

characters: Refactoring, improve validation in kcm, optimize

Summary:
The config keys are now in a separate file, instead of having them at two places.
Runner
The duplicate call to reloadConfiguration has been removed and aliases/codes get validated.
Additionally the match method has been simplified and the character is now copied to the clipboard when selected.
KCM
Validation of the add/delete buttons, error message now in GUI and deprecated methods removed.

Test Plan:
Compile, test runner and test editing entries.
Manually add alias(and no hex value) to ~/.config/krunnerrc and reload the kcm, error message should be shown.

Before:
{F8075415}
{F8075416}

After:
{F8075419}
{F8075420}
{F8075422}

Reviewers: davidedmundson, ngraham, sitter, broulik, #plasma

Reviewed By: sitter

Subscribers: davidre, dvratil, plasma-devel

Tags: #plasma

Differential Revision: https://phabricator.kde.org/D27096
parent f07e3a54
/* Copyright 2010 Anton Kreuzkamp <akreuzkamp@web.de>
* Copyright 2020 Alexander Lohnau <alexander.lohnau@gmx.de>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
......@@ -18,27 +19,24 @@
*/
#include "charrunner.h"
#include "config_keys.h"
// KF
#include <KRunner/QueryMatch>
#include <KLocalizedString>
// Qt
#include <QGuiApplication>
#include <QDebug>
#include <QClipboard>
//Names of config-entries
static const char CONFIG_TRIGGERWORD[] = "triggerWord";
static const char CONFIG_ALIASES[] = "aliases";
static const char CONFIG_CODES[] = "codes";
CharacterRunner::CharacterRunner( QObject* parent, const QVariantList &args )
CharacterRunner::CharacterRunner(QObject *parent, const QVariantList &args)
: Plasma::AbstractRunner(parent, args)
{
Q_UNUSED(args)
setObjectName(QLatin1String( "CharacterRunner" ));
setObjectName(QStringLiteral("CharacterRunner"));
setIgnoredTypes(Plasma::RunnerContext::Directory | Plasma::RunnerContext::File |
Plasma::RunnerContext::NetworkLocation | Plasma::RunnerContext::Executable |
Plasma::RunnerContext::ShellCommand);
reloadConfiguration();
Plasma::RunnerContext::NetworkLocation | Plasma::RunnerContext::Executable |
Plasma::RunnerContext::ShellCommand);
}
CharacterRunner::~CharacterRunner()
......@@ -47,55 +45,58 @@ CharacterRunner::~CharacterRunner()
void CharacterRunner::reloadConfiguration()
{
KConfigGroup grp = config(); //Create config-object
m_triggerWord = grp.readEntry(CONFIG_TRIGGERWORD, "#"); //read out the triggerword
m_aliases = grp.readEntry(CONFIG_ALIASES, QStringList());
m_codes = grp.readEntry(CONFIG_CODES, QStringList());
addSyntax(Plasma::RunnerSyntax(m_triggerWord + QLatin1String( ":q:" ),
i18n("Creates Characters from :q: if it is a hexadecimal code or defined alias.")));
const KConfigGroup grp = config();
m_triggerWord = grp.readEntry(CONFIG_TRIGGERWORD, DEFAULT_TRIGGERWORD.toString());
m_aliases = grp.readEntry(CONFIG_ALIASES, QStringList());
m_codes = grp.readEntry(CONFIG_CODES, QStringList());
if (m_codes.size() != m_aliases.size()) {
m_aliases.clear();
m_codes.clear();
qWarning() << "Config entries for alias list and code list have different sizes, ignoring all.";
}
addSyntax(Plasma::RunnerSyntax(m_triggerWord + QStringLiteral(":q:"),
i18n("Creates Characters from :q: if it is a hexadecimal code or defined alias.")));
}
void CharacterRunner::match(Plasma::RunnerContext &context)
{
QString term = context.query();
QString term = context.query().remove(QLatin1Char(' '));
term = term.replace(QLatin1Char( ' ' ), QLatin1String( "" )); //remove blanks
if (term.length() < 2) //ignore too short queries
{
if (term.length() < 2 || !term.startsWith(m_triggerWord) || !context.isValid()) {
return;
}
if (!term.startsWith(m_triggerWord)) //ignore queries without the triggerword
{
return;
}
term = term.remove(0, m_triggerWord.length()); //remove the triggerword
if (m_aliases.contains(term)) //replace aliases by their hex.-code
{
term = m_codes[m_aliases.indexOf(term)];
//replace aliases by their hex.-code
if (m_aliases.contains(term)) {
term = m_codes[m_aliases.indexOf(term)];
}
bool ok; //checkvariable
bool ok;
int hex = term.toInt(&ok, 16); //convert query into int
if (!ok) //check if conversion was successful
{
return;
if (!ok) {
return;
}
//make special character out of the hex.-code
const QString specChar = QChar(hex);
//create match
Plasma::QueryMatch match(this);
match.setType(Plasma::QueryMatch::InformationalMatch);
match.setType(Plasma::QueryMatch::ExactMatch);
match.setIconName(QStringLiteral("accessories-character-map"));
match.setText(specChar);
match.setData(specChar);
match.setId(QString());
context.addMatch(match);
}
void CharacterRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match)
{
Q_UNUSED(context)
QGuiApplication::clipboard()->setText(match.data().toString());
}
K_EXPORT_PLASMA_RUNNER(CharacterRunner, CharacterRunner)
#include "charrunner.moc"
/* Copyright 2010 Anton Kreuzkamp <akreuzkamp@web.de>
* Copyright 2020 Alexander Lohnau <alexander.lohnau@gmx.de>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
......@@ -30,9 +31,10 @@ class CharacterRunner : public Plasma::AbstractRunner
CharacterRunner(QObject* parent, const QVariantList &args);
~CharacterRunner() override;
void match(Plasma::RunnerContext &context) override;
void reloadConfiguration() override;
void match(Plasma::RunnerContext &context) override;
void run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match) override;
private:
//config-variables
QString m_triggerWord;
......
/* Copyright 2010 Anton Kreuzkamp <akreuzkamp@web.de>
* Copyright 2020 Alexander Lohnau <alexander.lohnau@gmx.de>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
......@@ -18,117 +19,115 @@
*/
#include "charrunner_config.h"
#include "config_keys.h"
// KF
#include <KRunner/AbstractRunner>
#include <KSharedConfig>
// Qt
#include <QDebug>
#include <KMessageWidget>
#include <KLocalizedString>
//Names of config-entries
static const char CONFIG_TRIGGERWORD[] = "triggerWord";
static const char CONFIG_ALIASES[] = "aliases";
static const char CONFIG_CODES[] = "codes";
CharacterRunnerConfigForm::CharacterRunnerConfigForm(QWidget* parent) : QWidget(parent)
CharacterRunnerConfigForm::CharacterRunnerConfigForm(QWidget *parent)
: QWidget(parent)
{
setupUi(this);
}
CharacterRunnerConfig::CharacterRunnerConfig(QWidget* parent, const QVariantList& args)
CharacterRunnerConfig::CharacterRunnerConfig(QWidget *parent, const QVariantList &args)
: KCModule(parent, args)
{
m_ui = new CharacterRunnerConfigForm(this);
QGridLayout* layout = new QGridLayout(this);
QGridLayout *layout = new QGridLayout(this);
layout->addWidget(m_ui, 0, 0);
connect(m_ui->edit_trigger, SIGNAL(textChanged(QString)), this, SLOT(changed()));
connect(m_ui->addItem, SIGNAL(clicked()), this, SLOT(addItem()));
connect(m_ui->deleteItem, SIGNAL(clicked()), this, SLOT(deleteItem()));
}
void CharacterRunnerConfig::addItem() //add Item to the list-view widget
{
QTreeWidgetItem* item = new QTreeWidgetItem(m_ui->list, 2);
item->setText(0, m_ui->edit_alias->text());
item->setText(1, m_ui->edit_hex->text());
m_ui->list->addTopLevelItem(item);
m_ui->edit_alias->clear();
m_ui->edit_hex->clear();
emit changed(true);
}
void CharacterRunnerConfig::deleteItem() //remove Item to the list-view widget
{
m_ui->list->takeTopLevelItem(m_ui->list->indexOfTopLevelItem(m_ui->list->currentItem()));
emit changed(true);
connect(m_ui->edit_trigger, &QLineEdit::textChanged, this, &CharacterRunnerConfig::markAsChanged);
connect(m_ui->addItem, &QPushButton::clicked, this, &CharacterRunnerConfig::addItem);
connect(m_ui->addItem, &QPushButton::clicked, this, &CharacterRunnerConfig::markAsChanged);
connect(m_ui->deleteItem, &QPushButton::clicked, this, &CharacterRunnerConfig::deleteItem);
connect(m_ui->deleteItem, &QPushButton::clicked, this, &CharacterRunnerConfig::markAsChanged);
connect(m_ui->list, &QTreeWidget::itemSelectionChanged, this, &CharacterRunnerConfig::validateDeleteButton);
connect(m_ui->edit_alias, &QLineEdit::textChanged, this, &CharacterRunnerConfig::validateAddButton);
connect(m_ui->edit_hex, &QLineEdit::textChanged, this, &CharacterRunnerConfig::validateAddButton);
}
void CharacterRunnerConfig::load()
{
KCModule::load();
//create config-object
KSharedConfig::Ptr cfg = KSharedConfig::openConfig(QLatin1String( "krunnerrc" ));
KConfigGroup grp = cfg->group("Runners");
grp = KConfigGroup(&grp, "CharacterRunner");
KSharedConfig::Ptr cfg = KSharedConfig::openConfig(QStringLiteral("krunnerrc"));
KConfigGroup grp = cfg->group("Runners").group("CharacterRunner");
m_ui->edit_trigger->setText(grp.readEntry(CONFIG_TRIGGERWORD, "#")); //read out triggerword and put into the trigger-lineEdit
const auto aliasList = grp.readEntry(CONFIG_ALIASES, QList<QString>());
const auto codeList = grp.readEntry(CONFIG_CODES, QList<QString>());
m_ui->edit_trigger->setText(grp.readEntry(CONFIG_TRIGGERWORD, DEFAULT_TRIGGERWORD.toString()));
const auto aliasList = grp.readEntry(CONFIG_ALIASES, QStringList());
const auto codeList = grp.readEntry(CONFIG_CODES, QStringList());
if (aliasList.size() == codeList.size()) {
for (int i = 0; i < aliasList.size(); ++i) { //read out aliaslist and add Items to the list-view widget
QTreeWidgetItem* item = new QTreeWidgetItem(m_ui->list, 2);
for (int i = 0; i < aliasList.size(); ++i) {
QTreeWidgetItem *item = new QTreeWidgetItem(m_ui->list, 2);
item->setText(0, aliasList[i]);
item->setText(1, codeList[i]);
m_ui->list->addTopLevelItem(item);
}
} else {
qWarning() << "Config entries for alias list and code list have different sizes, ignoring all.";
const auto msg = new KMessageWidget(i18nc("Message that config is corrupted",
"Config entries for alias list and code list have different sizes, ignoring all."), this);
m_ui->verticalLayout->insertWidget(0, msg);
}
emit changed(false);
}
void CharacterRunnerConfig::save()
{
KCModule::save();
//create config-object
KSharedConfig::Ptr cfg = KSharedConfig::openConfig(QLatin1String( "krunnerrc" ));
KConfigGroup grp = cfg->group("Runners");
grp = KConfigGroup(&grp, "CharacterRunner");
grp.writeEntry(CONFIG_TRIGGERWORD,m_ui->edit_trigger->text()); //write content from the triggerword-line Edit into the config
//Write the content of the List into the config
QList<QString> aliaslist;
QList<QString> codelist;
for(int i=0; i < m_ui->list->topLevelItemCount(); i++)
{
QString blub = m_ui->list->topLevelItem(i)->text(0);
aliaslist.append(blub);
codelist.append(m_ui->list->topLevelItem(i)->text(1));
}
grp.writeEntry(CONFIG_ALIASES, aliaslist);
grp.writeEntry(CONFIG_CODES, codelist);
KSharedConfig::Ptr cfg = KSharedConfig::openConfig(QStringLiteral("krunnerrc"));
KConfigGroup grp = cfg->group("Runners").group("CharacterRunner");
emit changed(false);
grp.writeEntry(CONFIG_TRIGGERWORD, m_ui->edit_trigger->text().isEmpty()
? DEFAULT_TRIGGERWORD.toString() : m_ui->edit_trigger->text());
QList<QString> aliasList;
QList<QString> codeList;
for (int i = 0; i < m_ui->list->topLevelItemCount(); ++i) {
aliasList.append(m_ui->list->topLevelItem(i)->text(0));
codeList.append(m_ui->list->topLevelItem(i)->text(1));
}
grp.writeEntry(CONFIG_ALIASES, aliasList);
grp.writeEntry(CONFIG_CODES, codeList);
}
void CharacterRunnerConfig::defaults()
{
KCModule::defaults();
m_ui->edit_trigger->setText(QLatin1String( "#" )); //set the content of the triggerword-lineEdit to default '#'
for(int i=0; i<m_ui->list->topLevelItemCount(); i++) //remove every item from the alias-list
{
m_ui->list->takeTopLevelItem(i);
}
m_ui->edit_trigger->setText(DEFAULT_TRIGGERWORD.toString());
m_ui->list->clear();
emit changed(true);
emit markAsChanged();
}
void CharacterRunnerConfig::addItem()
{
QTreeWidgetItem *item = new QTreeWidgetItem(m_ui->list, 2);
item->setText(0, m_ui->edit_alias->text());
item->setText(1, m_ui->edit_hex->text());
m_ui->list->addTopLevelItem(item);
m_ui->edit_alias->clear();
m_ui->edit_hex->clear();
}
void CharacterRunnerConfig::deleteItem()
{
m_ui->list->takeTopLevelItem(m_ui->list->indexOfTopLevelItem(m_ui->list->currentItem()));
}
void CharacterRunnerConfig::validateAddButton()
{
m_ui->addItem->setDisabled(m_ui->edit_alias->text().isEmpty() || m_ui->edit_hex->text().isEmpty());
}
void CharacterRunnerConfig::validateDeleteButton()
{
m_ui->deleteItem->setDisabled(!m_ui->list->selectedItems().count());
}
K_PLUGIN_FACTORY(CharacterRunnerConfigFactory,
......
/* Copyright 2010 Anton Kreuzkamp <akreuzkamp@web.de>
* Copyright 2020 Alexander Lohnau <alexander.lohnau@gmx.de>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
......@@ -20,8 +21,8 @@
#ifndef CHARRUNNERCONFIG_H
#define CHARRUNNERCONFIG_H
//Project-Includes
#include "ui_charrunner_config.h"
// KF
#include <KCModule>
......@@ -49,6 +50,8 @@ class CharacterRunnerConfig : public KCModule
private Q_SLOTS:
void addItem();
void deleteItem();
void validateAddButton();
void validateDeleteButton();
private:
CharacterRunnerConfigForm* m_ui;
......
......@@ -103,6 +103,9 @@
<property name="text">
<string>Add Item</string>
</property>
<property name="enabled">
<bool>false</bool>
</property>
</widget>
</item>
<item>
......@@ -110,6 +113,9 @@
<property name="text">
<string>Delete Item</string>
</property>
<property name="enabled">
<bool>false</bool>
</property>
</widget>
</item>
<item>
......
/* Copyright 2020 Alexander Lohnau <alexander.lohnau@gmx.de>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) version 3, or any
* later version accepted by the membership of KDE e.V. (or its
* successor approved by the membership of KDE e.V.), which shall
* act as a proxy defined in Section 6 of version 3 of the license.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef CONFIG_KEYS_H
#define CONFIG_KEYS_H
static const char CONFIG_TRIGGERWORD[] = "triggerWord";
static const char CONFIG_ALIASES[] = "aliases";
static const char CONFIG_CODES[] = "codes";
static constexpr QStringView DEFAULT_TRIGGERWORD = u"#";
#endif //CONFIG_KEYS_H
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment