Alkimia API

alknewstuffengine.cpp
1/*
2 SPDX-FileCopyrightText: 2024 Ralf Habacker ralf.habacker @freenet.de
3
4 This file is part of libalkimia.
5
6 SPDX-License-Identifier: LGPL-2.1-or-later
7*/
8
9#include "alknewstuffengine.h"
10#include "alknewstuffentry_p.h"
11
12#include "alkdebug.h"
13
14#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
15#include <KNSCore/Cache>
16#include <KNSCore/EngineBase>
17#include <KNSCore/Provider>
18#include <KNSCore/ResultsStream>
19#elif QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
20#include <KNSCore/Cache>
21#include <knewstuff_version.h>
22#include <knscore/engine.h>
23#else
24#include <knewstuff3/core/cache.h>
25#include <knewstuff3/downloadmanager.h>
26#define KNEWSTUFF_VERSION 0
27#endif
28
29#include <QEventLoop>
30#include <QPointer>
31#include <QWidget>
32
33class AlkNewStuffEngine::Private : public QObject
34{
36public:
38#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
40 #define KNS3 KNSCore
42 bool m_providersLoaded{false};
43 bool m_wantUpdates{false};
44#elif QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
47 bool m_providersLoaded{false};
48 bool m_wantUpdates{false};
49#else
51 Cache* m_cache;
52#endif
53 QEventLoop m_loop;
54
55 explicit Private(AlkNewStuffEngine *parent);
56 ~Private();
57
58 bool init(const QString &configFile);
59 void checkForUpdates();
60
61 const AlkNewStuffEntryList installedEntries();
62
63public Q_SLOTS:
64 void slotUpdatesAvailable(const KNS3::Entry::List &entries);
65};
66
67AlkNewStuffEngine::Private::Private(AlkNewStuffEngine *parent)
68 : q(parent)
69 , m_engine(nullptr)
70 , m_cache(nullptr)
71{
72}
73
74AlkNewStuffEngine::Private::~Private()
75{
76 delete m_engine;
77#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
78 delete m_cache;
79#endif
80}
81
82bool AlkNewStuffEngine::Private::init(const QString &configFile)
83{
84 bool state = false;
85#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
86 m_engine = new KNSCore::EngineBase(this);
87 state = m_engine->init(configFile);
88 if (!state)
89 return false;
90 m_cache = m_engine->cache();
91 connect(m_engine, &KNSCore::EngineBase::signalProvidersLoaded, this, [this]()
92 {
93 alkDebug() << "providers loaded";
94 m_providersLoaded = true;
95 if (m_wantUpdates) {
97 }
98 });
99#elif KNEWSTUFF_VERSION >= QT_VERSION_CHECK(5, 0, 0)
100 m_engine = new KNSCore::Engine(this);
101 state = m_engine->init(configFile);
102 if (!state)
103 return false;
104 m_cache = m_engine->cache();
105
106 q->connect(m_engine, &KNSCore::Engine::signalErrorCode, q, [](const KNSCore::ErrorCode &, const QString &message, const QVariant &) {
107 alkDebug() << message;
108 });
109
110 connect(m_engine, &KNSCore::Engine::signalProvidersLoaded, this, [this]()
111 {
112 alkDebug() << "providers loaded";
113 m_providersLoaded = true;
114 m_engine->reloadEntries();
115 alkDebug() << "cache" << m_engine->cache() << m_engine->cache()->registry();
116 if (m_wantUpdates)
117 m_engine->checkForUpdates();
118 });
119
120 connect(m_engine, &KNSCore::Engine::signalUpdateableEntriesLoaded, this, [this](const KNSCore::EntryInternal::List &entries)
121 {
122 alkDebug() << entries.size() << "updates loaded";
123 AlkNewStuffEntryList updateEntries;
124 toAlkEntryList(updateEntries, entries);
125 alkDebug() << updateEntries;
126 Q_EMIT q->updatesAvailable(updateEntries);
127 });
128#else
129 m_engine = new KNS3::DownloadManager(configFile, this);
130 QFileInfo f(configFile);
131 m_cache = new Cache(f.baseName());
132 m_cache->readRegistry();
133 // no chance get the state
134 state = true;
135
136 connect(m_engine, SIGNAL(searchResult(KNS3::Entry::List)), this,
137 SLOT(slotUpdatesAvailable(KNS3::Entry::List)));
138#endif
139 return state;
140}
141
142void AlkNewStuffEngine::Private::checkForUpdates()
143{
144#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
145 alkDebug() << "FIXME Qt6: no checkforUpdates() - how to proceed ?";
146#elif QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
147 if (m_providersLoaded && !m_wantUpdates) {
148 m_engine->checkForUpdates();
149 } else
150 m_wantUpdates = true;
151#else
152 m_engine->checkForUpdates();
153#endif
154}
155
156const AlkNewStuffEntryList AlkNewStuffEngine::Private::installedEntries()
157{
159 if (m_cache)
160 toAlkEntryList(result, m_cache->registry());
161
162 alkDebug() << result;
163 return result;
164}
165
166void AlkNewStuffEngine::Private::slotUpdatesAvailable(const KNS3::Entry::List &entries)
167{
168#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
169 Q_UNUSED(entries);
170#else
171 alkDebug() << entries.size() << "updates loaded";
172 AlkNewStuffEntryList updateEntries;
173 toAlkEntryList(updateEntries, entries);
174 alkDebug() << entries;
175
176 Q_EMIT q->updatesAvailable(updateEntries);
177#endif
178}
179
180AlkNewStuffEngine::AlkNewStuffEngine(QObject *parent)
181 : QObject{parent}
182 , d(new Private(this))
183{
184}
185
186bool AlkNewStuffEngine::init(const QString &configFile)
187{
188 bool result = d->init(configFile);
189 return result;
190}
191
193{
194 d->checkForUpdates();
195}
196
197AlkNewStuffEntryList AlkNewStuffEngine::installedEntries() const
198{
199 return d->installedEntries();
200}
201
203{
204 d->m_cache->readRegistry();
205}
206
207const char *toString(AlkNewStuffEntry::Status status)
208{
209 switch(status) {
210 case AlkNewStuffEntry::Invalid: return "Invalid";
211 case AlkNewStuffEntry::Downloadable: return "Downloadable";
212 case AlkNewStuffEntry::Installed: return "Installed";
213 case AlkNewStuffEntry::Updateable: return "Updateable";
214 case AlkNewStuffEntry::Deleted: return "Deleted";
215 case AlkNewStuffEntry::Installing: return "Installing";
216 case AlkNewStuffEntry::Updating: return "Updating";
217 }
218 return "";
219}
220
221#include "alknewstuffengine.moc"
Wrapper for debug output.
Platform independent wrapper for the new stuff engine.
void reload()
Reload installed entries.
bool init(const QString &configFile)
Initialization of the new stuff engine.
void checkForUpdates()
Start check for updates and return Update results are obtained using the updatesAvailable() signal.
Q_SCRIPTABLE CaptureState status()
Q_EMITQ_EMIT
Q_OBJECTQ_OBJECT
Q_SLOTSQ_SLOTS
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:01:13 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.