OSDN Git Service

3bc13a312d05014c0dc07b5fba2a1d793e9d309d
[qt-creator-jp/qt-creator-jp.git] / src / plugins / help / helpviewer.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
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.
24 **
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.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "helpviewer.h"
35 #include "helpconstants.h"
36 #include "localhelpmanager.h"
37
38 #include <QtCore/QFileInfo>
39 #include <QtCore/QStringBuilder>
40 #include <QtCore/QTemporaryFile>
41 #include <QtCore/QUrl>
42
43 #include <QtGui/QApplication>
44 #include <QtGui/QDesktopServices>
45 #include <QtGui/QMouseEvent>
46
47 #include <QtHelp/QHelpEngine>
48
49 using namespace Help::Internal;
50
51 const QString HelpViewer::NsNokia = QLatin1String("qthelp://com.nokia.");
52 const QString HelpViewer::NsTrolltech = QLatin1String("qthelp://com.trolltech.");
53
54 const QString HelpViewer::AboutBlankPage =
55     QCoreApplication::translate("HelpViewer", "<title>about:blank</title>");
56
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>");
62
63 struct ExtensionMap {
64     const char *extension;
65     const char *mimeType;
66 } extensionMap[] = {
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" },
96     { "about:blank", 0 },
97     { 0, 0 }
98 };
99
100 bool HelpViewer::isLocalUrl(const QUrl &url)
101 {
102     return url.scheme() == QLatin1String("qthelp");
103 }
104
105 bool HelpViewer::canOpenPage(const QString &url)
106 {
107     return !mimeFromUrl(url).isEmpty();
108 }
109
110 QString HelpViewer::mimeFromUrl(const QUrl &url)
111 {
112     const QString &path = url.path();
113     const int index = path.lastIndexOf(QLatin1Char('.'));
114     const QByteArray &ext = path.mid(index).toUtf8().toLower();
115
116     const ExtensionMap *e = extensionMap;
117     while (e->extension) {
118         if (ext == e->extension)
119             return QLatin1String(e->mimeType);
120         ++e;
121     }
122     return QLatin1String("");
123 }
124
125 bool HelpViewer::launchWithExternalApp(const QUrl &url)
126 {
127     if (isLocalUrl(url)) {
128         const QHelpEngineCore &helpEngine = LocalHelpManager::helpEngine();
129         const QUrl &resolvedUrl = helpEngine.findFile(url);
130         if (!resolvedUrl.isValid())
131             return false;
132
133         const QString& path = resolvedUrl.path();
134         if (!canOpenPage(path)) {
135             QTemporaryFile tmpTmpFile;
136             if (!tmpTmpFile.open())
137                 return false;
138
139             const QString &extension = QFileInfo(path).completeSuffix();
140             QFile actualTmpFile(tmpTmpFile.fileName() % QLatin1String(".")
141                 % extension);
142             if (!actualTmpFile.open(QIODevice::ReadWrite | QIODevice::Truncate))
143                 return false;
144
145             actualTmpFile.write(helpEngine.fileData(resolvedUrl));
146             actualTmpFile.close();
147             return QDesktopServices::openUrl(QUrl(actualTmpFile.fileName()));
148         }
149     }
150     return false;
151 }
152
153 void HelpViewer::home()
154 {
155     const QHelpEngineCore &engine = LocalHelpManager::helpEngine();
156     QString homepage = engine.customValue(QLatin1String("HomePage"),
157         QLatin1String("")).toString();
158
159     if (homepage.isEmpty()) {
160         homepage = engine.customValue(QLatin1String("DefaultHomePage"),
161             Help::Constants::AboutBlank).toString();
162     }
163
164     setSource(homepage);
165 }
166
167 void HelpViewer::slotLoadStarted()
168 {
169     qApp->setOverrideCursor(QCursor(Qt::WaitCursor));
170 }
171
172 void HelpViewer::slotLoadFinished(bool ok)
173 {
174     Q_UNUSED(ok)
175     emit sourceChanged(source());
176     qApp->restoreOverrideCursor();
177 }
178
179 bool HelpViewer::handleForwardBackwardMouseButtons(QMouseEvent *event)
180 {
181     if (event->button() == Qt::XButton1) {
182         backward();
183         return true;
184     }
185     if (event->button() == Qt::XButton2) {
186         forward();
187         return true;
188     }
189
190     return false;
191 }