Generic highlighter: Prevent overlapping in the mime type registration.

Also add a progress bar for the registration process.
This commit is contained in:
Leandro Melo
2010-08-18 15:13:39 +02:00
parent d82acb2c31
commit 368d982691
3 changed files with 36 additions and 7 deletions

View File

@@ -70,9 +70,13 @@
using namespace TextEditor; using namespace TextEditor;
using namespace Internal; 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(resultReadyAt(int)), this, SLOT(registerMimeType(int)));
connect(&m_mimeTypeWatcher, SIGNAL(finished()), this, SLOT(registerMimeTypesFinished()));
connect(&m_downloadWatcher, SIGNAL(finished()), this, SLOT(downloadDefinitionsFinished())); connect(&m_downloadWatcher, SIGNAL(finished()), this, SLOT(downloadDefinitionsFinished()));
} }
@@ -138,10 +142,19 @@ bool Manager::isBuildingDefinition(const QString &id) const
void Manager::registerMimeTypes() void Manager::registerMimeTypes()
{ {
clear(); if (!m_registeringMimeTypes) {
QFuture<Core::MimeType> future = m_registeringMimeTypes = true;
clear();
QFuture<Core::MimeType> future =
QtConcurrent::run(&Manager::gatherDefinitionsMimeTypes, this); 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<Core::MimeType> &future) void Manager::gatherDefinitionsMimeTypes(QFutureInterface<Core::MimeType> &future)
@@ -217,6 +230,15 @@ void Manager::registerMimeType(int index) const
Core::ICore::instance()->mimeDatabase()->addMimeType(mimeType); Core::ICore::instance()->mimeDatabase()->addMimeType(mimeType);
} }
void Manager::registerMimeTypesFinished()
{
m_registeringMimeTypes = false;
if (m_queuedMimeTypeRegistrations > 0) {
--m_queuedMimeTypeRegistrations;
registerMimeTypes();
}
}
QSharedPointer<HighlightDefinitionMetaData> Manager::parseMetadata(const QFileInfo &fileInfo) QSharedPointer<HighlightDefinitionMetaData> Manager::parseMetadata(const QFileInfo &fileInfo)
{ {
static const QLatin1Char kSemiColon(';'); static const QLatin1Char kSemiColon(';');
@@ -332,7 +354,7 @@ void Manager::downloadDefinitions(const QList<QUrl> &urls)
m_downloadWatcher.setFuture(future); m_downloadWatcher.setFuture(future);
Core::ICore::instance()->progressManager()->addTask(future, Core::ICore::instance()->progressManager()->addTask(future,
tr("Downloading definitions"), tr("Downloading definitions"),
Constants::TASK_DOWNLOAD); Constants::TASK_DOWNLOAD_DEFINITIONS);
} }
void Manager::downloadDefinitionsFinished() void Manager::downloadDefinitionsFinished()

View File

@@ -56,6 +56,8 @@ namespace Internal {
class HighlightDefinition; class HighlightDefinition;
class DefinitionDownloader; class DefinitionDownloader;
// This is the generic highlighter manager. It is not thread-safe.
class Manager : public QObject class Manager : public QObject
{ {
Q_OBJECT Q_OBJECT
@@ -82,6 +84,7 @@ public slots:
private slots: private slots:
void registerMimeType(int index) const; void registerMimeType(int index) const;
void registerMimeTypesFinished();
void downloadAvailableDefinitionsListFinished(); void downloadAvailableDefinitionsListFinished();
void downloadDefinitionsFinished(); void downloadDefinitionsFinished();
@@ -94,6 +97,10 @@ private:
QList<HighlightDefinitionMetaData> parseAvailableDefinitionsList(QIODevice *device) const; QList<HighlightDefinitionMetaData> parseAvailableDefinitionsList(QIODevice *device) const;
void clear(); void clear();
bool m_downloadingDefinitions;
bool m_registeringMimeTypes;
int m_queuedMimeTypeRegistrations;
QHash<QString, QString> m_idByName; QHash<QString, QString> m_idByName;
QHash<QString, QString> m_idByMimeType; QHash<QString, QString> m_idByMimeType;
QHash<QString, QSharedPointer<HighlightDefinition> > m_definitions; QHash<QString, QSharedPointer<HighlightDefinition> > m_definitions;
@@ -101,7 +108,6 @@ private:
QSet<QString> m_isBuilding; QSet<QString> m_isBuilding;
QList<DefinitionDownloader *> m_downloaders; QList<DefinitionDownloader *> m_downloaders;
bool m_downloadingDefinitions;
QNetworkAccessManager m_networkManager; QNetworkAccessManager m_networkManager;
QFutureWatcher<void> m_downloadWatcher; QFutureWatcher<void> m_downloadWatcher;

View File

@@ -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 GOTO_NEXT_WORD_WITH_SELECTION = "TextEditor.GotoNextWordWithSelection";
const char * const C_TEXTEDITOR_MIMETYPE_TEXT = "text/plain"; const char * const C_TEXTEDITOR_MIMETYPE_TEXT = "text/plain";
const char * const INFO_SYNTAX_DEFINITION = "TextEditor.InfoSyntaxDefinition"; 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 // Text color and style categories
const char * const C_TEXT = "Text"; const char * const C_TEXT = "Text";