simplechatengine.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. QtProtobuf::QGrpcSubscription *subscription = m_client->subscribeMessageListUpdates(None());
  67. QObject::connect(subscription, &QtProtobuf::QGrpcSubscription::error, this, [subscription] {
  68. qCritical() << "Subscription error, cancel";
  69. subscription->cancel();
  70. });
  71. QObject::connect(subscription, &QtProtobuf::QGrpcSubscription::updated, this, [this, name, subscription]() {
  72. if (m_userName != name) {
  73. m_userName = name;
  74. userNameChanged();
  75. loggedIn();
  76. }
  77. m_messages.reset(subscription->read<qtprotobuf::examples::ChatMessages>().messages());
  78. });
  79. }
  80. void SimpleChatEngine::sendMessage(const QString &content)
  81. {
  82. m_client->sendMessage(ChatMessage(static_cast<quint64>(QDateTime::currentMSecsSinceEpoch()), content.toUtf8(), ChatMessage::ContentType::Text));
  83. }
  84. qtprotobuf::examples::ChatMessage::ContentType SimpleChatEngine::clipBoardContentType() const
  85. {
  86. if (m_clipBoard != nullptr) {
  87. const QMimeData *mime = m_clipBoard->mimeData();
  88. if (mime != nullptr) {
  89. if (mime->hasImage() || mime->hasUrls()) {
  90. return qtprotobuf::examples::ChatMessage::ContentType::Image;
  91. } else if (mime->hasText()) {
  92. return qtprotobuf::examples::ChatMessage::ContentType::Text;
  93. }
  94. }
  95. }
  96. return qtprotobuf::examples::ChatMessage::Unknown;
  97. }
  98. void SimpleChatEngine::sendImageFromClipboard()
  99. {
  100. if (m_clipBoard == nullptr) {
  101. return;
  102. }
  103. QByteArray imgData;
  104. const QMimeData *mime = m_clipBoard->mimeData();
  105. if (mime != nullptr) {
  106. if (mime->hasImage()) {
  107. QImage img = mime->imageData().value<QImage>();
  108. QBuffer buffer(&imgData);
  109. buffer.open(QIODevice::WriteOnly);
  110. img.save(&buffer, "PNG");
  111. buffer.close();
  112. } else if (mime->hasUrls()) {
  113. QUrl imgUrl = mime->urls().first();
  114. if (!imgUrl.isLocalFile()) {
  115. qWarning() << "Only supports transfer of local images";
  116. return;
  117. }
  118. QImage img(imgUrl.toLocalFile());
  119. if (img.isNull()) {
  120. qWarning() << "Invalid image format";
  121. return;
  122. }
  123. QBuffer buffer(&imgData);
  124. buffer.open(QIODevice::WriteOnly);
  125. img.save(&buffer, "PNG");
  126. buffer.close();
  127. }
  128. }
  129. if (imgData.isEmpty()) {
  130. return;
  131. }
  132. m_client->sendMessage(ChatMessage(static_cast<quint64>(QDateTime::currentMSecsSinceEpoch()), imgData, qtprotobuf::examples::ChatMessage::ContentType::Image));
  133. }
  134. QString SimpleChatEngine::getImageThumbnail(const QByteArray &data) const
  135. {
  136. QImage img = QImage::fromData(data, "PNG");
  137. img = img.scaled(200, 200, Qt::KeepAspectRatio);
  138. QByteArray scaledData;
  139. QBuffer buffer(&scaledData);
  140. img.save(&buffer, "PNG");
  141. return getImage(scaledData);
  142. }