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
26#include <algorithm>
27
28QT_BEGIN_NAMESPACE
29
30struct QMimeGlobMatchResult
31{
32 void addMatch(const QString &mimeType, int weight, const QString &pattern,
33 qsizetype knownSuffixLength = 0);
34
35 QStringList m_matchingMimeTypes; // only those with highest weight
36 QStringList m_allMatchingMimeTypes;
37 int m_weight = 0;
38 qsizetype m_matchingPatternLength = 0;
39 qsizetype m_knownSuffixLength = 0;
40};
41
43{
44public:
45 static const unsigned MaxWeight = 100;
46 static const unsigned DefaultWeight = 50;
47 static const unsigned MinWeight = 1;
48
49 explicit QMimeGlobPattern(const QString &thePattern, const QString &theMimeType,
50 unsigned theWeight = DefaultWeight,
51 Qt::CaseSensitivity s = Qt::CaseInsensitive) :
54 m_weight(theWeight),
57 {
58 }
59
60 void swap(QMimeGlobPattern &other) noexcept
61 {
62 qSwap(m_pattern, other.m_pattern);
63 qSwap(m_mimeType, other.m_mimeType);
64 qSwap(m_weight, other.m_weight);
65 qSwap(m_caseSensitivity, other.m_caseSensitivity);
66 qSwap(m_patternType, other.m_patternType);
67 }
68
69 bool matchFileName(const QString &inputFileName) const;
70
71 inline const QString &pattern() const { return m_pattern; }
72 inline unsigned weight() const { return m_weight; }
73 inline const QString &mimeType() const { return m_mimeType; }
74 inline bool isCaseSensitive() const { return m_caseSensitivity == Qt::CaseSensitive; }
75
76private:
77 enum PatternType {
78 SuffixPattern,
79 PrefixPattern,
80 LiteralPattern,
81 VdrPattern, // special handling for "[0-9][0-9][0-9].vdr" pattern
82 AnimPattern, // special handling for "*.anim[1-9j]" pattern
83 OtherPattern
84 };
85 PatternType detectPatternType(QStringView pattern) const;
86
87 QString m_pattern;
88 QString m_mimeType;
89 int m_weight;
90 Qt::CaseSensitivity m_caseSensitivity;
91 PatternType m_patternType;
92};
93Q_DECLARE_SHARED(QMimeGlobPattern)
94
95using AddMatchFilterFunc = std::function<bool(const QString &)>;
96
98{
99public:
100 bool hasPattern(QStringView mimeType, QStringView pattern) const
101 {
102 auto matchesMimeAndPattern = [mimeType, pattern](const QMimeGlobPattern &e) {
103 return e.pattern() == pattern && e.mimeType() == mimeType;
104 };
105 return std::any_of(begin(), end(), matchesMimeAndPattern);
106 }
107
108 /*!
109 "noglobs" is very rare occurrence, so it's ok if it's slow
110 */
111 void removeMimeType(QStringView mimeType)
112 {
113 auto isMimeTypeEqual = [mimeType](const QMimeGlobPattern &pattern) {
114 return pattern.mimeType() == mimeType;
115 };
116 removeIf(isMimeTypeEqual);
117 }
118
119 void match(QMimeGlobMatchResult &result, const QString &fileName,
120 const AddMatchFilterFunc &filterFunc) const;
121};
122
123/*!
124 Result of the globs parsing, as data structures ready for efficient MIME type matching.
125 This contains:
126 1) a map of fast regular patterns (e.g. *.txt is stored as "txt" in a qhash's key)
127 2) a linear list of high-weight globs
128 3) a linear list of low-weight globs
129 */
131{
132public:
133 typedef QHash<QString, QStringList> PatternsMap; // MIME type -> patterns
134
135 void addGlob(const QMimeGlobPattern &glob);
136 void removeMimeType(const QString &mimeType);
137 void matchingGlobs(const QString &fileName, QMimeGlobMatchResult &result,
138 const AddMatchFilterFunc &filterFunc) const;
139 void clear();
140
141 PatternsMap m_fastPatterns; // example: "doc" -> "application/msword", "text/plain"
143 QMimeGlobPatternList m_lowWeightGlobs; // <= 50, including the non-fast 50 patterns
144};
145
146QT_END_NAMESPACE
147
148#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)
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.
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
const QString & mimeType() const
static const unsigned MaxWeight
const QString & pattern() const
bool matchFileName(const QString &inputFileName) 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:2568
static bool isTextFile(const QByteArray &data)
static Q_CONSTINIT const int qmime_secondsBetweenChecks
static QStringList locateMimeDirectories()
static QString directoryMimeType()
static QString plainTextMimeType()
QT_REQUIRE_CONFIG(mimetype)
bool isValid() const