6#include <QtCore/QFileInfo>
7#include <QtCore/QLibraryInfo>
8#include <QtCore/QLocale>
9#include <QtCore/QStringList>
10#include <QtCore/QTranslator>
13#include <QtWidgets/QApplication>
14#include <QtGui/QDesktopServices>
16#include <QtHelp/QHelpEngine>
17#include <QtHelp/QHelpSearchEngine>
19#include <QtSql/QSqlDatabase>
21#if defined(BROWSER_QTWEBKIT)
23#include <QWebSettings>
26#include "../shared/collectionconfiguration.h"
37using namespace Qt::StringLiterals;
42updateLastPagesOnUnregister(QHelpEngineCore& helpEngine,
const QString& nsName)
46 QStringList currentPages = CollectionConfiguration::lastShownPages(helpEngine);
47 if (!currentPages.isEmpty()) {
48 QStringList zoomList = CollectionConfiguration::lastZoomFactors(helpEngine);
49 while (zoomList.size() < currentPages.size())
50 zoomList.append(CollectionConfiguration::DefaultZoomFactor);
52 for (
int i = currentPages.size(); --i >= 0;) {
53 if (QUrl(currentPages.at(i)).host() == nsName) {
55 currentPages.removeAt(i);
56 lastPage = (lastPage == (i + 1)) ? 1 : lastPage;
60 CollectionConfiguration::setLastShownPages(helpEngine, currentPages);
62 CollectionConfiguration::setLastZoomFactors(helpEngine, zoomList);
66void stripNonexistingDocs(QHelpEngineCore& collection)
69 const QStringList &namespaces = collection.registeredDocumentations();
70 for (
const QString &ns : namespaces) {
71 QFileInfo fi(collection.documentationFileName(ns));
72 if (!fi.exists() || !fi.isFile())
73 collection.unregisterDocumentation(ns);
77QString indexFilesFolder(
const QString &collectionFile)
80 QString indexFilesFolder =
".fulltextsearch"_L1;
81 if (!collectionFile.isEmpty()) {
82 QFileInfo fi(collectionFile);
83 indexFilesFolder = u'.' + fi.fileName().left(fi.fileName().lastIndexOf(
".qhc"_L1));
85 return indexFilesFolder;
89
90
91
92
93QString constructCachedCollectionFilePath(
const QHelpEngineCore &collection)
96 const QString &filePath = collection.collectionFile();
97 const QString &fileName = QFileInfo(filePath).fileName();
98 const QString &cacheDir = CollectionConfiguration::cacheDir(collection);
99 const QString &dir = !cacheDir.isEmpty()
100 && CollectionConfiguration::cacheDirIsRelativeToCollection(collection)
101 ? QFileInfo(filePath).dir().absolutePath()
102 + QDir::separator() + cacheDir
103 : MainWindow::collectionFileDirectory(
false, cacheDir);
104 return dir + QDir::separator() + fileName;
107bool synchronizeDocs(QHelpEngineCore &collection,
108 QHelpEngineCore &cachedCollection,
112 const QDateTime &lastCollectionRegisterTime =
113 CollectionConfiguration::lastRegisterTime(collection);
114 if (!lastCollectionRegisterTime.isValid() || lastCollectionRegisterTime
115 < CollectionConfiguration::lastRegisterTime(cachedCollection))
118 const QStringList &docs = collection.registeredDocumentations();
119 const QStringList &cachedDocs = cachedCollection.registeredDocumentations();
122
123
124
125 for (
const QString &doc : docs) {
126 if (!cachedDocs.contains(doc)) {
127 const QString &docFile = collection.documentationFileName(doc);
128 if (!cachedCollection.registerDocumentation(docFile)) {
129 cmd.showMessage(QCoreApplication::translate(
"Assistant",
130 "Error registering documentation file '%1': %2").
131 arg(docFile, cachedCollection.error()),
true);
142bool removeSearchIndex(
const QString &collectionFile)
146 QFileInfo(collectionFile).path() + u'/' + indexFilesFolder(collectionFile);
152 const QStringList &list = dir.entryList(QDir::Files | QDir::Hidden);
153 for (
const QString &item : list)
158QCoreApplication* createApplication(
int &argc,
char *argv[])
163 const char * cmdModeArgs[] = {
164 "-help",
"-register",
"-unregister",
"-remove-search-index",
165 "-rebuild-search-index"
167 for (
int i = 1; i < argc; ++i) {
168 for (size_t j = 0; j <
sizeof cmdModeArgs/
sizeof *cmdModeArgs; ++j) {
169 if (strcmp(argv[i], cmdModeArgs[j]) == 0)
170 return new QCoreApplication(argc, argv);
174 QApplication *app =
new QApplication(argc, argv);
175 app->connect(app, &QGuiApplication::lastWindowClosed, app,
176 &QCoreApplication::quit);
180bool registerDocumentation(QHelpEngineCore &collection,
CmdLineParser &cmd,
184 if (!collection.registerDocumentation(cmd.helpFile())) {
185 cmd.showMessage(QCoreApplication::translate(
"Assistant",
186 "Could not register documentation file\n%1\n\nReason:\n%2")
187 .arg(cmd.helpFile(), collection.error()),
true);
191 cmd.showMessage(QCoreApplication::translate(
"Assistant",
192 "Documentation successfully registered."),
198bool unregisterDocumentation(QHelpEngineCore &collection,
199 const QString &namespaceName,
CmdLineParser &cmd,
bool printSuccess)
202 if (!collection.unregisterDocumentation(namespaceName)) {
203 cmd.showMessage(QCoreApplication::translate(
"Assistant",
204 "Could not unregister documentation"
205 " file\n%1\n\nReason:\n%2").
206 arg(cmd.helpFile(), collection.error()),
true);
209 updateLastPagesOnUnregister(collection, namespaceName);
211 cmd.showMessage(QCoreApplication::translate(
"Assistant",
212 "Documentation successfully unregistered."),
217void setupTranslation(
const QString &fileName,
const QString &dir)
219 QTranslator *translator =
new QTranslator(QCoreApplication::instance());
220 if (translator->load(QLocale(), fileName,
"_"_L1, dir))
221 QCoreApplication::installTranslator(translator);
224void setupTranslations()
227 const QString &resourceDir
228 = QLibraryInfo::path(QLibraryInfo::TranslationsPath);
229 setupTranslation(
"assistant"_L1, resourceDir);
230 setupTranslation(
"qt"_L1, resourceDir);
231 setupTranslation(
"qt_help"_L1, resourceDir);
245
246
247
248
249 const QString collectionFile = cmd->collectionFile();
250 const bool collectionFileGiven = !collectionFile.isEmpty();
251 std::unique_ptr<QHelpEngineCore> collection;
252 if (collectionFileGiven) {
253 collection.reset(
new QHelpEngineCore(collectionFile));
254 if (!collection->setupData()) {
255 cmd->showMessage(QCoreApplication::translate(
"Assistant",
256 "Error reading collection file '%1': %2.")
257 .arg(collectionFile, collection->error()),
true);
261 const QString &cachedCollectionFile = collectionFileGiven
262 ? constructCachedCollectionFilePath(*collection)
263 : MainWindow::defaultHelpCollectionFileName();
264 if (collectionFileGiven && !QFileInfo(cachedCollectionFile).exists()
265 && !collection->copyCollectionFile(cachedCollectionFile)) {
266 cmd->showMessage(QCoreApplication::translate(
"Assistant",
267 "Error creating collection file '%1': %2.")
268 .arg(cachedCollectionFile, collection->error()),
true);
271 QHelpEngineCore cachedCollection(cachedCollectionFile);
272 if (!cachedCollection.setupData()) {
273 cmd->showMessage(QCoreApplication::translate(
"Assistant",
274 "Error reading collection file '%1': %2.")
275 .arg(cachedCollectionFile, cachedCollection.error()),
true);
279 stripNonexistingDocs(cachedCollection);
280 if (collectionFileGiven) {
284 if (!synchronizeDocs(*collection, cachedCollection, *cmd))
289 const QStringList &cachedDocs =
290 cachedCollection.registeredDocumentations();
291 const QString &namespaceName =
292 QHelpEngineCore::namespaceName(cmd->helpFile());
294 if (collectionFileGiven
295 && !registerDocumentation(*collection, *cmd,
true))
297 if (!cachedDocs.contains(namespaceName)
298 && !registerDocumentation(cachedCollection, *cmd, !collectionFileGiven))
303 if (collectionFileGiven
304 && !unregisterDocumentation(*collection, namespaceName, *cmd,
true))
306 if (cachedDocs.contains(namespaceName)
307 && !unregisterDocumentation(cachedCollection, namespaceName,
308 *cmd, !collectionFileGiven))
315 return removeSearchIndex(cachedCollectionFile)
316 ? ExitSuccess : ExitFailure;
319 if (!QSqlDatabase::isDriverAvailable(
"QSQLITE"_L1)) {
320 cmd->showMessage(QCoreApplication::translate(
"Assistant",
321 "Cannot load sqlite database driver!"),
326 if (!cmd->currentFilter().isEmpty()) {
327 if (collectionFileGiven)
328 collection->setCurrentFilter(cmd->currentFilter());
329 cachedCollection.setCurrentFilter(cmd->currentFilter());
332 if (collectionFileGiven)
333 cmd->setCollectionFile(cachedCollectionFile);
338int main(
int argc,
char *argv[])
341 std::unique_ptr<QCoreApplication> a(createApplication(argc, argv));
342#if QT_CONFIG(library)
343 a->addLibraryPath(a->applicationDirPath() +
"/plugins"_L1);
347#if defined(BROWSER_QTWEBKIT)
348 if (qobject_cast<QApplication *>(a.data())) {
350 f.setStyleHint(QFont::SansSerif);
351 QWebSettings::globalSettings()->setFontFamily(QWebSettings::StandardFont, f.defaultFamily());
357 CmdLineParser::Result res = cmd.parse();
358 if (res == CmdLineParser::Help)
360 else if (res == CmdLineParser::Error)
365 case ExitFailure:
return EXIT_FAILURE;
366 case ExitSuccess:
return EXIT_SUCCESS;
374
375
376
377
378 const int retval = a->exec();
RegisterState registerRequest() const
bool removeSearchIndex() const
static bool isNewer(const QHelpEngineCore &newer, const QHelpEngineCore &older)
static void setLastTabPage(QHelpEngineCore &helpEngine, int lastPage)
static void copyConfiguration(const QHelpEngineCore &source, QHelpEngineCore &target)
static void updateLastRegisterTime(QHelpEngineCore &helpEngine)
static int lastTabPage(const QHelpEngineCore &helpEngine)
static void removeInstance()
static ExitStatus preliminarySetup(CmdLineParser *cmd)
int main(int argc, char *argv[])
[ctor_close]