forked from vdr-projects/vdr-plugin-live
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathepg_events.cpp
408 lines (343 loc) · 10.1 KB
/
epg_events.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#include <time.h>
#include <glob.h>
#include <algorithm>
#include <vdr/player.h>
#include "tools.h"
#include "recman.h"
#include "epg_events.h"
#include "setup.h"
using namespace std;
namespace vdrlive
{
/*
* -------------------------------------------------------------------------
* EpgInfo
* -------------------------------------------------------------------------
*/
EpgInfo::EpgInfo(const std::string& id, const std::string& caption) :
m_eventId(id),
m_caption(caption)
{
}
EpgInfo::~EpgInfo()
{
}
const string EpgInfo::CurrentTime(const char* format) const
{
return FormatDateTime(format, time(0));
}
const string EpgInfo::StartTime(const char* format) const
{
time_t start = GetStartTime();
return start ? FormatDateTime(format, start) : "";
}
const string EpgInfo::EndTime(const char* format) const
{
time_t end = GetEndTime();
return end ? FormatDateTime(format, end) : "";
}
int EpgInfo::Elapsed() const
{
return EpgEvents::ElapsedTime(GetStartTime(), GetEndTime());
}
int EpgInfo::Duration() const
{
return EpgEvents::Duration(GetStartTime(), GetEndTime());
}
/*
* -------------------------------------------------------------------------
* EpgEvent
* -------------------------------------------------------------------------
*/
EpgEvent::EpgEvent(const std::string& id, const cEvent* event, const char* channelName) :
EpgInfo(id, channelName),
m_event(event)
{
}
EpgEvent::~EpgEvent()
{
}
/*
* -------------------------------------------------------------------------
* EpgString
* -------------------------------------------------------------------------
*/
EpgString::EpgString(const string& id, const string& caption, const string& info) :
EpgInfo(id, caption),
m_info(info)
{
}
EpgString::~EpgString()
{
}
const string EpgString::Title() const
{
return m_info;
}
const string EpgString::ShortDescr() const
{
return "";
}
const string EpgString::LongDescr() const
{
return "";
}
// virtual const std::string Archived() const { return std::string(); }
time_t EpgString::GetStartTime() const
{
return time(0);
}
time_t EpgString::GetEndTime() const
{
return time(0);
}
/*
* -------------------------------------------------------------------------
* EpgRecording
* -------------------------------------------------------------------------
*/
EpgRecording::EpgRecording(const string& recid, const cRecording* recording, const char* caption) :
EpgInfo(recid, (caption != 0) ? caption : ""),
m_recording(recording),
m_ownCaption(caption != 0),
m_checkedArchived(false),
m_archived()
{
}
EpgRecording::~EpgRecording()
{
m_recording = 0;
}
const string EpgRecording::Caption() const
{
if (m_ownCaption) {
return EpgInfo::Caption();
}
if (!m_recording) {
return "";
}
return Name();
}
const string EpgRecording::Title() const
{
if (!m_recording) {
return "";
}
const cRecordingInfo* info = m_recording->Info();
return (info && info->Title()) ? info->Title() : Name();
}
const string EpgRecording::ShortDescr() const
{
const cRecordingInfo* info = m_recording ? m_recording->Info() : 0;
return (info && info->ShortText()) ? info->ShortText() : "";
}
const string EpgRecording::LongDescr() const
{
const cRecordingInfo* info = m_recording ? m_recording->Info() : 0;
return (info && info->Description()) ? info->Description() : "";
}
const string EpgRecording::Archived() const
{
if (!m_checkedArchived) {
if (m_recording) {
m_archived = RecordingsManager::GetArchiveDescr(m_recording);
m_checkedArchived = true;
}
}
return m_archived;
}
time_t EpgRecording::GetStartTime() const
{
return m_recording ? m_recording->Start() : 0;
}
time_t EpgRecording::GetEndTime() const
{
time_t endTime = 0;
if (m_recording)
{
time_t startTime = m_recording->Start();
int length = m_recording->LengthInSeconds();
endTime = (length < 0) ? startTime : startTime + length;
}
return endTime;
}
int EpgRecording::Elapsed() const
{
cControl* pControl = cControl::Control();
if (pControl)
{
int current, total;
if (pControl->GetIndex(current,total))
{
if (total)
{
return (100 * current) / total;
}
}
}
return 0;
}
const string EpgRecording::Name() const
{
string name(m_recording->Name());
size_t index = name.find_last_of('~');
if (index != string::npos) {
name = name.substr(index+1);
}
return name;
}
/*
* -------------------------------------------------------------------------
* EmptyEvent
* -------------------------------------------------------------------------
*/
EmptyEvent::EmptyEvent(std::string const &id, tChannelID const &channelID, const char* channelName) :
EpgInfo(id, channelName),
m_channelID(channelID)
{
}
EmptyEvent::~EmptyEvent()
{
}
/*
* -------------------------------------------------------------------------
* EpgEvents
* -------------------------------------------------------------------------
*/
namespace EpgEvents {
string EncodeDomId(tChannelID const &chanId, tEventID const &eId)
{
string channelId(chanId.ToString());
string eventId("event_");
channelId = vdrlive::EncodeDomId(channelId, ".-", "pm");
eventId += channelId;
eventId += '_';
eventId += lexical_cast<std::string>(eId);
return eventId;
}
void DecodeDomId(string const &epgid, tChannelID& channelId, tEventID& eventId)
{
string const eventStr("event_");
size_t delimPos = epgid.find_last_of('_');
string cIdStr = epgid.substr(eventStr.length(), delimPos - eventStr.length());
cIdStr = vdrlive::DecodeDomId(cIdStr, "mp", "-.");
string const eIdStr = epgid.substr(delimPos+1);
channelId = tChannelID::FromString(cIdStr.c_str());
eventId = lexical_cast<tEventID>(eIdStr);
}
EpgInfoPtr CreateEpgInfo(string const &epgid, cSchedules const *schedules)
{
string const errorInfo(tr("Epg error"));
cSchedulesLock schedulesLock;
tEventID eventId = tEventID();
tChannelID channelId = tChannelID();
DecodeDomId(epgid, channelId, eventId);
cChannel const *channel = Channels.GetByChannelID(channelId);
if (!channel) {
return CreateEpgInfo(epgid, errorInfo, tr("Wrong channel id"));
}
cSchedule const *schedule = schedules->GetSchedule(channel);
if (!schedule) {
return CreateEpgInfo(epgid, errorInfo, tr("Channel has no schedule"));
}
cEvent const *event = schedule->GetEvent(eventId);
if (!event) {
return CreateEpgInfo(epgid, errorInfo, tr("Wrong event id"));
}
return CreateEpgInfo(channel, event, epgid.c_str());
}
EpgInfoPtr CreateEpgInfo(cChannel const *chan, cEvent const *event, char const *idOverride)
{
if (event) {
string domId(idOverride ? idOverride : EncodeDomId(chan->GetChannelID(), event->EventID()));
return EpgInfoPtr(new EpgEvent(domId, event, chan->Name()));
}
if (LiveSetup().GetShowChannelsWithoutEPG()) {
string domId(idOverride ? idOverride : EncodeDomId(chan->GetChannelID(), 0));
return EpgInfoPtr(new EmptyEvent(domId, chan->GetChannelID(), chan->Name()));
}
return EpgInfoPtr();
}
EpgInfoPtr CreateEpgInfo(string const &recid, cRecording const *recording, char const *caption)
{
return EpgInfoPtr(new EpgRecording(recid, recording, caption));
}
EpgInfoPtr CreateEpgInfo(string const &id, string const &caption, string const &info)
{
return EpgInfoPtr(new EpgString(id, caption, info));
}
bool ScanForEpgImages(string const & imageId, string const & wildcard, list<string> & images)
{
bool found = false;
const string filemask(LiveSetup().GetEpgImageDir() + "/" + imageId + wildcard);
glob_t globbuf;
globbuf.gl_offs = 0;
if (!LiveSetup().GetEpgImageDir().empty() && glob(filemask.c_str(), GLOB_DOOFFS, NULL, &globbuf) == 0) {
for(size_t i = 0; i < globbuf.gl_pathc; i++) {
const string imagefile(globbuf.gl_pathv[i]);
size_t delimPos = imagefile.find_last_of('/');
images.push_back(imagefile.substr(delimPos+1));
found = true;
}
globfree(&globbuf);
}
return found;
}
list<string> EpgImages(string const &epgid)
{
size_t delimPos = epgid.find_last_of('_');
string imageId = epgid.substr(delimPos+1);
list<string> images;
// Initially we scan for images that follow the scheme
// '<epgid>_<distinction>.*' where distincition is any
// character sequence. Usually distinction will be used
// to assign more than one image to an epg event. Thus it
// will be a digit or number. The sorting of the images
// will depend on the 'distinction' lexical sorting
// (similar to what ls does).
// Example:
// 112123_0.jpg first epg image for event id 112123
// 112123_1.png second epg image for event id 112123
if (! ScanForEpgImages(imageId, "_*.*", images))
{
// if we didn't find images that follow the scheme
// above we try to find images that contain only the
// event id as file name without extension:
if (! ScanForEpgImages(imageId, ".*", images))
{
#if TVM2VDR_PL_WORKAROUND
// if we didn't get images try to work arround a
// bug in tvm2vdr. tvm2vdr seems always to use
// one digit less, which leads in some rare cases
// to the bug in LIVE, that unrelated and to many
// images are displayed. But without this 'fix'
// no images would be visible at all. The bug
// should be fixed in tvm2vdr.pl (Perl version of
// tvm2vdr). There exists a plugin - also called
// tvm2vdr - which does not have that bug.
imageId = imageId.substr(0, imageId.size()-1);
ScanForEpgImages(imageId, "*.*", images);
#endif
}
}
return images;
}
int ElapsedTime(time_t const startTime, time_t const endTime)
{
// Elapsed time is only meaningful when there is a non zero
// duration (e.g. startTime != endTime and endTime > startTime)
int duration = Duration(startTime, endTime);
if (duration > 0) {
time_t now = time(0);
if ((startTime <= now) && (now <= endTime)) {
return 100 * (now - startTime) / duration;
}
}
return -1;
}
int Duration(time_t const startTime, time_t const endTime)
{
return endTime - startTime;
}
} // namespace EpgEvents
}; // namespace vdrlive