Commit 1026957a authored by Alexander Lohnau's avatar Alexander Lohnau 💬
Browse files

applets/comic: Fully remove DataEngines

parent ac153d25
......@@ -5,10 +5,11 @@
*/
#include "checknewstrips.h"
#include "engine/comic.h"
#include <QTimer>
CheckNewStrips::CheckNewStrips(const QStringList &identifiers, Plasma::DataEngine *engine, int minutes, QObject *parent)
CheckNewStrips::CheckNewStrips(const QStringList &identifiers, ComicEngine *engine, int minutes, QObject *parent)
: QObject(parent)
, mMinutes(minutes)
, mIndex(0)
......@@ -24,7 +25,7 @@ CheckNewStrips::CheckNewStrips(const QStringList &identifiers, Plasma::DataEngin
start();
}
void CheckNewStrips::dataUpdated(const QString &source, const Plasma::DataEngine::Data &data)
void CheckNewStrips::dataUpdated(const QString &source, const QVariantMap &data)
{
QString lastIdentifierSuffix;
......@@ -33,8 +34,6 @@ void CheckNewStrips::dataUpdated(const QString &source, const Plasma::DataEngine
lastIdentifierSuffix.remove(source);
}
mEngine->disconnectSource(source, this);
if (!lastIdentifierSuffix.isEmpty()) {
QString temp = source;
temp.remove(QLatin1Char(':'));
......@@ -44,7 +43,9 @@ void CheckNewStrips::dataUpdated(const QString &source, const Plasma::DataEngine
if (mIndex < mIdentifiers.count()) {
const QString newSource = mIdentifiers[mIndex] + QLatin1Char(':');
mEngine->connectSource(newSource, this);
mEngine->requestSource(newSource, [this, newSource](const QVariantMap &data) {
dataUpdated(newSource, data);
});
} else {
mIndex = 0;
}
......@@ -59,6 +60,8 @@ void CheckNewStrips::start()
if (mIndex < mIdentifiers.count()) {
const QString newSource = mIdentifiers[mIndex] + QLatin1Char(':');
mEngine->connectSource(newSource, this);
mEngine->requestSource(newSource, [this, newSource](const QVariantMap &data) {
dataUpdated(newSource, data);
});
}
}
......@@ -7,7 +7,7 @@
#ifndef CHECK_NEW_STRIPS_H
#define CHECK_NEW_STRIPS_H
#include <Plasma/DataEngine>
#include "engine/comic.h"
/**
* This class searches for the newest comic strips of predefined comics in a defined interval.
......@@ -18,7 +18,7 @@ class CheckNewStrips : public QObject
Q_OBJECT
public:
CheckNewStrips(const QStringList &identifiers, Plasma::DataEngine *engine, int minutes, QObject *parent = nullptr);
CheckNewStrips(const QStringList &identifiers, ComicEngine *engine, int minutes, QObject *parent = nullptr);
Q_SIGNALS:
/**
......@@ -30,7 +30,7 @@ Q_SIGNALS:
void lastStrip(int index, const QString &identifier, const QString &suffix);
public Q_SLOTS:
void dataUpdated(const QString &name, const Plasma::DataEngine::Data &data);
void dataUpdated(const QString &name, const QVariantMap &data);
private Q_SLOTS:
void start();
......@@ -38,7 +38,7 @@ private Q_SLOTS:
private:
int mMinutes;
int mIndex;
Plasma::DataEngine *mEngine;
ComicEngine *mEngine;
const QStringList mIdentifiers;
};
......
......@@ -141,13 +141,12 @@ ComicApplet::~ComicApplet()
delete mSavingDir;
}
void ComicApplet::dataUpdated(const QString &source, const Plasma::DataEngine::Data &data)
void ComicApplet::dataUpdated(const QString &source, const QVariantMap &data)
{
setBusy(false);
// disconnect prefetched comic strips
if (mEngine && source != mOldSource) {
mEngine->disconnectSource(source, this);
return;
}
......@@ -159,7 +158,6 @@ void ComicApplet::dataUpdated(const QString &source, const Plasma::DataEngine::D
if (hasError) {
const QString previousIdentifierSuffix = data[QStringLiteral("Previous identifier suffix")].toString();
if (mEngine && !mShowErrorPicture && !previousIdentifierSuffix.isEmpty()) {
mEngine->disconnectSource(source, this);
updateComic(previousIdentifierSuffix);
}
return;
......@@ -180,19 +178,18 @@ void ComicApplet::dataUpdated(const QString &source, const Plasma::DataEngine::D
slotStorePosition();
if (mEngine) {
// disconnect if there is either no error, or an error that can not be fixed automatically
if (!errorAutoFixable) {
mEngine->disconnectSource(source, this);
}
// prefetch the previous and following comic for faster navigation
if (mCurrent.hasNext()) {
const QString prefetch = mCurrent.id() + QLatin1Char(':') + mCurrent.next();
mEngine->connectSource(prefetch, this);
mEngine->requestSource(prefetch, [this, prefetch](const QVariantMap &data) {
dataUpdated(prefetch, data);
});
}
if (mCurrent.hasPrev()) {
const QString prefetch = mCurrent.id() + QLatin1Char(':') + mCurrent.prev();
mEngine->connectSource(prefetch, this);
mEngine->requestSource(prefetch, [this, prefetch](const QVariantMap &data) {
dataUpdated(prefetch, data);
});
}
}
......@@ -333,8 +330,7 @@ void ComicApplet::configChanged()
auto oldMaxComicLimit = mMaxComicLimit;
mMaxComicLimit = cg.readEntry("maxComicLimit", CACHE_LIMIT);
if (oldMaxComicLimit != mMaxComicLimit && mEngine) {
mEngine->disconnectSource(QLatin1String("setting_maxComicLimit:") + QString::number(oldMaxComicLimit), this);
mEngine->connectSource(QLatin1String("setting_maxComicLimit:") + QString::number(mMaxComicLimit), this);
mEngine->setMaxComicLimit(mMaxComicLimit);
}
globalComicUpdater->load();
......@@ -475,18 +471,16 @@ void ComicApplet::updateComic(const QString &identifierSuffix)
// if there was an error only disconnect the oldSource if it had nothing to do with the error or if the comic changed, that way updates of the error can
// come in
if (!mIdentifierError.isEmpty() && !mIdentifierError.contains(id)) {
mEngine->disconnectSource(mIdentifierError, this);
mIdentifierError.clear();
}
if ((mIdentifierError != mOldSource) && (mOldSource != identifier)) {
mEngine->disconnectSource(mOldSource, this);
}
mOldSource = identifier;
mEngine->connectSource(identifier, this);
mEngine->requestSource(identifier, [this, identifier](const QVariantMap &data) {
dataUpdated(identifier, data);
});
slotScaleToContent();
} else {
qWarning() << "Either no identifier was specified or the engine could not be created:"
<< "id" << id << "engine valid:" << (mEngine && mEngine->isValid());
<< "id" << id;
setConfigurationRequired(true);
}
updateContextMenu();
......
......@@ -118,7 +118,7 @@ Q_SIGNALS:
void maxComicLimitChanged();
public Q_SLOTS:
void dataUpdated(const QString &name, const Plasma::DataEngine::Data &data);
void dataUpdated(const QString &name, const QVariantMap &data);
private Q_SLOTS:
void slotTabChanged(const QString &newIdentifier);
......
......@@ -15,7 +15,7 @@
#include <QImage>
ComicArchiveJob::ComicArchiveJob(const QUrl &dest,
Plasma::DataEngine *engine,
ComicEngine *engine,
ComicArchiveJob::ArchiveType archiveType,
IdentifierType identifierType,
const QString &pluginName,
......@@ -80,7 +80,7 @@ bool ComicArchiveJob::isValid() const
break;
}
return mEngine->isValid() && mZip && mZip->isOpen();
return mZip && mZip->isOpen();
}
void ComicArchiveJob::setToIdentifier(const QString &toIdentifier)
......@@ -121,7 +121,7 @@ void ComicArchiveJob::start()
}
}
void ComicArchiveJob::dataUpdated(const QString &source, const Plasma::DataEngine::Data &data)
void ComicArchiveJob::dataUpdated(const QString &source, const QVariantMap &data)
{
if (!mZip) {
qWarning() << "No zip file, aborting.";
......@@ -227,8 +227,6 @@ void ComicArchiveJob::dataUpdated(const QString &source, const Plasma::DataEngin
setError(KilledJobError);
emitResultIfNeeded();
}
mEngine->disconnectSource(source, this);
}
bool ComicArchiveJob::doKill()
......@@ -332,8 +330,9 @@ void ComicArchiveJob::requestComic(QString identifier) // krazy:exclude=passbyva
qMakePair(QStringLiteral("source"), identifier),
qMakePair(QStringLiteral("destination"), mDest.toString()));
mEngine->connectSource(identifier, this);
// mEngine->query( identifier );
mEngine->requestSource(identifier, [this, identifier](const QVariantMap &data) {
dataUpdated(identifier, data);
});
}
bool ComicArchiveJob::addFileToZip(const QString &path)
......
......@@ -10,8 +10,8 @@
#include "comicinfo.h"
#include "engine/comic.h"
#include <KIO/Job>
#include <Plasma/DataEngine>
class QTemporaryFile;
class KZip;
......@@ -30,7 +30,7 @@ public:
/**
* Creates a comic archive job.
* The engine has to be a working comic dataengine.
* The engine has to be a working comic engine.
* The archiveType defines what kind of input is given, e.g. if ArchiveAll is
* used no other parameters need to be defined, while ArchiveFromTo needs
* both toIdentifier and fromIdentifier (from <= to!), the other two types need only the toIdentifier.
......@@ -39,7 +39,7 @@ public:
* @see setToIdentifier, setFromIdentifier
*/
ComicArchiveJob(const QUrl &dest,
Plasma::DataEngine *engine,
ComicEngine *engine,
ArchiveType archiveType,
IdentifierType identifierType,
const QString &pluginName,
......@@ -66,7 +66,7 @@ public:
void start() override;
public Q_SLOTS:
void dataUpdated(const QString &source, const Plasma::DataEngine::Data &data);
void dataUpdated(const QString &source, const QVariantMap &data);
protected:
bool doKill() override;
......@@ -119,7 +119,7 @@ private:
int mComicNumber;
int mProcessedFiles;
int mTotalFiles;
Plasma::DataEngine *mEngine;
ComicEngine *mEngine;
QTemporaryFile *mZipFile;
KZip *mZip;
QString mPluginName;
......
......@@ -54,7 +54,7 @@ void ComicData::storePosition(bool store)
save();
}
void ComicData::setData(const Plasma::DataEngine::Data &data)
void ComicData::setData(const QVariantMap &data)
{
const bool hasError = data[QStringLiteral("Error")].toBool();
if (!hasError) {
......@@ -111,7 +111,7 @@ void ComicData::setData(const Plasma::DataEngine::Data &data)
save();
}
void ComicData::createErrorPicture(const Plasma::DataEngine::Data &data)
void ComicData::createErrorPicture(const QVariantMap &data)
{
QPixmap errorPic(500, 400);
errorPic.fill();
......
......@@ -9,8 +9,8 @@
#include "comicinfo.h"
#include <Plasma/DataEngine>
// Qt
#include <KConfigGroup>
#include <QImage>
#include <QString>
#include <QUrl>
......@@ -22,7 +22,7 @@ public:
void init(const QString &id, const KConfigGroup &config);
void setData(const Plasma::DataEngine::Data &data);
void setData(const QVariantMap &data);
IdentifierType type() const
{
......@@ -194,7 +194,7 @@ public:
private:
void load();
void createErrorPicture(const Plasma::DataEngine::Data &data);
void createErrorPicture(const QVariantMap &data);
private:
IdentifierType mType;
......
......@@ -11,19 +11,14 @@
#include <QTime>
#include <QWidget>
#include <KConfigGroup>
#include <KNSCore/EntryInternal>
#include <Plasma/DataEngine>
namespace KNSCore
{
class Engine;
}
namespace Plasma
{
class DataEngine;
}
class ComicUpdater : public QObject
{
Q_OBJECT
......
......@@ -22,10 +22,10 @@
#include "comicproviderkross.h"
ComicEngine::ComicEngine()
: Plasma::DataEngine(nullptr, QVariantList())
: QObject()
, mEmptySuffix(false)
{
setPollingInterval(0);
// TODO setPollingInterval(0);
loadProviders();
}
......@@ -41,7 +41,7 @@ void ComicEngine::init()
void ComicEngine::onOnlineStateChanged(bool isOnline)
{
if (isOnline && !mIdentifierError.isEmpty()) {
sourceRequestEvent(mIdentifierError);
// TODO sourceRequestEvent(mIdentifierError);
}
}
......@@ -66,22 +66,16 @@ QList<ComicProviderInfo> ComicEngine::loadProviders()
mProviders << comic.pluginId();
providers << data;
}
forceImmediateUpdateOfAllVisualizations();
return providers;
}
bool ComicEngine::updateSourceEvent(const QString &identifier)
void ComicEngine::setMaxComicLimit(int maxComicLimit)
{
CachedProvider::setMaxComicLimit(maxComicLimit);
}
bool ComicEngine::requestSource(const QString &identifier, ComicRequestCallback callback)
{
if (identifier == QLatin1String("providers")) {
Q_UNREACHABLE();
} else if (identifier.startsWith(QLatin1String("setting_maxComicLimit:"))) {
bool worked;
const int maxComicLimit = identifier.mid(22).toInt(&worked);
if (worked) {
CachedProvider::setMaxComicLimit(maxComicLimit);
}
return worked;
} else {
if (m_jobs.contains(identifier)) {
return true;
}
......@@ -95,14 +89,18 @@ bool ComicEngine::updateSourceEvent(const QString &identifier)
ComicProvider *provider = new CachedProvider(this, args);
m_jobs[identifier] = provider;
connect(provider, &ComicProvider::finished, this, &ComicEngine::finished);
connect(provider, &ComicProvider::error, this, &ComicEngine::error);
connect(provider, &ComicProvider::finished, this, [this, callback, provider]() {
finished(provider, callback);
});
connect(provider, &ComicProvider::error, this, [this, callback, provider]() {
error(provider, callback);
});
return true;
}
// ... start a new query otherwise
if (parts.count() < 2) {
setData(identifier, QLatin1String("Error"), true);
callback(QVariantMap{{QLatin1String("Error"), true}});
qWarning() << "Less than two arguments specified.";
return false;
}
......@@ -110,7 +108,7 @@ bool ComicEngine::updateSourceEvent(const QString &identifier)
// User might have installed more from GHNS
loadProviders();
if (!mProviders.contains(parts[0])) {
setData(identifier, QLatin1String("Error"), true);
callback(QVariantMap{{QLatin1String("Error"), true}});
qWarning() << identifier << "comic plugin does not seem to be installed.";
return false;
}
......@@ -119,10 +117,12 @@ bool ComicEngine::updateSourceEvent(const QString &identifier)
// check if there is a connection
if (!m_networkConfigurationManager.isOnline()) {
mIdentifierError = identifier;
setData(identifier, QLatin1String("Error"), true);
setData(identifier, QLatin1String("Error automatically fixable"), true);
setData(identifier, QLatin1String("Identifier"), identifier);
setData(identifier, QLatin1String("Previous identifier suffix"), lastCachedIdentifier(identifier));
callback(QVariantMap{
{QLatin1String("Error"), true},
{QLatin1String("Error automatically fixable"), true},
{QLatin1String("Identifier"), identifier},
{QLatin1String("Previous identifier suffix"), lastCachedIdentifier(identifier)},
});
qCDebug(PLASMA_COMIC) << "No internet connection, using cached data";
return true;
}
......@@ -153,33 +153,29 @@ bool ComicEngine::updateSourceEvent(const QString &identifier)
// provider = service->createInstance<ComicProvider>(this, args);
provider = new ComicProviderKross(this, args);
if (!provider) {
setData(identifier, QLatin1String("Error"), true);
callback(QVariantMap{{QLatin1String("Error"), true}});
return false;
}
provider->setIsCurrent(isCurrentComic);
m_jobs[identifier] = provider;
connect(provider, &ComicProvider::finished, this, &ComicEngine::finished);
connect(provider, &ComicProvider::error, this, &ComicEngine::error);
connect(provider, &ComicProvider::finished, this, [this, callback, provider]() {
finished(provider, callback);
});
connect(provider, &ComicProvider::error, this, [this, callback, provider]() {
error(provider, callback);
});
return true;
}
}
bool ComicEngine::sourceRequestEvent(const QString &identifier)
{
setData(identifier, DataEngine::Data());
return updateSourceEvent(identifier);
}
void ComicEngine::finished(ComicProvider *provider)
void ComicEngine::finished(ComicProvider *provider, ComicRequestCallback callback)
{
// sets the data
setComicData(provider);
setComicData(provider, callback);
if (provider->image().isNull()) {
qCWarning(PLASMA_COMIC) << "Provider returned null image" << provider->name();
error(provider);
error(provider, callback);
return;
}
......@@ -236,10 +232,10 @@ void ComicEngine::finished(ComicProvider *provider)
}
}
void ComicEngine::error(ComicProvider *provider)
void ComicEngine::error(ComicProvider *provider, ComicRequestCallback callback)
{
// sets the data
setComicData(provider);
setComicData(provider, callback);
QString identifier(provider->identifier());
mIdentifierError = identifier;
......@@ -255,16 +251,18 @@ void ComicEngine::error(ComicProvider *provider)
identifier = identifier.left(identifier.indexOf(QLatin1Char(':')) + 1);
}
setData(identifier, QLatin1String("Identifier"), identifier);
setData(identifier, QLatin1String("Error"), true);
QVariantMap data{
{QLatin1String("Identifier"), identifier},
{QLatin1String("Error"), true},
};
// if there was an error loading the last cached comic strip, do not return its id anymore
const QString lastCachedId = lastCachedIdentifier(identifier);
if (lastCachedId != provider->identifier().mid(provider->identifier().indexOf(QLatin1Char(':')) + 1)) {
// sets the previousIdentifier to the identifier of a strip that has been cached before
setData(identifier, QLatin1String("Previous identifier suffix"), lastCachedId);
data.insert(QLatin1String("Previous identifier suffix"), lastCachedId);
}
setData(identifier, QLatin1String("Next identifier suffix"), QString());
data.insert(QLatin1String("Next identifier suffix"), QString());
const QString key = m_jobs.key(provider);
if (!key.isEmpty()) {
......@@ -272,9 +270,10 @@ void ComicEngine::error(ComicProvider *provider)
}
provider->deleteLater();
callback(data);
}
void ComicEngine::setComicData(ComicProvider *provider)
void ComicEngine::setComicData(ComicProvider *provider, ComicRequestCallback callback)
{
QString identifier(provider->identifier());
......@@ -287,22 +286,24 @@ void ComicEngine::setComicData(ComicProvider *provider)
identifier = identifier.left(identifier.indexOf(QLatin1Char(':')) + 1);
}
setData(identifier, QLatin1String("Image"), provider->image());
setData(identifier, QLatin1String("Website Url"), provider->websiteUrl());
setData(identifier, QLatin1String("Image Url"), provider->imageUrl());
setData(identifier, QLatin1String("Shop Url"), provider->shopUrl());
setData(identifier, QLatin1String("Next identifier suffix"), provider->nextIdentifier());
setData(identifier, QLatin1String("Previous identifier suffix"), provider->previousIdentifier());
setData(identifier, QLatin1String("Comic Author"), provider->comicAuthor());
setData(identifier, QLatin1String("Additional text"), provider->additionalText());
setData(identifier, QLatin1String("Strip title"), provider->stripTitle());
setData(identifier, QLatin1String("First strip identifier suffix"), provider->firstStripIdentifier());
setData(identifier, QLatin1String("Identifier"), provider->identifier());
setData(identifier, QLatin1String("Title"), provider->name());
setData(identifier, QLatin1String("SuffixType"), provider->suffixType());
setData(identifier, QLatin1String("isLeftToRight"), provider->isLeftToRight());
setData(identifier, QLatin1String("isTopToBottom"), provider->isTopToBottom());
setData(identifier, QLatin1String("Error"), false);
callback({
{QLatin1String("Image"), provider->image()},
{QLatin1String("Website Url"), provider->websiteUrl()},
{QLatin1String("Image Url"), provider->imageUrl()},
{QLatin1String("Shop Url"), provider->shopUrl()},
{QLatin1String("Next identifier suffix"), provider->nextIdentifier()},
{QLatin1String("Previous identifier suffix"), provider->previousIdentifier()},
{QLatin1String("Comic Author"), provider->comicAuthor()},
{QLatin1String("Additional text"), provider->additionalText()},
{QLatin1String("Strip title"), provider->stripTitle()},
{QLatin1String("First strip identifier suffix"), provider->firstStripIdentifier()},
{QLatin1String("Identifier"), provider->identifier()},
{QLatin1String("Title"), provider->name()},
{QLatin1String("SuffixType"), provider->suffixType()},
{QLatin1String("isLeftToRight"), provider->isLeftToRight()},
{QLatin1String("isTopToBottom"), provider->isTopToBottom()},
{QLatin1String("Error"), false},
});
}
QString ComicEngine::lastCachedIdentifier(const QString &identifier) const
......
......@@ -4,13 +4,14 @@
* SPDX-License-Identifier: LGPL-2.0-only
*/
#ifndef COMIC_DATAENGINE_H
#define COMIC_DATAENGINE_H
#ifndef COMIC_ENGINE_H
#define COMIC_ENGINE_H
#include <Plasma/DataEngine>
// Qt
#include <QIcon>
#include <QNetworkConfigurationManager>
#include <QSet>
#include <QVariant>
class ComicProvider;
......@@ -33,7 +34,7 @@ struct ComicProviderInfo {
* if the suffix is empty the latest comic will be returned
*
*/
class ComicEngine : public Plasma::DataEngine
class ComicEngine : public QObject
{
Q_OBJECT
......@@ -43,21 +44,24 @@ public:
QList<ComicProviderInfo> loadProviders();
void setMaxComicLimit(int maxComicLimit);
using ComicRequestCallback = const std::function<void(const QVariantMap &data)> &;
bool requestSource(const QString &identifier, ComicRequestCallback callback);
Q_SIGNALS:
void sourceUpdated(const QString &identifier);
protected:
void init();
bool sourceRequestEvent(const QString &identifier) override;
protected Q_SLOTS:
bool updateSourceEvent(const QString &identifier) override;
private Q_SLOTS:
void finished(ComicProvider *);
void error(ComicProvider *);
private:
void finished(ComicProvider *, ComicRequestCallback callback);