forked from yavdr/vdr-plugin-restfulapi
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathosd.cpp
356 lines (305 loc) · 13 KB
/
osd.cpp
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#include "osd.h"
using namespace std;
void OsdResponder::reply(ostream& out, cxxtools::http::Request& request, cxxtools::http::Reply& reply)
{
QueryHandler q("/osd", request);
if ( request.method() != "GET" ) {
reply.httpReturn(403, "Only GET-method is supported!");
return;
}
BasicOsd* osd = StatusMonitor::get()->getOsd();
if ( osd == NULL ) {
if ( q.isFormat(".html") ) {
reply.addHeader("Content-Type", "text /html; charset=utf-8");
printEmptyHtml(out);
return;
} else {
reply.httpReturn(404, "No OSD opened!");
return;
}
}
string format = "";
if ( q.isFormat(".json") ) {
reply.addHeader("Content-Type", "application/json; charset=utf-8");
format = ".json";
} else if ( q.isFormat(".html") ) {
format = ".html";
reply.addHeader("Content-Type", "text/html; charset=utf-8");
} else if ( q.isFormat(".xml") ) {
reply.addHeader("Content-Type", "text/xml; charset=utf-8");
format = ".xml";
} else {
reply.httpReturn(403, "Resources are not available for the selected format. (Use: .json, .html or .xml)");
return;
}
int start_filter = q.getOptionAsInt("start");
int limit_filter = q.getOptionAsInt("limit");
switch(osd->Type())
{
case 0x01: printTextOsd(out, (TextOsd*)osd, format, start_filter, limit_filter);
break;
case 0x02: { ChannelOsdWrapper* w = new ChannelOsdWrapper(&out);
w->print((ChannelOsd*)osd, format);
delete w; }
break;
case 0x03: { ProgrammeOsdWrapper* w = new ProgrammeOsdWrapper(&out);
w->print((ProgrammeOsd*)osd, format);
delete w; }
break;
}
}
void operator<<= (cxxtools::SerializationInfo& si, const SerTextOsdItem& o)
{
si.addMember("content") <<= o.Content;
si.addMember("is_selected") <<= o.Selected;
}
void operator<<= (cxxtools::SerializationInfo& si, const SerTextOsd& o)
{
si.addMember("type") <<= "TextOsd";
si.addMember("title") <<= o.Title;
si.addMember("message") <<= o.Message;
si.addMember("red") <<= o.Red;
si.addMember("green") <<= o.Green;
si.addMember("yellow") <<= o.Yellow;
si.addMember("blue") <<= o.Blue;
si.addMember("items") <<= o.ItemContainer->items;
}
void operator<<= (cxxtools::SerializationInfo& si, const SerProgrammeOsd& o)
{
si.addMember("present_time") <<= o.PresentTime;
si.addMember("present_title") <<= o.PresentTitle;
si.addMember("present_subtitle") <<= o.PresentSubtitle;
si.addMember("following_time") <<= o.FollowingTime;
si.addMember("following_title") <<= o.FollowingTitle;
si.addMember("following_subtitle") <<= o.FollowingSubtitle;
}
void OsdResponder::printEmptyHtml(ostream& out)
{
StreamExtension se(&out);
HtmlHeader htmlHeader;
htmlHeader.Title("VDR Restfulapi: No OSD opened!");
htmlHeader.Stylesheet("/var/lib/vdr/plugins/restfulapi/osd.css");
htmlHeader.Script("/var/lib/vdr/plugins/restfulapi/osd.js");
htmlHeader.MetaTag("<meta http-equiv=\"refresh\" content=\"1\" />");
htmlHeader.ToStream(&se);
se.write("\n<div id=\"osd_bg\"><div id=\"osd_container\"> </div></div>\n");
se.write("</body></html>");
}
void OsdResponder::printTextOsd(ostream& out, TextOsd* osd, string format, int start_filter, int limit_filter)
{
TextOsdList* osdList = NULL;
if ( format == ".json" ) {
osdList = (TextOsdList*)new JsonTextOsdList(&out);
} else if ( format == ".xml" ) {
osdList = (TextOsdList*)new XmlTextOsdList(&out);
} else if ( format == ".html" ) {
osdList = (TextOsdList*)new HtmlTextOsdList(&out);
}
if ( osdList != NULL ) {
if (start_filter >= 0 && limit_filter >= 1 ) {
osdList->activateLimit(start_filter, limit_filter);
}
osdList->printTextOsd(osd);
}
}
// --- XmlTextOsdList --------------------------------------------------------------------------------
void XmlTextOsdList::printTextOsd(TextOsd* osd)
{
s->writeXmlHeader();
s->write("<TextOsd xmlns=\"http://www.domain.org/restfulapi/2011/TextOsd-xml\">\n");
s->write(cString::sprintf(" <title>%s</title>\n", StringExtension::encodeToXml(osd->Title()).c_str()));
s->write(cString::sprintf(" <message>%s</message>\n", StringExtension::encodeToXml(osd->Message()).c_str()));
s->write(cString::sprintf(" <red>%s</red>\n", StringExtension::encodeToXml(osd->Red()).c_str()));
s->write(cString::sprintf(" <green>%s</green>\n", StringExtension::encodeToXml(osd->Green()).c_str()));
s->write(cString::sprintf(" <yellow>%s</yellow>\n", StringExtension::encodeToXml(osd->Yellow()).c_str()));
s->write(cString::sprintf(" <blue>%s</blue>\n", StringExtension::encodeToXml(osd->Blue()).c_str()));
list<TextOsdItem*>::iterator it;
list<TextOsdItem*> items = osd->GetItems();
s->write(" <items>\n");
for( it = items.begin(); it != items.end(); ++it ) {
if (!filtered()) {
const char* selected = (*it) == osd->Selected() ? "true" : "false";
TextOsdItem* item = *it;
s->write(cString::sprintf(" <item selected=\"%s\">%s</item>\n", selected, StringExtension::encodeToXml(item->Text()).c_str()));
}
}
s->write(" </items>\n");
s->write("</TextOsd>\n");
}
// --- JsonTextOsdList -------------------------------------------------------------------------------
void JsonTextOsdList::printTextOsd(TextOsd* textOsd)
{
SerTextOsd t;
t.Title = StringExtension::UTF8Decode(textOsd->Title());
t.Message = StringExtension::UTF8Decode(textOsd->Message());
t.Red = StringExtension::UTF8Decode(textOsd->Red());
t.Green = StringExtension::UTF8Decode(textOsd->Green());
t.Yellow = StringExtension::UTF8Decode(textOsd->Yellow());
t.Blue = StringExtension::UTF8Decode(textOsd->Blue());
SerTextOsdItemContainer* itemContainer = new SerTextOsdItemContainer();
list<TextOsdItem*>::iterator it;
list<TextOsdItem*> items = textOsd->GetItems();
for(it = items.begin(); it != items.end(); ++it)
{
if (!filtered()) {
SerTextOsdItem sitem;
sitem.Content = cxxtools::String(StringExtension::UTF8Decode((*it)->Text()));
sitem.Selected = (*it) == textOsd->Selected() ? true : false;
itemContainer->items.push_back(sitem);
}
}
t.ItemContainer = itemContainer;;
cxxtools::JsonSerializer serializer(*s->getBasicStream());
serializer.serialize(t, "TextOsd");
serializer.finish();
//delete itemsArray;
delete itemContainer;
}
// --- HtmlTextOsdList -------------------------------------------------------------------------------
void HtmlTextOsdList::printTextOsd(TextOsd* textOsd)
{
HtmlHeader htmlHeader;
htmlHeader.Title("HtmlTextOsdList");
htmlHeader.Stylesheet("/var/lib/vdr/plugins/restfulapi/osd.css");
htmlHeader.Script("/var/lib/vdr/plugins/restfulapi/osd.js");
htmlHeader.MetaTag("<meta http-equiv=\"refresh\" content=\"1\" />");
htmlHeader.ToStream(s);
s->write("\n<div id=\"osd_bg\"><div id=\"osd_container\">");
s->write("\n<div id=\"header\">");
if (textOsd->Title().length() > 0)
s->write(textOsd->Title().c_str());
if (textOsd->Message().length() > 0) {
s->write("\n");
s->write(textOsd->Message().c_str());
}
s->write("</div><!-- closing header container -->\n");
s->write("<div id=\"content\">\n");
list< TextOsdItem* > items = textOsd->GetItems();
list< TextOsdItem* >::iterator it;
s->write("<ul type=\"none\">\n");
for(it = items.begin(); it != items.end(); ++it) {
if (!filtered()) {
s->write("<li class=\"item\"");
s->write((*it) == textOsd->Selected() ? " id=\"selectedItem\">" : ">" );
s->write((*it)->Text().c_str());
s->write("</li>\n");
}
}
s->write("</ul>\n");
s->write("</div><!-- closing content container -->\n");
s->write("<div id=\"color_buttons\">\n");
if (textOsd->Red().length() > 0)
s->write(cString::sprintf("<div id=\"red\" class=\"first active\">%s</div>\n", textOsd->Red().c_str()));
else
s->write("<div id=\"red\" class=\"first inactive\"> </div>\n");
if (textOsd->Green().length() > 0)
s->write(cString::sprintf("<div id=\"green\" class=\"second active\">%s</div>\n", textOsd->Green().c_str()));
else
s->write("<div id=\"green\" class=\"second inactive\"> </div>\n");
if (textOsd->Yellow().length() > 0)
s->write(cString::sprintf("<div id=\"yellow\" class=\"third active\">%s</div>\n", textOsd->Yellow().c_str()));
else
s->write("<div id=\"yellow\" class=\"third inactive\"> </div>\n");
if (textOsd->Blue().length() > 0)
s->write(cString::sprintf("<div id=\"blue\" class=\"fourth active\">%s</div>\n", textOsd->Blue().c_str()));
else
s->write("<div id=\"blue\" class=\"fourth inactive\"> </div>\n");
s->write("<br class=\"clear\">\n</div><!-- closing color_buttons container -->\n");
s->write("</div></div><!-- closing osd_container -->\n");
s->write("</body></html>");
}
// --- ProgrammeOsdWrapper ---------------------------------------------------------------------------
void ProgrammeOsdWrapper::print(ProgrammeOsd* osd, string format)
{
if ( format == ".json" ) {
printJson(osd);
} else if ( format == ".html" ) {
printHtml(osd);
} else if ( format == ".xml") {
printXml(osd);
}
}
void ProgrammeOsdWrapper::printXml(ProgrammeOsd* osd)
{
s->writeXmlHeader();
s->write("<ProgrammeOsd xmlns=\"http://www.domain.org/restfulapi/2011/ProgrammeOsd-xml\">\n");
s->write(cString::sprintf(" <presenttime>%i</presenttime>\n", (int)osd->PresentTime()));
s->write(cString::sprintf(" <presenttitle>%s</presenttitle>\n", StringExtension::encodeToXml(osd->PresentTitle()).c_str()));
s->write(cString::sprintf(" <presentsubtitle>%s</presentsubtitle>\n", StringExtension::encodeToXml(osd->PresentSubtitle()).c_str()));
s->write(cString::sprintf(" <followingtime>%i</followingtime>\n", (int)osd->FollowingTime()));
s->write(cString::sprintf(" <followingtitle>%s</followingtitle>\n", StringExtension::encodeToXml(osd->FollowingTitle()).c_str()));
s->write(cString::sprintf(" <followingsubtitle>%s</followingsubtitle>\n", StringExtension::encodeToXml(osd->FollowingSubtitle()).c_str()));
s->write("</ProgrammeOsd>\n");
}
void ProgrammeOsdWrapper::printJson(ProgrammeOsd* osd)
{
cxxtools::JsonSerializer serializer(*s->getBasicStream());
SerProgrammeOsd p;
p.PresentTime = osd->PresentTime();
p.PresentTitle = StringExtension::UTF8Decode(osd->PresentTitle());
p.PresentSubtitle = StringExtension::UTF8Decode(osd->PresentSubtitle());
p.FollowingTime = osd->FollowingTime();
p.FollowingTitle = StringExtension::UTF8Decode(osd->FollowingTitle());
p.FollowingSubtitle = StringExtension::UTF8Decode(osd->FollowingSubtitle());
serializer.serialize(p, "ProgrammeOsd");
serializer.finish();
}
void ProgrammeOsdWrapper::printHtml(ProgrammeOsd* osd)
{
HtmlHeader htmlHeader;
htmlHeader.Title("ProgrammeOsdWrapper");
htmlHeader.Stylesheet("/var/lib/vdr/plugins/restfulapi/osd.css");
htmlHeader.Script("/var/lib/vdr/plugins/restfulapi/osd.js");
htmlHeader.MetaTag("<meta http-equiv=\"refresh\" content=\"1\" />");
htmlHeader.ToStream(s);
s->write("<div id=\"osd_bg\"><div id=\"osd_container\">\n");
s->write("<div id=\"content2\"><div id=\"innercontent\">");
s->write(cString::sprintf("<div id=\"eventtitle\">%s</div>", osd->PresentTitle().c_str()));
s->write(cString::sprintf("<div id=\"eventsubtitle\">%s - %s</div>",
osd->PresentSubtitle().c_str(),
StringExtension::timeToString(osd->PresentTime()).c_str()));
s->write(cString::sprintf("<div id=\"eventtitle\">%s</div>", osd->FollowingTitle().c_str()));
s->write(cString::sprintf("<div id=\"eventsubtitle\">%s - %s</div>",
osd->FollowingSubtitle().c_str(),
StringExtension::timeToString(osd->FollowingTime()).c_str()));
s->write("</div></div>\n");
s->write("</div></div>\n");
s->write("</body></html>\n");
}
// --- ChannelOsdWrapper -----------------------------------------------------------------------------
void ChannelOsdWrapper::print(ChannelOsd* osd, string format)
{
if ( format == ".json" ) {
printJson(osd);
} else if ( format == ".html" ) {
printHtml(osd);
} else if ( format == ".xml" ) {
printXml(osd);
}
}
void ChannelOsdWrapper::printXml(ChannelOsd* osd)
{
s->writeXmlHeader();
s->write("<ChannelOsd xmlns=\"http://www.domain.org/restfulapi/2011/ChannelOsd-xml\">\n");
s->write(cString::sprintf(" <Text>%s</Text>\n", StringExtension::encodeToXml(osd->Channel()).c_str()));
s->write("</ChannelOsd>\n");
}
void ChannelOsdWrapper::printJson(ChannelOsd* osd)
{
cxxtools::JsonSerializer serializer(*s->getBasicStream());
serializer.serialize(StringExtension::UTF8Decode(osd->Channel()), "ChannelOsd");
serializer.finish();
}
void ChannelOsdWrapper::printHtml(ChannelOsd* osd)
{
HtmlHeader htmlHeader;
htmlHeader.Title("ChannelOsdWrapper");
htmlHeader.Stylesheet("/var/lib/vdr/plugins/restfulapi/osd.css");
htmlHeader.Script("/var/lib/vdr/plugins/restfulapi/osd.js");
htmlHeader.MetaTag("<meta http-equiv=\"refresh\" content=\"1\" />");
htmlHeader.ToStream(s);
s->write("<div id=\"header\">");
s->write(StringExtension::encodeToXml(osd->Channel()).c_str());
s->write("</div>\n");
s->write("</body></html>");
}