summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/client.cc20
-rw-r--r--src/client.h2
-rw-r--r--src/qookie-cast-client.cc85
-rw-r--r--src/qookie-cast-client.h29
-rw-r--r--src/viewer.cc2
5 files changed, 99 insertions, 39 deletions
diff --git a/src/client.cc b/src/client.cc
index 4d1bb7f..91f16d3 100644
--- a/src/client.cc
+++ b/src/client.cc
@@ -28,16 +28,28 @@ bool Client::connectToHost(const QString& host)
return true;
}
-bool Client::writeData(const QByteArray& data)
+bool Client::writeData(const QString& title, const QByteArray& data)
{
if(socket->state() != QAbstractSocket::ConnectedState)
{
return false;
}
- std::uint32_t size = data.size();
- socket->write((char*)&size, sizeof(std::uint32_t));
+ auto title_data = title.toUtf8();
+ std::uint32_t title_size = title_data.size();
+ std::uint32_t data_size = data.size();
+
+ // firt write size of the entire payload (without the payload size)
+ std::uint32_t payload_size = data_size + title_size + sizeof(std::uint32_t);
+ socket->write((char*)&payload_size, sizeof(std::uint32_t));
+
+ // then write title size and title
+ socket->write((char*)&title_size, sizeof(std::uint32_t));
+ socket->write(title_data);
+
+ // finally write the rest of the payload
socket->write(data);
+
return socket->waitForBytesWritten(100); // wait at most 100ms
}
@@ -77,5 +89,5 @@ void Client::timeout()
void Client::retryConnect()
{
- socket->connectToHost(host, 1024);
+ socket->connectToHost(host, 10024);
}
diff --git a/src/client.h b/src/client.h
index d590a59..a03a648 100644
--- a/src/client.h
+++ b/src/client.h
@@ -16,7 +16,7 @@ public:
public slots:
bool connectToHost(const QString& host);
- bool writeData(const QByteArray& data);
+ bool writeData(const QString& title, const QByteArray& data);
void errorOccurred(QAbstractSocket::SocketError socketError);
void connected();
void disconnected();
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 <cstdint>
+#include <iostream>
#include <QApplication>
+#include <QTabWidget>
-Server::Server(WebView& webview, QObject *parent)
+#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
+// Qt4 support
+#include <QWebView>
+#define WebView QWebView
+#else
+#include <QTextEdit>
+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<QTcpSocket*>(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();
}
diff --git a/src/qookie-cast-client.h b/src/qookie-cast-client.h
index 766b48b..e22b8e7 100644
--- a/src/qookie-cast-client.h
+++ b/src/qookie-cast-client.h
@@ -1,22 +1,14 @@
// -*- c++ -*-
#include <QtCore>
#include <QtNetwork>
-
-#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
-// Qt4 support
-#include <QWebView>
-using WebView = QWebView;
-#else
-#include <QTextEdit>
-using WebView = QTextEdit;
-#endif
+#include <QTabWidget>
class Server
: public QObject
{
Q_OBJECT
public:
- Server(WebView& webview, QObject *parent = 0);
+ Server(QTabWidget& tabs, QObject *parent = 0);
signals:
void dataReceived(QByteArray);
@@ -28,7 +20,18 @@ private slots:
private:
QTcpServer *server;
- WebView& webview;
- QString html;
- int size{-1};
+ QTabWidget& tabs;
+ QByteArray payload;
+ int payload_size;
+};
+
+class MyTabs : public QTabWidget
+{
+ Q_OBJECT
+public:
+ MyTabs();
+
+public slots:
+ void doCloseIt(int index);
};
+
diff --git a/src/viewer.cc b/src/viewer.cc
index ac9796c..917b65d 100644
--- a/src/viewer.cc
+++ b/src/viewer.cc
@@ -97,5 +97,5 @@ void Viewer::show(const Recipe& recipe)
;
textEdit->setHtml(html);
- client.writeData(html.toUtf8());
+ client.writeData(QString::fromUtf8(recipe.title.data()), html.toUtf8());
}