File indexing completed on 2024-05-05 17:34:05

0001 /*
0002  *   SPDX-FileCopyrightText: 2008 Montel Laurent <montel@kde.org>
0003  *   based on kate session from sebas
0004  *   SPDX-FileCopyrightText: 2020 Alexander Lohnau <alexander.lohnau@gmx.de>
0005  *
0006  *   SPDX-License-Identifier: LGPL-2.0-or-later
0007  */
0008 
0009 #include "konsoleprofiles.h"
0010 
0011 // KF
0012 #include <KConfig>
0013 #include <KConfigGroup>
0014 #include <KDirWatch>
0015 #include <KLocalizedString>
0016 #include <QDir>
0017 #include <QFileInfo>
0018 #include <QStandardPaths>
0019 
0020 KonsoleProfiles::KonsoleProfiles(QObject *parent, const KPluginMetaData &metaData, const QVariantList &args)
0021     : AbstractRunner(parent, metaData, args)
0022 {
0023     setObjectName(QStringLiteral("Konsole Profiles"));
0024 
0025     RunnerSyntax s(QStringLiteral(":q:"), i18n("Finds Konsole profiles matching :q:."));
0026     s.addExampleQuery(QStringLiteral("konsole :q:"));
0027     addSyntax(s);
0028     addSyntax(RunnerSyntax(QStringLiteral("konsole"), i18n("Lists all the Konsole profiles in your account.")));
0029     setMinLetterCount(3);
0030 }
0031 
0032 void KonsoleProfiles::match(RunnerContext &context)
0033 {
0034     QString term = context.query();
0035     term = term.remove(m_triggerWord).simplified();
0036     for (int i = 0, count = m_model->rowCount(); i < count; ++i) {
0037         QModelIndex idx = m_model->index(i);
0038         const QString name = idx.data(ProfilesModel::NameRole).toString();
0039         if (name.contains(term, Qt::CaseInsensitive)) {
0040             const QString profileIdentifier = idx.data(ProfilesModel::ProfileIdentifierRole).toString();
0041             QueryMatch match(this);
0042             match.setType(QueryMatch::PossibleMatch);
0043             match.setIconName(idx.data(ProfilesModel::IconNameRole).toString());
0044             match.setData(profileIdentifier);
0045             match.setText(QStringLiteral("Konsole: ") + name);
0046             match.setRelevance((float)term.length() / (float)name.length());
0047             context.addMatch(match);
0048         }
0049     }
0050 }
0051 
0052 void KonsoleProfiles::run(const RunnerContext &context, const QueryMatch &match)
0053 {
0054     Q_UNUSED(context)
0055     const QString profile = match.data().toString();
0056     m_model->openProfile(profile);
0057 }
0058 
0059 void KonsoleProfiles::init()
0060 {
0061     // Only create this in the correct thread. Inside we use KDirWatch which is thread sensitive.
0062     m_model = new ProfilesModel(this);
0063     m_model->setAppName(m_triggerWord);
0064 }
0065 
0066 K_PLUGIN_CLASS_WITH_JSON(KonsoleProfiles, "plasma-runner-konsoleprofiles.json")
0067 
0068 #include "konsoleprofiles.moc"