fixes bug #593
[enigma2.git] / lib / python / Screens / SubservicesQuickzap.py
1 from Screens.Screen import Screen
2 from Components.ActionMap import NumberActionMap
3 from Components.Label import Label
4
5 from Screens.ChoiceBox import ChoiceBox
6 from Screens.MessageBox import MessageBox
7 from InfoBarGenerics import InfoBarShowHide, InfoBarMenu, InfoBarInstantRecord, InfoBarTimeshift, InfoBarSeek, InfoBarTimeshiftState, InfoBarExtensions, InfoBarSubtitleSupport, InfoBarAudioSelection
8 from Components.ServiceEventTracker import InfoBarBase
9
10 from enigma import eTimer
11
12 class SubservicesQuickzap(InfoBarBase, InfoBarShowHide, InfoBarMenu, \
13                 InfoBarInstantRecord, InfoBarSeek, InfoBarTimeshift, \
14                 InfoBarTimeshiftState, InfoBarExtensions, InfoBarSubtitleSupport, \
15                 InfoBarAudioSelection, Screen):
16
17         def __init__(self, session, subservices):
18                 Screen.__init__(self, session)
19                 for x in InfoBarBase, InfoBarShowHide, InfoBarMenu, \
20                                 InfoBarInstantRecord, InfoBarSeek, InfoBarTimeshift, \
21                                 InfoBarTimeshiftState, InfoBarSubtitleSupport, \
22                                 InfoBarExtensions, InfoBarAudioSelection:
23                         x.__init__(self)
24
25                 self.restoreService = self.session.nav.getCurrentlyPlayingServiceReference()
26                 
27                 self["CurrentSubserviceNumber"] = Label("")
28                 self.currentSubserviceNumberLabel = self["CurrentSubserviceNumber"]
29                 
30                 self.updateSubservices()
31                 self.currentlyPlayingSubservice = 0
32
33                 self.timer = eTimer()
34                 self.timer.callback.append(self.playSubservice)
35                 self.onLayoutFinish.append(self.onLayoutFinished)
36
37                 self["actions"] = NumberActionMap( [ "InfobarSubserviceQuickzapActions", "NumberActions", "DirectionActions", "ColorActions" ], 
38                         {
39                                 "up": self.showSelection,
40                                 "down": self.showSelection,
41                                 "right": self.nextSubservice,
42                                 "left": self.previousSubservice,
43                                 "green": self.showSelection,
44                                 "exit": self.quitQuestion,
45                                 "1": self.keyNumberGlobal,
46                                 "2": self.keyNumberGlobal,
47                                 "3": self.keyNumberGlobal,
48                                 "4": self.keyNumberGlobal,
49                                 "5": self.keyNumberGlobal,
50                                 "6": self.keyNumberGlobal,
51                                 "7": self.keyNumberGlobal,
52                                 "8": self.keyNumberGlobal,
53                                 "9": self.keyNumberGlobal,
54                                 "0": self.keyNumberGlobal
55                         }, 0)
56                 
57                 self.onClose.append(self.__onClose)
58
59         def __onClose(self):
60                 self.session.nav.playService(self.restoreService, False)
61
62         def onLayoutFinished(self):
63                 self.timer.start(0,True)
64
65         def updateSubservices(self):
66                 self.service = self.session.nav.getCurrentService()
67                 self.subservices = self.service and self.service.subServices()
68                 self.n = self.subservices and self.subservices.getNumberOfSubservices()
69         
70         def nextSubservice(self):
71                 self.updateSubservices()
72                 if self.n:
73                         if self.currentlyPlayingSubservice >= self.n - 1:
74                                 self.playSubservice(0)
75                         else:
76                                 self.playSubservice(self.currentlyPlayingSubservice + 1)
77         
78         def previousSubservice(self):
79                 self.updateSubservices()
80                 if self.n:
81                         if self.currentlyPlayingSubservice > self.n:
82                                 self.currentlyPlayingSubservice = self.n
83                         if self.currentlyPlayingSubservice == 0:
84                                 self.playSubservice(self.n - 1)
85                         else:
86                                 self.playSubservice(self.currentlyPlayingSubservice - 1)
87
88         def getSubserviceIndex(self, service):
89                 self.updateSubservices()
90                 if self.n is None:
91                         return -1
92                 for x in range(self.n):
93                         if service == self.subservices.getSubservice(x):
94                                 return x
95         
96         def keyNumberGlobal(self, number):
97                 print number, "pressed"
98                 self.updateSubservices()
99                 if number == 0:
100                         self.playSubservice(self.lastservice)
101                 elif self.n is not None and number <= self.n - 1:
102                         self.playSubservice(number)
103         
104         def showSelection(self):
105                 self.updateSubservices()
106                 tlist = []
107                 n = self.n or 0
108                 if n:
109                         idx = 0
110                         while idx < n:
111                                 i = self.subservices.getSubservice(idx)
112                                 tlist.append((i.getName(), idx))
113                                 idx += 1
114
115                 keys = [ "", "1", "2", "3", "4", "5", "6", "7", "8", "9" ] + [""] * n
116                 self.session.openWithCallback(self.subserviceSelected, ChoiceBox, title=_("Please select a subservice..."), list = tlist, selection = self.currentlyPlayingSubservice, keys = keys)
117         
118         def subserviceSelected(self, service):
119                 print "playing subservice number", service
120                 if service is not None:
121                         self.playSubservice(service[1])
122         
123         def keyOK(self):
124                 pass
125         
126         def quitQuestion(self):
127                 self.session.openWithCallback(self.quit, MessageBox, _("Really exit the subservices quickzap?"))
128         
129         def quit(self, answer):
130                 if answer:
131                         self.close()
132                 
133         def playSubservice(self, number = 0):
134                 newservice = self.subservices.getSubservice(number)
135                 if newservice.valid():
136                         del self.subservices
137                         del self.service
138                         self.lastservice = self.currentlyPlayingSubservice
139                         self.session.nav.playService(newservice, False)
140                         self.currentlyPlayingSubservice = number
141                         self.currentSubserviceNumberLabel.setText(str(number))
142                         self.doShow()