simplechatengine.cpp 5.8 KB

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