File indexing completed on 2024-05-12 09:31:46

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)
0020     : AbstractRunner(parent, metaData)
0021 {
0022 }
0023 
0024 CharacterRunner::~CharacterRunner()
0025 {
0026 }
0027 
0028 void CharacterRunner::reloadConfiguration()
0029 {
0030     const KConfigGroup grp = config();
0031     m_triggerWord = grp.readEntry(CONFIG_TRIGGERWORD, DEFAULT_TRIGGERWORD.toString());
0032     m_aliases = grp.readEntry(CONFIG_ALIASES, QStringList());
0033     m_codes = grp.readEntry(CONFIG_CODES, QStringList());
0034     if (m_codes.size() != m_aliases.size()) {
0035         m_aliases.clear();
0036         m_codes.clear();
0037         qWarning() << "Config entries for alias list and code list have different sizes, ignoring all.";
0038     }
0039 
0040     addSyntax(m_triggerWord + QStringLiteral(":q:"), i18n("Creates Characters from :q: if it is a hexadecimal code or defined alias."));
0041     setTriggerWords({m_triggerWord});
0042     setMinLetterCount(minLetterCount() + 1);
0043 }
0044 
0045 void CharacterRunner::match(RunnerContext &context)
0046 {
0047     QString term = context.query().remove(QLatin1Char(' '));
0048     term = term.remove(0, m_triggerWord.length()); // remove the triggerword
0049 
0050     // replace aliases by their hex.-code
0051     if (m_aliases.contains(term)) {
0052         term = m_codes[m_aliases.indexOf(term)];
0053     }
0054 
0055     bool ok;
0056     int hex = term.toInt(&ok, 16); // convert query into int
0057     if (!ok) {
0058         return;
0059     }
0060 
0061     // QChar asserts this.
0062     if (hex > 0xffff) {
0063         return;
0064     }
0065 
0066     // make special character out of the hex.-code
0067     const QString specChar = QChar(hex);
0068     QueryMatch match(this);
0069     match.setCategoryRelevance(QueryMatch::CategoryRelevance::Highest);
0070     match.setIconName(QStringLiteral("accessories-character-map"));
0071     match.setText(specChar);
0072     match.setData(specChar);
0073     context.addMatch(match);
0074 }
0075 
0076 void CharacterRunner::run(const RunnerContext & /*context*/, const QueryMatch &match)
0077 {
0078     QGuiApplication::clipboard()->setText(match.data().toString());
0079 }
0080 
0081 K_PLUGIN_CLASS_WITH_JSON(CharacterRunner, "plasma-runner-character.json")
0082 
0083 #include "charrunner.moc"