KNewStuff

resultsstream.cpp
1/*
2 SPDX-FileCopyrightText: 2023 Aleix Pol Gonzalez <aleixpol@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.1-or-later
5*/
6
7#include "resultsstream.h"
8#include "enginebase_p.h"
9#include "knewstuffcore_debug.h"
10
11#include <QTimer>
12
13using namespace KNSCore;
14
15class KNSCore::ResultsStreamPrivate
16{
17public:
19 EngineBase const *engine;
21};
22
23ResultsStream::ResultsStream(const Provider::SearchRequest &request, EngineBase *base)
24 : d(new ResultsStreamPrivate{
25 .providers = base->d->providers.values(),
26 .engine = base,
27 .request = request,
28 })
29{
30 auto finished = [this](const KNSCore::Provider::SearchRequest &request, const KNSCore::Entry::List &entries) {
31 if (request != d->request) {
32 return;
33 }
34
35 if (d->providers.removeAll(qobject_cast<Provider *>(sender())) <= 0) {
36 qCDebug(KNEWSTUFFCORE) << "Request finished twice, check your provider" << sender() << d->engine << entries.size();
37 return;
38 }
39
40 if (entries.isEmpty() && d->providers.isEmpty()) {
41 finish();
42 }
43 if (!entries.isEmpty()) {
44 Q_EMIT entriesFound(entries);
45 }
46 };
47 auto failed = [this](const KNSCore::Provider::SearchRequest &request) {
48 if (request == d->request) {
49 finish();
50 }
51 };
52 for (const auto &provider : d->providers) {
53 connect(provider.data(), &Provider::loadingFinished, this, finished);
54 connect(provider.data(), &Provider::entryDetailsLoaded, this, [this](const KNSCore::Entry &entry) {
55 if (d->request.filter == KNSCore::Provider::ExactEntryId && d->request.searchTerm == entry.uniqueId()) {
56 if (entry.isValid()) {
57 Q_EMIT entriesFound({entry});
58 }
59 finish();
60 }
61 });
62 connect(provider.data(), &Provider::loadingFailed, this, failed);
63 }
64}
65
66ResultsStream::~ResultsStream() = default;
67
69{
70 if (d->request.filter != Provider::Installed) {
71 // when asking for installed entries, never use the cache
72 Entry::List cacheEntries = d->engine->cache()->requestFromCache(d->request);
73 if (!cacheEntries.isEmpty()) {
74 Q_EMIT entriesFound(cacheEntries);
75 return;
76 }
77 }
78
79 for (const QSharedPointer<KNSCore::Provider> &p : std::as_const(d->providers)) {
80 if (p->isInitialized()) {
81 QTimer::singleShot(0, this, [this, p] {
82 p->loadEntries(d->request);
83 });
84 } else {
85 connect(p.get(), &KNSCore::Provider::providerInitialized, this, [this, p] {
86 disconnect(p.get(), &KNSCore::Provider::providerInitialized, this, nullptr);
87 p->loadEntries(d->request);
88 });
89 }
90 }
91}
92
94{
95 d->request.page++;
96 fetch();
97}
98
99void ResultsStream::finish()
100{
101 Q_EMIT finished();
102 deleteLater();
103}
104
105#include "moc_resultsstream.cpp"
KNewStuff engine.
Definition enginebase.h:52
KNewStuff data entry container.
Definition entry.h:48
void fetch()
Issues the search, make sure all signals are connected before calling.
void fetchMore()
Increments the requested page and issues another search.
QCA_EXPORT ProviderList providers()
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void deleteLater()
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
used to keep track of a search
Definition provider.h:70
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 24 2024 11:54:38 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.