File indexing completed on 2024-04-28 15:29:45

0001 /*
0002     SPDX-FileCopyrightText: 2009 Aaron Seigo <aseigo@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "runnersyntax.h"
0008 
0009 #include <KLocalizedString>
0010 
0011 namespace Plasma
0012 {
0013 class RunnerSyntaxPrivate
0014 {
0015 public:
0016     RunnerSyntaxPrivate(const QStringList &_exampleQueries, const QString &_description)
0017         : description(_description)
0018     {
0019         for (const QString &query : _exampleQueries) {
0020             addExampleQuery(query);
0021         }
0022     }
0023 
0024     void addExampleQuery(const QString &s)
0025     {
0026         Q_ASSERT_X(!s.isEmpty(), "KRunner::RunnerSyntax", "Example queries must not be empty!");
0027         const QString termDesc(QLatin1Char('<') + termDescription + QLatin1Char('>'));
0028         exampleQueries.append(QString(s).replace(QStringLiteral(":q:"), termDesc));
0029     }
0030 
0031     QStringList exampleQueries;
0032     QString description;
0033     QString termDescription = i18n("search term");
0034 };
0035 
0036 RunnerSyntax::RunnerSyntax(const QString &exampleQuery, const QString &description)
0037     : d(new RunnerSyntaxPrivate({exampleQuery}, description))
0038 {
0039 }
0040 
0041 RunnerSyntax::RunnerSyntax(const QStringList &exampleQueries, const QString &description)
0042     : d(new RunnerSyntaxPrivate(exampleQueries, description))
0043 {
0044     Q_ASSERT_X(!exampleQueries.isEmpty(), "KRunner::RunnerSyntax", "Example queries must not be empty");
0045 }
0046 
0047 RunnerSyntax::RunnerSyntax(const RunnerSyntax &other)
0048     : d(new RunnerSyntaxPrivate(*other.d))
0049 {
0050 }
0051 
0052 RunnerSyntax::~RunnerSyntax() = default;
0053 
0054 RunnerSyntax &RunnerSyntax::operator=(const RunnerSyntax &rhs)
0055 {
0056     *d = *rhs.d;
0057     return *this;
0058 }
0059 
0060 #if KRUNNER_BUILD_DEPRECATED_SINCE(5, 106)
0061 void RunnerSyntax::addExampleQuery(const QString &exampleQuery)
0062 {
0063     d->addExampleQuery(exampleQuery);
0064 }
0065 #endif
0066 
0067 QStringList RunnerSyntax::exampleQueries() const
0068 {
0069     return d->exampleQueries;
0070 }
0071 
0072 #if KRUNNER_BUILD_DEPRECATED_SINCE(5, 76)
0073 QStringList RunnerSyntax::exampleQueriesWithTermDescription() const
0074 {
0075     QStringList queries;
0076     const QString termDesc(QLatin1Char('<') + searchTermDescription() + QLatin1Char('>'));
0077     for (QString query : std::as_const(d->exampleQueries)) {
0078         queries << query.replace(QStringLiteral(":q:"), termDesc);
0079     }
0080 
0081     return queries;
0082 }
0083 #endif
0084 
0085 #if KRUNNER_BUILD_DEPRECATED_SINCE(5, 106)
0086 void RunnerSyntax::setDescription(const QString &description)
0087 {
0088     d->description = description;
0089 }
0090 #endif
0091 
0092 QString RunnerSyntax::description() const
0093 {
0094 #if KRUNNER_BUILD_DEPRECATED_SINCE(5, 76)
0095     QString description = d->description;
0096     description.replace(QLatin1String(":q:"), QLatin1Char('<') + searchTermDescription() + QLatin1Char('>'));
0097     return description;
0098 #else
0099     return d->description;
0100 #endif
0101 }
0102 
0103 #if KRUNNER_BUILD_DEPRECATED_SINCE(5, 76)
0104 void RunnerSyntax::setSearchTermDescription(const QString &description)
0105 {
0106     d->termDescription = description;
0107 }
0108 #endif
0109 
0110 #if KRUNNER_BUILD_DEPRECATED_SINCE(5, 76)
0111 QString RunnerSyntax::searchTermDescription() const
0112 {
0113     return d->termDescription;
0114 }
0115 #endif
0116 
0117 } // Plasma namespace