diff --git a/dataengines/potd/CMakeLists.txt b/dataengines/potd/CMakeLists.txt index 0c916ab2c..00c56ed31 100644 --- a/dataengines/potd/CMakeLists.txt +++ b/dataengines/potd/CMakeLists.txt @@ -1,137 +1,146 @@ set(potd_engine_SRCS cachedprovider.cpp potd.cpp ) add_library(plasma_engine_potd MODULE ${potd_engine_SRCS} ) target_link_libraries(plasma_engine_potd plasmapotdprovidercore KF5::Plasma KF5::KIOCore ) kcoreaddons_desktop_to_json(plasma_engine_potd plasma-dataengine-potd.desktop SERVICE_TYPES plasma-dataengine.desktop) install(TARGETS plasma_engine_potd DESTINATION ${KDE_INSTALL_PLUGINDIR}/plasma/dataengine ) install(FILES plasma-dataengine-potd.desktop DESTINATION ${KDE_INSTALL_KSERVICES5DIR} ) ########### plugin core library ############ set(POTDPROVIDER_VERSION 1.0.0) set(POTDPROVIDER_VERSION_MAJOR 1) set(potd_provider_core_SRCS potdprovider.cpp ${CMAKE_CURRENT_BINARY_DIR}/plasma_potd_export.h ) add_library( plasmapotdprovidercore SHARED ${potd_provider_core_SRCS} ) add_library(Plasma::PotdProvider ALIAS plasmapotdprovidercore) set_target_properties(plasmapotdprovidercore PROPERTIES VERSION ${POTDPROVIDER_VERSION} SOVERSION ${POTDPROVIDER_VERSION_MAJOR} EXPORT_NAME PotdProvider ) target_link_libraries( plasmapotdprovidercore Qt5::Gui KF5::CoreAddons ) target_include_directories(plasmapotdprovidercore PUBLIC "$" INTERFACE "$" ) generate_export_header(plasmapotdprovidercore BASE_NAME PLASMA_POTD EXPORT_FILE_NAME plasma_potd_export.h) install(TARGETS plasmapotdprovidercore EXPORT plasmapotdproviderTargets ${KDE_INSTALL_TARGETS_DEFAULT_ARGS} ) install(FILES potdprovider.h ${CMAKE_CURRENT_BINARY_DIR}/plasma_potd_export.h DESTINATION ${KDE_INSTALL_INCLUDEDIR}/plasma/potdprovider COMPONENT Devel ) write_basic_package_version_file(${CMAKE_CURRENT_BINARY_DIR}/PlasmaPotdProviderConfigVersion.cmake VERSION "${POTDPROVIDER_VERSION}" COMPATIBILITY SameMajorVersion ) set(CMAKECONFIG_INSTALL_DIR ${KDE_INSTALL_LIBDIR}/cmake/PlasmaPotdProvider) configure_package_config_file(PlasmaPotdProvider.cmake.in "${CMAKE_CURRENT_BINARY_DIR}/PlasmaPotdProviderConfig.cmake" INSTALL_DESTINATION ${CMAKECONFIG_INSTALL_DIR} ) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/PlasmaPotdProviderConfig.cmake ${CMAKE_CURRENT_BINARY_DIR}/PlasmaPotdProviderConfigVersion.cmake DESTINATION ${CMAKECONFIG_INSTALL_DIR} COMPONENT Devel ) install(EXPORT plasmapotdproviderTargets NAMESPACE Plasma:: DESTINATION ${CMAKECONFIG_INSTALL_DIR} FILE PlasmaPotdProviderTargets.cmake COMPONENT Devel ) ########### plugins ############### set( potd_flickr_provider_SRCS flickrprovider.cpp ) add_library( plasma_potd_flickrprovider MODULE ${potd_flickr_provider_SRCS} ) target_link_libraries( plasma_potd_flickrprovider plasmapotdprovidercore KF5::KIOCore KF5::CoreAddons) install( TARGETS plasma_potd_flickrprovider DESTINATION ${KDE_INSTALL_PLUGINDIR}/potd ) set( potd_apod_provider_SRCS apodprovider.cpp ) add_library( plasma_potd_apodprovider MODULE ${potd_apod_provider_SRCS} ) target_link_libraries( plasma_potd_apodprovider plasmapotdprovidercore KF5::KIOCore) install( TARGETS plasma_potd_apodprovider DESTINATION ${KDE_INSTALL_PLUGINDIR}/potd ) set( potd_natgeo_provider_SRCS natgeoprovider.cpp ) add_library( plasma_potd_natgeoprovider MODULE ${potd_natgeo_provider_SRCS} ) target_link_libraries( plasma_potd_natgeoprovider plasmapotdprovidercore KF5::KIOCore) install( TARGETS plasma_potd_natgeoprovider DESTINATION ${KDE_INSTALL_PLUGINDIR}/potd ) set( potd_epod_provider_SRCS epodprovider.cpp ) add_library( plasma_potd_epodprovider MODULE ${potd_epod_provider_SRCS} ) target_link_libraries( plasma_potd_epodprovider plasmapotdprovidercore KF5::KIOCore) install( TARGETS plasma_potd_epodprovider DESTINATION ${KDE_INSTALL_PLUGINDIR}/potd ) set( potd_noaa_provider_SRCS noaaprovider.cpp ) add_library( plasma_potd_noaaprovider MODULE ${potd_noaa_provider_SRCS} ) target_link_libraries( plasma_potd_noaaprovider plasmapotdprovidercore KF5::KIOCore) install( TARGETS plasma_potd_noaaprovider DESTINATION ${KDE_INSTALL_PLUGINDIR}/potd ) set( potd_wcpotd_provider_SRCS wcpotdprovider.cpp ) add_library( plasma_potd_wcpotdprovider MODULE ${potd_wcpotd_provider_SRCS} ) target_link_libraries( plasma_potd_wcpotdprovider plasmapotdprovidercore KF5::KIOCore ) install( TARGETS plasma_potd_wcpotdprovider DESTINATION ${KDE_INSTALL_PLUGINDIR}/potd ) set( potd_bing_provider_SRCS bingprovider.cpp ) add_library( plasma_potd_bingprovider MODULE ${potd_bing_provider_SRCS} ) 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.cpp b/dataengines/potd/unsplashprovider.cpp new file mode 100644 index 000000000..ce4e2c139 --- /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.h b/dataengines/potd/unsplashprovider.h new file mode 100644 index 000000000..31c1bb448 --- /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.json b/dataengines/potd/unsplashprovider.json new file mode 100644 index 000000000..5b41316c9 --- /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" +}