forked from qt-creator/qt-creator
EditorManager: Do not instantiate MimeTypes during startup
Which would trigger reading the MIME database, which is not needed at that point in time. We persist the map of MIME type to custom default factories via the MIME type names anyway, so just stay with that. Change-Id: I7570432573b16700e00811fe409d43a2db6c5347 Reviewed-by: David Schulz <david.schulz@qt.io> Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
@@ -1170,33 +1170,30 @@ Id EditorManagerPrivate::getOpenWithEditorId(const Utils::FilePath &fileName, bo
|
|||||||
return selectedId;
|
return selectedId;
|
||||||
}
|
}
|
||||||
|
|
||||||
static QMap<QString, QVariant> toMap(const QHash<Utils::MimeType, IEditorFactory *> &hash)
|
static QMap<QString, QVariant> toMap(const QHash<QString, IEditorFactory *> &hash)
|
||||||
{
|
{
|
||||||
QMap<QString, QVariant> map;
|
QMap<QString, QVariant> map;
|
||||||
auto it = hash.begin();
|
auto it = hash.begin();
|
||||||
const auto end = hash.end();
|
const auto end = hash.end();
|
||||||
while (it != end) {
|
while (it != end) {
|
||||||
map.insert(it.key().name(), it.value()->id().toSetting());
|
map.insert(it.key(), it.value()->id().toSetting());
|
||||||
++it;
|
++it;
|
||||||
}
|
}
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
static QHash<Utils::MimeType, IEditorFactory *> fromMap(const QMap<QString, QVariant> &map)
|
static QHash<QString, IEditorFactory *> fromMap(const QMap<QString, QVariant> &map)
|
||||||
{
|
{
|
||||||
const EditorFactories factories = IEditorFactory::allEditorFactories();
|
const EditorFactories factories = IEditorFactory::allEditorFactories();
|
||||||
QHash<Utils::MimeType, IEditorFactory *> hash;
|
QHash<QString, IEditorFactory *> hash;
|
||||||
auto it = map.begin();
|
auto it = map.begin();
|
||||||
const auto end = map.end();
|
const auto end = map.end();
|
||||||
while (it != end) {
|
while (it != end) {
|
||||||
const Utils::MimeType mimeType = Utils::mimeTypeForName(it.key());
|
|
||||||
if (mimeType.isValid()) {
|
|
||||||
const Id factoryId = Id::fromSetting(it.value());
|
const Id factoryId = Id::fromSetting(it.value());
|
||||||
IEditorFactory *factory = Utils::findOrDefault(factories,
|
IEditorFactory *factory = Utils::findOrDefault(factories,
|
||||||
Utils::equal(&IEditorFactory::id, factoryId));
|
Utils::equal(&IEditorFactory::id, factoryId));
|
||||||
if (factory)
|
if (factory)
|
||||||
hash.insert(mimeType, factory);
|
hash.insert(it.key(), factory);
|
||||||
}
|
|
||||||
++it;
|
++it;
|
||||||
}
|
}
|
||||||
return hash;
|
return hash;
|
||||||
@@ -1223,7 +1220,7 @@ void EditorManagerPrivate::readSettings()
|
|||||||
else
|
else
|
||||||
HostOsInfo::setOverrideFileNameCaseSensitivity(sensitivity);
|
HostOsInfo::setOverrideFileNameCaseSensitivity(sensitivity);
|
||||||
|
|
||||||
const QHash<Utils::MimeType, IEditorFactory *> preferredEditorFactories = fromMap(
|
const QHash<QString, IEditorFactory *> preferredEditorFactories = fromMap(
|
||||||
qs->value(preferredEditorFactoriesKey).toMap());
|
qs->value(preferredEditorFactoriesKey).toMap());
|
||||||
setUserPreferredEditorTypes(preferredEditorFactories);
|
setUserPreferredEditorTypes(preferredEditorFactories);
|
||||||
|
|
||||||
|
|||||||
@@ -131,7 +131,7 @@ static void mimeTypeFactoryLookup(const Utils::MimeType &mimeType,
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
static QList<IEditorFactory *> g_editorFactories;
|
static QList<IEditorFactory *> g_editorFactories;
|
||||||
static QHash<Utils::MimeType, IEditorFactory *> g_userPreferredEditorTypes;
|
static QHash<QString, IEditorFactory *> g_userPreferredEditorTypes;
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
Creates an IEditorFactory.
|
Creates an IEditorFactory.
|
||||||
@@ -190,7 +190,7 @@ const EditorFactories IEditorFactory::preferredEditorTypes(const FilePath &fileP
|
|||||||
MimeMatchMode::MatchDefaultAndRemote);
|
MimeMatchMode::MatchDefaultAndRemote);
|
||||||
EditorFactories factories = defaultEditorFactories(mimeType);
|
EditorFactories factories = defaultEditorFactories(mimeType);
|
||||||
// user preferred factory to front
|
// user preferred factory to front
|
||||||
IEditorFactory *userPreferred = Internal::userPreferredEditorTypes().value(mimeType);
|
IEditorFactory *userPreferred = Internal::userPreferredEditorTypes().value(mimeType.name());
|
||||||
if (userPreferred) {
|
if (userPreferred) {
|
||||||
factories.removeAll(userPreferred);
|
factories.removeAll(userPreferred);
|
||||||
factories.prepend(userPreferred);
|
factories.prepend(userPreferred);
|
||||||
@@ -238,7 +238,7 @@ const EditorFactories IEditorFactory::preferredEditorFactories(const FilePath &f
|
|||||||
factories.prepend(f);
|
factories.prepend(f);
|
||||||
};
|
};
|
||||||
// user preferred factory to front
|
// user preferred factory to front
|
||||||
IEditorFactory *userPreferred = Internal::userPreferredEditorTypes().value(mimeType);
|
IEditorFactory *userPreferred = Internal::userPreferredEditorTypes().value(mimeType.name());
|
||||||
if (userPreferred && userPreferred->isInternalEditor())
|
if (userPreferred && userPreferred->isInternalEditor())
|
||||||
factories_moveToFront(userPreferred);
|
factories_moveToFront(userPreferred);
|
||||||
// open text files > 48 MB in binary editor
|
// open text files > 48 MB in binary editor
|
||||||
@@ -328,7 +328,7 @@ void IEditorFactory::setEditorStarter(const std::function<bool(const FilePath &,
|
|||||||
/*!
|
/*!
|
||||||
\internal
|
\internal
|
||||||
*/
|
*/
|
||||||
QHash<Utils::MimeType, IEditorFactory *> Internal::userPreferredEditorTypes()
|
QHash<QString, IEditorFactory *> Internal::userPreferredEditorTypes()
|
||||||
{
|
{
|
||||||
return g_userPreferredEditorTypes;
|
return g_userPreferredEditorTypes;
|
||||||
}
|
}
|
||||||
@@ -336,7 +336,7 @@ QHash<Utils::MimeType, IEditorFactory *> Internal::userPreferredEditorTypes()
|
|||||||
/*!
|
/*!
|
||||||
\internal
|
\internal
|
||||||
*/
|
*/
|
||||||
void Internal::setUserPreferredEditorTypes(const QHash<Utils::MimeType, IEditorFactory *> &factories)
|
void Internal::setUserPreferredEditorTypes(const QHash<QString, IEditorFactory *> &factories)
|
||||||
{
|
{
|
||||||
g_userPreferredEditorTypes = factories;
|
g_userPreferredEditorTypes = factories;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,8 +3,6 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <utils/mimeutils.h>
|
|
||||||
|
|
||||||
#include <QHash>
|
#include <QHash>
|
||||||
|
|
||||||
namespace Core {
|
namespace Core {
|
||||||
@@ -13,8 +11,8 @@ class IEditorFactory;
|
|||||||
|
|
||||||
namespace Internal {
|
namespace Internal {
|
||||||
|
|
||||||
QHash<Utils::MimeType, IEditorFactory *> userPreferredEditorTypes();
|
QHash<QString, IEditorFactory *> userPreferredEditorTypes();
|
||||||
void setUserPreferredEditorTypes(const QHash<Utils::MimeType, IEditorFactory *> &factories);
|
void setUserPreferredEditorTypes(const QHash<QString, IEditorFactory *> &factories);
|
||||||
|
|
||||||
} // Internal
|
} // Internal
|
||||||
} // Core
|
} // Core
|
||||||
|
|||||||
@@ -104,7 +104,7 @@ public:
|
|||||||
|
|
||||||
QList<Utils::MimeType> m_mimeTypes;
|
QList<Utils::MimeType> m_mimeTypes;
|
||||||
mutable QHash<Utils::MimeType, QList<IEditorFactory *>> m_handlersByMimeType;
|
mutable QHash<Utils::MimeType, QList<IEditorFactory *>> m_handlersByMimeType;
|
||||||
QHash<Utils::MimeType, IEditorFactory *> m_userDefault;
|
QHash<QString, IEditorFactory *> m_userDefault;
|
||||||
};
|
};
|
||||||
|
|
||||||
int MimeTypeSettingsModel::rowCount(const QModelIndex &) const
|
int MimeTypeSettingsModel::rowCount(const QModelIndex &) const
|
||||||
@@ -149,7 +149,7 @@ QVariant MimeTypeSettingsModel::data(const QModelIndex &modelIndex, int role) co
|
|||||||
} else if (role == Qt::FontRole) {
|
} else if (role == Qt::FontRole) {
|
||||||
if (column == 1) {
|
if (column == 1) {
|
||||||
const Utils::MimeType &type = m_mimeTypes.at(modelIndex.row());
|
const Utils::MimeType &type = m_mimeTypes.at(modelIndex.row());
|
||||||
if (m_userDefault.contains(type)) {
|
if (m_userDefault.contains(type.name())) {
|
||||||
QFont font = QGuiApplication::font();
|
QFont font = QGuiApplication::font();
|
||||||
font.setItalic(true);
|
font.setItalic(true);
|
||||||
return font;
|
return font;
|
||||||
@@ -174,9 +174,9 @@ bool MimeTypeSettingsModel::setData(const QModelIndex &index, const QVariant &va
|
|||||||
const QList<IEditorFactory *> handlers = handlersForMimeType(mimeType);
|
const QList<IEditorFactory *> handlers = handlersForMimeType(mimeType);
|
||||||
QTC_ASSERT(handlers.contains(factory), return false);
|
QTC_ASSERT(handlers.contains(factory), return false);
|
||||||
if (handlers.first() == factory) // selection is the default anyhow
|
if (handlers.first() == factory) // selection is the default anyhow
|
||||||
m_userDefault.remove(mimeType);
|
m_userDefault.remove(mimeType.name());
|
||||||
else
|
else
|
||||||
m_userDefault.insert(mimeType, factory);
|
m_userDefault.insert(mimeType.name(), factory);
|
||||||
emit dataChanged(index, index);
|
emit dataChanged(index, index);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -209,8 +209,8 @@ QList<IEditorFactory *> MimeTypeSettingsModel::handlersForMimeType(const Utils::
|
|||||||
|
|
||||||
IEditorFactory *MimeTypeSettingsModel::defaultHandlerForMimeType(const Utils::MimeType &mimeType) const
|
IEditorFactory *MimeTypeSettingsModel::defaultHandlerForMimeType(const Utils::MimeType &mimeType) const
|
||||||
{
|
{
|
||||||
if (m_userDefault.contains(mimeType))
|
if (m_userDefault.contains(mimeType.name()))
|
||||||
return m_userDefault.value(mimeType);
|
return m_userDefault.value(mimeType.name());
|
||||||
const QList<IEditorFactory *> handlers = handlersForMimeType(mimeType);
|
const QList<IEditorFactory *> handlers = handlersForMimeType(mimeType);
|
||||||
return handlers.isEmpty() ? nullptr : handlers.first();
|
return handlers.isEmpty() ? nullptr : handlers.first();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user