Commit a37b285a authored by Ralf Habacker's avatar Ralf Habacker
Browse files

Added support for using QtNetwork to download online quotes

To enable the mentioned support, a new cmake variable
BUILD_WITH_QTNETWORK has been added.

When the mentioned cmake variable is set to 1 or ON, which is
default on Windows, AlkOnlineQuotes::launchNative() uses Qt
network support for downloading data. If it is set to 0 or
OFF, the KIO library support is used, which is the default on
non Windows platforms.

The advantage of using Qt network support is less dependency
on 3rd party components, thus less error-proneness and
higher speed on Windows.

BUG:452122
FIXED-IN:8.1.1
parent 97eceaee
......@@ -9,6 +9,7 @@ project(alkimia VERSION 8.1.0)
option(BUILD_QT4 "Build for Qt4" OFF)
option(BUILD_DOXYGEN_DOCS "Build api docs" ON)
option(BUILD_TOOLS "Build tools" ON)
option(BUILD_WITH_QTNETWORK "Build with Qt Network library for downloading" WIN32)
option(BUILD_WITH_WEBKIT "Build with Qt Web Kit library" OFF)
option(BUILD_SHARED_LIBS "Build shared libraries" ON)
......@@ -57,11 +58,22 @@ if(BUILD_QT4)
else()
feature_notes(FALSE TRUE FALSE FALSE)
endif()
find_package(Qt4 REQUIRED COMPONENTS QtCore QtGui QtDBus QtTest ${QT_BROWSER_COMPONENT} QtDeclarative)
if(BUILD_WITH_QTNETWORK)
add_definitions(-DBUILD_WITH_QTNETWORK)
set(QT_NETWORK_COMPONENT QtNetwork)
set(QT_NETWORK_LIB ${QT_USE_LIBSPREFIX}Network)
else()
set(QT_NETWORK_COMPONENT)
endif()
find_package(Qt4 REQUIRED COMPONENTS QtCore QtGui QtDBus ${QT_NETWORK_COMPONENT} QtTest ${QT_BROWSER_COMPONENT} QtDeclarative)
set(_kde4_uninstall_rule_created 1)
find_package(KDE4 REQUIRED)
if(NOT BUILD_WITH_QTNETWORK)
set(KDE_NETWORK_LIBS ${KDE4_KIO_LIBS})
endif()
include(KDE4Defaults)
set(KDE_LIBRARIES ${KDE4_KDECORE_LIBS} ${KDE4_KIO_LIBS})
include_directories(${KDE4_INCLUDES})
macro(ki18n_wrap_ui)
kde4_add_ui_files(${ARGN})
......@@ -114,11 +126,22 @@ else()
set(QT_BROWSER_COMPONENT Widgets)
feature_notes(FALSE TRUE FALSE FALSE)
endif()
set(QT_BROWSER_LIB ${QT_USE_LIBSPREFIX}${QT_BROWSER_COMPONENT})
if(BUILD_WITH_QTNETWORK)
add_definitions(-DBUILD_WITH_QTNETWORK)
set(QT_NETWORK_COMPONENT Network)
set(KDE_NETWORK_COMPONENT)
set(KDE_NETWORK_LIBS)
else()
set(QT_NETWORK_COMPONENT)
set(KDE_NETWORK_COMPONENT KIO)
set(KDE_NETWORK_LIBS KF5::KIOCore KF5::KIOWidgets)
endif()
set(QT_NETWORK_LIB ${QT_USE_LIBSPREFIX}${QT_NETWORK_COMPONENT})
find_package(Qt5 REQUIRED COMPONENTS
Core
${QT_NETWORK_COMPONENT}
Test
${QT_BROWSER_COMPONENT}
Qml
......@@ -136,7 +159,7 @@ else()
I18n
${PLASMA_COMPONENT}
NewStuff
KIO
${KDE_NETWORK_COMPONENT}
IconThemes
TextWidgets
XmlGui
......
......@@ -19,6 +19,7 @@ Required development components to build Alkimia with Qt4 and KDELibs4
Required development components to build Alkimia5 with Qt5 and KF5
Qt5::Core
Qt5::Network (optional -DBUILD_WITH_QTNETWORK=1)
Qt5::Test
Qt5::WebKitWidgets
Qt5::WebEngineWidgets (optional, experimental)
......@@ -31,7 +32,7 @@ Required development components to build Alkimia5 with Qt5 and KF5
KF5::Package (optional)
KF5::Plasma (optional)
KF5::NewStuff
KF5::KIO
KF5::KIO (optional -DBUILD_WITH_QTNETWORK=0)
KF5::IconThemes
KF5::TextWidgets
KF5::XmlGui
......@@ -67,7 +68,7 @@ Packages
kcompletion-devel
ki18n-devel
kiconthemes-devel
kio-devel
kio-devel (optional)
knewstuff-devel
kpackage-devel (optional)
ktextwidgets-devel
......@@ -84,7 +85,7 @@ Packages
kde/frameworks/.../kcompletion
kde/frameworks/.../ki18n
kde/frameworks/.../kiconthemes
kde/frameworks/.../kio
kde/frameworks/.../kio (optional)
kde/frameworks/.../knewstuff
kde/frameworks/.../kpackage (optional)
kde/frameworks/.../ktextwidgets
......
......@@ -9,7 +9,7 @@ set(TEST_LIB
)
if(BUILD_QT4)
list(APPEND TEST_LIB ${KDE_LIBRARIES})
list(APPEND TEST_LIB ${KDE4_KDECORE_LIBS})
endif()
ecm_add_tests(
......
......@@ -109,9 +109,11 @@ kde_target_enable_exceptions(alkimia PUBLIC)
if(BUILD_QT4)
set(ALKIMIA_LIBS
PRIVATE
${KDE_LIBRARIES}
${QT_NETWORK_LIB}
${KDE4_KDECORE_LIBS}
${KDE4_KDEUI_LIBS}
${KDE4_KNEWSTUFF3_LIBS}
${KDE_NETWORK_LIBS}
)
else()
set(ALKIMIA_LIBS
......@@ -119,11 +121,10 @@ else()
${QT5_OPTIONAL_COMPONENTS}
KF5::Completion
KF5::I18n
${KDE_NETWORK_LIBS}
KF5::NewStuff
KF5::IconThemes
KF5::TextWidgets
KF5::KIOCore
KF5::KIOWidgets
)
endif()
......
......@@ -5,13 +5,16 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")
include(CMakeFindDependencyMacro)
if("@BUILD_QT4@" STREQUAL "ON" OR "@BUILD_QT4@" STREQUAL "1")
find_package(Qt4 COMPONENTS QtCore QtDBus)
find_package(Qt4 COMPONENTS QtCore QtDBus @QT_NETWORK_LIB@)
else()
find_dependency(Qt5Core)
find_dependency(Qt5DBus)
if (@QT_BROWSER_LIB@)
find_dependency(@QT_BROWSER_LIB@)
endif()
if (@QT_NETWORK_LIB@)
find_dependency(@QT_NETWORK_LIB@)
endif()
endif()
find_dependency(@MP_CMAKE_MODULE@)
......@@ -21,7 +24,9 @@ if (NOT @BUILD_SHARED_LIBS@)
find_dependency(KF5NewStuff)
find_dependency(KF5IconThemes)
find_dependency(KF5TextWidgets)
find_dependency(KF5KIO)
if(NOT @BUILD_WITH_QTNETWORK@)
find_dependency(KF5KIO)
endif()
endif()
include("${CMAKE_CURRENT_LIST_DIR}/LibAlkimia@ALKIMIA_PATH_SUFFIX@Targets.cmake")
......
......@@ -29,6 +29,7 @@
#include "alkonlinequotesprofile.h"
#include "alkonlinequotesprofilemanager.h"
#include "alkonlinequotesource.h"
#include "alkimia/alkversion.h"
#include "alkwebpage.h"
#include <QApplication>
......@@ -38,10 +39,20 @@
#include <QTextStream>
#include <QTextCodec>
#ifdef BUILD_WITH_QTNETWORK
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QNetworkProxyFactory>
#else
#include <KIO/Scheduler>
#endif
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
#include <KLocalizedString>
#include <KIO/Scheduler>
#ifndef BUILD_WITH_QTNETWORK
#include <KIO/Job>
#endif
#include <QDebug>
#include <QTemporaryFile>
#define kDebug(a) qDebug()
......@@ -53,8 +64,9 @@
#include <KGlobal>
#include <KLocale>
#include <KUrl>
#ifndef BUILD_WITH_QTNETWORK
#include <kio/netaccess.h>
#include <kio/scheduler.h>
#endif
#endif
#include <KConfigGroup>
......@@ -142,6 +154,7 @@ public:
bool parseDate(const QString &datestr);
bool downloadUrl(const KUrl& url);
bool processDownloadedFile(const KUrl& url, const QString& tmpFile);
bool processDownloadedPage(const KUrl &url, const QByteArray &page);
public slots:
void slotLoadStarted();
......@@ -150,7 +163,11 @@ public slots:
bool slotParseQuote(const QString &_quotedata);
private slots:
#ifndef BUILD_WITH_QTNETWORK
void downloadUrlDone(KJob* job);
#else
void downloadUrlDone(QNetworkReply *reply);
#endif
};
bool AlkOnlineQuote::Private::initLaunch(const QString &_symbol, const QString &_id, const QString &_source)
......@@ -338,6 +355,19 @@ bool AlkOnlineQuote::Private::processDownloadedFile(const KUrl& url, const QStri
if (f.open(QIODevice::ReadOnly)) {
// Find out the page encoding and convert it to unicode
QByteArray page = f.readAll();
result = processDownloadedPage(url, page);
f.close();
} else {
emit m_p->error(i18n("Failed to open downloaded file"));
m_errors |= Errors::URL;
result = slotParseQuote(QString());
}
return result;
}
bool AlkOnlineQuote::Private::processDownloadedPage(const KUrl& url, const QByteArray& page)
{
bool result = false;
KEncodingProber prober(KEncodingProber::Universal);
prober.feed(page);
QTextCodec *codec = QTextCodec::codecForName(prober.encoding());
......@@ -345,20 +375,16 @@ bool AlkOnlineQuote::Private::processDownloadedFile(const KUrl& url, const QStri
codec = QTextCodec::codecForLocale();
}
QString quote = codec->toUnicode(page);
f.close();
emit m_p->status(i18n("URL found: %1...", url.prettyUrl()));
if (AlkOnlineQuotesProfileManager::instance().webPageEnabled())
AlkOnlineQuotesProfileManager::instance().webPage()->setContent(quote.toLocal8Bit());
result = slotParseQuote(quote);
} else {
emit m_p->error(i18n("Failed to open downloaded file"));
m_errors |= Errors::URL;
result = slotParseQuote(QString());
}
return result;
return result;
}
#ifndef BUILD_WITH_QTNETWORK
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
bool AlkOnlineQuote::Private::downloadUrl(const QUrl& url)
{
// Create a temporary filename (w/o leaving the file on the filesystem)
......@@ -397,7 +423,7 @@ void AlkOnlineQuote::Private::downloadUrlDone(KJob* job)
m_eventLoop->exit(result);
}
#else
#else // QT_VERSION
// This is simply a placeholder. It is unused but needs to be present
// to make the linker happy (since the declaration of the slot cannot
......@@ -424,7 +450,40 @@ bool AlkOnlineQuote::Private::downloadUrl(const KUrl& url)
}
return result;
}
#endif
#endif // QT_VERSION
#else // BUILD_WITH_QTNETWORK
void AlkOnlineQuote::Private::downloadUrlDone(QNetworkReply *reply)
{
bool result = true;
if (reply->error() == QNetworkReply::NoError) {
kDebug(Private::dbgArea()) << "Downloaded data from" << reply->url();
result = processDownloadedPage(KUrl(reply->url()), reply->readAll());
} else {
emit m_p->error(reply->errorString());
m_errors |= Errors::URL;
result = slotParseQuote(QString());
}
m_eventLoop->exit(result);
}
bool AlkOnlineQuote::Private::downloadUrl(const KUrl &url)
{
QNetworkAccessManager manager(this);
connect(&manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(downloadUrlDone(QNetworkReply*)));
QNetworkRequest request;
request.setUrl(url);
request.setRawHeader("User-Agent", "alkimia " ALK_VERSION_STRING);
manager.get(request);
m_eventLoop = new QEventLoop;
bool result = m_eventLoop->exec(QEventLoop::ExcludeUserInputEvents);
delete m_eventLoop;
m_eventLoop = nullptr;
return result;
}
#endif // BUILD_WITH_QTNETWORK
#ifdef ENABLE_FINANCEQUOTE
bool AlkOnlineQuote::Private::launchFinanceQuote(const QString &_symbol, const QString &_id,
......
......@@ -9,7 +9,7 @@ set(TEST_LIB
if(BUILD_QT4)
list(APPEND TEST_LIB
${QT_USE_LIBSPREFIX}Declarative
${KDE_LIBRARIES}
${KDE4_KDECORE_LIBS}
)
else()
list(APPEND TEST_LIB
......
......@@ -38,8 +38,6 @@ if(BUILD_QT4)
${KDE4_KDECORE_LIBS}
${KDE4_KDEUI_LIBS}
${QT_USE_LIBSPREFIX}Core
${QT_USE_LIBSPREFIX}Network
${QT_USE_LIBSPREFIX}WebKit
)
else()
set(LIBS
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment