timer: fix displayed state. Don't save instant records. properly remove timerentries.
[enigma2.git] / lib / python / Screens / InfoBarGenerics.py
1 from Screen import Screen
2 from Components.ActionMap import ActionMap
3 from Components.ActionMap import NumberActionMap
4 from Components.Label import Label
5 from Components.config import configfile, configsequencearg
6 from Components.config import config, configElement, ConfigSubsection, configSequence
7 from ChannelSelection import ChannelSelection
8
9 from Components.ServiceName import ServiceName
10 from Components.EventInfo import EventInfo
11
12 from EpgSelection import EPGSelection
13
14 from Screens.MessageBox import MessageBox
15 from Screens.Volume import Volume
16 from Screens.Mute import Mute
17 from Screens.Standby import Standby
18
19 #from enigma import eTimer, eDVBVolumecontrol, quitMainloop
20 from enigma import *
21
22 import time
23 import os
24
25 # hack alert!
26 from Menu import MainMenu, mdom
27
28 class InfoBarVolumeControl:
29         """Volume control, handles volUp, volDown, volMute actions and display 
30         a corresponding dialog"""
31         def __init__(self):
32                 config.audio = ConfigSubsection()
33                 config.audio.volume = configElement("config.audio.volume", configSequence, [5], configsequencearg.get("INTEGER", (0, 100)))
34
35                 self["VolumeActions"] = ActionMap( ["InfobarVolumeActions"] ,
36                         {
37                                 "volumeUp": self.volUp,
38                                 "volumeDown": self.volDown,
39                                 "volumeMute": self.volMute,
40                         })
41
42                 self.volumeDialog = self.session.instantiateDialog(Volume)
43                 self.muteDialog = self.session.instantiateDialog(Mute)
44
45                 self.hideVolTimer = eTimer()
46                 self.hideVolTimer.timeout.get().append(self.volHide)
47
48                 vol = config.audio.volume.value[0]
49                 self.volumeDialog.setValue(vol)
50                 eDVBVolumecontrol.getInstance().setVolume(vol, vol)
51         
52         def volSave(self):
53                 config.audio.volume.value = eDVBVolumecontrol.getInstance().getVolume()
54                 config.audio.volume.save()
55                 
56         def     volUp(self):
57                 eDVBVolumecontrol.getInstance().volumeUp()
58                 self.volumeDialog.instance.show()
59                 self.volumeDialog.setValue(eDVBVolumecontrol.getInstance().getVolume())
60                 self.volSave()
61                 self.hideVolTimer.start(3000)
62
63         def     volDown(self):
64                 eDVBVolumecontrol.getInstance().volumeDown()
65                 self.volumeDialog.instance.show()
66                 self.volumeDialog.setValue(eDVBVolumecontrol.getInstance().getVolume())
67                 self.volSave()
68                 self.hideVolTimer.start(3000)
69                 
70         def volHide(self):
71                 self.volumeDialog.instance.hide()
72
73         def     volMute(self):
74                 eDVBVolumecontrol.getInstance().volumeToggleMute()
75                 self.volumeDialog.setValue(eDVBVolumecontrol.getInstance().getVolume())
76                 
77                 if (eDVBVolumecontrol.getInstance().isMuted()):
78                         self.muteDialog.instance.show()
79                 else:
80                         self.muteDialog.instance.hide()
81
82 class InfoBarShowHide:
83         """ InfoBar show/hide control, accepts toggleShow and hide actions, might start
84         fancy animations. """
85         STATE_HIDDEN = 0
86         STATE_HIDING = 1
87         STATE_SHOWING = 2
88         STATE_SHOWN = 3
89         
90         def __init__(self):
91                 self["ShowHideActions"] = ActionMap( ["InfobarShowHideActions"] ,
92                         {
93                                 "toggleShow": self.toggleShow,
94                                 "hide": self.hide,
95                         })
96
97                 self.state = self.STATE_HIDDEN
98                 
99                 self.hideTimer = eTimer()
100                 self.hideTimer.timeout.get().append(self.doTimerHide)
101                 #self.hideTimer.start(1000)
102
103         def hide(self): 
104                 self.instance.hide()
105
106         def doTimerHide(self):
107                 if self.state == self.STATE_SHOWN:
108                         self.instance.hide()
109                         self.state = self.STATE_HIDDEN
110
111         def toggleShow(self):
112                 if self.state == self.STATE_SHOWN:
113                         self.instance.hide()
114                         #pls check animation support, sorry
115 #                       self.startHide()
116                         self.state = self.STATE_HIDDEN
117                 else:
118                         self.instance.show()
119 #                       self.startShow()
120                         self.state = self.STATE_SHOWN
121                         #TODO: make it customizable
122                         self.hideTimer.start(5000)
123
124         def startShow(self):
125                 self.instance.m_animation.startMoveAnimation(ePoint(0, 600), ePoint(0, 380), 100)
126                 self.state = self.STATE_SHOWN
127         
128         def startHide(self):
129                 self.instance.m_animation.startMoveAnimation(ePoint(0, 380), ePoint(0, 600), 100)
130                 self.state = self.STATE_HIDDEN
131
132 class NumberZap(Screen):
133         def quit(self):
134                 self.Timer.stop()
135                 self.close()
136
137         def keyOK(self):
138                 self.Timer.stop()
139                 print "do the action here"
140                 self.close()
141
142         def keyNumberGlobal(self, number):
143                 self.Timer.start(3000)          #reset timer
144                 self.field = self.field + str(number)
145                 self["number"].setText(self.field)
146                 if len(self.field) >= 4:
147                         self.keyOK()
148
149         def __init__(self, session, number):
150                 Screen.__init__(self, session)
151                 self.field = str(number)
152                 
153                 self["number"] = Label(self.field)
154
155                 self["actions"] = NumberActionMap( [ "SetupActions" ], 
156                         {
157                                 "cancel": self.quit,
158                                 "ok": self.keyOK,
159                                 "1": self.keyNumberGlobal,
160                                 "2": self.keyNumberGlobal,
161                                 "3": self.keyNumberGlobal,
162                                 "4": self.keyNumberGlobal,
163                                 "5": self.keyNumberGlobal,
164                                 "6": self.keyNumberGlobal,
165                                 "7": self.keyNumberGlobal,
166                                 "8": self.keyNumberGlobal,
167                                 "9": self.keyNumberGlobal,
168                                 "0": self.keyNumberGlobal
169                         })
170
171                 self.Timer = eTimer()
172                 self.Timer.timeout.get().append(self.keyOK)
173                 self.Timer.start(3000)
174
175 class InfoBarPowerKey:
176         """ PowerKey stuff - handles the powerkey press and powerkey release actions"""
177         
178         def __init__(self):
179                 self.powerKeyTimer = eTimer()
180                 self.powerKeyTimer.timeout.get().append(self.powertimer)
181                 self["PowerKeyActions"] = ActionMap( ["PowerKeyActions"],
182                         {
183                                 "powerdown": self.powerdown,
184                                 "powerup": self.powerup,
185                         })
186
187         def powertimer(self):   
188                 print "PowerOff - Now!"
189                 self.quit()
190         
191         def powerdown(self):
192                 self.standbyblocked = 0
193                 self.powerKeyTimer.start(3000)
194
195         def powerup(self):
196                 self.powerKeyTimer.stop()
197                 if self.standbyblocked == 0:
198                         self.standbyblocked = 1
199                         self.session.open(Standby, self)
200
201         def quit(self):
202                 quitMainloop(0)
203
204 class InfoBarNumberZap:
205         """ Handles an initial number for NumberZapping """
206         def __init__(self):
207                 self["NumberZapActions"] = NumberActionMap( [ "NumberZapActions"],
208                         {
209                                 "1": self.keyNumberGlobal,
210                                 "2": self.keyNumberGlobal,
211                                 "3": self.keyNumberGlobal,
212                                 "4": self.keyNumberGlobal,
213                                 "5": self.keyNumberGlobal,
214                                 "6": self.keyNumberGlobal,
215                                 "7": self.keyNumberGlobal,
216                                 "8": self.keyNumberGlobal,
217                                 "9": self.keyNumberGlobal,
218                                 "0": self.keyNumberGlobal,
219                         })
220
221         def keyNumberGlobal(self, number):
222                 print "You pressed number " + str(number)
223                 self.session.open(NumberZap, number)
224
225 class InfoBarChannelSelection:
226         """ ChannelSelection - handles the channelSelection dialog and the initial 
227         channelChange actions which open the channelSelection dialog """
228         def __init__(self):
229                 #instantiate forever
230                 self.servicelist = self.session.instantiateDialog(ChannelSelection)
231
232                 self["ChannelSelectActions"] = ActionMap( ["InfobarChannelSelection"],
233                         {
234                                 "switchChannelUp": self.switchChannelUp,
235                                 "switchChannelDown": self.switchChannelDown,
236                                 "zapUp": self.zapUp,
237                                 "zapDown": self.zapDown,
238                         })
239                         
240         def switchChannelUp(self):      
241                 self.servicelist.moveUp()
242                 self.session.execDialog(self.servicelist)
243
244         def switchChannelDown(self):    
245                 self.servicelist.moveDown()
246                 self.session.execDialog(self.servicelist)
247
248         def     zapUp(self):
249                 self.servicelist.moveUp()
250                 self.servicelist.zap()
251
252         def     zapDown(self):
253                 self.servicelist.moveDown()
254                 self.servicelist.zap()
255
256 class InfoBarMenu:
257         """ Handles a menu action, to open the (main) menu """
258         def __init__(self):
259                 self["MenuActions"] = ActionMap( [ "InfobarMenuActions" ], 
260                         {
261                                 "mainMenu": self.mainMenu,
262                         })
263
264         def mainMenu(self):
265                 print "loading mainmenu XML..."
266                 menu = mdom.childNodes[0]
267                 assert menu.tagName == "menu", "root element in menu must be 'menu'!"
268                 self.session.open(MainMenu, menu, menu.childNodes)
269
270 class InfoBarEPG:
271         """ EPG - Opens an EPG list when the showEPGList action fires """
272         def __init__(self):
273                 self["EPGActions"] = ActionMap( [ "InfobarEPGActions" ], 
274                         {
275                                 "showEPGList": self.showEPGList,
276                         })
277
278         def showEPGList(self):
279                 ref=self.session.nav.getCurrentlyPlayingServiceReference()
280                 ptr=eEPGCache.getInstance()
281                 if ptr.startTimeQuery(ref) != -1:
282                         self.session.open(EPGSelection, ref)
283                 else:
284                         print 'no epg for service', ref.toString()
285
286 class InfoBarEvent:     
287         """provides a current/next event info display"""
288         def __init__(self):
289                 self["Event_Now_StartTime"] = EventInfo(self.session.nav, EventInfo.Now_StartTime)
290                 self["Event_Next_StartTime"] = EventInfo(self.session.nav, EventInfo.Next_StartTime)
291                                 
292                 self["Event_Now"] = EventInfo(self.session.nav, EventInfo.Now)
293                 self["Event_Next"] = EventInfo(self.session.nav, EventInfo.Next)
294
295                 self["Event_Now_Duration"] = EventInfo(self.session.nav, EventInfo.Now_Duration)
296                 self["Event_Next_Duration"] = EventInfo(self.session.nav, EventInfo.Next_Duration)
297
298 class InfoBarServiceName:
299         def __init__(self):
300                 self["ServiceName"] = ServiceName(self.session.nav)
301
302 class InfoBarPVR:
303         """handles PVR specific actions like seeking, pause"""
304         def __init__(self):
305                 self["PVRActions"] = ActionMap( [ "InfobarPVRActions" ], 
306                         {
307                                 "pauseService": self.pauseService,
308                                 "unPauseService": self.unPauseService,
309                                 
310                                 "seekFwd": self.seekFwd,
311                                 "seekBack": self.seekBack,
312                         })
313                 
314         def pauseService(self):
315                 self.session.nav.pause(1)
316                 
317         def unPauseService(self):
318                 self.session.nav.pause(0)
319         
320         def doSeek(self, dir, seektime):
321                 service = self.session.nav.getCurrentService()
322                 if service is None:
323                         return
324                 
325                 seekable = service.seek()
326                 if seekable is None:
327                         return
328                 seekable.seekRelative(dir, 90 * seektime)
329
330         def seekFwd(self):
331                 self.doSeek(+1, 60000)
332         
333         def seekBack(self):
334                 self.doSeek(-1, 60000)
335
336 class InfoBarInstantRecord:
337         """Instant Record - handles the instantRecord action in order to 
338         start/stop instant records"""
339         def __init__(self):
340                 self["InstnantRecordActions"] = ActionMap( [ "InfobarInstantRecord" ],
341                         {
342                                 "instantRecord": self.instantRecord,
343                         })
344                 self.recording = None
345
346         def stopCurrentRecording(self): 
347                 self.session.nav.RecordTimer.removeEntry(self.recording)
348                 self.recording = None
349         
350         def startInstantRecording(self):
351                 serviceref = self.session.nav.getCurrentlyPlayingServiceReference()
352                         
353                 # try to get event info
354                 epg = None
355                 try:
356                         service = self.session.nav.getCurrentService()
357                         info = service.info()
358                         ev = info.getEvent(0)
359                         epg = ev
360                 except:
361                         pass
362                 
363                 # fix me, description. 
364                 self.recording = self.session.nav.recordWithTimer(time.time(), time.time() + 3600, serviceref, epg, "instant record")
365                 self.recording.dontSave = True
366
367         def recordQuestionCallback(self, answer):
368                 if answer == False:
369                         return
370                 
371                 if self.recording != None:
372                         self.stopCurrentRecording()
373                 else:
374                         self.startInstantRecording()
375
376         def instantRecord(self):
377                 try:
378                         stat = os.stat("/hdd/movies")
379                 except:
380                         self.session.open(MessageBox, "No HDD found!")
381                         return
382         
383                 if self.recording != None:
384                         self.session.openWithCallback(self.recordQuestionCallback, MessageBox, "Do you want to stop the current\n(instant) recording?")
385                 else:
386                         self.session.openWithCallback(self.recordQuestionCallback, MessageBox, "Start recording?")
387
388 from Screens.AudioSelection import AudioSelection
389
390 class InfoBarAudioSelection:
391         def __init__(self):
392                 self["AudioSelectionAction"] = ActionMap( [ "InfobarAudioSelectionActions" ], 
393                         {
394                                 "audioSelection": self.audioSelection,
395                         })
396
397         def audioSelection(self):
398                 service = self.session.nav.getCurrentService()
399                 audio = service.audioTracks()
400                 n = audio.getNumberOfTracks()
401                 if n > 0:
402                         self.session.open(AudioSelection, audio)