Sonde v1.1.0
Embeddable Model Context Protocol server for Qt applications
Loading...
Searching...
No Matches
qmltemplates_p.h
Go to the documentation of this file.
1#pragma once
2
5
6#include "trust_p.h"
7
9#include <sonde/server.h>
10
11#include <sonde/expected.h>
12
13#include <QtCore/QDateTime>
14#include <QtCore/QJsonObject>
15#include <QtCore/QJsonValue>
16#include <QtCore/QUrl>
17#include <QtCore/QList>
18#include <QtCore/QMutex>
19#include <QtCore/QString>
20#include <QtCore/QStringList>
21#include <QtQml/QQmlAbstractUrlInterceptor>
22
23#include <functional>
24#include <optional>
25
26namespace Sonde {
27
30{
31public:
33 using Answer = std::function<void(bool granted, TrustScope scope)>;
34
36 static TrustRequest create(const QString &templateName, const QList<TrustFile> &files,
39 static void expire(const TrustRequest &request);
41 static void ask(const QmlTemplateTrust *trust, const TrustRequest &request);
42
43 QString templateName;
44 QList<TrustFile> files;
46};
47
48namespace QmlTemplates {
49
50// Where the libraries and the trust file are, and the trust store: in Core (trust_p.h),
51// because the sonde command shares them.
53using Trust::filePath;
54using Trust::Library;
57using Trust::sha256;
59
63
65QList<Trust::TrustedFile> trusted(const QList<TrustFile> &files);
66
68constexpr qsizetype kMaxFileBytes = qsizetype{ 256 } * 1024;
69
72{
73 QString name;
74 QString spec;
75 bool optional = false;
76};
77
80{
81 QString name;
82 QJsonValue defaultValue;
83 QString description;
84};
85
87struct Header
88{
89 QString description;
90 QList<ObjectSlot> objects;
91 QList<ParamSlot> params;
92 QString writtenFor;
93};
94
98std::optional<QJsonObject> windowParams(const Header *header, const QJsonObject &given,
99 QString *error);
100
104{
105 QString name;
106 QString root;
107};
108
111QList<CompiledLibrary> compiledLibraries();
112
114Expected<void> registerCompiledLibrary(const QString &name, const QString &root);
115
118std::optional<Header> parseHeader(const QString &qml, QString *error);
119
121bool isTemplateName(const QString &name);
123bool isComponentName(const QString &name);
125bool isObjectName(const QString &name);
126
132class LibraryGuard : public QQmlAbstractUrlInterceptor
133{
134public:
136 void begin(const Locations &where);
138 void end();
140 QList<TrustFile> untrusted() const;
141
144 bool check(const QString &path) { return check(path, true); }
147 bool isTrusted(const QString &path) { return check(path, false); }
148
150 QUrl intercept(const QUrl &path, DataType type) override;
151
152private:
154 bool check(const QString &path, bool record);
155
156 mutable QMutex m_mutex;
157 bool m_active = false;
159 QList<TrustFile> m_untrusted;
160};
161
164{
165 QString name;
167 QString path;
168 bool component = false;
169 QByteArray data;
170 std::optional<Header> header;
171 QString headerError;
172 QString untrusted;
173 bool hidden = false;
174 QString compiled;
175
177 QString libraryLabel() const { return compiled.isEmpty() ? libraryName(library) : compiled; }
179 QUrl url() const;
180};
181
184std::optional<LibraryFile> readCompiledFile(const CompiledLibrary &library, const QString &name,
185 bool component);
186
189std::optional<LibraryFile> findTemplate(const Locations &where, const QString &name);
190
192LibraryFile readLibraryFile(const Locations &where, const QString &name, Library library,
193 bool component);
194
196bool checkName(const QString &name, bool component, QString *error);
197
199QString untrustedReply(const QString &templateName, const QList<TrustFile> &files,
200 const Locations &where);
201
204{
206 std::function<bool(const QByteArray &, const QString &, QString *)> compiles;
208 std::function<void()> clearCache;
209};
210
213void installTemplateTools(Server &server, const TemplateEngine &engine);
214
215} // namespace QmlTemplates
216
217} // namespace Sonde
A value of type T, or an error of type E, as std::expected.
Definition expected.h:106
Lets the application ask its own user to trust a QML template.
Definition qmltemplatetrust.h:108
Checks every file the QML engine loads from a library against the trust store.
Definition qmltemplates_p.h:133
bool check(const QString &path)
Checks path now, as intercept() would, recording it if it isn't trusted.
Definition qmltemplates_p.h:144
QUrl intercept(const QUrl &path, DataType type) override
See QQmlAbstractUrlInterceptor.
Definition qmltemplates.cpp:519
QList< TrustFile > m_untrusted
What was refused since begin().
Definition qmltemplates_p.h:159
bool m_active
Whether files are checked.
Definition qmltemplates_p.h:157
Locations m_where
The libraries and store being checked against.
Definition qmltemplates_p.h:158
QList< TrustFile > untrusted() const
The untrusted files recorded since begin().
Definition qmltemplates.cpp:468
void end()
Stops checking (for compiling a template before it is saved).
Definition qmltemplates.cpp:462
void begin(const Locations &where)
Starts checking, for the libraries and store in where; clears the record.
Definition qmltemplates.cpp:452
bool isTrusted(const QString &path)
Whether path is trusted, without recording it (looking for a file that may be passed over,...
Definition qmltemplates_p.h:147
QMutex m_mutex
Guards everything below.
Definition qmltemplates_p.h:156
An embeddable Model Context Protocol server.
Definition server.h:92
A request's shared state; TrustRequest is a handle to it.
Definition qmltemplates_p.h:30
static TrustRequest create(const QString &templateName, const QList< TrustFile > &files, Answer answer)
Creates a request for files of templateName, answered through answer.
Definition qmltemplates.cpp:27
QList< TrustFile > files
See TrustRequest::files().
Definition qmltemplates_p.h:44
static void ask(const QmlTemplateTrust *trust, const TrustRequest &request)
Gives request to trust's approval, if it has one.
Definition qmltemplates.cpp:89
std::function< void(bool granted, TrustScope scope)> Answer
The answer: whether the user granted, and where.
Definition qmltemplates_p.h:33
static void expire(const TrustRequest &request)
Answers request (if it wasn't yet), without calling its answer function.
Definition qmltemplates.cpp:37
Answer answer
Called once, by the first grant() or deny(); empty once answered.
Definition qmltemplates_p.h:45
QString templateName
See TrustRequest::templateName().
Definition qmltemplates_p.h:43
A pending request for trust, from ui_open.
Definition qmltemplatetrust.h:53
The trust store: which file contents the user trusts, and where.
Definition trust_p.h:102
Sonde::Expected, Sonde::Unexpected and Sonde::Error: results that are a value or an error (design 003...
Embeddable Model Context Protocol server for Qt applications.
Definition dispatch.cpp:10
TrustScope
Where a grant of trust applies.
Definition qmltemplatetrust.h:19
std::optional< LibraryFile > readCompiledFile(const CompiledLibrary &library, const QString &name, bool component)
Reads template or component name of compiled-in library library, if it has one by that name.
Definition qmltemplates.cpp:401
std::optional< LibraryFile > findTemplate(const Locations &where, const QString &name)
Finds template name: in the project library, the user library, then the compiled-in libraries,...
Definition qmltemplates.cpp:419
bool isObjectName(const QString &name)
Whether name can be the name of an object a template is given.
Definition qmltemplates.cpp:304
std::optional< Header > parseHeader(const QString &qml, QString *error)
Reads the header at the top of qml.
Definition qmltemplates.cpp:251
QList< Trust::TrustedFile > trusted(const QList< TrustFile > &files)
files as the trust store records them.
Definition qmltemplates.cpp:442
std::optional< QJsonObject > windowParams(const Header *header, const QJsonObject &given, QString *error)
The settings for a window from header's defaults and the call's given: unknown names and values of an...
Definition qmltemplates.cpp:311
bool isComponentName(const QString &name)
Whether name can be a component's name: a QML type name, at most 64.
Definition qmltemplates.cpp:296
bool checkName(const QString &name, bool component, QString *error)
Checks that name suits a template, or a component if component.
Definition qmltemplates.cpp:551
QString untrustedReply(const QString &templateName, const QList< TrustFile > &files, const Locations &where)
The refusal for files, with the commands that trust them.
Definition qmltemplates.cpp:566
bool isTemplateName(const QString &name)
Whether name can be a template's name: [a-z0-9][a-z0-9-]*, at most 64.
Definition qmltemplates.cpp:290
Locations locate()
Finds the libraries and the trust file now, searching for the project library above the executable,...
Definition qmltemplates.cpp:435
Expected< void > registerCompiledLibrary(const QString &name, const QString &root)
Registers a compiled-in library (addTemplateLibrary()).
Definition qmltemplates.cpp:363
QList< CompiledLibrary > compiledLibraries()
The compiled-in libraries, in the order they are searched: the application's, in the order they were ...
Definition qmltemplates.cpp:356
LibraryFile readLibraryFile(const Locations &where, const QString &name, Library library, bool component)
Reads template or component name of library, with its header and trust.
Definition qmltemplates.cpp:531
void installTemplateTools(Server &server, const TemplateEngine &engine)
Registers ui_template_list, ui_template_save and ui_template_delete (qmltemplatetools....
Definition qmltemplatetools.cpp:357
QString libraryName(Library library)
"project" or "user".
Definition trust.cpp:84
constexpr qsizetype kMaxFileBytes
Most bytes in one template or component.
Definition qmltemplates_p.h:68
Asking the application's user to trust a QML template (design 0023).
Sonde::Server, the embeddable MCP server.
A library compiled into the application as Qt resources: read-only, and needing no trust (design 0034...
Definition qmltemplates_p.h:104
QString root
Its resource directory, such as ":/sonde/instruments".
Definition qmltemplates_p.h:106
QString name
Its name, as ui_template_list shows it.
Definition qmltemplates_p.h:105
A template's header.
Definition qmltemplates_p.h:88
QList< ObjectSlot > objects
The objects it expects.
Definition qmltemplates_p.h:90
QString writtenFor
The application it was written for, if given.
Definition qmltemplates_p.h:92
QString description
One line, required.
Definition qmltemplates_p.h:89
QList< ParamSlot > params
The settings it takes.
Definition qmltemplates_p.h:91
A template or component found in a library.
Definition qmltemplates_p.h:164
std::optional< Header > header
A template's header, if it parses.
Definition qmltemplates_p.h:170
QString path
Its file.
Definition qmltemplates_p.h:167
QString compiled
The compiled-in library it is in, or empty for project and user.
Definition qmltemplates_p.h:174
QString libraryLabel() const
The name of its library: "project", "user", or a compiled-in one's.
Definition qmltemplates_p.h:177
QByteArray data
Its content.
Definition qmltemplates_p.h:169
QString name
Template name, or component type name.
Definition qmltemplates_p.h:165
QUrl url() const
The URL the engine loads it from.
Definition qmltemplates.cpp:394
QString headerError
Why a template's header doesn't parse.
Definition qmltemplates_p.h:171
Library library
Which library it is in.
Definition qmltemplates_p.h:166
bool component
Whether it is a component.
Definition qmltemplates_p.h:168
bool hidden
A template hidden by one of the same name in an earlier library.
Definition qmltemplates_p.h:173
QString untrusted
Why it isn't trusted here; empty if it is.
Definition qmltemplates_p.h:172
An object a template expects, from its header.
Definition qmltemplates_p.h:72
bool optional
Whether it may be missing (then it is null).
Definition qmltemplates_p.h:75
QString name
The context property's name.
Definition qmltemplates_p.h:73
QString spec
The default object spec.
Definition qmltemplates_p.h:74
A setting a template declares, from its header (design 0034).
Definition qmltemplates_p.h:80
QString description
What it is for; may be empty.
Definition qmltemplates_p.h:83
QString name
The setting's name, as sonde.params has it.
Definition qmltemplates_p.h:81
QJsonValue defaultValue
Its default; its JSON type is the setting's type.
Definition qmltemplates_p.h:82
What the template tools need from ui_open's windows (qmlwindowtools.cpp).
Definition qmltemplates_p.h:204
std::function< void()> clearCache
Forgets compiled QML, so the next load reads the files again.
Definition qmltemplates_p.h:208
std::function< bool(const QByteArray &, const QString &, QString *)> compiles
Whether QML compiles as the file at a path would; if not, why, in the third argument.
Definition qmltemplates_p.h:206
Where the libraries and the trust file are.
Definition trust_p.h:38
QString libraryName(Library library)
"project" or "user".
Definition trust.cpp:84
QString sha256(const QByteArray &data)
The SHA-256 of data, in hex.
Definition trust.cpp:162
QString canonicalOrClean(const QString &path)
path made canonical if it exists, otherwise only absolute and clean.
Definition trust.cpp:105
QString filePath(const QString &directory, const QString &name, bool component)
The file of template or component name in directory.
Definition trust.cpp:156
Private: where the QML template libraries and the trust file are, and the trust store (designs 0023 a...
Library
The two libraries.
Definition trust_p.h:20
@ Project
.sonde/qml/ in the checkout.
Definition trust_p.h:21