Changeset View
Changeset View
Standalone View
Standalone View
src/core/downloadmanager.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | Copyright (C) 2010 Frederik Gladhorn <gladhorn@kde.org> | ||||
3 | Copyright (C) 2016 Dan Leinir Turthra Jensen <admin@leinir.dk> | ||||
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_p.h" | ||||
26 | #include "entryinternal_p.h" | ||||
27 | | ||||
28 | namespace KNSCore | ||||
29 | { | ||||
30 | class DownloadManagerPrivate | ||||
31 | { | ||||
32 | public: | ||||
33 | DownloadManager *q; | ||||
34 | Engine *engine; | ||||
35 | | ||||
36 | DownloadManagerPrivate(DownloadManager *q) | ||||
37 | : q(q) | ||||
38 | , engine(new Engine) | ||||
39 | , isInitialized(false) | ||||
40 | , checkForUpdates(false) | ||||
41 | , checkForInstalled(false) | ||||
42 | , doSearch(false) | ||||
43 | , page(0) | ||||
44 | , pageSize(100) | ||||
45 | {} | ||||
46 | ~DownloadManagerPrivate() | ||||
47 | { | ||||
48 | delete engine; | ||||
49 | } | ||||
50 | | ||||
51 | bool isInitialized; | ||||
52 | bool checkForUpdates; | ||||
53 | bool checkForInstalled; | ||||
54 | bool doSearch; | ||||
55 | | ||||
56 | int page; | ||||
57 | int pageSize; | ||||
58 | | ||||
59 | void init(const QString &configFile); | ||||
60 | }; | ||||
61 | } | ||||
62 | | ||||
63 | using namespace KNSCore; | ||||
64 | | ||||
65 | DownloadManager::DownloadManager(QObject *parent) | ||||
66 | : QObject(parent) | ||||
67 | , d(new DownloadManagerPrivate(this)) | ||||
68 | { | ||||
69 | QString name = QCoreApplication::applicationName(); | ||||
70 | d->init(name + ".knsrc"); | ||||
71 | } | ||||
72 | | ||||
73 | DownloadManager::DownloadManager(const QString &configFile, QObject *parent) | ||||
74 | : QObject(parent) | ||||
75 | , d(new DownloadManagerPrivate(this)) | ||||
76 | { | ||||
77 | d->init(configFile); | ||||
78 | } | ||||
79 | | ||||
80 | void DownloadManagerPrivate::init(const QString &configFile) | ||||
81 | { | ||||
82 | q->connect(engine, SIGNAL(signalProvidersLoaded()), q, SLOT(slotProvidersLoaded())); | ||||
83 | q->connect(engine, SIGNAL(signalUpdateableEntriesLoaded(KNSCore::EntryInternal::List)), q, SIGNAL(searchResult(KNSCore::EntryInternal::List))); | ||||
84 | q->connect(engine, SIGNAL(signalEntriesLoaded(KNSCore::EntryInternal::List)), q, SIGNAL(searchResult(KNSCore::EntryInternal::List))); | ||||
85 | q->connect(engine, SIGNAL(signalEntryChanged(KNSCore::EntryInternal)), q, SIGNAL(entryStatusChanged(KNSCore::EntryInternal))); | ||||
86 | q->connect(engine, SIGNAL(signalError(QString)), q, SIGNAL(errorFound(QString))); | ||||
87 | engine->init(configFile); | ||||
88 | } | ||||
89 | | ||||
90 | DownloadManager::~DownloadManager() | ||||
91 | { | ||||
92 | delete d; | ||||
93 | } | ||||
94 | | ||||
95 | void DownloadManager::slotProvidersLoaded() | ||||
96 | { | ||||
97 | qCDebug(KNEWSTUFFCORE) << "providers loaded"; | ||||
98 | d->isInitialized = true; | ||||
99 | if (d->checkForInstalled) { | ||||
100 | d->engine->checkForInstalled(); | ||||
101 | } else if (d->checkForUpdates) { | ||||
102 | d->engine->checkForUpdates(); | ||||
103 | } else if (d->doSearch) { | ||||
104 | d->engine->requestData(d->page, d->pageSize); | ||||
105 | } | ||||
106 | } | ||||
107 | | ||||
108 | void DownloadManager::checkForUpdates() | ||||
109 | { | ||||
110 | if (d->isInitialized) { | ||||
111 | d->engine->checkForUpdates(); | ||||
112 | } else { | ||||
113 | d->checkForUpdates = true; | ||||
114 | } | ||||
115 | } | ||||
116 | | ||||
117 | void DownloadManager::checkForInstalled() | ||||
118 | { | ||||
119 | if (d->isInitialized) { | ||||
120 | d->engine->checkForInstalled(); | ||||
121 | } else { | ||||
122 | d->checkForInstalled = true; | ||||
123 | } | ||||
124 | } | ||||
125 | | ||||
126 | void DownloadManager::installEntry(const EntryInternal &entry) | ||||
127 | { | ||||
128 | if (entry.isValid()) { | ||||
129 | d->engine->install(entry); | ||||
130 | } | ||||
131 | } | ||||
132 | | ||||
133 | void DownloadManager::uninstallEntry(const EntryInternal &entry) | ||||
134 | { | ||||
135 | if (entry.isValid()) { | ||||
136 | d->engine->uninstall(entry); | ||||
137 | } | ||||
138 | } | ||||
139 | | ||||
140 | void DownloadManager::search(int page, int pageSize) | ||||
141 | { | ||||
142 | d->page = page; | ||||
143 | d->pageSize = pageSize; | ||||
144 | | ||||
145 | if (d->isInitialized) { | ||||
146 | d->engine->requestData(page, pageSize); | ||||
147 | } else { | ||||
148 | d->doSearch = true; | ||||
149 | } | ||||
150 | } | ||||
151 | | ||||
152 | void DownloadManager::setSearchOrder(DownloadManager::SortOrder order) | ||||
153 | { | ||||
154 | switch (order) { | ||||
155 | case Newest: | ||||
156 | d->engine->setSortMode(KNSCore::Provider::Newest); | ||||
157 | break; | ||||
158 | case Rating: | ||||
159 | d->engine->setSortMode(KNSCore::Provider::Rating); | ||||
160 | break; | ||||
161 | case Alphabetical: | ||||
162 | d->engine->setSortMode(KNSCore::Provider::Alphabetical); | ||||
163 | break; | ||||
164 | case Downloads: | ||||
165 | d->engine->setSortMode(KNSCore::Provider::Downloads); | ||||
166 | break; | ||||
167 | } | ||||
168 | } | ||||
169 | | ||||
170 | void DownloadManager::setSearchTerm(const QString &searchTerm) | ||||
171 | { | ||||
172 | d->engine->setSearchTerm(searchTerm); | ||||
173 | } | ||||
174 | | ||||
175 | void DownloadManager::fetchEntryById(const QString& id) | ||||
176 | { | ||||
177 | d->engine->fetchEntryById(id); | ||||
178 | } |