QMimeType: Use default key as fallback for comment() property

When QMimeProvider parses the shared mime database xml files,
it will read the <comment> element for mime comment and treat the
`xml:lang` attribute as locale language string. When no `xml:lang`
attr is provided, QMimeProvider will read the value and treat it as
a en_US locale string as the default key.

When we call QMimeType::comment(), it will try to get the locale
comment string with the default language (QLocale().name()), once
it can't find a matched result, it should return the default key
(which QMimeProvider set it as en_US locale before) as fallback.

Task-number: QTBUG-71314
Change-Id: I444f8159d6f19dfef6338cd79312f608d8f13394
Reviewed-by: David Faure <david.faure@kdab.com>
This commit is contained in:
Gary Wang 2018-10-23 14:17:29 +08:00
parent 8637235e85
commit e3c84b6da1
4 changed files with 18 additions and 3 deletions

View File

@ -502,7 +502,7 @@ void QMimeBinaryProvider::loadMimeTypePrivate(QMimeTypePrivate &data)
QString lang = xml.attributes().value(QLatin1String("xml:lang")).toString(); QString lang = xml.attributes().value(QLatin1String("xml:lang")).toString();
const QString text = xml.readElementText(); const QString text = xml.readElementText();
if (lang.isEmpty()) { if (lang.isEmpty()) {
lang = QLatin1String("en_US"); lang = QLatin1String("default"); // no locale attribute provided, treat it as default.
} }
data.localeComments.insert(lang, text); data.localeComments.insert(lang, text);
continue; // we called readElementText, so we're at the EndElement already. continue; // we called readElementText, so we're at the EndElement already.

View File

@ -258,6 +258,7 @@ QString QMimeType::comment() const
QStringList languageList; QStringList languageList;
languageList << QLocale().name(); languageList << QLocale().name();
languageList << QLocale().uiLanguages(); languageList << QLocale().uiLanguages();
languageList << QLatin1String("default"); // use the default locale if possible.
for (const QString &language : qAsConst(languageList)) { for (const QString &language : qAsConst(languageList)) {
const QString lang = language == QLatin1String("C") ? QLatin1String("en_US") : language; const QString lang = language == QLatin1String("C") ? QLatin1String("en_US") : language;
const QString comm = d->localeComments.value(lang); const QString comm = d->localeComments.value(lang);

View File

@ -248,11 +248,11 @@ bool QMimeTypeParserBase::parse(QIODevice *dev, const QString &fileName, QString
} }
break; break;
case ParseComment: { case ParseComment: {
// comments have locale attributes. We want the default, English one // comments have locale attributes.
QString locale = atts.value(QLatin1String(localeAttributeC)).toString(); QString locale = atts.value(QLatin1String(localeAttributeC)).toString();
const QString comment = reader.readElementText(); const QString comment = reader.readElementText();
if (locale.isEmpty()) if (locale.isEmpty())
locale = QString::fromLatin1("en_US"); locale = QString::fromLatin1("default");
data.localeComments.insert(locale, comment); data.localeComments.insert(locale, comment);
} }
break; break;

View File

@ -992,6 +992,20 @@ void tst_QMimeDatabase::installNewGlobalMimeType()
const QString fooTestFile2 = QLatin1String(RESOURCE_PREFIX "magic-and-hierarchy2.foo"); const QString fooTestFile2 = QLatin1String(RESOURCE_PREFIX "magic-and-hierarchy2.foo");
QCOMPARE(db.mimeTypeForFile(fooTestFile2).name(), QString::fromLatin1("application/vnd.qnx.bar-descriptor")); QCOMPARE(db.mimeTypeForFile(fooTestFile2).name(), QString::fromLatin1("application/vnd.qnx.bar-descriptor"));
// Test if we can use the default comment
{
struct RestoreLocale
{
~RestoreLocale() { QLocale::setDefault(QLocale::c()); }
} restoreLocale;
QLocale::setDefault(QLocale("zh_CN"));
QMimeType suseymp = db.mimeTypeForName("text/x-suse-ymp");
QVERIFY(suseymp.isValid());
QCOMPARE(suseymp.comment(),
QString::fromLatin1("YaST Meta Package"));
}
// Now test removing the mimetype definitions again // Now test removing the mimetype definitions again
for (int i = 0; i < m_additionalMimeFileNames.size(); ++i) for (int i = 0; i < m_additionalMimeFileNames.size(); ++i)
QFile::remove(destDir + m_additionalMimeFileNames.at(i)); QFile::remove(destDir + m_additionalMimeFileNames.at(i));