Qt 6.x
The Qt SDK
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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
26
28{
29 void addMatch(const QString &mimeType, int weight, const QString &pattern,
30 qsizetype knownSuffixLength = 0);
31
32 QStringList m_matchingMimeTypes; // only those with highest weight
34 int m_weight = 0;
37};
38
40{
41public:
42 static const unsigned MaxWeight = 100;
43 static const unsigned DefaultWeight = 50;
44 static const unsigned MinWeight = 1;
45
46 explicit QMimeGlobPattern(const QString &thePattern, const QString &theMimeType, unsigned theWeight = DefaultWeight, Qt::CaseSensitivity s = Qt::CaseInsensitive) :
47 m_pattern(s == Qt::CaseInsensitive ? thePattern.toLower() : thePattern),
48 m_mimeType(theMimeType),
49 m_weight(theWeight),
50 m_caseSensitivity(s),
51 m_patternType(detectPatternType(m_pattern))
52 {
53 }
54
55 void swap(QMimeGlobPattern &other) noexcept
56 {
57 qSwap(m_pattern, other.m_pattern);
58 qSwap(m_mimeType, other.m_mimeType);
59 qSwap(m_weight, other.m_weight);
60 qSwap(m_caseSensitivity, other.m_caseSensitivity);
61 qSwap(m_patternType, other.m_patternType);
62 }
63
64 bool matchFileName(const QString &inputFileName) const;
65
66 inline const QString &pattern() const { return m_pattern; }
67 inline unsigned weight() const { return m_weight; }
68 inline const QString &mimeType() const { return m_mimeType; }
69 inline bool isCaseSensitive() const { return m_caseSensitivity == Qt::CaseSensitive; }
70
71private:
72 enum PatternType {
73 SuffixPattern,
74 PrefixPattern,
75 LiteralPattern,
76 VdrPattern, // special handling for "[0-9][0-9][0-9].vdr" pattern
77 AnimPattern, // special handling for "*.anim[1-9j]" pattern
78 OtherPattern
79 };
80 PatternType detectPatternType(const QString &pattern) const;
81
82 QString m_pattern;
83 QString m_mimeType;
84 int m_weight;
85 Qt::CaseSensitivity m_caseSensitivity;
86 PatternType m_patternType;
87};
88Q_DECLARE_SHARED(QMimeGlobPattern)
89
91{
92public:
93 bool hasPattern(const QString &mimeType, const QString &pattern) const
94 {
95 const_iterator it = begin();
96 const const_iterator myend = end();
97 for (; it != myend; ++it)
98 if ((*it).pattern() == pattern && (*it).mimeType() == mimeType)
99 return true;
100 return false;
101 }
102
107 {
108 auto isMimeTypeEqual = [&mimeType](const QMimeGlobPattern &pattern) {
109 return pattern.mimeType() == mimeType;
110 };
111 removeIf(isMimeTypeEqual);
112 }
113
114 void match(QMimeGlobMatchResult &result, const QString &fileName) const;
115};
116
125{
126public:
127 typedef QHash<QString, QStringList> PatternsMap; // MIME type -> patterns
128
129 void addGlob(const QMimeGlobPattern &glob);
130 void removeMimeType(const QString &mimeType);
132 void clear();
133
134 PatternsMap m_fastPatterns; // example: "doc" -> "application/msword", "text/plain"
136 QMimeGlobPatternList m_lowWeightGlobs; // <= 50, including the non-fast 50 patterns
137};
138
140
141#endif // QMIMEGLOBPATTERN_P_H
\inmodule QtCore
Definition qhash.h:818
Definition qlist.h:74
Result of the globs parsing, as data structures ready for efficient MIME type matching.
void addGlob(const QMimeGlobPattern &glob)
void removeMimeType(const QString &mimeType)
void matchingGlobs(const QString &fileName, QMimeGlobMatchResult &result) const
QHash< QString, QStringList > PatternsMap
QMimeGlobPatternList m_highWeightGlobs
QMimeGlobPatternList m_lowWeightGlobs
void removeMimeType(const QString &mimeType)
"noglobs" is very rare occurrence, so it's ok if it's slow
bool hasPattern(const QString &mimeType, const QString &pattern) 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
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
qSwap(pi, e)
QSet< QString >::iterator it
Combined button and popup list for selecting options.
CaseSensitivity
@ CaseInsensitive
@ CaseSensitive
const char * mimeType
GLuint GLuint end
GLuint GLuint GLfloat weight
GLuint64EXT * result
[6]
GLdouble s
[6]
Definition qopenglext.h:235
GLubyte * pattern
QtPrivate::QRegularExpressionMatchIteratorRangeBasedForIterator begin(const QRegularExpressionMatchIterator &iterator)
#define QT_REQUIRE_CONFIG(feature)
static bool match(const uchar *found, uint foundLen, const char *target, uint targetLen)
ptrdiff_t qsizetype
Definition qtypes.h:70
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