diff --git a/dataengines/potd/apodprovider.cpp b/dataengines/potd/apodprovider.cpp index 31df1806b..7db426293 100644 --- a/dataengines/potd/apodprovider.cpp +++ b/dataengines/potd/apodprovider.cpp @@ -1,101 +1,82 @@ /* * Copyright (C) 2007 Tobias Koenig * Copyright 2008 by Anne-Marie Mahfouf * * 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 "apodprovider.h" #include -#include #include -#include +#include +#include -class ApodProvider::Private +ApodProvider::ApodProvider(QObject *parent, const QVariantList &args) + : PotdProvider(parent, args) { - public: - Private( ApodProvider *parent ) - : mParent( parent ) - { - } + const QUrl url(QStringLiteral("http://antwrp.gsfc.nasa.gov/apod/")); + + KIO::StoredTransferJob *job = KIO::storedGet(url, KIO::NoReload, KIO::HideProgressInfo); + connect(job, &KIO::StoredTransferJob::finished, this, &ApodProvider::pageRequestFinished); +} - void pageRequestFinished( KJob* ); - void imageRequestFinished( KJob* ); - void parsePage(); +ApodProvider::~ApodProvider() = default; - ApodProvider *mParent; - QImage mImage; -}; +QImage ApodProvider::image() const +{ + return mImage; +} -void ApodProvider::Private::pageRequestFinished( KJob *_job ) +void ApodProvider::pageRequestFinished(KJob *_job) { KIO::StoredTransferJob *job = static_cast( _job ); if ( job->error() ) { - emit mParent->error( mParent ); + emit error(this); return; } const QString data = QString::fromUtf8( job->data() ); const QString pattern = QStringLiteral("connect( imageJob, SIGNAL(finished(KJob*)), SLOT(imageRequestFinished(KJob*)) ); + connect(imageJob, &KIO::StoredTransferJob::finished, this, &ApodProvider::imageRequestFinished); } else { - emit mParent->error( mParent ); + emit error(this); } } -void ApodProvider::Private::imageRequestFinished( KJob *_job ) +void ApodProvider::imageRequestFinished(KJob *_job) { KIO::StoredTransferJob *job = static_cast( _job ); if ( job->error() ) { - emit mParent->error( mParent ); + emit error(this); return; } mImage = QImage::fromData( job->data() ); - emit mParent->finished( mParent ); -} - -ApodProvider::ApodProvider( QObject *parent, const QVariantList &args ) - : PotdProvider( parent, args ), d( new Private( this ) ) -{ - QUrl url( QLatin1String( "http://antwrp.gsfc.nasa.gov/apod/" ) ); - KIO::StoredTransferJob *job = KIO::storedGet( url, KIO::NoReload, KIO::HideProgressInfo ); - connect( job, SIGNAL(finished(KJob*)), SLOT(pageRequestFinished(KJob*)) ); -} - -ApodProvider::~ApodProvider() -{ - delete d; -} - -QImage ApodProvider::image() const -{ - return d->mImage; + emit finished(this); } K_PLUGIN_FACTORY_WITH_JSON(ApodProviderFactory, "apodprovider.json", registerPlugin();) -#include "moc_apodprovider.cpp" #include "apodprovider.moc" diff --git a/dataengines/potd/apodprovider.h b/dataengines/potd/apodprovider.h index 3d721e2c4..7f8eb9bb7 100644 --- a/dataengines/potd/apodprovider.h +++ b/dataengines/potd/apodprovider.h @@ -1,69 +1,73 @@ /* * Copyright (C) 2007 Tobias Koenig * Copyright 2008 by Anne-Marie Mahfouf * * 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 APODPROVIDER_H #define APODPROVIDER_H #include "potdprovider.h" +#include + +class KJob; + /** * This class provides the image for APOD * "Astronomy Picture Of the Day" * located at http://antwrp.gsfc.nasa.gov/apod. * Direct link to the picture of the day page is * http://antwrp.gsfc.nasa.gov/apod/apYYMMDD.html * where YY is the year last 2 digits, * MM is the month and DD the day, in 2 digits. */ class ApodProvider : public PotdProvider { Q_OBJECT public: /** * Creates a new APOD provider. * * @param date The date for which the image shall be fetched. * @param parent The parent object. */ ApodProvider( QObject *parent, const QVariantList &args ); /** * Destroys the APOD provider. */ ~ApodProvider() 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: - class Private; - Private* const d; + void pageRequestFinished(KJob *job); + void imageRequestFinished(KJob *job); - Q_PRIVATE_SLOT( d, void pageRequestFinished( KJob* ) ) - Q_PRIVATE_SLOT( d, void imageRequestFinished( KJob* ) ) + private: + QImage mImage; }; #endif diff --git a/dataengines/potd/bingprovider.cpp b/dataengines/potd/bingprovider.cpp index f22e2ff08..6f986e393 100644 --- a/dataengines/potd/bingprovider.cpp +++ b/dataengines/potd/bingprovider.cpp @@ -1,116 +1,94 @@ /* * Copyright 2017 Weng Xuetian * * 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 "bingprovider.h" -#include -#include #include #include #include -#include +#include +#include -class BingProvider::Private +BingProvider::BingProvider(QObject* parent, const QVariantList& args) + : PotdProvider(parent, args) { -public: - Private(BingProvider* parent) - : mParent(parent) - { - } + const QUrl url(QStringLiteral("https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1")); - void pageRequestFinished(KJob*); - void imageRequestFinished(KJob*); - void parsePage(); + KIO::StoredTransferJob* job = KIO::storedGet(url, KIO::NoReload, KIO::HideProgressInfo); + connect(job, &KIO::StoredTransferJob::finished, this, &BingProvider::pageRequestFinished); +} - BingProvider* mParent; - QImage mImage; -}; +BingProvider::~BingProvider() = default; -void BingProvider::Private::pageRequestFinished(KJob* _job) +QImage BingProvider::image() const +{ + return mImage; +} + +void BingProvider::pageRequestFinished(KJob* _job) { KIO::StoredTransferJob* job = static_cast(_job); if (job->error()) { - emit mParent->error(mParent); + emit error(this); return; } auto json = QJsonDocument::fromJson(job->data()); do { if (json.isNull()) { break; } auto imagesArray = json.object().value(QLatin1String("images")); if (!imagesArray.isArray() || imagesArray.toArray().size() <= 0) { break; } auto imageObj = imagesArray.toArray().at(0); if (!imageObj.isObject()) { break; } auto url = imageObj.toObject().value(QLatin1String("url")); if (!url.isString() || url.toString().isEmpty()) { break; } QUrl picUrl(QStringLiteral("https://www.bing.com/%1").arg(url.toString())); KIO::StoredTransferJob* imageJob = KIO::storedGet(picUrl, KIO::NoReload, KIO::HideProgressInfo); - mParent->connect(imageJob, SIGNAL(finished(KJob*)), SLOT(imageRequestFinished(KJob*))); + connect(imageJob, &KIO::StoredTransferJob::finished, this, &BingProvider::imageRequestFinished); return; } while (0); - emit mParent->error(mParent); + emit error(this); return; } -void BingProvider::Private::imageRequestFinished(KJob* _job) +void BingProvider::imageRequestFinished(KJob* _job) { KIO::StoredTransferJob* job = static_cast(_job); if (job->error()) { - emit mParent->error(mParent); + emit error(this); return; } QByteArray data = job->data(); mImage = QImage::fromData(data); - emit mParent->finished(mParent); -} - -BingProvider::BingProvider(QObject* parent, const QVariantList& args) - : PotdProvider(parent, args), d(new Private(this)) -{ - QUrl url(QStringLiteral("https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1")); - - KIO::StoredTransferJob* job = KIO::storedGet(url, KIO::NoReload, KIO::HideProgressInfo); - connect(job, SIGNAL(finished(KJob*)), SLOT(pageRequestFinished(KJob*))); -} - -BingProvider::~BingProvider() -{ - delete d; -} - -QImage BingProvider::image() const -{ - return d->mImage; + emit finished(this); } K_PLUGIN_FACTORY_WITH_JSON(BingProviderFactory, "bingprovider.json", registerPlugin();) #include "bingprovider.moc" -#include "moc_bingprovider.cpp" - diff --git a/dataengines/potd/bingprovider.h b/dataengines/potd/bingprovider.h index e427e7fef..e9221550f 100644 --- a/dataengines/potd/bingprovider.h +++ b/dataengines/potd/bingprovider.h @@ -1,64 +1,67 @@ /* * Copyright 2017 Weng Xuetian * * 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 BINGPROVIDER_H #define BINGPROVIDER_H #include "potdprovider.h" +// Qt +#include + +class KJob; /** * This class provides the image for the Bing's homepage * url is obtained from http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1 */ class BingProvider : public PotdProvider { Q_OBJECT public: /** * Creates a new Bing provider. * * @param date The date for which the image shall be fetched. * @param parent The parent object. */ BingProvider( QObject *parent, const QVariantList &args ); /** * Destroys the Bing provider. */ ~BingProvider() 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: - class Private; - Private* const d; + void pageRequestFinished(KJob *job); + void imageRequestFinished(KJob *job); - Q_PRIVATE_SLOT( d, void pageRequestFinished( KJob* ) ) - Q_PRIVATE_SLOT( d, void imageRequestFinished( KJob* ) ) + private: + QImage mImage; }; #endif - diff --git a/dataengines/potd/cachedprovider.cpp b/dataengines/potd/cachedprovider.cpp index 4229205ba..b45976827 100644 --- a/dataengines/potd/cachedprovider.cpp +++ b/dataengines/potd/cachedprovider.cpp @@ -1,112 +1,112 @@ /* * Copyright (C) 2007 Tobias Koenig * * 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 "cachedprovider.h" #include #include #include #include #include #include -#include +#include #include LoadImageThread::LoadImageThread(const QString &filePath) : m_filePath(filePath) { } void LoadImageThread::run() { QImage image; image.load(m_filePath ); emit done(image); } SaveImageThread::SaveImageThread(const QString &identifier, const QImage &image) : m_image(image), m_identifier(identifier) { } void SaveImageThread::run() { const QString path = CachedProvider::identifierToPath( m_identifier ); m_image.save(path, "PNG"); emit done( m_identifier, path, m_image ); } QString CachedProvider::identifierToPath( const QString &identifier ) { const QString dataDir = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + QLatin1String("/plasma_engine_potd/"); QDir d; d.mkpath(dataDir); return dataDir + identifier; } CachedProvider::CachedProvider( const QString &identifier, QObject *parent ) : PotdProvider( parent ), mIdentifier( identifier ) { LoadImageThread *thread = new LoadImageThread( identifierToPath( mIdentifier ) ); connect(thread, SIGNAL(done(QImage)), this, SLOT(triggerFinished(QImage))); QThreadPool::globalInstance()->start(thread); } CachedProvider::~CachedProvider() { } QImage CachedProvider::image() const { return mImage; } QString CachedProvider::identifier() const { return mIdentifier; } void CachedProvider::triggerFinished(const QImage &image) { mImage = image; emit finished( this ); } bool CachedProvider::isCached( const QString &identifier, bool ignoreAge ) { const QString path = identifierToPath( identifier ); if (!QFile::exists( path ) ) { return false; } if (!ignoreAge && !identifier.contains(QLatin1Char(':'))) { // no date in the identifier, so it's a daily; check to see ifthe modification time is today QFileInfo info( path ); if ( info.lastModified().daysTo( QDateTime::currentDateTime() ) > 1 ) { return false; } } return true; } diff --git a/dataengines/potd/epodprovider.cpp b/dataengines/potd/epodprovider.cpp index 07093930f..f924d97d9 100644 --- a/dataengines/potd/epodprovider.cpp +++ b/dataengines/potd/epodprovider.cpp @@ -1,101 +1,81 @@ /* * Copyright (C) 2007 Tobias Koenig * Copyright 2008 by Anne-Marie Mahfouf * * 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 "epodprovider.h" #include -#include #include -#include +#include +#include -class EpodProvider::Private +EpodProvider::EpodProvider( QObject *parent, const QVariantList &args ) + : PotdProvider(parent, args) { - public: - Private( EpodProvider *parent ) - : mParent( parent ) - { - } + const QUrl url(QStringLiteral("http://epod.usra.edu/blog/")); - void pageRequestFinished(KJob*); - void imageRequestFinished(KJob*); - void parsePage(); + KIO::StoredTransferJob *job = KIO::storedGet(url, KIO::NoReload, KIO::HideProgressInfo); + connect(job, &KIO::StoredTransferJob::finished, this, &EpodProvider::pageRequestFinished); +} - EpodProvider *mParent; - QImage mImage; -}; +EpodProvider::~EpodProvider() = default; + +QImage EpodProvider::image() const +{ + return mImage; +} -void EpodProvider::Private::pageRequestFinished(KJob *_job) +void EpodProvider::pageRequestFinished(KJob *_job) { KIO::StoredTransferJob *job = static_cast(_job); if ( job->error() ) { - emit mParent->error( mParent ); + emit error(this); return; } const QString data = QString::fromUtf8( job->data() ); const QString pattern = QStringLiteral("http://epod.usra.edu/.a/*-pi"); QRegExp exp( pattern ); exp.setPatternSyntax(QRegExp::Wildcard); int pos = exp.indexIn( data ) + pattern.length(); const QString sub = data.mid( pos-4, pattern.length()+6); const QUrl url(QStringLiteral("http://epod.usra.edu/.a/%1-pi").arg(sub)); KIO::StoredTransferJob *imageJob = KIO::storedGet( url, KIO::NoReload, KIO::HideProgressInfo ); - QObject::connect(imageJob, SIGNAL(finished(KJob*)), mParent, SLOT(imageRequestFinished(KJob*)) ); + connect(imageJob, &KIO::StoredTransferJob::finished, this, &EpodProvider::imageRequestFinished); } -void EpodProvider::Private::imageRequestFinished( KJob *_job) +void EpodProvider::imageRequestFinished(KJob *_job) { KIO::StoredTransferJob *job = static_cast(_job); if ( job->error() ) { - emit mParent->error( mParent ); + emit error(this); return; } // FIXME: this really should be done in a thread as this can block mImage = QImage::fromData( job->data() ); - emit mParent->finished( mParent ); -} - -EpodProvider::EpodProvider( QObject *parent, const QVariantList &args ) - : PotdProvider( parent, args ), d( new Private( this ) ) -{ - QUrl url( QLatin1String( "http://epod.usra.edu/blog/" ) ); - KIO::StoredTransferJob *job = KIO::storedGet( url, KIO::NoReload, KIO::HideProgressInfo ); - - connect( job, SIGNAL(finished(KJob*)), SLOT(pageRequestFinished(KJob*)) ); -} - -EpodProvider::~EpodProvider() -{ - delete d; -} - -QImage EpodProvider::image() const -{ - return d->mImage; + emit finished(this); } K_PLUGIN_FACTORY_WITH_JSON(EpodProviderFactory, "epodprovider.json", registerPlugin();) -#include "moc_epodprovider.cpp" #include "epodprovider.moc" diff --git a/dataengines/potd/epodprovider.h b/dataengines/potd/epodprovider.h index 6f96b6329..4644e6a27 100644 --- a/dataengines/potd/epodprovider.h +++ b/dataengines/potd/epodprovider.h @@ -1,65 +1,69 @@ /* * Copyright (C) 2007 Tobias Koenig * Copyright 2008 by Anne-Marie Mahfouf * * 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 EPODPROVIDER_H #define EPODPROVIDER_H #include "potdprovider.h" +// Qt +#include + +class KJob; /** * This class provides the image for EPOD * "Earth Science Picture Of the Day" * located at http://epod.usra.edu/. */ class EpodProvider : public PotdProvider { Q_OBJECT public: /** * Creates a new EPOD provider. * * @param date The date for which the image shall be fetched. * @param parent The parent object. */ EpodProvider( QObject *parent, const QVariantList &args ); /** * Destroys the EPOD provider. */ ~EpodProvider() 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: - class Private; - Private* const d; + void pageRequestFinished(KJob *job); + void imageRequestFinished(KJob *job); - Q_PRIVATE_SLOT( d, void pageRequestFinished( KJob* ) ) - Q_PRIVATE_SLOT( d, void imageRequestFinished( KJob* ) ) + private: + QImage mImage; }; #endif diff --git a/dataengines/potd/flickrprovider.cpp b/dataengines/potd/flickrprovider.cpp index 00ff172c6..51005044d 100644 --- a/dataengines/potd/flickrprovider.cpp +++ b/dataengines/potd/flickrprovider.cpp @@ -1,193 +1,167 @@ /* * Copyright (C) 2007 Tobias Koenig * Copyright 2008 by Anne-Marie Mahfouf * Copyright 2008 by Georges Toth * * 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 "flickrprovider.h" -#include -#include -#include #include #include -#include +#include +#include #define FLICKR_API_KEY QStringLiteral("11829a470557ad8e10b02e80afacb3af") -class FlickrProvider::Private +static +QUrl buildUrl(const QDate &date) { - public: - Private( FlickrProvider *parent ) - : mParent( parent ) - { - qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); - } + QUrl url(QLatin1String( "https://api.flickr.com/services/rest/")); + QUrlQuery urlQuery(url); + urlQuery.addQueryItem(QStringLiteral("api_key"), FLICKR_API_KEY); + urlQuery.addQueryItem(QStringLiteral("method"), QStringLiteral("flickr.interestingness.getList")); + urlQuery.addQueryItem(QStringLiteral("date"), date.toString(Qt::ISODate)); + // url_o might be either too small or too large. + urlQuery.addQueryItem(QStringLiteral("extras"), QStringLiteral("url_k,url_h,url_o")); + url.setQuery(urlQuery); + + return url; +} - QUrl buildUrl(const QDate &date) { - QUrl url(QLatin1String( "https://api.flickr.com/services/rest/")); - QUrlQuery urlQuery(url); - urlQuery.addQueryItem(QStringLiteral("api_key"), FLICKR_API_KEY); - urlQuery.addQueryItem(QStringLiteral("method"), QStringLiteral("flickr.interestingness.getList")); - urlQuery.addQueryItem(QStringLiteral("date"), date.toString(Qt::ISODate)); - // url_o might be either too small or too large. - urlQuery.addQueryItem(QStringLiteral("extras"), QStringLiteral("url_k,url_h,url_o")); - url.setQuery(urlQuery); - - return url; - } +FlickrProvider::FlickrProvider(QObject *parent, const QVariantList &args) + : PotdProvider(parent, args) +{ + qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); - void pageRequestFinished( KJob* ); - void imageRequestFinished( KJob* ); - void parsePage(); + mActualDate = date(); - FlickrProvider *mParent; - QDate mActualDate; - QImage mImage; + const QUrl url = buildUrl(mActualDate); - QXmlStreamReader xml; + KIO::StoredTransferJob *job = KIO::storedGet(url, KIO::NoReload, KIO::HideProgressInfo); + connect(job, &KIO::StoredTransferJob::finished, this, &FlickrProvider::pageRequestFinished); +} - int mFailureNumber = 0; +FlickrProvider::~FlickrProvider() = default; - private: - QStringList m_photoList; -}; +QImage FlickrProvider::image() const +{ + return mImage; +} -void FlickrProvider::Private::pageRequestFinished( KJob *_job ) +void FlickrProvider::pageRequestFinished(KJob *_job) { KIO::StoredTransferJob *job = static_cast( _job ); if (job->error()) { - emit mParent->error( mParent ); + emit error(this); qDebug() << "pageRequestFinished error"; return; } const QString data = QString::fromUtf8( job->data() ); // Clear the list m_photoList.clear(); xml.clear(); xml.addData(data); while (!xml.atEnd()) { xml.readNext(); if (xml.isStartElement()) { auto attributes = xml.attributes(); if (xml.name() == QLatin1String("rsp")) { const int maxFailure = 5; /* no pictures available for the specified parameters */ if (attributes.value ( QLatin1String( "stat" ) ).toString() != QLatin1String( "ok" )) { if (mFailureNumber < maxFailure) { /* To be sure, decrement the date to two days earlier... @TODO */ mActualDate = mActualDate.addDays(-2); QUrl url = buildUrl(mActualDate); KIO::StoredTransferJob *pageJob = KIO::storedGet(url, KIO::NoReload, KIO::HideProgressInfo); - mParent->connect( pageJob, SIGNAL(finished(KJob*)), SLOT(pageRequestFinished(KJob*)) ); + connect(pageJob, &KIO::StoredTransferJob::finished, this, &FlickrProvider::pageRequestFinished); mFailureNumber++; return; } else { - emit mParent->error(mParent); + emit error(this); qDebug() << "pageRequestFinished error"; return; } } } else if (xml.name() == QLatin1String( "photo" )) { if (attributes.value ( QLatin1String( "ispublic" ) ).toString() != QLatin1String( "1" )) { continue; } const char *fallbackList[] = { "url_k", "url_h" }; bool found = false; for (auto urlAttr : fallbackList) { // Get the best url. QLatin1String urlAttrString(urlAttr); if (attributes.hasAttribute(urlAttrString)) { m_photoList.append(attributes.value(urlAttrString).toString()); found = true; break; } } // The logic here is, if url_h or url_k are present, url_o must // has higher quality, otherwise, url_o is worse than k/h size. // If url_o is better, prefer url_o. if (found) { QLatin1String originAttr("url_o"); if (attributes.hasAttribute(originAttr)) { m_photoList.back() = attributes.value(QLatin1String(originAttr)).toString(); } } } } } if (xml.error() && xml.error() != QXmlStreamReader::PrematureEndOfDocumentError) { qWarning() << "XML ERROR:" << xml.lineNumber() << ": " << xml.errorString(); } if (m_photoList.begin() != m_photoList.end()) { QUrl url( m_photoList.at(qrand() % m_photoList.size()) ); KIO::StoredTransferJob *imageJob = KIO::storedGet(url, KIO::NoReload, KIO::HideProgressInfo); - mParent->connect( imageJob, SIGNAL(finished(KJob*)), SLOT(imageRequestFinished(KJob*)) ); + connect(imageJob, &KIO::StoredTransferJob::finished, this, &FlickrProvider::imageRequestFinished); } else { qDebug() << "empty list"; } } -void FlickrProvider::Private::imageRequestFinished( KJob *_job ) +void FlickrProvider::imageRequestFinished(KJob *_job) { KIO::StoredTransferJob *job = static_cast( _job ); if ( job->error() ) { - emit mParent->error( mParent ); + emit error(this); return; } mImage = QImage::fromData( job->data() ); - emit mParent->finished( mParent ); -} - -FlickrProvider::FlickrProvider( QObject *parent, const QVariantList &args ) - : PotdProvider( parent, args ), d( new Private( this ) ) -{ - d->mActualDate = date(); - - QUrl url = d->buildUrl(date()); - KIO::StoredTransferJob *job = KIO::storedGet( url, KIO::NoReload, KIO::HideProgressInfo ); - connect( job, SIGNAL(finished(KJob*)), SLOT(pageRequestFinished(KJob*)) ); -} - -FlickrProvider::~FlickrProvider() -{ - delete d; -} - -QImage FlickrProvider::image() const -{ - return d->mImage; + emit finished(this); } K_PLUGIN_FACTORY_WITH_JSON(FlickrProviderFactory, "flickrprovider.json", registerPlugin();) -#include "moc_flickrprovider.cpp" #include "flickrprovider.moc" diff --git a/dataengines/potd/flickrprovider.h b/dataengines/potd/flickrprovider.h index 23e7b054c..9b9046549 100644 --- a/dataengines/potd/flickrprovider.h +++ b/dataengines/potd/flickrprovider.h @@ -1,68 +1,80 @@ /* * Copyright (C) 2007 Tobias Koenig * Copyright 2008 by Anne-Marie Mahfouf * Copyright 2008 by Georges Toth * * 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 FLICKRPROVIDER_H #define FLICKRPROVIDER_H #include "potdprovider.h" -#include +// Qt +#include +#include +#include + +class KJob; /** * This class grabs a random image from the flickr * interestingness stream of pictures, for the given date. * Should there be no image for the current date, it tries * to grab one from the day before yesterday. */ class FlickrProvider : public PotdProvider { Q_OBJECT public: /** * Creates a new flickr provider. * * @param date The date for which the image shall be fetched. * @param parent The parent object. */ FlickrProvider( QObject *parent, const QVariantList &args ); /** * Destroys the flickr provider. */ ~FlickrProvider() 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: - class Private; - Private* const d; + void pageRequestFinished(KJob *job); + void imageRequestFinished(KJob *job); + + private: + QDate mActualDate; + QImage mImage; + + QXmlStreamReader xml; + + int mFailureNumber = 0; - Q_PRIVATE_SLOT( d, void pageRequestFinished( KJob* ) ) - Q_PRIVATE_SLOT( d, void imageRequestFinished( KJob* ) ) + QStringList m_photoList; }; #endif diff --git a/dataengines/potd/natgeoprovider.cpp b/dataengines/potd/natgeoprovider.cpp index 00d3022a1..601dfbad2 100644 --- a/dataengines/potd/natgeoprovider.cpp +++ b/dataengines/potd/natgeoprovider.cpp @@ -1,115 +1,91 @@ /* * Copyright 2007 Tobias Koenig * Copyright 2008 Anne-Marie Mahfouf * Copyright 2013 Aaron Seigo * * 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 "natgeoprovider.h" -#include -#include -#include #include +#include #include -class NatGeoProvider::Private + +NatGeoProvider::NatGeoProvider(QObject *parent, const QVariantList &args) + : PotdProvider(parent, args) { - public: - Private( NatGeoProvider *parent ) - : mParent( parent ) - { - } + const QUrl url(QStringLiteral("http://www.nationalgeographic.com/photography/photo-of-the-day/")); - void pageRequestFinished( KJob* ); - void imageRequestFinished( KJob* ); + KIO::StoredTransferJob *job = KIO::storedGet(url, KIO::NoReload, KIO::HideProgressInfo); + connect(job, &KIO::StoredTransferJob::finished, this, &NatGeoProvider::pageRequestFinished); +} - NatGeoProvider *mParent; - QImage mImage; +NatGeoProvider::~NatGeoProvider() = default; - QRegularExpression re; -}; +QImage NatGeoProvider::image() const +{ + return mImage; +} -void NatGeoProvider::Private::pageRequestFinished( KJob* _job ) +void NatGeoProvider::pageRequestFinished(KJob* _job) { KIO::StoredTransferJob *job = static_cast( _job ); if (job->error()) { - emit mParent->error( mParent ); + emit error(this); return; } const QString data = QString::fromUtf8( job->data() ); const QStringList lines = data.split(QLatin1Char('\n')); QString url; re.setPattern(QStringLiteral("^$")); for (int i = 0; i < lines.size(); i++) { QRegularExpressionMatch match = re.match(lines.at(i)); if (match.hasMatch()) { url = match.captured(1); } } if (url.isEmpty()) { - emit mParent->error( mParent ); + emit error(this); return; } KIO::StoredTransferJob *imageJob = KIO::storedGet( QUrl(url), KIO::NoReload, KIO::HideProgressInfo ); - mParent->connect( imageJob, &KIO::StoredTransferJob::finished, mParent, [this] (KJob *job) { - imageRequestFinished(job); - }); + connect(imageJob, &KIO::StoredTransferJob::finished, this, &NatGeoProvider::imageRequestFinished); } -void NatGeoProvider::Private::imageRequestFinished( KJob *_job ) +void NatGeoProvider::imageRequestFinished(KJob *_job) { KIO::StoredTransferJob *job = static_cast( _job ); if ( job->error() ) { - emit mParent->error( mParent ); + emit error(this); return; } mImage = QImage::fromData( job->data() ); - emit mParent->finished( mParent ); -} - -NatGeoProvider::NatGeoProvider( QObject *parent, const QVariantList &args ) - : PotdProvider( parent, args ), d( new Private( this ) ) -{ - const QUrl url( QLatin1String( "http://www.nationalgeographic.com/photography/photo-of-the-day/" ) ); - KIO::StoredTransferJob *job = KIO::storedGet( url, KIO::NoReload, KIO::HideProgressInfo ); - connect( job, &KIO::StoredTransferJob::finished, this, [this] (KJob *job) { - d->pageRequestFinished(job); - }); -} - -NatGeoProvider::~NatGeoProvider() -{ - delete d; -} - -QImage NatGeoProvider::image() const -{ - return d->mImage; + emit finished(this); } K_PLUGIN_FACTORY_WITH_JSON(NatGeoProviderFactory, "natgeoprovider.json", registerPlugin();) #include "natgeoprovider.moc" diff --git a/dataengines/potd/natgeoprovider.h b/dataengines/potd/natgeoprovider.h index 16d619cb3..60dbbe743 100644 --- a/dataengines/potd/natgeoprovider.h +++ b/dataengines/potd/natgeoprovider.h @@ -1,67 +1,77 @@ /* * Copyright 2007 Tobias Koenig * Copyright 2008 Anne-Marie Mahfouf * Copyright 2013 Aaron Seigo * * 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 NATGEOPROVIDER_H #define NATGEOPROVIDER_H #include "potdprovider.h" +// Qt +#include +#include + +class KJob; /** * This class provides the image for APOD * "Astronomy Picture Of the Day" * located at http://antwrp.gsfc.nasa.gov/apod. * Direct link to the picture of the day page is * http://antwrp.gsfc.nasa.gov/apod/apYYMMDD.html * where YY is the year last 2 digits, * MM is the month and DD the day, in 2 digits. */ class NatGeoProvider : public PotdProvider { Q_OBJECT public: /** * Creates a new APOD provider. * * @param date The date for which the image shall be fetched. * @param parent The parent object. */ NatGeoProvider( QObject *parent, const QVariantList &args ); /** * Destroys the APOD provider. */ ~NatGeoProvider() 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: - class Private; - Private* const d; + void pageRequestFinished(KJob *job); + void imageRequestFinished(KJob *job); + + private: + QImage mImage; + + QRegularExpression re; }; #endif diff --git a/dataengines/potd/noaaprovider.cpp b/dataengines/potd/noaaprovider.cpp index ef481e761..03321e253 100644 --- a/dataengines/potd/noaaprovider.cpp +++ b/dataengines/potd/noaaprovider.cpp @@ -1,112 +1,90 @@ /* * Copyright (C) 2007 Tobias Koenig * Copyright 2008 by Anne-Marie Mahfouf * * 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 "noaaprovider.h" -#include -#include #include #include -#include +#include +#include -class NOAAProvider::Private + +NOAAProvider::NOAAProvider(QObject *parent, const QVariantList &args) + : PotdProvider(parent, args) { - public: - Private( NOAAProvider *parent ) - : mParent( parent ) - { - } + const QUrl url(QStringLiteral("http://www.nnvl.noaa.gov/imageoftheday.php")); - void pageRequestFinished( KJob* ); - void imageRequestFinished( KJob* ); - void parsePage(); + KIO::StoredTransferJob *job = KIO::storedGet(url, KIO::NoReload, KIO::HideProgressInfo); + connect(job, &KIO::StoredTransferJob::finished, this, &NOAAProvider::pageRequestFinished); +} - NOAAProvider *mParent; - QImage mImage; -}; +NOAAProvider::~NOAAProvider() = default; -void NOAAProvider::Private::pageRequestFinished( KJob* _job ) +QImage NOAAProvider::image() const +{ + return mImage; +} + +void NOAAProvider::pageRequestFinished(KJob* _job) { KIO::StoredTransferJob *job = static_cast( _job ); if (job->error()) { - emit mParent->error( mParent ); + emit error(this); return; } const QString data = QString::fromUtf8( job->data() ); // Using regular expression could be fragile in such case, but the HTML // NOAA page itself is not a valid XML file and unfortunately it could // not be parsed successfully till the content we want. And we do not want // to use heavy weight QtWebkit. So we use QRegularExpression to capture // the wanted url here. QString url; QRegularExpression re(QStringLiteral("_curPic = (.*?)")); auto result = re.match(data); if (result.hasMatch()) { url = QLatin1String("http://www.nnvl.noaa.gov/") + result.captured(1); } if (url.isEmpty()) { - emit mParent->error( mParent ); + emit error(this); return; } KIO::StoredTransferJob *imageJob = KIO::storedGet( QUrl(url), KIO::NoReload, KIO::HideProgressInfo ); - mParent->connect( imageJob, &KIO::StoredTransferJob::finished, mParent, [this] (KJob* job) { - imageRequestFinished(job); - }); + connect(imageJob, &KIO::StoredTransferJob::finished, this, &NOAAProvider::imageRequestFinished); } -void NOAAProvider::Private::imageRequestFinished( KJob *_job ) +void NOAAProvider::imageRequestFinished(KJob *_job) { KIO::StoredTransferJob *job = static_cast( _job ); if ( job->error() ) { - emit mParent->error( mParent ); + emit error(this); return; } mImage = QImage::fromData( job->data() ); - emit mParent->finished( mParent ); -} - -NOAAProvider::NOAAProvider( QObject *parent, const QVariantList &args ) - : PotdProvider( parent, args ), d( new Private( this ) ) -{ - QUrl url( QLatin1String( "http://www.nnvl.noaa.gov/imageoftheday.php" ) ); - KIO::StoredTransferJob *job = KIO::storedGet( url, KIO::NoReload, KIO::HideProgressInfo ); - connect( job, &KIO::StoredTransferJob::finished, this, [this] (KJob *job) { - d->pageRequestFinished(job); - }); -} - -NOAAProvider::~NOAAProvider() -{ - delete d; -} - -QImage NOAAProvider::image() const -{ - return d->mImage; + emit finished(this); } K_PLUGIN_FACTORY_WITH_JSON(NOAAProviderFactory, "noaaprovider.json", registerPlugin();) #include "noaaprovider.moc" diff --git a/dataengines/potd/noaaprovider.h b/dataengines/potd/noaaprovider.h index d6091f2cc..3af063065 100644 --- a/dataengines/potd/noaaprovider.h +++ b/dataengines/potd/noaaprovider.h @@ -1,63 +1,70 @@ /* * Copyright (C) 2007 Tobias Koenig * Copyright 2008 by Anne-Marie Mahfouf * Copyright 2016 Weng Xuetian * * 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 NOAAPROVIDER_H #define NOAAPROVIDER_H #include "potdprovider.h" +// Qt +#include + +class KJob; /** * This class provides the image for NOAA Environmental Visualization Laboratory * Picture Of the Day * located at http://www.nnvl.noaa.gov/imageoftheday.php. */ class NOAAProvider : public PotdProvider { Q_OBJECT public: /** * Creates a new NOAA provider. * * @param date The date for which the image shall be fetched. * @param parent The parent object. */ NOAAProvider( QObject *parent, const QVariantList &args ); /** * Destroys the NOAA provider. */ ~NOAAProvider() 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: - class Private; - Private* const d; + void pageRequestFinished(KJob *job); + void imageRequestFinished(KJob *job); + + private: + QImage mImage; }; #endif diff --git a/dataengines/potd/potdprovider.cpp b/dataengines/potd/potdprovider.cpp index c7d83e7df..306d25e9f 100644 --- a/dataengines/potd/potdprovider.cpp +++ b/dataengines/potd/potdprovider.cpp @@ -1,72 +1,76 @@ /* * Copyright (C) 2007 Tobias Koenig * * 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 "potdprovider.h" +// Qt +#include + + class PotdProviderPrivate { public: QString name; QDate date; }; PotdProvider::PotdProvider( QObject *parent, const QVariantList &args ) : QObject( parent ), d(new PotdProviderPrivate) { if ( args.count() > 0 ) { d->name = args[ 0 ].toString(); if ( args.count() > 1 && args[ 1 ].canConvert( QVariant::Date ) ) { d->date = args[ 1 ].toDate(); } } else { d->name = QStringLiteral("Unknown"); } } PotdProvider::~PotdProvider() { } QString PotdProvider::name() const { return d->name; } QDate PotdProvider::date() const { return d->date.isNull() ? QDate::currentDate() : d->date; } bool PotdProvider::isFixedDate() const { return !d->date.isNull(); } QString PotdProvider::identifier() const { if (isFixedDate()) { return d->name + QLatin1Char(':') + d->date.toString(Qt::ISODate); } return d->name; } diff --git a/dataengines/potd/potdprovider.h b/dataengines/potd/potdprovider.h index 9e97ab209..18bd14cf6 100644 --- a/dataengines/potd/potdprovider.h +++ b/dataengines/potd/potdprovider.h @@ -1,100 +1,99 @@ /* * Copyright (C) 2007 Tobias Koenig * * 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 POTDPROVIDER_H #define POTDPROVIDER_H #include -#include - -#include +#include #include "plasma_potd_export.h" class QImage; +class QDate; /** * This class is an interface for PoTD providers. */ class PLASMA_POTD_EXPORT PotdProvider : public QObject { Q_OBJECT public: /** * Creates a new PoTD provider. * * @param parent The parent object. */ explicit PotdProvider(QObject *parent, const QVariantList &args = QVariantList()); /** * Destroys the PoTD provider. */ ~PotdProvider() override; /** * Returns the requested image. * * Note: This method returns only a valid image after the * finished() signal has been emitted. */ virtual QImage image() const = 0; /** * Returns the identifier of the PoTD request (name + date). */ virtual QString identifier() const; /** * @return the name of this provider (equiv to X-KDE-PlasmaPoTDProvider-Identifier) */ QString name() const; /** * @reutrn the date to load for this item, if any */ QDate date() const; /** * @return if the date is fixed, or if it should always be "today" */ bool isFixedDate() const; Q_SIGNALS: /** * This signal is emitted whenever a request has been finished * successfully. * * @param provider The provider which emitted the signal. */ void finished( PotdProvider *provider ); /** * This signal is emitted whenever an error has occurred. * * @param provider The provider which emitted the signal. */ void error( PotdProvider *provider ); private: const QScopedPointer d; }; #endif diff --git a/dataengines/potd/wcpotdprovider.cpp b/dataengines/potd/wcpotdprovider.cpp index 30983d227..c8c4c3df8 100644 --- a/dataengines/potd/wcpotdprovider.cpp +++ b/dataengines/potd/wcpotdprovider.cpp @@ -1,116 +1,98 @@ /* * Copyright (C) 2007 Tobias Koenig * Copyright 2008 by Anne-Marie Mahfouf * Copyright 2016 Weng Xuetian * * 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 "wcpotdprovider.h" #include #include #include #include #include -#include +#include +#include -class WcpotdProvider::Private + +WcpotdProvider::WcpotdProvider(QObject *parent, const QVariantList &args) + : PotdProvider(parent, args) { - public: - Private( WcpotdProvider *parent ) - : mParent( parent ) - { - } + QUrl url(QStringLiteral("https://commons.wikimedia.org/w/api.php")); + + QUrlQuery urlQuery(url); + urlQuery.addQueryItem(QStringLiteral("action"), QStringLiteral("parse")); + urlQuery.addQueryItem(QStringLiteral("text"), QStringLiteral("{{Potd}}")); + urlQuery.addQueryItem(QStringLiteral("contentmodel"), QStringLiteral("wikitext")); + urlQuery.addQueryItem(QStringLiteral("prop"), QStringLiteral("images")); + urlQuery.addQueryItem(QStringLiteral("format"), QStringLiteral("json")); + url.setQuery(urlQuery); - void pageRequestFinished( KJob* ); - void imageRequestFinished( KJob* ); - void parsePage(); + KIO::StoredTransferJob *job = KIO::storedGet(url, KIO::NoReload, KIO::HideProgressInfo); + connect(job, &KIO::StoredTransferJob::finished, this, &WcpotdProvider::pageRequestFinished); +} - WcpotdProvider *mParent; - QImage mImage; -}; +WcpotdProvider::~WcpotdProvider() = default; -void WcpotdProvider::Private::pageRequestFinished( KJob *_job ) +QImage WcpotdProvider::image() const +{ + return mImage; +} + +void WcpotdProvider::pageRequestFinished(KJob *_job) { KIO::StoredTransferJob *job = static_cast( _job ); if ( job->error() ) { - emit mParent->error( mParent ); + emit error(this); return; } auto jsonImageArray = QJsonDocument::fromJson(job->data()) .object().value(QLatin1String("parse")) .toObject().value(QLatin1String("images")) .toArray(); if (jsonImageArray.size() > 0) { const QString imageFile = jsonImageArray.at(0).toString(); if (!imageFile.isEmpty()) { const QUrl picUrl(QLatin1String("https://commons.wikimedia.org/wiki/Special:FilePath/") + imageFile); KIO::StoredTransferJob *imageJob = KIO::storedGet( picUrl, KIO::NoReload, KIO::HideProgressInfo ); - mParent->connect( imageJob, SIGNAL(finished(KJob*)), SLOT(imageRequestFinished(KJob*)) ); + connect(imageJob, &KIO::StoredTransferJob::finished, this, &WcpotdProvider::imageRequestFinished); return; } } - emit mParent->error(mParent); + emit error(this); } -void WcpotdProvider::Private::imageRequestFinished( KJob *_job ) +void WcpotdProvider::imageRequestFinished(KJob *_job) { KIO::StoredTransferJob *job = static_cast( _job ); if ( job->error() ) { - emit mParent->error( mParent ); + emit error(this); return; } QByteArray data = job->data(); mImage = QImage::fromData( data ); - emit mParent->finished( mParent ); -} - -WcpotdProvider::WcpotdProvider( QObject *parent, const QVariantList &args ) - : PotdProvider( parent, args ), d( new Private( this ) ) -{ - QUrl url(QStringLiteral("https://commons.wikimedia.org/w/api.php")); - - QUrlQuery urlQuery(url); - urlQuery.addQueryItem(QStringLiteral("action"), QStringLiteral("parse")); - urlQuery.addQueryItem(QStringLiteral("text"), QStringLiteral("{{Potd}}")); - urlQuery.addQueryItem(QStringLiteral("contentmodel"), QStringLiteral("wikitext")); - urlQuery.addQueryItem(QStringLiteral("prop"), QStringLiteral("images")); - urlQuery.addQueryItem(QStringLiteral("format"), QStringLiteral("json")); - url.setQuery(urlQuery); - - KIO::StoredTransferJob *job = KIO::storedGet( url, KIO::NoReload, KIO::HideProgressInfo ); - connect( job, SIGNAL(finished(KJob*)), SLOT(pageRequestFinished(KJob*)) ); -} - -WcpotdProvider::~WcpotdProvider() -{ - delete d; -} - -QImage WcpotdProvider::image() const -{ - return d->mImage; + emit finished(this); } K_PLUGIN_FACTORY_WITH_JSON(WcpotdProviderFactory, "wcpotdprovider.json", registerPlugin();) #include "wcpotdprovider.moc" -#include "moc_wcpotdprovider.cpp" diff --git a/dataengines/potd/wcpotdprovider.h b/dataengines/potd/wcpotdprovider.h index 894f3727c..ee4d66298 100644 --- a/dataengines/potd/wcpotdprovider.h +++ b/dataengines/potd/wcpotdprovider.h @@ -1,68 +1,72 @@ /* * Copyright (C) 2007 Tobias Koenig * Copyright 2008 by Anne-Marie Mahfouf * * 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 WCPOTDPROVIDER_H #define WCPOTDPROVIDER_H #include "potdprovider.h" +// Qt +#include + +class KJob; /** * This class provides the image for the "Wikimedia * Commons Picture Of the Day" * located at http://tools.wikimedia.de/~daniel/potd/commons/potd-800x600.html. * From there extract the picture. * Using 800x600 as the best size for now, others are available, see * http://tools.wikimedia.de/~daniel/potd/potd.php */ class WcpotdProvider : public PotdProvider { Q_OBJECT public: /** * Creates a new Wcpotd provider. * * @param date The date for which the image shall be fetched. * @param parent The parent object. */ WcpotdProvider( QObject *parent, const QVariantList &args ); /** * Destroys the Wcpotd provider. */ ~WcpotdProvider() 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: - class Private; - Private* const d; + void pageRequestFinished(KJob *job); + void imageRequestFinished(KJob *job); - Q_PRIVATE_SLOT( d, void pageRequestFinished( KJob* ) ) - Q_PRIVATE_SLOT( d, void imageRequestFinished( KJob* ) ) + private: + QImage mImage; }; #endif