Commit 48a2a2af authored by Alexey Andreyev's avatar Alexey Andreyev 🕊
Browse files

Add Simon Stalenhag POTD Provider

parent 1adda780
Pipeline #114076 passed with stage
in 1 minute and 26 seconds
......@@ -90,3 +90,6 @@ target_link_libraries( plasma_potd_bingprovider plasmapotdprovidercore KF5::KIOC
kcoreaddons_add_plugin(plasma_potd_unsplashprovider SOURCES unsplashprovider.cpp INSTALL_NAMESPACE "potd")
target_link_libraries( plasma_potd_unsplashprovider plasmapotdprovidercore KF5::KIOCore )
kcoreaddons_add_plugin(plasma_potd_simonstalenhagprovider SOURCES simonstalenhagprovider.cpp INSTALL_NAMESPACE "potd")
target_link_libraries( plasma_potd_simonstalenhagprovider plasmapotdprovidercore KF5::KIOCore )
/*
* SPDX-FileCopyrightText: 2021 Alexey Andreyev <aa13q@ya.ru>
*
* SPDX-License-Identifier: LicenseRef-KDE-Accepted-GPL
*/
#include "simonstalenhagprovider.h"
#include <QDebug>
#include <QJsonArray>
#include <QJsonDocument>
#include <QRandomGenerator>
#include <KIO/Job>
#include <KPluginFactory>
static QJsonValue randomArrayValueByKey(const QJsonObject &object, QLatin1String key)
{
QJsonValue result;
if (object.isEmpty()) {
return result;
}
auto array = object.value(key).toArray();
if (array.isEmpty()) {
return result;
}
auto arraySize = array.size();
return array.at(QRandomGenerator::global()->bounded(arraySize));
}
SimonStalenhagProvider::SimonStalenhagProvider(QObject *parent, const QVariantList &args)
: PotdProvider(parent, args)
{
const QUrl url(QStringLiteral("https://raw.githubusercontent.com/a-andreyev/simonstalenhag-se-metadata/main/entrypoint.json"));
KIO::StoredTransferJob *job = KIO::storedGet(url, KIO::NoReload, KIO::HideProgressInfo);
connect(job, &KIO::StoredTransferJob::finished, this, &SimonStalenhagProvider::entrypointRequestFinished);
}
QImage SimonStalenhagProvider::image() const
{
return mImage;
}
void SimonStalenhagProvider::entrypointRequestFinished(KJob *_job)
{
KIO::StoredTransferJob *job = static_cast<KIO::StoredTransferJob *>(_job);
if (job->error()) {
Q_EMIT error(this);
return;
}
auto json = QJsonDocument::fromJson(job->data());
auto metadataString = randomArrayValueByKey(json.object(), QLatin1String("simonstalenhag-se-entrypoint"));
auto urlStr = metadataString.toString();
if (urlStr.isEmpty()) {
Q_EMIT error(this);
return;
}
QUrl metaDataUrl(urlStr);
KIO::StoredTransferJob *metaDataJob = KIO::storedGet(metaDataUrl, KIO::NoReload, KIO::HideProgressInfo);
connect(metaDataJob, &KIO::StoredTransferJob::finished, this, &SimonStalenhagProvider::metaDataRequestFinished);
}
void SimonStalenhagProvider::metaDataRequestFinished(KJob *_job)
{
KIO::StoredTransferJob *job = static_cast<KIO::StoredTransferJob *>(_job);
if (job->error()) {
Q_EMIT error(this);
return;
}
auto json = QJsonDocument::fromJson(job->data());
auto imageObj = randomArrayValueByKey(json.object(), QLatin1String("simonstalenhag.se"));
auto urlStr = imageObj.toObject().value(QLatin1String("imagebig")).toString();
if (urlStr.isEmpty()) {
Q_EMIT error(this);
return;
}
QUrl picUrl(urlStr);
KIO::StoredTransferJob *imageJob = KIO::storedGet(picUrl, KIO::NoReload, KIO::HideProgressInfo);
connect(imageJob, &KIO::StoredTransferJob::finished, this, &SimonStalenhagProvider::imageRequestFinished);
}
void SimonStalenhagProvider::imageRequestFinished(KJob *_job)
{
KIO::StoredTransferJob *job = static_cast<KIO::StoredTransferJob *>(_job);
if (job->error()) {
Q_EMIT error(this);
return;
}
QByteArray data = job->data();
mImage = QImage::fromData(data);
Q_EMIT finished(this);
}
K_PLUGIN_CLASS_WITH_JSON(SimonStalenhagProvider, "simonstalenhagprovider.json")
#include "simonstalenhagprovider.moc"
/*
* SPDX-FileCopyrightText: 2021 Alexey Andreyev <aa13q@ya.ru>
*
* SPDX-License-Identifier: LicenseRef-KDE-Accepted-GPL
*/
#ifndef UNSPLASHPROVIDER_H
#define UNSPLASHPROVIDER_H
#include "potdprovider.h"
// Qt
#include <QImage>
class KJob;
/**
* This class provides random wallpapers from Simon Stalenhag website
* http://simonstalenhag.se/
* see also: https://github.com/a-andreyev/simonstalenhag-se-metadata
*/
class SimonStalenhagProvider : public PotdProvider
{
Q_OBJECT
public:
/**
* Creates a new SimonStalenhag provider.
*
* @param parent The parent object.
* @param args The arguments.
*/
SimonStalenhagProvider(QObject *parent, const QVariantList &args);
/**
* Returns the requested image.
*
* Note: This method returns only a valid image after the
* finished() signal has been emitted.
*/
QImage image() const override;
private:
void entrypointRequestFinished(KJob *job);
void metaDataRequestFinished(KJob *job);
void imageRequestFinished(KJob *job);
private:
QImage mImage;
};
#endif
{
"KPlugin": {
"Icon": "",
"Name": "Simon Stålenhag Wallpapers"
},
"X-KDE-PlasmaPoTDProvider-Identifier": "simonstalenhag"
}
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