-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathhttpserver.cpp
More file actions
140 lines (116 loc) · 4.45 KB
/
httpserver.cpp
File metadata and controls
140 lines (116 loc) · 4.45 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
132
133
134
135
136
137
138
139
140
/***************************************************************************
//
// softProjector - an open source media projection software
// Copyright (C) 2021 Vladislav Kobzar
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation version 3 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
***************************************************************************/
#include "httpserver.hpp"
HttpServer::HttpServer(QObject *parent) : QObject(parent)
{
isRunning = false;
}
HttpServer::~HttpServer()
{
stopServer();
}
void HttpServer::startServer(quint16 port)
{
server = new QTcpServer(this);
// waiting for the web brower to make contact, this will emit signal
connect(server, SIGNAL(newConnection()), this, SLOT(startConnection()));
if(!server->listen(QHostAddress::Any, port))
isRunning = false;
else
isRunning = true;
}
void HttpServer::startConnection()
{
QTcpSocket *socket = server->nextPendingConnection();
connect(socket, SIGNAL(readyRead()), this, SLOT(sendData()));
connect(socket, SIGNAL(disconnected()), this, SLOT(closingClient()));
}
void HttpServer::closingClient()
{
QTcpSocket* socket = (QTcpSocket*)sender();
socket->deleteLater();
}
void HttpServer::sendData()
{
QTcpSocket* socket = (QTcpSocket*)sender();
socket->write("HTTP/1.1 200 OK\r\n"); // \r needs to be before \n
socket->write("Content-Type: text/xml\r\n");
socket->write("Connection: close\r\n"); // Require two \r\n.
socket->write("\r\n");
socket->write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
socket->write("<softprojector>\r\n");
socket->write(xmlData.toUtf8());
socket->write("</softprojector>\r\n");
socket->disconnectFromHost();
}
void HttpServer::stopServer()
{
server->close();
delete server;
isRunning = false;
}
void HttpServer::setBibleText(Verse bVerse, BibleSettings &bSets)
{
// Translation flags
bool havePrimary = ("none" != bSets.versions.primaryBible);
bool haveSecondary = ("none" != bSets.versions.secondaryBible);
bool haveTrinary = ("none" != bSets.versions.trinaryBible);
xmlData = "";
xmlData += "<bible class=\"primary\">\r\n";
xmlData += "<verse>" + bVerse.primary_text + "</verse>\r\n";
xmlData += "<reference>" + bVerse.primary_caption + "</reference>\r\n";
xmlData += "<translation>" + bVerse.primary_abbr + "</translation>\r\n";
xmlData += "</bible>\r\n";
if (haveSecondary) {
xmlData += "<bible class=\"secondary\">\r\n";
xmlData += "<verse>" + bVerse.secondary_text + "</verse>\r\n";
xmlData += "<reference>" + bVerse.secondary_caption + "</reference>\r\n";
xmlData += "<translation>" + bVerse.secondary_abbr + "</translation>\r\n";
xmlData += "</bible>\r\n";
}
if (haveTrinary) {
xmlData += "<bible class=\"tertiary\">\r\n";
xmlData += "<verse>" + bVerse.trinary_text + "</verse>\r\n";
xmlData += "<reference>" + bVerse.trinary_caption + "</reference>\r\n";
xmlData += "<translation>" + bVerse.trinary_abbr + "</translation>\r\n";
xmlData += "</bible>\r\n";
}
}
void HttpServer::setSongText(Stanza stanza, SongSettings &sSets)
{
xmlData = "<song>\r\n";
xmlData += "<stanza>" + stanza.stanza + "</stanza>\r\n";
xmlData += "<title>" + stanza.stanzaTitle + "</title>\r\n";
xmlData += "<tune>" + stanza.tune + "</tune>\r\n";
xmlData += "<words_by>" + stanza.wordsBy + "</words_by>\r\n";
xmlData += "<music_by>" + stanza.musicBy + "</music_by>\r\n";
xmlData += "<number>" + QString::number(stanza.number) + "</number>\r\n";
xmlData += "<is_last>" + QString::number(stanza.isLast) + "</is_last>\r\n";
xmlData += "</song>\r\n";
}
void HttpServer::setAnnounceText(AnnounceSlide announce, TextSettings &aSets)
{
xmlData = "<announcement>\r\n";
xmlData += "<text>" + announce.text + "</text>\r\n";
xmlData += "</announcement>\r\n";
}
void HttpServer::setBlank()
{
xmlData = "";
}