diff --git a/src/plugins/help/localhelpmanager.cpp b/src/plugins/help/localhelpmanager.cpp index 94bcfe79070..d99cdce10a3 100644 --- a/src/plugins/help/localhelpmanager.cpp +++ b/src/plugins/help/localhelpmanager.cpp @@ -31,6 +31,7 @@ #include "bookmarkmanager.h" #include "helpconstants.h" +#include "helpviewer.h" #include #include @@ -55,6 +56,7 @@ LocalHelpManager::LocalHelpManager(QObject *parent) , m_needsCollectionFile(true) { m_instance = this; + qRegisterMetaType("Help::Internal::LocalHelpManager::HelpData"); } LocalHelpManager::~LocalHelpManager() @@ -143,10 +145,85 @@ bool LocalHelpManager::isValidUrl(const QString &link) || scheme == QLatin1String("https")); } -QByteArray LocalHelpManager::helpData(const QUrl &url) +QByteArray LocalHelpManager::loadErrorMessage(const QUrl &url, const QString &errorString) { + const char g_htmlPage[] = + "" + "" + "" + "%1" + "" + "" + "" + "
" + "" + "

%2

" + "

%3

" + "%4" + "
" + "" + ""; + + // some of the values we will replace %1...6 inside the former html + const QString g_percent1 = QCoreApplication::translate("Help", "Error loading page"); + // percent2 will be the error details + // percent3 will be the url of the page we got the error from + const QString g_percent4 = QCoreApplication::translate("Help", "

Check that you have the corresponding " + "documentation set installed.

