Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qmimeglobpattern_p.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#ifndef QMIMEGLOBPATTERN_P_H
6#define QMIMEGLOBPATTERN_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtCore/private/qglobal_p.h>
20
22
23#include <QtCore/qstringlist.h>
24#include <QtCore/qhash.h>
25#if QT_CONFIG(regularexpression)
26#include <QtCore/qregularexpression.h>
27#endif
28
29#include <algorithm>
30#include <optional>
31
32QT_BEGIN_NAMESPACE
33
34struct QMimeGlobMatchResult
35{
36 void addMatch(const QString &mimeType, int weight, const QString &pattern,
37 qsizetype knownSuffixLength = 0);
38
39 QStringList m_matchingMimeTypes; // only those with highest weight
40 QStringList m_allMatchingMimeTypes;
41 int m_weight = 0;
42 qsizetype m_matchingPatternLength = 0;
43 qsizetype m_knownSuffixLength = 0;
44};
45
47{
48public:
49 static const unsigned MaxWeight = 100;
50 static const unsigned DefaultWeight = 50;
51 static const unsigned MinWeight = 1;
52
53 explicit QMimeGlobPattern(const QString &thePattern, const QString &theMimeType,
54 unsigned theWeight = DefaultWeight,
55 Qt::CaseSensitivity s = Qt::CaseInsensitive) :
58 m_weight(theWeight),
60 m_patternType(detectPatternType(m_pattern))
61 {
62#if QT_CONFIG(regularexpression)
63 if (m_patternType == OtherPattern && !m_pattern.isEmpty())
64 m_regExp = QRegularExpression::fromWildcard(m_pattern);
65#endif
66 }
67
68 void swap(QMimeGlobPattern &other) noexcept
69 {
70 qSwap(m_pattern, other.m_pattern);
71 qSwap(m_mimeType, other.m_mimeType);
72 qSwap(m_weight, other.m_weight);
73 qSwap(m_caseSensitivity, other.m_caseSensitivity);
74 qSwap(m_patternType, other.m_patternType);
75#if QT_CONFIG(regularexpression)
76 qSwap(m_regExp, other.m_regExp);
77#endif
78 }
79
80 bool matchFileName(const QString &fileName) const;
81
82 inline const QString &pattern() const { return m_pattern; }
83 inline unsigned weight() const { return m_weight; }
84 inline const QString &mimeType() const { return m_mimeType; }
85 inline bool isCaseSensitive() const { return m_caseSensitivity == Qt::CaseSensitive; }
86
91 VdrPattern, // special handling for "[0-9][0-9][0-9].vdr" pattern
92 AnimPattern, // special handling for "*.anim[1-9j]" pattern
94 };
95 static PatternType detectPatternType(QLatin1StringView pattern);
96 template <typename Pattern>
97 static bool matchWithoutRegularExpression(PatternType patternType, Pattern pattern,
98 QStringView fileName, Qt::CaseSensitivity cs);
99
100private:
101 static PatternType detectPatternType(QStringView pattern);
102 template <typename Pattern>
103 static PatternType patternTypeOf(Pattern pattern);
104
105 QString m_pattern;
106 QString m_mimeType;
107 int m_weight;
108 Qt::CaseSensitivity m_caseSensitivity;
109 PatternType m_patternType;
110#if QT_CONFIG(regularexpression)
112#endif
113};
114Q_DECLARE_SHARED(QMimeGlobPattern)
115
116using AddMatchFilterFunc = std::function<bool(const QString &)>;
117
119{
120public:
121 bool hasPattern(QStringView mimeType, QStringView pattern) const
122 {
123 auto matchesMimeAndPattern = [mimeType, pattern](const QMimeGlobPattern &e) {
124 return e.pattern() == pattern && e.mimeType() == mimeType;
125 };
126 return std::any_of(begin(), end(), matchesMimeAndPattern);
127 }
128
129 /*!
130 "noglobs" is very rare occurrence, so it's ok if it's slow
131 */
132 void removeMimeType(QStringView mimeType)
133 {
134 auto isMimeTypeEqual = [mimeType](const QMimeGlobPattern &pattern) {
135 return pattern.mimeType() == mimeType;
136 };
137 removeIf(isMimeTypeEqual);
138 }
139
140 void match(QMimeGlobMatchResult &result, const QString &fileName,
141 const AddMatchFilterFunc &filterFunc) const;
142};
143
144/*!
145 Result of the globs parsing, as data structures ready for efficient MIME type matching.
146 This contains:
147 1) a map of fast regular patterns (e.g. *.txt is stored as "txt" in a qhash's key)
148 2) a linear list of high-weight globs
149 3) a linear list of low-weight globs
150 */
152{
153public:
154 typedef QHash<QString, QStringList> PatternsMap; // MIME type -> patterns
155
156 void addGlob(const QMimeGlobPattern &glob);
157 void removeMimeType(const QString &mimeType);
158 void matchingGlobs(const QString &fileName, QMimeGlobMatchResult &result,
159 const AddMatchFilterFunc &filterFunc) const;
160 void clear();
161
162 PatternsMap m_fastPatterns; // example: "doc" -> "application/msword", "text/plain"
164 QMimeGlobPatternList m_lowWeightGlobs; // <= 50, including the non-fast 50 patterns
165};
166
167QT_END_NAMESPACE
168
169#endif // QMIMEGLOBPATTERN_P_H
Result of the globs parsing, as data structures ready for efficient MIME type matching.
void addGlob(const QMimeGlobPattern &glob)
void matchingGlobs(const QString &fileName, QMimeGlobMatchResult &result, const AddMatchFilterFunc &filterFunc) const
void removeMimeType(const QString &mimeType)
QHash< QString, QStringList > PatternsMap
QMimeGlobPatternList m_highWeightGlobs
QMimeGlobPatternList m_lowWeightGlobs
QString resolveAlias(const QString &nameOrAlias)
QStringList listAliases(const QString &mimeName)
QList< QMimeType > allMimeTypes()
QString genericIcon(const QString &name)
QMimeTypePrivate::LocaleHash localeComments(const QString &name)
bool mimeInherits(const QString &mime, const QString &parent)
QMimeType mimeTypeForFileExtension(const QString &fileName)
QMimeType mimeTypeForFileNameAndData(const QString &fileName, QIODevice *device)
QMimeType mimeTypeForData(QIODevice *device)
QStringList mimeTypeForFileName(const QString &fileName)
bool inherits(const QString &mime, const QString &parent)
QMimeType mimeTypeForName(const QString &nameOrAlias)
QStringList mimeParents(const QString &mimeName)
QStringList globPatterns(const QString &name)
const QString & defaultMimeType() const
QString icon(const QString &name)
static QMimeDatabasePrivate * instance()
QMimeType mimeTypeForFile(const QString &fileName, const QFileInfo &fileInfo, QMimeDatabase::MatchMode mode)
QStringList parents(const QString &mimeName)
QMimeGlobMatchResult findByFileName(const QString &fileName)
bool magicRulesReject(const QString &mime, const QByteArray &data)
QMimeType findByData(const QByteArray &data, int *priorityPtr)
\inmodule QtCore
QMimeType mimeTypeForName(const QString &nameOrAlias) const
Returns a MIME type for nameOrAlias or an invalid one if none found.
QMimeType mimeTypeForFileNameAndData(const QString &fileName, QIODevice *device) const
Returns a MIME type for the given fileName and device data.
QMimeType mimeTypeForFile(const QFileInfo &fileInfo, MatchMode mode=MatchDefault) const
Returns a MIME type for fileInfo.
QList< QMimeType > mimeTypesForFileName(const QString &fileName) const
Returns the MIME types for the file name fileName.
QMimeType mimeTypeForUrl(const QUrl &url) const
Returns a MIME type for url.
QMimeType mimeTypeForData(const QByteArray &data) const
Returns a MIME type for data.
QString suffixForFileName(const QString &fileName) const
Returns the suffix for the file fileName, as known by the MIME database.
MatchMode
This enum specifies how matching a file to a MIME type is performed.
static void reload()
QList< QMimeType > allMimeTypes() const
Returns the list of all available MIME types.
QMimeType mimeTypeForFileNameAndData(const QString &fileName, const QByteArray &data) const
Returns a MIME type for the given fileName and device data.
QMimeType mimeTypeForData(QIODevice *device) const
Returns a MIME type for the data in device.
QMimeType mimeTypeForFile(const QString &fileName, MatchMode mode=MatchDefault) const
Returns a MIME type for the file named fileName using mode.
bool hasPattern(QStringView mimeType, QStringView pattern) const
void removeMimeType(QStringView mimeType)
"noglobs" is very rare occurrence, so it's ok if it's slow
void match(QMimeGlobMatchResult &result, const QString &fileName, const AddMatchFilterFunc &filterFunc) const
The QMimeGlobPattern class contains the glob pattern for file names for MIME type matching.
unsigned weight() const
static const unsigned MinWeight
QMimeGlobPattern(const QString &thePattern, const QString &theMimeType, unsigned theWeight=DefaultWeight, Qt::CaseSensitivity s=Qt::CaseInsensitive)
static const unsigned DefaultWeight
void swap(QMimeGlobPattern &other) noexcept
static bool matchWithoutRegularExpression(PatternType patternType, Pattern pattern, QStringView fileName, Qt::CaseSensitivity cs)
const QString & mimeType() const
static const unsigned MaxWeight
const QString & pattern() const
static PatternType detectPatternType(QLatin1StringView pattern)
bool matchFileName(const QString &fileName) const
bool isCaseSensitive() const
\inmodule QtCore
Definition qmimetype.h:26
QT_REQUIRE_CONFIG(animation)
Q_CORE_EXPORT QDebug operator<<(QDebug debug, QDir::Filters filters)
Definition qdir.cpp:2620
static bool isTextFile(const QByteArray &data)
static QStringList locateMimeDirectories()
static QString directoryMimeType()
static QString plainTextMimeType()
static constexpr auto qmime_secondsBetweenChecks
QT_REQUIRE_CONFIG(mimetype)
bool isValid() const