From 75ec461eeaa851cb5c53f4cfffc434e3e529ed1d Mon Sep 17 00:00:00 2001 From: "Felix (xq) Queißner" Date: Mon, 22 Jun 2020 21:10:04 +0200 Subject: Restructures the project source and cleans up a bit --- src/protocols/fingerclient.cpp | 83 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 src/protocols/fingerclient.cpp (limited to 'src/protocols/fingerclient.cpp') diff --git a/src/protocols/fingerclient.cpp b/src/protocols/fingerclient.cpp new file mode 100644 index 0000000..1c9a6a3 --- /dev/null +++ b/src/protocols/fingerclient.cpp @@ -0,0 +1,83 @@ +#include "fingerclient.hpp" +#include "ioutil.hpp" + +FingerClient::FingerClient() : ProtocolHandler(nullptr) +{ + connect(&socket, &QTcpSocket::connected, this, &FingerClient::on_connected); + connect(&socket, &QTcpSocket::readyRead, this, &FingerClient::on_readRead); + connect(&socket, &QTcpSocket::disconnected, this, &FingerClient::on_finished); + +#if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)) + connect(&socket, &QTcpSocket::errorOccurred, this, &FingerClient::on_socketError); +#else + connect(&socket, QOverload::of(&QTcpSocket::error), this, &FingerClient::on_socketError); +#endif +} + +FingerClient::~FingerClient() +{ + +} + +bool FingerClient::supportsScheme(const QString &scheme) const +{ + return (scheme == "finger"); +} + +bool FingerClient::startRequest(const QUrl &url, RequestOptions options) +{ + Q_UNUSED(options) + + if(isInProgress()) + return false; + + if(url.scheme() != "finger") + return false; + + this->requested_user = url.userName(); + this->was_cancelled = false; + socket.connectToHost(url.host(), url.port(79)); + + return true; +} + +bool FingerClient::isInProgress() const +{ + return socket.isOpen(); +} + +bool FingerClient::cancelRequest() +{ + was_cancelled = true; + socket.close(); + body.clear(); + return true; +} + +void FingerClient::on_connected() +{ + auto blob = (requested_user + "\r\n").toUtf8(); + + IoUtil::writeAll(socket, blob); +} + +void FingerClient::on_readRead() +{ + body.append(socket.readAll()); + emit this->requestProgress(body.size()); +} + +void FingerClient::on_finished() +{ + if(not was_cancelled) + { + emit this->requestComplete(this->body, "text/finger"); + was_cancelled = true; + } + body.clear(); +} + +void FingerClient::on_socketError(QAbstractSocket::SocketError error_code) +{ + this->emitNetworkError(error_code, socket.errorString()); +} -- cgit v1.2.3