diff options
Diffstat (limited to 'src/corelib/mimetypes/qmimetype.cpp')
-rw-r--r-- | src/corelib/mimetypes/qmimetype.cpp | 126 |
1 files changed, 55 insertions, 71 deletions
diff --git a/src/corelib/mimetypes/qmimetype.cpp b/src/corelib/mimetypes/qmimetype.cpp index cf153fd8d5..e26c8b898d 100644 --- a/src/corelib/mimetypes/qmimetype.cpp +++ b/src/corelib/mimetypes/qmimetype.cpp @@ -6,9 +6,6 @@ #include "qmimetype_p.h" #include "qmimedatabase_p.h" -#include "qmimeprovider_p.h" - -#include "qmimeglobpattern_p.h" #include <QtCore/QDebug> #include <QtCore/QLocale> @@ -20,33 +17,6 @@ QT_BEGIN_NAMESPACE using namespace Qt::StringLiterals; -QMimeTypePrivate::QMimeTypePrivate() - : loaded(false), fromCache(false) -{} - -QMimeTypePrivate::QMimeTypePrivate(const QMimeType &other) - : loaded(other.d->loaded), - name(other.d->name), - localeComments(other.d->localeComments), - genericIconName(other.d->genericIconName), - iconName(other.d->iconName), - globPatterns(other.d->globPatterns) -{} - -void QMimeTypePrivate::clear() -{ - name.clear(); - localeComments.clear(); - genericIconName.clear(); - iconName.clear(); - globPatterns.clear(); -} - -void QMimeTypePrivate::addGlobPattern(const QString &pattern) -{ - globPatterns.append(pattern); -} - /*! \class QMimeType \inmodule QtCore @@ -54,6 +24,7 @@ void QMimeTypePrivate::addGlobPattern(const QString &pattern) \brief The QMimeType class describes types of file or data, represented by a MIME type string. \since 5.0 + \compares equality For instance a file named "readme.txt" has the MIME type "text/plain". The MIME type can be determined from the file name, or from the file @@ -70,7 +41,7 @@ void QMimeTypePrivate::addGlobPattern(const QString &pattern) MIME types can inherit from each other: for instance a C source file is a specific type of plain text file, so text/x-csrc inherits text/plain. - \sa QMimeDatabase, {MIME Type Browser Example} + \sa QMimeDatabase, {MIME Type Browser} */ /*! @@ -141,14 +112,15 @@ QMimeType::~QMimeType() } /*! - \fn bool QMimeType::operator==(const QMimeType &other) const; - Returns \c true if \a other equals this QMimeType object, otherwise returns \c false. + \fn bool QMimeType::operator==(const QMimeType &lhs, const QMimeType &rhs); + Returns \c true if \a lhs equals to the \a rhs QMimeType object, otherwise + returns \c false. The name is the unique identifier for a mimetype, so two mimetypes with the same name, are equal. */ -bool QMimeType::operator==(const QMimeType &other) const +bool comparesEqual(const QMimeType &lhs, const QMimeType &rhs) noexcept { - return d == other.d || d->name == other.d->name; + return lhs.d == rhs.d || lhs.d->name == rhs.d->name; } /*! @@ -164,8 +136,9 @@ size_t qHash(const QMimeType &key, size_t seed) noexcept } /*! - \fn bool QMimeType::operator!=(const QMimeType &other) const; - Returns \c true if \a other does not equal this QMimeType object, otherwise returns \c false. + \fn bool QMimeType::operator!=(const QMimeType &lhs, const QMimeType &rhs); + Returns \c true if QMimeType \a lhs is not equal to QMimeType \a rhs, + otherwise returns \c false. */ /*! @@ -219,24 +192,38 @@ QString QMimeType::name() const */ QString QMimeType::comment() const { - QMimeDatabasePrivate::instance()->loadMimeTypePrivate(const_cast<QMimeTypePrivate&>(*d)); + const auto localeComments = QMimeDatabasePrivate::instance()->localeComments(d->name); + + QStringList languageList = QLocale().uiLanguages(QLocale::TagSeparator::Underscore); + qsizetype defaultIndex = languageList.indexOf(u"en_US"_s); + + // Include the default locale as fall-back. + if (defaultIndex >= 0) { + // en_US is generally the default, and may be omitted from the + // overtly-named locales in the MIME type's data (QTBUG-105007). + ++defaultIndex; // Skip over en_US. + // That's typically followed by en_Latn_US and en (in that order): + if (defaultIndex < languageList.size() && languageList.at(defaultIndex) == u"en_Latn_US") + ++defaultIndex; + if (defaultIndex < languageList.size() && languageList.at(defaultIndex) == u"en") + ++defaultIndex; + } else { + // Absent en-US, just append it: + defaultIndex = languageList.size(); + } + languageList.insert(defaultIndex, u"default"_s); - QStringList languageList; - languageList << QLocale().name(); - languageList << QLocale().uiLanguages(); - languageList << u"default"_s; // use the default locale if possible. for (const QString &language : std::as_const(languageList)) { const QString lang = language == "C"_L1 ? u"en_US"_s : language; - const QString comm = d->localeComments.value(lang); + QString comm = localeComments.value(lang); if (!comm.isEmpty()) return comm; - const qsizetype pos = lang.indexOf(u'_'); - if (pos != -1) { - // "pt_BR" not found? try just "pt" - const QString shortLang = lang.left(pos); - const QString commShort = d->localeComments.value(shortLang); - if (!commShort.isEmpty()) - return commShort; + const qsizetype cut = lang.indexOf(u'_'); + // If "de_CH" is missing, check for "de" (and similar): + if (cut != -1) { + comm = localeComments.value(lang.left(cut)); + if (!comm.isEmpty()) + return comm; } } @@ -260,8 +247,8 @@ QString QMimeType::comment() const */ QString QMimeType::genericIconName() const { - QMimeDatabasePrivate::instance()->loadGenericIcon(const_cast<QMimeTypePrivate&>(*d)); - if (d->genericIconName.isEmpty()) { + QString genericIconName = QMimeDatabasePrivate::instance()->genericIcon(d->name); + if (genericIconName.isEmpty()) { // From the spec: // If the generic icon name is empty (not specified by the mimetype definition) // then the mimetype is used to generate the generic icon by using the top-level @@ -274,7 +261,7 @@ QString QMimeType::genericIconName() const groupRef = groupRef.left(slashindex); return groupRef + "-x-generic"_L1; } - return d->genericIconName; + return genericIconName; } static QString make_default_icon_name_from_mimetype_name(QString iconName) @@ -296,11 +283,11 @@ static QString make_default_icon_name_from_mimetype_name(QString iconName) */ QString QMimeType::iconName() const { - QMimeDatabasePrivate::instance()->loadIcon(const_cast<QMimeTypePrivate&>(*d)); - if (d->iconName.isEmpty()) { + QString iconName = QMimeDatabasePrivate::instance()->icon(d->name); + if (iconName.isEmpty()) { return make_default_icon_name_from_mimetype_name(name()); } - return d->iconName; + return iconName; } /*! @@ -312,8 +299,7 @@ QString QMimeType::iconName() const */ QStringList QMimeType::globPatterns() const { - QMimeDatabasePrivate::instance()->loadMimeTypePrivate(const_cast<QMimeTypePrivate&>(*d)); - return d->globPatterns; + return QMimeDatabasePrivate::instance()->globPatterns(d->name); } /*! @@ -342,14 +328,17 @@ QStringList QMimeType::parentMimeTypes() const static void collectParentMimeTypes(const QString &mime, QStringList &allParents) { const QStringList parents = QMimeDatabasePrivate::instance()->mimeParents(mime); + QStringList newParents; for (const QString &parent : parents) { // I would use QSet, but since order matters I better not - if (!allParents.contains(parent)) + if (!allParents.contains(parent)) { allParents.append(parent); + newParents.append(parent); + } } // We want a breadth-first search, so that the least-specific parent (octet-stream) is last // This means iterating twice, unfortunately. - for (const QString &parent : parents) + for (const QString &parent : newParents) collectParentMimeTypes(parent, allParents); } @@ -407,10 +396,11 @@ QStringList QMimeType::aliases() const */ QStringList QMimeType::suffixes() const { - QMimeDatabasePrivate::instance()->loadMimeTypePrivate(const_cast<QMimeTypePrivate&>(*d)); + const QStringList patterns = globPatterns(); QStringList result; - for (const QString &pattern : std::as_const(d->globPatterns)) { + result.reserve(patterns.size()); + for (const QString &pattern : patterns) { // Not a simple suffix if it looks like: README or *. or *.* or *.JP*G or *.JP? if (pattern.startsWith("*."_L1) && pattern.size() > 2 && @@ -450,17 +440,11 @@ QString QMimeType::preferredSuffix() const */ QString QMimeType::filterString() const { - QMimeDatabasePrivate::instance()->loadMimeTypePrivate(const_cast<QMimeTypePrivate&>(*d)); + const QStringList patterns = globPatterns(); QString filter; - if (!d->globPatterns.empty()) { - filter += comment() + " ("_L1; - for (int i = 0; i < d->globPatterns.size(); ++i) { - if (i != 0) - filter += u' '; - filter += d->globPatterns.at(i); - } - filter += u')'; + if (!patterns.isEmpty()) { + filter = comment() + " ("_L1 + patterns.join(u' ') + u')'; } return filter; |