Debian Codegraph @

src/Gui/Assistant.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2008 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 #include <QCoreApplication>
24 #include <QDir>
25 #include <QFileInfo>
26 #include <QLibraryInfo>
27 #include <QMessageBox>
28 #include <QProcess>
29 #include <QTextStream>
30
31 #include <App/Application.h>
32 #include <Base/Console.h>
33 #include "Assistant.h"
34
35 using namespace Gui;
36
37 /* TRANSLATOR Gui::Assistant */
38
39 Assistant::Assistant()
40 : proc(nullptr)
41 {}
42
43 Assistant::~Assistant()
44 {
45 if (proc && proc->state() == QProcess::Running) {
46 proc->terminate();
47 proc->waitForFinished(3000);
48 }
49 }
50
51 void Assistant::showDocumentation(const QString& page)
52 {
53 if (!startAssistant()) {
54 return;
55 }
56 if (!page.isEmpty()) {
57 QTextStream str(proc);
58 str << QLatin1String("setSource qthelp://org.freecad.usermanual/doc/") << page
59 << QLatin1String("\n\n");
60 }
61 }
62
63 bool Assistant::startAssistant()
64 {
65 if (!proc) {
66 proc = new QProcess();
67 connect(proc, &QProcess::readyReadStandardOutput, this, &Assistant::readyReadStandardOutput);
68 connect(proc, &QProcess::readyReadStandardError, this, &Assistant::readyReadStandardError);
69 }
70
71 if (proc->state() != QProcess::Running) {
72 #ifdef Q_OS_WIN
73 QString app;
74 app = QDir::toNativeSeparators(
75 QString::fromStdString(App::Application::getHomePath()) + QLatin1String("bin/")
76 );
77 #elif defined(Q_OS_MACOS)
78 QString app = QCoreApplication::applicationDirPath() + QDir::separator();
79 #else
80 # if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
81 QString app = QLibraryInfo::location(QLibraryInfo::BinariesPath) + QDir::separator();
82 # else
83 QString app = QLibraryInfo::path(QLibraryInfo::BinariesPath) + QDir::separator();
84 # endif
85 #endif
86 app += QLatin1String("assistant");
87
88 // get the name of the executable and the doc path
89 QString exe = QString::fromStdString(App::Application::getExecutableName());
90 QString doc = QString::fromStdString(App::Application::getHelpDir());
91 QString qhc = doc + exe.toLower() + QLatin1String(".qhc");
92
93
94 QFileInfo fi(qhc);
95 if (!fi.isReadable()) {
96 QMessageBox::critical(
97 nullptr,
98 tr("%1 Help").arg(exe),
99 tr("%1 help files not found (%2). You might need to install the %1 documentation "
100 "package.")
101 .arg(exe, qhc)
102 );
103 return false;
104 }
105
106 static bool first = true;
107 if (first) {
108 Base::Console().log("Help file at %s\n", (const char*)qhc.toUtf8());
109 first = false;
110 }
111
112 // AppImage start
113 // AppImage mount location changes on each start. Assistant caches freecad.qhc
114 // file and sets an absolute path. As a result embedded documentation only works
115 // on the first AppImage (help) run. Register the .gch file, to overcome the issue.
116 static bool start = true;
117 if (start) {
118 char* appimage = getenv("APPIMAGE");
119 if (appimage) {
120 QString qch = doc + exe.toLower() + QLatin1String(".qch");
121 QFileInfo fi(qch);
122 if (fi.isReadable()) {
123 // Assume documentation is embedded
124 // Unregister qch file (path) from previous AppImage run
125 QStringList args;
126
127 args << QLatin1String("-collectionFile") << qhc << QLatin1String("-unregister")
128 << qch;
129
130 proc->start(app, args);
131
132 if (!proc->waitForFinished(50000)) {
133 QMessageBox::critical(
134 nullptr,
135 tr("%1 Help").arg(exe),
136 tr("Unable to launch Qt Assistant (%1)").arg(app)
137 );
138 return false;
139 }
140
141 // Register qch file (path) for current AppImage run
142 args.clear();
143
144 args << QLatin1String("-collectionFile") << qhc << QLatin1String("-register")
145 << qch;
146
147 proc->start(app, args);
148
149 if (!proc->waitForFinished(50000)) {
150 QMessageBox::critical(
151 nullptr,
152 tr("%1 Help").arg(exe),
153 tr("Unable to launch Qt Assistant (%1)").arg(app)
154 );
155 return false;
156 }
157 }
158 }
159 start = false;
160 }
161 // AppImage end
162
163 QStringList args;
164
165 args << QLatin1String("-collectionFile") << qhc << QLatin1String("-enableRemoteControl");
166
167 proc->start(app, args);
168
169 if (!proc->waitForStarted()) {
170 QMessageBox::critical(
171 nullptr,
172 tr("%1 Help").arg(exe),
173 tr("Unable to launch Qt Assistant (%1)").arg(app)
174 );
175 return false;
176 }
177 }
178
179 return true;
180 }
181
182 void Assistant::readyReadStandardOutput()
183 {
184 QByteArray data = proc->readAllStandardOutput();
185 Base::Console().log("Help view: %s\n", data.constData());
186 }
187
188 void Assistant::readyReadStandardError()
189 {
190 QByteArray data = proc->readAllStandardError();
191 Base::Console().log("Help view: %s\n", data.constData());
192 }
193
194 #include "moc_Assistant.cpp"