| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2007 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 |
* GNU Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
|
| 24 |
#include <QApplication> |
| 25 |
#include <QBuffer> |
| 26 |
#include <QImageWriter> |
| 27 |
#include <QMessageBox> |
| 28 |
#include <QTcpSocket> |
| 29 |
|
| 30 |
|
| 31 |
#include <Base/Interpreter.h> |
| 32 |
#include <Base/Exception.h> |
| 33 |
|
| 34 |
#include "OnlineDocumentation.h" |
| 35 |
#include "MainWindow.h" |
| 36 |
|
| 37 |
|
| 38 |
using namespace Gui; |
| 39 |
|
| 40 |
// the favicon |
| 41 |
// clang-format off |
| 42 |
// NOLINTBEGIN |
| 43 |
static const unsigned int navicon_data_len = 318; |
| 44 |
static const unsigned char navicon_data[] = { |
| 45 |
0x00,0x00,0x01,0x00,0x01,0x00,0x10,0x10,0x10,0x00,0x01,0x00,0x04,0x00, |
| 46 |
0x28,0x01,0x00,0x00,0x16,0x00,0x00,0x00,0x28,0x00,0x00,0x00,0x10,0x00, |
| 47 |
0x00,0x00,0x20,0x00,0x00,0x00,0x01,0x00,0x04,0x00,0x00,0x00,0x00,0x00, |
| 48 |
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
| 49 |
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0x00, |
| 50 |
0x84,0x82,0x84,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
| 51 |
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
| 52 |
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
| 53 |
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
| 54 |
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x11,0x11,0x00, |
| 55 |
0x00,0x00,0x00,0x00,0x01,0x10,0x01,0x10,0x00,0x00,0x00,0x00,0x11,0x00, |
| 56 |
0x00,0x10,0x00,0x00,0x00,0x00,0x11,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
| 57 |
0x11,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x11,0x00,0x00,0x00,0x00,0x00, |
| 58 |
0x00,0x00,0x11,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x11,0x00,0x00,0x10, |
| 59 |
0x00,0x00,0x00,0x00,0x01,0x10,0x01,0x10,0x00,0x20,0x00,0x00,0x00,0x11, |
| 60 |
0x11,0x00,0x00,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x00,0x00, |
| 61 |
0x00,0x00,0x00,0x00,0x00,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20, |
| 62 |
0x00,0x00,0x00,0x00,0x02,0x22,0x22,0x20,0x00,0x00,0x00,0x00,0x00,0x00, |
| 63 |
0x00,0x00,0xff,0xff,0x00,0x00,0xfc,0x3f,0x00,0x00,0xf9,0x9f,0x00,0x00, |
| 64 |
0x93,0xdf,0x00,0x00,0x93,0xff,0x00,0x00,0x93,0xff,0x00,0x00,0x93,0xff, |
| 65 |
0x00,0x00,0x93,0xfd,0x00,0x00,0x81,0xd8,0x00,0x00,0x99,0x9d,0x00,0x00, |
| 66 |
0x9c,0x3d,0x00,0x00,0x9f,0xfd,0x00,0x00,0x80,0xfd,0x00,0x00,0xff,0x7d, |
| 67 |
0x00,0x00,0xfe,0x01,0x00,0x00,0xff,0x7f,0x00,0x00}; |
| 68 |
// NOLINTEND |
| 69 |
// clang-format on |
| 70 |
|
| 71 |
PythonOnlineHelp::PythonOnlineHelp() = default; |
| 72 |
|
| 73 |
PythonOnlineHelp::~PythonOnlineHelp() = default; |
| 74 |
|
| 75 |
QByteArray PythonOnlineHelp::loadResource(const QString& filename) const |
| 76 |
{ |
| 77 |
if (filename == QLatin1String("/favicon.ico")) { |
| 78 |
return loadFavicon(); |
| 79 |
} |
| 80 |
|
| 81 |
if (filename == QLatin1String("/")) { |
| 82 |
return loadIndexPage(); |
| 83 |
} |
| 84 |
|
| 85 |
return loadHelpPage(filename); |
| 86 |
} |
| 87 |
|
| 88 |
QByteArray PythonOnlineHelp::loadFavicon() const |
| 89 |
{ |
| 90 |
// Return a resource icon in ico format |
| 91 |
QByteArray res; |
| 92 |
QBuffer buffer; |
| 93 |
buffer.open(QBuffer::WriteOnly); |
| 94 |
QImageWriter writer; |
| 95 |
writer.setDevice(&buffer); |
| 96 |
writer.setFormat("ICO"); |
| 97 |
if (writer.canWrite()) { |
| 98 |
const int size = 24; |
| 99 |
QPixmap px = qApp->windowIcon().pixmap(size, size); // NOLINT |
| 100 |
writer.write(px.toImage()); |
| 101 |
buffer.close(); |
| 102 |
res = buffer.data(); |
| 103 |
} |
| 104 |
else { |
| 105 |
// fallback |
| 106 |
res.reserve(navicon_data_len); |
| 107 |
for (int i = 0; i < (int)navicon_data_len; i++) { |
| 108 |
res[i] = navicon_data[i]; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
return res; |
| 113 |
} |
| 114 |
|
| 115 |
QByteArray PythonOnlineHelp::invoke(const std::function<std::string(Py::Module&)>& func) const |
| 116 |
{ |
| 117 |
// get the global interpreter lock otherwise the app may crash with the error |
| 118 |
// 'PyThreadState_Get: no current thread' (see pystate.c) |
| 119 |
Base::PyGILStateLocker lock; |
| 120 |
try { |
| 121 |
return tryInvoke(func); |
| 122 |
} |
| 123 |
catch (const Py::Exception&) { |
| 124 |
// load the error page |
| 125 |
Base::PyException e; |
| 126 |
e.reportException(); |
| 127 |
return loadFailed(QString::fromUtf8(e.what())); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
QByteArray PythonOnlineHelp::tryInvoke(const std::function<std::string(Py::Module&)>& func) const |
| 132 |
{ |
| 133 |
PyObject* module = PyImport_ImportModule("freecad.freecad_doc"); |
| 134 |
if (!module) { |
| 135 |
throw Py::Exception(); |
| 136 |
} |
| 137 |
|
| 138 |
Py::Module mod(module, true); |
| 139 |
std::string contents = func(mod); |
| 140 |
QByteArray res; |
| 141 |
res.append("HTTP/1.0 200 OK\n"); |
| 142 |
res.append("Content-type: text/html\n"); |
| 143 |
res.append(contents.c_str()); |
| 144 |
return res; |
| 145 |
} |
| 146 |
|
| 147 |
QByteArray PythonOnlineHelp::loadIndexPage() const |
| 148 |
{ |
| 149 |
return invoke([](Py::Module& mod) { |
| 150 |
Py::String output(mod.callMemberFunction("getIndex")); |
| 151 |
return static_cast<std::string>(output); |
| 152 |
}); |
| 153 |
} |
| 154 |
|
| 155 |
QByteArray PythonOnlineHelp::loadHelpPage(const QString& filename) const |
| 156 |
{ |
| 157 |
return invoke([filename](Py::Module& mod) { |
| 158 |
QString fn = filename.mid(1); |
| 159 |
QString name = fn.left(fn.length() - 5); |
| 160 |
|
| 161 |
Py::Tuple args(1); |
| 162 |
args.setItem(0, Py::String(name.toStdString())); |
| 163 |
Py::String output(mod.callMemberFunction("getPage", args)); |
| 164 |
return static_cast<std::string>(output); |
| 165 |
}); |
| 166 |
} |
| 167 |
|
| 168 |
QByteArray PythonOnlineHelp::fileNotFound() const |
| 169 |
{ |
| 170 |
const int pageNotFound = 404; |
| 171 |
QString contentType = QStringLiteral( |
| 172 |
"text/html\r\n" |
| 173 |
"\r\n" |
| 174 |
"<html><head><title>Error</title></head>" |
| 175 |
"<body bgcolor=\"#f0f0f8\">" |
| 176 |
"<table width=\"100%\" cellspacing=0 cellpadding=2 border=0 summary=\"heading\">" |
| 177 |
"<tr bgcolor=\"#7799ee\">" |
| 178 |
"<td valign=bottom> <br>" |
| 179 |
"<font color=\"#ffffff\" face=\"helvetica, arial\"> <br><big><big><strong>FreeCAD " |
| 180 |
"Documentation</strong></big></big></font></td>" |
| 181 |
"<td align=right valign=bottom>" |
| 182 |
"<font color=\"#ffffff\" face=\"helvetica, arial\"> </font></td></tr></table>" |
| 183 |
"<p><p>" |
| 184 |
"<h1>404 - File not found</h1>" |
| 185 |
"<div><p><strong>The requested URL was not found on this server." |
| 186 |
"</strong></p>" |
| 187 |
"</div></body>" |
| 188 |
"</html>" |
| 189 |
"\r\n" |
| 190 |
); |
| 191 |
|
| 192 |
QString header = QStringLiteral("content-type: %1\r\n").arg(contentType); |
| 193 |
|
| 194 |
QString http(QLatin1String("HTTP/1.1 %1 %2\r\n%3\r\n")); |
| 195 |
QString httpResponseHeader = http.arg(pageNotFound).arg(QStringLiteral("File not found"), header); |
| 196 |
|
| 197 |
QByteArray res = httpResponseHeader.toLatin1(); |
| 198 |
return res; |
| 199 |
} |
| 200 |
|
| 201 |
QByteArray PythonOnlineHelp::loadFailed(const QString& error) const |
| 202 |
{ |
| 203 |
const int pageNotFound = 404; |
| 204 |
QString contentType |
| 205 |
= QStringLiteral( |
| 206 |
"text/html\r\n" |
| 207 |
"\r\n" |
| 208 |
"<html><head><title>Error</title></head>" |
| 209 |
"<body bgcolor=\"#f0f0f8\">" |
| 210 |
"<table width=\"100%\" cellspacing=0 cellpadding=2 border=0 summary=\"heading\">" |
| 211 |
"<tr bgcolor=\"#7799ee\">" |
| 212 |
"<td valign=bottom> <br>" |
| 213 |
"<font color=\"#ffffff\" face=\"helvetica, " |
| 214 |
"arial\"> <br><big><big><strong>FreeCAD " |
| 215 |
"Documentation</strong></big></big></font></td>" |
| 216 |
"<td align=right valign=bottom>" |
| 217 |
"<font color=\"#ffffff\" face=\"helvetica, arial\"> </font></td></tr></table>" |
| 218 |
"<p><p>" |
| 219 |
"<h1>%1</h1>" |
| 220 |
"</body>" |
| 221 |
"</html>" |
| 222 |
"\r\n" |
| 223 |
) |
| 224 |
.arg(error); |
| 225 |
|
| 226 |
QString header = QStringLiteral("content-type: %1\r\n").arg(contentType); |
| 227 |
|
| 228 |
QString http(QLatin1String("HTTP/1.1 %1 %2\r\n%3\r\n")); |
| 229 |
QString httpResponseHeader = http.arg(pageNotFound).arg(QStringLiteral("File not found"), header); |
| 230 |
|
| 231 |
QByteArray res = httpResponseHeader.toLatin1(); |
| 232 |
return res; |
| 233 |
} |
| 234 |
|
| 235 |
HttpServer::HttpServer(QObject* parent) |
| 236 |
: QTcpServer(parent) |
| 237 |
, disabled(false) |
| 238 |
{} |
| 239 |
|
| 240 |
void HttpServer::incomingConnection(qintptr socket) |
| 241 |
{ |
| 242 |
if (disabled) { |
| 243 |
return; |
| 244 |
} |
| 245 |
|
| 246 |
// When a new client connects the server constructs a QTcpSocket and all |
| 247 |
// communication with the client is done over this QTcpSocket. QTcpSocket |
| 248 |
// works asynchronously, this means that all the communication is done |
| 249 |
// in the two slots readClient() and discardClient(). |
| 250 |
auto s = new QTcpSocket(this); |
| 251 |
connect(s, &QTcpSocket::readyRead, this, &HttpServer::readClient); |
| 252 |
connect(s, &QTcpSocket::disconnected, this, &HttpServer::discardClient); |
| 253 |
s->setSocketDescriptor(socket); |
| 254 |
} |
| 255 |
|
| 256 |
void HttpServer::pause() |
| 257 |
{ |
| 258 |
disabled = true; |
| 259 |
} |
| 260 |
|
| 261 |
void HttpServer::resume() |
| 262 |
{ |
| 263 |
disabled = false; |
| 264 |
} |
| 265 |
|
| 266 |
void HttpServer::readClient() |
| 267 |
{ |
| 268 |
if (disabled) { |
| 269 |
return; |
| 270 |
} |
| 271 |
|
| 272 |
// This slot is called when the client sent data to the server. The |
| 273 |
// server looks if it was a GET request and sends back the |
| 274 |
// corresponding HTML document from the ZIP file. |
| 275 |
auto socket = qobject_cast<QTcpSocket*>(sender()); |
| 276 |
if (socket && socket->canReadLine()) { |
| 277 |
// NOLINTBEGIN |
| 278 |
QString httpRequestHeader = QString::fromLatin1(socket->readLine()); |
| 279 |
QStringList lst = httpRequestHeader.simplified().split(QLatin1String(" ")); |
| 280 |
QString method; |
| 281 |
QString path; |
| 282 |
if (lst.count() > 0) { |
| 283 |
QString m = lst[0]; |
| 284 |
if (lst.count() > 1) { |
| 285 |
QString p = lst[1]; |
| 286 |
if (lst.count() > 2) { |
| 287 |
QString v = lst[2]; |
| 288 |
if (v.length() >= 8 && v.left(5) == QLatin1String("HTTP/") && v[5].isDigit() |
| 289 |
&& v[6] == QLatin1Char('.') && v[7].isDigit()) { |
| 290 |
method = m; |
| 291 |
path = p; |
| 292 |
} |
| 293 |
} |
| 294 |
} |
| 295 |
} |
| 296 |
// NOLINTEND |
| 297 |
|
| 298 |
if (method == QLatin1String("GET")) { |
| 299 |
socket->write(help.loadResource(path)); |
| 300 |
socket->close(); |
| 301 |
if (socket->state() == QTcpSocket::UnconnectedState) { |
| 302 |
// mark the socket for deletion but do not destroy immediately |
| 303 |
socket->deleteLater(); |
| 304 |
} |
| 305 |
} |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
void HttpServer::discardClient() |
| 310 |
{ |
| 311 |
if (auto socket = qobject_cast<QTcpSocket*>(sender())) { |
| 312 |
socket->deleteLater(); |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
// -------------------------------------------------------------------- |
| 317 |
|
| 318 |
/* TRANSLATOR Gui::StdCmdPythonHelp */ |
| 319 |
|
| 320 |
StdCmdPythonHelp::StdCmdPythonHelp() |
| 321 |
: Command("Std_PythonHelp") |
| 322 |
, server(nullptr) |
| 323 |
{ |
| 324 |
sGroup = "Tools"; |
| 325 |
sMenuText = QT_TR_NOOP("Python &Modules Documentation"); |
| 326 |
sToolTipText = QT_TR_NOOP("Opens the Python Modules documentation"); |
| 327 |
sWhatsThis = "Std_PythonHelp"; |
| 328 |
sStatusTip = sToolTipText; |
| 329 |
sPixmap = "applications-python"; |
| 330 |
} |
| 331 |
|
| 332 |
StdCmdPythonHelp::~StdCmdPythonHelp() |
| 333 |
{ |
| 334 |
if (server) { |
| 335 |
server->close(); |
| 336 |
delete server; |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
void StdCmdPythonHelp::activated(int iMsg) |
| 341 |
{ |
| 342 |
Q_UNUSED(iMsg); |
| 343 |
// try to open a connection over this port |
| 344 |
const qint16 port = 7465; |
| 345 |
if (!this->server) { |
| 346 |
this->server = new HttpServer(); |
| 347 |
} |
| 348 |
|
| 349 |
// if server is not yet running try to open one |
| 350 |
if (this->server->isListening() |
| 351 |
|| this->server->listen(QHostAddress(QHostAddress::LocalHost), port)) { |
| 352 |
std::string url = "http://localhost:"; |
| 353 |
url += std::to_string(port); |
| 354 |
OpenURLInBrowser(url.c_str()); |
| 355 |
} |
| 356 |
else { |
| 357 |
QMessageBox::critical( |
| 358 |
Gui::getMainWindow(), |
| 359 |
QObject::tr("No Server"), |
| 360 |
QObject::tr("Unable to start the server to port %1: %2.").arg(port).arg(server->errorString()) |
| 361 |
); |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
bool Gui::OpenURLInBrowser(const char* URL) |
| 366 |
{ |
| 367 |
// The webbrowser Python module allows one to start the system browser in an OS-independent way |
| 368 |
Base::PyGILStateLocker lock; |
| 369 |
try { |
| 370 |
PyObject* module = PyImport_ImportModule("webbrowser"); |
| 371 |
if (module) { |
| 372 |
Py::Module mod(module, true); |
| 373 |
mod.callMemberFunction("open", Py::TupleN(Py::String(URL))); |
| 374 |
return true; |
| 375 |
} |
| 376 |
|
| 377 |
throw Py::Exception(); |
| 378 |
} |
| 379 |
catch (Py::Exception& e) { |
| 380 |
e.clear(); |
| 381 |
QMessageBox::critical( |
| 382 |
Gui::getMainWindow(), |
| 383 |
QObject::tr("No Browser"), |
| 384 |
QObject::tr("Unable to open your system browser.") |
| 385 |
); |
| 386 |
return false; |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
#include "moc_OnlineDocumentation.cpp" |