translate text attribute for skin objects
[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() || cc == lang)
88                                 {
89                                         m_event_name = convertDVBUTF8(sed->getEventName(), table, tsidonid);
90                                         m_short_description = convertDVBUTF8(sed->getText(), table, tsidonid);
91                                         retval=1;
92                                 }
93                                 break;
94                         }
95                         case EXTENDED_EVENT_DESCRIPTOR:
96                         {
97                                 const ExtendedEventDescriptor *eed = (ExtendedEventDescriptor*)*desc;
98                                 const std::string &cc = eed->getIso639LanguageCode();
99                                 int table=encodingHandler.getCountryCodeDefaultMapping(cc);
100                                 if (lang.empty() || cc == lang)
101                                 {
102                                         m_extended_description += convertDVBUTF8(eed->getText(), table, tsidonid);
103                                         retval=1;
104                                 }
105 #if 0
106                                 const ExtendedEventList *itemlist = eed->getItems();
107                                 for (ExtendedEventConstIterator it = itemlist->begin(); it != itemlist->end(); ++it)
108                                 {
109                                         m_extended_description += '\n';
110                                         m_extended_description += convertDVBUTF8((*it)->getItemDescription());
111                                         m_extended_description += ' ';
112                                         m_extended_description += convertDVBUTF8((*it)->getItem());
113                                 }
114 #endif
115                                 break;
116                         }
117                         default:
118                                 break;
119                 }
120         }
121         if ( retval == 1 )
122         {
123                 for (DescriptorConstIterator desc = evt->getDescriptors()->begin(); desc != evt->getDescriptors()->end(); ++desc)
124                 {
125                         switch ((*desc)->getTag())
126                         {
127                                 case COMPONENT_DESCRIPTOR:
128                                 {
129                                         const ComponentDescriptor *cp = (ComponentDescriptor*)*desc;
130                                         eComponentData data;
131                                         data.m_streamContent = cp->getStreamContent();
132                                         data.m_componentType = cp->getComponentType();
133                                         data.m_componentTag = cp->getComponentTag();
134                                         data.m_iso639LanguageCode = cp->getIso639LanguageCode();
135                                         int table=encodingHandler.getCountryCodeDefaultMapping(data.m_iso639LanguageCode);
136                                         data.m_text = convertDVBUTF8(cp->getText(),table,tsidonid);
137                                         m_component_data.push_back(data);
138                                         break;
139                                 }
140                                 case LINKAGE_DESCRIPTOR:
141                                 {
142                                         const LinkageDescriptor  *ld = (LinkageDescriptor*)*desc;
143                                         if ( ld->getLinkageType() == 0xB0 )
144                                         {
145                                                 eServiceReference ref;
146                                                 ref.type = eServiceReference::idDVB;
147                                                 eServiceReferenceDVB &dvb_ref = (eServiceReferenceDVB&) ref;
148                                                 dvb_ref.setServiceType(1);
149                                                 dvb_ref.setTransportStreamID(ld->getTransportStreamId());
150                                                 dvb_ref.setOriginalNetworkID(ld->getOriginalNetworkId());
151                                                 dvb_ref.setServiceID(ld->getServiceId());
152                                                 const PrivateDataByteVector *privateData = ld->getPrivateDataBytes();
153                                                 dvb_ref.name = convertDVBUTF8((const unsigned char*)&((*privateData)[0]), privateData->size(), 0, tsidonid);
154                                                 m_linkage_services.push_back(ref);
155                                         }
156                                         break;
157                                 }
158                         }
159                 }
160         }
161         if ( m_extended_description.find(m_short_description) == 0 )
162                 m_short_description="";
163         return retval;
164 }
165
166 RESULT eServiceEvent::parseFrom(Event *evt, int tsidonid)
167 {
168         uint16_t stime_mjd = evt->getStartTimeMjd();
169         uint32_t stime_bcd = evt->getStartTimeBcd();
170         uint32_t duration = evt->getDuration();
171         m_begin = parseDVBtime(
172                 stime_mjd >> 8,
173                 stime_mjd&0xFF,
174                 stime_bcd >> 16,
175                 (stime_bcd >> 8)&0xFF,
176                 stime_bcd & 0xFF
177         );
178         m_event_id = evt->getEventId();
179         m_duration = fromBCD(duration>>16)*3600+fromBCD(duration>>8)*60+fromBCD(duration);
180         for (int i=0; i < MAX_LANG; i++)
181                 if (m_language==ISOtbl[i][0])
182                         if (loadLanguage(evt, ISOtbl[i][1], tsidonid))
183                                 return 0;
184         if (loadLanguage(evt, "eng", tsidonid))
185                 return 0;
186         if (loadLanguage(evt, std::string(), tsidonid))
187                 return 0;
188         return 0;
189 }
190
191 RESULT eServiceEvent::parseFrom(const std::string filename, int tsidonid)
192 {
193         if (!filename.empty())
194         {
195                 int fd = ::open( filename.c_str(), O_RDONLY );
196                 if ( fd > -1 )
197                 {
198                         __u8 buf[4096];
199                         int rd = ::read(fd, buf, 4096);
200                         ::close(fd);
201                         if ( rd > 12 /*EIT_LOOP_SIZE*/ )
202                         {
203                                 Event ev(buf);
204                                 parseFrom(&ev, tsidonid);
205                                 return 0;
206                         }
207                 }
208         }
209         return -1;
210 }
211
212 std::string eServiceEvent::getBeginTimeString() const
213 {
214         tm t;
215         localtime_r(&m_begin, &t);
216         char tmp[13];
217         snprintf(tmp, 13, "%02d.%02d, %02d:%02d",
218                 t.tm_mday, t.tm_mon+1,
219                 t.tm_hour, t.tm_min);
220         return std::string(tmp, 12);
221 }
222
223 RESULT eServiceEvent::getComponentData(ePtr<eComponentData> &dest, int tagnum) const
224 {
225         std::list<eComponentData>::const_iterator it =
226                 m_component_data.begin();
227         for(;it != m_component_data.end(); ++it)
228         {
229                 if ( it->m_componentTag == tagnum )
230                 {
231                         dest=new eComponentData(*it);
232                         return 0;
233                 }
234         }
235         dest=0;
236         return -1;
237 }
238
239 RESULT eServiceEvent::getLinkageService(eServiceReference &service, eServiceReference &parent, int num) const
240 {
241         std::list<eServiceReference>::const_iterator it =
242                 m_linkage_services.begin();
243         while( it != m_linkage_services.end() && num-- )
244                 ++it;
245         if ( it != m_linkage_services.end() )
246         {
247                 service = *it;
248                 eServiceReferenceDVB &subservice = (eServiceReferenceDVB&) service;
249                 eServiceReferenceDVB &current = (eServiceReferenceDVB&) parent;
250                 subservice.setDVBNamespace(current.getDVBNamespace());
251                 if ( current.getParentTransportStreamID().get() )
252                 {
253                         subservice.setParentTransportStreamID( current.getParentTransportStreamID() );
254                         subservice.setParentServiceID( current.getParentServiceID() );
255                 }
256                 else
257                 {
258                         subservice.setParentTransportStreamID( current.getTransportStreamID() );
259                         subservice.setParentServiceID( current.getServiceID() );
260                 }
261                 if ( subservice.getParentTransportStreamID() == subservice.getTransportStreamID() &&
262                         subservice.getParentServiceID() == subservice.getServiceID() )
263                 {
264                         subservice.setParentTransportStreamID( eTransportStreamID(0) );
265                         subservice.setParentServiceID( eServiceID(0) );
266                 }
267                 return 0;
268         }
269         service.type = eServiceReference::idInvalid;
270         return -1;
271 }
272
273 DEFINE_REF(eDebugClass);