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