-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqpropresenttext.cpp
More file actions
131 lines (120 loc) · 3.28 KB
/
qpropresenttext.cpp
File metadata and controls
131 lines (120 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "qpropresenttext.h"
QProPresentText::QProPresentText(QObject* parent)
: QObject(parent)
{
m_socket = new QTcpSocket(this);
m_port = 0;
m_hostName = "";
m_password = "";
m_buffer = "";
m_currentSlide = "";
m_nextSlide = "";
m_instream = new QTextStream(m_socket);
connect(m_socket, SIGNAL(connected()), this, SLOT(onSocketConnect()));
connect(m_socket, SIGNAL(disconnected()), this, SLOT(onSocketDisconnect()));
connect(m_socket, SIGNAL(readyRead()), this, SLOT(onSocketReadyRead()), Qt::QueuedConnection);
connect(m_socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onSocketError(QAbstractSocket::SocketError)));
}
QProPresentText::~QProPresentText()
{
m_socket->disconnectFromHost();
delete m_instream;
delete m_socket;
}
// public:
void
QProPresentText::connectToProPresent(const QString & hostName, quint16 port, const QString & password)
{
m_port = port;
m_hostName = hostName;
m_password = password;
if(m_socket->state() != QAbstractSocket::UnconnectedState) {
// Temporarily don't emit disconnect signals?
disconnectFromProPresent();
}
m_socket->connectToHost(hostName, port);
delete m_instream;
m_instream = new QTextStream(m_socket);
}
// public:
void
QProPresentText::disconnectFromProPresent()
{
m_socket->disconnectFromHost();
}
// publis slot:
void
QProPresentText::onSocketConnect()
{
QString login = "<StageDisplayLogin>" + m_password + "</StageDisplayLogin>\r\n";
m_socket->write(login.toUtf8());
// start a login re-try timer?
}
//
void
QProPresentText::onSocketReadyRead()
{
m_buffer = m_instream->readAll();
m_buffer.remove(0, m_buffer.indexOf("<?xml")); // this sometimes needs to cut off DisplayLayouts
//qDebug() << "Received: " << m_buffer;
QXmlStreamReader xml(m_buffer);
while(!xml.atEnd() && !xml.hasError()) {
QXmlStreamReader::TokenType token = xml.readNext();
if(token == QXmlStreamReader::StartDocument) {
continue;
}
if(token == QXmlStreamReader::StartElement) {
if(xml.name() == "StageDisplayLoginSuccess") {
// end any login re-attempts
emit connected();
}
//if(xml.name() == "DisplayLayouts") {
// skip over this element
//}
if(xml.name() == "StageDisplayData") {
continue; // don't skip over this element
}
if(xml.name() == "Fields") {
continue; // don't skip over this element
}
if(xml.name() == "Field") {
QXmlStreamAttributes a = xml.attributes();
if(a.value("identifier") == "CurrentSlide") {
QString text = xml.readElementText();
if(m_currentSlide != text) {
emit currentSlideChanged(text);
m_currentSlide = text;
}
}
if(a.value("identifier") == "NextSlide") {
QString text = xml.readElementText();
if(m_nextSlide != text) {
emit nextSlideChanged(text);
m_nextSlide = text;
}
}
continue;
}
}
xml.skipCurrentElement();
}
if(xml.hasError()) {
if(!xml.error() == QXmlStreamReader::PrematureEndOfDocumentError) {
qDebug() << "ProPresentText: XML Parse Error(" << xml.errorString() << ")";
}
}
xml.clear();
}
//
void
QProPresentText::onSocketDisconnect()
{
// Don't clear m_currentSlide or m_nextSlide. These are buffers.
emit disconnected();
}
//
void
QProPresentText::onSocketError(QAbstractSocket::SocketError)
{
qDebug() << "ProPresentText: Socket Error(" << m_socket->errorString() << ")";
}