Skip to content

Commit 6bc17fc

Browse files
committed
completed epgsearch implementation
1 parent b25c9c2 commit 6bc17fc

File tree

4 files changed

+89
-18
lines changed

4 files changed

+89
-18
lines changed

events.cpp

+9-6
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ void EventsResponder::replyEvents(std::ostream& out, cxxtools::http::Request& re
8080
int ts = event->StartTime();
8181
int te = ts + event->Duration();
8282
if ( (ts <= to && te > from) || (te > from && timespan == 0) ) {
83-
eventList->addEvent(event);
83+
eventList->addEvent(event, channel);
8484
}else{
8585
if(ts > to) break;
8686
if(te <= from) {
@@ -195,7 +195,7 @@ void EventsResponder::replySearchResult(std::ostream& out, cxxtools::http::Reque
195195
if (result != NULL) {
196196
for(int i=0;i<result->Count();i++) {
197197
item = result->Get(i);
198-
eventList->addEvent(((cEvent*)item->event));
198+
eventList->addEvent(((cEvent*)item->event), channelInstance);
199199
total++;
200200
}
201201
}
@@ -227,6 +227,7 @@ void operator<<= (cxxtools::SerializationInfo& si, const SerEvent& e)
227227

228228
EventList::EventList(std::ostream *_out) {
229229
s = new StreamExtension(_out);
230+
total = 0;
230231
}
231232

232233
EventList::~EventList()
@@ -240,7 +241,7 @@ void HtmlEventList::init()
240241
s->write("<ul>");
241242
}
242243

243-
void HtmlEventList::addEvent(cEvent* event)
244+
void HtmlEventList::addEvent(cEvent* event, cChannel* channel)
244245
{
245246
if ( filtered() ) return;
246247
s->write("<li>");
@@ -254,15 +255,15 @@ void HtmlEventList::finish()
254255
s->write("</body></html>");
255256
}
256257

257-
void JsonEventList::addEvent(cEvent* event)
258+
void JsonEventList::addEvent(cEvent* event, cChannel* channel)
258259
{
259260
if ( filtered() ) return;
260-
261261

262262
cxxtools::String eventTitle;
263263
cxxtools::String eventShortText;
264264
cxxtools::String eventDescription;
265265
cxxtools::String empty = StringExtension::UTF8Decode("");
266+
cxxtools::String channelStr = StringExtension::UTF8Decode((const char*)channel->GetChannelID().ToString());
266267
SerEvent serEvent;
267268

268269
if( !event->Title() ) { eventTitle = empty; } else { eventTitle = StringExtension::UTF8Decode(event->Title()); }
@@ -273,6 +274,7 @@ void JsonEventList::addEvent(cEvent* event)
273274
serEvent.Title = eventTitle;
274275
serEvent.ShortText = eventShortText;
275276
serEvent.Description = eventDescription;
277+
serEvent.Channel = channelStr;
276278
serEvent.StartTime = event->StartTime();
277279
serEvent.Duration = event->Duration();
278280

@@ -298,7 +300,7 @@ void XmlEventList::init()
298300
s->write("<events xmlns=\"http://www.domain.org/restfulapi/2011/events-xml\">\n");
299301
}
300302

301-
void XmlEventList::addEvent(cEvent* event)
303+
void XmlEventList::addEvent(cEvent* event, cChannel* channel)
302304
{
303305
if ( filtered() ) return;
304306

@@ -315,6 +317,7 @@ void XmlEventList::addEvent(cEvent* event)
315317
s->write(cString::sprintf(" <param name=\"title\">%s</param>\n", StringExtension::encodeToXml(eventTitle).c_str()));
316318
s->write(cString::sprintf(" <param name=\"short_text\">%s</param>\n", StringExtension::encodeToXml(eventShortText).c_str()));
317319
s->write(cString::sprintf(" <param name=\"description\">%s</param>\n", StringExtension::encodeToXml(eventDescription).c_str()));
320+
s->write(cString::sprintf(" <param name=\"channel\">%s</param>\n", StringExtension::encodeToXml((const char*)channel->GetChannelID().ToString()).c_str()));
318321
s->write(cString::sprintf(" <param name=\"start_time\">%i</param>\n", (int)event->StartTime()));
319322
s->write(cString::sprintf(" <param name=\"duration\">%i</param>\n", event->Duration()));
320323

events.h

+10-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
#include "tools.h"
1212
#include "epgsearch/services.h"
1313

14+
#ifndef __RESTFUL_EVENTS_H
15+
#define __RESTFUL_EVENTS_H
16+
1417
class EventsResponder : public cxxtools::http::Responder
1518
{
1619
public:
@@ -31,6 +34,7 @@ struct SerEvent
3134
cxxtools::String Title;
3235
cxxtools::String ShortText;
3336
cxxtools::String Description;
37+
cxxtools::String Channel;
3438
int StartTime;
3539
int Duration;
3640
int Images;
@@ -47,7 +51,7 @@ class EventList : public BaseList
4751
EventList(std::ostream* _out);
4852
~EventList();
4953
virtual void init() { };
50-
virtual void addEvent(cEvent* event) { };
54+
virtual void addEvent(cEvent* event, cChannel* channel) { };
5155
virtual void finish() { };
5256
virtual void setTotal(int _total) { total = _total; }
5357
};
@@ -58,7 +62,7 @@ class HtmlEventList : EventList
5862
HtmlEventList(std::ostream* _out) : EventList(_out) { };
5963
~HtmlEventList() { };
6064
virtual void init();
61-
virtual void addEvent(cEvent* event);
65+
virtual void addEvent(cEvent* event, cChannel* channel);
6266
virtual void finish();
6367
};
6468

@@ -69,7 +73,7 @@ class JsonEventList : EventList
6973
public:
7074
JsonEventList(std::ostream* _out) : EventList(_out) { };
7175
~JsonEventList() { };
72-
virtual void addEvent(cEvent* event);
76+
virtual void addEvent(cEvent* event, cChannel* channel);
7377
virtual void finish();
7478
};
7579

@@ -79,6 +83,8 @@ class XmlEventList : EventList
7983
XmlEventList(std::ostream* _out) : EventList(_out) { };
8084
~XmlEventList() { };
8185
virtual void init();
82-
virtual void addEvent(cEvent* event);
86+
virtual void addEvent(cEvent* event, cChannel* channel);
8387
virtual void finish();
8488
};
89+
90+
#endif //__RESTFUL_EVENTS_H

searchtimers.cpp

+67-8
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,20 @@ void SearchTimersResponder::reply(std::ostream& out, cxxtools::http::Request& re
66
if (plugin == NULL) {
77
reply.httpReturn(403, "Epgsearch isn't installed!");
88
return;
9-
}
10-
11-
if (request.method() == "GET") {
12-
replyShow(out, request, reply);
13-
} else if (request.method() == "POST") {
14-
replyCreate(out, request, reply);
15-
} else {
16-
reply.httpReturn(404, "The searchtimer-service does only support the following methods: GET, POST and DELETE.");
9+
}
10+
11+
if ((int)request.url().find("/searchtimers/search/") == 0 ) {
12+
replySearch(out, request, reply);
13+
} else {
14+
if (request.method() == "GET") {
15+
replyShow(out, request, reply);
16+
} else if (request.method() == "POST") {
17+
replyCreate(out, request, reply);
18+
} else if (request.method() == "DELETE") {
19+
replyDelete(out, request, reply);
20+
} else {
21+
reply.httpReturn(404, "The searchtimer-service does only support the following methods: GET, POST and DELETE.");
22+
}
1723
}
1824
}
1925

@@ -80,6 +86,59 @@ void SearchTimersResponder::replyCreate(std::ostream& out, cxxtools::http::Reque
8086
delete searchTimer;
8187
}
8288

89+
void SearchTimersResponder::replyDelete(std::ostream& out, cxxtools::http::Request& request, cxxtools::http::Reply& reply)
90+
{
91+
QueryHandler q("/searchtimers", request);
92+
vdrlive::SearchTimers searchTimers;
93+
std::string id = q.getParamAsString(0);
94+
bool result = searchTimers.Delete(id);
95+
96+
if (!result)
97+
reply.httpReturn(408, "Deleting searchtimer failed!");
98+
}
99+
100+
void SearchTimersResponder::replySearch(std::ostream& out, cxxtools::http::Request& request, cxxtools::http::Reply& reply)
101+
{
102+
QueryHandler q("/searchtimers/search", request);
103+
vdrlive::SearchResults searchResults;
104+
int id = q.getParamAsInt(0);
105+
106+
EventList* eventList;
107+
108+
if ( q.isFormat(".json") ) {
109+
reply.addHeader("Content-Type", "application/json; charset=utf-8");
110+
eventList = (EventList*)new JsonEventList(&out);
111+
} else if ( q.isFormat(".html") ) {
112+
reply.addHeader("Content-Type", "text/html; charset=utf-8");
113+
eventList = (EventList*)new HtmlEventList(&out);
114+
} else if ( q.isFormat(".xml") ) {
115+
reply.addHeader("Content-Type", "text/xml; charset=utf-8");
116+
eventList = (EventList*)new XmlEventList(&out);
117+
} else {
118+
reply.httpReturn(403, "Resources are not available for the selected format. (Use: .json, .xml or .html)");
119+
return;
120+
}
121+
122+
searchResults.GetByID(id);
123+
124+
int start_filter = q.getOptionAsInt("start");
125+
int limit_filter = q.getOptionAsInt("limit");
126+
if ( start_filter >= 0 && limit_filter >= 1 )
127+
eventList->activateLimit(start_filter, limit_filter);
128+
129+
eventList->init();
130+
int total = 0;
131+
132+
for (vdrlive::SearchResults::iterator item = searchResults.begin(); item != searchResults.end(); ++item) {
133+
eventList->addEvent((cEvent*)item->GetEvent(), (cChannel*)item->GetChannel());
134+
total++;
135+
}
136+
137+
eventList->setTotal(total);
138+
eventList->finish();
139+
delete eventList;
140+
}
141+
83142
SearchTimerList::SearchTimerList(std::ostream* _out)
84143
{
85144
s = new StreamExtension(_out);

searchtimers.h

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "tools.h"
1212
#include "epgsearch/services.h"
1313
#include "epgsearch.h"
14+
#include "events.h"
1415

1516
#ifndef __RESTFUL_SEARCHTIMERS_H
1617
#define __RESETFUL_SEARCHTIMERS_H
@@ -24,6 +25,8 @@ class SearchTimersResponder : public cxxtools::http::Responder
2425
virtual void reply(std::ostream& out, cxxtools::http::Request& request, cxxtools::http::Reply& reply);
2526
virtual void replyShow(std::ostream& out, cxxtools::http::Request& request, cxxtools::http::Reply& reply);
2627
virtual void replyCreate(std::ostream& out, cxxtools::http::Request& request, cxxtools::http::Reply& reply);
28+
virtual void replyDelete(std::ostream& out, cxxtools::http::Request& request, cxxtools::http::Reply& reply);
29+
virtual void replySearch(std::ostream& out, cxxtools::http::Request& request, cxxtools::http::Reply& reply);
2730
};
2831

2932
typedef cxxtools::http::CachedService<SearchTimersResponder> SearchTimersService;

0 commit comments

Comments
 (0)