From 4ba0d2f9f2353d3dcf02e92a1e7c79fff78f533a Mon Sep 17 00:00:00 2001 From: Ivailo Monev Date: Sat, 16 Sep 2023 14:06:23 +0300 Subject: [PATCH] kdeplasma-addons: new pexels POTD provider requires the following commit from kdelibs: 552d89425b81d7999c08e867a98c95a673f90b4d Signed-off-by: Ivailo Monev --- .../dataengines/potd/CMakeLists.txt | 21 +++ .../dataengines/potd/pexelsprovider.cpp | 143 ++++++++++++++++++ .../dataengines/potd/pexelsprovider.desktop | 9 ++ .../dataengines/potd/pexelsprovider.h | 42 +++++ 4 files changed, 215 insertions(+) create mode 100644 kdeplasma-addons/dataengines/potd/pexelsprovider.cpp create mode 100644 kdeplasma-addons/dataengines/potd/pexelsprovider.desktop create mode 100644 kdeplasma-addons/dataengines/potd/pexelsprovider.h diff --git a/kdeplasma-addons/dataengines/potd/CMakeLists.txt b/kdeplasma-addons/dataengines/potd/CMakeLists.txt index d0d70e70..0c042b3a 100644 --- a/kdeplasma-addons/dataengines/potd/CMakeLists.txt +++ b/kdeplasma-addons/dataengines/potd/CMakeLists.txt @@ -81,3 +81,24 @@ install( FILES apodprovider.desktop DESTINATION ${KDE4_SERVICES_INSTALL_DIR} ) + + +set(potd_pexels_provider_SRCS + pexelsprovider.cpp +) + +kde4_add_plugin(plasma_potd_pexelsprovider ${potd_pexels_provider_SRCS}) +target_link_libraries(plasma_potd_pexelsprovider + KDE4::kio + KDE4::kdecore + ${QT_QTGUI_LIBRARY} + plasmapotdprovidercore +) +install( + TARGETS plasma_potd_pexelsprovider + DESTINATION ${KDE4_PLUGIN_INSTALL_DIR} +) +install( + FILES pexelsprovider.desktop + DESTINATION ${KDE4_SERVICES_INSTALL_DIR} +) diff --git a/kdeplasma-addons/dataengines/potd/pexelsprovider.cpp b/kdeplasma-addons/dataengines/potd/pexelsprovider.cpp new file mode 100644 index 00000000..9feac79c --- /dev/null +++ b/kdeplasma-addons/dataengines/potd/pexelsprovider.cpp @@ -0,0 +1,143 @@ +/* + * Copyright (C) 2023 Ivailo Monev + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details + * + * You should have received a copy of the GNU Library General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "pexelsprovider.h" + +#include +#include + +#include +#include +#include + +// for reference: +// https://www.pexels.com/api/documentation/#photos-curated + +static const QString s_pexelsapikey = QString::fromLatin1("WeDUAEYr4oV9211fAie7Jcwat6tNlkrW0BTx7kaoiyITGWp7WMcxpSoM"); +static const QString s_pexelsapiurl = QString::fromLatin1("https://api.pexels.com/v1/curated"); + +POTDPROVIDER_EXPORT_PLUGIN(PexelsProvider, "PexelsProvider", "") + +class PexelsProvider::Private +{ +public: + Private(PexelsProvider *parent) + : mParent(parent) + { + } + + void pageRequestFinished(KJob*); + void imageRequestFinished(KJob*); + void parsePage(); + + PexelsProvider *mParent; + QImage mImage; + +private: + QStringList m_photoList; +}; + +void PexelsProvider::Private::pageRequestFinished(KJob *kjob) +{ + KIO::StoredTransferJob* kstoredjob = qobject_cast(kjob); + if (kstoredjob->error()) { + kWarning() << "request error" << kstoredjob->url(); + kstoredjob->deleteLater(); + emit mParent->error(mParent); + return; + } + + m_photoList.clear(); + + const QByteArray jsondata = kstoredjob->data(); + kstoredjob->deleteLater(); + + const QJsonDocument jsondoc = QJsonDocument::fromJson(jsondata); + if (jsondoc.isNull()) { + kWarning() << "JSON error" << jsondoc.errorString(); + return; + } + + const QVariantMap jsonmap = jsondoc.toVariant().toMap(); + const QVariantList jsonphotoslist = jsonmap["photos"].toList(); + foreach (const QVariant &photo, jsonphotoslist) { + const QVariantMap photomap = photo.toMap()["src"].toMap(); + const QString photourl = photomap["landscape"].toString(); + if (photourl.isEmpty()) { + kDebug() << "skipping photo without landscape url"; + continue; + } + kDebug() << "photo" << photourl; + m_photoList.append(photourl); + } + + if (m_photoList.isEmpty()) { + kWarning() << "empty photo list"; + return; + } + + const KUrl photourl(m_photoList.at(KRandom::randomMax(m_photoList.size()))); + kDebug() << "chosen photo" << photourl.prettyUrl(); + kstoredjob = KIO::storedGet(photourl, KIO::NoReload, KIO::HideProgressInfo); + kstoredjob->setAutoDelete(false); + kstoredjob->addMetaData("Authorization", s_pexelsapikey); + mParent->connect(kstoredjob, SIGNAL(finished(KJob*)), SLOT(imageRequestFinished(KJob*))); +} + +void PexelsProvider::Private::imageRequestFinished(KJob *kjob) +{ + KIO::StoredTransferJob* kstoredjob = qobject_cast(kjob); + if (kstoredjob->error()) { + kWarning() << "image job error" << kstoredjob->url(); + kstoredjob->deleteLater(); + emit mParent->error(mParent); + return; + } + + mImage = QImage::fromData(kstoredjob->data()); + if (mImage.isNull()) { + kWarning() << "null image for" << kstoredjob->url(); + } + kstoredjob->deleteLater(); + emit mParent->finished(mParent); +} + +PexelsProvider::PexelsProvider(QObject *parent, const QVariantList &args) + : PotdProvider(parent, args), + d(new Private(this)) +{ + const KUrl queryurl(s_pexelsapiurl); + kDebug() << "starting job for" << queryurl.prettyUrl(); + KIO::StoredTransferJob *kstoredjob = KIO::storedGet(queryurl, KIO::NoReload, KIO::HideProgressInfo); + kstoredjob->setAutoDelete(false); + kstoredjob->addMetaData("Authorization", s_pexelsapikey); + connect(kstoredjob, SIGNAL(finished(KJob*)), SLOT(pageRequestFinished(KJob*))); +} + +PexelsProvider::~PexelsProvider() +{ + delete d; +} + +QImage PexelsProvider::image() const +{ + return d->mImage; +} + +#include "moc_pexelsprovider.cpp" diff --git a/kdeplasma-addons/dataengines/potd/pexelsprovider.desktop b/kdeplasma-addons/dataengines/potd/pexelsprovider.desktop new file mode 100644 index 00000000..dc02437a --- /dev/null +++ b/kdeplasma-addons/dataengines/potd/pexelsprovider.desktop @@ -0,0 +1,9 @@ +[Desktop Entry] +Type=Service +X-KDE-ServiceTypes=PlasmaPoTD/Plugin +X-KDE-Library=plasma_potd_pexelsprovider +X-KDE-PlasmaPoTDProvider-Identifier=pexels +Icon= + +Name=Pexels Picture of the Day +Comment=Pexels Provider diff --git a/kdeplasma-addons/dataengines/potd/pexelsprovider.h b/kdeplasma-addons/dataengines/potd/pexelsprovider.h new file mode 100644 index 00000000..0fdc774d --- /dev/null +++ b/kdeplasma-addons/dataengines/potd/pexelsprovider.h @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2023 Ivailo Monev + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details + * + * You should have received a copy of the GNU Library General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef PEXELSPROVIDER_H +#define PEXELSPROVIDER_H + +#include "potdprovider.h" + +class PexelsProvider : public PotdProvider +{ + Q_OBJECT +public: + PexelsProvider(QObject *parent, const QVariantList &args); + ~PexelsProvider(); + + virtual QImage image() const; + +private: + class Private; + Private* const d; + + Q_PRIVATE_SLOT(d, void pageRequestFinished(KJob *)) + Q_PRIVATE_SLOT(d, void imageRequestFinished(KJob *)) +}; + +#endif