cache movie filelengths in .meta file, by luke_s
[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 <lib/dvb/idvb.h>
6 #include <dvbsi++/event_information_section.h>
7 #include <dvbsi++/short_event_descriptor.h>
8 #include <dvbsi++/extended_event_descriptor.h>
9 #include <dvbsi++/linkage_descriptor.h>
10 #include <dvbsi++/component_descriptor.h>
11 #include <dvbsi++/descriptor_tag.h>
12
13 #include <sys/types.h>
14 #include <fcntl.h>
15
16 // static members / methods
17 std::string eServiceEvent::m_language = "de_DE";
18
19 void eServiceEvent::setEPGLanguage( const std::string language )
20 {
21         m_language = language;
22 }
23 ///////////////////////////
24
25 DEFINE_REF(eServiceEvent);
26 DEFINE_REF(eComponentData);
27
28 const char MAX_LANG = 37;
29 /* OSD language (see /share/locales/locales) to iso639 conversion table */
30 std::string ISOtbl[MAX_LANG][2] =
31 {
32         {"ar_AE","ara"},
33         {"C","eng"},
34         {"cs_CZ","ces"},     /* or 'cze' */
35         {"cs_CZ","cze"},
36         {"da_DK","dan"},
37         {"de_DE","deu"},     /* also 'ger' is valid iso639 code!! */
38         {"de_DE","ger"},
39         {"el_GR","gre"},     /* also 'ell' is valid */
40         {"el_GR","ell"},
41         {"es_ES","esl"},     /* also 'spa' is ok */
42         {"es_ES","spa"},
43         {"et_EE","est"},
44         {"fi_FI","fin"},
45         {"fr_FR","fra"},
46         {"hr_HR","hrv"},     /* or 'scr' */
47         {"hr_HR","scr"},
48         {"hu_HU","hun"},
49         {"is_IS","isl"},     /* or 'ice' */
50         {"is_IS","ice"},
51         {"it_IT","ita"},
52         {"lt_LT","lit"},
53         {"nl_NL","nld"},     /* or 'dut' */
54         {"nl_NL","dut"},
55         {"no_NO","nor"},
56         {"pl_PL","pol"},
57         {"pt_PT","por"},
58         {"ro_RO","ron"},     /* or 'rum' */
59         {"ro_RO","rum"},
60         {"ru_RU","rus"},
61         {"sk_SK","slk"},     /* or 'slo' */
62         {"sk_SK","slo"},
63         {"sl_SI","slv"},
64         {"sr_YU","srp"},     /* or 'scc' */
65         {"sr_YU","scc"},
66         {"sv_SE","swe"},
67         {"tr_TR","tur"},
68         {"ur_IN","urd"}
69 };
70
71 /* search for the presence of language from given EIT event descriptors*/
72 bool eServiceEvent::loadLanguage(Event *evt, std::string lang, int tsidonid)
73 {
74         bool retval=0;
75         for (DescriptorConstIterator desc = evt->getDescriptors()->begin(); desc != evt->getDescriptors()->end(); ++desc)
76         {
77                 switch ((*desc)->getTag())
78                 {
79                         case LINKAGE_DESCRIPTOR:
80                                 m_linkage_services.clear();
81                                 break;
82                         case SHORT_EVENT_DESCRIPTOR:
83                         {
84                                 const ShortEventDescriptor *sed = (ShortEventDescriptor*)*desc;
85                                 const std::string &cc = sed->getIso639LanguageCode();
86                                 int table=encodingHandler.getCountryCodeDefaultMapping(cc);
87                                 if (lang.empty())
88                                         lang = cc;  // use first found language
89                                 if (cc == lang)
90                                 {
91                                         m_event_name = convertDVBUTF8(sed->getEventName(), table, tsidonid);
92                                         m_short_description = convertDVBUTF8(sed->getText(), table, tsidonid);
93                                         retval=1;
94                                 }
95                                 break;
96                         }
97                         case EXTENDED_EVENT_DESCRIPTOR:
98                         {
99                                 const ExtendedEventDescriptor *eed = (ExtendedEventDescriptor*)*desc;
100                                 const std::string &cc = eed->getIso639LanguageCode();
101                                 int table=encodingHandler.getCountryCodeDefaultMapping(cc);
102                                 if (lang.empty())
103                                         lang = cc;  // use first found language
104                                 if (cc == lang)
105                                 {
106                                         m_extended_description += convertDVBUTF8(eed->getText(), table, tsidonid);
107                                         retval=1;
108                                 }
109 #if 0
110                                 const ExtendedEventList *itemlist = eed->getItems();
111                                 for (ExtendedEventConstIterator it = itemlist->begin(); it != itemlist->end(); ++it)
112                                 {
113                                         m_extended_description += '\n';
114                                         m_extended_description += convertDVBUTF8((*it)->getItemDescription());
115                                         m_extended_description += ' ';
116                                         m_extended_description += convertDVBUTF8((*it)->getItem());
117                                 }
118 #endif
119                                 break;
120                         }
121                         default:
122                                 break;
123                 }
124         }
125         if ( retval == 1 )
126         {
127                 for (DescriptorConstIterator desc = evt->getDescriptors()->begin(); desc != evt->getDescriptors()->end(); ++desc)
128                 {
129                         switch ((*desc)->getTag())
130                         {
131                                 case COMPONENT_DESCRIPTOR:
132                                 {
133                                         const ComponentDescriptor *cp = (ComponentDescriptor*)*desc;
134                                         eComponentData data;
135                                         data.m_streamContent = cp->getStreamContent();
136                                         data.m_componentType = cp->getComponentType();
137                                         data.m_componentTag = cp->getComponentTag();
138                                         data.m_iso639LanguageCode = cp->getIso639LanguageCode();
139                                         int table=encodingHandler.getCountryCodeDefaultMapping(data.m_iso639LanguageCode);
140                                         data.m_text = convertDVBUTF8(cp->getText(),table,tsidonid);
141                                         m_component_data.push_back(data);
142                                         break;
143                                 }
144                                 case LINKAGE_DESCRIPTOR:
145                                 {
146                                         const LinkageDescriptor  *ld = (LinkageDescriptor*)*desc;
147                                         if ( ld->getLinkageType() == 0xB0 )
148                                         {
149                                                 eServiceReference ref;
150                                                 ref.type = eServiceReference::idDVB;
151                                                 eServiceReferenceDVB &dvb_ref = (eServiceReferenceDVB&) ref;
152                                                 dvb_ref.setServiceType(1);
153                                                 dvb_ref.setTransportStreamID(ld->getTransportStreamId());
154                                                 dvb_ref.setOriginalNetworkID(ld->getOriginalNetworkId());
155                                                 dvb_ref.setServiceID(ld->getServiceId());
156                                                 const PrivateDataByteVector *privateData = ld->getPrivateDataBytes();
157                                                 dvb_ref.name = convertDVBUTF8((const unsigned char*)&((*privateData)[0]), privateData->size(), 0, tsidonid);
158                                                 m_linkage_services.push_back(ref);
159                                         }
160                                         break;
161                                 }
162                         }
163                 }
164         }
165         if ( m_extended_description.find(m_short_description) == 0 )
166                 m_short_description="";
167         return retval;
168 }
169
170 RESULT eServiceEvent::parseFrom(Event *evt, int tsidonid)
171 {
172         uint16_t stime_mjd = evt->getStartTimeMjd();
173         uint32_t stime_bcd = evt->getStartTimeBcd();
174         uint32_t duration = evt->getDuration();
175         m_begin = parseDVBtime(
176                 stime_mjd >> 8,
177                 stime_mjd&0xFF,
178                 stime_bcd >> 16,
179                 (stime_bcd >> 8)&0xFF,
180                 stime_bcd & 0xFF
181         );
182         m_event_id = evt->getEventId();
183         m_duration = fromBCD(duration>>16)*3600+fromBCD(duration>>8)*60+fromBCD(duration);
184         for (int i=0; i < MAX_LANG; i++)
185                 if (m_language==ISOtbl[i][0])
186                         if (loadLanguage(evt, ISOtbl[i][1], tsidonid))
187                                 return 0;
188         if (loadLanguage(evt, "eng", tsidonid))
189                 return 0;
190         if (loadLanguage(evt, std::string(), tsidonid))
191                 return 0;
192         return 0;
193 }
194
195 RESULT eServiceEvent::parseFrom(const std::string filename, int tsidonid)
196 {
197         if (!filename.empty())
198         {
199                 int fd = ::open( filename.c_str(), O_RDONLY );
200                 if ( fd > -1 )
201                 {
202                         __u8 buf[4096];
203                         int rd = ::read(fd, buf, 4096);
204                         ::close(fd);
205                         if ( rd > 12 /*EIT_LOOP_SIZE*/ )
206                         {
207                                 Event ev(buf);
208                                 parseFrom(&ev, tsidonid);
209                                 return 0;
210                         }
211                 }
212         }
213         return -1;
214 }
215
216 std::string eServiceEvent::getBeginTimeString() const
217 {
218         tm t;
219         localtime_r(&m_begin, &t);
220         char tmp[13];
221         snprintf(tmp, 13, "%02d.%02d, %02d:%02d",
222                 t.tm_mday, t.tm_mon+1,
223                 t.tm_hour, t.tm_min);
224         return std::string(tmp, 12);
225 }
226
227 RESULT eServiceEvent::getComponentData(ePtr<eComponentData> &dest, int tagnum) const
228 {
229         std::list<eComponentData>::const_iterator it =
230                 m_component_data.begin();
231         for(;it != m_component_data.end(); ++it)
232         {
233                 if ( it->m_componentTag == tagnum )
234                 {
235                         dest=new eComponentData(*it);
236                         return 0;
237                 }
238         }
239         dest=0;
240         return -1;
241 }
242
243 PyObject *eServiceEvent::getComponentData() const
244 {
245         ePyObject ret = PyList_New(m_component_data.size());
246         int cnt=0;
247         for (std::list<eComponentData>::const_iterator it(m_component_data.begin()); it != m_component_data.end(); ++it)
248         {
249                 ePyObject tuple = PyTuple_New(5);
250                 PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(it->m_componentTag));
251                 PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(it->m_componentType));
252                 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(it->m_streamContent));
253                 PyTuple_SET_ITEM(tuple, 3, PyString_FromString(it->m_iso639LanguageCode.c_str()));
254                 PyTuple_SET_ITEM(tuple, 4, PyString_FromString(it->m_text.c_str()));
255                 PyList_SET_ITEM(ret, cnt++, tuple);
256         }
257         return ret;
258 }
259
260 RESULT eServiceEvent::getLinkageService(eServiceReference &service, eServiceReference &parent, int num) const
261 {
262         std::list<eServiceReference>::const_iterator it =
263                 m_linkage_services.begin();
264         while( it != m_linkage_services.end() && num-- )
265                 ++it;
266         if ( it != m_linkage_services.end() )
267         {
268                 service = *it;
269                 eServiceReferenceDVB &subservice = (eServiceReferenceDVB&) service;
270                 eServiceReferenceDVB &current = (eServiceReferenceDVB&) parent;
271                 subservice.setDVBNamespace(current.getDVBNamespace());
272                 if ( current.getParentTransportStreamID().get() )
273                 {
274                         subservice.setParentTransportStreamID( current.getParentTransportStreamID() );
275                         subservice.setParentServiceID( current.getParentServiceID() );
276                 }
277                 else
278                 {
279                         subservice.setParentTransportStreamID( current.getTransportStreamID() );
280                         subservice.setParentServiceID( current.getServiceID() );
281                 }
282                 if ( subservice.getParentTransportStreamID() == subservice.getTransportStreamID() &&
283                         subservice.getParentServiceID() == subservice.getServiceID() )
284                 {
285                         subservice.setParentTransportStreamID( eTransportStreamID(0) );
286                         subservice.setParentServiceID( eServiceID(0) );
287                 }
288                 return 0;
289         }
290         service.type = eServiceReference::idInvalid;
291         return -1;
292 }
293
294 void setServiceEventLanguage(const std::string language)
295 {
296         eServiceEvent::setEPGLanguage(language);
297 }
298
299 DEFINE_REF(eDebugClass);