"); + + return QString::fromLatin1(g_htmlPage).arg(g_percent1, errorString, + QCoreApplication::translate("Help", "Error loading: %1").arg(url.toString()), + g_percent4).toUtf8(); +} + +LocalHelpManager::HelpData LocalHelpManager::helpData(const QUrl &url) +{ + HelpData data; const QHelpEngineCore &engine = helpEngine(); - return engine.findFile(url).isValid() ? engine.fileData(url) - : tr("Could not load \"%1\".").arg(url.toString()).toUtf8(); + data.resolvedUrl = engine.findFile(url); + if (data.resolvedUrl.isValid()) { + data.data = engine.fileData(data.resolvedUrl); + data.mimeType = HelpViewer::mimeFromUrl(data.resolvedUrl); + if (data.mimeType.isEmpty()) + data.mimeType = QLatin1String("application/octet-stream"); + } else { + data.data = loadErrorMessage(url, QCoreApplication::translate( + "Help", "The page could not be found")); + data.mimeType = QLatin1String("text/html"); + } + return data; } diff --git a/src/plugins/help/localhelpmanager.h b/src/plugins/help/localhelpmanager.h index 7de812d615d..62c4efa9250 100644 --- a/src/plugins/help/localhelpmanager.h +++ b/src/plugins/help/localhelpmanager.h @@ -32,20 +32,26 @@ #include #include +#include QT_FORWARD_DECLARE_CLASS(QHelpEngine) -QT_FORWARD_DECLARE_CLASS(QUrl) class BookmarkManager; namespace Help { - namespace Internal { +namespace Internal { class LocalHelpManager : public QObject { Q_OBJECT public: + struct HelpData { + QUrl resolvedUrl; + QByteArray data; + QString mimeType; + }; + LocalHelpManager(QObject *parent = 0); ~LocalHelpManager(); @@ -60,7 +66,8 @@ public: static QVariant engineFontSettings(); static bool isValidUrl(const QString &link); - Q_INVOKABLE QByteArray helpData(const QUrl &url); + static QByteArray loadErrorMessage(const QUrl &url, const QString &errorString); + Q_INVOKABLE static Help::Internal::LocalHelpManager::HelpData helpData(const QUrl &url); private: bool m_guiNeedsSetup; @@ -72,7 +79,10 @@ private: static QMutex m_bkmarkMutex; static BookmarkManager *m_bookmarkManager; }; - } // Internal + +} // Internal } // Help +Q_DECLARE_METATYPE(Help::Internal::LocalHelpManager::HelpData) + #endif // LOCALHELPMANAGER_H diff --git a/src/plugins/help/macwebkithelpviewer.mm b/src/plugins/help/macwebkithelpviewer.mm index 64fd77b6eb0..65be98bac40 100644 --- a/src/plugins/help/macwebkithelpviewer.mm +++ b/src/plugins/help/macwebkithelpviewer.mm @@ -163,22 +163,19 @@ AutoreleasePool::~AutoreleasePool() - (void)startLoading { const QUrl &url = QUrl::fromNSURL(self.request.URL); - QByteArray data; + Help::Internal::LocalHelpManager::HelpData data; Help::Internal::LocalHelpManager *helpManager = Help::Internal::LocalHelpManager::instance(); QMetaObject::invokeMethod(helpManager, "helpData", Qt::BlockingQueuedConnection, - Q_RETURN_ARG(QByteArray, data), Q_ARG(QUrl, url)); + Q_RETURN_ARG(Help::Internal::LocalHelpManager::HelpData, data), + Q_ARG(QUrl, url)); - QString mtString = Help::Internal::HelpViewer::mimeFromUrl(url); - if (mtString.isEmpty()) - mtString = QLatin1String("application/octet-stream"); - NSString *mimeType = mtString.toNSString(); - - NSData *nsdata = QtMac::toNSData(data); // Qt 5.3 has this in QByteArray - - NSURLResponse *response = [[NSURLResponse alloc] initWithURL:self.request.URL + NSURL *resolvedURL = data.resolvedUrl.toNSURL(); + NSString *mimeType = data.mimeType.toNSString(); + NSData *nsdata = QtMac::toNSData(data.data); // Qt 5.3 has this in QByteArray + NSURLResponse *response = [[NSURLResponse alloc] initWithURL:resolvedURL MIMEType:mimeType - expectedContentLength:data.length() + expectedContentLength:data.data.length() textEncodingName:@"UTF8"]; [self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed]; diff --git a/src/plugins/help/qtwebkithelpviewer.cpp b/src/plugins/help/qtwebkithelpviewer.cpp index 7d28578776a..ec42c79d4b6 100644 --- a/src/plugins/help/qtwebkithelpviewer.cpp +++ b/src/plugins/help/qtwebkithelpviewer.cpp @@ -66,52 +66,6 @@ using namespace Core; 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 @@ -182,16 +136,8 @@ QNetworkReply *HelpNetworkAccessManager::createRequest(Operation op, if (!HelpViewer::isLocalUrl(request.url())) return Utils::NetworkAccessManager::createRequest(op, request, outgoingData); - QString url = request.url().toString(); - const QHelpEngineCore &engine = LocalHelpManager::helpEngine(); - - const QString &mimeType = HelpViewer::mimeFromUrl(url); - const QByteArray &data = engine.findFile(url).isValid() ? engine.fileData(url) - : 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); + LocalHelpManager::HelpData data = LocalHelpManager::helpData(request.url()); + return new HelpNetworkReply(request, data.data, data.mimeType); } // - HelpPage @@ -266,7 +212,7 @@ void HelpPage::onHandleUnsupportedContent(QNetworkReply *reply) } // set a default error string we are going to display - QString errorString = HelpViewer::tr("Unknown or unsupported Content!"); + 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())) { @@ -278,10 +224,8 @@ void HelpPage::onHandleUnsupportedContent(QNetworkReply *reply) 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); + const QString html = QString::fromUtf8(LocalHelpManager::loadErrorMessage(reply->url(), + errorString)); // update the current layout QList frames; @@ -416,9 +360,8 @@ void QtWebKitHelpWidget::slotNetworkReplyFinished(QNetworkReply *reply) { if (reply && reply->error() != QNetworkReply::NoError) { load(QUrl(Help::Constants::AboutBlank)); - 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())); + setHtml(QString::fromUtf8(LocalHelpManager::loadErrorMessage(reply->url(), + reply->errorString()))); } } diff --git a/src/plugins/help/textbrowserhelpviewer.cpp b/src/plugins/help/textbrowserhelpviewer.cpp index 4b8735c7f48..832af0d6aa7 100644 --- a/src/plugins/help/textbrowserhelpviewer.cpp +++ b/src/plugins/help/textbrowserhelpviewer.cpp @@ -122,36 +122,11 @@ QUrl TextBrowserHelpViewer::source() const void TextBrowserHelpViewer::setSource(const QUrl &url) { - const QString &string = url.toString(); - if (url.isValid() && string != QLatin1String("help")) { - if (launchWithExternalApp(url)) - return; - - QUrl resolvedUrl; - if (url.scheme() == QLatin1String("http")) - resolvedUrl = url; - - if (!resolvedUrl.isValid()) { - const QHelpEngineCore &engine = LocalHelpManager::helpEngine(); - resolvedUrl = engine.findFile(url); - } - - if (resolvedUrl.isValid()) { - m_textBrowser->setSource(resolvedUrl); - slotLoadFinished(); - return; - } - } + if (launchWithExternalApp(url)) + return; + slotLoadStarted(); m_textBrowser->setSource(url); - m_textBrowser->setHtml(string == Help::Constants::AboutBlank - ? HelpViewer::tr("about:blank") - : HelpViewer::tr("Error 404..." - "


    The page could not be found

    " - "

    \"%1\"

    ") - .arg(url.toString())); - slotLoadFinished(); } @@ -321,18 +296,9 @@ TextBrowserHelpWidget::TextBrowserHelpWidget(int zoom, TextBrowserHelpViewer *pa QVariant TextBrowserHelpWidget::loadResource(int type, const QUrl &name) { - QByteArray ba; - if (type < 4) { - const QHelpEngineCore &engine = LocalHelpManager::helpEngine(); - ba = engine.fileData(name); - if (name.toString().endsWith(QLatin1String(".svg"), Qt::CaseInsensitive)) { - QImage image; - image.loadFromData(ba, "svg"); - if (!image.isNull()) - return image; - } - } - return ba; + if (type < 4) + return LocalHelpManager::helpData(name).data; + return QByteArray(); } bool TextBrowserHelpWidget::hasAnchorAt(const QPoint &pos)