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 "entrywrapper.h"
14 #include "quickquestionlistener.h"
15 
16 #include "engine.h"
17 
18 class Engine::Private
19 {
20 public:
21  Private()
22  : engine(nullptr)
23  , categoriesModel(nullptr)
24  {}
25  KNSCore::Engine *engine;
26  bool isLoading{false};
27  CategoriesModel *categoriesModel;
28  QString configFile;
29 
30  KNSCore::EntryInternal::List changedEntries;
32  {
33  KNSCore::EntryWrapper *entry{nullptr};
34  if (property) {
35  Private* d = static_cast<Engine::Private*>(property->data);
36  if (d) {
37  if (i >= 0 && i < d->changedEntries.count()) {
38  // Lifetime management for these objects should be done by the consumer,
39  // but are also parented for auto-delete on application shutdown
40  entry = new KNSCore::EntryWrapper(d->changedEntries[i], property->object);
41  }
42  }
43  }
44  return entry;
45  }
46  static int getChangedEntriesCount(QQmlListProperty<KNSCore::EntryWrapper>* property)
47  {
48  int count{0};
49  if (property) {
50  Private* d = static_cast<Engine::Private*>(property->data);
51  if (d) {
52  count = d->changedEntries.count();
53  }
54  }
55  return count;
56  }
57 };
58 
59 Engine::Engine(QObject *parent)
60  : QObject(parent)
61  , d(new Private)
62 {
63 }
64 
66 {
67  delete d;
68 }
69 
70 bool Engine::allowedByKiosk() const
71 {
72  return KAuthorized::authorize(QStringLiteral("ghns"));
73 }
74 
75 QString Engine::configFile() const
76 {
77  return d->configFile;
78 }
79 
80 void Engine::setConfigFile(const QString &newFile)
81 {
82  if (d->configFile != newFile) {
83  d->isLoading = true;
84  emit isLoadingChanged();
85  d->configFile = newFile;
86  emit configFileChanged();
87 
88  if (allowedByKiosk()) {
89  if (!d->engine) {
90  d->engine = new KNSCore::Engine(this);
91  connect(d->engine, &KNSCore::Engine::signalProvidersLoaded, this, [=](){
92  d->isLoading = false;
93  emit isLoadingChanged();
94  });
95  connect(d->engine, &KNSCore::Engine::signalMessage, this, &Engine::message);
96  connect(d->engine, &KNSCore::Engine::signalIdle, this, &Engine::idleMessage);
97  connect(d->engine, &KNSCore::Engine::signalBusy, this, &Engine::busyMessage);
98  connect(d->engine, &KNSCore::Engine::signalError, this, &Engine::errorMessage);
99  connect(d->engine, &KNSCore::Engine::signalErrorCode, this, [=](const KNSCore::ErrorCode &errorCode, const QString &message, const QVariant &/*metadata*/) {
100  if (errorCode == KNSCore::ProviderError) {
101  // This means loading the providers file failed entirely and we cannot complete the
102  // initialisation. It also means the engine is done loading, but that nothing will
103  // work, and we need to inform the user of this.
104  d->isLoading = false;
105  emit isLoadingChanged();
106  }
107  emit errorMessage(message);
108  });
109  connect(d->engine, &KNSCore::Engine::signalEntryChanged, this, [this](const KNSCore::EntryInternal &entry){
110  if (d->changedEntries.contains(entry) ) {
111  d->changedEntries.removeAll(entry);
112  }
113  d->changedEntries << entry;
114  emit changedEntriesChanged();
115  });
116  emit engineChanged();
117  KNewStuffQuick::QuickQuestionListener::instance();
118  d->categoriesModel = new CategoriesModel(this);
119  emit categoriesChanged();
120  // And finally, let's just make sure we don't miss out the various things here getting changed
121  // In other words, when we're asked to reset the view, actually do that
122  connect(d->engine, &KNSCore::Engine::signalResetView, this, &Engine::categoriesFilterChanged);
123  connect(d->engine, &KNSCore::Engine::signalResetView, this, &Engine::filterChanged);
124  connect(d->engine, &KNSCore::Engine::signalResetView, this, &Engine::sortOrderChanged);
125  connect(d->engine, &KNSCore::Engine::signalResetView, this, &Engine::searchTermChanged);
126  emit categoriesFilterChanged();
127  emit filterChanged();
128  emit sortOrderChanged();
129  emit searchTermChanged();
130  }
131  d->engine->init(d->configFile);
132  emit engineInitialized();
133  } else {
134  // 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
135  // nothing the user can do to fix it, and we just tell them so they're not wondering what's wrong)
136  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."));
137  }
138  }
139 }
140 
141 QObject *Engine::engine() const
142 {
143  return d->engine;
144 }
145 
146 bool Engine::isLoading() const
147 {
148  return d->isLoading;
149 }
150 
151 bool Engine::hasAdoptionCommand() const
152 {
153  if (d->engine) {
154  return d->engine->hasAdoptionCommand();
155  }
156  return false;
157 }
158 
159 QString Engine::name() const
160 {
161  if (d->engine) {
162  return d->engine->name();
163  }
164  return QString{};
165 }
166 
168 {
169  return d->categoriesModel;
170 }
171 
173 {
174  if (d->engine) {
175  return d->engine->categoriesFilter();
176  }
177  return QStringList{};
178 }
179 
180 void Engine::setCategoriesFilter(const QStringList &newCategoriesFilter)
181 {
182  if (d->engine) {
183  // This ensures that if we somehow end up with any empty entries (such as the default
184  // option in the categories dropdowns), our list will remain empty.
185  QStringList filter{newCategoriesFilter};
186  filter.removeAll({});
187  if (d->engine->categoriesFilter() != filter) {
188  d->engine->setCategoriesFilter(filter);
189  emit categoriesFilterChanged();
190  }
191  }
192 }
193 
194 void Engine::resetCategoriesFilter()
195 {
196  if (d->engine) {
197  d->engine->setCategoriesFilter(d->engine->categories());
198  }
199 }
200 
201 int Engine::filter() const
202 {
203  if (d->engine) {
204  d->engine->filter();
205  }
206  return 0;
207 }
208 
209 void Engine::setFilter(int newFilter)
210 {
211  if (d->engine && d->engine->filter() != newFilter) {
212  d->engine->setFilter(static_cast<KNSCore::Provider::Filter>(newFilter));
213  emit filterChanged();
214  }
215 }
216 
217 int Engine::sortOrder() const
218 {
219  if (d->engine) {
220  return d->engine->sortMode();
221  }
222  return 0;
223 }
224 
225 void Engine::setSortOrder(int newSortOrder)
226 {
227  if (d->engine && d->engine->sortMode() != newSortOrder) {
228  d->engine->setSortMode(static_cast<KNSCore::Provider::SortMode>(newSortOrder));
229  emit sortOrderChanged();
230  }
231 }
232 
234 {
235  if (d->engine) {
236  return d->engine->searchTerm();
237  }
238  return QString{};
239 }
240 
241 void Engine::setSearchTerm(const QString &newSearchTerm)
242 {
243  if (d->engine && d->engine->searchTerm() != newSearchTerm) {
244  d->engine->setSearchTerm(newSearchTerm);
245  emit searchTermChanged();
246  }
247 }
248 
249 void Engine::resetSearchTerm()
250 {
251  setSearchTerm(QString{});
252 }
253 
254 QQmlListProperty<KNSCore::EntryWrapper> Engine::changedEntries()
255 {
256  return QQmlListProperty<KNSCore::EntryWrapper>(this, d, &Private::getChangedEntriesCount, &Private::getChangedEntry);
257 }
258 
259 int Engine::changedEntriesCount() const
260 {
261  return d->changedEntries.count();
262 }
263 
264 void Engine::resetChangedEntries()
265 {
266  d->changedEntries.clear();
267  emit changedEntriesChanged();
268 }
QString busyMessage() const
String representation of the engines busy state.
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:201
void setSearchTerm(const QString &searchString)
Sets a string search term.
Definition: engine.cpp:454
bool hasAdoptionCommand() const
Whether or not an adoption command exists for this engine.
Definition: engine.cpp:897
ErrorCode
An enumeration of specific error conditions which might occur and which users of KNewStuff would want...
Definition: errorcode.h:24
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:406
QString i18nc(const char *context, const char *text, const TYPE &arg...)
~Engine()
Destructor.
Definition: engine.cpp:93
QString searchTerm() const
The search term for the current search (empty if none is set)
Definition: engine.cpp:466
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:435
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:426
QFuture< void > filter(Sequence &sequence, KeepFunctor filterFunction)
KCALUTILS_EXPORT QString errorMessage(const KCalendarCore::Exception &exception)
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:196
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:206
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.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Tue Aug 11 2020 22:43:23 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.