addressbookengine.cpp 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 "addressbookengine.h"
  26. #include "addressbook.pb.h"
  27. #include "addressbook_grpc.pb.h"
  28. #include <QGrpcHttp2Channel>
  29. #include <InsecureCredentials>
  30. #include <SslCredentials>
  31. #include <QDebug>
  32. #include <QFile>
  33. #include <QSslConfiguration>
  34. #include <QCryptographicHash>
  35. using namespace qtprotobuf::examples;
  36. class AuthCredentials : public QtProtobuf::CallCredentials
  37. {
  38. public:
  39. AuthCredentials(const QString &userName, const QString &password) :
  40. CallCredentials(CredentialMap{{QLatin1String("user-name"), QVariant::fromValue(userName)},
  41. {QLatin1String("user-password"), QVariant::fromValue(password)}}) {}
  42. };
  43. AddressBookEngine::AddressBookEngine() : QObject()
  44. , m_client(new AddressBookClient)
  45. , m_contacts(new ContactsListModel({}, this))
  46. , m_callStatus(CallStatus::Inactive)
  47. {
  48. //Prepare ssl configuration
  49. QSslConfiguration conf = QSslConfiguration::defaultConfiguration();
  50. QFile certFile("cert.pem");
  51. certFile.open(QIODevice::ReadOnly);
  52. QByteArray cert = certFile.readAll();
  53. conf.setCaCertificates({QSslCertificate(cert)});
  54. conf.setProtocol(QSsl::TlsV1_2);
  55. conf.setAllowedNextProtocols({QSslConfiguration::ALPNProtocolHTTP2});
  56. std::shared_ptr<QtProtobuf::QAbstractGrpcChannel> channel(new QtProtobuf::QGrpcHttp2Channel(QUrl("https://localhost:65001"), QtProtobuf::SslCredentials(conf) |
  57. AuthCredentials("authorizedUser", QCryptographicHash::hash("test", QCryptographicHash::Md5).toHex())));
  58. m_client->attachChannel(channel);
  59. m_client->subscribeContactsUpdates(ListFrame());
  60. connect(m_client, &AddressBookClient::contactsUpdated, this, [this](const Contacts &contacts) {
  61. m_contacts->reset(contacts.list());
  62. });
  63. m_client->subscribeCallStatusUpdates(qtprotobuf::examples::None(), QPointer<CallStatus>(&m_callStatus));
  64. }
  65. void AddressBookEngine::addContact(qtprotobuf::examples::Contact *contact)
  66. {
  67. Q_ASSERT(contact != nullptr);
  68. m_client->addContact(*contact);
  69. }
  70. void AddressBookEngine::makeCall(qtprotobuf::examples::PhoneNumber *phoneNumber)
  71. {
  72. Q_ASSERT(phoneNumber != nullptr);
  73. m_client->makeCall(*phoneNumber);
  74. }
  75. void AddressBookEngine::endCall()
  76. {
  77. m_client->endCall(qtprotobuf::examples::None());
  78. }
  79. AddressBookEngine::~AddressBookEngine()
  80. {
  81. delete m_client;
  82. }