1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtCore module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #ifndef QMIMEGLOBPATTERN_P_H |
41 | #define QMIMEGLOBPATTERN_P_H |
42 | |
43 | // |
44 | // W A R N I N G |
45 | // ------------- |
46 | // |
47 | // This file is not part of the Qt API. It exists purely as an |
48 | // implementation detail. This header file may change from version to |
49 | // version without notice, or even be removed. |
50 | // |
51 | // We mean it. |
52 | // |
53 | |
54 | #include <QtCore/private/qglobal_p.h> |
55 | |
56 | QT_REQUIRE_CONFIG(mimetype); |
57 | |
58 | #include <QtCore/qstringlist.h> |
59 | #include <QtCore/qhash.h> |
60 | |
61 | QT_BEGIN_NAMESPACE |
62 | |
63 | struct QMimeGlobMatchResult |
64 | { |
65 | void addMatch(const QString &mimeType, int weight, const QString &pattern, int knownSuffixLength = 0); |
66 | |
67 | QStringList m_matchingMimeTypes; // only those with highest weight |
68 | QStringList m_allMatchingMimeTypes; |
69 | int m_weight = 0; |
70 | int m_matchingPatternLength = 0; |
71 | int m_knownSuffixLength = 0; |
72 | }; |
73 | |
74 | class QMimeGlobPattern |
75 | { |
76 | public: |
77 | static const unsigned MaxWeight = 100; |
78 | static const unsigned DefaultWeight = 50; |
79 | static const unsigned MinWeight = 1; |
80 | |
81 | explicit QMimeGlobPattern(const QString &thePattern, const QString &theMimeType, unsigned theWeight = DefaultWeight, Qt::CaseSensitivity s = Qt::CaseInsensitive) : |
82 | m_pattern(s == Qt::CaseInsensitive ? thePattern.toLower() : thePattern), |
83 | m_mimeType(theMimeType), |
84 | m_weight(theWeight), |
85 | m_caseSensitivity(s), |
86 | m_patternType(detectPatternType(pattern: m_pattern)) |
87 | { |
88 | } |
89 | |
90 | void swap(QMimeGlobPattern &other) noexcept |
91 | { |
92 | qSwap(value1&: m_pattern, value2&: other.m_pattern); |
93 | qSwap(value1&: m_mimeType, value2&: other.m_mimeType); |
94 | qSwap(value1&: m_weight, value2&: other.m_weight); |
95 | qSwap(value1&: m_caseSensitivity, value2&: other.m_caseSensitivity); |
96 | qSwap(value1&: m_patternType, value2&: other.m_patternType); |
97 | } |
98 | |
99 | bool matchFileName(const QString &inputFileName) const; |
100 | |
101 | inline const QString &pattern() const { return m_pattern; } |
102 | inline unsigned weight() const { return m_weight; } |
103 | inline const QString &mimeType() const { return m_mimeType; } |
104 | inline bool isCaseSensitive() const { return m_caseSensitivity == Qt::CaseSensitive; } |
105 | |
106 | private: |
107 | enum PatternType { |
108 | SuffixPattern, |
109 | PrefixPattern, |
110 | LiteralPattern, |
111 | VdrPattern, // special handling for "[0-9][0-9][0-9].vdr" pattern |
112 | AnimPattern, // special handling for "*.anim[1-9j]" pattern |
113 | OtherPattern |
114 | }; |
115 | PatternType detectPatternType(const QString &pattern) const; |
116 | |
117 | QString m_pattern; |
118 | QString m_mimeType; |
119 | int m_weight; |
120 | Qt::CaseSensitivity m_caseSensitivity; |
121 | PatternType m_patternType; |
122 | }; |
123 | Q_DECLARE_SHARED(QMimeGlobPattern) |
124 | |
125 | class QMimeGlobPatternList : public QList<QMimeGlobPattern> |
126 | { |
127 | public: |
128 | bool hasPattern(const QString &mimeType, const QString &pattern) const |
129 | { |
130 | const_iterator it = begin(); |
131 | const const_iterator myend = end(); |
132 | for (; it != myend; ++it) |
133 | if ((*it).pattern() == pattern && (*it).mimeType() == mimeType) |
134 | return true; |
135 | return false; |
136 | } |
137 | |
138 | /*! |
139 | "noglobs" is very rare occurrence, so it's ok if it's slow |
140 | */ |
141 | void removeMimeType(const QString &mimeType) |
142 | { |
143 | auto isMimeTypeEqual = [&mimeType](const QMimeGlobPattern &pattern) { |
144 | return pattern.mimeType() == mimeType; |
145 | }; |
146 | erase(afirst: std::remove_if(first: begin(), last: end(), pred: isMimeTypeEqual), alast: end()); |
147 | } |
148 | |
149 | void match(QMimeGlobMatchResult &result, const QString &fileName) const; |
150 | }; |
151 | |
152 | /*! |
153 | Result of the globs parsing, as data structures ready for efficient MIME type matching. |
154 | This contains: |
155 | 1) a map of fast regular patterns (e.g. *.txt is stored as "txt" in a qhash's key) |
156 | 2) a linear list of high-weight globs |
157 | 3) a linear list of low-weight globs |
158 | */ |
159 | class QMimeAllGlobPatterns |
160 | { |
161 | public: |
162 | typedef QHash<QString, QStringList> PatternsMap; // MIME type -> patterns |
163 | |
164 | void addGlob(const QMimeGlobPattern &glob); |
165 | void removeMimeType(const QString &mimeType); |
166 | void matchingGlobs(const QString &fileName, QMimeGlobMatchResult &result) const; |
167 | void clear(); |
168 | |
169 | PatternsMap m_fastPatterns; // example: "doc" -> "application/msword", "text/plain" |
170 | QMimeGlobPatternList m_highWeightGlobs; |
171 | QMimeGlobPatternList m_lowWeightGlobs; // <= 50, including the non-fast 50 patterns |
172 | }; |
173 | |
174 | QT_END_NAMESPACE |
175 | |
176 | #endif // QMIMEGLOBPATTERN_P_H |
177 | |