File indexing completed on 2024-04-28 15:28:58

0001 /*
0002     knewstuff3/provider.cpp
0003     SPDX-FileCopyrightText: 2002 Cornelius Schumacher <schumacher@kde.org>
0004     SPDX-FileCopyrightText: 2003-2007 Josef Spillner <spillner@kde.org>
0005     SPDX-FileCopyrightText: 2009 Jeremy Whiting <jpwhiting@kde.org>
0006     SPDX-FileCopyrightText: 2009 Frederik Gladhorn <gladhorn@kde.org>
0007 
0008     SPDX-License-Identifier: LGPL-2.1-or-later
0009 */
0010 
0011 #include "provider.h"
0012 
0013 #include "xmlloader.h"
0014 
0015 #include <KLocalizedString>
0016 
0017 #include <QTimer>
0018 
0019 namespace KNSCore
0020 {
0021 // TODO KF6 BCI: Add a real d-pointer
0022 class ProviderPrivate
0023 {
0024 public:
0025     Provider *q;
0026     QStringList tagFilter;
0027     QStringList downloadTagFilter;
0028 
0029     QTimer *basicsThrottle{nullptr};
0030     QString version;
0031     QUrl website;
0032     QUrl host;
0033     QString contactEmail;
0034     bool supportsSsl{false};
0035     bool basicsGot{false};
0036     void updateOnFirstBasicsGet()
0037     {
0038         if (!basicsGot) {
0039             basicsGot = true;
0040             QTimer::singleShot(0, q, &Provider::loadBasics);
0041         }
0042     };
0043     void throttleBasics()
0044     {
0045         if (!basicsThrottle) {
0046             basicsThrottle = new QTimer(q);
0047             basicsThrottle->setInterval(0);
0048             basicsThrottle->setSingleShot(true);
0049             QObject::connect(basicsThrottle, &QTimer::timeout, q, &Provider::basicsLoaded);
0050         }
0051         basicsThrottle->start();
0052     }
0053 };
0054 typedef QHash<const Provider *, ProviderPrivate *> ProviderPrivateHash;
0055 Q_GLOBAL_STATIC(ProviderPrivateHash, d_func)
0056 
0057 static ProviderPrivate *d(const Provider *provider)
0058 {
0059     ProviderPrivate *ret = d_func()->value(provider);
0060     if (!ret) {
0061         ret = new ProviderPrivate;
0062         d_func()->insert(provider, ret);
0063     }
0064     return ret;
0065 }
0066 
0067 static void delete_d(const Provider *provider)
0068 {
0069     if (auto d = d_func()) {
0070         delete d->take(provider);
0071     }
0072 }
0073 
0074 QString Provider::SearchRequest::hashForRequest() const
0075 {
0076     return QString(QString::number((int)sortMode) + QLatin1Char(',') + searchTerm + QLatin1Char(',') + categories.join(QLatin1Char('-')) + QLatin1Char(',')
0077                    + QString::number(page) + QLatin1Char(',') + QString::number(pageSize));
0078 }
0079 
0080 Provider::Provider()
0081 {
0082     d(this)->q = this;
0083 }
0084 
0085 Provider::~Provider()
0086 {
0087     delete_d(this);
0088 }
0089 
0090 QString Provider::name() const
0091 {
0092     return mName;
0093 }
0094 
0095 QUrl Provider::icon() const
0096 {
0097     return mIcon;
0098 }
0099 
0100 void Provider::setTagFilter(const QStringList &tagFilter)
0101 {
0102     d(this)->tagFilter = tagFilter;
0103 }
0104 
0105 QStringList Provider::tagFilter() const
0106 {
0107     return d(this)->tagFilter;
0108 }
0109 
0110 void Provider::setDownloadTagFilter(const QStringList &downloadTagFilter)
0111 {
0112     d(this)->downloadTagFilter = downloadTagFilter;
0113 }
0114 
0115 QStringList Provider::downloadTagFilter() const
0116 {
0117     return d(this)->downloadTagFilter;
0118 }
0119 
0120 QDebug operator<<(QDebug dbg, const Provider::SearchRequest &search)
0121 {
0122     QDebugStateSaver saver(dbg);
0123     dbg.nospace();
0124     dbg << "Provider::SearchRequest(";
0125     dbg << "searchTerm: " << search.searchTerm << ',';
0126     dbg << "categories: " << search.categories << ',';
0127     dbg << "filter: " << search.filter << ',';
0128     dbg << "page: " << search.page << ',';
0129     dbg << "pageSize: " << search.pageSize;
0130     dbg << ')';
0131     return dbg;
0132 }
0133 
0134 QString Provider::version() const
0135 {
0136     d(this)->updateOnFirstBasicsGet();
0137     return d(this)->version;
0138 }
0139 
0140 void Provider::setVersion(const QString &version)
0141 {
0142     if (d(this)->version != version) {
0143         d(this)->version = version;
0144         d(this)->throttleBasics();
0145     }
0146 }
0147 
0148 QUrl Provider::website() const
0149 {
0150     d(this)->updateOnFirstBasicsGet();
0151     return d(this)->website;
0152 }
0153 
0154 void Provider::setWebsite(const QUrl &website)
0155 {
0156     if (d(this)->website != website) {
0157         d(this)->website = website;
0158         d(this)->throttleBasics();
0159     }
0160 }
0161 
0162 QUrl Provider::host() const
0163 {
0164     d(this)->updateOnFirstBasicsGet();
0165     return d(this)->host;
0166 }
0167 
0168 void Provider::setHost(const QUrl &host)
0169 {
0170     if (d(this)->host != host) {
0171         d(this)->host = host;
0172         d(this)->throttleBasics();
0173     }
0174 }
0175 
0176 QString Provider::contactEmail() const
0177 {
0178     d(this)->updateOnFirstBasicsGet();
0179     return d(this)->contactEmail;
0180 }
0181 
0182 void Provider::setContactEmail(const QString &contactEmail)
0183 {
0184     if (d(this)->contactEmail != contactEmail) {
0185         d(this)->contactEmail = contactEmail;
0186         d(this)->throttleBasics();
0187     }
0188 }
0189 
0190 bool Provider::supportsSsl() const
0191 {
0192     d(this)->updateOnFirstBasicsGet();
0193     return d(this)->supportsSsl;
0194 }
0195 
0196 void Provider::setSupportsSsl(bool supportsSsl)
0197 {
0198     if (d(this)->supportsSsl != supportsSsl) {
0199         d(this)->supportsSsl = supportsSsl;
0200         d(this)->throttleBasics();
0201     }
0202 }
0203 }
0204 
0205 #include "moc_provider.cpp"