KNewStuff

enginebase.h
1/*
2 SPDX-FileCopyrightText: 2007 Josef Spillner <spillner@kde.org>
3 SPDX-FileCopyrightText: 2007-2010 Frederik Gladhorn <gladhorn@kde.org>
4 SPDX-FileCopyrightText: 2009 Jeremy Whiting <jpwhiting@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.1-or-later
7*/
8
9#ifndef KNEWSTUFF3_ENGINEBASE_H
10#define KNEWSTUFF3_ENGINEBASE_H
11
12#include <QHash>
13#include <QMetaEnum>
14#include <QObject>
15#include <QSharedPointer>
16#include <QString>
17
18#include "categorymetadata.h"
19#include "entry.h"
20#include "errorcode.h"
21#include "knewstuffcore_export.h"
22#include "provider.h"
23#include "searchpreset.h"
24
25#include <memory>
26
27class KJob;
28class EnginePrivate;
29class QDomDocument;
31
32namespace Attica
33{
34class Provider;
35}
36
37namespace KNSCore
38{
39class Cache;
40class CommentsModel;
41class ResultsStream;
42class EngineBasePrivate;
43class Installation;
44class SearchRequest;
45class ProviderCore;
46
47/**
48 * KNewStuff engine.
49 * An engine keeps track of data which is available locally and remote
50 * and offers high-level synchronization calls as well as upload and download
51 * primitives using an underlying GHNS protocol.
52 *
53 * This is a base class for different engine implementations
54 */
55class KNEWSTUFFCORE_EXPORT EngineBase : public QObject
56{
58
59 /**
60 * Text that should be displayed for the adoption button, this defaults to "Use"
61 * @since 5.77
62 */
64
65 /**
66 * Whether or not the configuration says that the providers are expected to support uploading.
67 * As it stands, this is used to determine whether or not to show the Upload... action where
68 * that is displayed (primarily NewStuff.Page).
69 * @since 5.85
70 */
72
73 /**
74 * @since 5.85
75 */
77
78 /**
79 * @copydoc contentWarningType
80 */
82
83public:
84 EngineBase(QObject *parent = nullptr);
85 ~EngineBase() override;
86 Q_DISABLE_COPY_MOVE(EngineBase)
87
88 /**
89 * List of all available config files. This list will contain no duplicated filenames.
90 * The returned file paths are absolute.
91 * @since 5.83
92 */
94
95 /**
96 * Initializes the engine. This step is application-specific and relies
97 * on an external configuration file, which determines all the details
98 * about the initialization.
99 *
100 * @param configfile KNewStuff2 configuration file (*.knsrc)
101 * @return \b true if any valid configuration was found, \b false otherwise
102 * @see KNS3::DownloadDialog
103 */
104 virtual bool init(const QString &configfile);
105
106 /**
107 * The name as defined by the knsrc file
108 * @return The name associated with the engine's configuration file
109 * @since 5.63
110 */
111 QString name() const;
112
113 /**
114 * Text that should be displayed for the adoption button, this defaults to i18n("Use")
115 * @since 5.77
116 */
117 QString useLabel() const;
118
119 /**
120 * Signal gets emitted when the useLabel property changes
121 * @since 5.77
122 */
124
125 /**
126 * Whether or not the configuration says that the providers are expected to support uploading.
127 * @return True if the providers are expected to support uploading
128 * @since 5.85
129 */
130 bool uploadEnabled() const;
131
132 /**
133 * Fired when the uploadEnabled property changes
134 * @since 5.85
135 */
137
138 /**
139 * The list of the server-side names of the categories handled by this
140 * engine instance. This corresponds directly to the list of categories
141 * in your knsrc file. This is not supposed to be used as user-facing
142 * strings - @see categoriesMetadata() for that.
143 *
144 * @return The categories which this instance of Engine handles
145 */
146 QStringList categories() const;
147
148#if KNEWSTUFFCORE_ENABLE_DEPRECATED_SINCE(6, 9)
149 /**
150 * Get the entries cache for this engine (note that it may be null if the engine is
151 * not yet initialized).
152 * @return The cache for this engine (or null if the engine is not initialized)
153 * @since 5.74
154 * @deprecated since 6.9 Do not use the cache directly
155 */
156 KNEWSTUFFCORE_DEPRECATED_VERSION(6, 9, "Do not use the cache directly")
157 QSharedPointer<Cache> cache() const;
158#endif
159
160#if KNEWSTUFFCORE_ENABLE_DEPRECATED_SINCE(6, 9)
161 /// @deprecated since 6.9 use categoriesMetadata2
162 KNEWSTUFFCORE_DEPRECATED_VERSION(6, 9, "Use categoriesMetadata2")
164#endif
165 /**
166 * The list of metadata for the categories handled by this engine instance.
167 * If you wish to show the categories to the user, this is the data to use.
168 * The category name is the string used to set categories for the filter,
169 * and also what is returned by both categories() and categoriesFilter().
170 * The human-readable name is displayName, and the only thing which should
171 * be shown to the user.
172 *
173 * @return The metadata for all categories handled by this engine
174 */
176
177#if KNEWSTUFFCORE_ENABLE_DEPRECATED_SINCE(6, 9)
178 /// @deprecated since 6.9 use searchPresets2
179 KNEWSTUFFCORE_DEPRECATED_VERSION(6, 9, "Use searchPresets2")
181#endif
182 /// @since 6.9
184
185 /**
186 * @returns the list of attica (OCS) providers this engine is connected to
187 * @since 5.92
188 */
190
191 /**
192 * Set a filter for results, which filters out all entries which do not match
193 * the filter, as applied to the tags for the entry. This filters only on the
194 * tags specified for the entry itself. To filter the downloadlinks, use
195 * setDownloadTagFilter(QStringList).
196 *
197 * @note The default filter if one is not set from your knsrc file will filter
198 * out entries marked as ghns_excluded=1. To retain this when setting a custom
199 * filter, add "ghns_excluded!=1" as one of the filters.
200 *
201 * @note Some tags provided by OCS do not supply a value (and are simply passed
202 * as a key). These will be interpreted as having the value 1 for filtering
203 * purposes. An example of this might be ghns_excluded, which in reality will
204 * generally be passed through ocs as "ghns_excluded" rather than "ghns_excluded=1"
205 *
206 * @note As tags are metadata, they are provided in the form of adjectives. They
207 * are never supplied as action verbs or instructions (as an example, a good tag
208 * to suggest that for example a wallpaper is painted would be "painted" as opposed
209 * to "paint", and another example might be that an item should be "excluded" as
210 * opposed to "exclude").
211 *
212 * == Examples of use ==
213 * Value for tag "tagname" must be exactly "tagdata":
214 * tagname==tagdata
215 *
216 * Value for tag "tagname" must be different from "tagdata":
217 * tagname!=tagdata
218 *
219 * == KNSRC entry ==
220 * A tag filter line in a .knsrc file, which is a comma separated list of
221 * tag/value pairs, might look like:
222 *
223 * TagFilter=ghns_excluded!=1,data##mimetype==application/cbr+zip,data##mimetype==application/cbr+rar
224 * which would honour the exclusion and filter out anything that does not
225 * include a comic book archive in either zip or rar format in one or more
226 * of the download items.
227 * Notice in particular that there are two data##mimetype entries. Use this
228 * for when a tag may have multiple values.
229 *
230 * TagFilter=application##architecture==x86_64
231 * which would not honour the exclusion, and would filter out all entries
232 * which do not mark themselves as having a 64bit application binary in at
233 * least one download item.
234 *
235 * The value does not current support wildcards. The list should be considered
236 * a binary AND operation (that is, all filter entries must match for the data
237 * entry to be included in the return data)
238 *
239 * @param filter The filter in the form of a list of strings
240 * @see setDownloadTagFilter(QStringList)
241 * @since 5.51
242 */
243 void setTagFilter(const QStringList &filter);
244 /**
245 * Gets the current tag filter list
246 * @see setTagFilter(QStringList)
247 * @since 5.51
248 */
249 QStringList tagFilter() const;
250 /**
251 * Add a single filter entry to the entry tag filter. The filter should be in
252 * the same form as the filter lines in the list used by setTagFilter(QStringList)
253 * @param filter The filter in the form of a string
254 * @see setTagFilter(QStringList)
255 * @since 5.51
256 */
257 void addTagFilter(const QString &filter);
258 /**
259 * Sets a filter to be applied to the downloads for an entry. The logic is the
260 * same as used in setTagFilter(QStringList), but vitally, only one downloadlink
261 * is required to match the filter for the list to be valid. If you do not wish
262 * to show the others in your client, you must hide them yourself.
263 *
264 * For an entry to be accepted when a download tag filter is set, it must also
265 * be accepted by the entry filter (so, for example, while a list of downloads
266 * might be accepted, if the entry has ghns_excluded set, and the default entry
267 * filter is set, the entry will still be filtered out).
268 *
269 * In your knsrc file, set DownloadTagFilter to the filter you wish to apply,
270 * using the same logic as described for the entry tagfilter.
271 *
272 * @param filter The filter in the form of a list of strings
273 * @see setTagFilter(QStringList)
274 * @since 5.51
275 */
276 void setDownloadTagFilter(const QStringList &filter);
277 /**
278 * Gets the current downloadlink tag filter list
279 * @see setDownloadTagFilter(QStringList)
280 * @since 5.51
281 */
283 /**
284 * Add a single filter entry to the download tag filter. The filter should be in
285 * the same form as the filter lines in the list used by setDownloadsTagFilter(QStringList)
286 * @param filter The filter in the form of a string
287 * @see setTagFilter(QStringList)
288 * @see setDownloadTagFilter(QStringList)
289 * @since 5.51
290 */
291 void addDownloadTagFilter(const QString &filter);
292
293 /**
294 * Whether or not a user is able to vote on the passed entry.
295 *
296 * @param entry The entry to check votability on
297 * @return True if the user is able to vote on the entry
298 */
299 bool userCanVote(const Entry &entry);
300 /**
301 * Cast a vote on the passed entry.
302 *
303 * @param entry The entry to vote on
304 * @param rating A number from 0 to 100, 50 being neutral, 0 being most negative and 100 being most positive.
305 */
306 void vote(const Entry &entry, uint rating);
307
308 /**
309 * Whether or not the user is allowed to become a fan of
310 * a particular entry.
311 * Not all providers (and consequently entries) support the fan functionality
312 * and you can use this function to determine this ability.
313 * @param entry The entry the user might wish to be a fan of
314 * @return Whether or not it is possible for the user to become a fan of that entry
315 */
316 bool userCanBecomeFan(const Entry &entry);
317 /**
318 * This will mark the user who is currently authenticated as a fan
319 * of the entry passed to the function.
320 * @param entry The entry the user wants to be a fan of
321 */
322 void becomeFan(const Entry &entry);
323 // FIXME There is currently no exposed API to remove the fan status
324
325#if KNEWSTUFFCORE_ENABLE_DEPRECATED_SINCE(6, 9)
326 /**
327 * The Provider instance with the passed ID
328 *
329 * @param providerId The ID of the Provider to fetch
330 * @return The Provider with the passed ID, or null if non such Provider exists
331 * @since 5.63
332 * @deprecated since 6.9 Do not write provider-specific code
333 */
334 KNEWSTUFFCORE_DEPRECATED_VERSION(6, 9, "Do not write provider-specific code")
335 QSharedPointer<Provider> provider(const QString &providerId) const;
336#endif
337
338#if KNEWSTUFFCORE_ENABLE_DEPRECATED_SINCE(6, 9)
339 /**
340 * Return the first provider in the providers list (usually the default provider)
341 * @return The first Provider (or null if the engine is not initialized)
342 * @since 5.63
343 * @deprecated since 6.9 Do not write provider-specific code
344 */
345 KNEWSTUFFCORE_DEPRECATED_VERSION(6, 9, "Do not write provider-specific code")
347#endif
348
349 /**
350 * The IDs of all providers known by this engine. Use this in combination with
351 * provider(const QString&) to iterate over all providers.
352 * @return The string IDs of all known providers
353 * @since 5.85
354 */
355 QStringList providerIDs() const;
356
357 /**
358 * Whether or not an adoption command exists for this engine
359 *
360 * @see adoptionCommand(KNSCore::Entry)
361 * @return True if an adoption command exists
362 */
363 bool hasAdoptionCommand() const;
364
365#if KNEWSTUFFCORE_ENABLE_DEPRECATED_SINCE(6, 9)
366 /**
367 * Returns a stream object that will fulfill the @p request.
368 *
369 * @since 6.0
370 * @deprecated since 6.9 Use the new search function
371 */
372 KNEWSTUFFCORE_DEPRECATED_VERSION(6, 9, "Use the new search function")
373 ResultsStream *search(const KNSCore::Provider::SearchRequest &request);
374#endif
375
376 /**
377 * Returns a stream object that will fulfill the @p request.
378 *
379 * @since 6.9
380 */
381 ResultsStream *search(const KNSCore::SearchRequest &request);
382
383 /**
384 * @brief The ContentWarningType enum
385 * @since 6.1
386 */
388 /**
389 * Content consists of static assets only
390 * Installation should not pose a security risk
391 */
393 /**
394 * Content may contain scripts or other executable code
395 * Users should be warned to treat it like any other program
396 */
398 };
399 Q_ENUM(ContentWarningType)
400
401 /**
402 * @brief The level of warning that should be presented to the user
403 * @since 6.1
404 * @see ContentWarningType
405 */
406 ContentWarningType contentWarningType() const;
407
408 /**
409 * Emitted after the initial config load
410 * @since 6.1
411 */
413
415 /**
416 * Indicates a message to be added to the ui's log, or sent to a messagebox
417 */
418 void signalMessage(const QString &message);
419
420 void signalProvidersLoaded();
421
422 /**
423 * Fires in the case of any critical or serious errors, such as network or API problems.
424 * @param errorCode Represents the specific type of error which has occurred
425 * @param message A human-readable message which can be shown to the end user
426 * @param metadata Any additional data which might be helpful to further work out the details of the error (see KNSCore::Entry::ErrorCode for the
427 * metadata details)
428 * @see KNSCore::Entry::ErrorCode
429 * @since 5.53
430 */
431 void signalErrorCode(KNSCore::ErrorCode::ErrorCode errorCode, const QString &message, const QVariant &metadata);
432
433#if KNEWSTUFFCORE_ENABLE_DEPRECATED_SINCE(6, 9)
434 /// @deprecated since 6.9 Use variant with new argument type
435 KNEWSTUFFCORE_DEPRECATED_VERSION(6, 9, "Use variant with new argument type")
437#endif
438 void signalCategoriesMetadataLoaded(const QList<KNSCore::CategoryMetadata> &categories);
439
440#if KNEWSTUFFCORE_ENABLE_DEPRECATED_SINCE(6, 9)
441 /**
442 * Fires when the engine has loaded search presets. These represent interesting
443 * searches for the user, such as recommendations.
444 * @since 5.83
445 * @deprecated since 6.9 Use variant with new argument type
446 */
447 KNEWSTUFFCORE_DEPRECATED_VERSION(6, 9, "Use variant with new argument type")
449#endif
450
451 /**
452 * Fires when the engine has loaded search presets. These represent interesting
453 * searches for the user, such as recommendations.
454 * @since 6.9
455 */
457
458#if KNEWSTUFFCORE_ENABLE_DEPRECATED_SINCE(6, 9)
459 /**
460 * Fired whenever the list of providers changes
461 * @since 5.85
462 * @deprecated since 6.9 Use providerAdded signal
463 */
464 KNEWSTUFFCORE_DEPRECATED_VERSION(6, 9, "Use providerAdded signal")
466#endif
467
468 void loadingProvider();
469 void providerAdded(KNSCore::ProviderCore *provider);
470
471private:
472 // the .knsrc file was loaded
473 void slotProviderFileLoaded(const QDomDocument &doc);
474 // instead of getting providers from knsrc, use what was configured in ocs systemsettings
475 void atticaProviderLoaded(const Attica::Provider &provider);
476 // called when a provider is ready to work
477 void providerInitialized(KNSCore::Provider *);
478
479 // loading the .knsrc file failed
480 void slotProvidersFailed();
481
482 /**
483 * load providers from the providersurl in the knsrc file
484 * creates providers based on their type and adds them to the list of providers
485 */
486 void loadProviders();
487
488protected:
489#if KNEWSTUFFCORE_ENABLE_DEPRECATED_SINCE(6, 9)
490 /**
491 * Add a provider and connect it to the right slots
492 * @deprecated since 6.9 Use providerAdded signal
493 */
494 KNEWSTUFFCORE_DEPRECATED_VERSION(6, 9, "Use providerAdded signal")
495 virtual void addProvider(QSharedPointer<KNSCore::Provider> provider);
496#endif
497 virtual void updateStatus();
498
499 friend class ResultsStream;
500 friend class Transaction;
501 friend class TransactionPrivate;
502 friend class EngineBasePrivate;
503 friend class ::SearchPresetModel;
504 Installation *installation() const; // Needed for quick engine
505#if KNEWSTUFFCORE_ENABLE_DEPRECATED_SINCE(6, 9)
506 /// @deprecated since 6.9 Do not write provider-specific code
507 KNEWSTUFFCORE_DEPRECATED_VERSION(6, 9, "Do not write provider-specific code")
509#endif
510 // FIXME KF7: make this private and declare QuickEngine a friend. this cannot be used from the outside!
511 std::unique_ptr<EngineBasePrivate> d;
512};
513
514}
515
516#endif
Describes a category.
A model which takes care of the comments for a single Entry.
bool uploadEnabled
Whether or not the configuration says that the providers are expected to support uploading.
Definition enginebase.h:71
ContentWarningType
The ContentWarningType enum.
Definition enginebase.h:387
@ Static
Content consists of static assets only Installation should not pose a security risk.
Definition enginebase.h:392
@ Executables
Content may contain scripts or other executable code Users should be warned to treat it like any othe...
Definition enginebase.h:397
void signalCategoriesMetadataLoded(const QList< Provider::CategoryMetadata > &categories)
QList< Provider::SearchPreset > searchPresets()
void signalErrorCode(KNSCore::ErrorCode::ErrorCode errorCode, const QString &message, const QVariant &metadata)
Fires in the case of any critical or serious errors, such as network or API problems.
void addTagFilter(const QString &filter)
Add a single filter entry to the entry tag filter.
bool userCanBecomeFan(const Entry &entry)
Whether or not the user is allowed to become a fan of a particular entry.
QStringList categories() const
The list of the server-side names of the categories handled by this engine instance.
virtual void addProvider(QSharedPointer< KNSCore::Provider > provider)
Add a provider and connect it to the right slots.
QList< QSharedPointer< Provider > > providers() const
void becomeFan(const Entry &entry)
This will mark the user who is currently authenticated as a fan of the entry passed to the function.
bool hasAdoptionCommand() const
Whether or not an adoption command exists for this engine.
QSharedPointer< Cache > cache() const
Get the entries cache for this engine (note that it may be null if the engine is not yet initialized)...
QList< SearchPreset > searchPresets2()
void setTagFilter(const QStringList &filter)
Set a filter for results, which filters out all entries which do not match the filter,...
QStringList tagFilter() const
Gets the current tag filter list.
QString name() const
The name as defined by the knsrc file.
Q_SIGNAL void contentWarningTypeChanged()
Emitted after the initial config load.
QSharedPointer< Provider > provider(const QString &providerId) const
The Provider instance with the passed ID.
Q_SIGNAL void uploadEnabledChanged()
Fired when the uploadEnabled property changes.
Q_SIGNAL void useLabelChanged()
Signal gets emitted when the useLabel property changes.
bool userCanVote(const Entry &entry)
Whether or not a user is able to vote on the passed entry.
static QStringList availableConfigFiles()
List of all available config files.
QStringList downloadTagFilter() const
Gets the current downloadlink tag filter list.
void providersChanged()
Fired whenever the list of providers changes.
void signalSearchPresetsLoaded(const QList< Provider::SearchPreset > &presets)
Fires when the engine has loaded search presets.
QString useLabel
Text that should be displayed for the adoption button, this defaults to "Use".
Definition enginebase.h:63
virtual bool init(const QString &configfile)
Initializes the engine.
QList< CategoryMetadata > categoriesMetadata2()
The list of metadata for the categories handled by this engine instance.
void vote(const Entry &entry, uint rating)
Cast a vote on the passed entry.
QList< Attica::Provider * > atticaProviders() const
QList< Provider::CategoryMetadata > categoriesMetadata()
ResultsStream * search(const KNSCore::Provider::SearchRequest &request)
Returns a stream object that will fulfill the request.
void addDownloadTagFilter(const QString &filter)
Add a single filter entry to the download tag filter.
void signalSearchPresetsLoaded(const QList< KNSCore::SearchPreset > &presets)
Fires when the engine has loaded search presets.
QSharedPointer< Provider > defaultProvider() const
Return the first provider in the providers list (usually the default provider)
QStringList providerIDs
Definition enginebase.h:76
ContentWarningType contentWarningType
Definition enginebase.h:81
void setDownloadTagFilter(const QStringList &filter)
Sets a filter to be applied to the downloads for an entry.
void signalMessage(const QString &message)
Indicates a message to be added to the ui's log, or sent to a messagebox.
KNewStuff data entry container.
Definition entry.h:48
KNewStuff Base Provider class.
KNewStuff Base Provider class.
Definition provider.h:47
The ResultsStream is returned by EngineBase::search.
Describes a search request that may come from the provider.
A search request.
The SearchPresetModel class.
QObject(QObject *parent)
Q_OBJECTQ_OBJECT
Q_PROPERTY(...)
Q_SIGNALQ_SIGNAL
Q_SIGNALSQ_SIGNALS
QObject * parent() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Jan 24 2025 11:50:44 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.