QtProtobuf  0.6
Protobuf plugin to generate Qt classes
qquickgrpcsubscription_p.h
1 /*
2  * MIT License
3  *
4  * Copyright (c) 2020 Alexey Edelev <semlanik@gmail.com>
5  *
6  * This file is part of qtprotobuf project https://git.semlanik.org/semlanik/qtprotobuf
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy of this
9  * software and associated documentation files (the "Software"), to deal in the Software
10  * without restriction, including without limitation the rights to use, copy, modify,
11  * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
12  * to permit persons to whom the Software is furnished to do so, subject to the following
13  * conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in all copies
16  * or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
19  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
20  * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
21  * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  */
25 
26 #pragma once
27 
28 #include <QObject>
29 #include <QAbstractGrpcClient>
30 #include <QGrpcStatus>
31 
89 class QJSValue;
90 
91 namespace QtProtobuf {
92 
93 class QGrpcSubscription;
94 
96 class QQuickGrpcSubscription : public QObject
97 {
98  Q_OBJECT
99  Q_PROPERTY(QAbstractGrpcClient *client READ client WRITE setClient NOTIFY clientChanged)
100  Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged)
101  Q_PROPERTY(QString method READ method WRITE setMethod NOTIFY methodChanged)
102  Q_PROPERTY(QObject *argument READ argument WRITE setArgument NOTIFY argumentChanged)
103  Q_PROPERTY(QObject *returnValue READ returnValue NOTIFY returnValueChanged)
104 
105 public:
106  QQuickGrpcSubscription(QObject *parent = nullptr);
107  ~QQuickGrpcSubscription();
108 
109  QAbstractGrpcClient *client() const {
110  return m_client;
111  }
112 
113  bool enabled() const {
114  return m_enabled;
115  }
116 
117  QString method() const {
118  return m_method;
119  }
120 
121  QObject *argument() const {
122  return m_argument;
123  }
124 
125  QObject *returnValue() const {
126  return m_returnValue;
127  }
128 
129  void setClient(QAbstractGrpcClient *client) {
130  if (m_client == client) {
131  return;
132  }
133 
134  m_client = client;
135  emit clientChanged();
136  updateSubscription();
137  }
138 
139  void setEnabled(bool enabled) {
140  if (m_enabled == enabled) {
141  return;
142  }
143 
144  m_enabled = enabled;
145  emit enabledChanged();
146  updateSubscription();
147  }
148 
149  void setMethod(QString method) {
150  if (m_method == method) {
151  return;
152  }
153 
154  m_method = method;
155  emit methodChanged();
156  updateSubscription();
157  }
158 
159  void setArgument(QObject *argument) {
160  if (m_argument == argument) {
161  return;
162  }
163 
164  m_argument = argument;
165  emit argumentChanged();
166  updateSubscription();
167  }
168 
169 signals:
170  void updated(const QJSValue &value);
171  void error(const QtProtobuf::QGrpcStatus &status);
172 
173  void clientChanged();
174  void methodChanged();
175  void enabledChanged();
176  void argumentChanged();
177  void returnValueChanged();
178 
179 private:
180  void updateSubscription();
181  bool subscribe();
182  QPointer<QAbstractGrpcClient> m_client;
183  bool m_enabled;
184  QString m_method;
185  QPointer<QObject> m_argument;
186  std::shared_ptr<QGrpcSubscription> m_subscription;
187  QObject *m_returnValue;
188 };
189 
190 }
The QGrpcStatus class contains information about last gRPC operation.
Definition: qgrpcstatus.h:58