Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
28
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) :
49 m_pattern(s == Qt::CaseInsensitive ? thePattern.toLower() : thePattern),
50 m_mimeType(theMimeType),
51 m_weight(theWeight),
52 m_caseSensitivity(s),
53 m_patternType(detectPatternType(m_pattern))
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:
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
109 {
110 auto isMimeTypeEqual = [mimeType](const QMimeGlobPattern &pattern) {
111 return pattern.mimeType() == mimeType;
112 };
113 removeIf(isMimeTypeEqual);
114 }
115
117 const AddMatchFilterFunc &filterFunc) const;
118};
119
128{
129public:
130 typedef QHash<QString, QStringList> PatternsMap; // MIME type -> patterns
131
132 void addGlob(const QMimeGlobPattern &glob);
133 void removeMimeType(const QString &mimeType);
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
144
145#endif // QMIMEGLOBPATTERN_P_H
Definition qlist.h:75
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
bool hasPattern(QStringView mimeType, QStringView pattern) const
void removeMimeType(QStringView mimeType)
"noglobs" is very rare occurrence, so it's ok if it's slow
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
\inmodule QtCore
Definition qstringview.h:78
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
Combined button and popup list for selecting options.
Definition qcompare.h:63
CaseSensitivity
@ CaseInsensitive
@ CaseSensitive
const char * mimeType
std::function< bool(const QString &)> AddMatchFilterFunc
GLuint GLuint end
GLuint GLuint GLfloat weight
GLdouble s
[6]
Definition qopenglext.h:235
GLuint64EXT * result
[6]
GLubyte * pattern
QtPrivate::QRegularExpressionMatchIteratorRangeBasedForIterator begin(const QRegularExpressionMatchIterator &iterator)
QT_BEGIN_NAMESPACE constexpr void qSwap(T &value1, T &value2) noexcept(std::is_nothrow_swappable_v< T >)
Definition qswap.h:20
#define QT_REQUIRE_CONFIG(feature)
static bool match(const uchar *found, uint foundLen, const char *target, uint targetLen)
ptrdiff_t qsizetype
Definition qtypes.h:165
QSharedPointer< T > other(t)
[5]
The QMimeGlobMatchResult class accumulates results from glob matching.
void addMatch(const QString &mimeType, int weight, const QString &pattern, qsizetype knownSuffixLength=0)
QStringList m_allMatchingMimeTypes