File indexing completed on 2024-04-28 16:44:42

0001 /* SPDX-FileCopyrightText: 2010 Anton Kreuzkamp <akreuzkamp@web.de>
0002  * SPDX-FileCopyrightText: 2020 Alexander Lohnau <alexander.lohnau@gmx.de>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005  */
0006 
0007 #include "charrunner.h"
0008 #include "config_keys.h"
0009 
0010 // KF
0011 #include <KConfigGroup>
0012 #include <KLocalizedString>
0013 #include <KRunner/QueryMatch>
0014 // Qt
0015 #include <QClipboard>
0016 #include <QDebug>
0017 #include <QGuiApplication>
0018 
0019 CharacterRunner::CharacterRunner(QObject *parent, const KPluginMetaData &metaData, const QVariantList &args)
0020     : AbstractRunner(parent, metaData, args)
0021 {
0022     setObjectName(QStringLiteral("CharacterRunner"));
0023 }
0024 
0025 CharacterRunner::~CharacterRunner()
0026 {
0027 }
0028 
0029 void CharacterRunner::reloadConfiguration()
0030 {
0031     const KConfigGroup grp = config();
0032     m_triggerWord = grp.readEntry(CONFIG_TRIGGERWORD, DEFAULT_TRIGGERWORD.toString());
0033     m_aliases = grp.readEntry(CONFIG_ALIASES, QStringList());
0034     m_codes = grp.readEntry(CONFIG_CODES, QStringList());
0035     if (m_codes.size() != m_aliases.size()) {
0036         m_aliases.clear();
0037         m_codes.clear();
0038         qWarning() << "Config entries for alias list and code list have different sizes, ignoring all.";
0039     }
0040 
0041     addSyntax(RunnerSyntax(m_triggerWord + QStringLiteral(":q:"), i18n("Creates Characters from :q: if it is a hexadecimal code or defined alias.")));
0042     setTriggerWords({m_triggerWord});
0043     setMinLetterCount(minLetterCount() + 1);
0044 }
0045 
0046 void CharacterRunner::match(RunnerContext &context)
0047 {
0048     QString term = context.query().remove(QLatin1Char(' '));
0049     term = term.remove(0, m_triggerWord.length()); // remove the triggerword
0050 
0051     // replace aliases by their hex.-code
0052     if (m_aliases.contains(term)) {
0053         term = m_codes[m_aliases.indexOf(term)];
0054     }
0055 
0056     bool ok;
0057     int hex = term.toInt(&ok, 16); // convert query into int
0058     if (!ok) {
0059         return;
0060     }
0061 
0062     // make special character out of the hex.-code
0063     const QString specChar = QChar(hex);
0064     QueryMatch match(this);
0065     match.setType(QueryMatch::ExactMatch);
0066     match.setIconName(QStringLiteral("accessories-character-map"));
0067     match.setText(specChar);
0068     match.setData(specChar);
0069     context.addMatch(match);
0070 }
0071 
0072 void CharacterRunner::run(const RunnerContext &context, const QueryMatch &match)
0073 {
0074     Q_UNUSED(context)
0075     QGuiApplication::clipboard()->setText(match.data().toString());
0076 }
0077 
0078 K_PLUGIN_CLASS_WITH_JSON(CharacterRunner, "plasma-runner-character.json")
0079 
0080 #include "charrunner.moc"