enigma2 20130801 (master) -> 20130815 (master)
[enigma2.git] / usr / lib / enigma2 / 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.stopService()
61                 self.session.nav.playService(self.restoreService, False)
62
63         def onLayoutFinished(self):
64                 self.timer.start(0,True)
65
66         def updateSubservices(self):
67                 self.service = self.session.nav.getCurrentService()
68                 self.subservices = self.service and self.service.subServices()
69                 self.n = self.subservices and self.subservices.getNumberOfSubservices()
70         
71         def nextSubservice(self):
72                 self.updateSubservices()
73                 if self.n:
74                         if self.currentlyPlayingSubservice >= self.n - 1:
75                                 self.playSubservice(0)
76                         else:
77                                 self.playSubservice(self.currentlyPlayingSubservice + 1)
78         
79         def previousSubservice(self):
80                 self.updateSubservices()
81                 if self.n:
82                         if self.currentlyPlayingSubservice > self.n:
83                                 self.currentlyPlayingSubservice = self.n
84                         if self.currentlyPlayingSubservice == 0:
85                                 self.playSubservice(self.n - 1)
86                         else:
87                                 self.playSubservice(self.currentlyPlayingSubservice - 1)
88
89         def getSubserviceIndex(self, service):
90                 self.updateSubservices()
91                 if self.n is None:
92                         return -1
93                 for x in range(self.n):
94                         if service == self.subservices.getSubservice(x):
95                                 return x
96         
97         def keyNumberGlobal(self, number):
98                 print number, "pressed"
99                 self.updateSubservices()
100                 if number == 0:
101                         self.playSubservice(self.lastservice)
102                 elif self.n is not None and number <= self.n - 1:
103                         self.playSubservice(number)
104         
105         def showSelection(self):
106                 self.updateSubservices()
107                 tlist = []
108                 n = self.n or 0
109                 if n:
110                         idx = 0
111                         while idx < n:
112                                 i = self.subservices.getSubservice(idx)
113                                 tlist.append((i.getName(), idx))
114                                 idx += 1
115
116                 keys = [ "", "1", "2", "3", "4", "5", "6", "7", "8", "9" ] + [""] * n
117                 self.session.openWithCallback(self.subserviceSelected, ChoiceBox, title=_("Please select a subservice..."), list = tlist, selection = self.currentlyPlayingSubservice, keys = keys)
118         
119         def subserviceSelected(self, service):
120                 print "playing subservice number", service
121                 if service is not None:
122                         self.playSubservice(service[1])
123         
124         def keyOK(self):
125                 pass
126         
127         def quitQuestion(self):
128                 self.session.openWithCallback(self.quit, MessageBox, _("Really exit the subservices quickzap?"))
129         
130         def quit(self, answer):
131                 if answer:
132                         self.close()
133                 
134         def playSubservice(self, number = 0):
135                 newservice = self.subservices.getSubservice(number)
136                 if newservice.valid():
137                         del self.subservices
138                         del self.service
139                         self.lastservice = self.currentlyPlayingSubservice
140                         self.session.nav.stopService()
141                         self.session.nav.playService(newservice, False)
142                         self.currentlyPlayingSubservice = number
143                         self.currentSubserviceNumberLabel.setText(str(number))
144                         self.doShow()