KNewStuff

core/downloadmanager.cpp
1 /*
2  Copyright (C) 2010 Frederik Gladhorn <[email protected]>
3  Copyright (C) 2016 Dan Leinir Turthra Jensen <[email protected]>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #include "downloadmanager.h"
20 
21 #include <knewstuffcore_debug.h>
22 
23 #include <QCoreApplication>
24 
25 #include "engine.h"
26 
27 namespace KNSCore
28 {
29 class DownloadManagerPrivate
30 {
31 public:
32  DownloadManager *q;
33  Engine *engine;
34 
35  DownloadManagerPrivate(DownloadManager *q)
36  : q(q)
37  , engine(new Engine)
38  , isInitialized(false)
39  , checkForUpdates(false)
40  , checkForInstalled(false)
41  , doSearch(false)
42  , page(0)
43  , pageSize(100)
44  {}
45  ~DownloadManagerPrivate()
46  {
47  delete engine;
48  }
49 
50  bool isInitialized;
51  bool checkForUpdates;
52  bool checkForInstalled;
53  bool doSearch;
54 
55  int page;
56  int pageSize;
57 
58  void init(const QString &configFile);
59 };
60 }
61 
62 using namespace KNSCore;
63 
65  : QObject(parent)
66  , d(new DownloadManagerPrivate(this))
67 {
69  d->init(name + QStringLiteral(".knsrc"));
70 }
71 
73  : QObject(parent)
74  , d(new DownloadManagerPrivate(this))
75 {
76  d->init(configFile);
77 }
78 
79 void DownloadManagerPrivate::init(const QString &configFile)
80 {
81  q->connect(engine, &KNSCore::Engine::signalProvidersLoaded, q, &DownloadManager::slotProvidersLoaded);
82  q->connect(engine, &KNSCore::Engine::signalUpdateableEntriesLoaded, q, &DownloadManager::searchResult);
83  q->connect(engine, &KNSCore::Engine::signalEntriesLoaded, q, &DownloadManager::searchResult);
84  q->connect(engine, &KNSCore::Engine::signalEntryChanged, q, &DownloadManager::entryStatusChanged);
85  q->connect(engine, &KNSCore::Engine::signalErrorCode, q, [this](const KNSCore::ErrorCode &, const QString &message, const QVariant &){ emit q->errorFound(message); });
86  engine->init(configFile);
87 }
88 
90 {
91  delete d;
92 }
93 
94 void DownloadManager::slotProvidersLoaded()
95 {
96  qCDebug(KNEWSTUFFCORE) << "providers loaded";
97  d->isInitialized = true;
98  if (d->checkForInstalled) {
99  d->engine->checkForInstalled();
100  } else if (d->checkForUpdates) {
101  d->engine->checkForUpdates();
102  } else if (d->doSearch) {
103  d->engine->requestData(d->page, d->pageSize);
104  }
105 }
106 
108 {
109  if (d->isInitialized) {
110  d->engine->checkForUpdates();
111  } else {
112  d->checkForUpdates = true;
113  }
114 }
115 
117 {
118  if (d->isInitialized) {
119  d->engine->checkForInstalled();
120  } else {
121  d->checkForInstalled = true;
122  }
123 }
124 
126 {
127  if (entry.isValid()) {
128  d->engine->install(entry);
129  }
130 }
131 
133 {
134  if (entry.isValid()) {
135  d->engine->uninstall(entry);
136  }
137 }
138 
139 void DownloadManager::search(int page, int pageSize)
140 {
141  d->page = page;
142  d->pageSize = pageSize;
143 
144  if (d->isInitialized) {
145  d->engine->requestData(page, pageSize);
146  } else {
147  d->doSearch = true;
148  }
149 }
150 
151 void DownloadManager::setSearchOrder(DownloadManager::SortOrder order)
152 {
153  switch (order) {
154  case Newest:
155  d->engine->setSortMode(KNSCore::Provider::Newest);
156  break;
157  case Rating:
158  d->engine->setSortMode(KNSCore::Provider::Rating);
159  break;
160  case Alphabetical:
161  d->engine->setSortMode(KNSCore::Provider::Alphabetical);
162  break;
163  case Downloads:
164  d->engine->setSortMode(KNSCore::Provider::Downloads);
165  break;
166  }
167 }
168 
170 {
171  d->engine->setSearchTerm(searchTerm);
172 }
173 
175 {
176  d->engine->fetchEntryById(id);
177 }
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:35
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:60
KNewStuff engine.
Definition: engine.h:70
void entryStatusChanged(const EntryInternal &entry)
The entry status has changed: emitted when the entry has been installed, updated or removed...
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.
QString applicationName()
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun Aug 9 2020 22:43:40 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.