1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Copyright (C) 2015 Klaralvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author David Faure <david.faure@kdab.com>
5** Contact: https://www.qt.io/licensing/
6**
7** This file is part of the QtCore module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial License Usage
11** Licensees holding valid commercial Qt licenses may use this file in
12** accordance with the commercial license agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and The Qt Company. For licensing terms
15** and conditions see https://www.qt.io/terms-conditions. For further
16** information use the contact form at https://www.qt.io/contact-us.
17**
18** GNU Lesser General Public License Usage
19** Alternatively, this file may be used under the terms of the GNU Lesser
20** General Public License version 3 as published by the Free Software
21** Foundation and appearing in the file LICENSE.LGPL3 included in the
22** packaging of this file. Please review the following information to
23** ensure the GNU Lesser General Public License version 3 requirements
24** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
25**
26** GNU General Public License Usage
27** Alternatively, this file may be used under the terms of the GNU
28** General Public License version 2.0 or (at your option) the GNU General
29** Public license version 3 or any later version approved by the KDE Free
30** Qt Foundation. The licenses are as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
32** included in the packaging of this file. Please review the following
33** information to ensure the GNU General Public License requirements will
34** be met: https://www.gnu.org/licenses/gpl-2.0.html and
35** https://www.gnu.org/licenses/gpl-3.0.html.
36**
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#ifndef QMIMEPROVIDER_P_H
42#define QMIMEPROVIDER_P_H
43
44//
45// W A R N I N G
46// -------------
47//
48// This file is not part of the Qt API. It exists purely as an
49// implementation detail. This header file may change from version to
50// version without notice, or even be removed.
51//
52// We mean it.
53//
54
55#include "qmimedatabase_p.h"
56
57QT_REQUIRE_CONFIG(mimetype);
58
59#include "qmimeglobpattern_p.h"
60#include <QtCore/qdatetime.h>
61#include <QtCore/qset.h>
62
63QT_BEGIN_NAMESPACE
64
65class QMimeMagicRuleMatcher;
66
67class QMimeProviderBase
68{
69public:
70 QMimeProviderBase(QMimeDatabasePrivate *db, const QString &directory);
71 virtual ~QMimeProviderBase() {}
72
73 virtual bool isValid() = 0;
74 virtual bool isInternalDatabase() const = 0;
75 virtual QMimeType mimeTypeForName(const QString &name) = 0;
76 virtual void addFileNameMatches(const QString &fileName, QMimeGlobMatchResult &result) = 0;
77 virtual void addParents(const QString &mime, QStringList &result) = 0;
78 virtual QString resolveAlias(const QString &name) = 0;
79 virtual void addAliases(const QString &name, QStringList &result) = 0;
80 virtual void findByMagic(const QByteArray &data, int *accuracyPtr, QMimeType &candidate) = 0;
81 virtual void addAllMimeTypes(QList<QMimeType> &result) = 0;
82 virtual void loadIcon(QMimeTypePrivate &) {}
83 virtual void loadGenericIcon(QMimeTypePrivate &) {}
84 virtual void ensureLoaded() {}
85
86 QString directory() const { return m_directory; }
87
88 QMimeDatabasePrivate *m_db;
89 QString m_directory;
90};
91
92/*
93 Parses the files 'mime.cache' and 'types' on demand
94 */
95class QMimeBinaryProvider : public QMimeProviderBase
96{
97public:
98 QMimeBinaryProvider(QMimeDatabasePrivate *db, const QString &directory);
99 virtual ~QMimeBinaryProvider();
100
101 bool isValid() override;
102 bool isInternalDatabase() const override;
103 QMimeType mimeTypeForName(const QString &name) override;
104 void addFileNameMatches(const QString &fileName, QMimeGlobMatchResult &result) override;
105 void addParents(const QString &mime, QStringList &result) override;
106 QString resolveAlias(const QString &name) override;
107 void addAliases(const QString &name, QStringList &result) override;
108 void findByMagic(const QByteArray &data, int *accuracyPtr, QMimeType &candidate) override;
109 void addAllMimeTypes(QList<QMimeType> &result) override;
110 static void loadMimeTypePrivate(QMimeTypePrivate &);
111 void loadIcon(QMimeTypePrivate &) override;
112 void loadGenericIcon(QMimeTypePrivate &) override;
113 void ensureLoaded() override;
114
115private:
116 struct CacheFile;
117
118 void matchGlobList(QMimeGlobMatchResult &result, CacheFile *cacheFile, int offset, const QString &fileName);
119 bool matchSuffixTree(QMimeGlobMatchResult &result, CacheFile *cacheFile, int numEntries, int firstOffset, const QString &fileName, int charPos, bool caseSensitiveCheck);
120 bool matchMagicRule(CacheFile *cacheFile, int numMatchlets, int firstOffset, const QByteArray &data);
121 QLatin1String iconForMime(CacheFile *cacheFile, int posListOffset, const QByteArray &inputMime);
122 void loadMimeTypeList();
123 bool checkCacheChanged();
124
125 CacheFile *m_cacheFile = nullptr;
126 QStringList m_cacheFileNames;
127 QSet<QString> m_mimetypeNames;
128 bool m_mimetypeListLoaded;
129};
130
131/*
132 Parses the raw XML files (slower)
133 */
134class QMimeXMLProvider : public QMimeProviderBase
135{
136public:
137 enum InternalDatabaseEnum { InternalDatabase };
138#if QT_CONFIG(mimetype_database)
139 enum : bool { InternalDatabaseAvailable = true };
140#else
141 enum : bool { InternalDatabaseAvailable = false };
142#endif
143 QMimeXMLProvider(QMimeDatabasePrivate *db, InternalDatabaseEnum);
144 QMimeXMLProvider(QMimeDatabasePrivate *db, const QString &directory);
145 ~QMimeXMLProvider();
146
147 bool isValid() override;
148 bool isInternalDatabase() const override;
149 QMimeType mimeTypeForName(const QString &name) override;
150 void addFileNameMatches(const QString &fileName, QMimeGlobMatchResult &result) override;
151 void addParents(const QString &mime, QStringList &result) override;
152 QString resolveAlias(const QString &name) override;
153 void addAliases(const QString &name, QStringList &result) override;
154 void findByMagic(const QByteArray &data, int *accuracyPtr, QMimeType &candidate) override;
155 void addAllMimeTypes(QList<QMimeType> &result) override;
156 void ensureLoaded() override;
157
158 bool load(const QString &fileName, QString *errorMessage);
159
160 // Called by the mimetype xml parser
161 void addMimeType(const QMimeType &mt);
162 void addGlobPattern(const QMimeGlobPattern &glob);
163 void addParent(const QString &child, const QString &parent);
164 void addAlias(const QString &alias, const QString &name);
165 void addMagicMatcher(const QMimeMagicRuleMatcher &matcher);
166
167private:
168 void load(const QString &fileName);
169 void load(const char *data, qsizetype len);
170
171 typedef QHash<QString, QMimeType> NameMimeTypeMap;
172 NameMimeTypeMap m_nameMimeTypeMap;
173
174 typedef QHash<QString, QString> AliasHash;
175 AliasHash m_aliases;
176
177 typedef QHash<QString, QStringList> ParentsHash;
178 ParentsHash m_parents;
179 QMimeAllGlobPatterns m_mimeTypeGlobs;
180
181 QList<QMimeMagicRuleMatcher> m_magicMatchers;
182 QStringList m_allFiles;
183};
184
185QT_END_NAMESPACE
186
187#endif // QMIMEPROVIDER_P_H
188

source code of qtbase/src/corelib/mimetypes/qmimeprovider_p.h