Branch data Line data Source code
1 : : // Copyright (C) 2021 The Qt Company Ltd.
2 : : // Copyright (C) 2019 Luxoft Sweden AB
3 : : // Copyright (C) 2018 Pelagicore AG
4 : : // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
5 : :
6 : : #include <QQmlInfo>
7 : :
8 : : #include "intenthandler.h"
9 : : #include "intentclient.h"
10 : :
11 : : QT_BEGIN_NAMESPACE_AM
12 : :
13 : 73 : AbstractIntentHandler::AbstractIntentHandler(QObject *parent)
14 [ + - ]: 73 : : QObject(parent)
15 : 73 : { }
16 : :
17 : 73 : AbstractIntentHandler::~AbstractIntentHandler()
18 : : {
19 [ + - ]: 73 : if (auto ie = IntentClient::instance())
20 : 73 : ie->unregisterHandler(this);
21 : 73 : }
22 : :
23 : 95 : QStringList AbstractIntentHandler::intentIds() const
24 : : {
25 : 95 : return m_intentIds;
26 : : }
27 : :
28 : : /*! \qmltype IntentHandler
29 : : \inqmlmodule QtApplicationManager.Application
30 : : \ingroup app-instantiatable
31 : : \brief A handler for intent requests received by applications.
32 : :
33 : : Any application that has intents listed in its manifest file needs to have a corresponding
34 : : IntentHandler instance that is actually able to handle incoming requests. This class gives
35 : : you the flexibility to handle multiple, different intent ids via a single IntentHandler
36 : : instance or have a dedicated IntentHandler instance for every intent id (or any combination of
37 : : those).
38 : :
39 : : \note For handling intent requests within the System UI, you have to use the System UI side
40 : : component IntentServerHandler, which works the same way, but provides all the necessary
41 : : meta-data from within QML.
42 : :
43 : : Here is a fairly standard way to handle an incoming intent request and send out a result or
44 : : error message:
45 : :
46 : : \qml
47 : : Image {
48 : : id: viewer
49 : : }
50 : :
51 : : IntentHandler {
52 : : intentIds: [ "show-image" ]
53 : : onRequestReceived: {
54 : : var url = request.parameters["url"]
55 : : if (!url.startsWith("file://")) {
56 : : request.sendErrorReply("Only file:// urls are supported")
57 : : } else {
58 : : viewer.source = url
59 : : request.sendReply({ "status": source.status })
60 : : }
61 : : }
62 : : }
63 : : \endqml
64 : :
65 : : */
66 : :
67 : : /*! \qmlproperty list<string> IntentHandler::intentIds
68 : :
69 : : Every handler needs to register at least one unique intent id that it will handle. Having
70 : : multiple IntentHandlers that are registering the same intent id is not possible.
71 : :
72 : : \note Any changes to this property after component completion will have no effect.
73 : : */
74 : :
75 : : /*! \qmlsignal IntentHandler::requestReceived(IntentRequest request)
76 : :
77 : : This signal will be emitted once for every incoming intent \a request that this handler was
78 : : registered for via its intentIds property.
79 : :
80 : : Handling the request can be done synchronously or asynchronously. As soon as your handler has
81 : : either produced a result or detected an error condition, it should call
82 : : IntentRequest::sendReply() or IntentRequest::sendErrorReply() respectively to send a reply back
83 : : to the requesting party. Even if your intent does not have a return value, you still need to
84 : : send an empty object \c{{}} reply to signal that the intent request has been handled
85 : : successfully.
86 : :
87 : : Only the first call to one of these functions will have any effect. Any further invocations
88 : : will be ignored.
89 : :
90 : : If these functions are not called after receiving an intent request within the \l{Intent
91 : : Timeout Specification}{system's specified timeout interval}, the system will send an implicit
92 : : "failed due to timeout" error reply back to the original sender.
93 : : */
94 : :
95 : 51 : IntentHandler::IntentHandler(QObject *parent)
96 : 51 : : AbstractIntentHandler(parent)
97 : 51 : { }
98 : :
99 : 51 : void IntentHandler::setIntentIds(const QStringList &intentIds)
100 : : {
101 [ - + ]: 51 : if (m_completed) {
102 [ # # ]: 0 : qmlWarning(this) << "Cannot change the intentIds property of an intent handler after creation.";
103 : 0 : return;
104 : : }
105 [ + - ]: 51 : if (m_intentIds != intentIds) {
106 : 51 : m_intentIds = intentIds;
107 : 102 : emit intentIdsChanged();
108 : : }
109 : : }
110 : :
111 : 51 : void IntentHandler::classBegin()
112 : 51 : { }
113 : :
114 : 51 : void IntentHandler::componentComplete()
115 : : {
116 : 51 : IntentClient::instance()->registerHandler(this);
117 : 51 : m_completed = true;
118 : 51 : }
119 : :
120 : 40 : void IntentHandler::internalRequestReceived(IntentClientRequest *request)
121 : : {
122 : 40 : emit requestReceived(request);
123 : 40 : }
124 : :
125 : : QT_END_NAMESPACE_AM
126 : :
127 : : #include "moc_intenthandler.cpp"
|