[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [kdeplasma-addons] /: Remove Webkit usage in potd data engine.
From:       Weng Xuetian <wengxt () gmail ! com>
Date:       2016-11-16 17:14:12
Message-ID: E1c73mm-0006u3-DK () code ! kde ! org
[Download RAW message or body]

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 parsing
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/8a8522917f6ab028e90d478f25dc003a05832a6f

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 Quick Qml \
Widgets X11Extras)  
 find_package(KF5 ${KF5_MIN_VERSION} REQUIRED COMPONENTS
-    Activities Config ConfigWidgets CoreAddons I18n KIO KCMUtils Plasma Runner \
Service UnitConversion KDELibs4Support NewStuff Kross WebKit) +    Activities Config \
ConfigWidgets CoreAddons I18n KIO KCMUtils Plasma Runner 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 \
KF5::KIOCore KF5::WebKit) +target_link_libraries( plasma_potd_natgeoprovider \
plasmapotdprovidercore KF5::KIOCore)  
 kcoreaddons_desktop_to_json(plasma_potd_natgeoprovider natgeoprovider.desktop \
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/natgeoprovider.cpp index 090cad2..d156440 100644
--- a/dataengines/potd/natgeoprovider.cpp
+++ b/dataengines/potd/natgeoprovider.cpp
@@ -23,59 +23,63 @@
 
 #include <QtCore/QRegExp>
 #include <QtGui/QImage>
+#include <QXmlStreamReader>
 
 #include <QDebug>
 #include <KIO/Job>
 
-#include <KWebPage>
-#include <QWebFrame>
-#include <QWebElement>
-#include <QWebElementCollection>
-
 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 = static_cast<KIO::StoredTransferJob *>( _job );
+    if (job->error()) {
         emit mParent->error( mParent );
         return;
     }
 
-    QWebElementCollection links = mPage->mainFrame()->findAllElements( \
                QLatin1String( "meta" ) );
-    if ( links.count() < 1 ) {
-        emit mParent->error( mParent );
-        return;
-    }
+    const QString data = QString::fromUtf8( job->data() );
+
+    mXmlReader.clear();
+    mXmlReader.addData(data);
 
     QString url;
-    for (int i = 0, e = links.count(); i < e; i++) {
-        if (links.at(i).attribute(QLatin1String("property")) == "og:image") {
-            url = links.at(i).attribute(QLatin1String("content"));
-            break;
+    while (!mXmlReader.atEnd()) {
+        mXmlReader.readNext();
+
+        if (mXmlReader.isStartElement() && mXmlReader.name() == QLatin1String( \
"meta" )) { +            const auto attrs = mXmlReader.attributes();
+            if (attrs.value(QLatin1String("property")).toString() == \
QLatin1String("og:image")) { +                url = \
attrs.value(QLatin1String("content")).toString(); +                break;
+            }
         }
     }
+
     if (url.isEmpty()) {
         emit mParent->error( mParent );
         return;
     }
 
     KIO::StoredTransferJob *imageJob = KIO::storedGet( QUrl(url), KIO::NoReload, \
                KIO::HideProgressInfo );
-    mParent->connect( imageJob, SIGNAL(finished(KJob*)), \
SLOT(imageRequestFinished(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 \
QVariantList &args )  : PotdProvider( parent, args ), d( new Private( this ) )
 {
     const QUrl url( QLatin1String( \
                "https://www.nationalgeographic.com/photography/photo-of-the-day" ) \
                );
-    connect( d->mPage, SIGNAL(loadFinished(bool)), this, \
                SLOT(pageRequestFinished(bool)) );
-    d->mPage->mainFrame()->setUrl( url );
+    KIO::StoredTransferJob *job = KIO::storedGet( url, KIO::NoReload, \
KIO::HideProgressInfo ); +    connect( job, &KIO::StoredTransferJob::finished, this, \
[this] (KJob *job) { +        d->pageRequestFinished(job);
+    });
 }
 
 NatGeoProvider::~NatGeoProvider()
@@ -111,4 +117,3 @@ QImage NatGeoProvider::image() const
 K_PLUGIN_FACTORY_WITH_JSON(NatGeoProviderFactory, "natgeoprovider.json", \
registerPlugin<NatGeoProvider>();)  
 #include "natgeoprovider.moc"
-#include "moc_natgeoprovider.moc"
diff --git a/dataengines/potd/natgeoprovider.h b/dataengines/potd/natgeoprovider.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/noaaprovider.cpp
index 3886695..cacd485 100644
--- a/dataengines/potd/noaaprovider.cpp
+++ b/dataengines/potd/noaaprovider.cpp
@@ -22,51 +22,47 @@
 
 #include <QtCore/QRegExp>
 #include <QtGui/QImage>
+#include <QRegularExpression>
 
 #include <QDebug>
 #include <kio/job.h>
-#include <KWebPage>
-#include <QWebFrame>
-#include <QWebElementCollection>
 
 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 = mPage->mainFrame()->findAllElements( \
                QLatin1String( "script" ) );
-    if ( links.count() < 1 ) {
+    KIO::StoredTransferJob *job = static_cast<KIO::StoredTransferJob *>( _job );
+    if (job->error()) {
         emit mParent->error( mParent );
         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;
-    for (int i = 0, e = links.count(); i < e; i++) {
-        const auto text = links.at(i).toPlainText();
-        if (text.startsWith("_curPic = ")) {
-            url = "http://www.nnvl.noaa.gov/" + text.mid(10);
-            break;
-        }
+    QRegularExpression re("_curPic = (.*?)</script>");
+    auto result = re.match(data);
+    if (result.hasMatch()) {
+        url = QString("http://www.nnvl.noaa.gov/").append(result.captured(1));
     }
     if (url.isEmpty()) {
         emit mParent->error( mParent );
@@ -74,7 +70,9 @@ void NOAAProvider::Private::pageRequestFinished( bool ok )
     }
 
     KIO::StoredTransferJob *imageJob = KIO::storedGet( QUrl(url), KIO::NoReload, \
                KIO::HideProgressInfo );
-    mParent->connect( imageJob, SIGNAL(finished(KJob*)), \
SLOT(imageRequestFinished(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 QVariantList \
&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(pageRequestFinished(bool)) );
-    d->mPage->mainFrame()->setUrl( url );
+    KIO::StoredTransferJob *job = KIO::storedGet( url, KIO::NoReload, \
KIO::HideProgressInfo ); +    connect( job, &KIO::StoredTransferJob::finished, this, \
[this] (KJob *job) { +        d->pageRequestFinished(job);
+    });
 }
 
 NOAAProvider::~NOAAProvider()
@@ -109,5 +109,4 @@ QImage NOAAProvider::image() const
 
 K_PLUGIN_FACTORY_WITH_JSON(NOAAProviderFactory, "noaaprovider.json", \
registerPlugin<NOAAProvider>();)  
-#include "moc_noaaprovider.cpp"
 #include "noaaprovider.moc"
diff --git a/dataengines/potd/noaaprovider.h b/dataengines/potd/noaaprovider.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


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic