bug #238
[enigma2.git] / lib / python / Components / VolumeControl.py
1 from enigma import eDVBVolumecontrol, eTimer
2 from Tools.Profile import profile
3 from Screens.Volume import Volume
4 from Screens.Mute import Mute
5 from GlobalActions import globalActionMap
6 from config import config, ConfigSubsection, ConfigInteger
7
8 profile("VolumeControl")
9 #TODO .. move this to a own .py file
10 class VolumeControl:
11         """Volume control, handles volUp, volDown, volMute actions and display
12         a corresponding dialog"""
13         def __init__(self, session):
14                 global globalActionMap
15                 globalActionMap.actions["volumeUp"]=self.volUp
16                 globalActionMap.actions["volumeDown"]=self.volDown
17                 globalActionMap.actions["volumeMute"]=self.volMute
18
19                 config.audio = ConfigSubsection()
20                 config.audio.volume = ConfigInteger(default = 100, limits = (0, 100))
21
22                 self.volumeDialog = session.instantiateDialog(Volume)
23                 self.muteDialog = session.instantiateDialog(Mute)
24
25                 self.hideVolTimer = eTimer()
26                 self.hideVolTimer.callback.append(self.volHide)
27
28                 vol = config.audio.volume.value
29                 self.volumeDialog.setValue(vol)
30                 self.volctrl = eDVBVolumecontrol.getInstance()
31                 self.volctrl.setVolume(vol, vol)
32
33         def volSave(self):
34                 if self.volctrl.isMuted():
35                         config.audio.volume.value = 0
36                 else:
37                         config.audio.volume.value = self.volctrl.getVolume()
38                 config.audio.volume.save()
39
40         def volUp(self):
41                 self.setVolume(+1)
42
43         def volDown(self):
44                 self.setVolume(-1)
45
46         def setVolume(self, direction):
47                 oldvol = self.volctrl.getVolume()
48                 if direction > 0:
49                         self.volctrl.volumeUp()
50                 else:
51                         self.volctrl.volumeDown()
52                 is_muted = self.volctrl.isMuted()
53                 vol = self.volctrl.getVolume()
54                 self.volumeDialog.show()
55                 if is_muted:
56                         self.volMute() # unmute
57                 elif not vol:
58                         self.volMute(False, True) # mute but dont show mute symbol
59                 if self.volctrl.isMuted():
60                         self.volumeDialog.setValue(0)
61                 else:
62                         self.volumeDialog.setValue(self.volctrl.getVolume())
63                 self.volSave()
64                 self.hideVolTimer.start(3000, True)
65
66         def volHide(self):
67                 self.volumeDialog.hide()
68
69         def volMute(self, showMuteSymbol=True, force=False):
70                 vol = self.volctrl.getVolume()
71                 if vol or force:
72                         self.volctrl.volumeToggleMute()
73                         if self.volctrl.isMuted():
74                                 if showMuteSymbol:
75                                         self.muteDialog.show()
76                                 self.volumeDialog.setValue(0)
77                         else:
78                                 self.muteDialog.hide()
79                                 self.volumeDialog.setValue(vol)