sorry, i REALLY don't know, how to fix it other than using a try/except block
[enigma2.git] / lib / service / event.cpp
1 #include <lib/service/event.h>
2 #include <lib/base/estring.h>
3 #include <lib/base/encoding.h>
4 #include <lib/dvb/dvbtime.h>
5 #include <dvbsi++/event_information_section.h>
6 #include <dvbsi++/short_event_descriptor.h>
7 #include <dvbsi++/extended_event_descriptor.h>
8 #include <dvbsi++/descriptor_tag.h>
9
10 DEFINE_REF(eServiceEvent);
11
12 const char MAX_LANG = 37;
13 /* OSD language (see /share/locales/locales) to iso639 conversion table */
14 std::string ISOtbl[MAX_LANG][2] =
15 {
16         {"ar_AE","ara"},
17         {"C","eng"},
18         {"cs_CZ","ces"},     /* or 'cze' */
19         {"cs_CZ","cze"},
20         {"da_DK","dan"},
21         {"de_DE","deu"},     /* also 'ger' is valid iso639 code!! */
22         {"de_DE","ger"},
23         {"el_GR","gre"},     /* also 'ell' is valid */
24         {"el_GR","ell"},
25         {"es_ES","esl"},     /* also 'spa' is ok */
26         {"es_ES","spa"},
27         {"et_EE","est"},
28         {"fi_FI","fin"},
29         {"fr_FR","fra"},
30         {"hr_HR","hrv"},     /* or 'scr' */
31         {"hr_HR","scr"},
32         {"hu_HU","hun"},
33         {"is_IS","isl"},     /* or 'ice' */
34         {"is_IS","ice"},
35         {"it_IT","ita"},
36         {"lt_LT","lit"},
37         {"nl_NL","nld"},     /* or 'dut' */
38         {"nl_NL","dut"},
39         {"no_NO","nor"},
40         {"pl_PL","pol"},
41         {"pt_PT","por"},
42         {"ro_RO","ron"},     /* or 'rum' */
43         {"ro_RO","rum"},
44         {"ru_RU","rus"},
45         {"sk_SK","slk"},     /* or 'slo' */
46         {"sk_SK","slo"},
47         {"sl_SI","slv"},
48         {"sr_YU","srp"},     /* or 'scc' */
49         {"sr_YU","scc"},
50         {"sv_SE","swe"},
51         {"tr_TR","tur"},
52         {"ur_IN","urd"}
53 };
54
55 /* search for the presence of language from given EIT event descriptors*/
56 bool eServiceEvent::loadLanguage(Event *evt, std::string lang, int tsidonid)
57 {
58         bool retval=0;
59         for (DescriptorConstIterator desc = evt->getDescriptors()->begin(); desc != evt->getDescriptors()->end(); ++desc)
60         {
61                 switch ((*desc)->getTag())
62                 {
63                         case SHORT_EVENT_DESCRIPTOR:
64                         {
65                                 const ShortEventDescriptor *sed = (ShortEventDescriptor*)*desc;
66                                 const std::string &cc = sed->getIso639LanguageCode();
67                                 int table=encodingHandler.getCountryCodeDefaultMapping(cc);
68                                 if (lang.empty() || cc == lang)
69                                 {
70                                         m_event_name = convertDVBUTF8(sed->getEventName(), table, tsidonid);
71                                         m_short_description = convertDVBUTF8(sed->getText(), table, tsidonid);
72                                         retval=1;
73                                 }
74                                 break;
75                         }
76                         case EXTENDED_EVENT_DESCRIPTOR:
77                         {
78                                 const ExtendedEventDescriptor *eed = (ExtendedEventDescriptor*)*desc;
79                                 const std::string &cc = eed->getIso639LanguageCode();
80                                 int table=encodingHandler.getCountryCodeDefaultMapping(cc);
81                                 if (lang.empty() || cc == lang)
82                                 {
83                                         m_extended_description += convertDVBUTF8(eed->getText(), table, tsidonid);
84                                         retval=1;
85                                 }
86 #if 0
87                                 const ExtendedEventList *itemlist = eed->getItems();
88                                 for (ExtendedEventConstIterator it = itemlist->begin(); it != itemlist->end(); ++it)
89                                 {
90                                         m_extended_description += '\n';
91                                         m_extended_description += convertDVBUTF8((*it)->getItemDescription());
92                                         m_extended_description += ' ';
93                                         m_extended_description += convertDVBUTF8((*it)->getItem());
94                                 }
95 #endif
96                                 break;
97                         }
98                         default:
99                                 break;
100                 }
101         }
102         if ( m_extended_description.find(m_short_description) == 0 )
103                 m_short_description="";
104         return retval;
105 }
106
107 RESULT eServiceEvent::parseFrom(Event *evt, int tsidonid)
108 {
109         uint16_t stime_mjd = evt->getStartTimeMjd();
110         uint32_t stime_bcd = evt->getStartTimeBcd();
111         uint32_t duration = evt->getDuration();
112         m_begin = parseDVBtime(
113                 stime_mjd >> 8,
114                 stime_mjd&0xFF,
115                 stime_bcd >> 16,
116                 (stime_bcd >> 8)&0xFF,
117                 stime_bcd & 0xFF
118         );
119         m_duration = fromBCD(duration>>16)*3600+fromBCD(duration>>8)*60+fromBCD(duration);
120         std::string country="de_DE";  // TODO use local data here
121         for (int i=0; i < MAX_LANG; i++)
122                 if (country==ISOtbl[i][0])
123                         if (loadLanguage(evt, ISOtbl[i][1], tsidonid))
124                                 return 0;
125         if (loadLanguage(evt, "eng", tsidonid))
126                 return 0;
127         if (loadLanguage(evt, std::string(), tsidonid))
128                 return 0;
129         return 0;
130 }
131
132 std::string eServiceEvent::getBeginTimeString()
133 {
134         tm t;
135         localtime_r(&m_begin, &t);
136         char tmp[13];
137         snprintf(tmp, 13, "%02d.%02d, %02d:%02d",
138                 t.tm_mday, t.tm_mon+1,
139                 t.tm_hour, t.tm_min);
140         return std::string(tmp, 12);
141 }
142
143 DEFINE_REF(eDebugClass);