OSDN Git Service

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