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 
39 
40 #include <QFileInfo>
41 #include <QNetworkAccessManager>
42 #include <QNetworkProxy>
43 #include <QTimer>
44 #include <QWebPage>
45 #include <QWebView>
46 #include <QtNetwork/QNetworkReply>
47 
48 namespace
49 {
50 // ****************************************************************************
51 // CLASS pqHelpWindowNetworkReply
52 // ****************************************************************************
55 class pqHelpWindowNetworkReply : public QNetworkReply
56 {
57  typedef QNetworkReply Superclass;
58 public:
59  pqHelpWindowNetworkReply(const QUrl& url, QHelpEngineCore* helpEngine);
60 
61  virtual void abort() {}
62  virtual qint64 bytesAvailable() const
63  {
64  return (this->RawData.size() - this->Offset) +
65  this->Superclass::bytesAvailable();
66  }
67  virtual bool isSequential() const {return true;}
68 protected:
69  virtual qint64 readData(char *data, qint64 maxSize);
70 
71  QByteArray RawData;
72  qint64 Offset;
73 private:
74  Q_DISABLE_COPY(pqHelpWindowNetworkReply)
75 };
76 
77 //-----------------------------------------------------------------------------
78 pqHelpWindowNetworkReply::pqHelpWindowNetworkReply(
79  const QUrl& my_url, QHelpEngineCore* engine) : Superclass(engine), Offset(0)
80 {
81  Q_ASSERT(engine);
82 
83  this->RawData = engine->fileData(my_url);
84 
85  QString content_type = "text/plain";
86  QString extension = QFileInfo(my_url.path()).suffix().toLower();
87  QMap<QString, QString> extension_type_map;
88  extension_type_map["jpg"] = "image/jpeg";
89  extension_type_map["jpeg"] = "image/jpeg";
90  extension_type_map["png"] = "image/png";
91  extension_type_map["gif"] = "image/gif";
92  extension_type_map["tiff"] = "image/tiff";
93  extension_type_map["htm"] = "text/html";
94  extension_type_map["html"] = "text/html";
95  extension_type_map["css"] = "text/css";
96  extension_type_map["xml"] = "text/xml";
97 
98  if (extension_type_map.contains(extension))
99  {
100  content_type = extension_type_map[extension];
101  }
102 
103  this->setHeader(QNetworkRequest::ContentLengthHeader,
104  QVariant(this->RawData.size()));
105  this->setHeader(QNetworkRequest::ContentTypeHeader, content_type);
106  this->open(QIODevice::ReadOnly|QIODevice::Unbuffered);
107  this->setUrl(my_url);
108  QTimer::singleShot(0, this, SIGNAL(readyRead()));
109  QTimer::singleShot(0, this, SLOT(finished()));
110 }
111 
112 //-----------------------------------------------------------------------------
113 qint64 pqHelpWindowNetworkReply::readData(char *data, qint64 maxSize)
114 {
115  if (this->Offset <= this->RawData.size())
116  {
117  qint64 end = qMin(this->Offset + maxSize,
118  static_cast<qint64>(this->RawData.size()));
119  qint64 delta = end - this->Offset;
120  memcpy(data, this->RawData.constData() + this->Offset, delta);
121  this->Offset += delta;
122  return delta;
123  }
124  return -1;
125 }
126 
127 // ****************************************************************************
128 // CLASS pqNetworkAccessManager
129 // ****************************************************************************
130 //-----------------------------------------------------------------------------
131 class pqNetworkAccessManager : public QNetworkAccessManager
132 {
133  typedef QNetworkAccessManager Superclass;
134  QPointer<QHelpEngineCore> Engine;
135 public:
136  pqNetworkAccessManager(
137  QHelpEngineCore* helpEngine, QNetworkAccessManager *manager,
138  QObject *parentObject) :
139  Superclass(parentObject),
140  Engine(helpEngine)
141  {
142  Q_ASSERT(manager != NULL && helpEngine != NULL);
143 
144  this->setCache(manager->cache());
145  this->setCookieJar(manager->cookieJar());
146  this->setProxy(manager->proxy());
147  this->setProxyFactory(manager->proxyFactory());
148  }
149 
150 protected:
151  virtual QNetworkReply *createRequest(
152  Operation operation, const QNetworkRequest &request, QIODevice *device)
153  {
154  if (request.url().scheme() == "qthelp" && operation == GetOperation)
155  {
156  return new pqHelpWindowNetworkReply(request.url(), this->Engine);
157  }
158  else
159  {
160  return this->Superclass::createRequest(operation, request, device);
161  }
162  }
163 
164 private:
165  Q_DISABLE_COPY(pqNetworkAccessManager)
166 };
167 
168 //----------------------------------------------------------------------------------
170 class pqWebView : public QWebView
171 {
172  typedef QWebView Superclass;
173 public:
174  pqWebView(QWidget* parentObject)
175  : Superclass(parentObject)
176  {
177  }
178  ~pqWebView()
179  {
180  }
181 
182  static pqWebView* newInstance(QHelpEngine* engine, QWidget* parentObject)
183  {
184  pqWebView* instance = new pqWebView(parentObject);
185  QNetworkAccessManager *oldManager = instance->page()->networkAccessManager();
186  pqNetworkAccessManager* newManager = new pqNetworkAccessManager(engine, oldManager, parentObject);
187  instance->page()->setNetworkAccessManager(newManager);
188  instance->page()->setForwardUnsupportedContent(false);
189  return instance;
190  }
191 
192 private:
193  Q_DISABLE_COPY(pqWebView)
194 };
195 
196 } // end of namespace
197 #endif