[FritzCall] Major UPD: make plugin Python2 and Python3 compatible (#30)
[enigma2-plugins.git] / webinterface / src / WebComponents / Sources / AudioTracks.py
1 from Components.config import config
2 from Components.Sources.Source import Source
3 from Components.SystemInfo import SystemInfo
4 from Tools.ISO639 import LanguageCodes
5
6 class AudioTracks(Source):
7         GET = 0
8         SET = 1
9         DOWNMIX = 2
10
11         text = _("False")
12
13         def __init__(self, session, func=GET):
14                 self.cmd = None
15                 self.session = session
16                 self.func = func
17                 Source.__init__(self)
18
19         def handleCommand(self, cmd):
20                 self.cmd = cmd
21
22         def getResult(self):
23                 return self.handleDownmix()
24
25         def setAudioTrack(self):
26                 if self.cmd is not None:
27                         service = self.session.nav.getCurrentService()
28                         audio = service and service.audioTracks()
29                         try:
30                                 cmd = int(self.cmd)
31                         except ValueError:
32                                 cmd = -1
33
34                         print "COMMAND is %s" % self.cmd
35                         if self.session.nav.getCurrentService().audioTracks().getNumberOfTracks() > cmd and cmd >= 0:
36                                 audio.selectTrack(cmd)
37                                 return _("Success")
38                         else:
39                                 return _("Error")
40                 else:
41                         return _("Error")
42
43         def handleDownmix(self):
44                 if SystemInfo["CanDownmixAC3"]:
45                         if self.cmd == "True":
46                                 config.av.downmix_ac3.value = True
47                         elif self.cmd == "False":
48                                 config.av.downmix_ac3.value = False
49
50                         text = _("AC3 Downmix enabled") if config.av.downmix_ac3.value else _("AC3 Downmix disabled")
51                         return config.av.downmix_ac3.value, text
52
53                 return False, _("This device does not support AC3 Downmix")
54
55         def getAudioTracks(self):
56                 service = self.session.nav.getCurrentService()
57                 audio = service and service.audioTracks()
58                 n = audio and audio.getNumberOfTracks() or 0
59
60                 tracklist = []
61
62                 #check for standby
63                 if audio is not None and service is not None:
64                         currentTrack = audio.getCurrentTrack()
65
66                         if n > 0:
67                                 print "[AudioTracks.py] got %s Audiotracks!" % (n)
68
69                                 x = 0
70                                 while x < n:
71                                         cnt = 0
72                                         i = audio.getTrackInfo(x)
73
74                                         languages = i.getLanguage().split('/')
75                                         description = i.getDescription()
76                                         pid = i.getPID()
77                                         language = ''
78
79                                         for lang in languages:
80                                                 if cnt:
81                                                         language += ' / '
82                                                 if LanguageCodes.has_key(lang):
83                                                         language += LanguageCodes[lang][0]
84                                                 else:
85                                                         language += lang
86                                                 cnt += 1
87
88                                         if description:
89                                                 description += " (" + language + ")"
90                                         else:
91                                                 description = language
92
93                                         if x == currentTrack:
94                                                 active = "True"
95                                         else:
96                                                 active = "False"
97
98                                         tracklist.append((description, x, pid, active))
99                                         x += 1
100
101                 return tracklist
102
103         result = property(getResult)
104         text = property(setAudioTrack)
105         list = property(getAudioTracks)
106         lut = {"Description": 0, "Id": 1, "Pid": 2, "Active": 3}