Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

Commit 2029e2b9 authored by Aleix Pol Gonzalez's avatar Aleix Pol Gonzalez 🐧

KNS: Make it possible to request resources as the user needs them

This way we won't be getting an overwhelming list of resources at once.
parent 6d4eaf8a
......@@ -227,27 +227,31 @@ void KNSBackend::receivedEntries(const KNSCore::EntryInternal::List& entries)
const auto resources = kTransform<QVector<AbstractResource*>>(entries, [this](const KNSCore::EntryInternal& entry){ return resourceForEntry(entry); });
if (!resources.isEmpty()) {
Q_EMIT receivedResources(resources);
}
if(resources.isEmpty()) {
} else {
Q_EMIT searchFinished();
Q_EMIT availableForQueries();
setFetching(false);
return;
}
// qDebug() << "received" << objectName() << this << m_resourcesByName.count();
if (!m_responsePending && !m_onePage) {
// We _have_ to set this first. If we do not, we may run into a situation where the
// data request will conclude immediately, causing m_responsePending to remain true
// for perpetuity as the slots will be called before the function returns.
m_responsePending = true;
m_engine->requestMoreData();
} else {
if (m_onePage) {
Q_EMIT availableForQueries();
setFetching(false);
}
}
void KNSBackend::fetchMore()
{
if (m_responsePending)
return;
// We _have_ to set this first. If we do not, we may run into a situation where the
// data request will conclude immediately, causing m_responsePending to remain true
// for perpetuity as the slots will be called before the function returns.
m_responsePending = true;
m_engine->requestMoreData();
}
void KNSBackend::statusChanged(const KNSCore::EntryInternal& entry)
{
resourceForEntry(entry);
......@@ -387,6 +391,7 @@ void KNSBackend::searchStream(ResultsStream* stream, const QString &searchText)
m_onePage = false;
m_responsePending = true;
connect(stream, &ResultsStream::fetchMore, this, &KNSBackend::fetchMore);
connect(this, &KNSBackend::receivedResources, stream, &ResultsStream::resourcesFound);
connect(this, &KNSBackend::searchFinished, stream, &ResultsStream::finish);
connect(this, &KNSBackend::startingSearch, stream, &ResultsStream::finish);
......
......@@ -82,7 +82,8 @@ private:
void setFetching(bool f);
void markInvalid(const QString &message);
void searchStream(ResultsStream* stream, const QString &searchText);
void fetchMore();
bool m_onePage = false;
bool m_responsePending = false;
bool m_fetching;
......
......@@ -63,7 +63,10 @@ QVector<AbstractResource*> KNSBackendTest::getResources(ResultsStream* stream, b
Q_ASSERT(stream->objectName() != QLatin1String("KNS-void"));
QSignalSpy spyResources(stream, &ResultsStream::destroyed);
QVector<AbstractResource*> resources;
connect(stream, &ResultsStream::resourcesFound, this, [&resources](const QVector<AbstractResource*>& res) { resources += res; });
connect(stream, &ResultsStream::resourcesFound, this, [&resources, stream](const QVector<AbstractResource*>& res) {
resources += res;
stream->fetchMore();
});
Q_ASSERT(spyResources.wait(10000));
Q_ASSERT(!resources.isEmpty() || canBeEmpty);
return resources;
......
......@@ -49,6 +49,7 @@ class DISCOVERCOMMON_EXPORT ResultsStream : public QObject
Q_SIGNALS:
void resourcesFound(const QVector<AbstractResource*>& resources);
void fetchMore();
};
/**
......
......@@ -279,6 +279,7 @@ AggregatedResultsStream::AggregatedResultsStream(const QSet<ResultsStream*>& str
for (auto stream: streams) {
connect(stream, &ResultsStream::resourcesFound, this, &AggregatedResultsStream::addResults);
connect(stream, &QObject::destroyed, this, &AggregatedResultsStream::destruction);
connect(this, &ResultsStream::fetchMore, stream, &ResultsStream::fetchMore);
m_streams << stream;
}
......
......@@ -578,3 +578,17 @@ AbstractResource * ResourcesProxyModel::resourceAt(int row) const
{
return m_displayedResources[row];
}
bool ResourcesProxyModel::canFetchMore(const QModelIndex& parent) const
{
Q_ASSERT(!parent.isValid());
return m_currentStream;
}
void ResourcesProxyModel::fetchMore(const QModelIndex& parent)
{
Q_ASSERT(!parent.isValid());
if (!m_currentStream)
return;
m_currentStream->fetchMore();
}
......@@ -127,6 +127,9 @@ public:
Q_SCRIPTABLE void invalidateFilter();
void invalidateSorting();
bool canFetchMore(const QModelIndex & parent) const override;
void fetchMore(const QModelIndex & parent) override;
void classBegin() override {}
void componentComplete() override;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment