simplechatengine.cpp 5.7 KB

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