File indexing completed on 2024-12-29 05:00:27
0001 /* 0002 * SPDX-FileCopyrightText: 2021 Alexey Andreyev <aa13q@ya.ru> 0003 * 0004 * SPDX-License-Identifier: LicenseRef-KDE-Accepted-GPL 0005 */ 0006 0007 #include "simonstalenhagprovider.h" 0008 0009 #include <random> 0010 0011 #include <QJsonArray> 0012 #include <QJsonDocument> 0013 0014 #include <KIO/StoredTransferJob> 0015 #include <KPluginFactory> 0016 0017 static QJsonValue randomArrayValueByKey(const QJsonObject &object, QLatin1String key) 0018 { 0019 QJsonValue result; 0020 0021 if (object.isEmpty()) { 0022 return result; 0023 } 0024 0025 auto array = object.value(key).toArray(); 0026 0027 if (array.isEmpty()) { 0028 return result; 0029 } 0030 0031 // Plasma 5.24.0 release date 0032 std::mt19937 randomEngine(QDate(2022, 2, 3).daysTo(QDate::currentDate())); 0033 std::uniform_int_distribution<int> distrib(0, array.size() - 1); 0034 0035 return array.at(distrib(randomEngine)); 0036 } 0037 0038 SimonStalenhagProvider::SimonStalenhagProvider(QObject *parent, const KPluginMetaData &data, const QVariantList &args) 0039 : PotdProvider(parent, data, args) 0040 { 0041 const QUrl url(QStringLiteral("https://raw.githubusercontent.com/a-andreyev/simonstalenhag-se-metadata/main/entrypoint.json")); 0042 0043 KIO::StoredTransferJob *job = KIO::storedGet(url, KIO::NoReload, KIO::HideProgressInfo); 0044 connect(job, &KIO::StoredTransferJob::finished, this, &SimonStalenhagProvider::entrypointRequestFinished); 0045 } 0046 0047 void SimonStalenhagProvider::entrypointRequestFinished(KJob *_job) 0048 { 0049 KIO::StoredTransferJob *job = static_cast<KIO::StoredTransferJob *>(_job); 0050 if (job->error()) { 0051 Q_EMIT error(this); 0052 return; 0053 } 0054 0055 auto json = QJsonDocument::fromJson(job->data()); 0056 auto metadataString = randomArrayValueByKey(json.object(), QLatin1String("simonstalenhag-se-entrypoint")); 0057 auto urlStr = metadataString.toString(); 0058 if (urlStr.isEmpty()) { 0059 Q_EMIT error(this); 0060 return; 0061 } 0062 QUrl metaDataUrl(urlStr); 0063 KIO::StoredTransferJob *metaDataJob = KIO::storedGet(metaDataUrl, KIO::NoReload, KIO::HideProgressInfo); 0064 connect(metaDataJob, &KIO::StoredTransferJob::finished, this, &SimonStalenhagProvider::metaDataRequestFinished); 0065 } 0066 0067 void SimonStalenhagProvider::metaDataRequestFinished(KJob *_job) 0068 { 0069 KIO::StoredTransferJob *job = static_cast<KIO::StoredTransferJob *>(_job); 0070 if (job->error()) { 0071 Q_EMIT error(this); 0072 return; 0073 } 0074 0075 auto json = QJsonDocument::fromJson(job->data()); 0076 auto imageObj = randomArrayValueByKey(json.object(), QLatin1String("simonstalenhag.se")); 0077 auto urlStr = imageObj.toObject().value(QLatin1String("imagebig")).toString(); 0078 if (urlStr.isEmpty()) { 0079 Q_EMIT error(this); 0080 return; 0081 } 0082 m_remoteUrl = QUrl(urlStr); 0083 0084 const QString titleStr = imageObj.toObject().value(QStringLiteral("name")).toString(); 0085 const QString sectionStr = imageObj.toObject().value(QStringLiteral("section")).toString(); 0086 if (!titleStr.isEmpty()) { 0087 if (!sectionStr.isEmpty()) { 0088 m_title = sectionStr + " - " + titleStr; 0089 } else { 0090 m_title = titleStr; 0091 } 0092 } 0093 0094 KIO::StoredTransferJob *imageJob = KIO::storedGet(m_remoteUrl, KIO::NoReload, KIO::HideProgressInfo); 0095 connect(imageJob, &KIO::StoredTransferJob::finished, this, &SimonStalenhagProvider::imageRequestFinished); 0096 } 0097 0098 void SimonStalenhagProvider::imageRequestFinished(KJob *_job) 0099 { 0100 KIO::StoredTransferJob *job = static_cast<KIO::StoredTransferJob *>(_job); 0101 if (job->error()) { 0102 Q_EMIT error(this); 0103 return; 0104 } 0105 QByteArray data = job->data(); 0106 Q_EMIT finished(this, QImage::fromData(data)); 0107 } 0108 0109 K_PLUGIN_CLASS_WITH_JSON(SimonStalenhagProvider, "simonstalenhagprovider.json") 0110 0111 #include "simonstalenhagprovider.moc"