Commit 5882229e authored by Alexander Lohnau's avatar Alexander Lohnau 💬
Browse files

applets/comic: Use struct rather than using QVariantMap

parent 1026957a
......@@ -5,7 +5,6 @@
*/
#include "checknewstrips.h"
#include "engine/comic.h"
#include <QTimer>
......@@ -25,12 +24,12 @@ CheckNewStrips::CheckNewStrips(const QStringList &identifiers, ComicEngine *engi
start();
}
void CheckNewStrips::dataUpdated(const QString &source, const QVariantMap &data)
void CheckNewStrips::dataUpdated(const QString &source, const ComicMetaData &data)
{
QString lastIdentifierSuffix;
if (!data[QStringLiteral("Error")].toBool()) {
lastIdentifierSuffix = data[QStringLiteral("Identifier")].toString();
if (!data.error) {
lastIdentifierSuffix = data.identifier;
lastIdentifierSuffix.remove(source);
}
......@@ -43,7 +42,7 @@ void CheckNewStrips::dataUpdated(const QString &source, const QVariantMap &data)
if (mIndex < mIdentifiers.count()) {
const QString newSource = mIdentifiers[mIndex] + QLatin1Char(':');
mEngine->requestSource(newSource, [this, newSource](const QVariantMap &data) {
mEngine->requestSource(newSource, [this, newSource](const auto &data) {
dataUpdated(newSource, data);
});
} else {
......@@ -60,7 +59,7 @@ void CheckNewStrips::start()
if (mIndex < mIdentifiers.count()) {
const QString newSource = mIdentifiers[mIndex] + QLatin1Char(':');
mEngine->requestSource(newSource, [this, newSource](const QVariantMap &data) {
mEngine->requestSource(newSource, [this, newSource](const auto &data) {
dataUpdated(newSource, data);
});
}
......
......@@ -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 QVariantMap &data);
void dataUpdated(const QString &name, const ComicMetaData &data);
private Q_SLOTS:
void start();
......
......@@ -35,7 +35,6 @@
#include "comicmodel.h"
#include "comicupdater.h"
#include "engine/comic.h"
Q_GLOBAL_STATIC(ComicUpdater, globalComicUpdater)
......@@ -141,7 +140,7 @@ ComicApplet::~ComicApplet()
delete mSavingDir;
}
void ComicApplet::dataUpdated(const QString &source, const QVariantMap &data)
void ComicApplet::dataUpdated(const QString &source, const ComicMetaData &data)
{
setBusy(false);
......@@ -153,12 +152,9 @@ void ComicApplet::dataUpdated(const QString &source, const QVariantMap &data)
setConfigurationRequired(false);
// there was an error, display information as image
const bool hasError = data[QStringLiteral("Error")].toBool();
const bool errorAutoFixable = data[QStringLiteral("Error automatically fixable")].toBool();
if (hasError) {
const QString previousIdentifierSuffix = data[QStringLiteral("Previous identifier suffix")].toString();
if (mEngine && !mShowErrorPicture && !previousIdentifierSuffix.isEmpty()) {
updateComic(previousIdentifierSuffix);
if (data.error) {
if (mEngine && !mShowErrorPicture && !data.previousIdentifier.isEmpty()) {
updateComic(data.previousIdentifier);
}
return;
}
......@@ -181,13 +177,13 @@ void ComicApplet::dataUpdated(const QString &source, const QVariantMap &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 QVariantMap &data) {
mEngine->requestSource(prefetch, [this, prefetch](const auto &data) {
dataUpdated(prefetch, data);
});
}
if (mCurrent.hasPrev()) {
const QString prefetch = mCurrent.id() + QLatin1Char(':') + mCurrent.prev();
mEngine->requestSource(prefetch, [this, prefetch](const QVariantMap &data) {
mEngine->requestSource(prefetch, [this, prefetch](const auto &data) {
dataUpdated(prefetch, data);
});
}
......@@ -474,7 +470,7 @@ void ComicApplet::updateComic(const QString &identifierSuffix)
mIdentifierError.clear();
}
mOldSource = identifier;
mEngine->requestSource(identifier, [this, identifier](const QVariantMap &data) {
mEngine->requestSource(identifier, [this, identifier](const auto &data) {
dataUpdated(identifier, data);
});
slotScaleToContent();
......
......@@ -11,14 +11,14 @@
#ifndef COMIC_H
#define COMIC_H
#include "comicdata.h"
#include <QDate>
#include <QUrl>
#include <Plasma/Applet>
#include "activecomicmodel.h"
#include "comicdata.h"
#include "engine/comic.h"
class CheckNewStrips;
class ComicModel;
......@@ -29,7 +29,6 @@ class QAction;
class QSortFilterProxyModel;
class QTimer;
class SavingDir;
class ComicEngine;
class ComicApplet : public Plasma::Applet
{
......@@ -118,7 +117,7 @@ Q_SIGNALS:
void maxComicLimitChanged();
public Q_SLOTS:
void dataUpdated(const QString &name, const QVariantMap &data);
void dataUpdated(const QString &name, const ComicMetaData &data);
private Q_SLOTS:
void slotTabChanged(const QString &newIdentifier);
......
......@@ -121,7 +121,7 @@ void ComicArchiveJob::start()
}
}
void ComicArchiveJob::dataUpdated(const QString &source, const QVariantMap &data)
void ComicArchiveJob::dataUpdated(const QString &source, const ComicMetaData &data)
{
if (!mZip) {
qWarning() << "No zip file, aborting.";
......@@ -131,21 +131,21 @@ void ComicArchiveJob::dataUpdated(const QString &source, const QVariantMap &data
return;
}
const QString currentIdentifier = data[QStringLiteral("Identifier")].toString();
const QString currentIdentifier = data.identifier;
QString currentIdentifierSuffix = currentIdentifier;
currentIdentifierSuffix.remove(mPluginName + QLatin1Char(':'));
const QImage image = data[QStringLiteral("Image")].value<QImage>();
const bool hasError = data[QStringLiteral("Error")].toBool() || image.isNull();
const QString previousIdentifierSuffix = data[QStringLiteral("Previous identifier suffix")].toString();
const QString nextIdentifierSuffix = data[QStringLiteral("Next identifier suffix")].toString();
const QString firstIdentifierSuffix = data[QStringLiteral("First strip identifier suffix")].toString();
const QImage image = data.image;
const bool hasError = data.error || image.isNull();
const QString previousIdentifierSuffix = data.previousIdentifier;
const QString nextIdentifierSuffix = data.nextIdentifier;
const QString firstIdentifierSuffix = data.firstStripIdentifier;
mAuthors << data[QStringLiteral("Comic Author")].toString().split(QLatin1Char(','), Qt::SkipEmptyParts);
mAuthors << data.comicAuthor.split(QLatin1Char(','), Qt::SkipEmptyParts);
mAuthors.removeDuplicates();
if (mComicTitle.isEmpty()) {
mComicTitle = data[QStringLiteral("Title")].toString();
mComicTitle = data.stripTitle;
}
if (hasError) {
......@@ -330,7 +330,7 @@ void ComicArchiveJob::requestComic(QString identifier) // krazy:exclude=passbyva
qMakePair(QStringLiteral("source"), identifier),
qMakePair(QStringLiteral("destination"), mDest.toString()));
mEngine->requestSource(identifier, [this, identifier](const QVariantMap &data) {
mEngine->requestSource(identifier, [this, identifier](const auto &data) {
dataUpdated(identifier, data);
});
}
......
......@@ -66,7 +66,7 @@ public:
void start() override;
public Q_SLOTS:
void dataUpdated(const QString &source, const QVariantMap &data);
void dataUpdated(const QString &source, const ComicMetaData &data);
protected:
bool doKill() override;
......
......@@ -54,25 +54,24 @@ void ComicData::storePosition(bool store)
save();
}
void ComicData::setData(const QVariantMap &data)
void ComicData::setData(const ComicMetaData &data)
{
const bool hasError = data[QStringLiteral("Error")].toBool();
if (!hasError) {
mImage = data[QStringLiteral("Image")].value<QImage>();
mPrev = data[QStringLiteral("Previous identifier suffix")].toString();
mNext = data[QStringLiteral("Next identifier suffix")].toString();
mAdditionalText = data[QStringLiteral("Additional text")].toString();
if (!data.error) {
mImage = data.image;
mPrev = data.previousIdentifier;
mNext = data.nextIdentifier;
mAdditionalText = data.additionalText;
}
mWebsiteUrl = data[QStringLiteral("Website Url")].toUrl();
mImageUrl = data[QStringLiteral("Image Url")].toUrl();
mShopUrl = data[QStringLiteral("Shop Url")].toUrl();
mFirst = data[QStringLiteral("First strip identifier suffix")].toString();
mStripTitle = data[QStringLiteral("Strip title")].toString();
mAuthor = data[QStringLiteral("Comic Author")].toString();
mTitle = data[QStringLiteral("Title")].toString();
mWebsiteUrl = data.websiteUrl;
mImageUrl = data.imageUrl;
mShopUrl = data.shopUrl;
mFirst = data.firstStripIdentifier;
mStripTitle = data.stripTitle;
mAuthor = data.comicAuthor;
mTitle = data.providerName;
const QString suffixType = data[QStringLiteral("SuffixType")].toString();
const QString suffixType = data.suffixType;
if (suffixType == QLatin1String("Date")) {
mType = Date;
} else if (suffixType == QLatin1String("Number")) {
......@@ -81,7 +80,7 @@ void ComicData::setData(const QVariantMap &data)
mType = String;
}
QString temp = data[QStringLiteral("Identifier")].toString();
QString temp = data.identifier;
mCurrent = temp.remove(mId + QLatin1Char(':'));
// found a new last identifier
......@@ -105,8 +104,8 @@ void ComicData::setData(const QVariantMap &data)
mCurrentReadable = mCurrent;
}
mIsLeftToRight = data[QStringLiteral("isLeftToRight")].toBool();
mIsTopToBottom = data[QStringLiteral("isTopToBottom")].toBool();
mIsLeftToRight = data.isLeftToRight;
mIsTopToBottom = data.isTopToBottom;
save();
}
......
......@@ -8,6 +8,7 @@
#define COMIC_DATA_H
#include "comicinfo.h"
#include "engine/types.h"
// Qt
#include <KConfigGroup>
......@@ -22,7 +23,7 @@ public:
void init(const QString &id, const KConfigGroup &config);
void setData(const QVariantMap &data);
void setData(const ComicMetaData &data);
IdentifierType type() const
{
......
......@@ -7,7 +7,6 @@
*/
#include "comicmodel.h"
#include "engine/comic.h"
#include "engine/comicprovider.h"
#include <QDebug>
......
......@@ -12,6 +12,7 @@
#include <QAbstractTableModel>
#include "engine/comic.h"
class ComicModel : public QAbstractTableModel
{
Q_OBJECT
......
......@@ -6,6 +6,7 @@
#include "cachedprovider.h"
#include "comic_debug.h"
#include "types.h"
#include <QDebug>
#include <QDir>
......@@ -121,7 +122,7 @@ bool CachedProvider::isCached(const QString &identifier)
return QFile::exists(identifierToPath(identifier));
}
bool CachedProvider::storeInCache(const QString &identifier, const QImage &comic, const Settings &info)
bool CachedProvider::storeInCache(const QString &identifier, const QImage &comic, const ComicMetaData &data)
{
const QString path = identifierToPath(identifier);
......@@ -130,19 +131,22 @@ bool CachedProvider::storeInCache(const QString &identifier, const QImage &comic
const QString pathMain = identifierToPath(comicName);
const QString dirPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1String("/plasma_engine_comic/");
if (!info.isEmpty()) {
QSettings settings(path + QLatin1String(".conf"), QSettings::IniFormat);
QSettings settingsMain(pathMain + QLatin1String(".conf"), QSettings::IniFormat);
settingsMain.setValue(QStringLiteral("firstStripIdentifier"), data.firstStripIdentifier);
settingsMain.setValue(QStringLiteral("title"), data.providerName);
settingsMain.setValue(QStringLiteral("lastCachedStripIdentifier"), data.lastCachedStripIdentifier);
settingsMain.setValue(QStringLiteral("suffixType"), data.suffixType);
settingsMain.setValue(QStringLiteral("shopUrl"), data.shopUrl);
settingsMain.setValue(QStringLiteral("isLeftToRight"), data.isLeftToRight);
settingsMain.setValue(QStringLiteral("isTopToBottom"), data.isTopToBottom);
for (Settings::const_iterator i = info.constBegin(); i != info.constEnd(); ++i) {
if ((i.key() == QLatin1String("firstStripIdentifier")) || (i.key() == QLatin1String("title"))
|| (i.key() == QLatin1String("lastCachedStripIdentifier")) || (i.key() == QLatin1String("suffixType")) || (i.key() == QLatin1String("shopUrl"))
|| (i.key() == QLatin1String("isLeftToRight")) || (i.key() == QLatin1String("isTopToBottom"))) {
settingsMain.setValue(i.key(), i.value());
} else {
settings.setValue(i.key(), i.value());
}
}
QSettings settings(path + QLatin1String(".conf"), QSettings::IniFormat);
settings.setValue(QStringLiteral("additionalText"), data.additionalText);
settings.setValue(QStringLiteral("comicAuthor"), data.comicAuthor);
settings.setValue(QStringLiteral("imageUrl"), data.imageUrl);
settings.setValue(QStringLiteral("nextIdentifier"), data.nextIdentifier);
settings.setValue(QStringLiteral("previousIdentifier"), data.previousIdentifier);
settings.setValue(QStringLiteral("websiteUrl"), data.websiteUrl);
QStringList comics;
if (settingsMain.contains(QLatin1String("comics"))) {
......@@ -180,7 +184,6 @@ bool CachedProvider::storeInCache(const QString &identifier, const QImage &comic
}
}
settingsMain.setValue(QLatin1String("comics"), comics);
}
return comic.save(path, "PNG");
}
......
......@@ -8,6 +8,7 @@
#define CACHEDPROVIDER_H
#include "comicprovider.h"
#include "types.h"
#include <QHash>
......@@ -121,7 +122,7 @@ public:
/**
* Stores the given @p comic with the given @p identifier in the cache.
*/
static bool storeInCache(const QString &identifier, const QImage &comic, const Settings &info = Settings());
static bool storeInCache(const QString &identifier, const QImage &comic, const ComicMetaData &info);
/**
* Returns the website of the comic.
......
......@@ -35,7 +35,10 @@ ComicEngine::~ComicEngine()
void ComicEngine::init()
{
QT_WARNING_PUSH
QT_WARNING_DISABLE_DEPRECATED
connect(&m_networkConfigurationManager, &QNetworkConfigurationManager::onlineStateChanged, this, &ComicEngine::onOnlineStateChanged);
QT_WARNING_POP
}
void ComicEngine::onOnlineStateChanged(bool isOnline)
......@@ -100,7 +103,7 @@ bool ComicEngine::requestSource(const QString &identifier, ComicRequestCallback
// ... start a new query otherwise
if (parts.count() < 2) {
callback(QVariantMap{{QLatin1String("Error"), true}});
callback(ComicMetaData{.error = true});
qWarning() << "Less than two arguments specified.";
return false;
}
......@@ -108,7 +111,7 @@ bool ComicEngine::requestSource(const QString &identifier, ComicRequestCallback
// User might have installed more from GHNS
loadProviders();
if (!mProviders.contains(parts[0])) {
callback(QVariantMap{{QLatin1String("Error"), true}});
callback(ComicMetaData{.error = true});
qWarning() << identifier << "comic plugin does not seem to be installed.";
return false;
}
......@@ -117,12 +120,12 @@ bool ComicEngine::requestSource(const QString &identifier, ComicRequestCallback
// check if there is a connection
if (!m_networkConfigurationManager.isOnline()) {
mIdentifierError = identifier;
callback(QVariantMap{
{QLatin1String("Error"), true},
{QLatin1String("Error automatically fixable"), true},
{QLatin1String("Identifier"), identifier},
{QLatin1String("Previous identifier suffix"), lastCachedIdentifier(identifier)},
});
ComicMetaData data;
data.error = true;
data.errorAutomaticallyFixable = true;
data.identifier = identifier;
data.previousIdentifier = lastCachedIdentifier(identifier);
callback(data);
qCDebug(PLASMA_COMIC) << "No internet connection, using cached data";
return true;
}
......@@ -150,10 +153,9 @@ bool ComicEngine::requestSource(const QString &identifier, ComicRequestCallback
}
args << QStandardPaths::locate(QStandardPaths::GenericDataLocation, QLatin1String("plasma/comics/") + parts[0] + QLatin1String("/metadata.desktop"));
// provider = service->createInstance<ComicProvider>(this, args);
provider = new ComicProviderKross(this, args);
if (!provider) {
callback(QVariantMap{{QLatin1String("Error"), true}});
callback(ComicMetaData{.error = true});
return false;
}
provider->setIsCurrent(isCurrentComic);
......@@ -193,33 +195,31 @@ void ComicEngine::finished(ComicProvider *provider, ComicRequestCallback callbac
// if there is a valid image and if there is a next comic
// (if we're on today's comic it could become stale)
if (!provider->inherits("CachedProvider") && !provider->image().isNull() && !provider->nextIdentifier().isEmpty()) {
CachedProvider::Settings info;
info[QLatin1String("websiteUrl")] = provider->websiteUrl().toString(QUrl::PrettyDecoded);
info[QLatin1String("imageUrl")] = provider->imageUrl().url();
info[QLatin1String("shopUrl")] = provider->shopUrl().toString(QUrl::PrettyDecoded);
info[QLatin1String("nextIdentifier")] = provider->nextIdentifier();
info[QLatin1String("previousIdentifier")] = provider->previousIdentifier();
info[QLatin1String("title")] = provider->name();
info[QLatin1String("suffixType")] = provider->suffixType();
info[QLatin1String("lastCachedStripIdentifier")] = provider->identifier().mid(provider->identifier().indexOf(QLatin1Char(':')) + 1);
QString isLeftToRight;
QString isTopToBottom;
info[QLatin1String("isLeftToRight")] = isLeftToRight.setNum(provider->isLeftToRight());
info[QLatin1String("isTopToBottom")] = isTopToBottom.setNum(provider->isTopToBottom());
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.suffixType = provider->suffixType();
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[QLatin1String("comicAuthor")] = provider->comicAuthor();
info.comicAuthor = provider->comicAuthor();
}
if (!provider->firstStripIdentifier().isEmpty()) {
info[QLatin1String("firstStripIdentifier")] = provider->firstStripIdentifier();
info.firstStripIdentifier = provider->firstStripIdentifier();
}
if (!provider->additionalText().isEmpty()) {
info[QLatin1String("additionalText")] = provider->additionalText();
info.additionalText = provider->additionalText();
}
if (!provider->stripTitle().isEmpty()) {
info[QLatin1String("stripTitle")] = provider->stripTitle();
info.stripTitle = provider->stripTitle();
}
CachedProvider::storeInCache(provider->identifier(), provider->image(), info);
......@@ -251,18 +251,17 @@ void ComicEngine::error(ComicProvider *provider, ComicRequestCallback callback)
identifier = identifier.left(identifier.indexOf(QLatin1Char(':')) + 1);
}
QVariantMap data{
{QLatin1String("Identifier"), identifier},
{QLatin1String("Error"), true},
};
ComicMetaData data;
data.identifier = identifier;
data.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
data.insert(QLatin1String("Previous identifier suffix"), lastCachedId);
data.previousIdentifier = lastCachedId;
}
data.insert(QLatin1String("Next identifier suffix"), QString());
data.nextIdentifier = QString();
const QString key = m_jobs.key(provider);
if (!key.isEmpty()) {
......@@ -286,24 +285,24 @@ void ComicEngine::setComicData(ComicProvider *provider, ComicRequestCallback cal
identifier = identifier.left(identifier.indexOf(QLatin1Char(':')) + 1);
}
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},
});
ComicMetaData data;
data.imageUrl = provider->imageUrl();
data.image = provider->image();
data.websiteUrl = provider->websiteUrl();
data.shopUrl = provider->shopUrl();
data.nextIdentifier = provider->nextIdentifier();
data.previousIdentifier = provider->previousIdentifier();
data.comicAuthor = provider->comicAuthor();
data.additionalText = provider->additionalText();
data.stripTitle = provider->stripTitle();
data.firstStripIdentifier = provider->firstStripIdentifier();
data.identifier = provider->identifier();
data.providerName = provider->name();
data.suffixType = provider->suffixType();
data.isLeftToRight = provider->isLeftToRight();
data.isTopToBottom = provider->isTopToBottom();
callback(data);
}
QString ComicEngine::lastCachedIdentifier(const QString &identifier) const
......
......@@ -11,15 +11,12 @@
#include <QIcon>
#include <QNetworkConfigurationManager>
#include <QSet>
#include <QUrl>
#include <QVariant>
class ComicProvider;
#include "types.h"
struct ComicProviderInfo {
QString pluginId;
QString name;
QString icon;
};
class ComicProvider;
/**
* This class provides the comic strip.
......@@ -45,7 +42,7 @@ public:
QList<ComicProviderInfo> loadProviders();
void setMaxComicLimit(int maxComicLimit);
using ComicRequestCallback = const std::function<void(const QVariantMap &data)> &;
using ComicRequestCallback = const std::function<void(const ComicMetaData &data)> &;
bool requestSource(const QString &identifier, ComicRequestCallback callback);
Q_SIGNALS:
......@@ -65,7 +62,10 @@ private:
QString lastCachedIdentifier(const QString &identifier) const;
QString mIdentifierError;
QHash<QString, ComicProvider *> m_jobs;
QT_WARNING_PUSH
QT_WARNING_DISABLE_DEPRECATED
QNetworkConfigurationManager m_networkConfigurationManager;
QT_WARNING_POP
QSet<QString> mProviders;
};
......
#ifndef COMIC_ENGINE_TYPES
#define COMIC_ENGINE_TYPES