ParaView
pqHelpWindowWebKit.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ParaView
4  Module: pqHelpWindowWebKit.h
5 
6  Copyright (c) 2005,2006 Sandia Corporation, Kitware Inc.
7  All rights reserved.
8 
9  ParaView is a free software; you can redistribute it and/or modify it
10  under the terms of the ParaView license version 1.2.
11 
12  See License_v1.2.txt for the full ParaView license.
13  A copy of this license can be obtained by contacting
14  Kitware Inc.
15  28 Corporate Drive
16  Clifton Park, NY 12065
17  USA
18 
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR
23 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 ========================================================================*/
32 #ifndef pqHelpWindowWebKit_h
33 #define pqHelpWindowWebKit_h
34 
42 #include <QFileInfo>
43 #include <QNetworkAccessManager>
44 #include <QNetworkProxy>
45 #include <QTimer>
46 #include <QWebPage>
47 #include <QWebView>
48 #include <QtNetwork/QNetworkReply>
49 
50 namespace
51 {
52 // ****************************************************************************
53 // CLASS pqHelpWindowNetworkReply
54 // ****************************************************************************
59 class pqHelpWindowNetworkReply : public QNetworkReply
60 {
61  typedef QNetworkReply Superclass;
62 
63 public:
64  pqHelpWindowNetworkReply(const QUrl& url, QHelpEngineCore* helpEngine);
65 
66  virtual void abort() {}
67  virtual qint64 bytesAvailable() const
68  {
69  return (this->RawData.size() - this->Offset) + this->Superclass::bytesAvailable();
70  }
71  virtual bool isSequential() const { return true; }
72 protected:
73  virtual qint64 readData(char* data, qint64 maxSize);
74 
75  QByteArray RawData;
76  qint64 Offset;
77 
78 private:
79  Q_DISABLE_COPY(pqHelpWindowNetworkReply)
80 };
81 
82 //-----------------------------------------------------------------------------
83 pqHelpWindowNetworkReply::pqHelpWindowNetworkReply(const QUrl& my_url, QHelpEngineCore* engine)
84  : Superclass(engine)
85  , Offset(0)
86 {
87  Q_ASSERT(engine);
88 
89  this->RawData = engine->fileData(my_url);
90 
91  QString content_type = "text/plain";
92  QString extension = QFileInfo(my_url.path()).suffix().toLower();
93  QMap<QString, QString> extension_type_map;
94  extension_type_map["jpg"] = "image/jpeg";
95  extension_type_map["jpeg"] = "image/jpeg";
96  extension_type_map["png"] = "image/png";
97  extension_type_map["gif"] = "image/gif";
98  extension_type_map["tiff"] = "image/tiff";
99  extension_type_map["htm"] = "text/html";
100  extension_type_map["html"] = "text/html";
101  extension_type_map["css"] = "text/css";
102  extension_type_map["xml"] = "text/xml";
103 
104  if (extension_type_map.contains(extension))
105  {
106  content_type = extension_type_map[extension];
107  }
108 
109  this->setHeader(QNetworkRequest::ContentLengthHeader, QVariant(this->RawData.size()));
110  this->setHeader(QNetworkRequest::ContentTypeHeader, content_type);
111  this->open(QIODevice::ReadOnly | QIODevice::Unbuffered);
112  this->setUrl(my_url);
113  QTimer::singleShot(0, this, SIGNAL(readyRead()));
114  QTimer::singleShot(0, this, SLOT(finished()));
115 }
116 
117 //-----------------------------------------------------------------------------
118 qint64 pqHelpWindowNetworkReply::readData(char* data, qint64 maxSize)
119 {
120  if (this->Offset <= this->RawData.size())
121  {
122  qint64 end = qMin(this->Offset + maxSize, static_cast<qint64>(this->RawData.size()));
123  qint64 delta = end - this->Offset;
124  memcpy(data, this->RawData.constData() + this->Offset, delta);
125  this->Offset += delta;
126  return delta;
127  }
128  return -1;
129 }
130 
131 // ****************************************************************************
132 // CLASS pqNetworkAccessManager
133 // ****************************************************************************
134 //-----------------------------------------------------------------------------
135 class pqNetworkAccessManager : public QNetworkAccessManager
136 {
137  typedef QNetworkAccessManager Superclass;
138  QPointer<QHelpEngineCore> Engine;
139 
140 public:
141  pqNetworkAccessManager(
142  QHelpEngineCore* helpEngine, QNetworkAccessManager* manager, QObject* parentObject)
143  : Superclass(parentObject)
144  , Engine(helpEngine)
145  {
146  Q_ASSERT(manager != NULL && helpEngine != NULL);
147 
148  this->setCache(manager->cache());
149  this->setCookieJar(manager->cookieJar());
150  this->setProxy(manager->proxy());
151  this->setProxyFactory(manager->proxyFactory());
152  }
153 
154 protected:
155  virtual QNetworkReply* createRequest(
156  Operation operation, const QNetworkRequest& request, QIODevice* device)
157  {
158  if (request.url().scheme() == "qthelp" && operation == GetOperation)
159  {
160  return new pqHelpWindowNetworkReply(request.url(), this->Engine);
161  }
162  else
163  {
164  return this->Superclass::createRequest(operation, request, device);
165  }
166  }
167 
168 private:
169  Q_DISABLE_COPY(pqNetworkAccessManager)
170 };
171 
172 //----------------------------------------------------------------------------------
176 class pqWebView : public QWebView
177 {
178  typedef QWebView Superclass;
179 
180 public:
181  pqWebView(QWidget* parentObject)
182  : Superclass(parentObject)
183  {
184  }
185  ~pqWebView() {}
186 
187  static pqWebView* newInstance(QHelpEngine* engine, QWidget* parentObject)
188  {
189  pqWebView* instance = new pqWebView(parentObject);
190  QNetworkAccessManager* oldManager = instance->page()->networkAccessManager();
191  pqNetworkAccessManager* newManager =
192  new pqNetworkAccessManager(engine, oldManager, parentObject);
193  instance->page()->setNetworkAccessManager(newManager);
194  instance->page()->setForwardUnsupportedContent(false);
195  return instance;
196  }
197 
198 private:
199  Q_DISABLE_COPY(pqWebView)
200 };
201 
202 } // end of namespace
203 #endif