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 QtQml 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 QQMLIMPORT_P_H
41#define QQMLIMPORT_P_H
42
43#include <QtCore/qurl.h>
44#include <QtCore/qcoreapplication.h>
45#include <QtCore/qset.h>
46#include <QtCore/qstringlist.h>
47#include <QtQml/qqmlerror.h>
48#include <private/qqmldirparser_p.h>
49#include <private/qqmltype_p.h>
50#include <private/qstringhash_p.h>
51
52//
53// W A R N I N G
54// -------------
55//
56// This file is not part of the Qt API. It exists purely as an
57// implementation detail. This header file may change from version to
58// version without notice, or even be removed.
59//
60// We mean it.
61//
62
63QT_BEGIN_NAMESPACE
64
65class QQmlTypeNameCache;
66class QQmlEngine;
67class QDir;
68class QQmlImportNamespace;
69class QQmlImportsPrivate;
70class QQmlImportDatabase;
71class QQmlTypeLoader;
72class QQmlTypeLoaderQmldirContent;
73
74namespace QQmlImport {
75 enum RecursionRestriction { PreventRecursion, AllowRecursion };
76}
77
78struct QQmlImportInstance
79{
80 QString uri; // e.g. QtQuick
81 QString url; // the base path of the import
82 QString localDirectoryPath; // the base path of the import if it's a local file
83 int majversion; // the major version imported
84 int minversion; // the minor version imported
85 bool isLibrary; // true means that this is not a file import
86 bool implicitlyImported = false;
87 QQmlDirComponents qmlDirComponents; // a copy of the components listed in the qmldir
88 QQmlDirScripts qmlDirScripts; // a copy of the scripts in the qmldir
89
90 bool setQmldirContent(const QString &resolvedUrl, const QQmlTypeLoaderQmldirContent &qmldir,
91 QQmlImportNamespace *nameSpace, QList<QQmlError> *errors);
92
93 static QQmlDirScripts getVersionedScripts(const QQmlDirScripts &qmldirscripts, int vmaj, int vmin);
94
95 bool resolveType(QQmlTypeLoader *typeLoader, const QHashedStringRef &type,
96 int *vmajor, int *vminor, QQmlType* type_return,
97 QString *base = nullptr, bool *typeRecursionDetected = nullptr,
98 QQmlType::RegistrationType = QQmlType::AnyRegistrationType,
99 QQmlImport::RecursionRestriction recursionRestriction = QQmlImport::PreventRecursion,
100 QList<QQmlError> *errors = nullptr) const;
101};
102
103class QQmlImportNamespace
104{
105public:
106 QQmlImportNamespace() : nextNamespace(nullptr) {}
107 ~QQmlImportNamespace() { qDeleteAll(imports); }
108
109 QList<QQmlImportInstance *> imports;
110
111 QQmlImportInstance *findImport(const QString &uri) const;
112
113 bool resolveType(QQmlTypeLoader *typeLoader, const QHashedStringRef& type,
114 int *vmajor, int *vminor, QQmlType* type_return,
115 QString *base = nullptr, QList<QQmlError> *errors = nullptr,
116 QQmlType::RegistrationType registrationType = QQmlType::AnyRegistrationType,
117 QQmlImport::RecursionRestriction recursionRestriction = QQmlImport::PreventRecursion);
118
119 // Prefix when used as a qualified import. Otherwise empty.
120 QHashedString prefix;
121
122 // Used by QQmlImportsPrivate::qualifiedSets
123 QQmlImportNamespace *nextNamespace;
124};
125
126class Q_QML_PRIVATE_EXPORT QQmlImports
127{
128public:
129 enum ImportVersion { FullyVersioned, PartiallyVersioned, Unversioned };
130
131 QQmlImports(QQmlTypeLoader *);
132 QQmlImports(const QQmlImports &);
133 ~QQmlImports();
134 QQmlImports &operator=(const QQmlImports &);
135
136 void setBaseUrl(const QUrl &url, const QString &urlString = QString());
137 QUrl baseUrl() const;
138
139 bool resolveType(const QHashedStringRef &type,
140 QQmlType *type_return,
141 int *version_major, int *version_minor,
142 QQmlImportNamespace **ns_return,
143 QList<QQmlError> *errors = nullptr,
144 QQmlType::RegistrationType registrationType = QQmlType::AnyRegistrationType,
145 QQmlImport::RecursionRestriction recursionRestriction
146 = QQmlImport::PreventRecursion) const;
147 bool resolveType(QQmlImportNamespace *,
148 const QHashedStringRef& type,
149 QQmlType *type_return, int *version_major, int *version_minor,
150 QQmlType::RegistrationType registrationType
151 = QQmlType::AnyRegistrationType) const;
152
153 bool addImplicitImport(QQmlImportDatabase *importDb, QList<QQmlError> *errors);
154
155 bool addFileImport(QQmlImportDatabase *,
156 const QString& uri, const QString& prefix, int vmaj, int vmin, bool incomplete,
157 QList<QQmlError> *errors);
158
159 bool addLibraryImport(QQmlImportDatabase *importDb,
160 const QString &uri, const QString &prefix, int vmaj, int vmin,
161 const QString &qmldirIdentifier, const QString &qmldirUrl, bool incomplete, QList<QQmlError> *errors);
162
163 bool updateQmldirContent(QQmlImportDatabase *importDb,
164 const QString &uri, const QString &prefix,
165 const QString &qmldirIdentifier, const QString &qmldirUrl, QList<QQmlError> *errors);
166
167 bool locateQmldir(QQmlImportDatabase *,
168 const QString &uri, int vmaj, int vmin,
169 QString *qmldirFilePath, QString *url);
170
171 void populateCache(QQmlTypeNameCache *cache) const;
172
173 struct ScriptReference
174 {
175 QString nameSpace;
176 QString qualifier;
177 QUrl location;
178 };
179
180 QList<ScriptReference> resolvedScripts() const;
181
182 struct CompositeSingletonReference
183 {
184 QString typeName;
185 QString prefix;
186 int majorVersion;
187 int minorVersion;
188 };
189
190 QList<CompositeSingletonReference> resolvedCompositeSingletons() const;
191
192 static QStringList completeQmldirPaths(const QString &uri, const QStringList &basePaths, int vmaj, int vmin);
193 static QString versionString(int vmaj, int vmin, ImportVersion version);
194
195 static bool isLocal(const QString &url);
196 static bool isLocal(const QUrl &url);
197 static QUrl urlFromLocalFileOrQrcOrUrl(const QString &);
198
199 static void setDesignerSupportRequired(bool b);
200
201private:
202 friend class QQmlImportDatabase;
203 QQmlImportsPrivate *d;
204};
205
206class QQmlImportDatabase
207{
208 Q_DECLARE_TR_FUNCTIONS(QQmlImportDatabase)
209public:
210 enum PathType { Local, Remote, LocalOrRemote };
211
212 QQmlImportDatabase(QQmlEngine *);
213 ~QQmlImportDatabase();
214
215#if QT_CONFIG(library)
216 bool importDynamicPlugin(const QString &filePath, const QString &uri, const QString &importNamespace, int vmaj, QList<QQmlError> *errors);
217#endif
218
219 QStringList importPathList(PathType type = LocalOrRemote) const;
220 void setImportPathList(const QStringList &paths);
221 void addImportPath(const QString& dir);
222
223 QStringList pluginPathList() const;
224 void setPluginPathList(const QStringList &paths);
225 void addPluginPath(const QString& path);
226
227private:
228 friend class QQmlImportsPrivate;
229 QString resolvePlugin(QQmlTypeLoader *typeLoader,
230 const QString &qmldirPath, const QString &qmldirPluginPath,
231 const QString &baseName, const QStringList &suffixes,
232 const QString &prefix = QString());
233 QString resolvePlugin(QQmlTypeLoader *typeLoader,
234 const QString &qmldirPath, const QString &qmldirPluginPath,
235 const QString &baseName);
236 bool importStaticPlugin(QObject *instance, const QString &basePath, const QString &uri,
237 const QString &typeNamespace, int vmaj, QList<QQmlError> *errors);
238 bool registerPluginTypes(QObject *instance, const QString &basePath,
239 const QString &uri, const QString &typeNamespace, int vmaj, QList<QQmlError> *errors);
240 void clearDirCache();
241
242 struct QmldirCache {
243 int versionMajor;
244 int versionMinor;
245 QString qmldirFilePath;
246 QString qmldirPathUrl;
247 QmldirCache *next;
248 };
249 // Maps from an import to a linked list of qmldir info.
250 // Used in QQmlImportsPrivate::locateQmldir()
251 QStringHash<QmldirCache *> qmldirCache;
252
253 // XXX thread
254 QStringList filePluginPath;
255 QStringList fileImportPath;
256
257 QSet<QString> qmlDirFilesForWhichPluginsHaveBeenLoaded;
258 QSet<QString> initializedPlugins;
259 QQmlEngine *engine;
260};
261
262void qmlClearEnginePlugins();// For internal use by qmlClearRegisteredProperties
263
264QT_END_NAMESPACE
265
266#endif // QQMLIMPORT_P_H
267
268