fix not visible sleeptimer in extensions menu on dm800
[enigma2.git] / lib / python / Screens / InputBox.py
1 from enigma import eRCInput, getPrevAsciiCode
2 from Screens.Screen import Screen
3 from Screens.MessageBox import MessageBox
4 from Components.ActionMap import NumberActionMap
5 from Components.Label import Label
6 from Components.Input import Input
7 from Tools.BoundFunction import boundFunction
8 from time import time
9
10 class InputBox(Screen):
11         def __init__(self, session, title = "", windowTitle = _("Input"), **kwargs):
12                 Screen.__init__(self, session)
13
14                 self["text"] = Label(title)
15                 self["input"] = Input(**kwargs)
16                 self.onShown.append(boundFunction(self.setTitle, windowTitle))
17
18                 self["actions"] = NumberActionMap(["WizardActions", "InputBoxActions", "InputAsciiActions", "KeyboardInputActions"], 
19                 {
20                         "gotAsciiCode": self.gotAsciiCode,
21                         "ok": self.go,
22                         "back": self.cancel,
23                         "left": self.keyLeft,
24                         "right": self.keyRight,
25                         "home": self.keyHome,
26                         "end": self.keyEnd,
27                         "deleteForward": self.keyDelete,
28                         "deleteBackward": self.keyBackspace,
29                         "tab": self.keyTab,
30                         "toggleOverwrite": self.keyInsert,
31                         "1": self.keyNumberGlobal,
32                         "2": self.keyNumberGlobal,
33                         "3": self.keyNumberGlobal,
34                         "4": self.keyNumberGlobal,
35                         "5": self.keyNumberGlobal,
36                         "6": self.keyNumberGlobal,
37                         "7": self.keyNumberGlobal,
38                         "8": self.keyNumberGlobal,
39                         "9": self.keyNumberGlobal,
40                         "0": self.keyNumberGlobal
41                 }, -1)
42                 if self["input"].type == Input.TEXT:
43                         rcinput = eRCInput.getInstance()
44                         rcinput.setKeyboardMode(rcinput.kmAscii)
45
46         def gotAsciiCode(self):
47                 self["input"].handleAscii(getPrevAsciiCode())
48
49         def keyLeft(self):
50                 self["input"].left()
51
52         def keyRight(self):
53                 self["input"].right()
54
55         def keyNumberGlobal(self, number):
56                 self["input"].number(number)
57
58         def keyDelete(self):
59                 self["input"].delete()
60
61         def go(self):
62                 rcinput = eRCInput.getInstance()
63                 rcinput.setKeyboardMode(rcinput.kmNone)
64                 self.close(self["input"].getText())
65
66         def cancel(self):
67                 rcinput = eRCInput.getInstance()
68                 rcinput.setKeyboardMode(rcinput.kmNone)
69                 self.close(None)
70
71         def keyHome(self):
72                 self["input"].home()
73
74         def keyEnd(self):
75                 self["input"].end()
76
77         def keyBackspace(self):
78                 self["input"].deleteBackward()
79
80         def keyTab(self):
81                 self["input"].tab()
82
83         def keyInsert(self):
84                 self["input"].toggleOverwrite()
85
86 class PinInput(InputBox):
87         def __init__(self, session, service = "", triesEntry = None, pinList = [], *args, **kwargs):
88                 InputBox.__init__(self, session = session, text="    ", maxSize=True, type=Input.PIN, *args, **kwargs)
89                 
90                 self.waitTime = 15
91                 
92                 self.triesEntry = triesEntry
93                 
94                 self.pinList = pinList
95                 self["service"] = Label(service)
96                 
97                 if self.getTries() == 0:
98                         if (self.triesEntry.time.value + (self.waitTime * 60)) > time():
99                                 remaining = (self.triesEntry.time.value + (self.waitTime * 60)) - time()
100                                 remainingMinutes = int(remaining / 60)
101                                 remainingSeconds = int(remaining % 60)
102                                 self.onFirstExecBegin.append(boundFunction(self.session.openWithCallback, self.closePinCancel, MessageBox, _("You have to wait for") + " " + str(remainingMinutes) + " " + _("minutes and") + " " + str(remainingSeconds) + " " + _("seconds."), MessageBox.TYPE_ERROR))
103                         else:
104                                 self.setTries(3)
105
106                 self["tries"] = Label("")
107                 self.onShown.append(self.showTries)
108
109         def gotAsciiCode(self):
110                 if self["input"].currPos == len(self["input"]) - 1:
111                         InputBox.gotAsciiCode(self)
112                         self.go()
113                 else:
114                         InputBox.gotAsciiCode(self)
115
116         def keyNumberGlobal(self, number):
117                 if self["input"].currPos == len(self["input"]) - 1:
118                         InputBox.keyNumberGlobal(self, number)
119                         self.go()
120                 else:
121                         InputBox.keyNumberGlobal(self, number)
122                 
123         def checkPin(self, pin):
124                 if pin is not None and pin.find(" ") == -1 and int(pin) in self.pinList:
125                         return True
126                 return False
127                 
128         def go(self):
129                 self.triesEntry.time.value = int(time())
130                 self.triesEntry.time.save()
131                 if self.checkPin(self["input"].getText()):
132                         self.setTries(3)
133                         self.closePinCorrect()
134                 else:
135                         self.keyHome()
136                         self.decTries()
137                         if self.getTries() == 0:
138                                 self.closePinWrong()
139                         else:
140                                 pass
141         
142         def closePinWrong(self, *args):
143                 rcinput = eRCInput.getInstance()
144                 rcinput.setKeyboardMode(rcinput.kmNone)
145                 print "args:", args
146                 self.close(False)
147                 
148         def closePinCorrect(self, *args):
149                 rcinput = eRCInput.getInstance()
150                 rcinput.setKeyboardMode(rcinput.kmNone)
151                 self.close(True)
152                 
153         def closePinCancel(self, *args):
154                 rcinput = eRCInput.getInstance()
155                 rcinput.setKeyboardMode(rcinput.kmNone)
156                 self.close(None)
157                         
158         def cancel(self):
159                 self.closePinCancel()
160                 
161         def getTries(self):
162                 return self.triesEntry.tries.value
163
164         def decTries(self):
165                 self.setTries(self.triesEntry.tries.value - 1)
166                 self.showTries()
167                 
168         def setTries(self, tries):
169                 self.triesEntry.tries.value = tries
170                 self.triesEntry.tries.save()
171                                 
172         def showTries(self):
173                 self["tries"].setText(_("Tries left:") + " " + str(self.getTries()))