KNewStuff

downloadmanager.cpp
1 /*
2  SPDX-FileCopyrightText: 2010 Frederik Gladhorn <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.1-or-later
5 */
6 
7 #include "downloadmanager.h"
8 
9 #include <QCoreApplication>
10 #include <knewstuff_debug.h>
11 
12 #include "core/engine.h"
13 #include "entry_p.h"
14 #include "ui/widgetquestionlistener.h"
15 
16 #if KNEWSTUFF_BUILD_DEPRECATED_SINCE(5, 29)
17 namespace KNS3
18 {
19 class DownloadManagerPrivate
20 {
21 public:
22  DownloadManager *const q;
23  KNSCore::Engine *const engine;
24 
25  DownloadManagerPrivate(DownloadManager *q)
26  : q(q)
27  , engine(new KNSCore::Engine)
28  {}
29  ~DownloadManagerPrivate()
30  {
31  delete engine;
32  }
33 
34  bool isInitialized = false;
35  bool checkForUpdates = false;
36  bool checkForInstalled = false;
37  bool doSearch = false;
38 
39  int page = 0;
40  int pageSize = 100;
41 
42  void init(const QString &configFile);
43  void _k_slotProvidersLoaded();
44  void _k_slotEngineError(const QString &error);
45  void _k_slotUpdatesLoaded(const KNSCore::EntryInternal::List &entries);
46  void _k_slotEntryStatusChanged(const KNSCore::EntryInternal &entry);
47  void _k_slotEntriesLoaded(const KNSCore::EntryInternal::List &entries);
48 };
49 }
50 
51 using namespace KNS3;
52 
54  : QObject(parent)
55  , d(new DownloadManagerPrivate(this))
56 {
58  d->init(name + QStringLiteral(".knsrc"));
59 }
60 
62  : QObject(parent)
63  , d(new DownloadManagerPrivate(this))
64 {
65  d->init(configFile);
66 }
67 
68 void DownloadManagerPrivate::init(const QString &configFile)
69 {
70  q->connect(engine, SIGNAL(signalProvidersLoaded()), q, SLOT(_k_slotProvidersLoaded()));
71  q->connect(engine, SIGNAL(signalUpdateableEntriesLoaded(KNSCore::EntryInternal::List)), q, SLOT(_k_slotEntriesLoaded(KNSCore::EntryInternal::List)));
72  q->connect(engine, SIGNAL(signalEntriesLoaded(KNSCore::EntryInternal::List)), q, SLOT(_k_slotEntriesLoaded(KNSCore::EntryInternal::List)));
73  q->connect(engine, SIGNAL(signalEntryChanged(KNSCore::EntryInternal)), q, SLOT(_k_slotEntryStatusChanged(KNSCore::EntryInternal)));
74  q->connect(engine, SIGNAL(signalError(QString)), q, SLOT(_k_slotEngineError(QString)));
75  engine->init(configFile);
76  WidgetQuestionListener::instance();
77 }
78 
80 {
81  delete d;
82 }
83 
84 void DownloadManagerPrivate::_k_slotEngineError(const QString &error)
85 {
86  qCWarning(KNEWSTUFF) << "engine error" << error;
87 
88  Q_EMIT q->errorFound(error);
89 }
90 
91 void DownloadManagerPrivate::_k_slotProvidersLoaded()
92 {
93  qCDebug(KNEWSTUFF) << "providers loaded";
94  isInitialized = true;
95  if (checkForInstalled) {
96  engine->checkForInstalled();
97  } else if (checkForUpdates) {
98  engine->checkForUpdates();
99  } else if (doSearch) {
100  engine->requestData(page, pageSize);
101  }
102 }
103 
105 {
106  if (d->isInitialized) {
107  d->engine->checkForUpdates();
108  } else {
109  d->checkForUpdates = true;
110  }
111 }
112 
114 {
115  if (d->isInitialized) {
116  d->engine->checkForInstalled();
117  } else {
118  d->checkForInstalled = true;
119  }
120 }
121 
122 void DownloadManagerPrivate::_k_slotEntriesLoaded(const KNSCore::EntryInternal::List &entries)
123 {
124  KNS3::Entry::List result;
125  result.reserve(entries.size());
126  for (const KNSCore::EntryInternal &entry : entries) {
127  result.append(EntryPrivate::fromInternal(&entry));
128  }
129  Q_EMIT q->searchResult(result);
130 }
131 
132 void KNS3::DownloadManagerPrivate::_k_slotEntryStatusChanged(const KNSCore::EntryInternal &entry)
133 {
134  Q_EMIT q->entryStatusChanged(EntryPrivate::fromInternal(&entry));
135 }
136 
138 {
139  KNSCore::EntryInternal entryInternal = KNSCore::EntryInternal::fromEntry(entry);
140  if (entryInternal.isValid()) {
141  d->engine->install(entryInternal);
142  }
143 }
144 
146 {
147  KNSCore::EntryInternal entryInternal = KNSCore::EntryInternal::fromEntry(entry);
148  if (entryInternal.isValid()) {
149  d->engine->uninstall(entryInternal);
150  }
151 }
152 
153 void DownloadManager::search(int page, int pageSize)
154 {
155  d->page = page;
156  d->pageSize = pageSize;
157 
158  if (d->isInitialized) {
159  d->engine->requestData(page, pageSize);
160  } else {
161  d->doSearch = true;
162  }
163 }
164 
165 void DownloadManager::setSearchOrder(DownloadManager::SortOrder order)
166 {
167  switch (order) {
168  case Newest:
169  d->engine->setSortMode(KNSCore::Provider::Newest);
170  break;
171  case Rating:
172  d->engine->setSortMode(KNSCore::Provider::Rating);
173  break;
174  case Alphabetical:
175  d->engine->setSortMode(KNSCore::Provider::Alphabetical);
176  break;
177  case Downloads:
178  d->engine->setSortMode(KNSCore::Provider::Downloads);
179  break;
180  }
181 }
182 
184 {
185  d->engine->setSearchTerm(searchTerm);
186 }
187 
189 {
190  d->engine->fetchEntryById(id);
191 }
192 
193 #include "moc_downloadmanager.cpp"
194 #endif
195 
void checkForInstalled()
Check for installed resources Use searchResult to get notified about installed entries.
void reserve(int alloc)
Contains the core functionality for handling interaction with NewStuff providers. ...
KNewStuff update checker.
Encapsulates a KNSCore::Engine for use in Qt Quick.
Definition: quickengine.h:23
int size() const const
DownloadManager(QObject *parent=nullptr)
Create a DownloadManager It will try to find a appname.knsrc file.
void append(const T &value)
void uninstallEntry(const KNS3::Entry &entry)
Uninstalls the given entry.
void installEntry(const KNS3::Entry &entry)
Installs or updates an entry.
KNewStuff information about changed entries.
Definition: entry.h:32
void search(int page=0, int pageSize=100)
Search for a list of entries.
~DownloadManager()
destructor
QCA_EXPORT void init()
KREPORT_EXPORT QPageSize::PageSizeId pageSize(const QString &key)
void setSearchOrder(SortOrder order)
Set the sort order of the results.
KNewStuff data entry container.
Definition: entryinternal.h:49
void setSearchTerm(const QString &searchTerm)
Sets the search term to filter the results on the server.
KNewStuff engine.
Definition: engine.h:59
void fetchEntryById(const QString &id)
Triggers a search for an entry with id as its unique id.
void checkForUpdates()
Check for available updates.
The namespace for the KNewStuff classes.
Definition: button.cpp:19
QObject * parent() const const
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.