From 672fcbe9c6586e909513c91f5b36ad9a83a0ef1c Mon Sep 17 00:00:00 2001 From: David Faure Date: Sun, 28 Jul 2013 20:03:57 +0200 Subject: [PATCH] QMimeDatabase: Fix handling of duplicate mimetype definitions (2/2). 7721c3d27c6a fixed the case where two similar definitions are in the same directory. This commit fixes the case where two similar definitions are in different directories, both in the search path (GenericDataLocation). If the file extension gives us the same mimetype twice, there's no conflict, i.e. no reason to fallback to determination from contents. Change-Id: I72c56004b6d5e88964159e53ec160ce8b06c2264 Reviewed-by: Thiago Macieira --- src/corelib/mimetypes/qmimeglobpattern.cpp | 8 +++++--- .../mimetypes/qmimedatabase/tst_qmimedatabase.cpp | 14 +++++++++++++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/corelib/mimetypes/qmimeglobpattern.cpp b/src/corelib/mimetypes/qmimeglobpattern.cpp index 96798cabcd..530d3de4a2 100644 --- a/src/corelib/mimetypes/qmimeglobpattern.cpp +++ b/src/corelib/mimetypes/qmimeglobpattern.cpp @@ -77,9 +77,11 @@ void QMimeGlobMatchResult::addMatch(const QString &mimeType, int weight, const Q m_matchingPatternLength = pattern.length(); m_weight = weight; } - m_matchingMimeTypes.append(mimeType); - if (pattern.startsWith(QLatin1String("*."))) - m_foundSuffix = pattern.mid(2); + if (!m_matchingMimeTypes.contains(mimeType)) { + m_matchingMimeTypes.append(mimeType); + if (pattern.startsWith(QLatin1String("*."))) + m_foundSuffix = pattern.mid(2); + } } /*! diff --git a/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp b/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp index a90bfadd73..07d3c5c7b8 100644 --- a/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp +++ b/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp @@ -876,7 +876,10 @@ void tst_QMimeDatabase::installNewLocalMimeType() QDir().mkpath(destDir); const QString destFile = destDir + QLatin1String(yastFileName); QFile::remove(destFile); + const QString destQmlFile = destDir + QLatin1String(qmlAgainFileName); + QFile::remove(destQmlFile); QVERIFY(QFile::copy(m_yastMimeTypes, destFile)); + QVERIFY(QFile::copy(m_qmlAgainFileName, destQmlFile)); if (!runUpdateMimeDatabase(mimeDir)) { const QString skipWarning = QStringLiteral("shared-mime-info not found, skipping mime.cache test (") + QDir::toNativeSeparators(mimeDir) + QLatin1Char(')'); @@ -888,8 +891,17 @@ void tst_QMimeDatabase::installNewLocalMimeType() QVERIFY(db.mimeTypeForName(QLatin1String("text/x-suse-ymp")).isValid()); checkHasMimeType("text/x-suse-ymp"); - // Now test removing it again (note, this leaves a mostly-empty mime.cache file) + // Test that a double-definition of a mimetype doesn't lead to sniffing ("conflicting globs"). + const QString qmlTestFile = QFINDTESTDATA("test.qml"); + QVERIFY2(!qmlTestFile.isEmpty(), + qPrintable(QString::fromLatin1("Cannot find '%1' starting from '%2'"). + arg("test.qml", QDir::currentPath()))); + QCOMPARE(db.mimeTypeForFile(qmlTestFile).name(), + QString::fromLatin1("text/x-qml")); + + // Now test removing the local mimetypes again (note, this leaves a mostly-empty mime.cache file) QFile::remove(destFile); + QFile::remove(destQmlFile); if (!waitAndRunUpdateMimeDatabase(mimeDir)) QSKIP("shared-mime-info not found, skipping mime.cache test"); QCOMPARE(db.mimeTypeForFile(QLatin1String("foo.ymu"), QMimeDatabase::MatchExtension).name(),