55de9c7167f0566c83779e19ac8aef5991f7137b
[openblackhole/openblackhole-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 Tools.Notifications import AddPopup
9 from time import time
10
11 class InputBox(Screen):
12         def __init__(self, session, title = "", windowTitle = None, useableChars = None, **kwargs):
13                 Screen.__init__(self, session)
14
15                 self["text"] = Label(title)
16                 self["input"] = Input(**kwargs)
17                 if windowTitle is None:
18                         windowTitle = _("Input")
19                 self.onShown.append(boundFunction(self.setTitle, windowTitle))
20                 if useableChars is not None:
21                         self["input"].setUseableChars(useableChars)
22
23                 self["actions"] = NumberActionMap(["WizardActions", "InputBoxActions", "InputAsciiActions", "KeyboardInputActions"],
24                 {
25                         "gotAsciiCode": self.gotAsciiCode,
26                         "ok": self.go,
27                         "back": self.cancel,
28                         "left": self.keyLeft,
29                         "right": self.keyRight,
30                         "home": self.keyHome,
31                         "end": self.keyEnd,
32                         "deleteForward": self.keyDelete,
33                         "deleteBackward": self.keyBackspace,
34                         "tab": self.keyTab,
35                         "toggleOverwrite": self.keyInsert,
36                         "1": self.keyNumberGlobal,
37                         "2": self.keyNumberGlobal,
38                         "3": self.keyNumberGlobal,
39                         "4": self.keyNumberGlobal,
40                         "5": self.keyNumberGlobal,
41                         "6": self.keyNumberGlobal,
42                         "7": self.keyNumberGlobal,
43                         "8": self.keyNumberGlobal,
44                         "9": self.keyNumberGlobal,
45                         "0": self.keyNumberGlobal
46                 }, -1)
47
48                 if self["input"].type == Input.TEXT:
49                         try:
50                                 vumachine = file("/proc/stb/info/vumodel").read().strip()
51                                 if vumachine == "ultimo" or vumachine == "zero":
52                                         self.onExecBegin.append(self.setKeyboardModeNone)
53                                 else:
54                                         self.onExecBegin.append(self.setKeyboardModeAscii)
55                         except:
56                                 self.onExecBegin.append(self.setKeyboardModeAscii)
57                 else:
58                         self.onExecBegin.append(self.setKeyboardModeNone)
59
60         def gotAsciiCode(self):
61                 self["input"].handleAscii(getPrevAsciiCode())
62
63         def keyLeft(self):
64                 self["input"].left()
65
66         def keyRight(self):
67                 self["input"].right()
68
69         def keyNumberGlobal(self, number):
70                 self["input"].number(number)
71
72         def keyDelete(self):
73                 self["input"].delete()
74
75         def go(self):
76                 self.close(self["input"].getText())
77
78         def cancel(self):
79                 self.close(None)
80
81         def keyHome(self):
82                 self["input"].home()
83
84         def keyEnd(self):
85                 self["input"].end()
86
87         def keyBackspace(self):
88                 self["input"].deleteBackward()
89
90         def keyTab(self):
91                 self["input"].tab()
92
93         def keyInsert(self):
94                 self["input"].toggleOverwrite()
95
96 class PinInput(InputBox):
97         def __init__(self, session, service = "", triesEntry = None, pinList = [], popup = False, simple=True, *args, **kwargs):
98                 InputBox.__init__(self, session = session, text = "    ", maxSize = True, type = Input.PIN, *args, **kwargs)
99
100                 self.waitTime = 15
101                 self.triesEntry = triesEntry
102                 self.pinList = pinList
103                 self["service"] = Label(service)
104
105                 if service and simple:
106                         self.skinName = "PinInputPopup"
107
108                 if self.getTries() == 0:
109                         if (self.triesEntry.time.value + (self.waitTime * 60)) > time():
110                                 remaining = (self.triesEntry.time.value + (self.waitTime * 60)) - time()
111                                 remainingMinutes = int(remaining / 60)
112                                 remainingSeconds = int(remaining % 60)
113                                 messageText = _("You have to wait %s!") % (str(remainingMinutes) + " " + _("minutes") + ", " + str(remainingSeconds) + " " + _("seconds"))
114                                 if service and simple:
115                                         AddPopup(messageText, type = MessageBox.TYPE_ERROR, timeout = 3)
116                                         self.closePinCancel()
117                                 else:
118                                         self.onFirstExecBegin.append(boundFunction(self.session.openWithCallback, self.closePinCancel, MessageBox, messageText, MessageBox.TYPE_ERROR, timeout = 3))
119                         else:
120                                 self.setTries(3)
121
122                 self["tries"] = Label("")
123                 self.onShown.append(self.showTries)
124
125         def gotAsciiCode(self):
126                 if self["input"].currPos == len(self["input"]) - 1:
127                         InputBox.gotAsciiCode(self)
128                         self.go()
129                 else:
130                         InputBox.gotAsciiCode(self)
131
132         def keyNumberGlobal(self, number):
133                 if self["input"].currPos == len(self["input"]) - 1:
134                         InputBox.keyNumberGlobal(self, number)
135                         self.go()
136                 else:
137                         InputBox.keyNumberGlobal(self, number)
138
139         def checkPin(self, pin):
140                 if pin is not None and " " not in pin and int(pin) in self.pinList:
141                         return True
142                 return False
143
144         def go(self):
145                 if self.pinList:
146                         self.triesEntry.time.value = int(time())
147                         self.triesEntry.time.save()
148                         if self.checkPin(self["input"].getText()):
149                                 self.setTries(3)
150                                 self.closePinCorrect()
151                         else:
152                                 self.keyHome()
153                                 self.decTries()
154                                 if self.getTries() == 0:
155                                         self.closePinWrong()
156                 else:
157                         pin = self["input"].getText()
158                         if pin and pin.isdigit():
159                                 self.close(int(pin))
160                         else:
161                                 self.close(None)
162
163         def closePinWrong(self, *args):
164                 print "args:", args
165                 self.close(False)
166
167         def closePinCorrect(self, *args):
168                 self.setTries(3)
169                 self.close(True)
170
171         def closePinCancel(self, *args):
172                 self.close(None)
173
174         def cancel(self):
175                 self.closePinCancel()
176
177         def getTries(self):
178                 return self.triesEntry and self.triesEntry.tries.value
179
180         def decTries(self):
181                 self.setTries(self.triesEntry.tries.value - 1)
182                 self.showTries()
183
184         def setTries(self, tries):
185                 self.triesEntry.tries.value = tries
186                 self.triesEntry.tries.save()
187
188         def showTries(self):
189                 self["tries"].setText(self.triesEntry and _("Tries left:") + " " + str(self.getTries() or ""))
190
191         def keyRight(self):
192                 pass