simplechatengine.cpp 5.8 KB

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