NetworkController.cpp
53 lines
| 1.4 KiB
| text/x-c
|
CppLexer
r339 | #include "Network/NetworkController.h" | |||
#include <QMutex> | ||||
r386 | #include <QNetworkAccessManager> | |||
#include <QNetworkReply> | ||||
r339 | #include <QThread> | |||
Q_LOGGING_CATEGORY(LOG_NetworkController, "NetworkController") | ||||
struct NetworkController::NetworkControllerPrivate { | ||||
r386 | explicit NetworkControllerPrivate(NetworkController *parent) | |||
: m_WorkingMutex{}, m_AccessManager{std::make_unique<QNetworkAccessManager>()} | ||||
{ | ||||
} | ||||
r339 | QMutex m_WorkingMutex; | |||
r386 | ||||
std::unique_ptr<QNetworkAccessManager> m_AccessManager{nullptr}; | ||||
r339 | }; | |||
NetworkController::NetworkController(QObject *parent) | ||||
: QObject(parent), impl{spimpl::make_unique_impl<NetworkControllerPrivate>(this)} | ||||
{ | ||||
r386 | ||||
} | ||||
void NetworkController::execute(QNetworkReply *reply) | ||||
{ | ||||
auto replyReadyToRead =[reply, this] () { | ||||
auto content = reply->readAll(); | ||||
emit this->replyToRead(); | ||||
}; | ||||
connect(impl->m_Reply, &QNetworkReply::finished, this, replyReadyToRead); | ||||
connect(impl->m_Reply, &QNetworkReply::aboutToClose, this, replyReadyToRead); | ||||
r339 | } | |||
void NetworkController::initialize() | ||||
{ | ||||
qCDebug(LOG_NetworkController()) << tr("NetworkController init") << QThread::currentThread(); | ||||
impl->m_WorkingMutex.lock(); | ||||
qCDebug(LOG_NetworkController()) << tr("NetworkController init END"); | ||||
} | ||||
void NetworkController::finalize() | ||||
{ | ||||
impl->m_WorkingMutex.unlock(); | ||||
} | ||||
void NetworkController::waitForFinish() | ||||
{ | ||||
QMutexLocker locker{&impl->m_WorkingMutex}; | ||||
} | ||||