From 928adb5d6b625c2a89b01fb52ffa73b6cbe973d0 Mon Sep 17 00:00:00 2001 From: kh1 Date: Tue, 22 May 2012 14:36:18 +0200 Subject: Fix broken handling of unsupported mime types. Task-number: QTCREATORBUG-2701 Task-number: QTCREATORBUG-4059 Enable plugins to support pdf, etc. files out of the box in Qt Creator. If we still fail we try to open the link using the desktop service, as last resort to open a page showing some error message and hints about the error. Adjust the old page not found message to match the new design. Change-Id: I7fef79d27e8bc279a6169b6f3da4787ffa0d023d Reviewed-by: Niels Weber Reviewed-by: Joerg Bornemann Reviewed-by: Karsten Heimrich --- src/plugins/help/helpviewer_qwv.cpp | 128 ++++++++++++++++++++++++++++++++++-- 1 file changed, 121 insertions(+), 7 deletions(-) (limited to 'src/plugins/help') diff --git a/src/plugins/help/helpviewer_qwv.cpp b/src/plugins/help/helpviewer_qwv.cpp index a8d1cca8eb..3e277a441b 100644 --- a/src/plugins/help/helpviewer_qwv.cpp +++ b/src/plugins/help/helpviewer_qwv.cpp @@ -39,12 +39,15 @@ #include "localhelpmanager.h" #include "openpagesmanager.h" +#include #include #include #include #include +#include #include +#include #include #include @@ -58,6 +61,52 @@ using namespace Find; using namespace Help; using namespace Help::Internal; +static const char g_htmlPage[] = "%1

%2

%3

" + "
    %4%5%6%7
