diff --git a/dataengines/potd/CMakeLists.txt b/dataengines/potd/CMakeLists.txt --- a/dataengines/potd/CMakeLists.txt +++ b/dataengines/potd/CMakeLists.txt @@ -91,6 +91,15 @@ install( TARGETS plasma_potd_apodprovider DESTINATION ${KDE_INSTALL_PLUGINDIR}/potd ) +set( potd_siod_provider_SRCS + siodprovider.cpp +) + +add_library( plasma_potd_siodprovider MODULE ${potd_siod_provider_SRCS} ) +target_link_libraries( plasma_potd_siodprovider plasmapotdprovidercore KF5::KIOCore) + +install( TARGETS plasma_potd_siodprovider DESTINATION ${KDE_INSTALL_PLUGINDIR}/potd ) + set( potd_natgeo_provider_SRCS natgeoprovider.cpp ) diff --git a/dataengines/potd/siodprovider.h b/dataengines/potd/siodprovider.h new file mode 100644 --- /dev/null +++ b/dataengines/potd/siodprovider.h @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2018 Tagore Chandan Reddy + * + * 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 SIODPROVIDER_H +#define SIODPROVIDER_H + +#include "potdprovider.h" + +#include + +class KJob; + +/** + * This class provides the image for SIOD + * "Space Image of the Day" + * located at http://www.space.com/imageoftheday/. + */ +class SiodProvider : public PotdProvider +{ + Q_OBJECT + + public: + /** + * Creates a new SIOD provider. + * + * @param date The date for which the image shall be fetched. + * @param parent The parent object. + */ + explicit SiodProvider(QObject *parent, const QVariantList &args); + + /** + * Destroys the SIOD provider. + */ + ~SiodProvider() 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/siodprovider.cpp b/dataengines/potd/siodprovider.cpp new file mode 100644 --- /dev/null +++ b/dataengines/potd/siodprovider.cpp @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2018 Tagore Chandan Reddy + * + * 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 "siodprovider.h" + +#include +#include + +#include +#include + +SiodProvider::SiodProvider(QObject *parent, const QVariantList &args) + : PotdProvider(parent, args) +{ + const QUrl url(QStringLiteral("http://www.space.com/imageoftheday/")); + + KIO::StoredTransferJob *job = KIO::storedGet(url, KIO::NoReload, KIO::HideProgressInfo); + connect(job, &KIO::StoredTransferJob::finished, this, &SiodProvider::pageRequestFinished); +} + +SiodProvider::~SiodProvider() = default; + +QImage SiodProvider::image() const +{ + return mImage; +} + +void SiodProvider::pageRequestFinished(KJob *_job) +{ + KIO::StoredTransferJob *job = static_cast(_job); + if (job->error()) + { + emit error(this); + return; + } + + const QString data = QString::fromUtf8(job->data()); + + const QString pattern = QStringLiteral("^$"); + QRegularExpression exp(pattern, QRegularExpression::MultilineOption); + auto result = exp.match(data); + if (result.hasMatch()) + { + const QUrl url(result.captured(1)); + KIO::StoredTransferJob *imageJob = KIO::storedGet(url, KIO::NoReload, KIO::HideProgressInfo); + connect(imageJob, &KIO::StoredTransferJob::finished, this, &SiodProvider::imageRequestFinished); + } + else + { + emit error(this); + } +} + +void SiodProvider::imageRequestFinished(KJob *_job) +{ + KIO::StoredTransferJob *job = static_cast(_job); + if (job->error()) + { + emit error(this); + return; + } + + mImage = QImage::fromData(job->data()); + emit finished(this); +} + +K_PLUGIN_FACTORY_WITH_JSON(SiodProviderFactory, "siodprovider.json", registerPlugin();) + +#include "siodprovider.moc" diff --git a/dataengines/potd/siodprovider.json b/dataengines/potd/siodprovider.json new file mode 100644 --- /dev/null +++ b/dataengines/potd/siodprovider.json @@ -0,0 +1,9 @@ +{ + "KPlugin": { + "Description": "Siod Provider", + "ServiceTypes": [ + "PlasmaPoTD/Plugin" + ] + }, + "X-KDE-PlasmaPoTDProvider-Identifier": "siod" +}