simplechatengine.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. #include "simplechatengine.h"
  26. #include "simplechat_grpc.qpb.h"
  27. #include <QGrpcHttp2Channel>
  28. #include <QGrpcUserPasswordCredentials>
  29. #include <QGrpcSslCredentials>
  30. #include <QDebug>
  31. #include <QFile>
  32. #include <QSslConfiguration>
  33. #include <QCryptographicHash>
  34. #include <QDateTime>
  35. #include <QClipboard>
  36. #include <QGuiApplication>
  37. #include <QMimeData>
  38. #include <QImage>
  39. #include <QByteArray>
  40. #include <QBuffer>
  41. using namespace qtprotobuf::examples;
  42. SimpleChatEngine::SimpleChatEngine(QObject *parent) : QObject(parent), m_client(new SimpleChatClient)
  43. , m_clipBoard(QGuiApplication::clipboard())
  44. {
  45. if (m_clipBoard) {
  46. connect(m_clipBoard, &QClipboard::dataChanged, this, &SimpleChatEngine::clipBoardContentTypeChanged);
  47. }
  48. }
  49. SimpleChatEngine::~SimpleChatEngine()
  50. {
  51. delete m_client;
  52. }
  53. void SimpleChatEngine::login(const QString &name, const QString &password)
  54. {
  55. QSslConfiguration conf = QSslConfiguration::defaultConfiguration();
  56. QFile certFile("cert.pem");
  57. certFile.open(QIODevice::ReadOnly);
  58. QByteArray cert = certFile.readAll();
  59. conf.setCaCertificates({QSslCertificate(cert)});
  60. conf.setProtocol(QSsl::TlsV1_2);
  61. conf.setAllowedNextProtocols({QSslConfiguration::ALPNProtocolHTTP2});
  62. QUrl url("https://localhost:65002");
  63. std::shared_ptr<QtProtobuf::QAbstractGrpcChannel> channel(new QtProtobuf::QGrpcHttp2Channel(url, QtProtobuf::QGrpcSslCredentials(conf) |
  64. QtProtobuf::QGrpcUserPasswordCredentials<>(name, QCryptographicHash::hash(password.toUtf8(), QCryptographicHash::Md5).toHex())));
  65. m_client->attachChannel(channel);
  66. m_client->subscribeMessageListUpdates(None());
  67. QObject::connect(m_client, &SimpleChatClient::messageListUpdated, this, [this, name](const qtprotobuf::examples::ChatMessages &messages) {
  68. if (m_userName != name) {
  69. m_userName = name;
  70. userNameChanged();
  71. loggedIn();
  72. }
  73. m_messages.reset(messages.messages());
  74. });
  75. }
  76. void SimpleChatEngine::sendMessage(const QString &content)
  77. {
  78. m_client->sendMessage(ChatMessage(QDateTime::currentMSecsSinceEpoch(), content.toUtf8(), ChatMessage::ContentType::Text));
  79. }
  80. qtprotobuf::examples::ChatMessage::ContentType SimpleChatEngine::clipBoardContentType() const
  81. {
  82. if (m_clipBoard != nullptr) {
  83. const QMimeData *mime = m_clipBoard->mimeData();
  84. if (mime != nullptr) {
  85. if (mime->hasImage() || mime->hasUrls()) {
  86. return qtprotobuf::examples::ChatMessage::ContentType::Image;
  87. } else if(mime->hasText()) {
  88. return qtprotobuf::examples::ChatMessage::ContentType::Text;
  89. }
  90. }
  91. }
  92. return qtprotobuf::examples::ChatMessage::Unknown;
  93. }
  94. void SimpleChatEngine::sendImageFromClipboard()
  95. {
  96. if (m_clipBoard == nullptr) {
  97. return;
  98. }
  99. QByteArray imgData;
  100. const QMimeData *mime = m_clipBoard->mimeData();
  101. if (mime != nullptr) {
  102. if (mime->hasImage()) {
  103. QImage img = mime->imageData().value<QImage>();
  104. QBuffer buffer(&imgData);
  105. buffer.open(QIODevice::WriteOnly);
  106. img.save(&buffer, "PNG");
  107. buffer.close();
  108. } else if (mime->hasUrls()) {
  109. QUrl imgUrl = mime->urls().first();
  110. if (!imgUrl.isLocalFile()) {
  111. qWarning() << "Only supports transfer of local images";
  112. return;
  113. }
  114. QImage img(imgUrl.toLocalFile());
  115. if (img.isNull()) {
  116. qWarning() << "Invalid image format";
  117. return;
  118. }
  119. QBuffer buffer(&imgData);
  120. buffer.open(QIODevice::WriteOnly);
  121. img.save(&buffer, "PNG");
  122. buffer.close();
  123. }
  124. }
  125. if (imgData.isEmpty()) {
  126. return;
  127. }
  128. m_client->sendMessage(ChatMessage(QDateTime::currentMSecsSinceEpoch(), imgData, qtprotobuf::examples::ChatMessage::ContentType::Image));
  129. }
  130. QString SimpleChatEngine::getImageThumbnail(const QByteArray &data) const
  131. {
  132. QImage img = QImage::fromData(data, "PNG");
  133. img = img.scaled(200, 200, Qt::KeepAspectRatio);
  134. QByteArray scaledData;
  135. QBuffer buffer(&scaledData);
  136. img.save(&buffer, "PNG");
  137. return getImage(scaledData);
  138. }