[VPS]Update German localization
[enigma2-plugins.git] / vps / src_py / Vps_check.py
1 # -*- coding: utf-8 -*-
2
3 from . import _
4 from enigma import eTimer, eConsoleAppContainer, getBestPlayableServiceReference, eServiceReference, eEPGCache
5 from Screens.Screen import Screen
6 from Components.ActionMap import ActionMap
7 from Components.Sources.StaticText import StaticText
8 from Screens.MessageBox import MessageBox
9 from Screens.ChoiceBox import ChoiceBox
10 from Tools.XMLTools import stringToXML
11 from Tools import Directories
12 from time import time
13 from Vps import vps_exe
14 import NavigationInstance
15 from xml.etree.cElementTree import parse as xml_parse
16
17 check_pdc_interval_available = 3600*24*30*8
18 check_pdc_interval_unavailable = 3600*24*30*2
19
20 class VPS_check_PDC:
21         def __init__(self):
22                 self.checked_services = { }
23                 self.load_pdc()
24         
25         def load_pdc(self):
26                 try:
27                         doc = xml_parse(Directories.resolveFilename(Directories.SCOPE_CONFIG, "vps.xml"))
28                         xmlroot = doc.getroot()
29                         
30                         if xmlroot is not None:
31                                 for xml in xmlroot.findall("channel"):
32                                         serviceref = xml.get("serviceref").encode("utf-8")
33                                         has_pdc = xml.get("has_pdc")
34                                         last_check = xml.get("last_check")
35                                         default_vps = xml.get("default_vps")
36                                         self.checked_services[serviceref] = { }
37                                         self.checked_services[serviceref]["last_check"] = int(last_check)
38                                         self.checked_services[serviceref]["has_pdc"] = int(has_pdc)
39                                         if default_vps and default_vps != "None":
40                                                 self.checked_services[serviceref]["default_vps"] = int(default_vps)
41                                         else:
42                                                 self.checked_services[serviceref]["default_vps"] = 0
43                 except:
44                         pass
45         
46         def save_pdc(self):
47                 list = []
48                 list.append('<?xml version="1.0" ?>\n')
49                 list.append('<pdc_available>\n')
50                 
51                 now = time()
52                 for ch in self.checked_services:
53                         if (self.checked_services[ch]["last_check"] < (now - check_pdc_interval_available)) and self.checked_services[ch]["default_vps"] != 1:
54                                 continue
55                         list.append('<channel')
56                         list.append(' serviceref="' + stringToXML(ch) + '"')
57                         list.append(' has_pdc="' + str(int(self.checked_services[ch]["has_pdc"])) + '"')
58                         list.append(' last_check="' + str(int(self.checked_services[ch]["last_check"])) + '"')
59                         list.append(' default_vps="' + str(int(self.checked_services[ch]["default_vps"])) + '"')
60                         list.append('></channel>\n')
61                 
62                 list.append('</pdc_available>\n')
63                 
64                 file = open(Directories.resolveFilename(Directories.SCOPE_CONFIG, "vps.xml"), "w")
65                 for x in list:
66                         file.write(x)
67                 file.close()
68         
69         def check_service(self, service):
70                 service_str = service.toCompareString()
71                 
72                 try:
73                         if self.checked_services[service_str] is not None:
74                                 return self.checked_services[service_str]["has_pdc"], self.checked_services[service_str]["last_check"], self.checked_services[service_str]["default_vps"]
75                         else:
76                                 return -1, 0, 0
77                 except:
78                         return -1, 0, 0
79         
80         def setServicePDC(self, service, state, default_vps):
81                 service_str = service.toCompareString()
82                 
83                 if state == -1 and default_vps == 0:
84                         try:
85                                 del self.checked_services[service_str]
86                         except:
87                                 pass
88                 else:
89                         self.checked_services[service_str] = { }
90                         self.checked_services[service_str]["has_pdc"] = state
91                         self.checked_services[service_str]["last_check"] = time()
92                         self.checked_services[service_str]["default_vps"] = default_vps
93                 
94                 self.save_pdc()
95                 
96         def recheck(self, has_pdc, last_check):
97                 return not ((has_pdc == 1 and last_check > (time() - check_pdc_interval_available)) or (has_pdc == 0 and last_check > (time() - check_pdc_interval_unavailable)))
98
99 Check_PDC = VPS_check_PDC()
100
101
102 # Prüfen, ob PDC-Descriptor vorhanden ist.
103 class VPS_check_PDC_Screen(Screen):
104         skin = """<screen name="vpsCheck" position="center,center" size="540,110" title="VPS-Plugin">
105                 <widget source="infotext" render="Label" position="10,10" size="520,90" font="Regular;21" valign="center" halign="center" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
106         </screen>"""
107         
108         def __init__(self, session, service, timer_entry, manual_timer = True):
109                 Screen.__init__(self, session)
110                 
111                 self["infotext"] = StaticText(_("VPS-Plugin checks if the channel supports VPS ..."))
112                 
113                 self["actions"] = ActionMap(["OkCancelActions"], 
114                         {
115                                 "cancel": self.finish,
116                         }, -1)
117                 
118                 if service is None or service.getPath():
119                         self.close()
120                         return
121
122                 self.service = service
123                 self.program = eConsoleAppContainer()
124                 self.program.dataAvail.append(self.program_dataAvail)
125                 self.program.appClosed.append(self.program_closed)
126                 self.check = eTimer()
127                 self.check.callback.append(self.doCheck)
128                 self.simulate_recordService = None
129                 self.last_serviceref = None
130                 self.calledfinished = False
131                 self.timer_entry = timer_entry
132                 self.manual_timer = manual_timer
133                 
134                 self.has_pdc, self.last_check, self.default_vps = Check_PDC.check_service(self.service)
135                 
136                 self.check.start(100, True)
137
138         
139         def doCheck(self):
140                 if not Check_PDC.recheck(self.has_pdc, self.last_check):
141                         self.finish()
142                         return
143                 
144                 self.demux = -1
145                 if self.simulate_recordService is None:
146                         self.simulate_recordService = NavigationInstance.instance.recordService(self.service, True)
147                         if self.simulate_recordService:
148                                 res = self.simulate_recordService.start()
149                                 if res != 0 and res != -1:
150                                         # Fehler aufgetreten (kein Tuner frei?)
151                                         NavigationInstance.instance.stopRecordService(self.simulate_recordService)
152                                         self.simulate_recordService = None
153                                         
154                                         if self.last_serviceref is not None:
155                                                 self.finish()
156                                                 return
157                                         else:
158                                                 cur_ref = NavigationInstance.instance.getCurrentlyPlayingServiceReference()
159                                                 if cur_ref and not cur_ref.getPath():
160                                                         self.last_serviceref = cur_ref
161                                                         NavigationInstance.instance.playService(None)
162                                                         self.check.start(1500, True)
163                                                         return
164                                 else: # hat geklappt
165                                         self.check.start(1000, True)
166                                         return
167                 else:
168                         stream = self.simulate_recordService.stream()
169                         if stream:
170                                 streamdata = stream.getStreamingData()
171                                 if (streamdata and ('demux' in streamdata)):
172                                         self.demux = streamdata['demux']
173                         if self.demux != -1:
174                                 self.startProgram()
175                                 return
176                 
177                 if self.simulate_recordService is not None:
178                         NavigationInstance.instance.stopRecordService(self.simulate_recordService)
179                         self.simulate_recordService = None
180                 if self.last_serviceref is not None:
181                         NavigationInstance.instance.playService(self.last_serviceref)
182                 self.finish()
183         
184         def startProgram(self):
185                 sid = self.service.getData(1)
186                 tsid = self.service.getData(2)
187                 onid = self.service.getData(3)
188                 demux = "/dev/dvb/adapter0/demux" + str(self.demux)
189                 
190                 cmd = vps_exe + " "+ demux +" 10 "+ str(onid) +" "+ str(tsid) +" "+ str(sid) +" 0 0"
191                 self.program.execute(cmd)
192         
193         def program_closed(self, retval):
194                 if not self.calledfinished:
195                         self.setServicePDC(-1)
196                         self.finish()
197         
198         def program_dataAvail(self, str):
199                 lines = str.split("\n")
200                 for line in lines:
201                         if line == "PDC_AVAILABLE" and not self.calledfinished:
202                                 self.calledfinished = True
203                                 self.setServicePDC(1)
204                                 self.finish()
205                                 
206                         elif line == "NO_PDC_AVAILABLE" and not self.calledfinished:
207                                 self.calledfinished = True
208                                 self.setServicePDC(0)
209                                 self.finish()
210         
211         def setServicePDC(self, state):
212                 Check_PDC.setServicePDC(self.service, state, self.default_vps)
213                 self.has_pdc = state
214                 
215         def finish(self):
216                 self.calledfinished = True
217                 self.check.stop()
218                 
219                 if self.simulate_recordService is not None:
220                         NavigationInstance.instance.stopRecordService(self.simulate_recordService)
221                         self.simulate_recordService = None
222                 
223                 if self.last_serviceref is not None:
224                         NavigationInstance.instance.playService(self.last_serviceref)
225                 
226                 if self.manual_timer:
227                         if self.has_pdc == 1: # PDC vorhanden
228                                 self.close()
229                         elif self.has_pdc == 0: # kein PDC
230                                 #nachfragen
231                                 self.session.openWithCallback(self.finish_callback, MessageBox, _("The selected channel doesn't support VPS for manually programmed timers!\n Do you really want to enable VPS?"), default = False)
232                         else: # konnte nicht ermitteln
233                                 self.session.openWithCallback(self.finish_callback, MessageBox, _("The VPS-Plugin couldn't check if the selected channel supports VPS for manually programmed timers!\n Do you really want to enable VPS?"), default = False)
234                 else:
235                         if self.has_pdc == 1: # PDC vorhanden
236                                 self.close()
237                         else:
238                                 choiceList = [(_("No"), 0), (_("Yes"), 1), (_("Yes, don't ask again"), 2)]
239                                 self.session.openWithCallback(self.finish_callback2, ChoiceBox, title = _("VPS-Plugin couldn't check if the selected channel supports VPS.\n Do you really want to enable VPS?"), list = choiceList)
240         
241         def finish_callback(self, result):
242                 if not result:
243                         self.timer_entry.timerentry_vpsplugin_enabled.value = False
244                         self.timer_entry.createSetup("config")
245                         #self.timer_entry["config"].setCurrentIndex(self.timer_entry["config"].getCurrentIndex() + 1)
246                 
247                 self.close()
248         
249         def finish_callback2(self, result):
250                 if result is None or result[1] == 0:
251                         self.finish_callback(False)
252                         return
253                 
254                 elif result[1] == 2:
255                         Check_PDC.setServicePDC(self.service, self.has_pdc, 1) # nicht mehr nachfragen
256                 
257                 self.close()