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 *qq)
26  : q(qq)
27  , engine(new KNSCore::Engine)
28  {
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  void _k_slotProvidersLoaded();
45  void _k_slotEngineError(const QString &error);
46  void _k_slotUpdatesLoaded(const KNSCore::EntryInternal::List &entries);
47  void _k_slotEntryStatusChanged(const KNSCore::EntryInternal &entry);
48  void _k_slotEntriesLoaded(const KNSCore::EntryInternal::List &entries);
49 };
50 }
51 
52 using namespace KNS3;
53 
55  : QObject(parent)
56  , d(new DownloadManagerPrivate(this))
57 {
59  d->init(name + QStringLiteral(".knsrc"));
60 }
61 
63  : QObject(parent)
64  , d(new DownloadManagerPrivate(this))
65 {
66  d->init(configFile);
67 }
68 
69 void DownloadManagerPrivate::init(const QString &configFile)
70 {
71  q->connect(engine, SIGNAL(signalProvidersLoaded()), q, SLOT(_k_slotProvidersLoaded()));
72  q->connect(engine, SIGNAL(signalUpdateableEntriesLoaded(KNSCore::EntryInternal::List)), q, SLOT(_k_slotEntriesLoaded(KNSCore::EntryInternal::List)));
73  q->connect(engine, SIGNAL(signalEntriesLoaded(KNSCore::EntryInternal::List)), q, SLOT(_k_slotEntriesLoaded(KNSCore::EntryInternal::List)));
74  q->connect(engine, SIGNAL(signalEntryChanged(KNSCore::EntryInternal)), q, SLOT(_k_slotEntryStatusChanged(KNSCore::EntryInternal)));
75  q->connect(engine, SIGNAL(signalError(QString)), q, SLOT(_k_slotEngineError(QString)));
76  engine->init(configFile);
77  WidgetQuestionListener::instance();
78 }
79 
81 {
82  delete d;
83 }
84 
85 void DownloadManagerPrivate::_k_slotEngineError(const QString &error)
86 {
87  qCWarning(KNEWSTUFF) << "engine error" << error;
88 
89  Q_EMIT q->errorFound(error);
90 }
91 
92 void DownloadManagerPrivate::_k_slotProvidersLoaded()
93 {
94  qCDebug(KNEWSTUFF) << "providers loaded";
95  isInitialized = true;
96  if (checkForInstalled) {
97  engine->checkForInstalled();
98  } else if (checkForUpdates) {
99  engine->checkForUpdates();
100  } else if (doSearch) {
101  engine->requestData(page, pageSize);
102  }
103 }
104 
106 {
107  if (d->isInitialized) {
108  d->engine->checkForUpdates();
109  } else {
110  d->checkForUpdates = true;
111  }
112 }
113 
115 {
116  if (d->isInitialized) {
117  d->engine->checkForInstalled();
118  } else {
119  d->checkForInstalled = true;
120  }
121 }
122 
123 void DownloadManagerPrivate::_k_slotEntriesLoaded(const KNSCore::EntryInternal::List &entries)
124 {
125  KNS3::Entry::List result;
126  result.reserve(entries.size());
127  for (const KNSCore::EntryInternal &entry : entries) {
128  result.append(EntryPrivate::fromInternal(&entry));
129  }
130  Q_EMIT q->searchResult(result);
131 }
132 
133 void KNS3::DownloadManagerPrivate::_k_slotEntryStatusChanged(const KNSCore::EntryInternal &entry)
134 {
135  Q_EMIT q->entryStatusChanged(EntryPrivate::fromInternal(&entry));
136 }
137 
139 {
140  KNSCore::EntryInternal entryInternal = KNSCore::EntryInternal::fromEntry(entry);
141  if (entryInternal.isValid()) {
142  d->engine->install(entryInternal);
143  }
144 }
145 
147 {
148  KNSCore::EntryInternal entryInternal = KNSCore::EntryInternal::fromEntry(entry);
149  if (entryInternal.isValid()) {
150  d->engine->uninstall(entryInternal);
151  }
152 }
153 
154 void DownloadManager::search(int page, int pageSize)
155 {
156  d->page = page;
157  d->pageSize = pageSize;
158 
159  if (d->isInitialized) {
160  d->engine->requestData(page, pageSize);
161  } else {
162  d->doSearch = true;
163  }
164 }
165 
166 void DownloadManager::setSearchOrder(DownloadManager::SortOrder order)
167 {
168  switch (order) {
169  case Newest:
170  d->engine->setSortMode(KNSCore::Provider::Newest);
171  break;
172  case Rating:
173  d->engine->setSortMode(KNSCore::Provider::Rating);
174  break;
175  case Alphabetical:
176  d->engine->setSortMode(KNSCore::Provider::Alphabetical);
177  break;
178  case Downloads:
179  d->engine->setSortMode(KNSCore::Provider::Downloads);
180  break;
181  }
182 }
183 
185 {
186  d->engine->setSearchTerm(searchTerm);
187 }
188 
190 {
191  d->engine->fetchEntryById(id);
192 }
193 
194 #include "moc_downloadmanager.cpp"
195 #endif
void append(const T &value)
Encapsulates a KNSCore::Engine for use in Qt Quick.
Definition: quickengine.h:27
KNewStuff data entry container.
Definition: entryinternal.h:52
KNewStuff engine.
Definition: engine.h:60
void uninstallEntry(const KNS3::Entry &entry)
Uninstalls the given entry.
KNewStuff update checker.
KNewStuff information about changed entries.
Definition: entry.h:35
void checkForInstalled()
Check for installed resources Use searchResult to get notified about installed entries.
void installEntry(const KNS3::Entry &entry)
Installs or updates an entry.
void search(int page=0, int pageSize=100)
Search for a list of entries.
void reserve(int alloc)
int size() const const
The namespace for the KNewStuff classes.
Definition: button.cpp:21
~DownloadManager() override
destructor
void init(KXmlGuiWindow *window, KgDifficulty *difficulty=nullptr)
DownloadManager(QObject *parent=nullptr)
Create a DownloadManager It will try to find a appname.knsrc file.
void fetchEntryById(const QString &id)
Triggers a search for an entry with id as its unique id.
void checkForUpdates()
Check for available updates.
Contains the core functionality for handling interaction with NewStuff providers.
void setSearchTerm(const QString &searchTerm)
Sets the search term to filter the results on the server.
void setSearchOrder(SortOrder order)
Set the sort order of the results.
KREPORT_EXPORT QPageSize::PageSizeId pageSize(const QString &key)
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Fri Aug 12 2022 03:54:03 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.