Commit 4d798e1b authored by Friedrich W. H. Kossebau's avatar Friedrich W. H. Kossebau
Browse files

[potd dataengine] De-pimplify potd providers for more simple code

Summary:
There is no need to use pimpl for non-public-API classes.

While touching the code, also change to use consistently
modern signal-slot connections.

Test Plan: All plugins still build, load & work as before.

Reviewers: xuetianweng

Subscribers: plasma-devel

Tags: #plasma

Differential Revision: https://phabricator.kde.org/D11957
parent 13bd9739
......@@ -21,32 +21,32 @@
#include "apodprovider.h"
#include <QRegExp>
#include <QImage>
#include <QDebug>
#include <kio/job.h>
#include <KPluginFactory>
#include <KIO/Job>
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<KIO::StoredTransferJob *>( _job );
if ( job->error() ) {
emit mParent->error( mParent );
emit error(this);
return;
}
......@@ -59,43 +59,24 @@ void ApodProvider::Private::pageRequestFinished( KJob *_job )
const QString sub = exp.cap(1);
const QUrl url(QLatin1String("http://antwrp.gsfc.nasa.gov/apod/") + sub);
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, &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<KIO::StoredTransferJob *>( _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<ApodProvider>();)
#include "moc_apodprovider.cpp"
#include "apodprovider.moc"
......@@ -23,6 +23,10 @@
#include "potdprovider.h"
#include <QImage>
class KJob;
/**
* This class provides the image for APOD
* "Astronomy Picture Of the Day"
......@@ -59,11 +63,11 @@ class ApodProvider : public PotdProvider
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
......@@ -19,35 +19,34 @@
#include "bingprovider.h"
#include <QRegExp>
#include <QImage>
#include <QJsonDocument>
#include <QJsonArray>
#include <QDebug>
#include <kio/job.h>
#include <KPluginFactory>
#include <KIO/Job>
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<KIO::StoredTransferJob*>(_job);
if (job->error()) {
emit mParent->error(mParent);
emit error(this);
return;
}
......@@ -70,47 +69,26 @@ void BingProvider::Private::pageRequestFinished(KJob* _job)
}
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<KIO::StoredTransferJob*>(_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<BingProvider>();)
#include "bingprovider.moc"
#include "moc_bingprovider.cpp"
......@@ -21,6 +21,10 @@
#define BINGPROVIDER_H
#include "potdprovider.h"
// Qt
#include <QImage>
class KJob;
/**
* This class provides the image for the Bing's homepage
......@@ -53,12 +57,11 @@ class BingProvider : public PotdProvider
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
......@@ -25,7 +25,7 @@
#include <QThreadPool>
#include <QStandardPaths>
#include <QDir>
#include <QImage>
#include <QDateTime>
#include <QDebug>
......
......@@ -21,32 +21,32 @@
#include "epodprovider.h"
#include <QRegExp>
#include <QImage>
#include <QDebug>
#include <kio/job.h>
#include <KPluginFactory>
#include <KIO/Job>
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<KIO::StoredTransferJob *>(_job);
if ( job->error() ) {
emit mParent->error( mParent );
emit error(this);
return;
}
......@@ -60,42 +60,22 @@ void EpodProvider::Private::pageRequestFinished(KJob *_job)
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<KIO::StoredTransferJob *>(_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<EpodProvider>();)
#include "moc_epodprovider.cpp"
#include "epodprovider.moc"
......@@ -22,6 +22,10 @@
#define EPODPROVIDER_H
#include "potdprovider.h"
// Qt
#include <QImage>
class KJob;
/**
* This class provides the image for EPOD
......@@ -55,11 +59,11 @@ class EpodProvider : public PotdProvider
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
......@@ -21,59 +21,54 @@
#include "flickrprovider.h"
#include <QRegExp>
#include <QImage>
#include <QXmlStreamReader>
#include <QUrlQuery>
#include <QDebug>
#include <kio/job.h>
#include <KPluginFactory>
#include <KIO/Job>
#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<KIO::StoredTransferJob *>( _job );
if (job->error()) {
emit mParent->error( mParent );
emit error(this);
qDebug() << "pageRequestFinished error";
return;
}
......@@ -100,11 +95,11 @@ void FlickrProvider::Private::pageRequestFinished( KJob *_job )
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;
}
......@@ -149,45 +144,24 @@ void FlickrProvider::Private::pageRequestFinished( KJob *_job )
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<KIO::StoredTransferJob *>( _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<FlickrProvider>();)
#include "moc_flickrprovider.cpp"
#include "flickrprovider.moc"
......@@ -23,7 +23,12 @@
#define FLICKRPROVIDER_H
#include "potdprovider.h"
#include <kio/job.h>
// Qt
#include <QImage>
#include <QDate>
#include <QXmlStreamReader>
class KJob;
/**
* This class grabs a random image from the flickr
......@@ -58,11 +63,18 @@ class FlickrProvider : public PotdProvider
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
......@@ -21,35 +21,33 @@
#include "natgeoprovider.h"
#include <QRegExp>
#include <QImage>
#include <QRegularExpression>
#include <QDebug>
#include <KPluginFactory>