KNewStuff

quickengine.cpp
1 /*
2  SPDX-FileCopyrightText: 2016 Dan Leinir Turthra Jensen <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5 */
6 
7 #include "quickengine.h"
8 
9 #include <KAuthorized>
10 #include <KLocalizedString>
11 
12 #include "categoriesmodel.h"
13 #include "quickquestionlistener.h"
14 
15 #include "engine.h"
16 
17 class Engine::Private
18 {
19 public:
20  Private()
21  : engine(nullptr)
22  , categoriesModel(nullptr)
23  {}
24  KNSCore::Engine *engine;
25  bool isLoading{false};
26  CategoriesModel *categoriesModel;
27  QString configFile;
28 
29  KNSCore::EntryInternal::List changedEntries;
31  {
32  KNSCore::EntryWrapper *entry{nullptr};
33  if (property) {
34  Private* d = static_cast<Engine::Private*>(property->data);
35  if (d) {
36  if (i >= 0 && i < d->changedEntries.count()) {
37  // Lifetime management for these objects should be done by the consumer,
38  // but are also parented for auto-delete on application shutdown
39  entry = new KNSCore::EntryWrapper(d->changedEntries[i], property->object);
40  }
41  }
42  }
43  return entry;
44  }
45  static int getChangedEntriesCount(QQmlListProperty<KNSCore::EntryWrapper>* property)
46  {
47  int count{0};
48  if (property) {
49  Private* d = static_cast<Engine::Private*>(property->data);
50  if (d) {
51  count = d->changedEntries.count();
52  }
53  }
54  return count;
55  }
56 };
57 
58 Engine::Engine(QObject *parent)
59  : QObject(parent)
60  , d(new Private)
61 {
62 }
63 
65 {
66  delete d;
67 }
68 
69 bool Engine::allowedByKiosk() const
70 {
71  return KAuthorized::authorize(QStringLiteral("ghns"));
72 }
73 
74 QString Engine::configFile() const
75 {
76  return d->configFile;
77 }
78 
79 void Engine::setConfigFile(const QString &newFile)
80 {
81  if (d->configFile != newFile) {
82  d->isLoading = true;
83  Q_EMIT isLoadingChanged();
84  d->configFile = newFile;
85  Q_EMIT configFileChanged();
86 
87  if (allowedByKiosk()) {
88  if (!d->engine) {
89  d->engine = new KNSCore::Engine(this);
90  connect(d->engine, &KNSCore::Engine::signalProvidersLoaded, this, [=](){
91  d->isLoading = false;
92  Q_EMIT isLoadingChanged();
93  });
94  connect(d->engine, &KNSCore::Engine::signalMessage, this, &Engine::message);
95  connect(d->engine, &KNSCore::Engine::busyStateChanged, this, [this]() {
96  if (!d->engine->busyState()) {
97  idleMessage(QString());
98  } else {
99  busyMessage(d->engine->busyMessage());
100  }
101  });
102  connect(d->engine, &KNSCore::Engine::signalErrorCode, this, [=](const KNSCore::ErrorCode &errorCode, const QString &message, const QVariant &/*metadata*/) {
103  if (errorCode == KNSCore::ProviderError) {
104  // This means loading the providers file failed entirely and we cannot complete the
105  // initialisation. It also means the engine is done loading, but that nothing will
106  // work, and we need to inform the user of this.
107  d->isLoading = false;
108  Q_EMIT isLoadingChanged();
109  }
110  Q_EMIT errorMessage(message);
111  });
112  connect(d->engine, &KNSCore::Engine::signalEntryEvent,
113  this, [this](const KNSCore::EntryInternal &entry, KNSCore::EntryInternal::EntryEvent event) {
114  if (event != KNSCore::EntryInternal::StatusChangedEvent) {
115  return;
116  }
117  if (d->changedEntries.contains(entry)) {
118  d->changedEntries.removeAll(entry);
119  }
120  d->changedEntries << entry;
121  Q_EMIT changedEntriesChanged();
122  });
123  Q_EMIT engineChanged();
124  KNewStuffQuick::QuickQuestionListener::instance();
125  d->categoriesModel = new CategoriesModel(this);
126  Q_EMIT categoriesChanged();
127  // And finally, let's just make sure we don't miss out the various things here getting changed
128  // In other words, when we're asked to reset the view, actually do that
129  connect(d->engine, &KNSCore::Engine::signalResetView, this, &Engine::categoriesFilterChanged);
130  connect(d->engine, &KNSCore::Engine::signalResetView, this, &Engine::filterChanged);
131  connect(d->engine, &KNSCore::Engine::signalResetView, this, &Engine::sortOrderChanged);
132  connect(d->engine, &KNSCore::Engine::signalResetView, this, &Engine::searchTermChanged);
133  Q_EMIT categoriesFilterChanged();
134  Q_EMIT filterChanged();
135  Q_EMIT sortOrderChanged();
136  Q_EMIT searchTermChanged();
137  }
138  d->engine->init(d->configFile);
139  Q_EMIT engineInitialized();
140  } else {
141  // This is not an error message in the proper sense, and the message is not intended to look like an error (as there is really
142  // nothing the user can do to fix it, and we just tell them so they're not wondering what's wrong)
143  Q_EMIT message(i18nc("An informational message which is shown to inform the user they are not authorized to use GetHotNewStuff functionality", "You are not authorized to Get Hot New Stuff. If you think this is in error, please contact the person in charge of your permissions."));
144  }
145  }
146 }
147 
148 QObject *Engine::engine() const
149 {
150  return d->engine;
151 }
152 
153 bool Engine::isLoading() const
154 {
155  return d->isLoading;
156 }
157 
158 bool Engine::hasAdoptionCommand() const
159 {
160  if (d->engine) {
161  return d->engine->hasAdoptionCommand();
162  }
163  return false;
164 }
165 
166 QString Engine::name() const
167 {
168  if (d->engine) {
169  return d->engine->name();
170  }
171  return QString{};
172 }
173 
175 {
176  return d->categoriesModel;
177 }
178 
180 {
181  if (d->engine) {
182  return d->engine->categoriesFilter();
183  }
184  return QStringList{};
185 }
186 
187 void Engine::setCategoriesFilter(const QStringList &newCategoriesFilter)
188 {
189  if (d->engine) {
190  // This ensures that if we somehow end up with any empty entries (such as the default
191  // option in the categories dropdowns), our list will remain empty.
192  QStringList filter{newCategoriesFilter};
193  filter.removeAll({});
194  if (d->engine->categoriesFilter() != filter) {
195  d->engine->setCategoriesFilter(filter);
196  Q_EMIT categoriesFilterChanged();
197  }
198  }
199 }
200 
201 void Engine::resetCategoriesFilter()
202 {
203  if (d->engine) {
204  d->engine->setCategoriesFilter(d->engine->categories());
205  }
206 }
207 
208 int Engine::filter() const
209 {
210  if (d->engine) {
211  d->engine->filter();
212  }
213  return 0;
214 }
215 
216 void Engine::setFilter(int newFilter)
217 {
218  if (d->engine && d->engine->filter() != newFilter) {
219  d->engine->setFilter(static_cast<KNSCore::Provider::Filter>(newFilter));
220  Q_EMIT filterChanged();
221  }
222 }
223 
224 int Engine::sortOrder() const
225 {
226  if (d->engine) {
227  return d->engine->sortMode();
228  }
229  return 0;
230 }
231 
232 void Engine::setSortOrder(int newSortOrder)
233 {
234  if (d->engine && d->engine->sortMode() != newSortOrder) {
235  d->engine->setSortMode(static_cast<KNSCore::Provider::SortMode>(newSortOrder));
236  Q_EMIT sortOrderChanged();
237  }
238 }
239 
241 {
242  if (d->engine) {
243  return d->engine->searchTerm();
244  }
245  return QString{};
246 }
247 
248 void Engine::setSearchTerm(const QString &newSearchTerm)
249 {
250  if (d->engine && d->engine->searchTerm() != newSearchTerm) {
251  d->engine->setSearchTerm(newSearchTerm);
252  Q_EMIT searchTermChanged();
253  }
254 }
255 
256 void Engine::resetSearchTerm()
257 {
258  setSearchTerm(QString{});
259 }
260 
261 QQmlListProperty<KNSCore::EntryWrapper> Engine::changedEntries()
262 {
263  return QQmlListProperty<KNSCore::EntryWrapper>(this, d, &Private::getChangedEntriesCount, &Private::getChangedEntry);
264 }
265 
266 int Engine::changedEntriesCount() const
267 {
268  return d->changedEntries.count();
269 }
270 
271 void Engine::resetChangedEntries()
272 {
273  d->changedEntries.clear();
274  Q_EMIT changedEntriesChanged();
275 }
A model which shows the categories found in an Engine.
QStringList categories() const
The list of the server-side names of the categories handled by this engine instance.
Definition: engine.cpp:289
void setSearchTerm(const QString &searchString)
Sets a string search term.
Definition: engine.cpp:545
bool hasAdoptionCommand() const
Whether or not an adoption command exists for this engine.
Definition: engine.cpp:934
ErrorCode
An enumeration of specific error conditions which might occur and which users of KNewStuff would want...
Definition: errorcode.h:25
virtual bool event(QEvent *e)
Q_SIGNAL void busyStateChanged()
Signal gets emitted when the busy state changes.
int count(const T &value) const const
QVariant property(const char *name) const const
void setCategoriesFilter(const QStringList &categories)
Set the categories that will be included in searches.
Definition: engine.cpp:497
QString i18nc(const char *context, const char *text, const TYPE &arg...)
~Engine()
Destructor.
Definition: engine.cpp:176
QString searchTerm() const
The search term for the current search (empty if none is set)
Definition: engine.cpp:557
void signalMessage(const QString &message)
Indicates a message to be added to the ui&#39;s log, or sent to a messagebox.
Provider::Filter filter() const
The result filter set on the current request.
Definition: engine.cpp:526
void setFilter(Provider::Filter filter)
Set a filter for results (defaults to none), which will allow you to show only installed entries...
Definition: engine.cpp:517
QFuture< void > filter(Sequence &sequence, KeepFunctor filterFunction)
KCALUTILS_EXPORT QString errorMessage(const KCalendarCore::Exception &exception)
if(recurs()&&!first)
bool isLoading() const
Whether or not the engine is performing its initial loading operations.
QString name() const
The name as defined by the knsrc file.
Definition: engine.cpp:284
KNewStuff data entry container.
Definition: entryinternal.h:49
KCONFIGCORE_EXPORT bool authorize(const QString &action)
KNewStuff engine.
Definition: engine.h:59
QStringList categoriesFilter() const
The list of categories searches will actually show results from.
Definition: engine.cpp:294
TODO KF6 see above (in short, make this class irrelevant so it can be removed)
Definition: entrywrapper.h:35
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const const
void signalErrorCode(const KNSCore::ErrorCode &errorCode, const QString &message, const QVariant &metadata)
Fires in the case of any critical or serious errors, such as network or API problems.
Q_EMITQ_EMIT
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Jan 18 2021 22:43:50 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.