simplechatengine.cpp 5.6 KB

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