qquickgrpcsubscription_p.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. #pragma once
  26. #include <QObject>
  27. #include <QAbstractGrpcClient>
  28. #include <QGrpcStatus>
  29. class QJSValue;
  30. namespace QtProtobuf {
  31. class QGrpcSubscription;
  32. class QQuickGrpcSubscription : public QObject
  33. {
  34. Q_OBJECT
  35. Q_PROPERTY(QAbstractGrpcClient *client READ client WRITE setClient NOTIFY clientChanged)
  36. Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged)
  37. Q_PROPERTY(QString method READ method WRITE setMethod NOTIFY methodChanged)
  38. Q_PROPERTY(QObject *argument READ argument WRITE setArgument NOTIFY argumentChanged)
  39. public:
  40. QQuickGrpcSubscription(QObject *parent = nullptr);
  41. ~QQuickGrpcSubscription() = default;
  42. QAbstractGrpcClient *client() const {
  43. return m_client;
  44. }
  45. bool enabled() const {
  46. return m_enabled;
  47. }
  48. QString method() const {
  49. return m_method;
  50. }
  51. QObject *argument() const {
  52. return m_argument;
  53. }
  54. void setClient(QAbstractGrpcClient *client) {
  55. if (m_client == client) {
  56. return;
  57. }
  58. m_client = client;
  59. emit clientChanged();
  60. updateSubscription();
  61. }
  62. void setEnabled(bool enabled) {
  63. if (m_enabled == enabled) {
  64. return;
  65. }
  66. m_enabled = enabled;
  67. emit enabledChanged();
  68. updateSubscription();
  69. }
  70. void setMethod(QString method) {
  71. if (m_method == method) {
  72. return;
  73. }
  74. m_method = method;
  75. emit methodChanged();
  76. updateSubscription();
  77. }
  78. void setArgument(QObject *argument) {
  79. if (m_argument == argument) {
  80. return;
  81. }
  82. m_argument = argument;
  83. emit argumentChanged();
  84. updateSubscription();
  85. }
  86. signals:
  87. void updated(const QJSValue &value);
  88. void error(const QtProtobuf::QGrpcStatus &status);
  89. void clientChanged();
  90. void methodChanged();
  91. void enabledChanged();
  92. void argumentChanged();
  93. private:
  94. void updateSubscription();
  95. bool subscribe();
  96. QPointer<QAbstractGrpcClient> m_client;
  97. bool m_enabled;
  98. QString m_method;
  99. QPointer<QObject> m_argument;
  100. QGrpcSubscription *m_subscription;
  101. QObject *m_returnValue;
  102. };
  103. }