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
4#ifndef QMIMEGLOBPATTERN_P_H
5#define QMIMEGLOBPATTERN_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtCore/private/qglobal_p.h>
19
21
22#include <QtCore/qstringlist.h>
23#include <QtCore/qhash.h>
24
25#include <algorithm>
26
27QT_BEGIN_NAMESPACE
28
29struct QMimeGlobMatchResult
30{
31 void addMatch(const QString &mimeType, int weight, const QString &pattern,
32 qsizetype knownSuffixLength = 0);
33
34 QStringList m_matchingMimeTypes; // only those with highest weight
35 QStringList m_allMatchingMimeTypes;
36 int m_weight = 0;
37 qsizetype m_matchingPatternLength = 0;
38 qsizetype m_knownSuffixLength = 0;
39};
40
42{
43public:
44 static const unsigned MaxWeight = 100;
45 static const unsigned DefaultWeight = 50;
46 static const unsigned MinWeight = 1;
47
48 explicit QMimeGlobPattern(const QString &thePattern, const QString &theMimeType, unsigned theWeight = DefaultWeight, Qt::CaseSensitivity s = Qt::CaseInsensitive) :
51 m_weight(theWeight),
54 {
55 }
56
57 void swap(QMimeGlobPattern &other) noexcept
58 {
59 qSwap(m_pattern, other.m_pattern);
60 qSwap(m_mimeType, other.m_mimeType);
61 qSwap(m_weight, other.m_weight);
62 qSwap(m_caseSensitivity, other.m_caseSensitivity);
63 qSwap(m_patternType, other.m_patternType);
64 }
65
66 bool matchFileName(const QString &inputFileName) const;
67
68 inline const QString &pattern() const { return m_pattern; }
69 inline unsigned weight() const { return m_weight; }
70 inline const QString &mimeType() const { return m_mimeType; }
71 inline bool isCaseSensitive() const { return m_caseSensitivity == Qt::CaseSensitive; }
72
73private:
74 enum PatternType {
75 SuffixPattern,
76 PrefixPattern,
77 LiteralPattern,
78 VdrPattern, // special handling for "[0-9][0-9][0-9].vdr" pattern
79 AnimPattern, // special handling for "*.anim[1-9j]" pattern
80 OtherPattern
81 };
82 PatternType detectPatternType(QStringView pattern) const;
83
84 QString m_pattern;
85 QString m_mimeType;
86 int m_weight;
87 Qt::CaseSensitivity m_caseSensitivity;
88 PatternType m_patternType;
89};
90Q_DECLARE_SHARED(QMimeGlobPattern)
91
92using AddMatchFilterFunc = std::function<bool(const QString &)>;
93
95{
96public:
97 bool hasPattern(QStringView mimeType, QStringView pattern) const
98 {
99 auto matchesMimeAndPattern = [mimeType, pattern](const QMimeGlobPattern &e) {
100 return e.pattern() == pattern && e.mimeType() == mimeType;
101 };
102 return std::any_of(begin(), end(), matchesMimeAndPattern);
103 }
104
105 /*!
106 "noglobs" is very rare occurrence, so it's ok if it's slow
107 */
108 void removeMimeType(QStringView mimeType)
109 {
110 auto isMimeTypeEqual = [mimeType](const QMimeGlobPattern &pattern) {
111 return pattern.mimeType() == mimeType;
112 };
113 removeIf(isMimeTypeEqual);
114 }
115
116 void match(QMimeGlobMatchResult &result, const QString &fileName,
117 const AddMatchFilterFunc &filterFunc) const;
118};
119
120/*!
121 Result of the globs parsing, as data structures ready for efficient MIME type matching.
122 This contains:
123 1) a map of fast regular patterns (e.g. *.txt is stored as "txt" in a qhash's key)
124 2) a linear list of high-weight globs
125 3) a linear list of low-weight globs
126 */
128{
129public:
130 typedef QHash<QString, QStringList> PatternsMap; // MIME type -> patterns
131
132 void addGlob(const QMimeGlobPattern &glob);
133 void removeMimeType(const QString &mimeType);
134 void matchingGlobs(const QString &fileName, QMimeGlobMatchResult &result,
135 const AddMatchFilterFunc &filterFunc) const;
136 void clear();
137
138 PatternsMap m_fastPatterns; // example: "doc" -> "application/msword", "text/plain"
140 QMimeGlobPatternList m_lowWeightGlobs; // <= 50, including the non-fast 50 patterns
141};
142
143QT_END_NAMESPACE
144
145#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:25
QT_REQUIRE_CONFIG(animation)
Q_CORE_EXPORT QDebug operator<<(QDebug debug, QDir::Filters filters)
Definition qdir.cpp:2462
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