Commit 8294f044 authored by Alexander Lohnau's avatar Alexander Lohnau 💬
Browse files

applets/comic: Refactor API to get rid of std::function parameter for callback

parent e6c372c9
......@@ -23,10 +23,12 @@ CheckNewStrips::CheckNewStrips(const QStringList &identifiers, ComicEngine *engi
// start at once, that way the user does not have to wait for minutes to get the initial result
start();
connect(mEngine, &ComicEngine::requestFinished, this, &CheckNewStrips::dataUpdated);
}
void CheckNewStrips::dataUpdated(const QString &source, const ComicMetaData &data)
void CheckNewStrips::dataUpdated(const ComicMetaData &data)
{
const QString source = data.identifier;
QString lastIdentifierSuffix;
if (!data.error) {
......@@ -43,9 +45,7 @@ void CheckNewStrips::dataUpdated(const QString &source, const ComicMetaData &dat
if (mIndex < mIdentifiers.count()) {
const QString newSource = mIdentifiers[mIndex] + QLatin1Char(':');
mEngine->requestSource(newSource, [this, newSource](const auto &data) {
dataUpdated(newSource, data);
});
mEngine->requestSource(newSource);
} else {
mIndex = 0;
}
......@@ -60,8 +60,6 @@ void CheckNewStrips::start()
if (mIndex < mIdentifiers.count()) {
const QString newSource = mIdentifiers[mIndex] + QLatin1Char(':');
mEngine->requestSource(newSource, [this, newSource](const auto &data) {
dataUpdated(newSource, data);
});
mEngine->requestSource(newSource);
}
}
......@@ -34,7 +34,7 @@ private Q_SLOTS:
void start();
private:
void dataUpdated(const QString &name, const ComicMetaData &data);
void dataUpdated(const ComicMetaData &data);
int mMinutes;
int mIndex;
ComicEngine *mEngine;
......
......@@ -14,6 +14,7 @@
#include "comic_debug.h"
#include "comicarchivedialog.h"
#include "comicarchivejob.h"
#include "comicdata.h"
#include "comicsaver.h"
#include "stripselector.h"
......@@ -70,6 +71,7 @@ void ComicApplet::init()
configChanged();
mEngine = new ComicEngine(this);
connect(mEngine, &ComicEngine::requestFinished, this, &ComicApplet::dataUpdated);
mModel = new ComicModel(mEngine, mTabIdentifier, this);
mProxy = new QSortFilterProxyModel(this);
mProxy->setSourceModel(mModel);
......@@ -141,9 +143,7 @@ void ComicApplet::init()
return;
}
qCDebug(PLASMA_COMIC) << "Online status changed to true, requesting comic" << mPreviousFailedIdentifier;
mEngine->requestSource(mPreviousFailedIdentifier, [this](const auto &data) {
dataUpdated(mPreviousFailedIdentifier, data);
});
mEngine->requestSource(mPreviousFailedIdentifier);
});
QT_WARNING_POP
}
......@@ -153,12 +153,16 @@ ComicApplet::~ComicApplet()
delete mSavingDir;
}
void ComicApplet::dataUpdated(const QString &source, const ComicMetaData &data)
void ComicApplet::dataUpdated(const ComicMetaData &data)
{
const QString source = data.identifier;
if (source.startsWith(mOldSource)) {
mOldSource = source;
}
setBusy(false);
// disconnect prefetched comic strips
if (mEngine && source != mOldSource) {
if (source != mOldSource) {
return;
}
......@@ -191,15 +195,11 @@ void ComicApplet::dataUpdated(const QString &source, const ComicMetaData &data)
// prefetch the previous and following comic for faster navigation
if (mCurrent.hasNext()) {
const QString prefetch = mCurrent.id() + QLatin1Char(':') + mCurrent.next();
mEngine->requestSource(prefetch, [this, prefetch](const auto &data) {
dataUpdated(prefetch, data);
});
mEngine->requestSource(prefetch);
}
if (mCurrent.hasPrev()) {
const QString prefetch = mCurrent.id() + QLatin1Char(':') + mCurrent.prev();
mEngine->requestSource(prefetch, [this, prefetch](const auto &data) {
dataUpdated(prefetch, data);
});
mEngine->requestSource(prefetch);
}
}
......@@ -477,9 +477,7 @@ void ComicApplet::updateComic(const QString &identifierSuffix)
mIdentifierError.clear();
}
mOldSource = identifier;
mEngine->requestSource(identifier, [this, identifier](const auto &data) {
dataUpdated(identifier, data);
});
mEngine->requestSource(identifier);
slotScaleToContent();
} else {
qWarning() << "Either no identifier was specified or the engine could not be created:"
......
......@@ -169,7 +169,7 @@ private:
void refreshComicData();
void setTabHighlighted(const QString &id, bool highlight);
bool isTabHighlighted(const QString &id) const;
void dataUpdated(const QString &name, const ComicMetaData &data);
void dataUpdated(const ComicMetaData &data);
private:
static const int CACHE_LIMIT;
......
......@@ -46,6 +46,7 @@ ComicArchiveJob::ComicArchiveJob(const QUrl &dest,
} else {
qWarning() << "Could not create a temporary file for the zip file.";
}
connect(engine, &ComicEngine::requestFinished, this, &ComicArchiveJob::dataUpdated);
}
ComicArchiveJob::~ComicArchiveJob()
......@@ -122,8 +123,9 @@ void ComicArchiveJob::start()
}
}
void ComicArchiveJob::dataUpdated(const QString &source, const ComicMetaData &data)
void ComicArchiveJob::dataUpdated(const ComicMetaData &data)
{
const QString source = data.identifier;
if (!mZip) {
qWarning() << "No zip file, aborting.";
setErrorText(i18n("No zip file is existing, aborting."));
......@@ -331,9 +333,7 @@ void ComicArchiveJob::requestComic(QString identifier) // krazy:exclude=passbyva
qMakePair(QStringLiteral("source"), identifier),
qMakePair(QStringLiteral("destination"), mDest.toString()));
mEngine->requestSource(identifier, [this, identifier](const auto &data) {
dataUpdated(identifier, data);
});
mEngine->requestSource(identifier);
}
bool ComicArchiveJob::addFileToZip(const QString &path)
......
......@@ -72,7 +72,7 @@ protected:
bool doResume() override;
private:
void dataUpdated(const QString &source, const ComicMetaData &data);
void dataUpdated(const ComicMetaData &data);
/**
* Sets the total number of comics to download.
* @param currentSuffix if empty the from and to identifier suffix will be used.
......
......@@ -60,7 +60,7 @@ void ComicEngine::setMaxComicLimit(int maxComicLimit)
CachedProvider::setMaxComicLimit(maxComicLimit);
}
bool ComicEngine::requestSource(const QString &identifier, ComicRequestCallback callback)
bool ComicEngine::requestSource(const QString &identifier)
{
if (m_jobs.contains(identifier)) {
return true;
......@@ -72,18 +72,14 @@ bool ComicEngine::requestSource(const QString &identifier, ComicRequestCallback
if (parts.count() > 1 && (CachedProvider::isCached(identifier) || !m_networkConfigurationManager.isOnline())) {
ComicProvider *provider = new CachedProvider(this, KPluginMetaData{}, IdentifierType::StringIdentifier, identifier);
m_jobs[identifier] = provider;
connect(provider, &ComicProvider::finished, this, [this, callback, provider]() {
finished(provider, callback);
});
connect(provider, &ComicProvider::error, this, [this, callback, provider]() {
error(provider, callback);
});
connect(provider, &ComicProvider::finished, this, &ComicEngine::finished);
connect(provider, &ComicProvider::error, this, &ComicEngine::error);
return true;
}
// ... start a new query otherwise
if (parts.count() < 2) {
callback(ComicMetaData{.error = true});
Q_EMIT requestFinished(ComicMetaData{.error = true});
qWarning() << "Less than two arguments specified.";
return false;
}
......@@ -91,7 +87,7 @@ bool ComicEngine::requestSource(const QString &identifier, ComicRequestCallback
// User might have installed more from GHNS
loadProviders();
if (!mProviders.contains(parts[0])) {
callback(ComicMetaData{.error = true});
Q_EMIT requestFinished(ComicMetaData{.error = true});
qWarning() << identifier << "comic plugin does not seem to be installed.";
return false;
}
......@@ -106,7 +102,7 @@ bool ComicEngine::requestSource(const QString &identifier, ComicRequestCallback
data.errorAutomaticallyFixable = true;
data.identifier = identifier;
data.previousIdentifier = lastCachedIdentifier(identifier);
callback(data);
Q_EMIT requestFinished(data);
qCDebug(PLASMA_COMIC) << "No internet connection, using cached data";
return true;
}
......@@ -136,25 +132,22 @@ bool ComicEngine::requestSource(const QString &identifier, ComicRequestCallback
m_jobs[identifier] = provider;
connect(provider, &ComicProvider::finished, this, [this, callback, provider]() {
finished(provider, callback);
});
connect(provider, &ComicProvider::error, this, [this, callback, provider]() {
error(provider, callback);
});
connect(provider, &ComicProvider::finished, this, &ComicEngine::finished);
connect(provider, &ComicProvider::error, this, &ComicEngine::error);
return true;
}
void ComicEngine::finished(ComicProvider *provider, ComicRequestCallback callback)
void ComicEngine::finished(ComicProvider *provider)
{
// sets the data
setComicData(provider, callback);
if (provider->image().isNull()) {
qCWarning(PLASMA_COMIC) << "Provider returned null image" << provider->name();
error(provider, callback);
error(provider);
return;
}
ComicMetaData data = metaDataFromProvider(provider);
// different comic -- with no error yet -- has been chosen, old error is invalidated
QString temp = mIdentifierError.left(mIdentifierError.indexOf(QLatin1Char(':')) + 1);
if (!mIdentifierError.isEmpty() && provider->identifier().indexOf(temp) == -1) {
......@@ -167,34 +160,7 @@ void ComicEngine::finished(ComicProvider *provider, ComicRequestCallback callbac
// store in cache if it's not the response of a CachedProvider,
if (!provider->inherits("CachedProvider") && !provider->image().isNull()) {
ComicMetaData info;
info.websiteUrl = provider->websiteUrl();
info.imageUrl = provider->imageUrl();
info.shopUrl = provider->shopUrl();
info.nextIdentifier = provider->nextIdentifier();
info.previousIdentifier = provider->previousIdentifier();
info.providerName = provider->name();
info.identifierType = provider->identifierType();
info.lastCachedStripIdentifier = provider->identifier().mid(provider->identifier().indexOf(QLatin1Char(':')) + 1);
info.isLeftToRight = provider->isLeftToRight();
info.isTopToBottom = provider->isTopToBottom();
// data that should be only written if available
if (!provider->comicAuthor().isEmpty()) {
info.comicAuthor = provider->comicAuthor();
}
if (!provider->firstStripIdentifier().isEmpty()) {
info.firstStripIdentifier = provider->firstStripIdentifier();
}
if (!provider->additionalText().isEmpty()) {
info.additionalText = provider->additionalText();
}
if (!provider->stripTitle().isEmpty()) {
info.stripTitle = provider->stripTitle();
}
CachedProvider::storeInCache(provider->identifier(), provider->image(), info);
CachedProvider::storeInCache(provider->identifier(), provider->image(), data);
}
provider->deleteLater();
......@@ -202,13 +168,11 @@ void ComicEngine::finished(ComicProvider *provider, ComicRequestCallback callbac
if (!key.isEmpty()) {
m_jobs.remove(key);
}
Q_EMIT requestFinished(data);
}
void ComicEngine::error(ComicProvider *provider, ComicRequestCallback callback)
void ComicEngine::error(ComicProvider *provider)
{
// sets the data
setComicData(provider, callback);
QString identifier(provider->identifier());
mIdentifierError = identifier;
......@@ -223,7 +187,7 @@ void ComicEngine::error(ComicProvider *provider, ComicRequestCallback callback)
identifier = identifier.left(identifier.indexOf(QLatin1Char(':')) + 1);
}
ComicMetaData data;
ComicMetaData data = metaDataFromProvider(provider);
data.identifier = identifier;
data.error = true;
......@@ -241,10 +205,10 @@ void ComicEngine::error(ComicProvider *provider, ComicRequestCallback callback)
}
provider->deleteLater();
callback(data);
Q_EMIT requestFinished(data);
}
void ComicEngine::setComicData(ComicProvider *provider, ComicRequestCallback callback)
ComicMetaData ComicEngine::metaDataFromProvider(ComicProvider *provider)
{
QString identifier(provider->identifier());
......@@ -274,7 +238,7 @@ void ComicEngine::setComicData(ComicProvider *provider, ComicRequestCallback cal
data.isLeftToRight = provider->isLeftToRight();
data.isTopToBottom = provider->isTopToBottom();
callback(data);
return data;
}
QString ComicEngine::lastCachedIdentifier(const QString &identifier) const
......
......@@ -42,13 +42,15 @@ public:
QList<ComicProviderInfo> loadProviders();
void setMaxComicLimit(int maxComicLimit);
using ComicRequestCallback = const std::function<void(const ComicMetaData &data)> &;
bool requestSource(const QString &identifier, ComicRequestCallback callback);
bool requestSource(const QString &identifier);
Q_SIGNALS:
void requestFinished(const ComicMetaData &data);
private:
void finished(ComicProvider *, ComicRequestCallback callback);
void error(ComicProvider *, ComicRequestCallback callback);
void setComicData(ComicProvider *provider, ComicRequestCallback callback);
void finished(ComicProvider *);
void error(ComicProvider *);
ComicMetaData metaDataFromProvider(ComicProvider *provider);
QString lastCachedIdentifier(const QString &identifier) const;
private:
......
......@@ -27,7 +27,7 @@ public:
{
mTimer = new QTimer(parent);
mTimer->setSingleShot(true);
mTimer->setInterval(15000); // timeout after 15 seconds
mTimer->setInterval(60000); // timeout after 1 minute
connect(mTimer, &QTimer::timeout, mParent, [this]() {
// operation took too long, abort it
Q_EMIT mParent->error(mParent);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment