forked from qt-creator/qt-creator
Mime types: Fix opening files with mime types with multiple inheritance
It was opening e.g. ruby files in the binary editor because the ruby mime type inherits from application/x-executable (which inherits from application/octet-stream) as well as text/plain, and it was searching depth-first. Task-number: QTCREATORBUG-13996 Change-Id: I1980d7ed04fabf111f8fb76df657225ae9ebe552 Reviewed-by: Orgad Shaneh <orgads@gmail.com>
This commit is contained in:
@@ -2195,36 +2195,42 @@ IEditor *EditorManager::activateEditorForDocument(IDocument *document, OpenEdito
|
|||||||
* or IExternalEditor), find the one best matching the mimetype passed in.
|
* or IExternalEditor), find the one best matching the mimetype passed in.
|
||||||
* Recurse over the parent classes of the mimetype to find them. */
|
* Recurse over the parent classes of the mimetype to find them. */
|
||||||
template <class EditorFactoryLike>
|
template <class EditorFactoryLike>
|
||||||
static void mimeTypeFactoryRecursion(const Utils::MimeType &mimeType,
|
static void mimeTypeFactoryLookup(const Utils::MimeType &mimeType,
|
||||||
const QList<EditorFactoryLike*> &allFactories,
|
const QList<EditorFactoryLike*> &allFactories,
|
||||||
bool firstMatchOnly,
|
bool firstMatchOnly,
|
||||||
QList<EditorFactoryLike*> *list)
|
QList<EditorFactoryLike*> *list)
|
||||||
{
|
{
|
||||||
typedef typename QList<EditorFactoryLike*>::const_iterator EditorFactoryLikeListConstIterator;
|
Utils::MimeDatabase mdb;
|
||||||
// Loop factories to find type
|
QSet<EditorFactoryLike *> matches;
|
||||||
const EditorFactoryLikeListConstIterator fcend = allFactories.constEnd();
|
// search breadth-first through parent hierarchy, e.g. for hierarchy
|
||||||
for (EditorFactoryLikeListConstIterator fit = allFactories.constBegin(); fit != fcend; ++fit) {
|
// * application/x-ruby
|
||||||
// Exclude duplicates when recursing over xml or C++ -> C -> text.
|
// * application/x-executable
|
||||||
EditorFactoryLike *factory = *fit;
|
// * application/octet-stream
|
||||||
if (!list->contains(factory)) {
|
// * text/plain
|
||||||
foreach (const QString &mt, factory->mimeTypes()) {
|
QList<Utils::MimeType> queue;
|
||||||
if (mimeType.matchesName(mt)) {
|
queue.append(mimeType);
|
||||||
list->push_back(*fit);
|
while (!queue.isEmpty()) {
|
||||||
|
Utils::MimeType mt = queue.takeFirst();
|
||||||
|
// check for matching factories
|
||||||
|
foreach (EditorFactoryLike *factory, allFactories) {
|
||||||
|
if (!matches.contains(factory)) {
|
||||||
|
foreach (const QString &mimeName, factory->mimeTypes()) {
|
||||||
|
if (mt.matchesName(mimeName)) {
|
||||||
|
list->append(factory);
|
||||||
if (firstMatchOnly)
|
if (firstMatchOnly)
|
||||||
return;
|
return;
|
||||||
|
matches.insert(factory);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Any parent mime type classes? -> recurse
|
// add parent mime types
|
||||||
QStringList parentNames = mimeType.parentMimeTypes();
|
QStringList parentNames = mt.parentMimeTypes();
|
||||||
if (parentNames.empty())
|
|
||||||
return;
|
|
||||||
Utils::MimeDatabase mdb;
|
|
||||||
foreach (const QString &parentName, parentNames) {
|
foreach (const QString &parentName, parentNames) {
|
||||||
const Utils::MimeType parent = mdb.mimeTypeForName(parentName);
|
const Utils::MimeType parent = mdb.mimeTypeForName(parentName);
|
||||||
if (parent.isValid())
|
if (parent.isValid())
|
||||||
mimeTypeFactoryRecursion(parent, allFactories, firstMatchOnly, list);
|
queue.append(parent);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2233,7 +2239,7 @@ EditorManager::EditorFactoryList
|
|||||||
{
|
{
|
||||||
EditorFactoryList rc;
|
EditorFactoryList rc;
|
||||||
const EditorFactoryList allFactories = ExtensionSystem::PluginManager::getObjects<IEditorFactory>();
|
const EditorFactoryList allFactories = ExtensionSystem::PluginManager::getObjects<IEditorFactory>();
|
||||||
mimeTypeFactoryRecursion(mimeType, allFactories, bestMatchOnly, &rc);
|
mimeTypeFactoryLookup(mimeType, allFactories, bestMatchOnly, &rc);
|
||||||
if (debugEditorManager)
|
if (debugEditorManager)
|
||||||
qDebug() << Q_FUNC_INFO << mimeType.name() << " returns " << rc;
|
qDebug() << Q_FUNC_INFO << mimeType.name() << " returns " << rc;
|
||||||
return rc;
|
return rc;
|
||||||
@@ -2244,7 +2250,7 @@ EditorManager::ExternalEditorList
|
|||||||
{
|
{
|
||||||
ExternalEditorList rc;
|
ExternalEditorList rc;
|
||||||
const ExternalEditorList allEditors = ExtensionSystem::PluginManager::getObjects<IExternalEditor>();
|
const ExternalEditorList allEditors = ExtensionSystem::PluginManager::getObjects<IExternalEditor>();
|
||||||
mimeTypeFactoryRecursion(mimeType, allEditors, bestMatchOnly, &rc);
|
mimeTypeFactoryLookup(mimeType, allEditors, bestMatchOnly, &rc);
|
||||||
if (debugEditorManager)
|
if (debugEditorManager)
|
||||||
qDebug() << Q_FUNC_INFO << mimeType.name() << " returns " << rc;
|
qDebug() << Q_FUNC_INFO << mimeType.name() << " returns " << rc;
|
||||||
return rc;
|
return rc;
|
||||||
|
Reference in New Issue
Block a user