1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "helpviewer.h"
35 #include "helpconstants.h"
36 #include "localhelpmanager.h"
38 #include <QtCore/QFileInfo>
39 #include <QtCore/QStringBuilder>
40 #include <QtCore/QTemporaryFile>
41 #include <QtCore/QUrl>
43 #include <QtGui/QApplication>
44 #include <QtGui/QDesktopServices>
45 #include <QtGui/QMouseEvent>
47 #include <QtHelp/QHelpEngine>
49 using namespace Help::Internal;
51 const QString HelpViewer::NsNokia = QLatin1String("qthelp://com.nokia.");
52 const QString HelpViewer::NsTrolltech = QLatin1String("qthelp://com.trolltech.");
54 const QString HelpViewer::AboutBlankPage =
55 QCoreApplication::translate("HelpViewer", "<title>about:blank</title>");
57 const QString HelpViewer::PageNotFoundMessage =
58 QCoreApplication::translate("HelpViewer", "<html><head><meta http-equiv=\""
59 "content-type\" content=\"text/html; charset=UTF-8\"><title>Error 404...</title>"
60 "</head><body><div align=\"center\"><br><br><h1>The page could not be found</h1>"
61 "<br><h3>'%1'</h3></div></body>");
64 const char *extension;
67 { ".bmp", "image/bmp" },
68 { ".css", "text/css" },
69 { ".gif", "image/gif" },
70 { ".html", "text/html" },
71 { ".htm", "text/html" },
72 { ".ico", "image/x-icon" },
73 { ".jpeg", "image/jpeg" },
74 { ".jpg", "image/jpeg" },
75 { ".js", "application/x-javascript" },
76 { ".mng", "video/x-mng" },
77 { ".pbm", "image/x-portable-bitmap" },
78 { ".pgm", "image/x-portable-graymap" },
79 { ".pdf", "application/pdf" },
80 { ".png", "image/png" },
81 { ".ppm", "image/x-portable-pixmap" },
82 { ".rss", "application/rss+xml" },
83 { ".svg", "image/svg+xml" },
84 { ".svgz", "image/svg+xml" },
85 { ".text", "text/plain" },
86 { ".tif", "image/tiff" },
87 { ".tiff", "image/tiff" },
88 { ".txt", "text/plain" },
89 { ".xbm", "image/x-xbitmap" },
90 { ".xml", "text/xml" },
91 { ".xpm", "image/x-xpm" },
92 { ".xsl", "text/xsl" },
93 { ".xhtml", "application/xhtml+xml" },
94 { ".wml", "text/vnd.wap.wml" },
95 { ".wmlc", "application/vnd.wap.wmlc" },
100 bool HelpViewer::isLocalUrl(const QUrl &url)
102 return url.scheme() == QLatin1String("qthelp");
105 bool HelpViewer::canOpenPage(const QString &url)
107 return !mimeFromUrl(url).isEmpty();
110 QString HelpViewer::mimeFromUrl(const QUrl &url)
112 const QString &path = url.path();
113 const int index = path.lastIndexOf(QLatin1Char('.'));
114 const QByteArray &ext = path.mid(index).toUtf8().toLower();
116 const ExtensionMap *e = extensionMap;
117 while (e->extension) {
118 if (ext == e->extension)
119 return QLatin1String(e->mimeType);
122 return QLatin1String("");
125 bool HelpViewer::launchWithExternalApp(const QUrl &url)
127 if (isLocalUrl(url)) {
128 const QHelpEngineCore &helpEngine = LocalHelpManager::helpEngine();
129 const QUrl &resolvedUrl = helpEngine.findFile(url);
130 if (!resolvedUrl.isValid())
133 const QString& path = resolvedUrl.path();
134 if (!canOpenPage(path)) {
135 QTemporaryFile tmpTmpFile;
136 if (!tmpTmpFile.open())
139 const QString &extension = QFileInfo(path).completeSuffix();
140 QFile actualTmpFile(tmpTmpFile.fileName() % QLatin1String(".")
142 if (!actualTmpFile.open(QIODevice::ReadWrite | QIODevice::Truncate))
145 actualTmpFile.write(helpEngine.fileData(resolvedUrl));
146 actualTmpFile.close();
147 return QDesktopServices::openUrl(QUrl(actualTmpFile.fileName()));
153 void HelpViewer::home()
155 const QHelpEngineCore &engine = LocalHelpManager::helpEngine();
156 QString homepage = engine.customValue(QLatin1String("HomePage"),
157 QLatin1String("")).toString();
159 if (homepage.isEmpty()) {
160 homepage = engine.customValue(QLatin1String("DefaultHomePage"),
161 Help::Constants::AboutBlank).toString();
167 void HelpViewer::slotLoadStarted()
169 qApp->setOverrideCursor(QCursor(Qt::WaitCursor));
172 void HelpViewer::slotLoadFinished(bool ok)
175 emit sourceChanged(source());
176 qApp->restoreOverrideCursor();
179 bool HelpViewer::handleForwardBackwardMouseButtons(QMouseEvent *event)
181 if (event->button() == Qt::XButton1) {
185 if (event->button() == Qt::XButton2) {