diff --git a/dataengines/potd/CMakeLists.txt b/dataengines/potd/CMakeLists.txt --- a/dataengines/potd/CMakeLists.txt +++ b/dataengines/potd/CMakeLists.txt @@ -135,3 +135,12 @@ target_link_libraries( plasma_potd_bingprovider plasmapotdprovidercore KF5::KIOCore ) install( TARGETS plasma_potd_bingprovider DESTINATION ${KDE_INSTALL_PLUGINDIR}/potd ) + +set( potd_unsplash_provider_SRCS + unsplashprovider.cpp +) + +add_library( plasma_potd_unsplashprovider MODULE ${potd_unsplash_provider_SRCS} ) +target_link_libraries( plasma_potd_unsplashprovider plasmapotdprovidercore KF5::KIOCore ) + +install( TARGETS plasma_potd_unsplashprovider DESTINATION ${KDE_INSTALL_PLUGINDIR}/potd ) diff --git a/dataengines/potd/unsplashprovider.h b/dataengines/potd/unsplashprovider.h new file mode 100644 --- /dev/null +++ b/dataengines/potd/unsplashprovider.h @@ -0,0 +1,67 @@ +/* + * Copyright 2019 Guo Yunhe + * + * 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 UNSPLASHPROVIDER_H +#define UNSPLASHPROVIDER_H + +#include "potdprovider.h" +// Qt +#include + +class KJob; + +/** + * This class provides random wallpapers from Unsplash Wallpapers + * Image urls are parsed from https://unsplash.com/wallpaper/1065396/desktop-wallpapers + */ +class UnsplashProvider : public PotdProvider +{ + Q_OBJECT + + public: + /** + * Creates a new Unsplash provider. + * + * @param parent The parent object. + * @param args The arguments. + */ + UnsplashProvider( QObject *parent, const QVariantList &args ); + + /** + * Destroys the Unsplash provider. + */ + ~UnsplashProvider() override; + + /** + * 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 pageRequestFinished(KJob *job); + void imageRequestFinished(KJob *job); + + private: + QImage mImage; +}; + +#endif diff --git a/dataengines/potd/unsplashprovider.cpp b/dataengines/potd/unsplashprovider.cpp new file mode 100644 --- /dev/null +++ b/dataengines/potd/unsplashprovider.cpp @@ -0,0 +1,83 @@ +/* + * Copyright 2019 Guo Yunhe + * + * 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 "unsplashprovider.h" + +#include +#include + +#include +#include + +UnsplashProvider::UnsplashProvider(QObject* parent, const QVariantList& args) + : PotdProvider(parent, args) +{ + const QUrl url(QStringLiteral("https://unsplash.com/wallpaper/1065396/desktop-wallpapers")); + + KIO::StoredTransferJob* job = KIO::storedGet(url, KIO::NoReload, KIO::HideProgressInfo); + connect(job, &KIO::StoredTransferJob::finished, this, &UnsplashProvider::pageRequestFinished); +} + +UnsplashProvider::~UnsplashProvider() = default; + +QImage UnsplashProvider::image() const +{ + return mImage; +} + +void UnsplashProvider::pageRequestFinished(KJob* _job) +{ + KIO::StoredTransferJob* job = static_cast(_job); + if (job->error()) { + emit error(this); + return; + } + + const QString html = QString::fromUtf8(job->data()); + + QRegularExpression re(QStringLiteral("src=\"(https://images\\.unsplash\\.com/photo-\\w+-\\w+)")); + + QRegularExpressionMatch match = re.match(html); + + if (match.hasMatch()) { + QUrl picUrl(match.captured(1)); // url to full size photo (compressed) + KIO::StoredTransferJob* imageJob = KIO::storedGet(picUrl, KIO::NoReload, KIO::HideProgressInfo); + connect(imageJob, &KIO::StoredTransferJob::finished, this, &UnsplashProvider::imageRequestFinished); + return; + } else { + emit error(this); + return; + } +} + +void UnsplashProvider::imageRequestFinished(KJob* _job) +{ + KIO::StoredTransferJob* job = static_cast(_job); + if (job->error()) { + emit error(this); + return; + } + QByteArray data = job->data(); + mImage = QImage::fromData(data); + emit finished(this); +} + +K_PLUGIN_CLASS_WITH_JSON(UnsplashProvider, "unsplashprovider.json") + +#include "unsplashprovider.moc" diff --git a/dataengines/potd/unsplashprovider.json b/dataengines/potd/unsplashprovider.json new file mode 100644 --- /dev/null +++ b/dataengines/potd/unsplashprovider.json @@ -0,0 +1,11 @@ +{ + "KPlugin": { + "Description": "Unsplash Wallpaper Provider", + "Icon": "", + "Name": "Unsplash Wallpapers", + "ServiceTypes": [ + "PlasmaPoTD/Plugin" + ] + }, + "X-KDE-PlasmaPoTDProvider-Identifier": "unsplash" +}