rendered paste bodyFrom b0c6219922cf1427b2ded32c4e9cdcbcc5a7fb2e Mon Sep 17 00:00:00 2001
From: Kenneth Rohde Christiansen <kenneth.christiansen@openbossa.org>
Date: Wed, 30 Sep 2009 16:39:52 -0300
Subject: [PATCH] Use the new error page extension from QWebPage for showing better error pages.
---
src/webpage.cpp | 112 +++++++++++++++++++++++++++++++-----------------------
src/webpage.h | 3 +
2 files changed, 67 insertions(+), 48 deletions(-)
diff --git a/src/webpage.cpp b/src/webpage.cpp
index d619c7c..f09fd9e 100644
--- a/src/webpage.cpp
+++ b/src/webpage.cpp
@@ -114,6 +114,58 @@ WebPage::WebPage(QObject *parent)
loadSettings();
}
+bool WebPage::supportsExtension(Extension extension) const
+{
+ if (extension == ErrorPageExtension)
+ return true;
+ return false;
+}
+
+bool WebPage::extension(Extension extension, const ExtensionOption *option, ExtensionReturn *output)
+{
+ if (extension == ErrorPageExtension)
+ return errorPageExtension(
+ static_cast<const ErrorPageExtensionOption*>(option),
+ static_cast<ErrorPageExtensionReturn*>(output));
+
+ return false;
+}
+
+bool WebPage::errorPageExtension(const ErrorPageExtensionOption *option, ErrorPageExtensionReturn *output)
+{
+ // Disregard Policy Changes, which happens then unsupported content in encountered, as the
+ // policy changes from "use" to "download".
+ if (option->domain == WebKit && option->error == 102)
+ return false;
+
+ // Generate translated not found error page with an image
+ QFile notFoundErrorFile(QLatin1String(":/notfound.html"));
+ if (!notFoundErrorFile.open(QIODevice::ReadOnly))
+ return false;
+
+ QString title = tr("Error loading page: %1").arg(QString::fromUtf8(option->url.toEncoded()));
+ QString html = QLatin1String(notFoundErrorFile.readAll());
+ QPixmap pixmap = qApp->style()->standardIcon(QStyle::SP_MessageBoxWarning, 0, view()).pixmap(QSize(32, 32));
+ QBuffer imageBuffer;
+ imageBuffer.open(QBuffer::ReadWrite);
+ if (pixmap.save(&imageBuffer, "PNG")) {
+ html.replace(QLatin1String("IMAGE_BINARY_DATA_HERE"),
+ QLatin1String(imageBuffer.buffer().toBase64()));
+ }
+
+ html = html.arg(title, option->errorString,
+ tr("When connecting to: %1.").arg(QString::fromUtf8(option->url.toEncoded())),
+ tr("Check the address for errors such as <b>ww</b>.arora-browser.org instead of <b>www</b>.arora-browser.org"),
+ tr("If the address is correct, try checking the network connection."),
+ tr("If your computer or network is protected by a firewall or proxy, make sure that the browser is permitted to access the network."));
+
+ // Sets the right URL. FIXME: Remove hack when WebKit is fixed.
+ output->baseUrl = option->url;
+ output->content = html.toUtf8();
+
+ return true;
+}
+
WebPluginFactory *WebPage::webPluginFactory()
{
if (!s_webPluginFactory)
@@ -324,56 +376,20 @@ void WebPage::handleUnsupportedContent(QNetworkReply *reply)
if (replyUrl.scheme() == QLatin1String("abp"))
return;
- switch (reply->error()) {
- case QNetworkReply::NoError:
- if (reply->header(QNetworkRequest::ContentTypeHeader).isValid()) {
- BrowserApplication::downloadManager()->handleUnsupportedContent(reply);
- return;
- }
- break;
- case QNetworkReply::ProtocolUnknownError: {
- QSettings settings;
- settings.beginGroup(QLatin1String("WebView"));
- QStringList externalSchemes = settings.value(QLatin1String("externalSchemes")).toStringList();
- if (externalSchemes.contains(replyUrl.scheme())) {
- QDesktopServices::openUrl(replyUrl);
- return;
- }
- break;
- }
- default:
- break;
- }
-
- // Find the frame that has the unsupported content
- if (replyUrl.isEmpty() || replyUrl != m_requestedUrl)
- return;
-
- QWebFrame *notFoundFrame = mainFrame();
- if (!notFoundFrame)
+ if (reply->header(QNetworkRequest::ContentTypeHeader).isValid()) {
+ BrowserApplication::downloadManager()->handleUnsupportedContent(reply);
return;
+ }
- // Generate translated not found error page with an image
- QFile notFoundErrorFile(QLatin1String(":/notfound.html"));
- if (!notFoundErrorFile.open(QIODevice::ReadOnly))
- return;
- QString title = tr("Error loading page: %1").arg(QString::fromUtf8(replyUrl.toEncoded()));
- QString html = QLatin1String(notFoundErrorFile.readAll());
- QPixmap pixmap = qApp->style()->standardIcon(QStyle::SP_MessageBoxWarning, 0, view()).pixmap(QSize(32, 32));
- QBuffer imageBuffer;
- imageBuffer.open(QBuffer::ReadWrite);
- if (pixmap.save(&imageBuffer, "PNG")) {
- html.replace(QLatin1String("IMAGE_BINARY_DATA_HERE"),
- QLatin1String(imageBuffer.buffer().toBase64()));
+ QSettings settings;
+ settings.beginGroup(QLatin1String("WebView"));
+ QStringList externalSchemes = settings.value(QLatin1String("externalSchemes")).toStringList();
+ if (externalSchemes.contains(replyUrl.scheme())) {
+ if (QDesktopServices::openUrl(replyUrl));
+ return;
}
- html = html.arg(title,
- reply->errorString(),
- tr("When connecting to: %1.").arg(QString::fromUtf8(replyUrl.toEncoded())),
- tr("Check the address for errors such as <b>ww</b>.arora-browser.org instead of <b>www</b>.arora-browser.org"),
- tr("If the address is correct, try checking the network connection."),
- tr("If your computer or network is protected by a firewall or proxy, make sure that the browser is permitted to access the network."));
- notFoundFrame->setHtml(html, replyUrl);
- // Don't put error pages to the history.
- BrowserApplication::instance()->historyManager()->removeHistoryEntry(replyUrl, notFoundFrame->title());
+ // FIXME: would be nice to be able to set an error on the reply instead.
+ // like QNetworkReply::UnknownContentError.
+ reply->abort();
}
diff --git a/src/webpage.h b/src/webpage.h
index b9a80ad..c0bbe87 100644
--- a/src/webpage.h
+++ b/src/webpage.h
@@ -78,8 +78,11 @@ public:
static WebPluginFactory *webPluginFactory();
QList<WebPageLinkedResource> linkedResources(const QString &relation = QString());
+ virtual bool supportsExtension(QWebPage::Extension extension) const;
+ virtual bool extension(Extension extension, const ExtensionOption *option, ExtensionReturn *output);
protected:
+ bool errorPageExtension(const ErrorPageExtensionOption *option, ErrorPageExtensionReturn *output);
QString userAgentForUrl(const QUrl &url) const;
bool acceptNavigationRequest(QWebFrame *frame, const QNetworkRequest &request,
NavigationType type);
--
1.6.0.4