From 7fee7053d4c22438c6f252004fcd75403414e3a7 Mon Sep 17 00:00:00 2001 From: nowrep Date: Sun, 8 Jul 2012 00:15:03 +0200 Subject: [PATCH] Plugins: New method in API (createRequest in network manager) --- src/lib/adblock/adblockmanager.cpp | 2 +- src/lib/adblock/adblockmanager.h | 2 +- src/lib/network/networkmanager.cpp | 8 +++++++- src/lib/plugins/plugininterface.h | 2 ++ src/lib/plugins/pluginproxy.cpp | 12 ++++++++++++ src/lib/plugins/pluginproxy.h | 2 ++ src/lib/tools/followredirectreply.cpp | 5 +++++ src/lib/tools/followredirectreply.h | 1 + src/lib/tools/globalfunctions.cpp | 25 ++++++++++++++----------- src/lib/tools/globalfunctions.h | 3 ++- 10 files changed, 47 insertions(+), 15 deletions(-) diff --git a/src/lib/adblock/adblockmanager.cpp b/src/lib/adblock/adblockmanager.cpp index a153f23fb..1c37840c3 100644 --- a/src/lib/adblock/adblockmanager.cpp +++ b/src/lib/adblock/adblockmanager.cpp @@ -193,7 +193,7 @@ void AdBlockManager::load() continue; } - const QString absolutePath = adblockDir.absoluteFilePath(fileName); + const QString &absolutePath = adblockDir.absoluteFilePath(fileName); QFile file(absolutePath); if (!file.open(QFile::ReadOnly)) { continue; diff --git a/src/lib/adblock/adblockmanager.h b/src/lib/adblock/adblockmanager.h index 762a78565..289bd9f82 100644 --- a/src/lib/adblock/adblockmanager.h +++ b/src/lib/adblock/adblockmanager.h @@ -69,7 +69,7 @@ public slots: AdBlockDialog* showDialog(); private: - bool canBeBlocked(const QUrl &url) const; + inline bool canBeBlocked(const QUrl &url) const; static AdBlockManager* s_adBlockManager; bool m_loaded; diff --git a/src/lib/network/networkmanager.cpp b/src/lib/network/networkmanager.cpp index 4c35cc172..5f4ea2bc0 100644 --- a/src/lib/network/networkmanager.cpp +++ b/src/lib/network/networkmanager.cpp @@ -347,7 +347,7 @@ QNetworkReply* NetworkManager::createRequest(QNetworkAccessManager::Operation op QNetworkRequest req = request; QNetworkReply* reply = 0; - //SchemeHandlers + // SchemeHandlers if (m_schemeHandlers.contains(req.url().scheme())) { reply = m_schemeHandlers[req.url().scheme()]->createRequest(op, req, outgoingData); if (reply) { @@ -355,6 +355,12 @@ QNetworkReply* NetworkManager::createRequest(QNetworkAccessManager::Operation op } } + // Plugins + reply = mApp->plugins()->createRequest(op, request, outgoingData); + if (reply) { + return reply; + } + if (m_doNotTrack) { req.setRawHeader("DNT", QByteArray("1")); } diff --git a/src/lib/plugins/plugininterface.h b/src/lib/plugins/plugininterface.h index 7963796b2..654b17283 100644 --- a/src/lib/plugins/plugininterface.h +++ b/src/lib/plugins/plugininterface.h @@ -81,6 +81,8 @@ public: virtual bool keyPress(const Qz::ObjectName &type, QObject* obj, QKeyEvent* event) { Q_UNUSED(type) Q_UNUSED(obj) Q_UNUSED(event) return false; } virtual bool keyRelease(const Qz::ObjectName &type, QObject* obj, QKeyEvent* event) { Q_UNUSED(type) Q_UNUSED(obj) Q_UNUSED(event) return false; } + + virtual QNetworkReply* createRequest(QNetworkAccessManager::Operation op, const QNetworkRequest &request, QIODevice* outgoingData) { Q_UNUSED(op) Q_UNUSED(request) Q_UNUSED(outgoingData) return 0; } }; Q_DECLARE_INTERFACE(PluginInterface, "QupZilla.Browser.PluginInterface/1.1") diff --git a/src/lib/plugins/pluginproxy.cpp b/src/lib/plugins/pluginproxy.cpp index f1800f323..30513d55f 100644 --- a/src/lib/plugins/pluginproxy.cpp +++ b/src/lib/plugins/pluginproxy.cpp @@ -191,6 +191,18 @@ bool PluginProxy::processKeyRelease(const Qz::ObjectName &type, QObject* obj, QK return accepted; } +QNetworkReply* PluginProxy::createRequest(QNetworkAccessManager::Operation op, const QNetworkRequest &request, QIODevice* outgoingData) +{ + foreach(PluginInterface * iPlugin, m_loadedPlugins) { + QNetworkReply* reply = iPlugin->createRequest(op, request, outgoingData); + if (reply) { + return reply; + } + } + + return 0; +} + void PluginProxy::emitWebPageCreated(WebPage* page) { emit webPageCreated(page); diff --git a/src/lib/plugins/pluginproxy.h b/src/lib/plugins/pluginproxy.h index 71fc033ab..2f004147d 100644 --- a/src/lib/plugins/pluginproxy.h +++ b/src/lib/plugins/pluginproxy.h @@ -49,6 +49,8 @@ public: bool processKeyPress(const Qz::ObjectName &type, QObject* obj, QKeyEvent* event); bool processKeyRelease(const Qz::ObjectName &type, QObject* obj, QKeyEvent* event); + QNetworkReply* createRequest(QNetworkAccessManager::Operation op, const QNetworkRequest &request, QIODevice* outgoingData); + void emitWebPageCreated(WebPage* page); void emitWebPageDeleted(WebPage* page); diff --git a/src/lib/tools/followredirectreply.cpp b/src/lib/tools/followredirectreply.cpp index d3ff3f89d..c327d00e9 100644 --- a/src/lib/tools/followredirectreply.cpp +++ b/src/lib/tools/followredirectreply.cpp @@ -33,6 +33,11 @@ QNetworkReply* FollowRedirectReply::reply() const return m_reply; } +QUrl FollowRedirectReply::originalUrl() const +{ + return m_reply->request().url(); +} + QUrl FollowRedirectReply::url() const { return m_reply->url(); diff --git a/src/lib/tools/followredirectreply.h b/src/lib/tools/followredirectreply.h index 06f8ada46..06410f92a 100644 --- a/src/lib/tools/followredirectreply.h +++ b/src/lib/tools/followredirectreply.h @@ -36,6 +36,7 @@ public: ~FollowRedirectReply(); QNetworkReply* reply() const; + QUrl originalUrl() const; QUrl url() const; QNetworkReply::NetworkError error() const; diff --git a/src/lib/tools/globalfunctions.cpp b/src/lib/tools/globalfunctions.cpp index 91271cdb5..527fd0a30 100644 --- a/src/lib/tools/globalfunctions.cpp +++ b/src/lib/tools/globalfunctions.cpp @@ -53,11 +53,13 @@ QPixmap qz_pixmapFromByteArray(const QByteArray &data) QByteArray qz_readAllFileContents(const QString &filename) { QFile file(filename); - file.open(QFile::ReadOnly); - QByteArray a = file.readAll(); - file.close(); + if (file.open(QFile::ReadOnly)) { + QByteArray a = file.readAll(); + file.close(); + return a; + } - return a; + return QByteArray(); } void qz_centerWidgetOnScreen(QWidget* w) @@ -176,23 +178,24 @@ QString qz_urlEncodeQueryString(const QUrl &url) return returnString; } -QString qz_ensureUniqueFilename(const QString &pathToFile) +QString qz_ensureUniqueFilename(const QString &name, const QString &appendFormat) { - if (!QFile::exists(pathToFile)) { - return pathToFile; + if (!QFile::exists(name)) { + return name; } - QString tmpFileName = pathToFile; + QString tmpFileName = name; int i = 1; while (QFile::exists(tmpFileName)) { - tmpFileName = pathToFile; + tmpFileName = name; int index = tmpFileName.lastIndexOf("."); + QString appendString = appendFormat.arg(i); if (index == -1) { - tmpFileName.append("(" + QString::number(i) + ")"); + tmpFileName.append(appendString); } else { - tmpFileName = tmpFileName.left(index) + "(" + QString::number(i) + ")" + tmpFileName.mid(index); + tmpFileName = tmpFileName.left(index) + appendString + tmpFileName.mid(index); } i++; } diff --git a/src/lib/tools/globalfunctions.h b/src/lib/tools/globalfunctions.h index 94efe9276..e538a49c3 100644 --- a/src/lib/tools/globalfunctions.h +++ b/src/lib/tools/globalfunctions.h @@ -19,6 +19,7 @@ #define GLOBALFUNCTIONS_H #include +#include #include "qz_namespace.h" @@ -43,7 +44,7 @@ QString QT_QUPZILLA_EXPORT qz_samePartOfStrings(const QString &one, const QStrin QUrl QT_QUPZILLA_EXPORT qz_makeRelativeUrl(const QUrl &baseUrl, const QUrl &rUrl); QString QT_QUPZILLA_EXPORT qz_urlEncodeQueryString(const QUrl &url); -QString QT_QUPZILLA_EXPORT qz_ensureUniqueFilename(const QString &name); +QString QT_QUPZILLA_EXPORT qz_ensureUniqueFilename(const QString &name, const QString &appendFormat = QString("(%1)")); QString QT_QUPZILLA_EXPORT qz_getFileNameFromUrl(const QUrl &url); QString QT_QUPZILLA_EXPORT qz_filterCharsFromFilename(const QString &name);