1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #include "helpviewer.h"
34 #include "helpconstants.h"
35 #include "localhelpmanager.h"
37 #include <QtCore/QFileInfo>
38 #include <QtCore/QStringBuilder>
39 #include <QtCore/QTemporaryFile>
40 #include <QtCore/QUrl>
42 #include <QtGui/QApplication>
43 #include <QtGui/QDesktopServices>
44 #include <QtGui/QMouseEvent>
46 #include <QtHelp/QHelpEngine>
48 using namespace Help::Internal;
50 const QString HelpViewer::NsNokia = QLatin1String("qthelp://com.nokia.");
51 const QString HelpViewer::NsTrolltech = QLatin1String("qthelp://com.trolltech.");
53 const QString HelpViewer::AboutBlankPage =
54 QCoreApplication::translate("HelpViewer", "<title>about:blank</title>");
56 const QString HelpViewer::PageNotFoundMessage =
57 QCoreApplication::translate("HelpViewer", "<html><head><meta http-equiv=\""
58 "content-type\" content=\"text/html; charset=UTF-8\"><title>Error 404...</title>"
59 "</head><body><div align=\"center\"><br><br><h1>The page could not be found</h1>"
60 "<br><h3>'%1'</h3></div></body>");
63 const char *extension;
66 { ".bmp", "image/bmp" },
67 { ".css", "text/css" },
68 { ".gif", "image/gif" },
69 { ".html", "text/html" },
70 { ".htm", "text/html" },
71 { ".ico", "image/x-icon" },
72 { ".jpeg", "image/jpeg" },
73 { ".jpg", "image/jpeg" },
74 { ".js", "application/x-javascript" },
75 { ".mng", "video/x-mng" },
76 { ".pbm", "image/x-portable-bitmap" },
77 { ".pgm", "image/x-portable-graymap" },
78 { ".pdf", "application/pdf" },
79 { ".png", "image/png" },
80 { ".ppm", "image/x-portable-pixmap" },
81 { ".rss", "application/rss+xml" },
82 { ".svg", "image/svg+xml" },
83 { ".svgz", "image/svg+xml" },
84 { ".text", "text/plain" },
85 { ".tif", "image/tiff" },
86 { ".tiff", "image/tiff" },
87 { ".txt", "text/plain" },
88 { ".xbm", "image/x-xbitmap" },
89 { ".xml", "text/xml" },
90 { ".xpm", "image/x-xpm" },
91 { ".xsl", "text/xsl" },
92 { ".xhtml", "application/xhtml+xml" },
93 { ".wml", "text/vnd.wap.wml" },
94 { ".wmlc", "application/vnd.wap.wmlc" },
99 bool HelpViewer::isLocalUrl(const QUrl &url)
101 return url.scheme() == QLatin1String("qthelp");
104 bool HelpViewer::canOpenPage(const QString &url)
106 return !mimeFromUrl(url).isEmpty();
109 QString HelpViewer::mimeFromUrl(const QUrl &url)
111 const QString &path = url.path();
112 const int index = path.lastIndexOf(QLatin1Char('.'));
113 const QByteArray &ext = path.mid(index).toUtf8().toLower();
115 const ExtensionMap *e = extensionMap;
116 while (e->extension) {
117 if (ext == e->extension)
118 return QLatin1String(e->mimeType);
121 return QLatin1String("");
124 bool HelpViewer::launchWithExternalApp(const QUrl &url)
126 if (isLocalUrl(url)) {
127 const QHelpEngineCore &helpEngine = LocalHelpManager::helpEngine();
128 const QUrl &resolvedUrl = helpEngine.findFile(url);
129 if (!resolvedUrl.isValid())
132 const QString& path = resolvedUrl.path();
133 if (!canOpenPage(path)) {
134 QTemporaryFile tmpTmpFile;
135 if (!tmpTmpFile.open())
138 const QString &extension = QFileInfo(path).completeSuffix();
139 QFile actualTmpFile(tmpTmpFile.fileName() % QLatin1String(".")
141 if (!actualTmpFile.open(QIODevice::ReadWrite | QIODevice::Truncate))
144 actualTmpFile.write(helpEngine.fileData(resolvedUrl));
145 actualTmpFile.close();
146 return QDesktopServices::openUrl(QUrl(actualTmpFile.fileName()));
152 void HelpViewer::home()
154 const QHelpEngineCore &engine = LocalHelpManager::helpEngine();
155 QString homepage = engine.customValue(QLatin1String("HomePage"),
156 QLatin1String("")).toString();
158 if (homepage.isEmpty()) {
159 homepage = engine.customValue(QLatin1String("DefaultHomePage"),
160 Help::Constants::AboutBlank).toString();
166 void HelpViewer::slotLoadStarted()
168 qApp->setOverrideCursor(QCursor(Qt::WaitCursor));
171 void HelpViewer::slotLoadFinished(bool ok)
174 emit sourceChanged(source());
175 qApp->restoreOverrideCursor();
178 bool HelpViewer::handleForwardBackwardMouseButtons(QMouseEvent *event)
180 if (event->button() == Qt::XButton1) {
184 if (event->button() == Qt::XButton2) {