From 079f090ffbb9af349b376d3a080304228eab43e6 Mon Sep 17 00:00:00 2001 From: ck Date: Thu, 19 Nov 2009 16:14:54 +0100 Subject: Assistant: Check qch files for update. We install watchers on the current collection's help files and react accordingly when they are deleted or replaced. Note that this has no effect on Windows, because opened files cannot be written or deleted by another application. Task-number: QTBUG-2348 Reviewed-by: kh1 --- tools/assistant/tools/assistant/mainwindow.cpp | 57 +++++++++++++++++++--- tools/assistant/tools/assistant/mainwindow.h | 3 ++ .../tools/assistant/preferencesdialog.cpp | 22 ++++++--- .../assistant/tools/assistant/preferencesdialog.h | 5 +- tools/assistant/tools/assistant/qtdocinstaller.cpp | 18 +++++-- tools/assistant/tools/assistant/qtdocinstaller.h | 4 +- tools/assistant/tools/assistant/remotecontrol.cpp | 22 ++++++--- tools/assistant/tools/assistant/remotecontrol.h | 5 +- 8 files changed, 112 insertions(+), 24 deletions(-) diff --git a/tools/assistant/tools/assistant/mainwindow.cpp b/tools/assistant/tools/assistant/mainwindow.cpp index bc8b86f..9a2a5bd 100644 --- a/tools/assistant/tools/assistant/mainwindow.cpp +++ b/tools/assistant/tools/assistant/mainwindow.cpp @@ -57,6 +57,7 @@ #include #include #include +#include #include #include #include @@ -93,6 +94,7 @@ MainWindow::MainWindow(CmdLineParser *cmdLine, QWidget *parent) , m_cmdLine(cmdLine) , m_progressWidget(0) , m_qtDocInstaller(0) + , m_qchWatcher(new QFileSystemWatcher(this)) , m_connectedInitSignals(false) { setToolButtonStyle(Qt::ToolButtonFollowStyle); @@ -198,7 +200,7 @@ MainWindow::MainWindow(CmdLineParser *cmdLine, QWidget *parent) QTimer::singleShot(0, this, SLOT(insertLastPages())); if (m_cmdLine->enableRemoteControl()) - (void)new RemoteControl(this, m_helpEngine); + (void)new RemoteControl(this, m_helpEngine, m_qchWatcher); if (m_cmdLine->contents() == CmdLineParser::Show) showContents(); @@ -237,6 +239,13 @@ MainWindow::MainWindow(CmdLineParser *cmdLine, QWidget *parent) QTimer::singleShot(0, this, SLOT(lookForNewQtDocumentation())); else checkInitState(); + + foreach(const QString &ns, m_helpEngine->registeredDocumentations()) { + const QString &docFile = m_helpEngine->documentationFileName(ns); + m_qchWatcher->addPath(docFile); + connect(m_qchWatcher, SIGNAL(fileChanged(QString)), this, + SLOT(qchFileChanged(QString))); + } } setTabPosition(Qt::AllDockWidgetAreas, QTabWidget::North); } @@ -297,8 +306,11 @@ bool MainWindow::initHelpDB() } QHelpEngineCore hc(fi.absoluteFilePath()); hc.setupData(); - hc.unregisterDocumentation(intern); - hc.registerDocumentation(helpFile); + const QString internalFile = hc.documentationFileName(intern); + if (hc.unregisterDocumentation(intern)) + m_qchWatcher->removePath(internalFile); + if (hc.registerDocumentation(helpFile)) + m_qchWatcher->addPath(helpFile); needsSetup = true; } @@ -317,14 +329,18 @@ bool MainWindow::initHelpDB() needsSetup = true; } - if (needsSetup) + if (needsSetup) { m_helpEngine->setupData(); + Q_ASSERT(m_qchWatcher->files().count() + == m_helpEngine->registeredDocumentations().count()); + } return true; } void MainWindow::lookForNewQtDocumentation() { - m_qtDocInstaller = new QtDocInstaller(m_helpEngine->collectionFile()); + m_qtDocInstaller = + new QtDocInstaller(m_helpEngine->collectionFile(), m_qchWatcher); connect(m_qtDocInstaller, SIGNAL(errorMessage(QString)), this, SLOT(displayInstallationError(QString))); connect(m_qtDocInstaller, SIGNAL(docsInstalled(bool)), this, @@ -783,7 +799,7 @@ void MainWindow::showTopicChooser(const QMap &links, void MainWindow::showPreferences() { - PreferencesDialog dia(m_helpEngine, this); + PreferencesDialog dia(m_helpEngine, m_qchWatcher, this); connect(&dia, SIGNAL(updateApplicationFont()), this, SLOT(updateApplicationFont())); @@ -1125,4 +1141,33 @@ void MainWindow::currentFilterChanged(const QString &filter) m_filterCombo->setCurrentIndex(index); } +void MainWindow::qchFileChanged(const QString &fileName) +{ + /* + * We don't use QHelpEngineCore::namespaceName(fileName), because the file + * may not exist anymore or contain a different namespace. + */ + QString ns; + foreach (const QString &curNs, m_helpEngine->registeredDocumentations()) { + if (m_helpEngine->documentationFileName(curNs) == fileName) { + ns = curNs; + break; + } + } + + /* + * We can't do an assertion here, because QFileSystemWatcher may send the + * signal more than once. + */ + if (ns.isEmpty()) + return; + + CentralWidget* widget = CentralWidget::instance(); + widget->closeTabs(widget->currentSourceFileList().keys(ns)); + if (m_helpEngine->unregisterDocumentation(ns) && + (!QFileInfo(fileName).exists() + || !m_helpEngine->registerDocumentation(fileName))) + m_qchWatcher->removePath(fileName); +} + QT_END_NAMESPACE diff --git a/tools/assistant/tools/assistant/mainwindow.h b/tools/assistant/tools/assistant/mainwindow.h index 96daf99..6a0ab58 100644 --- a/tools/assistant/tools/assistant/mainwindow.h +++ b/tools/assistant/tools/assistant/mainwindow.h @@ -48,6 +48,7 @@ QT_BEGIN_NAMESPACE class QAction; +class QFileSystemWatcher; class QLineEdit; class QComboBox; class QMenu; @@ -119,6 +120,7 @@ private slots: void displayInstallationError(const QString &errorMessage); void qtDocumentationInstalled(bool newDocsInstalled); void checkInitState(); + void qchFileChanged(const QString &fileName); void updateBookmarkMenu(); void showBookmark(QAction *action); @@ -177,6 +179,7 @@ private: QWidget *m_progressWidget; QtDocInstaller *m_qtDocInstaller; + QFileSystemWatcher * const m_qchWatcher; bool m_connectedInitSignals; }; diff --git a/tools/assistant/tools/assistant/preferencesdialog.cpp b/tools/assistant/tools/assistant/preferencesdialog.cpp index 2aa451e..d63c109 100644 --- a/tools/assistant/tools/assistant/preferencesdialog.cpp +++ b/tools/assistant/tools/assistant/preferencesdialog.cpp @@ -46,7 +46,8 @@ #include "centralwidget.h" #include "aboutdialog.h" -#include +#include +#include #include #include @@ -60,11 +61,13 @@ QT_BEGIN_NAMESPACE -PreferencesDialog::PreferencesDialog(QHelpEngineCore *helpEngine, QWidget *parent) +PreferencesDialog::PreferencesDialog(QHelpEngineCore *helpEngine, + QFileSystemWatcher *qchWatcher, QWidget *parent) : QDialog(parent) , m_helpEngine(helpEngine) , m_appFontChanged(false) , m_browserFontChanged(false) + , m_qchWatcher(qchWatcher) { m_ui.setupUi(this); @@ -275,7 +278,11 @@ void PreferencesDialog::addDocumentationLocal() continue; } - m_helpEngine->registerDocumentation(fileName); + if (m_helpEngine->registerDocumentation(fileName)) { + m_qchWatcher->addPath(fileName); + Q_ASSERT(m_qchWatcher->files().count() + == m_helpEngine->registeredDocumentations().count()); + } m_ui.registeredDocsListWidget->addItem(nameSpace); m_regDocs.append(nameSpace); m_unregDocs.removeAll(nameSpace); @@ -380,10 +387,13 @@ void PreferencesDialog::applyChanges() CentralWidget::instance()->closeTabs(m_TabsToClose); - if (m_unregDocs.count()) { - foreach (const QString &doc, m_unregDocs) - m_helpEngine->unregisterDocumentation(doc); + foreach (const QString &doc, m_unregDocs) { + const QString docFile = m_helpEngine->documentationFileName(doc); + if (m_helpEngine->unregisterDocumentation(doc)) + m_qchWatcher->removePath(docFile); } + Q_ASSERT(m_qchWatcher->files().count() + == m_helpEngine->registeredDocumentations().count()); if (filtersWereChanged || m_regDocs.count() || m_unregDocs.count()) m_helpEngine->setupData(); diff --git a/tools/assistant/tools/assistant/preferencesdialog.h b/tools/assistant/tools/assistant/preferencesdialog.h index c9d56ba..7abf431 100644 --- a/tools/assistant/tools/assistant/preferencesdialog.h +++ b/tools/assistant/tools/assistant/preferencesdialog.h @@ -48,6 +48,7 @@ QT_BEGIN_NAMESPACE class FontPanel; +class QFileSystemWatcher; class QHelpEngineCore; enum { @@ -61,7 +62,8 @@ class PreferencesDialog : public QDialog Q_OBJECT public: - PreferencesDialog(QHelpEngineCore *helpEngine, QWidget *parent = 0); + PreferencesDialog(QHelpEngineCore *helpEngine, + QFileSystemWatcher *qchWatcher, QWidget *parent = 0); ~PreferencesDialog(); void showDialog(); @@ -107,6 +109,7 @@ private: FontPanel *m_browserFontPanel; bool m_appFontChanged; bool m_browserFontChanged; + QFileSystemWatcher * const m_qchWatcher; }; QT_END_NAMESPACE diff --git a/tools/assistant/tools/assistant/qtdocinstaller.cpp b/tools/assistant/tools/assistant/qtdocinstaller.cpp index b37e588..42085cb 100644 --- a/tools/assistant/tools/assistant/qtdocinstaller.cpp +++ b/tools/assistant/tools/assistant/qtdocinstaller.cpp @@ -42,12 +42,15 @@ #include #include #include +#include #include #include "qtdocinstaller.h" QT_BEGIN_NAMESPACE -QtDocInstaller::QtDocInstaller(const QString &collectionFile) +QtDocInstaller::QtDocInstaller(const QString &collectionFile, + QFileSystemWatcher *qchWatcher) + : m_qchWatcher(qchWatcher) { m_abort = false; m_collectionFile = collectionFile; @@ -131,15 +134,24 @@ bool QtDocInstaller::installDoc(const QString &name, QHelpEngineCore *helpEngine if (namespaceName.isEmpty()) continue; - if (helpEngine->registeredDocumentations().contains(namespaceName)) - helpEngine->unregisterDocumentation(namespaceName); + if (helpEngine->registeredDocumentations().contains(namespaceName)) { + const QString docFile = + helpEngine->documentationFileName(namespaceName); + if (helpEngine->unregisterDocumentation(namespaceName)) + m_qchWatcher->removePath(docFile); + } if (!helpEngine->registerDocumentation(fi.absoluteFilePath())) { emit errorMessage( tr("The file %1 could not be registered successfully!\n\nReason: %2") .arg(fi.absoluteFilePath()).arg(helpEngine->error())); + } else { + m_qchWatcher->addPath(fi.absoluteFilePath()); } + Q_ASSERT(m_qchWatcher->files().count() + == helpEngine->registeredDocumentations().count()); + helpEngine->setCustomValue(versionKey, fi.lastModified().toString(Qt::ISODate) + QLatin1String("|") + fi.absoluteFilePath()); return true; diff --git a/tools/assistant/tools/assistant/qtdocinstaller.h b/tools/assistant/tools/assistant/qtdocinstaller.h index 987272b..0d503fd 100644 --- a/tools/assistant/tools/assistant/qtdocinstaller.h +++ b/tools/assistant/tools/assistant/qtdocinstaller.h @@ -47,6 +47,7 @@ QT_BEGIN_NAMESPACE +class QFileSystemWatcher; class QHelpEngineCore; class QtDocInstaller : public QThread @@ -54,7 +55,7 @@ class QtDocInstaller : public QThread Q_OBJECT public: - QtDocInstaller(const QString &collectionFile); + QtDocInstaller(const QString &collectionFile, QFileSystemWatcher *qchWatcher); ~QtDocInstaller(); void installDocs(); @@ -70,6 +71,7 @@ private: bool m_abort; QString m_collectionFile; QMutex m_mutex; + QFileSystemWatcher * const m_qchWatcher; }; QT_END_NAMESPACE diff --git a/tools/assistant/tools/assistant/remotecontrol.cpp b/tools/assistant/tools/assistant/remotecontrol.cpp index 0ccf743..3c123d6 100644 --- a/tools/assistant/tools/assistant/remotecontrol.cpp +++ b/tools/assistant/tools/assistant/remotecontrol.cpp @@ -45,6 +45,7 @@ #include #include +#include #include #include #include @@ -105,7 +106,8 @@ void StdInListenerWin::run() } #endif -RemoteControl::RemoteControl(MainWindow *mainWindow, QHelpEngine *helpEngine) +RemoteControl::RemoteControl(MainWindow *mainWindow, QHelpEngine *helpEngine, + QFileSystemWatcher *qchWatcher) : QObject(mainWindow) , m_mainWindow(mainWindow) , m_helpEngine(helpEngine) @@ -113,6 +115,7 @@ RemoteControl::RemoteControl(MainWindow *mainWindow, QHelpEngine *helpEngine) , m_caching(true) , m_syncContents(false) , m_expandTOC(-2) + , m_qchWatcher(qchWatcher) { connect(m_mainWindow, SIGNAL(initDone()), this, SLOT(applyCache())); @@ -293,10 +296,14 @@ void RemoteControl::handleRegisterCommand(const QString &arg) { const QString &absFileName = QFileInfo(arg).absoluteFilePath(); if (m_helpEngine->registeredDocumentations(). - contains(QHelpEngineCore::namespaceName(absFileName))) + contains(QHelpEngineCore::namespaceName(absFileName))) return; - m_helpEngine->registerDocumentation(absFileName); - m_helpEngine->setupData(); + if (m_helpEngine->registerDocumentation(absFileName)) { + m_qchWatcher->addPath(absFileName); + m_helpEngine->setupData(); + Q_ASSERT(m_qchWatcher->files().count() + == m_helpEngine->registeredDocumentations().count()); + } } void RemoteControl::handleUnregisterCommand(const QString &arg) @@ -306,8 +313,11 @@ void RemoteControl::handleUnregisterCommand(const QString &arg) if (m_helpEngine->registeredDocumentations().contains(ns)) { CentralWidget* widget = CentralWidget::instance(); widget->closeTabs(widget->currentSourceFileList().keys(ns)); - m_helpEngine->unregisterDocumentation(ns); - m_helpEngine->setupData(); + const QString docFile = m_helpEngine->documentationFileName(ns); + if (m_helpEngine->unregisterDocumentation(ns)) { + m_qchWatcher->removePath(docFile); + m_helpEngine->setupData(); + } } } diff --git a/tools/assistant/tools/assistant/remotecontrol.h b/tools/assistant/tools/assistant/remotecontrol.h index c1c3105..6f7eb5f 100644 --- a/tools/assistant/tools/assistant/remotecontrol.h +++ b/tools/assistant/tools/assistant/remotecontrol.h @@ -49,6 +49,7 @@ QT_BEGIN_NAMESPACE class MainWindow; +class QFileSystemWatcher; class QHelpEngine; class RemoteControl : public QObject @@ -56,7 +57,8 @@ class RemoteControl : public QObject Q_OBJECT public: - RemoteControl(MainWindow *mainWindow, QHelpEngine *helpEngine); + RemoteControl(MainWindow *mainWindow, QHelpEngine *helpEngine, + QFileSystemWatcher *qchWatcher); private slots: void receivedData(); @@ -89,6 +91,7 @@ private: QString m_activateIdentifier; int m_expandTOC; QString m_currentFilter; + QFileSystemWatcher * const m_qchWatcher; }; QT_END_NAMESPACE -- cgit v0.12