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 namespace KNS3
17 {
18 class DownloadManagerPrivate
19 {
20 public:
21  DownloadManager *q;
22  KNSCore::Engine *engine;
23 
24  DownloadManagerPrivate(DownloadManager *q)
25  : q(q)
26  , engine(new KNSCore::Engine)
27  , isInitialized(false)
28  , checkForUpdates(false)
29  , checkForInstalled(false)
30  , doSearch(false)
31  , page(0)
32  , pageSize(100)
33  {}
34  ~DownloadManagerPrivate()
35  {
36  delete engine;
37  }
38 
39  bool isInitialized;
40  bool checkForUpdates;
41  bool checkForInstalled;
42  bool doSearch;
43 
44  int page;
45  int pageSize;
46 
47  void init(const QString &configFile);
48  void _k_slotProvidersLoaded();
49  void _k_slotEngineError(const QString &error);
50  void _k_slotUpdatesLoaded(const KNSCore::EntryInternal::List &entries);
51  void _k_slotEntryStatusChanged(const KNSCore::EntryInternal &entry);
52  void _k_slotEntriesLoaded(const KNSCore::EntryInternal::List &entries);
53 };
54 }
55 
56 using namespace KNS3;
57 
59  : QObject(parent)
60  , d(new DownloadManagerPrivate(this))
61 {
63  d->init(name + QStringLiteral(".knsrc"));
64 }
65 
67  : QObject(parent)
68  , d(new DownloadManagerPrivate(this))
69 {
70  d->init(configFile);
71 }
72 
73 void DownloadManagerPrivate::init(const QString &configFile)
74 {
75  q->connect(engine, SIGNAL(signalProvidersLoaded()), q, SLOT(_k_slotProvidersLoaded()));
76  q->connect(engine, SIGNAL(signalUpdateableEntriesLoaded(KNSCore::EntryInternal::List)), q, SLOT(_k_slotEntriesLoaded(KNSCore::EntryInternal::List)));
77  q->connect(engine, SIGNAL(signalEntriesLoaded(KNSCore::EntryInternal::List)), q, SLOT(_k_slotEntriesLoaded(KNSCore::EntryInternal::List)));
78  q->connect(engine, SIGNAL(signalEntryChanged(KNSCore::EntryInternal)), q, SLOT(_k_slotEntryStatusChanged(KNSCore::EntryInternal)));
79  q->connect(engine, SIGNAL(signalError(QString)), q, SLOT(_k_slotEngineError(QString)));
80  engine->init(configFile);
81  WidgetQuestionListener::instance();
82 }
83 
85 {
86  delete d;
87 }
88 
89 void DownloadManagerPrivate::_k_slotEngineError(const QString &error)
90 {
91  qCWarning(KNEWSTUFF) << "engine error" << error;
92 
93  Q_EMIT q->errorFound(error);
94 }
95 
96 void DownloadManagerPrivate::_k_slotProvidersLoaded()
97 {
98  qCDebug(KNEWSTUFF) << "providers loaded";
99  isInitialized = true;
100  if (checkForInstalled) {
101  engine->checkForInstalled();
102  } else if (checkForUpdates) {
103  engine->checkForUpdates();
104  } else if (doSearch) {
105  engine->requestData(page, pageSize);
106  }
107 }
108 
110 {
111  if (d->isInitialized) {
112  d->engine->checkForUpdates();
113  } else {
114  d->checkForUpdates = true;
115  }
116 }
117 
119 {
120  if (d->isInitialized) {
121  d->engine->checkForInstalled();
122  } else {
123  d->checkForInstalled = true;
124  }
125 }
126 
127 void DownloadManagerPrivate::_k_slotEntriesLoaded(const KNSCore::EntryInternal::List &entries)
128 {
129  KNS3::Entry::List result;
130  result.reserve(entries.size());
131  for (const KNSCore::EntryInternal &entry : entries) {
132  result.append(EntryPrivate::fromInternal(&entry));
133  }
134  emit q->searchResult(result);
135 }
136 
137 void KNS3::DownloadManagerPrivate::_k_slotEntryStatusChanged(const KNSCore::EntryInternal &entry)
138 {
139  emit q->entryStatusChanged(EntryPrivate::fromInternal(&entry));
140 }
141 
143 {
144  KNSCore::EntryInternal entryInternal = KNSCore::EntryInternal::fromEntry(entry);
145  if (entryInternal.isValid()) {
146  d->engine->install(entryInternal);
147  }
148 }
149 
151 {
152  KNSCore::EntryInternal entryInternal = KNSCore::EntryInternal::fromEntry(entry);
153  if (entryInternal.isValid()) {
154  d->engine->uninstall(entryInternal);
155  }
156 }
157 
158 void DownloadManager::search(int page, int pageSize)
159 {
160  d->page = page;
161  d->pageSize = pageSize;
162 
163  if (d->isInitialized) {
164  d->engine->requestData(page, pageSize);
165  } else {
166  d->doSearch = true;
167  }
168 }
169 
170 void DownloadManager::setSearchOrder(DownloadManager::SortOrder order)
171 {
172  switch (order) {
173  case Newest:
174  d->engine->setSortMode(KNSCore::Provider::Newest);
175  break;
176  case Rating:
177  d->engine->setSortMode(KNSCore::Provider::Rating);
178  break;
179  case Alphabetical:
180  d->engine->setSortMode(KNSCore::Provider::Alphabetical);
181  break;
182  case Downloads:
183  d->engine->setSortMode(KNSCore::Provider::Downloads);
184  break;
185  }
186 }
187 
189 {
190  d->engine->setSearchTerm(searchTerm);
191 }
192 
194 {
195  d->engine->fetchEntryById(id);
196 }
197 
198 #include "moc_downloadmanager.cpp"
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.
Definition: button.cpp:18
QObject * parent() const const
Q_EMITQ_EMIT
QString applicationName()
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.