File indexing completed on 2024-04-28 15:28:54

0001 /*
0002     SPDX-FileCopyrightText: 2010 Frederik Gladhorn <gladhorn@kde.org>
0003     SPDX-FileCopyrightText: 2016 Dan Leinir Turthra Jensen <admin@leinir.dk>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-or-later
0006 */
0007 
0008 #include "knewstuff_export.h"
0009 #if KNEWSTUFF_BUILD_DEPRECATED_SINCE(5, 79)
0010 #include "downloadmanager.h"
0011 
0012 #include <knewstuffcore_debug.h>
0013 
0014 #include <QCoreApplication>
0015 
0016 #include "engine.h"
0017 
0018 namespace KNSCore
0019 {
0020 class DownloadManagerPrivate
0021 {
0022 public:
0023     DownloadManager *const q;
0024     Engine *const engine;
0025 
0026     DownloadManagerPrivate(DownloadManager *qq)
0027         : q(qq)
0028         , engine(new Engine)
0029     {
0030     }
0031     ~DownloadManagerPrivate()
0032     {
0033         delete engine;
0034     }
0035 
0036     bool isInitialized = false;
0037     bool checkForUpdates = false;
0038     bool checkForInstalled = false;
0039     bool doSearch = false;
0040 
0041     int page = 0;
0042     int pageSize = 100;
0043 
0044     void init(const QString &configFile);
0045 };
0046 }
0047 
0048 using namespace KNSCore;
0049 
0050 DownloadManager::DownloadManager(QObject *parent)
0051     : QObject(parent)
0052     , d(new DownloadManagerPrivate(this))
0053 {
0054     QString name = QCoreApplication::applicationName();
0055     d->init(name + QStringLiteral(".knsrc"));
0056 }
0057 
0058 DownloadManager::DownloadManager(const QString &configFile, QObject *parent)
0059     : QObject(parent)
0060     , d(new DownloadManagerPrivate(this))
0061 {
0062     d->init(configFile);
0063 }
0064 
0065 void DownloadManagerPrivate::init(const QString &configFile)
0066 {
0067     q->connect(engine, &KNSCore::Engine::signalProvidersLoaded, q, &DownloadManager::slotProvidersLoaded);
0068     q->connect(engine, &KNSCore::Engine::signalUpdateableEntriesLoaded, q, &DownloadManager::searchResult);
0069     q->connect(engine, &KNSCore::Engine::signalEntriesLoaded, q, &DownloadManager::searchResult);
0070     q->connect(engine, &KNSCore::Engine::signalEntryEvent, q, [this](const EntryInternal &entry, EntryInternal::EntryEvent event) {
0071         if (event == EntryInternal::StatusChangedEvent) {
0072             q->entryStatusChanged(entry);
0073         }
0074     });
0075     q->connect(engine, &KNSCore::Engine::signalErrorCode, q, [this](const KNSCore::ErrorCode &, const QString &message, const QVariant &) {
0076         Q_EMIT q->errorFound(message);
0077     });
0078     engine->init(configFile);
0079 }
0080 
0081 DownloadManager::~DownloadManager()
0082 {
0083     delete d;
0084 }
0085 
0086 void DownloadManager::slotProvidersLoaded()
0087 {
0088     qCDebug(KNEWSTUFFCORE) << "providers loaded";
0089     d->isInitialized = true;
0090     if (d->checkForInstalled) {
0091         d->engine->checkForInstalled();
0092     } else if (d->checkForUpdates) {
0093         d->engine->checkForUpdates();
0094     } else if (d->doSearch) {
0095         d->engine->requestData(d->page, d->pageSize);
0096     }
0097 }
0098 
0099 void DownloadManager::checkForUpdates()
0100 {
0101     if (d->isInitialized) {
0102         d->engine->checkForUpdates();
0103     } else {
0104         d->checkForUpdates = true;
0105     }
0106 }
0107 
0108 void DownloadManager::checkForInstalled()
0109 {
0110     if (d->isInitialized) {
0111         d->engine->checkForInstalled();
0112     } else {
0113         d->checkForInstalled = true;
0114     }
0115 }
0116 
0117 void DownloadManager::installEntry(const EntryInternal &entry)
0118 {
0119     if (entry.isValid()) {
0120         d->engine->install(entry);
0121     }
0122 }
0123 
0124 void DownloadManager::uninstallEntry(const EntryInternal &entry)
0125 {
0126     if (entry.isValid()) {
0127         d->engine->uninstall(entry);
0128     }
0129 }
0130 
0131 void DownloadManager::search(int page, int pageSize)
0132 {
0133     d->page = page;
0134     d->pageSize = pageSize;
0135 
0136     if (d->isInitialized) {
0137         d->engine->requestData(page, pageSize);
0138     } else {
0139         d->doSearch = true;
0140     }
0141 }
0142 
0143 void DownloadManager::setSearchOrder(DownloadManager::SortOrder order)
0144 {
0145     switch (order) {
0146     case Newest:
0147         d->engine->setSortMode(KNSCore::Provider::Newest);
0148         break;
0149     case Rating:
0150         d->engine->setSortMode(KNSCore::Provider::Rating);
0151         break;
0152     case Alphabetical:
0153         d->engine->setSortMode(KNSCore::Provider::Alphabetical);
0154         break;
0155     case Downloads:
0156         d->engine->setSortMode(KNSCore::Provider::Downloads);
0157         break;
0158     }
0159 }
0160 
0161 void DownloadManager::setSearchTerm(const QString &searchTerm)
0162 {
0163     d->engine->setSearchTerm(searchTerm);
0164 }
0165 
0166 void DownloadManager::fetchEntryById(const QString &id)
0167 {
0168     d->engine->fetchEntryById(id);
0169 }
0170 
0171 #include "moc_downloadmanager.cpp"
0172 
0173 #endif