diff --git a/dataengines/potd/CMakeLists.txt b/dataengines/potd/CMakeLists.txt --- a/dataengines/potd/CMakeLists.txt +++ b/dataengines/potd/CMakeLists.txt @@ -136,11 +136,26 @@ install( TARGETS plasma_potd_bingprovider DESTINATION ${KDE_INSTALL_PLUGINDIR}/potd ) +# Unsplash + set( potd_unsplash_provider_SRCS unsplashprovider.cpp + unsplashbaseprovider.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 ) + +# Unsplash Nature + +set( potd_unsplash_nature_provider_SRCS + unsplashnatureprovider.cpp + unsplashbaseprovider.cpp +) + +add_library( plasma_potd_unsplashnatureprovider MODULE ${potd_unsplash_nature_provider_SRCS} ) +target_link_libraries( plasma_potd_unsplashnatureprovider plasmapotdprovidercore KF5::KIOCore ) + +install( TARGETS plasma_potd_unsplashnatureprovider DESTINATION ${KDE_INSTALL_PLUGINDIR}/potd ) diff --git a/dataengines/potd/unsplashprovider.h b/dataengines/potd/unsplashbaseprovider.h copy from dataengines/potd/unsplashprovider.h copy to dataengines/potd/unsplashbaseprovider.h --- a/dataengines/potd/unsplashprovider.h +++ b/dataengines/potd/unsplashbaseprovider.h @@ -17,20 +17,23 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#ifndef UNSPLASHPROVIDER_H -#define UNSPLASHPROVIDER_H +#ifndef UNSPLASHBASEPROVIDER_H +#define UNSPLASHBASEPROVIDER_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 + * This class provides random wallpapers from Unsplash Wallpapers Desktop Collection + * https://unsplash.com/wallpaper/1065396/desktop-wallpapers + * + * By extending this class and modify mCollection (the number in url), + * we can get photo wallpaper from any collections */ -class UnsplashProvider : public PotdProvider +class UnsplashBaseProvider : public PotdProvider { Q_OBJECT @@ -41,12 +44,12 @@ * @param parent The parent object. * @param args The arguments. */ - UnsplashProvider( QObject *parent, const QVariantList &args ); + UnsplashBaseProvider( const int collectionId, QObject *parent, const QVariantList &args ); /** * Destroys the Unsplash provider. */ - ~UnsplashProvider() override; + ~UnsplashBaseProvider() override; /** * Returns the requested image. @@ -57,7 +60,6 @@ QImage image() const override; private: - void pageRequestFinished(KJob *job); void imageRequestFinished(KJob *job); private: diff --git a/dataengines/potd/unsplashbaseprovider.cpp b/dataengines/potd/unsplashbaseprovider.cpp new file mode 100644 --- /dev/null +++ b/dataengines/potd/unsplashbaseprovider.cpp @@ -0,0 +1,56 @@ +/* + * 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 "unsplashbaseprovider.h" + +#include +#include + +#include +#include + +UnsplashBaseProvider::UnsplashBaseProvider(const int collectionId, QObject* parent, const QVariantList& args) + : PotdProvider(parent, args) +{ + const QUrl url(QStringLiteral("https://source.unsplash.com/collection/%1/3840x2160/daily").arg(collectionId)); + + KIO::StoredTransferJob* job = KIO::storedGet(url, KIO::NoReload, KIO::HideProgressInfo); + connect(job, &KIO::StoredTransferJob::finished, this, &UnsplashBaseProvider::imageRequestFinished); +} + +UnsplashBaseProvider::~UnsplashBaseProvider() = default; + +QImage UnsplashBaseProvider::image() const +{ + return mImage; +} + +void UnsplashBaseProvider::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); +} + +#include "unsplashbaseprovider.moc" diff --git a/dataengines/potd/unsplashnatureprovider.h b/dataengines/potd/unsplashnatureprovider.h new file mode 100644 --- /dev/null +++ b/dataengines/potd/unsplashnatureprovider.h @@ -0,0 +1,37 @@ +/* + * 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 UNSPLASHNATUREPROVIDER_H +#define UNSPLASHNATUREPROVIDER_H + +#include "unsplashbaseprovider.h" + +/** + * This class provides random wallpapers from Unsplash Nature Wallpapers Collection + * https://unsplash.com/wallpaper/1065376/nature-wallpapers + */ +class UnsplashNatureProvider : public UnsplashBaseProvider +{ + Q_OBJECT +public: + UnsplashNatureProvider( QObject *parent, const QVariantList &args ); + ~UnsplashNatureProvider(); +}; + +#endif diff --git a/dataengines/potd/unsplashnatureprovider.cpp b/dataengines/potd/unsplashnatureprovider.cpp new file mode 100644 --- /dev/null +++ b/dataengines/potd/unsplashnatureprovider.cpp @@ -0,0 +1,35 @@ +/* + * 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 "unsplashnatureprovider.h" + +#include + +UnsplashNatureProvider::UnsplashNatureProvider(QObject* parent, const QVariantList& args) + : UnsplashBaseProvider(1065376, parent, args) +{ +} + +UnsplashNatureProvider::~UnsplashNatureProvider() +{ +} + +K_PLUGIN_CLASS_WITH_JSON(UnsplashNatureProvider, "unsplashnatureprovider.json") + +#include "unsplashnatureprovider.moc" diff --git a/dataengines/potd/unsplashnatureprovider.json b/dataengines/potd/unsplashnatureprovider.json new file mode 100644 --- /dev/null +++ b/dataengines/potd/unsplashnatureprovider.json @@ -0,0 +1,11 @@ +{ + "KPlugin": { + "Description": "Unsplash Nature Wallpaper Provider", + "Icon": "", + "Name": "Unsplash Nature Wallpapers", + "ServiceTypes": [ + "PlasmaPoTD/Plugin" + ] + }, + "X-KDE-PlasmaPoTDProvider-Identifier": "unsplashnature" +} diff --git a/dataengines/potd/unsplashprovider.h b/dataengines/potd/unsplashprovider.h --- a/dataengines/potd/unsplashprovider.h +++ b/dataengines/potd/unsplashprovider.h @@ -20,48 +20,18 @@ #ifndef UNSPLASHPROVIDER_H #define UNSPLASHPROVIDER_H -#include "potdprovider.h" -// Qt -#include - -class KJob; +#include "unsplashbaseprovider.h" /** - * This class provides random wallpapers from Unsplash Wallpapers - * Image urls are parsed from https://unsplash.com/wallpaper/1065396/desktop-wallpapers + * This class provides random wallpapers from Unsplash Desktop Wallpapers Collection + * https://unsplash.com/wallpaper/1065396/desktop-wallpapers */ -class UnsplashProvider : public PotdProvider +class UnsplashProvider : public UnsplashBaseProvider { 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; +public: + UnsplashProvider( QObject *parent, const QVariantList &args ); + ~UnsplashProvider(); }; #endif diff --git a/dataengines/potd/unsplashprovider.cpp b/dataengines/potd/unsplashprovider.cpp --- a/dataengines/potd/unsplashprovider.cpp +++ b/dataengines/potd/unsplashprovider.cpp @@ -19,73 +19,15 @@ #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 + : UnsplashBaseProvider(1065396, parent, args) { - 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()); - - // "?ixlib" will filter out the banner image which rarely change... - QRegularExpression re(QStringLiteral("src=\"(https://images\\.unsplash\\.com/photo-\\w+-\\w+)\\?ixlib")); - - QRegularExpressionMatchIterator i = re.globalMatch(html); - - QStringList urls; - - while (i.hasNext()) { - QRegularExpressionMatch match = i.next(); - QString url = match.captured(1); - urls << url; - } - - if (urls.size() > 0) { - // Pick a ramdom photo because the wallpaper page doesn't update every day - QUrl picUrl(urls.at(rand() % urls.size())); // 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) +UnsplashProvider::~UnsplashProvider() { - 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")