KNewStuff

core/downloadmanager.cpp
1 /*
2  SPDX-FileCopyrightText: 2010 Frederik Gladhorn <[email protected]>
3  SPDX-FileCopyrightText: 2016 Dan Leinir Turthra Jensen <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.1-or-later
6 */
7 
8 #include "knewstuff_export.h"
9 #if KNEWSTUFF_BUILD_DEPRECATED_SINCE(5, 79)
10 #include "downloadmanager.h"
11 
12 #include <knewstuffcore_debug.h>
13 
14 #include <QCoreApplication>
15 
16 #include "engine.h"
17 
18 namespace KNSCore
19 {
20 class DownloadManagerPrivate
21 {
22 public:
23  DownloadManager *const q;
24  Engine *const engine;
25 
26  DownloadManagerPrivate(DownloadManager *q)
27  : q(q)
28  , engine(new Engine)
29  {}
30  ~DownloadManagerPrivate()
31  {
32  delete engine;
33  }
34 
35  bool isInitialized = false;
36  bool checkForUpdates = false;
37  bool checkForInstalled = false;
38  bool doSearch = false;
39 
40  int page = 0;
41  int pageSize = 100;
42 
43  void init(const QString &configFile);
44 };
45 }
46 
47 using namespace KNSCore;
48 
50  : QObject(parent)
51  , d(new DownloadManagerPrivate(this))
52 {
54  d->init(name + QStringLiteral(".knsrc"));
55 }
56 
58  : QObject(parent)
59  , d(new DownloadManagerPrivate(this))
60 {
61  d->init(configFile);
62 }
63 
64 void DownloadManagerPrivate::init(const QString &configFile)
65 {
66  q->connect(engine, &KNSCore::Engine::signalProvidersLoaded, q, &DownloadManager::slotProvidersLoaded);
67  q->connect(engine, &KNSCore::Engine::signalUpdateableEntriesLoaded, q, &DownloadManager::searchResult);
68  q->connect(engine, &KNSCore::Engine::signalEntriesLoaded, q, &DownloadManager::searchResult);
69  q->connect(engine, &KNSCore::Engine::signalEntryEvent, q, [this](const EntryInternal &entry, EntryInternal::EntryEvent event) {
70  if (event == EntryInternal::StatusChangedEvent) {
71  q->entryStatusChanged(entry);
72  }
73  });
74  q->connect(engine, &KNSCore::Engine::signalErrorCode, q, [this](const KNSCore::ErrorCode &, const QString &message, const QVariant &){ Q_EMIT q->errorFound(message); });
75  engine->init(configFile);
76 }
77 
79 {
80  delete d;
81 }
82 
83 void DownloadManager::slotProvidersLoaded()
84 {
85  qCDebug(KNEWSTUFFCORE) << "providers loaded";
86  d->isInitialized = true;
87  if (d->checkForInstalled) {
88  d->engine->checkForInstalled();
89  } else if (d->checkForUpdates) {
90  d->engine->checkForUpdates();
91  } else if (d->doSearch) {
92  d->engine->requestData(d->page, d->pageSize);
93  }
94 }
95 
97 {
98  if (d->isInitialized) {
99  d->engine->checkForUpdates();
100  } else {
101  d->checkForUpdates = true;
102  }
103 }
104 
106 {
107  if (d->isInitialized) {
108  d->engine->checkForInstalled();
109  } else {
110  d->checkForInstalled = true;
111  }
112 }
113 
115 {
116  if (entry.isValid()) {
117  d->engine->install(entry);
118  }
119 }
120 
122 {
123  if (entry.isValid()) {
124  d->engine->uninstall(entry);
125  }
126 }
127 
128 void DownloadManager::search(int page, int pageSize)
129 {
130  d->page = page;
131  d->pageSize = pageSize;
132 
133  if (d->isInitialized) {
134  d->engine->requestData(page, pageSize);
135  } else {
136  d->doSearch = true;
137  }
138 }
139 
140 void DownloadManager::setSearchOrder(DownloadManager::SortOrder order)
141 {
142  switch (order) {
143  case Newest:
144  d->engine->setSortMode(KNSCore::Provider::Newest);
145  break;
146  case Rating:
147  d->engine->setSortMode(KNSCore::Provider::Rating);
148  break;
149  case Alphabetical:
150  d->engine->setSortMode(KNSCore::Provider::Alphabetical);
151  break;
152  case Downloads:
153  d->engine->setSortMode(KNSCore::Provider::Downloads);
154  break;
155  }
156 }
157 
159 {
160  d->engine->setSearchTerm(searchTerm);
161 }
162 
164 {
165  d->engine->fetchEntryById(id);
166 }
167 #endif
void setSearchOrder(SortOrder order)
Set the sort order of the results.
KNewStuff update checker.
Contains the core functionality for handling interaction with NewStuff providers. ...
void searchResult(const EntryInternal::List &entries)
Returns the search result.
void uninstallEntry(const EntryInternal &entry)
Uninstalls the given entry.
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)
void fetchEntryById(const QString &id)
Triggers a search for an entry with id as its unique id.
void installEntry(const EntryInternal &entry)
Installs or updates an entry.
void setSearchTerm(const QString &searchTerm)
Sets the search term to filter the results on the server.
void search(int page=0, int pageSize=100)
Search for a list of entries.
void checkForUpdates()
Check for available updates.
QCA_EXPORT void init()
KREPORT_EXPORT QPageSize::PageSizeId pageSize(const QString &key)
DownloadManager(QObject *parent=nullptr)
Create a DownloadManager It will try to find a appname.knsrc file.
KNewStuff data entry container.
Definition: entryinternal.h:49
KNewStuff engine.
Definition: engine.h:59
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.
void checkForInstalled()
Check for installed resources Use searchResult to get notified about installed entries.
Q_EMITQ_EMIT
QString applicationName()
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Jan 18 2021 22:43:49 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.