simplechatengine.h 3.0 KB

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