simplechatengine.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2019 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 "simplechat.qpb.h"
  28. #include "simplechat_grpc.qpb.h"
  29. #include "universallistmodel.h"
  30. class QClipboard;
  31. namespace qtprotobuf {
  32. namespace examples {
  33. using ChatMessageModel = UniversalListModel<qtprotobuf::examples::ChatMessage>;
  34. class SimpleChatEngine : public QObject
  35. {
  36. Q_OBJECT
  37. Q_PROPERTY(QString userName READ userName NOTIFY userNameChanged)
  38. Q_PROPERTY(qtprotobuf::examples::ChatMessageModel *messages READ messages CONSTANT)
  39. Q_PROPERTY(qtprotobuf::examples::ChatMessage::ContentType clipBoardContentType READ clipBoardContentType NOTIFY clipBoardContentTypeChanged)
  40. ChatMessageModel m_messages;
  41. SimpleChatClient *m_client;
  42. QClipboard *m_clipBoard;
  43. QString m_userName;
  44. public:
  45. explicit SimpleChatEngine(QObject *parent = nullptr);
  46. ~SimpleChatEngine();
  47. Q_INVOKABLE void login(const QString &name, const QString &password);
  48. Q_INVOKABLE void sendMessage(const QString &message);
  49. Q_INVOKABLE QString getText(const QByteArray &data) const { return QString::fromUtf8(data); }
  50. Q_INVOKABLE QString getImage(const QByteArray &data) const { return QString("data:image/png;base64,") + data.toBase64(); }
  51. Q_INVOKABLE QString getImageThumbnail(const QByteArray &data) const;
  52. Q_INVOKABLE void sendImageFromClipboard();
  53. ChatMessageModel *messages()
  54. {
  55. return &m_messages;
  56. }
  57. qtprotobuf::examples::ChatMessage::ContentType clipBoardContentType() const;
  58. QString userName() const
  59. {
  60. return m_userName;
  61. }
  62. Q_SIGNALS:
  63. void loggedIn();
  64. void authFailed();
  65. void clipBoardContentTypeChanged();
  66. void userNameChanged();
  67. };
  68. }
  69. }
  70. Q_DECLARE_METATYPE(qtprotobuf::examples::ChatMessageModel*)