From 368d98269137f56def9b3bbbc434d8aec0853f13 Mon Sep 17 00:00:00 2001 From: Leandro Melo Date: Wed, 18 Aug 2010 15:13:39 +0200 Subject: [PATCH] Generic highlighter: Prevent overlapping in the mime type registration. Also add a progress bar for the registration process. --- .../texteditor/generichighlighter/manager.cpp | 32 ++++++++++++++++--- .../texteditor/generichighlighter/manager.h | 8 ++++- src/plugins/texteditor/texteditorconstants.h | 3 +- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/src/plugins/texteditor/generichighlighter/manager.cpp b/src/plugins/texteditor/generichighlighter/manager.cpp index c75e0c2b6ba..571fa4065e6 100644 --- a/src/plugins/texteditor/generichighlighter/manager.cpp +++ b/src/plugins/texteditor/generichighlighter/manager.cpp @@ -70,9 +70,13 @@ using namespace TextEditor; using namespace Internal; -Manager::Manager() : m_downloadingDefinitions(false) +Manager::Manager() : + m_downloadingDefinitions(false), + m_registeringMimeTypes(false), + m_queuedMimeTypeRegistrations(0) { connect(&m_mimeTypeWatcher, SIGNAL(resultReadyAt(int)), this, SLOT(registerMimeType(int))); + connect(&m_mimeTypeWatcher, SIGNAL(finished()), this, SLOT(registerMimeTypesFinished())); connect(&m_downloadWatcher, SIGNAL(finished()), this, SLOT(downloadDefinitionsFinished())); } @@ -138,10 +142,19 @@ bool Manager::isBuildingDefinition(const QString &id) const void Manager::registerMimeTypes() { - clear(); - QFuture future = + if (!m_registeringMimeTypes) { + m_registeringMimeTypes = true; + clear(); + QFuture future = QtConcurrent::run(&Manager::gatherDefinitionsMimeTypes, this); - m_mimeTypeWatcher.setFuture(future); + m_mimeTypeWatcher.setFuture(future); + Core::ICore::instance()->progressManager()->addTask(future, + tr("Registering definitions"), + Constants::TASK_REGISTER_DEFINITIONS); + } else { + // QFutures returned from QConcurrent::run cannot be cancelled. So the queue. + ++m_queuedMimeTypeRegistrations; + } } void Manager::gatherDefinitionsMimeTypes(QFutureInterface &future) @@ -217,6 +230,15 @@ void Manager::registerMimeType(int index) const Core::ICore::instance()->mimeDatabase()->addMimeType(mimeType); } +void Manager::registerMimeTypesFinished() +{ + m_registeringMimeTypes = false; + if (m_queuedMimeTypeRegistrations > 0) { + --m_queuedMimeTypeRegistrations; + registerMimeTypes(); + } +} + QSharedPointer Manager::parseMetadata(const QFileInfo &fileInfo) { static const QLatin1Char kSemiColon(';'); @@ -332,7 +354,7 @@ void Manager::downloadDefinitions(const QList &urls) m_downloadWatcher.setFuture(future); Core::ICore::instance()->progressManager()->addTask(future, tr("Downloading definitions"), - Constants::TASK_DOWNLOAD); + Constants::TASK_DOWNLOAD_DEFINITIONS); } void Manager::downloadDefinitionsFinished() diff --git a/src/plugins/texteditor/generichighlighter/manager.h b/src/plugins/texteditor/generichighlighter/manager.h index 73f18ef1653..eac24665ed2 100644 --- a/src/plugins/texteditor/generichighlighter/manager.h +++ b/src/plugins/texteditor/generichighlighter/manager.h @@ -56,6 +56,8 @@ namespace Internal { class HighlightDefinition; class DefinitionDownloader; +// This is the generic highlighter manager. It is not thread-safe. + class Manager : public QObject { Q_OBJECT @@ -82,6 +84,7 @@ public slots: private slots: void registerMimeType(int index) const; + void registerMimeTypesFinished(); void downloadAvailableDefinitionsListFinished(); void downloadDefinitionsFinished(); @@ -94,6 +97,10 @@ private: QList parseAvailableDefinitionsList(QIODevice *device) const; void clear(); + bool m_downloadingDefinitions; + bool m_registeringMimeTypes; + int m_queuedMimeTypeRegistrations; + QHash m_idByName; QHash m_idByMimeType; QHash > m_definitions; @@ -101,7 +108,6 @@ private: QSet m_isBuilding; QList m_downloaders; - bool m_downloadingDefinitions; QNetworkAccessManager m_networkManager; QFutureWatcher m_downloadWatcher; diff --git a/src/plugins/texteditor/texteditorconstants.h b/src/plugins/texteditor/texteditorconstants.h index 40a8034dd7a..56ca8961c65 100644 --- a/src/plugins/texteditor/texteditorconstants.h +++ b/src/plugins/texteditor/texteditorconstants.h @@ -88,7 +88,8 @@ const char * const GOTO_PREVIOUS_WORD_WITH_SELECTION = "TextEditor.GotoPreviousW const char * const GOTO_NEXT_WORD_WITH_SELECTION = "TextEditor.GotoNextWordWithSelection"; const char * const C_TEXTEDITOR_MIMETYPE_TEXT = "text/plain"; const char * const INFO_SYNTAX_DEFINITION = "TextEditor.InfoSyntaxDefinition"; -const char * const TASK_DOWNLOAD = "TextEditor.Task.Download"; +const char * const TASK_DOWNLOAD_DEFINITIONS = "TextEditor.Task.Download"; +const char * const TASK_REGISTER_DEFINITIONS = "TextEditor.Task.Register"; // Text color and style categories const char * const C_TEXT = "Text";