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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
|
#define DISABLE_TEMPLATES_COLLIDING_WITH_STL
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <vdr/menu.h>
#include "services/remotetimers.h"
#include "helpers.h"
#include "switchtimer.h"
#include "timerconflict.h"
#include <vdr/timers.h>
#include "recmanager.h"
static int CompareRecording(const void *p1, const void *p2) {
return (int)((*(cRecording **)p1)->Start() - (*(cRecording **)p2)->Start());
}
cRecManager::cRecManager(void) {
epgSearchPlugin = NULL;
epgSearchAvailable = false;
}
cRecManager::~cRecManager(void) {
}
void cRecManager::SetEPGSearchPlugin(void) {
epgSearchPlugin = cPluginManager::GetPlugin("epgsearch");
if (epgSearchPlugin) {
epgSearchAvailable = true;
}
}
bool cRecManager::RefreshRemoteTimers(void) {
cString errorMsg;
if (!pRemoteTimers->Service("RemoteTimers::RefreshTimers-v1.0", &errorMsg)) {
esyslog("tvguide: %s", *errorMsg);
return false;
}
return true;
}
bool cRecManager::CheckEventForTimer(const cEvent *event) {
bool hasTimer = false;
if (config.useRemoteTimers && pRemoteTimers) {
RemoteTimers_GetMatch_v1_0 rtMatch;
rtMatch.event = event;
pRemoteTimers->Service("RemoteTimers::GetMatch-v1.0", &rtMatch);
if (rtMatch.timerMatch == tmFull)
hasTimer = true;
} else {
#if defined (APIVERSNUM) && (APIVERSNUM >= 20301)
eTimerMatch TimerMatch = tmNone;
LOCK_TIMERS_READ;
const cTimers *timers = Timers;
if (timers->GetMatch(event, &TimerMatch) && (TimerMatch == tmFull))
hasTimer = true;
#else
hasTimer = event->HasTimer();
#endif
}
return hasTimer;
}
const cTimer *cRecManager::GetTimerForEvent(const cEvent *event) {
const cTimer *timer = NULL;
if (config.useRemoteTimers && pRemoteTimers) {
RemoteTimers_GetMatch_v1_0 rtMatch;
rtMatch.event = event;
pRemoteTimers->Service("RemoteTimers::GetMatch-v1.0", &rtMatch);
timer = rtMatch.timer;
return timer;
}
#if defined (APIVERSNUM) && (APIVERSNUM >= 20301)
LOCK_TIMERS_READ;
timer = Timers->GetMatch(event);
#else
timer = Timers.GetMatch(event);
#endif
return timer;
}
cTimer *cRecManager::createTimer(const cEvent *event, std::string path) {
cTimer *timer = NULL;
if (config.useRemoteTimers && pRemoteTimers) {
timer = createRemoteTimer(event, path);
} else {
timer = createLocalTimer(event, path);
}
return timer;
}
cTimer *cRecManager::createLocalTimer(const cEvent *event, std::string path) {
cTimer *timer = new cTimer(event);
#if defined (APIVERSNUM) && (APIVERSNUM >= 20301)
LOCK_TIMERS_WRITE;
cTimers* timers = Timers;
timers->SetExplicitModify();
if (Setup.SVDRPPeering && *Setup.SVDRPDefaultHost)
timer->SetRemote(Setup.SVDRPDefaultHost);
#else
cTimers* timers = &Timers;
#endif
cTimer *t = timers->GetTimer(timer);
if (t) {
t->OnOff();
#if defined (APIVERSNUM) && (APIVERSNUM >= 20301)
t->SetEvent(event);
#else
t->SetEventFromSchedule();
#endif
delete timer;
timer = t;
isyslog("timer %s reactivated", *t->ToDescr());
} else {
SetTimerPath(timer, event, path);
timers->Add(timer);
timers->SetModified();
#if defined (APIVERSNUM) && (APIVERSNUM >= 20301)
if (!HandleRemoteTimerModifications(timer)) {
timers->Del(timer);
esyslog(tr("tvguide: RemoteTimerModifications failed"));
} else {
isyslog("timer %s added (active)", *timer->ToDescr());
}
#else
isyslog("timer %s added (active)", *timer->ToDescr());
#endif
}
return timers->GetTimer(timer);
}
cTimer *cRecManager::createRemoteTimer(const cEvent *event, std::string path) {
cTimer *t = new cTimer(event);
SetTimerPath(t, event, path);
RemoteTimers_Timer_v1_0 rt;
rt.timer = t;
pRemoteTimers->Service("RemoteTimers::GetTimer-v1.0", &rt.timer);
if (rt.timer) {
rt.timer->OnOff();
if (!pRemoteTimers->Service("RemoteTimers::ModTimer-v1.0", &rt))
rt.timer = NULL;
} else {
rt.timer = t;
if (!pRemoteTimers->Service("RemoteTimers::NewTimer-v1.0", &rt))
isyslog("%s", *rt.errorMsg);
}
RefreshRemoteTimers();
return rt.timer;
}
void cRecManager::SetTimerPath(cTimer *timer, const cEvent *event, std::string path) {
if (config.instRecFolderMode == eFolderFixed) {
Epgsearch_services_v1_2 *epgSearch = new Epgsearch_services_v1_2;
std::string recDir = config.instRecFixedFolder;
std::replace(recDir.begin(), recDir.end(), '/', '~');
if (strchr(recDir.c_str(), '%') != NULL) {
if (epgSearchPlugin->Service("Epgsearch-services-v1.1", epgSearch)) {
std::string newFileName = epgSearch->handler->Evaluate(recDir, event);
if (strchr(newFileName.c_str(), '%') == NULL) // only set directory to new value if all categories could have been replaced
timer->SetFile(newFileName.c_str());
else
esyslog("tvguide: timer path not set because replacing variable was not successfull: %s", newFileName.c_str());
}
} else {
cString newFileName;
if (recDir.size() > 0) {
newFileName = cString::sprintf("%s~%s", recDir.c_str(), timer->File());
timer->SetFile(*newFileName);
}
}
return;
}
//Set choosen path
bool addSubtitle = false;
if (!isempty(event->ShortText())) { // add subtitle if present
addSubtitle = (config.addSubtitleToTimer != addSubtitleNever);
if (config.addSubtitleToTimer == addSubtitleSmart)
if (event->Duration() > 80 * 60)
addSubtitle = false;
}
cString newFileName;
if (path.size() > 0) {
std::replace(path.begin(), path.end(), '/', '~');
if (addSubtitle)
newFileName = cString::sprintf("%s~%s~%s", path.c_str(), event->Title(), event->ShortText());
else
newFileName = cString::sprintf("%s~%s", path.c_str(), timer->File());
} else {
if (addSubtitle)
newFileName = cString::sprintf("%s~%s", event->Title(), event->ShortText());
else
newFileName = event->Title();
}
timer->SetFile(*newFileName);
}
void cRecManager::DeleteTimer(int timerID) {
const cTimer *t;
#if defined (APIVERSNUM) && (APIVERSNUM >= 20301)
{
LOCK_TIMERS_READ;
t = Timers->Get(timerID);
}
#else
t = Timers.Get(timerID);
#endif
if (!t)
return;
DeleteTimer(t);
}
void cRecManager::DeleteTimer(const cEvent *event) {
if (!event)
return;
if (config.useRemoteTimers && pRemoteTimers) {
DeleteRemoteTimer(event);
} else {
DeleteLocalTimer(event);
}
}
void cRecManager::DeleteLocalTimer(const cEvent *event) {
const cTimer *t;
#if defined (APIVERSNUM) && (APIVERSNUM >= 20301)
{
LOCK_TIMERS_READ;
t = Timers->GetMatch(event);
}
#else
t = Timers.GetMatch(event);
#endif
if (!t)
return;
DeleteTimer(t);
}
void cRecManager::DeleteTimer(const cTimer *timer) {
#if defined (APIVERSNUM) && (APIVERSNUM >= 20301)
LOCK_TIMERS_WRITE;
cTimers* timers = Timers;
if (timer && timer->Remote() && !timer->Recording()) {
if (HandleRemoteTimerModifications(NULL, (cTimer*)timer)) {
timers->Del((cTimer*)timer);
}
timers->SetModified();
}
cTimer* t = timers->GetTimer(timer);
#else
cTimers* timers = &Timers;
cTimer* t = timers->GetTimer((cTimer*)timer);
#endif
if (!t)
return;
if (t->Recording()) {
t->Skip();
#if defined (APIVERSNUM) && (APIVERSNUM >= 20301)
cRecordControls::Process(timers, time(NULL));
#else
cRecordControls::Process(time(NULL));
#endif
}
isyslog("timer %s deleted", *t->ToDescr());
timers->Del(t, true);
timers->SetModified();
}
void cRecManager::DeleteRemoteTimer(const cEvent *event) {
RemoteTimers_GetMatch_v1_0 rtMatch;
rtMatch.event = event;
pRemoteTimers->Service("RemoteTimers::GetMatch-v1.0", &rtMatch);
if (rtMatch.timer) {
RemoteTimers_Timer_v1_0 rt;
rt.timer = rtMatch.timer;
isyslog("remotetimer %s deleted", *rt.timer->ToDescr());
if (!pRemoteTimers->Service("RemoteTimers::DelTimer-v1.0", &rt))
isyslog("remotetimer error");
RefreshRemoteTimers();
}
}
void cRecManager::SaveTimer(const cTimer *t, cTimer *newTimerSettings) {
if (!t)
return;
#if defined (APIVERSNUM) && (APIVERSNUM >= 20301)
LOCK_TIMERS_WRITE;
cTimers* timers = Timers;
timers->SetExplicitModify();
if (t && t->Remote()) {
if (!HandleRemoteTimerModifications(newTimerSettings, (cTimer *)t)) {
esyslog(tr("tvguide: RemoteTimerModifications failed"));
}
}
cTimer *timer = timers->GetTimer(t);
#else
cTimers* timers = &Timers;
cTimer *timer = timers->GetTimer((cTimer *)t);
#endif
if (!timer) {
return;
}
bool active = newTimerSettings->HasFlags(tfActive);
int prio = newTimerSettings->Priority();
int lifetime = newTimerSettings->Lifetime();
time_t day = newTimerSettings->Day();
int start = newTimerSettings->Start();
int stop = newTimerSettings->Stop();
std::string fileName = newTimerSettings->File();
timer->SetDay(day);
timer->SetStart(start);
timer->SetStop(stop);
timer->SetPriority(prio);
timer->SetLifetime(lifetime);
timer->SetFile(fileName.c_str());
if (active)
timer->SetFlags(tfActive);
else
timer->ClrFlags(tfActive);
#if VDRVERSNUM < 20300
timer->SetEventFromSchedule();
#endif
if (config.useRemoteTimers && pRemoteTimers) {
RemoteTimers_Timer_v1_0 rt;
rt.timer = timer;
if (!pRemoteTimers->Service("RemoteTimers::ModTimer-v1.0", &rt))
rt.timer = NULL;
RefreshRemoteTimers();
} else {
timers->SetModified();
}
}
bool cRecManager::IsRecorded(const cEvent *event) {
#if defined (APIVERSNUM) && (APIVERSNUM >= 20301)
LOCK_TIMERS_WRITE;
cTimer *timer = Timers->GetMatch(event);
#else
cTimer *timer = Timers.GetMatch(event);
#endif
if (!timer)
return false;
return timer->Recording();
}
cTVGuideTimerConflicts *cRecManager::CheckTimerConflict(void) {
cTVGuideTimerConflicts *conflictList = new cTVGuideTimerConflicts();
if (!epgSearchAvailable)
return conflictList;
Epgsearch_services_v1_1 *epgSearch = new Epgsearch_services_v1_1;
if (epgSearchPlugin->Service("Epgsearch-services-v1.1", epgSearch)) {
std::list<std::string> conflicts = epgSearch->handler->TimerConflictList();
int numConflicts = conflicts.size();
if (numConflicts == 0)
return conflictList;
for (std::list<std::string>::iterator it=conflicts.begin(); it != conflicts.end(); ++it) {
conflictList->AddConflict(*it);
}
}
delete epgSearch;
conflictList->CalculateConflicts();
return conflictList;
}
void cRecManager::CreateSeriesTimer(cTimer *seriesTimer) {
#if VDRVERSNUM < 20300
seriesTimer->SetEventFromSchedule();
#endif
if (config.useRemoteTimers && pRemoteTimers) {
RemoteTimers_Timer_v1_0 rt;
rt.timer = seriesTimer;
if (!pRemoteTimers->Service("RemoteTimers::NewTimer-v1.0", &rt))
isyslog("%s", *rt.errorMsg);
RefreshRemoteTimers();
} else {
#if defined (APIVERSNUM) && (APIVERSNUM >= 20301)
LOCK_TIMERS_WRITE;
cTimers* timers = Timers;
#else
cTimers* timers = &Timers;
#endif
timers->Add(seriesTimer);
timers->SetModified();
}
}
void cRecManager::ReadEPGSearchTemplates(std::vector<TVGuideEPGSearchTemplate> *epgTemplates) {
cString ConfigDir = cPlugin::ConfigDirectory("epgsearch");
cString epgsearchConf = "epgsearchtemplates.conf";
cString fileName = AddDirectory(*ConfigDir, *epgsearchConf);
if (access(fileName, F_OK) == 0) {
FILE *f = fopen(fileName, "r");
if (f) {
char *s;
cReadLine ReadLine;
while ((s = ReadLine.Read(f)) != NULL) {
char *p = strchr(s, '#');
if (p)
*p = 0;
stripspace(s);
try {
if (!isempty(s)) {
std::string templ = s;
int posID = templ.find_first_of(":");
int posName = templ.find_first_of(":", posID+1);
std::string name = templ.substr(posID+1, posName - posID - 1);
std::string templValue = templ.substr(posName);
TVGuideEPGSearchTemplate tmp;
tmp.name = name;
tmp.templValue = templValue;
epgTemplates->push_back(tmp);
}
} catch (...){}
}
}
}
}
const cEvent **cRecManager::PerformSearchTimerSearch(std::string epgSearchString, int &numResults) {
if (!epgSearchAvailable)
return NULL;
const cEvent **searchResults = NULL;
Epgsearch_services_v1_1 *epgSearch = new Epgsearch_services_v1_1;
if (epgSearchPlugin->Service("Epgsearch-services-v1.1", epgSearch)) {
std::list<std::string> results = epgSearch->handler->QuerySearch(epgSearchString);
numResults = results.size();
if (numResults > 0) {
searchResults = new const cEvent *[numResults];
const cSchedules *schedules;
#if defined (APIVERSNUM) && (APIVERSNUM >= 20301)
{
LOCK_SCHEDULES_READ;
schedules = Schedules;
}
#else
cSchedulesLock schedulesLock;
schedules = cSchedules::Schedules(schedulesLock);
#endif
const cEvent *event = NULL;
int index=0;
for (std::list<std::string>::iterator it=results.begin(); it != results.end(); ++it) {
try {
splitstring s(it->c_str());
std::vector<std::string> flds = s.split(':', 1);
int eventID = atoi(flds[1].c_str());
std::string channelID = flds[7];
tChannelID chanID = tChannelID::FromString(channelID.c_str());
const cChannel *channel;
#if defined (APIVERSNUM) && (APIVERSNUM >= 20301)
{
LOCK_CHANNELS_READ;
channel = Channels->GetByChannelID(chanID);
}
#else
channel = Channels.GetByChannelID(chanID);
#endif
if (channel) {
const cSchedule *Schedule = NULL;
Schedule = schedules->GetSchedule(channel);
event = Schedule->GetEvent(eventID);
if (event) {
searchResults[index] = event;
} else
return NULL;
} else
return NULL;
index++;
} catch (...){}
}
}
}
return searchResults;
}
const cEvent **cRecManager::PerformSearch(Epgsearch_searchresults_v1_0 data, int &numResults) {
if (epgSearchAvailable) {
if (epgSearchPlugin->Service("Epgsearch-searchresults-v1.0", &data)) {
cList<Epgsearch_searchresults_v1_0::cServiceSearchResult> *list = data.pResultList;
if (!list)
return NULL;
int numElements = list->Count();
const cEvent **searchResults = NULL;
if (numElements > 0) {
searchResults = new const cEvent *[numElements];
numResults = numElements;
int index = 0;
for (Epgsearch_searchresults_v1_0::cServiceSearchResult *r = list->First(); r ; r = list->Next(r)) {
searchResults[index] = r->event;
index++;
}
}
delete list;
return searchResults;
}
}
return NULL;
}
void cRecManager::GetSearchTimers(std::vector<cTVGuideSearchTimer> *searchTimer) {
if (!epgSearchAvailable) {
return;
}
Epgsearch_services_v1_1 *epgSearch = new Epgsearch_services_v1_1;
if (epgSearchPlugin->Service("Epgsearch-services-v1.1", epgSearch)) {
std::list<std::string> searchTimerList;
searchTimerList = epgSearch->handler->SearchTimerList();
for(std::list<std::string>::iterator it = searchTimerList.begin(); it != searchTimerList.end(); it++) {
cTVGuideSearchTimer timer;
timer.SetEPGSearchString(it->c_str());
if (timer.Parse())
searchTimer->push_back(timer);
}
}
std::sort(searchTimer->begin(), searchTimer->end());
}
int cRecManager::CreateSearchTimer(std::string epgSearchString) {
int timerID = -1;
if (!epgSearchAvailable)
return timerID;
Epgsearch_services_v1_1 *epgSearch = new Epgsearch_services_v1_1;
if (epgSearchPlugin->Service("Epgsearch-services-v1.1", epgSearch)) {
timerID = epgSearch->handler->AddSearchTimer(epgSearchString);
}
return timerID;
}
bool cRecManager::SaveSearchTimer(cTVGuideSearchTimer *searchTimer) {
if (!epgSearchAvailable)
return false;
Epgsearch_services_v1_1 *epgSearch = new Epgsearch_services_v1_1;
if (searchTimer->GetID() > -1) {
if (epgSearchPlugin->Service("Epgsearch-services-v1.1", epgSearch)) {
bool success = epgSearch->handler->ModSearchTimer(searchTimer->BuildSearchString());
if (success) {
esyslog("tvguide: search timer with id %d sucessfully modified", searchTimer->GetID());
return true;
} else {
esyslog("tvguide: error modifying search timer with id %d, build string %s", searchTimer->GetID(), searchTimer->BuildSearchString().c_str());
return false;
}
}
} else {
if (epgSearchPlugin->Service("Epgsearch-services-v1.1", epgSearch)) {
int timerID = epgSearch->handler->AddSearchTimer(searchTimer->BuildSearchString());
if (timerID >=0) {
esyslog("tvguide: search timer with id %d sucessfully created", timerID);
return true;
} else {
esyslog("tvguide: error creating search timer, build string %s", searchTimer->BuildSearchString().c_str());
return false;
}
}
}
return false;
}
void cRecManager::DeleteSearchTimer(cTVGuideSearchTimer *searchTimer, bool delTimers) {
if (!epgSearchAvailable)
return;
int searchTimerID = searchTimer->GetID();
if (delTimers) {
cTimers* timers;
#if defined (APIVERSNUM) && (APIVERSNUM >= 20301)
{
LOCK_TIMERS_WRITE;
timers = Timers;
}
#else
timers = &Timers;
#endif
cTimer *timer = timers->First();
while (timer) {
if (!timer->Recording()) {
char* searchID = GetAuxValue(timer, "s-id");
if (searchID) {
if (searchTimerID == atoi(searchID)) {
cTimer* timerNext = timers->Next(timer);
DeleteTimer(timer);
timer = timerNext;
} else {
timer = timers->Next(timer);
}
free(searchID);
} else {
timer = timers->Next(timer);
}
} else {
timer = timers->Next(timer);
}
}
}
Epgsearch_services_v1_1 *epgSearch = new Epgsearch_services_v1_1;
if (epgSearchPlugin->Service("Epgsearch-services-v1.1", epgSearch)) {
bool success = epgSearch->handler->DelSearchTimer(searchTimerID);
if (success) {
esyslog("tvguide: search timer \"%s\" sucessfully deleted", searchTimer->SearchString().c_str());
} else {
esyslog("tvguide: error deleting search timer \"%s\"", searchTimer->SearchString().c_str());
}
}
}
void cRecManager::UpdateSearchTimers(void) {
if (epgSearchAvailable) {
Epgsearch_updatesearchtimers_v1_0 data;
data.showMessage = false;
epgSearchPlugin->Service("Epgsearch-updatesearchtimers-v1.0", &data);
}
}
// announceOnly: 0 = switch, 1 = announce only, 2 = ask for switch
bool cRecManager::CreateSwitchTimer(const cEvent *event, cSwitchTimer switchTimer) {
if (epgSearchAvailable && event) {
Epgsearch_switchtimer_v1_0 data;
data.event = event;
data.mode = 1;
data.switchMinsBefore = switchTimer.switchMinsBefore;
data.announceOnly = switchTimer.announceOnly;
data.success = false;
epgSearchPlugin->Service("Epgsearch-switchtimer-v1.0", &data);
cSwitchTimer *t = new cSwitchTimer(event);
SwitchTimers.Add(t);
return data.success;
}
return false;
}
void cRecManager::DeleteSwitchTimer(const cEvent *event) {
SwitchTimers.DeleteSwitchTimer(event);
if (epgSearchAvailable) {
Epgsearch_switchtimer_v1_0 data;
data.event = event;
data.mode = 2;
data.switchMinsBefore = 0;
data.announceOnly = 0;
data.success = false;
epgSearchPlugin->Service("Epgsearch-switchtimer-v1.0", &data);
}
}
const cRecording **cRecManager::SearchForRecordings(std::string searchString, int &numResults) {
const cRecording **matchingRecordings = NULL;
int num = 0;
numResults = 0;
#if defined (APIVERSNUM) && (APIVERSNUM >= 20301)
LOCK_RECORDINGS_READ;
const cRecordings* recordings = Recordings;
#else
const cRecordings* recordings = &Recordings;
#endif
for (const cRecording *recording = recordings->First(); recording; recording = recordings->Next(recording)) {
std::string s1 = recording->Name();
std::string s2 = searchString;
if (s1.empty() || s2.empty()) continue;
// tolerance for fuzzy searching: 90% of the shorter text length, but at least 1
int tolerance = std::max(1, (int)std::min(s1.size(), s2.size()) / 10);
bool match = FindIgnoreCase(s1, s2) >= 0 || FindIgnoreCase(s2, s1) >= 0;
if (!match) {
AFUZZY af = { NULL, NULL, NULL, NULL, NULL, NULL, { 0 }, { 0 }, 0, 0, 0, 0, 0, 0 };
if (s1.size() > 32) s1 = s1.substr(0, 32);
afuzzy_init(s1.c_str(), tolerance, 0, &af);
/* Checking substring */
int res = afuzzy_checkSUB(s2.c_str(), &af);
afuzzy_free(&af);
match = (res > 0);
}
if (!match) {
AFUZZY af = { NULL, NULL, NULL, NULL, NULL, NULL, { 0 }, { 0 }, 0, 0, 0, 0, 0, 0 };
if (s2.size() > 32) s2 = s2.substr(0, 32);
afuzzy_init(s2.c_str(), tolerance, 0, &af);
/* Checking substring */
int res = afuzzy_checkSUB(s1.c_str(), &af);
afuzzy_free(&af);
match = (res > 0);
}
if (match) {
matchingRecordings = (const cRecording **)realloc(matchingRecordings, (num + 1) * sizeof(cRecording *));
matchingRecordings[num++] = recording;
}
}
if (num > 0) {
qsort(matchingRecordings, num, sizeof(cRecording *), CompareRecording);
numResults = num;
return matchingRecordings;
}
return NULL;
}
const cEvent **cRecManager::LoadReruns(const cEvent *event, int &numResults) {
if (epgSearchAvailable && !isempty(event->Title())) {
Epgsearch_searchresults_v1_0 data;
std::string strQuery = event->Title();
if (config.useSubtitleRerun > 0) {
if (config.useSubtitleRerun == 2 || !isempty(event->ShortText()))
strQuery += "~";
if (!isempty(event->ShortText()))
strQuery += event->ShortText();
data.useSubTitle = true;
} else {
data.useSubTitle = false;
}
data.query = (char *)strQuery.c_str();
data.mode = 0;
data.channelNr = 0;
data.useTitle = true;
data.useDescription = false;
if (epgSearchPlugin->Service("Epgsearch-searchresults-v1.0", &data)) {
cList<Epgsearch_searchresults_v1_0::cServiceSearchResult>* list = data.pResultList;
if (!list)
return NULL;
const cEvent **searchResults = NULL;
int numElements = list->Count();
if (numElements > 0) {
searchResults = new const cEvent *[numElements];
int index = 0;
for (Epgsearch_searchresults_v1_0::cServiceSearchResult *r = list->First(); r; r = list->Next(r)) {
if ((event->ChannelID() == r->event->ChannelID()) && (event->StartTime() == r->event->StartTime()))
continue;
searchResults[index] = r->event;
index++;
}
delete list;
numResults = index;
return searchResults;
}
}
}
return NULL;
}
void cRecManager::GetFavorites(std::vector<cTVGuideSearchTimer> *favorites) {
if (!epgSearchAvailable) {
return;
}
Epgsearch_services_v1_1 *epgSearch = new Epgsearch_services_v1_1;
if (epgSearchPlugin->Service("Epgsearch-services-v1.1", epgSearch)) {
std::list<std::string> searchTimerList;
searchTimerList = epgSearch->handler->SearchTimerList();
for(std::list<std::string>::iterator it = searchTimerList.begin(); it != searchTimerList.end(); it++) {
cTVGuideSearchTimer timer;
timer.SetEPGSearchString(it->c_str());
if (timer.Parse()) {
if (timer.UseInFavorites())
favorites->push_back(timer);
}
}
}
}
const cEvent **cRecManager::WhatsOnNow(bool nowOrNext, int &numResults) {
std::vector<const cEvent*> tmpResults;
#if defined (APIVERSNUM) && (APIVERSNUM >= 20301)
LOCK_CHANNELS_READ;
const cChannels* channels = Channels;
LOCK_SCHEDULES_READ;
const cSchedules* schedules = Schedules;
#else
cChannels* channels = &Channels;
cSchedulesLock schedulesLock;
const cSchedules* schedules = cSchedules::Schedules(schedulesLock);
#endif
const cChannel *startChannel = NULL, *stopChannel = NULL;
if (config.favLimitChannels) {
startChannel = channels->GetByNumber(config.favStartChannel);
stopChannel = channels->GetByNumber(config.favStopChannel);
}
if (!startChannel)
startChannel = channels->First();
for (const cChannel *channel = startChannel; channel; channel = channels->Next(channel)) {
if (channel->GroupSep()) continue;
const cSchedule *Schedule = schedules->GetSchedule(channel);
if (!Schedule) continue;
const cEvent *event = NULL;
if (nowOrNext)
event = Schedule->GetPresentEvent();
else
event = Schedule->GetFollowingEvent();
if (event) {
tmpResults.push_back(event);
}
if (stopChannel && (stopChannel->Number() <= channel->Number()))
break;
}
numResults = tmpResults.size();
const cEvent **results = new const cEvent *[numResults];
for (int i=0; i<numResults; i++) {
results[i] = tmpResults[i];
}
return results;
}
const cEvent **cRecManager::UserDefinedTime(int userTime, int &numResults) {
std::vector<const cEvent*> tmpResults;
int favTime = 0;
if (userTime == 1) {
favTime = config.favTime1;
} else if (userTime == 2) {
favTime = config.favTime2;
} else if (userTime == 3) {
favTime = config.favTime3;
} else if (userTime == 4) {
favTime = config.favTime4;
}
time_t now = time(0);
tm *midn = localtime(&now);
midn->tm_sec = 0;
midn->tm_min = 0;
midn->tm_hour = 0;
time_t midnight = mktime(midn);
int hours = favTime/100;
int mins = favTime - hours * 100;
time_t searchTime = midnight + hours*60*60 + mins*60;
if (searchTime < now)
searchTime += 24*60*60;
#if defined (APIVERSNUM) && (APIVERSNUM >= 20301)
LOCK_CHANNELS_READ;
const cChannels* channels = Channels;
LOCK_SCHEDULES_READ;
const cSchedules* schedules = Schedules;
#else
cChannels* channels = &Channels;
cSchedulesLock schedulesLock;
const cSchedules* schedules = cSchedules::Schedules(schedulesLock);
#endif
const cChannel *startChannel = NULL, *stopChannel = NULL;
if (config.favLimitChannels) {
startChannel = channels->GetByNumber(config.favStartChannel);
stopChannel = channels->GetByNumber(config.favStopChannel);
}
if (!startChannel)
startChannel = channels->First();
for (const cChannel *channel = startChannel; channel; channel = channels->Next(channel)) {
if (channel->GroupSep()) continue;
const cSchedule *Schedule = schedules->GetSchedule(channel);
if (!Schedule) continue;
const cEvent *event = Schedule->GetEventAround(searchTime);
if (!event) continue;
//if event is more or less over (only 15mns left), take next
if ((event->EndTime() - searchTime) < 15*60) {
event = Schedule->Events()->Next(event);
}
if (event)
tmpResults.push_back(event);
if (stopChannel && (stopChannel->Number() <= channel->Number()))
break;
}
numResults = tmpResults.size();
const cEvent **results = new const cEvent *[numResults];
for (int i=0; i<numResults; i++) {
results[i] = tmpResults[i];
}
return results;
}
|