diff --git a/dataengines/potd/CMakeLists.txt b/dataengines/potd/CMakeLists.txt --- a/dataengines/potd/CMakeLists.txt +++ b/dataengines/potd/CMakeLists.txt @@ -21,6 +21,7 @@ set(potd_provider_core_SRCS potdprovider.cpp + unsplashprovider.cpp ${CMAKE_CURRENT_BINARY_DIR}/plasma_potd_export.h ) @@ -31,7 +32,7 @@ SOVERSION ${POTDPROVIDER_VERSION_MAJOR} EXPORT_NAME PotdProvider ) -target_link_libraries( plasmapotdprovidercore Qt5::Gui KF5::CoreAddons ) +target_link_libraries( plasmapotdprovidercore Qt5::Gui KF5::CoreAddons KF5::KIOCore ) target_include_directories(plasmapotdprovidercore PUBLIC "$" INTERFACE "$" @@ -41,6 +42,7 @@ install(TARGETS plasmapotdprovidercore EXPORT plasmapotdproviderTargets ${KDE_INSTALL_TARGETS_DEFAULT_ARGS} ) install(FILES potdprovider.h + unsplashprovider.h ${CMAKE_CURRENT_BINARY_DIR}/plasma_potd_export.h DESTINATION ${KDE_INSTALL_INCLUDEDIR}/plasma/potdprovider COMPONENT Devel @@ -136,11 +138,24 @@ install( TARGETS plasma_potd_bingprovider DESTINATION ${KDE_INSTALL_PLUGINDIR}/potd ) -set( potd_unsplash_provider_SRCS - unsplashprovider.cpp +# Unsplash + +set( potd_unsplash_all_provider_SRCS + unsplashallprovider.cpp +) + +add_library( plasma_potd_unsplashallprovider MODULE ${potd_unsplash_all_provider_SRCS} ) +target_link_libraries( plasma_potd_unsplashallprovider plasmapotdprovidercore KF5::KIOCore ) + +install( TARGETS plasma_potd_unsplashallprovider DESTINATION ${KDE_INSTALL_PLUGINDIR}/potd ) + +# Unsplash Nature + +set( potd_unsplash_nature_provider_SRCS + unsplashnatureprovider.cpp ) -add_library( plasma_potd_unsplashprovider MODULE ${potd_unsplash_provider_SRCS} ) -target_link_libraries( plasma_potd_unsplashprovider plasmapotdprovidercore KF5::KIOCore ) +add_library( plasma_potd_unsplashnatureprovider MODULE ${potd_unsplash_nature_provider_SRCS} ) +target_link_libraries( plasma_potd_unsplashnatureprovider plasmapotdprovidercore KF5::KIOCore ) -install( TARGETS plasma_potd_unsplashprovider DESTINATION ${KDE_INSTALL_PLUGINDIR}/potd ) +install( TARGETS plasma_potd_unsplashnatureprovider DESTINATION ${KDE_INSTALL_PLUGINDIR}/potd ) diff --git a/dataengines/potd/unsplashallprovider.h b/dataengines/potd/unsplashallprovider.h new file mode 100644 --- /dev/null +++ b/dataengines/potd/unsplashallprovider.h @@ -0,0 +1,40 @@ +/* + * 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 UNSPLASHALLPROVIDER_H +#define UNSPLASHALLPROVIDER_H + +#include "unsplashprovider.h" + +/** + * This class provides random wallpapers from Unsplash Desktop Wallpapers Collection + * https://unsplash.com/wallpaper/1065396/desktop-wallpapers + */ +class UnsplashAllProvider : public UnsplashProvider +{ + Q_OBJECT +public: + UnsplashAllProvider( QObject *parent, const QVariantList &args ); + ~UnsplashAllProvider(); + +private: + const int mCollection = 1065396; +}; + +#endif diff --git a/dataengines/potd/unsplashallprovider.cpp b/dataengines/potd/unsplashallprovider.cpp new file mode 100644 --- /dev/null +++ b/dataengines/potd/unsplashallprovider.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 "unsplashallprovider.h" + +#include + +UnsplashAllProvider::UnsplashAllProvider(QObject* parent, const QVariantList& args) + : UnsplashProvider(parent, args) +{ +} + +UnsplashAllProvider::~UnsplashAllProvider() +{ +} + +K_PLUGIN_CLASS_WITH_JSON(UnsplashAllProvider, "unsplashallprovider.json") + +#include "unsplashallprovider.moc" diff --git a/dataengines/potd/unsplashprovider.json b/dataengines/potd/unsplashallprovider.json rename from dataengines/potd/unsplashprovider.json rename to dataengines/potd/unsplashallprovider.json --- a/dataengines/potd/unsplashprovider.json +++ b/dataengines/potd/unsplashallprovider.json @@ -29,5 +29,5 @@ "PlasmaPoTD/Plugin" ] }, - "X-KDE-PlasmaPoTDProvider-Identifier": "unsplash" + "X-KDE-PlasmaPoTDProvider-Identifier": "unsplashall" } 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,40 @@ +/* + * 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 "unsplashprovider.h" + +/** + * This class provides random wallpapers from Unsplash Nature Wallpapers Collection + * https://unsplash.com/wallpaper/1065376/nature-wallpapers + */ +class UnsplashNatureProvider : public UnsplashProvider +{ + Q_OBJECT +public: + UnsplashNatureProvider( QObject *parent, const QVariantList &args ); + ~UnsplashNatureProvider(); + +protected: + const int mCollection = 1065376; +}; + +#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) + : UnsplashProvider(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 @@ -27,8 +27,11 @@ 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 { @@ -56,12 +59,14 @@ */ QImage image() const override; + int collection() const; + private: - void pageRequestFinished(KJob *job); void imageRequestFinished(KJob *job); private: QImage mImage; + const int mCollection = 1065396; }; #endif diff --git a/dataengines/potd/unsplashprovider.cpp b/dataengines/potd/unsplashprovider.cpp --- a/dataengines/potd/unsplashprovider.cpp +++ b/dataengines/potd/unsplashprovider.cpp @@ -28,10 +28,10 @@ UnsplashProvider::UnsplashProvider(QObject* parent, const QVariantList& args) : PotdProvider(parent, args) { - const QUrl url(QStringLiteral("https://unsplash.com/wallpaper/1065396/desktop-wallpapers")); + const QUrl url(QStringLiteral("https://source.unsplash.com/collection/%1/3840x2160/daily").arg(collection())); KIO::StoredTransferJob* job = KIO::storedGet(url, KIO::NoReload, KIO::HideProgressInfo); - connect(job, &KIO::StoredTransferJob::finished, this, &UnsplashProvider::pageRequestFinished); + connect(job, &KIO::StoredTransferJob::finished, this, &UnsplashProvider::imageRequestFinished); } UnsplashProvider::~UnsplashProvider() = default; @@ -41,39 +41,9 @@ return mImage; } -void UnsplashProvider::pageRequestFinished(KJob* _job) +int UnsplashProvider::collection() const { - 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; - } + return mCollection; } void UnsplashProvider::imageRequestFinished(KJob* _job) @@ -88,6 +58,4 @@ emit finished(this); } -K_PLUGIN_CLASS_WITH_JSON(UnsplashProvider, "unsplashprovider.json") - #include "unsplashprovider.moc"