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 | |
20 | QT_REQUIRE_CONFIG(mimetype); |
21 | |
22 | #include <QtCore/qstringlist.h> |
23 | #include <QtCore/qhash.h> |
24 | |
25 | QT_BEGIN_NAMESPACE |
26 | |
27 | struct QMimeGlobMatchResult |
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 |
33 | QStringList m_allMatchingMimeTypes; |
34 | int m_weight = 0; |
35 | qsizetype m_matchingPatternLength = 0; |
36 | qsizetype m_knownSuffixLength = 0; |
37 | }; |
38 | |
39 | class QMimeGlobPattern |
40 | { |
41 | public: |
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(pattern: m_pattern)) |
52 | { |
53 | } |
54 | |
55 | void swap(QMimeGlobPattern &other) noexcept |
56 | { |
57 | qSwap(value1&: m_pattern, value2&: other.m_pattern); |
58 | qSwap(value1&: m_mimeType, value2&: other.m_mimeType); |
59 | qSwap(value1&: m_weight, value2&: other.m_weight); |
60 | qSwap(value1&: m_caseSensitivity, value2&: other.m_caseSensitivity); |
61 | qSwap(value1&: m_patternType, value2&: 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 | |
71 | private: |
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 | }; |
88 | Q_DECLARE_SHARED(QMimeGlobPattern) |
89 | |
90 | class QMimeGlobPatternList : public QList<QMimeGlobPattern> |
91 | { |
92 | public: |
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 | |
103 | /*! |
104 | "noglobs" is very rare occurrence, so it's ok if it's slow |
105 | */ |
106 | void removeMimeType(const QString &mimeType) |
107 | { |
108 | auto isMimeTypeEqual = [&mimeType](const QMimeGlobPattern &pattern) { |
109 | return pattern.mimeType() == mimeType; |
110 | }; |
111 | removeIf(pred: isMimeTypeEqual); |
112 | } |
113 | |
114 | void match(QMimeGlobMatchResult &result, const QString &fileName) const; |
115 | }; |
116 | |
117 | /*! |
118 | Result of the globs parsing, as data structures ready for efficient MIME type matching. |
119 | This contains: |
120 | 1) a map of fast regular patterns (e.g. *.txt is stored as "txt" in a qhash's key) |
121 | 2) a linear list of high-weight globs |
122 | 3) a linear list of low-weight globs |
123 | */ |
124 | class QMimeAllGlobPatterns |
125 | { |
126 | public: |
127 | typedef QHash<QString, QStringList> PatternsMap; // MIME type -> patterns |
128 | |
129 | void addGlob(const QMimeGlobPattern &glob); |
130 | void removeMimeType(const QString &mimeType); |
131 | void matchingGlobs(const QString &fileName, QMimeGlobMatchResult &result) const; |
132 | void clear(); |
133 | |
134 | PatternsMap m_fastPatterns; // example: "doc" -> "application/msword", "text/plain" |
135 | QMimeGlobPatternList m_highWeightGlobs; |
136 | QMimeGlobPatternList m_lowWeightGlobs; // <= 50, including the non-fast 50 patterns |
137 | }; |
138 | |
139 | QT_END_NAMESPACE |
140 | |
141 | #endif // QMIMEGLOBPATTERN_P_H |
142 | |