From e38036524fdb050fcc6739d69360acf5805762ce Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Sat, 30 Apr 2022 16:18:36 +0200 Subject: Add title field to qookie-cast network package. Add tabs to Qookie-cast client. --- src/qookie-cast-client.cc | 85 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 65 insertions(+), 20 deletions(-) (limited to 'src/qookie-cast-client.cc') diff --git a/src/qookie-cast-client.cc b/src/qookie-cast-client.cc index 93e04c9..04e254d 100644 --- a/src/qookie-cast-client.cc +++ b/src/qookie-cast-client.cc @@ -1,22 +1,34 @@ #include "qookie-cast-client.h" #include +#include #include +#include -Server::Server(WebView& webview, QObject *parent) +#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0) +// Qt4 support +#include +#define WebView QWebView +#else +#include +using WebView = QTextEdit; +#endif + +Server::Server(QTabWidget& tabs, QObject *parent) : QObject(parent) - , webview(webview) + , tabs(tabs) + , payload_size(-1) { server = new QTcpServer(this); connect(server, SIGNAL(newConnection()), SLOT(newConnection())); - server->listen(QHostAddress::Any, 1024); + server->listen(QHostAddress::Any, 10024); } void Server::newConnection() { - size = -1; - html = ""; + payload_size = -1; + payload.clear(); while (server->hasPendingConnections()) { @@ -36,35 +48,68 @@ void Server::readyRead() { QTcpSocket *socket = static_cast(sender()); - if(size < 0) + while(socket->bytesAvailable() > 0) { - std::uint32_t sz; - socket->read((char*)&sz, sizeof(std::uint32_t)); - size = sz; + payload.append(socket->readAll()); } - while (socket->bytesAvailable() > 0) + // New/incoming qookie-cast + if(payload_size < 0 && payload.size() >= (int)sizeof(std::uint32_t)) { - auto data = socket->readAll(); - html.append(QString::fromUtf8(data)); - size -= data.size(); + std::uint32_t* size_ptr = (std::uint32_t*)payload.data(); + payload_size = *size_ptr; + // Skip the size field + payload = payload.mid(sizeof(std::uint32_t)); } - if(size <= 0) + // We have full payload + if(payload_size > 0 && payload_size <= payload.size()) { - webview.setHtml(html); - size = -1; - html = ""; + std::uint32_t* title_size_ptr = (std::uint32_t*)payload.data(); + auto title_ptr = payload.data() + sizeof(std::uint32_t); + + auto title_size = *title_size_ptr; + + QByteArray title(title_ptr, title_size); + // Calculate the html size as the remaining of the payload. + auto html_size = payload_size - title_size - sizeof(std::uint32_t); + QByteArray html(title_ptr + title_size, html_size); + + auto webview = new WebView(); + webview->setHtml(QString::fromUtf8(html)); + tabs.addTab(webview, QString::fromUtf8(title)); + payload_size = -1; + // Store remainder (ie. skip, title_size, title and html) + payload = payload.mid(html_size + title_size + sizeof(std::uint32_t)); + + if(payload.size() > 0) + { + // If theres enything left recurse to process excess. + readyRead(); + } } } +MyTabs::MyTabs() +{ + connect(this, SIGNAL(tabCloseRequested(int)), SLOT(doCloseIt(int))); +} + +void MyTabs::doCloseIt(int index) +{ + removeTab(index); +} + int main(int argc, char *argv[]) { QApplication qapp(argc, argv); - WebView webview; - webview.show();//Maximized(); + MyTabs tabs; + tabs.setTabsClosable(true); + tabs.setMovable(true); + tabs.setWindowTitle("Qookie-cast client"); + tabs.showMaximized(); - Server server(webview); + Server server(tabs); return qapp.exec(); } -- cgit v1.2.3