-
Notifications
You must be signed in to change notification settings - Fork 3
/
import.cpp
2055 lines (1895 loc) · 64.7 KB
/
import.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
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
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* import.h: A plugin for the Video Disk Recorder
*
* See the README file for copyright information and how to reach the author.
*
*/
#include <sqlite3.h>
#include <ctype.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <vdr/channels.h>
#include <regex>
#include "xmltv2vdr.h"
#include "import.h"
#include "event.h"
#include "debug.h"
extern char *strcatrealloc(char *, const char*);
struct cImport::split cImport::split(char *in, char delim)
{
struct split sp;
sp.count=1;
sp.pointers[0]=in;
while (*++in)
{
if (*in==delim)
{
*in=0;
sp.pointers[sp.count++]=in+1;
}
}
return sp;
}
char *cImport::RemoveNonASCII(const char *src)
{
if (!src) return NULL;
int len=strlen(src);
if (!len) return NULL;
char *dst=(char *) malloc(len+1);
if (!dst) return NULL;
char *tmp=dst;
bool lspc=false;
while (*src)
{
// 0x20,0x30-0x39,0x41-0x5A,0x61-0x7A
if ((*src==0x20) && (!lspc))
{
*tmp++=0x20;
lspc=true;
}
if (*src==':')
{
*tmp++=0x20;
lspc=true;
}
if ((*src>=0x30) && (*src<=0x39))
{
*tmp++=*src;
lspc=false;
}
if ((*src>=0x41) && (*src<=0x5A))
{
*tmp++=tolower(*src);
lspc=false;
}
if ((*src>=0x61) && (*src<=0x7A))
{
*tmp++=*src;
lspc=false;
}
src++;
}
*tmp=0;
return dst;
}
cEvent *cImport::SearchVDREventByTitle(cEPGSource *source, cSchedule* schedule, const char *Title, time_t StartTime,
int Duration, int hint)
{
const char *cxTitle=conv->Convert(Title);
// 2nd with StartTime
#if VDRVERSNUM >= 20701
cEvent *f=(cEvent *) schedule->GetEventByTime(StartTime+hint);
#else
cEvent *f=(cEvent *) schedule->GetEvent((tEventID) 0,StartTime+hint);
#endif
if (f)
{
if (!strcasecmp(f->Title(),cxTitle))
{
return f;
}
else
{
f=NULL;
}
}
// 3rd with StartTime +/- TimeDiff
int maxdiff=INT_MAX;
int eventTimeDiff=720;
if (Duration && eventTimeDiff>=Duration) eventTimeDiff/=3;
if (eventTimeDiff<100) eventTimeDiff=100;
for (cEvent *p =(cEvent *) schedule->Events()->First(); p; p = (cEvent *) schedule->Events()->Next(p))
{
int diff=abs((int) difftime(p->StartTime(),StartTime));
if (diff<=eventTimeDiff)
{
// found event with exact the same title
if (!strcasecmp(p->Title(),cxTitle))
{
if (diff<=maxdiff)
{
f=p;
maxdiff=diff;
}
}
else
{
if (f) continue; // we already have an event!
// cut both titles into pieces and check
// if we have at least one match with
// minimum length of 4 characters
// first remove all non ascii characters
// we just want the following codes
// 0x20,0x30-0x39,0x41-0x5A,0x61-0x7A
int wfound=0;
char *s1=RemoveNonASCII(p->Title());
char *s2=RemoveNonASCII(cxTitle);
if (s1 && s2)
{
if (!strcmp(s1,s2))
{
wfound++;
}
else
{
struct split w1 = split(s1,' ');
struct split w2 = split(s2,' ');
if ((w1.count) && (w2.count))
{
for (int i1=0; i1<w1.count; i1++)
{
for (int i2=0; i2<w2.count; i2++)
{
if (!strcmp(w1.pointers[i1],w2.pointers[i2]))
{
if (strlen(w1.pointers[i1])>3) wfound++;
}
}
}
}
}
}
if (s1) free(s1);
if (s2) free(s2);
if (wfound)
{
if (diff<=maxdiff)
{
if (!WasChanged(p))
{
tsyslogs(source,"found '%s' for '%s'",p->Title(),cxTitle);
}
f=p;
maxdiff=diff;
}
}
}
}
}
return f;
}
cEvent *cImport::SearchVDREvent(cEPGSource *source, cSchedule* schedule, cXMLTVEvent *xevent, bool append, int hint)
{
if (!source) return NULL;
if (!schedule) return NULL;
if (!xevent) return NULL;
cEvent *f=NULL;
// try to find an event,
// 1st with our own EventID
#if VDRVERSNUM >= 20701
if (xevent->EITEventID()) f=(cEvent *) schedule->GetEventById(xevent->EITEventID());
#else
if (xevent->EITEventID()) f=(cEvent *) schedule->GetEvent(xevent->EITEventID());
#endif
if (f) return f;
#if VDRVERSNUM >= 20701
if (xevent->EventID() && append) f=(cEvent *) schedule->GetEventById(xevent->EITEventID());
#else
if (xevent->EventID() && append) f=(cEvent *) schedule->GetEvent(xevent->EventID());
#endif
if (f) return f;
f=SearchVDREventByTitle(source, schedule, xevent->Title(), xevent->StartTime(),
xevent->Duration(), hint);
if (f) return f;
if (!xevent->AltTitle()) return NULL;
return SearchVDREventByTitle(source, schedule, xevent->AltTitle(), xevent->StartTime(),
xevent->Duration(), hint);
}
cEvent *cImport::GetEventBefore(cSchedule* schedule, time_t start)
{
if (!schedule) return NULL;
if (!schedule->Events()) return NULL;
if (!schedule->Events()->Count()) return NULL;
cEvent *last=(cEvent *) schedule->Events()->Last();
if ((last) && (last->StartTime()<start)) return last;
for (cEvent *p=(cEvent *) schedule->Events()->First(); p; p=(cEvent *) schedule->Events()->Next(p))
{
if (p->StartTime()>start)
{
return (cEvent *) p->Prev();
}
}
if (last) return last;
return NULL;
}
char *cImport::RemoveLastCharFromDescription(char *description)
{
if (description)
{
int len=strlen(description);
if (!len) return description;
description[len-1]=0;
}
return description;
}
char *cImport::Add2Description(char *description, const char *Value)
{
description = strcatrealloc(description,Value);
return description;
}
char *cImport::Add2Description(char *description, const char *Name, const char *Value)
{
description = strcatrealloc(description,Name);
description = strcatrealloc(description,": ");
description = strcatrealloc(description,Value);
description = strcatrealloc(description,"\n");
return description;
}
char *cImport::Add2Description(char *description, const char *Name, int Value)
{
char *value=NULL;
if (asprintf(&value,"%i",Value)==-1) return NULL;
description = strcatrealloc(description,Name);
description = strcatrealloc(description,": ");
description = strcatrealloc(description,value);
description = strcatrealloc(description,"\n");
free(value);
return description;
}
char *cImport::AddEOT2Description(char *description, bool checkutf8)
{
const char nbspUTF8[]={"\u00A0"};
if (checkutf8)
{
if (!g->Codeset())
{
description=strcatrealloc(description,nbspUTF8);
}
else
{
if (!strncasecmp(g->Codeset(),"UTF-8",5) || !strncasecmp(g->Codeset(),"UTF8",4))
{
description=strcatrealloc(description,nbspUTF8);
}
else
{
const char nbsp[]={"\xA0"};
description=strcatrealloc(description,nbsp);
}
}
}
else
{
description=strcatrealloc(description,nbspUTF8);
}
return description;
}
bool cImport::WasChanged(cEvent* Event)
{
if (!Event) return false;
if (!Event->Description()) return false;
if (!strchr(Event->Description(),0xA0)) return false;
char *p=strchr((char *) Event->Description(),0xA0);
if (!p) return false;
if (!g->Codeset()) return false;
if (!strncasecmp(g->Codeset(),"UTF-8",5) || !strncasecmp(g->Codeset(),"UTF8",4))
{
if ((unsigned char) p[-1]!=0xC2) return false;
}
return true;
}
void cImport::LinkPictures(const char *Source, cXMLTVStringList *Pics, tEventID DestID, tChannelID ChanID, bool MakeOld)
{
// source-pics are located in /var/lib/epgsources/%SOURCE%-img/
// dest-pics are located in imgdir (default /var/cache/vdr/epgimages)
for (int i=0; i<Pics->Size(); i++)
{
char *pic=(*Pics)[i];
char *ext=strrchr(pic,'.');
if (!ext) continue;
ext++;
char *dst,*dstold=NULL;
int ret;
char *src;
if (asprintf(&src,"/var/lib/epgsources/%s-img/%s",Source,pic)==-1) return;
if (MakeOld)
{
if (!i)
{
ret=asprintf(&dstold,"%s/%i.%s",g->ImgDir(),DestID,ext);
}
else
{
ret=asprintf(&dstold,"%s/%i_%i.%s",g->ImgDir(),DestID,i,ext);
}
if (ret==-1)
{
free(src);
return;
}
}
if (!i)
{
ret=asprintf(&dst,"%s/%s_%i.%s",g->ImgDir(),*ChanID.ToString(),DestID,ext);
}
else
{
ret=asprintf(&dst,"%s/%s_%i_%i.%s",g->ImgDir(),*ChanID.ToString(),DestID,i,ext);
}
if (ret==-1)
{
free(src);
if (dstold) free(dstold);
return;
}
struct stat statbuf;
if (MakeOld)
{
if (stat(dstold,&statbuf)!=-1)
{
unlink(dstold);
}
if (symlink(src,dstold)==-1)
{
if (!i)
{
tsyslog("failed to link %s to %i.%s",pic,DestID,ext);
}
else
{
tsyslog("failed to link %s to %i_%i.%s",pic,DestID,i,ext);
}
}
else
{
if (!i)
{
tsyslog("linked %s to %i.%s",pic,DestID,ext);
}
else
{
tsyslog("linked %s to %i_%i.%s",pic,DestID,i,ext);
}
}
}
if (stat(dst,&statbuf)!=-1)
{
unlink(dst);
}
if (symlink(src,dst)==-1)
{
if (!i)
{
tsyslog("failed to link %s to %s_%i.%s",pic,*ChanID.ToString(),DestID,ext);
}
else
{
tsyslog("failed to link %s to %s_%i_%i.%s",pic,*ChanID.ToString(),DestID,i,ext);
}
}
else
{
if (!i)
{
tsyslog("linked %s to %s_%i.%s",pic,*ChanID.ToString(),DestID,ext);
}
else
{
tsyslog("linked %s to %s_%i_%i.%s",pic,*ChanID.ToString(),DestID,i,ext);
}
}
free(src);
free(dst);
if (dstold) free(dstold);
}
}
char *cImport::Add2Description(char *description, cXMLTVEvent *xEvent, int Flags, int what)
{
if (what==USE_LONGTEXT)
{
bool lta=false;
if (((Flags & USE_LONGTEXT)==USE_LONGTEXT) || ((Flags & OPT_APPEND)==OPT_APPEND))
{
if (xEvent->Description() && (strlen(xEvent->Description())>0))
{
description=Add2Description(description,xEvent->Description());
lta=true;
}
}
if (!lta && xEvent->EITDescription() && (strlen(xEvent->EITDescription())>0))
{
description=Add2Description(description,xEvent->EITDescription());
}
description=Add2Description(description,"\n");
}
if ((what==USE_CREDITS) && ((Flags & USE_CREDITS)==USE_CREDITS))
{
cXMLTVStringList *credits=xEvent->Credits();
if (credits->Size())
{
cTEXTMapping *oldtext=NULL;
for (int i=0; i<credits->Size(); i++)
{
char *ctype=strdup((*credits)[i]);
if (ctype)
{
char *cval=strchr(ctype,'|');
if (cval)
{
*cval=0;
cval++;
bool add=true;
if (((Flags & CREDITS_ACTORS)!=CREDITS_ACTORS) &&
(!strcasecmp(ctype,"actor"))) add=false;
if (((Flags & CREDITS_DIRECTORS)!=CREDITS_DIRECTORS) &&
(!strcasecmp(ctype,"director"))) add=false;
if (((Flags & CREDITS_OTHERS)!=CREDITS_OTHERS) &&
(add) && (strcasecmp(ctype,"actor")) &&
(strcasecmp(ctype,"director"))) add=false;
if (add)
{
cTEXTMapping *text=g->TEXTMappings()->GetMap(ctype);
if ((Flags & CREDITS_LIST)==CREDITS_LIST)
{
if (oldtext!=text)
{
if (oldtext)
{
description=RemoveLastCharFromDescription(description);
description=RemoveLastCharFromDescription(description);
description=Add2Description(description,"\n");
}
description=Add2Description(description,text->Value());
description=Add2Description(description,": ");
}
description=Add2Description(description,cval);
description=Add2Description(description,", ");
}
else
{
if (text)
{
description=Add2Description(description,text->Value(),cval);
}
}
oldtext=text;
}
}
free(ctype);
}
}
if ((oldtext) && ((Flags & CREDITS_LIST)==CREDITS_LIST))
{
description=RemoveLastCharFromDescription(description);
description=RemoveLastCharFromDescription(description);
description=Add2Description(description,"\n");
}
}
}
if ((what==USE_COUNTRYDATE) && ((Flags & USE_COUNTRYDATE)==USE_COUNTRYDATE))
{
if (xEvent->Country())
{
cTEXTMapping *text=g->TEXTMappings()->GetMap("country");
if (text) description=Add2Description(description,text->Value(),xEvent->Country());
}
if (xEvent->Year())
{
cTEXTMapping *text=g->TEXTMappings()->GetMap("year");
if (text) description=Add2Description(description,text->Value(),xEvent->Year());
}
}
if ((what==USE_ORIGTITLE) && ((Flags & USE_ORIGTITLE)==USE_ORIGTITLE) &&
(xEvent->OrigTitle()))
{
cTEXTMapping *text=g->TEXTMappings()->GetMap("originaltitle");
if (text) description=Add2Description(description,text->Value(),xEvent->OrigTitle());
}
if ((what==USE_CATEGORIES) && ((Flags & USE_CATEGORIES)==USE_CATEGORIES) &&
(xEvent->Category()->Size()))
{
cTEXTMapping *text=g->TEXTMappings()->GetMap("category");
if (text)
{
cXMLTVStringList *categories=xEvent->Category();
// prevent duplicates
if ((*categories)[0][0]!='G' && (*categories)[0][1]!=' ')
description=Add2Description(description,text->Value(),(*categories)[0]);
for (int i=1; i<categories->Size(); i++)
{
if (strcasecmp((*categories)[i],(*categories)[i-1]))
{
if ((*categories)[i][0]!='G' && (*categories)[i][1]!=' ')
description=Add2Description(description,text->Value(),(*categories)[i]);
}
}
}
}
if ((what==USE_VIDEO) && ((Flags & USE_VIDEO)==USE_VIDEO) && (xEvent->Video()->Size()))
{
cTEXTMapping *text=g->TEXTMappings()->GetMap("video");
if (text)
{
description=Add2Description(description,text->Value());
description=Add2Description(description,": ");
cXMLTVStringList *video=xEvent->Video();
for (int i=0; i<video->Size(); i++)
{
char *vtype=strdup((*video)[i]);
if (vtype)
{
char *vval=strchr(vtype,'|');
if (vval)
{
*vval=0;
vval++;
if (i)
{
description=Add2Description(description,", ");
}
if (!strcasecmp(vtype,"colour"))
{
if (!strcasecmp(vval,"no"))
{
cTEXTMapping *text=g->TEXTMappings()->GetMap("blacknwhite");
description=Add2Description(description,text->Value());
}
}
else
{
description=Add2Description(description,vval);
}
}
free(vtype);
}
}
description=Add2Description(description,"\n");
}
}
if ((what==USE_AUDIO) && ((Flags & USE_AUDIO)==USE_AUDIO))
{
if (xEvent->Audio())
{
cTEXTMapping *text=g->TEXTMappings()->GetMap("audio");
if (text)
{
if ((!strcasecmp(xEvent->Audio(),"mono")) || (!strcasecmp(xEvent->Audio(),"stereo")))
{
description=Add2Description(description,text->Value());
description=Add2Description(description,": ");
description=Add2Description(description,xEvent->Audio());
description=Add2Description(description,"\n");
}
else
{
cTEXTMapping *atext=g->TEXTMappings()->GetMap(xEvent->Audio());
if (atext)
{
description=Add2Description(description,text->Value());
description=Add2Description(description,": ");
description=Add2Description(description,atext->Value());
description=Add2Description(description,"\n");
}
}
}
}
}
if ((what==USE_SEASON) && ((Flags & USE_SEASON)==USE_SEASON))
{
if (xEvent->Season())
{
cTEXTMapping *text=g->TEXTMappings()->GetMap("season");
if (text) description=Add2Description(description,text->Value(),
xEvent->Season());
}
if (xEvent->Episode())
{
cTEXTMapping *text=g->TEXTMappings()->GetMap("episode");
if (text) description=Add2Description(description,text->Value(),
xEvent->Episode());
}
if (xEvent->EpisodeOverall())
{
cTEXTMapping *text=g->TEXTMappings()->GetMap("episodeoverall");
if (text) description=Add2Description(description,text->Value(),
xEvent->EpisodeOverall());
}
}
if ((what==USE_RATING) && ((Flags & USE_RATING)==USE_RATING) &&
(xEvent->Rating()->Size()))
{
#if VDRVERSNUM < 10711 && !EPGHANDLER
Flags|=OPT_RATING_TEXT; // always add to text if we dont have the internal tag!
#endif
if ((Flags & OPT_RATING_TEXT)==OPT_RATING_TEXT)
{
cXMLTVStringList *rating=xEvent->Rating();
for (int i=0; i<rating->Size(); i++)
{
char *rtype=strdup((*rating)[i]);
if (rtype)
{
char *rval=strchr(rtype,'|');
if (rval)
{
*rval=0;
rval++;
description=Add2Description(description,rtype);
description=Add2Description(description,": ");
description=Add2Description(description,rval);
description=Add2Description(description,"\n");
}
free(rtype);
}
}
}
}
if ((what==USE_STARRATING) && ((Flags & USE_STARRATING)==USE_STARRATING) &&
(xEvent->StarRating()->Size()))
{
cTEXTMapping *text=g->TEXTMappings()->GetMap("starrating");
if (text)
{
description=Add2Description(description,text->Value());
description=Add2Description(description,": ");
cXMLTVStringList *starrating=xEvent->StarRating();
for (int i=0; i<starrating->Size(); i++)
{
char *rtype=strdup((*starrating)[i]);
if (rtype)
{
char *rval=strchr(rtype,'|');
if (rval)
{
*rval=0;
rval++;
if (i)
{
description=Add2Description(description,", ");
}
if (strcasecmp(rtype,"*"))
{
description=Add2Description(description,rtype);
description=Add2Description(description," ");
}
description=Add2Description(description,rval);
}
free(rtype);
}
}
description=Add2Description(description,"\n");
}
}
if ((what==USE_REVIEW) && ((Flags & USE_REVIEW)==USE_REVIEW) &&
(xEvent->Review()->Size()))
{
cTEXTMapping *text=g->TEXTMappings()->GetMap("review");
if (text)
{
cXMLTVStringList *review=xEvent->Review();
for (int i=0; i<review->Size(); i++)
{
description=Add2Description(description,text->Value(),(*review)[i]);
}
}
}
return description;
}
bool cImport::PutEvent(cEPGSource *Source, sqlite3 *Db, cSchedule* Schedule,
cEvent *Event, cXMLTVEvent *xEvent,int Flags)
{
if (!Source) return false;
if (!Db) return false;
if (!xEvent) return false;
if (!g) return false;
#define CHANGED_NOTHING 0
#define CHANGED_TITLE 1
#define CHANGED_SHORTTEXT 2
#define CHANGED_DESCRIPTION 4
struct tm tm;
char from[80];
char till[80];
time_t start,end;
int changed=CHANGED_NOTHING;
bool append=false;
bool retcode=false;
bool added=false;
if ((Flags & OPT_APPEND)==OPT_APPEND) append=true;
if (append && !Event)
{
if (!Schedule) return false;
start=xEvent->StartTime();
end=start+xEvent->Duration();
/* checking the "space" for our new event */
cEvent *prev=GetEventBefore(Schedule,start);
if (prev)
{
if (cEvent *next=(cEvent *) prev->Next())
{
if (prev->EndTime()==next->StartTime())
{
// ok - no gap
localtime_r(&start,&tm);
strftime(from,sizeof(from)-1,"%b %d %H:%M",&tm);
localtime_r(&end,&tm);
strftime(till,sizeof(till)-1,"%b %d %H:%M",&tm);
esyslogs(Source,"cannot add '%s'@%s-%s",xEvent->Title(),from,till);
time_t pstart=prev->StartTime();
time_t pstop=prev->EndTime();
localtime_r(&pstart,&tm);
strftime(from,sizeof(from)-1,"%b %d %H:%M",&tm);
localtime_r(&pstop,&tm);
strftime(till,sizeof(till)-1,"%b %d %H:%M",&tm);
esyslogs(Source,"found '%s'@%s-%s",prev->Title(),from,till);
time_t nstart=next->StartTime();
time_t nstop=next->EndTime();
localtime_r(&nstart,&tm);
strftime(from,sizeof(from)-1,"%b %d %H:%M",&tm);
localtime_r(&nstop,&tm);
strftime(till,sizeof(till)-1,"%b %d %H:%M",&tm);
esyslogs(Source,"found '%s'@%s-%s",next->Title(),from,till);
return false;
}
if (end>next->StartTime())
{
int diff=(int) difftime(prev->EndTime(),start);
if (diff>420)
{
localtime_r(&start,&tm);
strftime(from,sizeof(from)-1,"%b %d %H:%M",&tm);
localtime_r(&end,&tm);
strftime(till,sizeof(till)-1,"%b %d %H:%M",&tm);
esyslogs(Source,"cannot add '%s'@%s-%s",xEvent->Title(),from,till);
time_t nstart=next->StartTime();
time_t nstop=next->EndTime();
localtime_r(&nstart,&tm);
strftime(from,sizeof(from)-1,"%b %d %H:%M",&tm);
localtime_r(&nstop,&tm);
strftime(till,sizeof(till)-1,"%b %d %H:%M",&tm);
esyslogs(Source,"found '%s'@%s-%s",next->Title(),from,till);
return false;
}
else
{
xEvent->SetDuration(xEvent->Duration()-diff);
}
}
}
else
{
// no next event, check for gaps
if (prev->EndTime()!=start)
{
tsyslogs(Source,"detected gap of %lis",(long int)(start-prev->EndTime()));
}
}
if (prev->EndTime()>start)
{
int diff=(int) difftime(prev->EndTime(),start);
if (diff>300)
{
localtime_r(&start,&tm);
strftime(from,sizeof(from)-1,"%b %d %H:%M",&tm);
localtime_r(&end,&tm);
strftime(till,sizeof(till)-1,"%b %d %H:%M",&tm);
esyslogs(Source,"cannot add '%s'@%s-%s",xEvent->Title(),from,till);
time_t pstart=prev->StartTime();
time_t pstop=prev->EndTime();
localtime_r(&pstart,&tm);
strftime(from,sizeof(from)-1,"%b %d %H:%M",&tm);
localtime_r(&pstop,&tm);
strftime(till,sizeof(till)-1,"%b %d %H:%M",&tm);
esyslogs(Source,"found '%s'@%s-%s",prev->Title(),from,till);
return false;
}
else
{
prev->SetDuration(prev->Duration()-diff);
}
}
if (!xEvent->Duration())
{
if (!prev->Duration())
{
prev->SetDuration(start-prev->StartTime());
}
}
}
/* add event */
Event=new cEvent(xEvent->EventID());
if (!Event) return false;
Event->SetStartTime(start);
Event->SetDuration(xEvent->Duration());
Event->SetTitle(xEvent->Title());
Event->SetVersion(0);
Event->SetTableID(0);
Schedule->AddEvent(Event);
Schedule->Sort();
added=true;
if (xEvent->Pics()->Size() && Source->UsePics())
{
/* here's a good place to link pictures! */
LinkPictures(xEvent->Source(),xEvent->Pics(),Event->EventID(),Event->ChannelID());
}
if (Source->Trace())
{
localtime_r(&start,&tm);
strftime(from,sizeof(from)-1,"%b %d %H:%M",&tm);
localtime_r(&end,&tm);
strftime(till,sizeof(till)-1,"%b %d %H:%M",&tm);
tsyslogs(Source,"{%5i} adding '%s'/'%s'@%s-%s",xEvent->EventID(),xEvent->Title(),
xEvent->ShortText(),from,till);
}
retcode=true;
}
if (!Event) return false;
if ((Flags & USE_TITLE)==USE_TITLE)
{
if (xEvent->Title() && (strlen(xEvent->Title())>0))
{
const char *dp=conv->Convert(xEvent->Title());
if (!Event->Title() || strcmp(Event->Title(),dp))
{
tsyslogs(Source,"{%5i} changing title from '%s' to '%s'",Event->EventID(),Event->Title(),dp);
Event->SetTitle(dp);
changed|=CHANGED_TITLE; // title really changed
}
}
}
if ((Flags & OPT_SEASON_STEXTITLE)==OPT_SEASON_STEXTITLE)
{
if (xEvent->AltTitle() && (strlen(xEvent->AltTitle())>0))
{
const char *dp=conv->Convert(xEvent->AltTitle());
if (!Event->Title() || strcmp(Event->Title(),dp))
{
tsyslogs(Source,"{%5i} changing title from '%s' to '%s'",Event->EventID(),Event->Title(),dp);
Event->SetTitle(dp);
changed|=CHANGED_TITLE; // title really changed
}
}
}
if (((Flags & USE_SHORTTEXT)==USE_SHORTTEXT) || (append))
{
if (xEvent->ShortText() && (strlen(xEvent->ShortText())>0))
{
if (!strcasecmp(xEvent->ShortText(),Event->Title()))
{
tsyslogs(Source,"{%5i} title and subtitle equal, clearing subtitle",Event->EventID());
Event->SetShortText(NULL);
}
else
{
if (!strcmp(xEvent->ShortText(),"@"))
{
if (!Event->ShortText() || strcmp(Event->ShortText(),""))
{
Event->SetShortText("");
changed|=CHANGED_SHORTTEXT; // shorttext really changed
}
}
else
{
const char *dp=conv->Convert(xEvent->ShortText());
if (!Event->ShortText() || strcmp(Event->ShortText(),dp))
{
Event->SetShortText(dp);
changed|=CHANGED_SHORTTEXT; // shorttext really changed
}
}
}
}
}
if (!append)
{
const char *eitdescription=Event->Description();
if (WasChanged(Event))
{
eitdescription=NULL; // we cannot use Event->Description() - it was already changed!
if (!xEvent->EITDescription()) return false; // no eitdescription in db? -> cannot mix!
}
if (!xEvent->EITEventID() || eitdescription)
{
if (!xEvent->EITEventID() && xEvent->Pics()->Size() && Source->UsePics())
{
/* here's a good place to link pictures! */
LinkPictures(xEvent->Source(),xEvent->Pics(),Event->EventID(),Event->ChannelID());
}
UpdateXMLTVEvent(Source,Db,Event,xEvent,eitdescription);
}
}
char *description=NULL;
const char *ot=g->Order();
if (!ot) return false;
while (*ot)
{
if (*ot==',') ot++;
if (!strncmp(ot,"LOT",3)) description=Add2Description(description,xEvent,Flags,USE_LONGTEXT);
if (!strncmp(ot,"CRS",3)) description=Add2Description(description,xEvent,Flags,USE_CREDITS);
if (!strncmp(ot,"CAD",3)) description=Add2Description(description,xEvent,Flags,USE_COUNTRYDATE);
if (!strncmp(ot,"ORT",3)) description=Add2Description(description,xEvent,Flags,USE_ORIGTITLE);