Merge branch 'bug_539_add_multicontent_pixmap_progress'
[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         instance = None
12         """Volume control, handles volUp, volDown, volMute actions and display
13         a corresponding dialog"""
14         def __init__(self, session):
15                 global globalActionMap
16                 globalActionMap.actions["volumeUp"]=self.volUp
17                 globalActionMap.actions["volumeDown"]=self.volDown
18                 globalActionMap.actions["volumeMute"]=self.volMute
19
20                 assert not VolumeControl.instance, "only one VolumeControl instance is allowed!"
21                 VolumeControl.instance = self
22
23                 config.audio = ConfigSubsection()
24                 config.audio.volume = ConfigInteger(default = 100, limits = (0, 100))
25
26                 self.volumeDialog = session.instantiateDialog(Volume)
27                 self.muteDialog = session.instantiateDialog(Mute)
28
29                 self.hideVolTimer = eTimer()
30                 self.hideVolTimer.callback.append(self.volHide)
31
32                 vol = config.audio.volume.value
33                 self.volumeDialog.setValue(vol)
34                 self.volctrl = eDVBVolumecontrol.getInstance()
35                 self.volctrl.setVolume(vol, vol)
36
37         def volSave(self):
38                 if self.volctrl.isMuted():
39                         config.audio.volume.value = 0
40                 else:
41                         config.audio.volume.value = self.volctrl.getVolume()
42                 config.audio.volume.save()
43
44         def volUp(self):
45                 self.setVolume(+1)
46
47         def volDown(self):
48                 self.setVolume(-1)
49
50         def setVolume(self, direction):
51                 oldvol = self.volctrl.getVolume()
52                 if direction > 0:
53                         self.volctrl.volumeUp()
54                 else:
55                         self.volctrl.volumeDown()
56                 is_muted = self.volctrl.isMuted()
57                 vol = self.volctrl.getVolume()
58                 self.volumeDialog.show()
59                 if is_muted:
60                         self.volMute() # unmute
61                 elif not vol:
62                         self.volMute(False, True) # mute but dont show mute symbol
63                 if self.volctrl.isMuted():
64                         self.volumeDialog.setValue(0)
65                 else:
66                         self.volumeDialog.setValue(self.volctrl.getVolume())
67                 self.volSave()
68                 self.hideVolTimer.start(3000, True)
69
70         def volHide(self):
71                 self.volumeDialog.hide()
72
73         def volMute(self, showMuteSymbol=True, force=False):
74                 vol = self.volctrl.getVolume()
75                 if vol or force:
76                         self.volctrl.volumeToggleMute()
77                         if self.volctrl.isMuted():
78                                 if showMuteSymbol:
79                                         self.muteDialog.show()
80                                 self.volumeDialog.setValue(0)
81                         else:
82                                 self.muteDialog.hide()
83                                 self.volumeDialog.setValue(vol)