"; + +// some of the values we will replace %1...6 inside the former html +const QString g_percent1 = QCoreApplication::translate("HelpViewer", "Error 404..."); +const QString g_percent2 = QCoreApplication::translate("HelpViewer", "The page could not be found!"); +// percent3 will be the url of the page we got the error from +const QString g_percent4 = QCoreApplication::translate("HelpViewer", "
  • Check that you have one or more " + "documentation sets installed.
  • "); +const QString g_percent5 = QCoreApplication::translate("HelpViewer", "
  • Check that you have installed the " + "appropriate browser plug-in to support the file your loading.
  • "); +const QString g_percent6 = QCoreApplication::translate("HelpViewer", "
  • If you try to access a public " + "URL, make sure to have a network connection.
  • "); +const QString g_percent7 = QCoreApplication::translate("HelpViewer", "
  • If your computer or network is " + "protected by a firewall or proxy, make sure the application is permitted to access the network.
  • "); + + // -- HelpNetworkReply class HelpNetworkReply : public QNetworkReply @@ -150,7 +199,8 @@ QNetworkReply *HelpNetworkAccessManager::createRequest(Operation op, const QString &mimeType = HelpViewer::mimeFromUrl(url); const QByteArray &data = engine.findFile(url).isValid() ? engine.fileData(url) - : HelpViewer::PageNotFoundMessage.arg(url).toUtf8(); + : QString::fromLatin1(g_htmlPage).arg(g_percent1, g_percent2, HelpViewer::tr("Error loading: %1") + .arg(url), g_percent4, g_percent6, g_percent7, QString()).toUtf8(); return new HelpNetworkReply(request, data, mimeType.isEmpty() ? QLatin1String("application/octet-stream") : mimeType); @@ -160,6 +210,7 @@ QNetworkReply *HelpNetworkAccessManager::createRequest(Operation op, class HelpPage : public QWebPage { + Q_OBJECT public: HelpPage(QObject *parent); @@ -170,13 +221,21 @@ protected: virtual bool acceptNavigationRequest(QWebFrame *frame, const QNetworkRequest &request, NavigationType type); +private slots: + void onHandleUnsupportedContent(QNetworkReply *reply); + private: + QUrl m_loadingUrl; bool closeNewTabIfNeeded; friend class Help::Internal::HelpViewer; Qt::MouseButtons m_pressedButtons; Qt::KeyboardModifiers m_keyboardModifiers; }; +#include "helpviewer_qwv.moc" + + +// - HelpPage HelpPage::HelpPage(QObject *parent) : QWebPage(parent) @@ -184,6 +243,9 @@ HelpPage::HelpPage(QObject *parent) , m_pressedButtons(Qt::NoButton) , m_keyboardModifiers(Qt::NoModifier) { + setForwardUnsupportedContent(true); + connect(this, SIGNAL(unsupportedContent(QNetworkReply*)), this, + SLOT(onHandleUnsupportedContent(QNetworkReply*))); } QWebPage *HelpPage::createWindow(QWebPage::WebWindowType) @@ -206,8 +268,8 @@ void HelpPage::triggerAction(WebAction action, bool checked) } } -bool HelpPage::acceptNavigationRequest(QWebFrame *, - const QNetworkRequest &request, QWebPage::NavigationType type) +bool HelpPage::acceptNavigationRequest(QWebFrame *frame, const QNetworkRequest &request, + QWebPage::NavigationType type) { const bool closeNewTab = closeNewTabIfNeeded; closeNewTabIfNeeded = false; @@ -227,9 +289,59 @@ bool HelpPage::acceptNavigationRequest(QWebFrame *, return false; } + if (frame == mainFrame()) + m_loadingUrl = request.url(); + return true; } +void HelpPage::onHandleUnsupportedContent(QNetworkReply *reply) +{ + // sub resource of this page + if (m_loadingUrl != reply->url()) { + qWarning() << "Resource" << reply->url().toEncoded() << "has unknown Content-Type, will be ignored."; + reply->deleteLater(); + return; + } + + // set a default error string we are going to display + QString errorString = HelpViewer::tr("Unknown or unsupported Content!"); + if (reply->error() == QNetworkReply::NoError) { + // try to open the url using using the desktop service + if (QDesktopServices::openUrl(reply->url())) { + reply->deleteLater(); + return; + } + // seems we failed, now we show the error page inside creator + } else { + errorString = reply->errorString(); + } + + // setup html + const QString html = QString::fromLatin1(g_htmlPage).arg(g_percent1, errorString, + HelpViewer::tr("Error loading: %1").arg(reply->url().toString()), g_percent4, g_percent5, g_percent6, + g_percent7); + + // update the current layout + QList frames; + frames.append(mainFrame()); + while (!frames.isEmpty()) { + QWebFrame *frame = frames.takeFirst(); + if (frame->url() == reply->url()) { + frame->setHtml(html, reply->url()); + return; + } + + QList children = frame->childFrames(); + foreach (QWebFrame *frame, children) + frames.append(frame); + } + + if (m_loadingUrl == reply->url()) + mainFrame()->setHtml(html, reply->url()); +} + + // -- HelpViewer HelpViewer::HelpViewer(qreal zoom, QWidget *parent) @@ -238,8 +350,9 @@ HelpViewer::HelpViewer(qreal zoom, QWidget *parent) setAcceptDrops(false); installEventFilter(this); - settings()->setAttribute(QWebSettings::JavaEnabled, false); - settings()->setAttribute(QWebSettings::PluginsEnabled, false); + QWebSettings::globalSettings()->setAttribute(QWebSettings::JavaEnabled, true); + QWebSettings::globalSettings()->setAttribute(QWebSettings::PluginsEnabled, true); + QWebSettings::globalSettings()->setAttribute(QWebSettings::DnsPrefetchEnabled, true); setPage(new HelpPage(this)); HelpNetworkAccessManager *manager = new HelpNetworkAccessManager(this); @@ -451,8 +564,9 @@ void HelpViewer::slotNetworkReplyFinished(QNetworkReply *reply) { if (reply && reply->error() != QNetworkReply::NoError) { setSource(QUrl(Help::Constants::AboutBlank)); - setHtml(HelpViewer::PageNotFoundMessage.arg(reply->url().toString() - + QString::fromLatin1("

    Error: %1").arg(reply->errorString()))); + setHtml(QString::fromLatin1(g_htmlPage).arg(g_percent1, reply->errorString(), + HelpViewer::tr("Error loading: %1").arg(reply->url().toString()), g_percent4, g_percent6, g_percent7, + QString())); } } -- cgit v1.2.1