many keyboard improvements by Anders Holst
[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                         "moveLeft": self.keyLeft,
26                         "moveRight": self.keyRight,
27                         "moveHome": self.keyHome,
28                         "moveEnd": self.keyEnd,
29                         "deleteForward": self.keyDelete,
30                         "deleteBackward": self.keyBackspace,
31                         "tab": self.keyTab,
32                         "toggleOverwrite": self.keyInsert,
33                         "accept": self.go,
34                         "1": self.keyNumberGlobal,
35                         "2": self.keyNumberGlobal,
36                         "3": self.keyNumberGlobal,
37                         "4": self.keyNumberGlobal,
38                         "5": self.keyNumberGlobal,
39                         "6": self.keyNumberGlobal,
40                         "7": self.keyNumberGlobal,
41                         "8": self.keyNumberGlobal,
42                         "9": self.keyNumberGlobal,
43                         "0": self.keyNumberGlobal
44                 }, -1)
45                 rcinput = eRCInput.getInstance()
46                 rcinput.setKeyboardMode(rcinput.kmAscii)
47
48         def gotAsciiCode(self):
49                 self["input"].handleAscii(getPrevAsciiCode())
50
51         def keyLeft(self):
52                 self["input"].left()
53
54         def keyRight(self):
55                 self["input"].right()
56
57         def keyNumberGlobal(self, number):
58                 self["input"].number(number)
59
60         def keyDelete(self):
61                 self["input"].delete()
62
63         def go(self):
64                 rcinput = eRCInput.getInstance()
65                 rcinput.setKeyboardMode(rcinput.kmNone)
66                 self.close(self["input"].getText())
67
68         def cancel(self):
69                 rcinput = eRCInput.getInstance()
70                 rcinput.setKeyboardMode(rcinput.kmNone)
71                 self.close(None)
72
73         def keyHome(self):
74                 self["input"].home()
75
76         def keyEnd(self):
77                 self["input"].end()
78
79         def keyBackspace(self):
80                 self["input"].deleteBackward()
81
82         def keyTab(self):
83                 self["input"].tab()
84
85         def keyInsert(self):
86                 self["input"].toggleOverwrite()
87
88 class PinInput(InputBox):
89         def __init__(self, session, service = "", triesEntry = None, pinList = [], *args, **kwargs):
90                 InputBox.__init__(self, session = session, text="    ", maxSize=True, type=Input.PIN, *args, **kwargs)
91                 
92                 self.waitTime = 15
93                 
94                 self.triesEntry = triesEntry
95                 
96                 self.pinList = pinList
97                 self["service"] = Label(service)
98                 
99                 if self.getTries() == 0:
100                         if (self.triesEntry.time.value + (self.waitTime * 60)) > time():
101                                 remaining = (self.triesEntry.time.value + (self.waitTime * 60)) - time()
102                                 remainingMinutes = int(remaining / 60)
103                                 remainingSeconds = int(remaining % 60)
104                                 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))
105                         else:
106                                 self.setTries(3)
107
108                 self["tries"] = Label("")
109                 self.onShown.append(self.showTries)
110
111         def gotAsciiCode(self):
112                 if self["input"].currPos == len(self["input"]) - 1:
113                         InputBox.gotAsciiCode(self)
114                         self.go()
115                 else:
116                         InputBox.gotAsciiCode(self)
117
118         def keyNumberGlobal(self, number):
119                 if self["input"].currPos == len(self["input"]) - 1:
120                         InputBox.keyNumberGlobal(self, number)
121                         self.go()
122                 else:
123                         InputBox.keyNumberGlobal(self, number)
124                 
125         def checkPin(self, pin):
126                 if pin is not None and pin.find(" ") == -1 and int(pin) in self.pinList:
127                         return True
128                 return False
129                 
130         def go(self):
131                 self.triesEntry.time.value = int(time())
132                 self.triesEntry.time.save()
133                 if self.checkPin(self["input"].getText()):
134                         self.setTries(3)
135                         self.closePinCorrect()
136                 else:
137                         self.keyHome()
138                         self.decTries()
139                         if self.getTries() == 0:
140                                 self.closePinWrong()
141                         else:
142                                 pass
143         
144         def closePinWrong(self, *args):
145                 rcinput = eRCInput.getInstance()
146                 rcinput.setKeyboardMode(rcinput.kmNone)
147                 print "args:", args
148                 self.close(False)
149                 
150         def closePinCorrect(self, *args):
151                 rcinput = eRCInput.getInstance()
152                 rcinput.setKeyboardMode(rcinput.kmNone)
153                 self.close(True)
154                 
155         def closePinCancel(self, *args):
156                 rcinput = eRCInput.getInstance()
157                 rcinput.setKeyboardMode(rcinput.kmNone)
158                 self.close(None)
159                         
160         def cancel(self):
161                 self.closePinCancel()
162                 
163         def getTries(self):
164                 return self.triesEntry.tries.value
165
166         def decTries(self):
167                 self.setTries(self.triesEntry.tries.value - 1)
168                 self.showTries()
169                 
170         def setTries(self, tries):
171                 self.triesEntry.tries.value = tries
172                 self.triesEntry.tries.save()
173                                 
174         def showTries(self):
175                 self["tries"].setText(_("Tries left:") + " " + str(self.getTries()))