From kde-commits Wed Nov 16 17:14:12 2016 From: Weng Xuetian Date: Wed, 16 Nov 2016 17:14:12 +0000 To: kde-commits Subject: [kdeplasma-addons] /: Remove Webkit usage in potd data engine. Message-Id: X-MARC-Message: https://marc.info/?l=kde-commits&m=147931646124509 Git commit 8a8522917f6ab028e90d478f25dc003a05832a6f by Weng Xuetian. Committed on 16/11/2016 at 17:14. Pushed by xuetianweng into branch 'master'. Remove Webkit usage in potd data engine. Summary: For natgeoprovider, since the page it can be accepted by QXmlStreamReader to get required content, we simply parse it as XML though HTML is NOT necessarily valid XML. For noaaprovider, QXmlStreamReader will hit an error in the middle of parsi= ng so we use regular expression to extract the url. Test Plan: Manually test with plasmashell Reviewers: mart, hein, broulik Reviewed By: broulik Subscribers: #plasma, plasma-devel Tags: #plasma Differential Revision: https://phabricator.kde.org/D3372 M +1 -1 CMakeLists.txt M +2 -2 dataengines/potd/CMakeLists.txt M +29 -24 dataengines/potd/natgeoprovider.cpp M +0 -3 dataengines/potd/natgeoprovider.h M +24 -25 dataengines/potd/noaaprovider.cpp M +0 -3 dataengines/potd/noaaprovider.h http://commits.kde.org/kdeplasma-addons/8a8522917f6ab028e90d478f25dc003a058= 32a6f diff --git a/CMakeLists.txt b/CMakeLists.txt index 8d8a555..60c743f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,7 +23,7 @@ set(KF5_MIN_VERSION 5.8.0) find_package(Qt5 ${REQUIRED_QT_VERSION} CONFIG REQUIRED Core Gui DBus Quic= k Qml Widgets X11Extras) = find_package(KF5 ${KF5_MIN_VERSION} REQUIRED COMPONENTS - Activities Config ConfigWidgets CoreAddons I18n KIO KCMUtils Plasma Ru= nner Service UnitConversion KDELibs4Support NewStuff Kross WebKit) + Activities Config ConfigWidgets CoreAddons I18n KIO KCMUtils Plasma Ru= nner Service UnitConversion KDELibs4Support NewStuff Kross) = find_package(LibTaskManager CONFIG REQUIRED) = diff --git a/dataengines/potd/CMakeLists.txt b/dataengines/potd/CMakeLists.= txt index b13db87..e8163e5 100644 --- a/dataengines/potd/CMakeLists.txt +++ b/dataengines/potd/CMakeLists.txt @@ -58,7 +58,7 @@ set( potd_natgeo_provider_SRCS ) = add_library( plasma_potd_natgeoprovider MODULE ${potd_natgeo_provider_SRCS= } ) -target_link_libraries( plasma_potd_natgeoprovider plasmapotdprovidercore K= F5::KIOCore KF5::WebKit) +target_link_libraries( plasma_potd_natgeoprovider plasmapotdprovidercore K= F5::KIOCore) = kcoreaddons_desktop_to_json(plasma_potd_natgeoprovider natgeoprovider.desk= top SERVICE_TYPES ${CMAKE_CURRENT_SOURCE_DIR}/plasma_potdprovider.desktop) = @@ -80,7 +80,7 @@ set( potd_noaa_provider_SRCS ) = add_library( plasma_potd_noaaprovider MODULE ${potd_noaa_provider_SRCS} ) -target_link_libraries( plasma_potd_noaaprovider plasmapotdprovidercore KF5= ::KIOCore KF5::WebKit) +target_link_libraries( plasma_potd_noaaprovider plasmapotdprovidercore KF5= ::KIOCore) = kcoreaddons_desktop_to_json(plasma_potd_noaaprovider noaaprovider.desktop = SERVICE_TYPES ${CMAKE_CURRENT_SOURCE_DIR}/plasma_potdprovider.desktop) = diff --git a/dataengines/potd/natgeoprovider.cpp b/dataengines/potd/natgeop= rovider.cpp index 090cad2..d156440 100644 --- a/dataengines/potd/natgeoprovider.cpp +++ b/dataengines/potd/natgeoprovider.cpp @@ -23,59 +23,63 @@ = #include #include +#include = #include #include = -#include -#include -#include -#include - class NatGeoProvider::Private { public: Private( NatGeoProvider *parent ) - : mParent( parent ), - mPage( new KWebPage( parent, KWebPage::KIOIntegration ) ) + : mParent( parent ) { } = - void pageRequestFinished( bool ok ); + void pageRequestFinished( KJob* ); void imageRequestFinished( KJob* ); = NatGeoProvider *mParent; QImage mImage; - KWebPage *mPage; + + QXmlStreamReader mXmlReader; }; = -void NatGeoProvider::Private::pageRequestFinished( bool ok ) +void NatGeoProvider::Private::pageRequestFinished( KJob* _job ) { - if ( !ok ) { + KIO::StoredTransferJob *job =3D static_cast(= _job ); + if (job->error()) { emit mParent->error( mParent ); return; } = - QWebElementCollection links =3D mPage->mainFrame()->findAllElements( Q= Latin1String( "meta" ) ); - if ( links.count() < 1 ) { - emit mParent->error( mParent ); - return; - } + const QString data =3D QString::fromUtf8( job->data() ); + + mXmlReader.clear(); + mXmlReader.addData(data); = QString url; - for (int i =3D 0, e =3D links.count(); i < e; i++) { - if (links.at(i).attribute(QLatin1String("property")) =3D=3D "og:im= age") { - url =3D links.at(i).attribute(QLatin1String("content")); - break; + while (!mXmlReader.atEnd()) { + mXmlReader.readNext(); + + if (mXmlReader.isStartElement() && mXmlReader.name() =3D=3D QLatin= 1String( "meta" )) { + const auto attrs =3D mXmlReader.attributes(); + if (attrs.value(QLatin1String("property")).toString() =3D=3D Q= Latin1String("og:image")) { + url =3D attrs.value(QLatin1String("content")).toString(); + break; + } } } + if (url.isEmpty()) { emit mParent->error( mParent ); return; } = KIO::StoredTransferJob *imageJob =3D KIO::storedGet( QUrl(url), KIO::N= oReload, KIO::HideProgressInfo ); - mParent->connect( imageJob, SIGNAL(finished(KJob*)), SLOT(imageRequest= Finished(KJob*)) ); + mParent->connect( imageJob, &KIO::StoredTransferJob::finished, mParent= , [this] (KJob *job) { + imageRequestFinished(job); + }); } = void NatGeoProvider::Private::imageRequestFinished( KJob *_job ) @@ -94,8 +98,10 @@ NatGeoProvider::NatGeoProvider( QObject *parent, const Q= VariantList &args ) : PotdProvider( parent, args ), d( new Private( this ) ) { const QUrl url( QLatin1String( "https://www.nationalgeographic.com/pho= tography/photo-of-the-day" ) ); - connect( d->mPage, SIGNAL(loadFinished(bool)), this, SLOT(pageRequestF= inished(bool)) ); - d->mPage->mainFrame()->setUrl( url ); + KIO::StoredTransferJob *job =3D KIO::storedGet( url, KIO::NoReload, KI= O::HideProgressInfo ); + connect( job, &KIO::StoredTransferJob::finished, this, [this] (KJob *j= ob) { + d->pageRequestFinished(job); + }); } = NatGeoProvider::~NatGeoProvider() @@ -111,4 +117,3 @@ QImage NatGeoProvider::image() const K_PLUGIN_FACTORY_WITH_JSON(NatGeoProviderFactory, "natgeoprovider.json", r= egisterPlugin();) = #include "natgeoprovider.moc" -#include "moc_natgeoprovider.moc" diff --git a/dataengines/potd/natgeoprovider.h b/dataengines/potd/natgeopro= vider.h index 4ce839b..dd32dde 100644 --- a/dataengines/potd/natgeoprovider.h +++ b/dataengines/potd/natgeoprovider.h @@ -62,9 +62,6 @@ class NatGeoProvider : public PotdProvider private: class Private; Private* const d; - - Q_PRIVATE_SLOT( d, void pageRequestFinished( bool ) ) - Q_PRIVATE_SLOT( d, void imageRequestFinished( KJob* ) ) }; = #endif diff --git a/dataengines/potd/noaaprovider.cpp b/dataengines/potd/noaaprovi= der.cpp index 3886695..cacd485 100644 --- a/dataengines/potd/noaaprovider.cpp +++ b/dataengines/potd/noaaprovider.cpp @@ -22,51 +22,47 @@ = #include #include +#include = #include #include -#include -#include -#include = class NOAAProvider::Private { public: Private( NOAAProvider *parent ) - : mParent( parent ), - mPage( new KWebPage( parent, KWebPage::KIOIntegration ) ) + : mParent( parent ) { } = - void pageRequestFinished( bool ); + void pageRequestFinished( KJob* ); void imageRequestFinished( KJob* ); void parsePage(); = NOAAProvider *mParent; QImage mImage; - KWebPage *mPage; }; = -void NOAAProvider::Private::pageRequestFinished( bool ok ) +void NOAAProvider::Private::pageRequestFinished( KJob* _job ) { - if ( !ok ) { - emit mParent->error( mParent ); - return; - } - - QWebElementCollection links =3D mPage->mainFrame()->findAllElements( Q= Latin1String( "script" ) ); - if ( links.count() < 1 ) { + KIO::StoredTransferJob *job =3D static_cast(= _job ); + if (job->error()) { emit mParent->error( mParent ); return; } = + const QString data =3D 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 captu= re + // the wanted url here. QString url; - for (int i =3D 0, e =3D links.count(); i < e; i++) { - const auto text =3D links.at(i).toPlainText(); - if (text.startsWith("_curPic =3D ")) { - url =3D "http://www.nnvl.noaa.gov/" + text.mid(10); - break; - } + QRegularExpression re("_curPic =3D (.*?)"); + auto result =3D re.match(data); + if (result.hasMatch()) { + url =3D QString("http://www.nnvl.noaa.gov/").append(result.capture= d(1)); } if (url.isEmpty()) { emit mParent->error( mParent ); @@ -74,7 +70,9 @@ void NOAAProvider::Private::pageRequestFinished( bool ok ) } = KIO::StoredTransferJob *imageJob =3D KIO::storedGet( QUrl(url), KIO::N= oReload, KIO::HideProgressInfo ); - mParent->connect( imageJob, SIGNAL(finished(KJob*)), SLOT(imageRequest= Finished(KJob*)) ); + mParent->connect( imageJob, &KIO::StoredTransferJob::finished, mParent= , [this] (KJob* job) { + imageRequestFinished(job); + }); } = void NOAAProvider::Private::imageRequestFinished( KJob *_job ) @@ -93,8 +91,10 @@ NOAAProvider::NOAAProvider( QObject *parent, const QVari= antList &args ) : PotdProvider( parent, args ), d( new Private( this ) ) { QUrl url( QLatin1String( "http://www.nnvl.noaa.gov/imageoftheday.php" = ) ); - connect( d->mPage, SIGNAL(loadFinished(bool)), this, SLOT(pageRequestF= inished(bool)) ); - d->mPage->mainFrame()->setUrl( url ); + KIO::StoredTransferJob *job =3D KIO::storedGet( url, KIO::NoReload, KI= O::HideProgressInfo ); + connect( job, &KIO::StoredTransferJob::finished, this, [this] (KJob *j= ob) { + d->pageRequestFinished(job); + }); } = NOAAProvider::~NOAAProvider() @@ -109,5 +109,4 @@ QImage NOAAProvider::image() const = K_PLUGIN_FACTORY_WITH_JSON(NOAAProviderFactory, "noaaprovider.json", regis= terPlugin();) = -#include "moc_noaaprovider.cpp" #include "noaaprovider.moc" diff --git a/dataengines/potd/noaaprovider.h b/dataengines/potd/noaaprovide= r.h index 23e19b9..5e2e407 100644 --- a/dataengines/potd/noaaprovider.h +++ b/dataengines/potd/noaaprovider.h @@ -58,9 +58,6 @@ class NOAAProvider : public PotdProvider private: class Private; Private* const d; - - Q_PRIVATE_SLOT( d, void pageRequestFinished( bool ) ) - Q_PRIVATE_SLOT( d, void imageRequestFinished( KJob* ) ) }; = #endif