This repository has been archived by the owner on Jan 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
timers.cpp
462 lines (393 loc) · 11.6 KB
/
timers.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#include "timers.h"
#include "exception.h"
#include "tools.h"
// STL headers need to be before VDR tools.h (included by <vdr/plugin.h>)
#include <sstream>
#include <memory>
#include <vdr/plugin.h>
#include <vdr/menu.h>
static bool operator<( cTimer const& left, cTimer const& right )
{
return left.Compare( right ) < 0;
}
namespace vdrlive {
using namespace std;
static char const* const TIMER_DELETE = "DELETE";
static char const* const TIMER_TOGGLE = "TOGGLE";
SortedTimers::SortedTimers()
#if VDRVERSNUM < 20301
: m_state( 0 )
#endif
{
ReloadTimers();
}
string SortedTimers::GetTimerId( cTimer const& timer )
{
ostringstream builder;
builder << timer.Channel()->GetChannelID() << ":" << timer.WeekDays() << ":"
<< timer.Day() << ":" << timer.Start() << ":" << timer.Stop();
return builder.str();
}
const cTimer* SortedTimers::GetByTimerId( string const& timerid )
{
vector< string > parts = StringSplit( timerid, ':' );
if ( parts.size() < 5 ) {
esyslog("GetByTimerId: invalid format %s", timerid.c_str() );
return 0;
}
#if VDRVERSNUM >= 20301
LOCK_CHANNELS_READ;
cChannel* channel = (cChannel *)Channels->GetByChannelID( tChannelID::FromString( parts[0].c_str() ) );
#else
cChannel* channel = Channels.GetByChannelID( tChannelID::FromString( parts[0].c_str() ) );
#endif
if ( channel == 0 ) {
esyslog("GetByTimerId: no channel %s", parts[0].c_str() );
return 0;
}
try {
int weekdays = lexical_cast< int >( parts[1] );
time_t day = lexical_cast< time_t >( parts[2] );
int start = lexical_cast< int >( parts[3] );
int stop = lexical_cast< int >( parts[4] );
cMutexLock MutexLock(&m_mutex);
for ( SortedTimers::iterator timer = begin(); timer != end(); ++timer ) {
if ( timer->Channel() == channel &&
( ( weekdays != 0 && timer->WeekDays() == weekdays ) || ( weekdays == 0 && timer->Day() == day ) ) &&
timer->Start() == start && timer->Stop() == stop )
return &*timer;
}
} catch ( bad_lexical_cast const& ex ) {
esyslog("GetByTimer: bad cast");
}
return 0;
}
string SortedTimers::EncodeDomId(string const& timerid)
{
string tId("timer_");
tId += vdrlive::EncodeDomId(timerid, ".-:", "pmc");
return tId;
}
string SortedTimers::DecodeDomId(string const &timerDomId)
{
string const timerStr("timer_");
string tId = timerDomId.substr(timerStr.length());
return vdrlive::DecodeDomId(tId, "pmc", ".-:");
}
void SortedTimers::ReloadTimers()
{
// dsyslog("live reloading timers");
cMutexLock MutexLock(&m_mutex);
clear();
#if VDRVERSNUM >= 20301
{
LOCK_TIMERS_READ;
for ( cTimer* timer = (cTimer *)Timers->First(); timer; timer = (cTimer *)Timers->Next( timer ) ) {
push_back( *timer );
}
}
#else
for ( cTimer* timer = Timers.First(); timer; timer = Timers.Next( timer ) ) {
push_back( *timer );
}
#endif
sort();
}
string SortedTimers::GetTimerDays(cTimer const& timer)
{
string currentDay = timer.WeekDays() > 0 ?
*cTimer::PrintDay(0, timer.WeekDays(), true) :
FormatDateTime(tr("%A, %x"), timer.Day());
return currentDay;
}
string SortedTimers::GetTimerInfo(cTimer const& timer)
{
ostringstream info;
info << trVDR("Priority") << ": " << timer.Priority() << endl;
info << trVDR("Lifetime") << ": " << timer.Lifetime() << endl;
info << trVDR("VPS") << ": " << (timer.HasFlags(tfVps)?trVDR("yes"):trVDR("no")) << endl;
if (timer.Aux())
{
string epgsearchinfo = GetXMLValue(timer.Aux(), "epgsearch");
if (!epgsearchinfo.empty())
{
string searchtimer = GetXMLValue(epgsearchinfo, "searchtimer");
if (!searchtimer.empty())
info << tr("Searchtimer") << ": " << searchtimer << endl;
}
}
return info.str();
}
string SortedTimers::SearchTimerName(cTimer const& timer)
{
ostringstream info;
if (timer.Aux())
{
string epgsearchinfo = GetXMLValue(timer.Aux(), "epgsearch");
if (!epgsearchinfo.empty())
{
string searchtimer = GetXMLValue(epgsearchinfo, "searchtimer");
if (!searchtimer.empty())
info << searchtimer;
}
}
return info.str();
}
#if VDRVERSNUM >= 20301
bool SortedTimers::Modified()
{
bool modified = false;
// will return != 0 only, if the Timers List has been changed since last read
if (cTimers::GetTimersRead(m_TimersStateKey)) {
modified = true;
m_TimersStateKey.Remove();
}
return modified;
}
#endif
TimerManager::TimerManager()
{
}
void TimerManager::UpdateTimer( const cTimer* timer, int flags, tChannelID& channel, string const& weekdays, string const& day,
int start, int stop, int priority, int lifetime, string const& title, string const& aux )
{
cMutexLock lock( this );
ostringstream builder;
builder << flags << ":"
<< channel << ":"
<< ( weekdays != "-------" ? weekdays : "" )
<< ( weekdays == "-------" || day.empty() ? "" : "@" ) << day << ":"
<< start << ":"
<< stop << ":"
<< priority << ":"
<< lifetime << ":"
<< StringReplace(title, ":", "|" ) << ":"
<< StringReplace(aux, ":", "|" );
// Use StringReplace here because if ':' are characters in the
// title or aux string it breaks parsing of timer definition
// in VDRs cTimer::Parse method. The '|' will be replaced
// back to ':' by the cTimer::Parse() method.
// Fix was submitted by rofafor: see
// http://www.vdr-portal.de/board/thread.php?threadid=100398
// dsyslog("%s", builder.str().c_str());
TimerPair timerData( timer, builder.str() );
// dsyslog("SV: in UpdateTimer");
m_updateTimers.push_back( timerData );
// dsyslog("SV: wait for update");
m_updateWait.Wait( *this );
// dsyslog("SV: update done");
string error = GetError( timerData );
if ( !error.empty() )
throw HtmlError( error );
}
void TimerManager::DelTimer( const cTimer* timer )
{
cMutexLock lock( this );
TimerPair timerData( timer, TIMER_DELETE );
m_updateTimers.push_back( timerData );
m_updateWait.Wait( *this );
string error = GetError( timerData );
if ( !error.empty() )
throw HtmlError( error );
}
void TimerManager::ToggleTimerActive( const cTimer* timer)
{
cMutexLock lock( this );
TimerPair timerData( timer, TIMER_TOGGLE );
m_updateTimers.push_back( timerData );
m_updateWait.Wait( *this );
string error = GetError( timerData );
if ( !error.empty() )
throw HtmlError( error );
}
void TimerManager::DoPendingWork()
{
if ( m_updateTimers.size() == 0 && !m_timers.Modified() && !m_reloadTimers )
return;
cMutexLock lock( this );
if ( m_updateTimers.size() > 0 ) {
DoUpdateTimers();
}
DoReloadTimers();
// dsyslog("SV: signalling waiters");
m_updateWait.Broadcast();
}
void TimerManager::DoUpdateTimers()
{
// dsyslog("SV: updating timers");
for ( TimerList::iterator timer = m_updateTimers.begin(); timer != m_updateTimers.end(); ++timer ) {
if ( timer->first == 0 ) // new timer
DoInsertTimer( *timer );
else if ( timer->second == TIMER_DELETE ) // delete timer
DoDeleteTimer( *timer );
else if ( timer->second == TIMER_TOGGLE ) // toggle timer
DoToggleTimer( *timer );
else // update timer
DoUpdateTimer( *timer );
}
m_updateTimers.clear();
}
void TimerManager::DoInsertTimer( TimerPair& timerData )
{
std::unique_ptr< cTimer > newTimer( new cTimer );
if ( !newTimer->Parse( timerData.second.c_str() ) ) {
StoreError( timerData, tr("Error in timer settings") );
return;
}
#if VDRVERSNUM >= 20301
LOCK_TIMERS_WRITE;
Timers->SetExplicitModify();
const cTimer *checkTimer = Timers->GetTimer( newTimer.get() );
#else
const cTimer* checkTimer = Timers.GetTimer( newTimer.get() );
#endif
if ( checkTimer ) {
StoreError( timerData, tr("Timer already defined") );
return;
}
#if VDRVERSNUM >= 20301
Timers->Add( newTimer.get() );
Timers->SetModified();
#else
Timers.Add( newTimer.get() );
Timers.SetModified();
#endif
isyslog( "live timer %s added", *newTimer->ToDescr() );
newTimer.release();
}
void TimerManager::DoUpdateTimer( TimerPair& timerData )
{
#if VDRVERSNUM < 20301
if ( Timers.BeingEdited() ) {
StoreError( timerData, tr("Timers are being edited - try again later") );
return;
}
#endif
#if VDRVERSNUM >= 20301
LOCK_TIMERS_WRITE;
Timers->SetExplicitModify();
cTimer* oldTimer = Timers->GetTimer( timerData.first );
#else
cTimer* oldTimer = Timers.GetTimer( const_cast<cTimer*>(timerData.first) );
#endif
if ( oldTimer == 0 ) {
StoreError( timerData, tr("Timer not defined") );
return;
}
cTimer copy = *oldTimer;
if ( !copy.Parse( timerData.second.c_str() ) ) {
StoreError( timerData, tr("Error in timer settings") );
return;
}
*oldTimer = copy;
#if VDRVERSNUM >= 20301
Timers->SetModified();
#else
Timers.SetModified();
#endif
isyslog("live timer %s modified (%s)", *oldTimer->ToDescr(), oldTimer->HasFlags(tfActive) ? "active" : "inactive");
}
void TimerManager::DoDeleteTimer( TimerPair& timerData )
{
#if VDRVERSNUM < 20301
if ( Timers.BeingEdited() ) {
StoreError( timerData, tr("Timers are being edited - try again later") );
return;
}
#endif
#if VDRVERSNUM >= 20301
LOCK_TIMERS_WRITE;
Timers->SetExplicitModify();
cTimer* oldTimer = Timers->GetTimer( timerData.first );
#else
cTimer* oldTimer = Timers.GetTimer( const_cast<cTimer*>(timerData.first) );
#endif
if ( oldTimer == 0 ) {
StoreError( timerData, tr("Timer not defined") );
return;
}
cTimer copy = *oldTimer;
if ( oldTimer->Recording() ) {
oldTimer->Skip();
#if VDRVERSNUM >= 20301
cRecordControls::Process( Timers, time( 0 ) );
#else
cRecordControls::Process( time( 0 ) );
#endif
}
#if VDRVERSNUM >= 20301
Timers->Del( oldTimer );
Timers->SetModified();
#else
Timers.Del( oldTimer );
Timers.SetModified();
#endif
isyslog("live timer %s deleted", *copy.ToDescr());
}
void TimerManager::DoToggleTimer( TimerPair& timerData )
{
#if VDRVERSNUM < 20301
if ( Timers.BeingEdited() ) {
StoreError( timerData, tr("Timers are being edited - try again later") );
return;
}
#endif
#if VDRVERSNUM >= 20301
LOCK_TIMERS_WRITE;
Timers->SetExplicitModify();
cTimer* toggleTimer = Timers->GetTimer( timerData.first );
#else
cTimer* toggleTimer = Timers.GetTimer( const_cast<cTimer*>(timerData.first) );
#endif
if ( toggleTimer == 0 ) {
StoreError( timerData, tr("Timer not defined") );
return;
}
#if VDRVERSNUM >= 20301
toggleTimer->OnOff();
Timers->SetModified();
#else
toggleTimer->OnOff();
Timers.SetModified();
#endif
isyslog("live timer %s toggled %s", *toggleTimer->ToDescr(), toggleTimer->HasFlags(tfActive) ? "on" : "off");
}
void TimerManager::StoreError( TimerPair const& timerData, std::string const& error )
{
m_failedUpdates.push_back( ErrorPair( timerData, error ) );
}
string TimerManager::GetError( TimerPair const& timerData )
{
for ( ErrorList::iterator error = m_failedUpdates.begin(); error != m_failedUpdates.end(); ++error ) {
if ( error->first == timerData ) {
string message = error->second;
m_failedUpdates.erase( error );
return message;
}
}
return "";
}
const cTimer* TimerManager::GetTimer(tEventID eventid, tChannelID channelid)
{
cMutexLock timersLock( &LiveTimerManager() );
SortedTimers& timers = LiveTimerManager().GetTimers();
for ( SortedTimers::iterator timer = timers.begin(); timer != timers.end(); ++timer )
if (timer->Channel() && timer->Channel()->GetChannelID() == channelid)
{
#if VDRVERSNUM >= 20301
LOCK_SCHEDULES_READ;
if (!timer->Event()) timer->SetEventFromSchedule(Schedules);
#else
if (!timer->Event()) timer->SetEventFromSchedule();
#endif
if (timer->Event() && timer->Event()->EventID() == eventid)
return &*timer;
}
return NULL;
}
TimerManager& LiveTimerManager()
{
static TimerManager instance;
return instance;
}
} // namespace vdrlive