File indexing completed on 2024-05-05 17:33:20

0001 /*
0002  *   SPDX-FileCopyrightText: 2012 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
0003  *
0004  *   SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #include "AbstractResourcesBackend.h"
0008 #include "Category/Category.h"
0009 #include "libdiscover_debug.h"
0010 #include <KLocalizedString>
0011 #include <QHash>
0012 #include <QMetaObject>
0013 #include <QMetaProperty>
0014 #include <QTimer>
0015 
0016 QDebug operator<<(QDebug debug, const AbstractResourcesBackend::Filters &filters)
0017 {
0018     QDebugStateSaver saver(debug);
0019     debug.nospace() << "Filters(";
0020     if (filters.category)
0021         debug.nospace() << "category: " << filters.category << ',';
0022     if (filters.state)
0023         debug.nospace() << "state: " << filters.state << ',';
0024     if (!filters.mimetype.isEmpty())
0025         debug.nospace() << "mimetype: " << filters.mimetype << ',';
0026     if (!filters.search.isEmpty())
0027         debug.nospace() << "search: " << filters.search << ',';
0028     if (!filters.extends.isEmpty())
0029         debug.nospace() << "extends:" << filters.extends << ',';
0030     if (!filters.origin.isEmpty())
0031         debug.nospace() << "origin:" << filters.origin << ',';
0032     if (!filters.resourceUrl.isEmpty())
0033         debug.nospace() << "resourceUrl:" << filters.resourceUrl << ',';
0034     debug.nospace() << ')';
0035 
0036     return debug;
0037 }
0038 
0039 ResultsStream::ResultsStream(const QString &objectName, const QVector<AbstractResource *> &resources)
0040     : ResultsStream(objectName)
0041 {
0042     Q_ASSERT(!resources.contains(nullptr));
0043     QTimer::singleShot(0, this, [resources, this]() {
0044         if (!resources.isEmpty())
0045             Q_EMIT resourcesFound(resources);
0046         finish();
0047     });
0048 }
0049 
0050 ResultsStream::ResultsStream(const QString &objectName)
0051 {
0052     setObjectName(objectName);
0053     QTimer::singleShot(5000, this, [objectName]() {
0054         qCDebug(LIBDISCOVER_LOG) << "stream took really long" << objectName;
0055     });
0056 }
0057 
0058 ResultsStream::~ResultsStream()
0059 {
0060 }
0061 
0062 void ResultsStream::finish()
0063 {
0064     deleteLater();
0065 }
0066 
0067 AbstractResourcesBackend::AbstractResourcesBackend(QObject *parent)
0068     : QObject(parent)
0069 {
0070     QTimer *fetchingChangedTimer = new QTimer(this);
0071     fetchingChangedTimer->setInterval(3000);
0072     fetchingChangedTimer->setSingleShot(true);
0073     connect(fetchingChangedTimer, &QTimer::timeout, this, [this] {
0074         qDebug() << "took really long to fetch" << this;
0075     });
0076 
0077     connect(this, &AbstractResourcesBackend::fetchingChanged, this, [this, fetchingChangedTimer] {
0078         // Q_ASSERT(isFetching() != fetchingChangedTimer->isActive());
0079         if (isFetching())
0080             fetchingChangedTimer->start();
0081         else
0082             fetchingChangedTimer->stop();
0083 
0084         Q_EMIT fetchingUpdatesProgressChanged();
0085     });
0086 }
0087 
0088 Transaction *AbstractResourcesBackend::installApplication(AbstractResource *app)
0089 {
0090     return installApplication(app, AddonList());
0091 }
0092 
0093 void AbstractResourcesBackend::setName(const QString &name)
0094 {
0095     m_name = name;
0096 }
0097 
0098 QString AbstractResourcesBackend::name() const
0099 {
0100     return m_name;
0101 }
0102 
0103 void AbstractResourcesBackend::emitRatingsReady()
0104 {
0105     Q_EMIT allDataChanged({"rating", "ratingPoints", "ratingCount", "sortableRating"});
0106 }
0107 
0108 bool AbstractResourcesBackend::Filters::shouldFilter(AbstractResource *res) const
0109 {
0110     Q_ASSERT(res);
0111 
0112     if (!extends.isEmpty() && !res->extends().contains(extends)) {
0113         return false;
0114     }
0115 
0116     if (!origin.isEmpty() && res->origin() != origin) {
0117         return false;
0118     }
0119 
0120     if (filterMinimumState ? (res->state() < state) : (res->state() != state)) {
0121         return false;
0122     }
0123 
0124     if (!mimetype.isEmpty() && !res->mimetypes().contains(mimetype)) {
0125         return false;
0126     }
0127 
0128     return !category || res->categoryMatches(category);
0129 }
0130 
0131 void AbstractResourcesBackend::Filters::filterJustInCase(QVector<AbstractResource *> &input) const
0132 {
0133     for (auto it = input.begin(); it != input.end();) {
0134         if (shouldFilter(*it))
0135             ++it;
0136         else
0137             it = input.erase(it);
0138     }
0139 }
0140 
0141 QStringList AbstractResourcesBackend::extends() const
0142 {
0143     return {};
0144 }
0145 
0146 int AbstractResourcesBackend::fetchingUpdatesProgress() const
0147 {
0148     return isFetching() ? 42 : 100;
0149 }
0150 
0151 InlineMessage *AbstractResourcesBackend::explainDysfunction() const
0152 {
0153     return new InlineMessage(InlineMessage::Error, QStringLiteral("network-disconnect"), i18n("Please verify Internet connectivity"));
0154 }