Warning, file /frameworks/knewstuff/src/downloadmanager.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     SPDX-FileCopyrightText: 2010 Frederik Gladhorn <gladhorn@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #include "downloadmanager.h"
0008 
0009 #include <QCoreApplication>
0010 #include <knewstuff_debug.h>
0011 
0012 #include "core/engine.h"
0013 #include "entry_p.h"
0014 #include "ui/widgetquestionlistener.h"
0015 
0016 #if KNEWSTUFF_BUILD_DEPRECATED_SINCE(5, 29)
0017 namespace KNS3
0018 {
0019 class DownloadManagerPrivate
0020 {
0021 public:
0022     DownloadManager *const q;
0023     KNSCore::Engine *const engine;
0024 
0025     DownloadManagerPrivate(DownloadManager *qq)
0026         : q(qq)
0027         , engine(new KNSCore::Engine)
0028     {
0029     }
0030     ~DownloadManagerPrivate()
0031     {
0032         delete engine;
0033     }
0034 
0035     bool isInitialized = false;
0036     bool checkForUpdates = false;
0037     bool checkForInstalled = false;
0038     bool doSearch = false;
0039 
0040     int page = 0;
0041     int pageSize = 100;
0042 
0043     void init(const QString &configFile);
0044     void _k_slotProvidersLoaded();
0045     void _k_slotEngineError(const QString &error);
0046     void _k_slotUpdatesLoaded(const KNSCore::EntryInternal::List &entries);
0047     void _k_slotEntryStatusChanged(const KNSCore::EntryInternal &entry);
0048     void _k_slotEntriesLoaded(const KNSCore::EntryInternal::List &entries);
0049 };
0050 }
0051 
0052 using namespace KNS3;
0053 
0054 DownloadManager::DownloadManager(QObject *parent)
0055     : QObject(parent)
0056     , d(new DownloadManagerPrivate(this))
0057 {
0058     QString name = QCoreApplication::applicationName();
0059     d->init(name + QStringLiteral(".knsrc"));
0060 }
0061 
0062 DownloadManager::DownloadManager(const QString &configFile, QObject *parent)
0063     : QObject(parent)
0064     , d(new DownloadManagerPrivate(this))
0065 {
0066     d->init(configFile);
0067 }
0068 
0069 void DownloadManagerPrivate::init(const QString &configFile)
0070 {
0071     q->connect(engine, SIGNAL(signalProvidersLoaded()), q, SLOT(_k_slotProvidersLoaded()));
0072     q->connect(engine, SIGNAL(signalUpdateableEntriesLoaded(KNSCore::EntryInternal::List)), q, SLOT(_k_slotEntriesLoaded(KNSCore::EntryInternal::List)));
0073     q->connect(engine, SIGNAL(signalEntriesLoaded(KNSCore::EntryInternal::List)), q, SLOT(_k_slotEntriesLoaded(KNSCore::EntryInternal::List)));
0074     q->connect(engine, SIGNAL(signalEntryChanged(KNSCore::EntryInternal)), q, SLOT(_k_slotEntryStatusChanged(KNSCore::EntryInternal)));
0075     q->connect(engine, SIGNAL(signalError(QString)), q, SLOT(_k_slotEngineError(QString)));
0076     engine->init(configFile);
0077     WidgetQuestionListener::instance();
0078 }
0079 
0080 DownloadManager::~DownloadManager()
0081 {
0082     delete d;
0083 }
0084 
0085 void DownloadManagerPrivate::_k_slotEngineError(const QString &error)
0086 {
0087     qCWarning(KNEWSTUFF) << "engine error" << error;
0088 
0089     Q_EMIT q->errorFound(error);
0090 }
0091 
0092 void DownloadManagerPrivate::_k_slotProvidersLoaded()
0093 {
0094     qCDebug(KNEWSTUFF) << "providers loaded";
0095     isInitialized = true;
0096     if (checkForInstalled) {
0097         engine->checkForInstalled();
0098     } else if (checkForUpdates) {
0099         engine->checkForUpdates();
0100     } else if (doSearch) {
0101         engine->requestData(page, pageSize);
0102     }
0103 }
0104 
0105 void DownloadManager::checkForUpdates()
0106 {
0107     if (d->isInitialized) {
0108         d->engine->checkForUpdates();
0109     } else {
0110         d->checkForUpdates = true;
0111     }
0112 }
0113 
0114 void KNS3::DownloadManager::checkForInstalled()
0115 {
0116     if (d->isInitialized) {
0117         d->engine->checkForInstalled();
0118     } else {
0119         d->checkForInstalled = true;
0120     }
0121 }
0122 
0123 void DownloadManagerPrivate::_k_slotEntriesLoaded(const KNSCore::EntryInternal::List &entries)
0124 {
0125     KNS3::Entry::List result;
0126     result.reserve(entries.size());
0127     for (const KNSCore::EntryInternal &entry : entries) {
0128         result.append(EntryPrivate::fromInternal(&entry));
0129     }
0130     Q_EMIT q->searchResult(result);
0131 }
0132 
0133 void KNS3::DownloadManagerPrivate::_k_slotEntryStatusChanged(const KNSCore::EntryInternal &entry)
0134 {
0135     Q_EMIT q->entryStatusChanged(EntryPrivate::fromInternal(&entry));
0136 }
0137 
0138 void DownloadManager::installEntry(const KNS3::Entry &entry)
0139 {
0140     KNSCore::EntryInternal entryInternal = KNSCore::EntryInternal::fromEntry(entry);
0141     if (entryInternal.isValid()) {
0142         d->engine->install(entryInternal);
0143     }
0144 }
0145 
0146 void DownloadManager::uninstallEntry(const KNS3::Entry &entry)
0147 {
0148     KNSCore::EntryInternal entryInternal = KNSCore::EntryInternal::fromEntry(entry);
0149     if (entryInternal.isValid()) {
0150         d->engine->uninstall(entryInternal);
0151     }
0152 }
0153 
0154 void DownloadManager::search(int page, int pageSize)
0155 {
0156     d->page = page;
0157     d->pageSize = pageSize;
0158 
0159     if (d->isInitialized) {
0160         d->engine->requestData(page, pageSize);
0161     } else {
0162         d->doSearch = true;
0163     }
0164 }
0165 
0166 void DownloadManager::setSearchOrder(DownloadManager::SortOrder order)
0167 {
0168     switch (order) {
0169     case Newest:
0170         d->engine->setSortMode(KNSCore::Provider::Newest);
0171         break;
0172     case Rating:
0173         d->engine->setSortMode(KNSCore::Provider::Rating);
0174         break;
0175     case Alphabetical:
0176         d->engine->setSortMode(KNSCore::Provider::Alphabetical);
0177         break;
0178     case Downloads:
0179         d->engine->setSortMode(KNSCore::Provider::Downloads);
0180         break;
0181     }
0182 }
0183 
0184 void DownloadManager::setSearchTerm(const QString &searchTerm)
0185 {
0186     d->engine->setSearchTerm(searchTerm);
0187 }
0188 
0189 void KNS3::DownloadManager::fetchEntryById(const QString &id)
0190 {
0191     d->engine->fetchEntryById(id);
0192 }
0193 
0194 #include "moc_downloadmanager.cpp"
0195 #endif