PEP8: Fix whitespace
[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                         self.onExecBegin.append(self.setKeyboardModeAscii)
50                 else:
51                         self.onExecBegin.append(self.setKeyboardModeNone)
52
53         def gotAsciiCode(self):
54                 self["input"].handleAscii(getPrevAsciiCode())
55
56         def keyLeft(self):
57                 self["input"].left()
58
59         def keyRight(self):
60                 self["input"].right()
61
62         def keyNumberGlobal(self, number):
63                 self["input"].number(number)
64
65         def keyDelete(self):
66                 self["input"].delete()
67
68         def go(self):
69                 self.close(self["input"].getText())
70
71         def cancel(self):
72                 self.close(None)
73
74         def keyHome(self):
75                 self["input"].home()
76
77         def keyEnd(self):
78                 self["input"].end()
79
80         def keyBackspace(self):
81                 self["input"].deleteBackward()
82
83         def keyTab(self):
84                 self["input"].tab()
85
86         def keyInsert(self):
87                 self["input"].toggleOverwrite()
88
89 class PinInput(InputBox):
90         def __init__(self, session, service = "", triesEntry = None, pinList = [], popup = False, simple=True, *args, **kwargs):
91                 InputBox.__init__(self, session = session, text = "    ", maxSize = True, type = Input.PIN, *args, **kwargs)
92
93                 self.waitTime = 15
94                 self.triesEntry = triesEntry
95                 self.pinList = pinList
96                 self["service"] = Label(service)
97
98                 if service and simple:
99                         self.skinName = "PinInputPopup"
100
101                 if self.getTries() == 0:
102                         if (self.triesEntry.time.value + (self.waitTime * 60)) > time():
103                                 remaining = (self.triesEntry.time.value + (self.waitTime * 60)) - time()
104                                 remainingMinutes = int(remaining / 60)
105                                 remainingSeconds = int(remaining % 60)
106                                 messageText = _("You have to wait %s!") % (str(remainingMinutes) + " " + _("minutes") + ", " + str(remainingSeconds) + " " + _("seconds"))
107                                 if service and simple:
108                                         AddPopup(messageText, type = MessageBox.TYPE_ERROR, timeout = 3)
109                                         self.closePinCancel()
110                                 else:
111                                         self.onFirstExecBegin.append(boundFunction(self.session.openWithCallback, self.closePinCancel, MessageBox, messageText, MessageBox.TYPE_ERROR, timeout = 3))
112                         else:
113                                 self.setTries(3)
114
115                 self["tries"] = Label("")
116                 self.onShown.append(self.showTries)
117
118         def gotAsciiCode(self):
119                 if self["input"].currPos == len(self["input"]) - 1:
120                         InputBox.gotAsciiCode(self)
121                         self.go()
122                 else:
123                         InputBox.gotAsciiCode(self)
124
125         def keyNumberGlobal(self, number):
126                 if self["input"].currPos == len(self["input"]) - 1:
127                         InputBox.keyNumberGlobal(self, number)
128                         self.go()
129                 else:
130                         InputBox.keyNumberGlobal(self, number)
131
132         def checkPin(self, pin):
133                 if pin is not None and " " not in pin and int(pin) in self.pinList:
134                         return True
135                 return False
136
137         def go(self):
138                 if self.pinList:
139                         self.triesEntry.time.value = int(time())
140                         self.triesEntry.time.save()
141                         if self.checkPin(self["input"].getText()):
142                                 self.setTries(3)
143                                 self.closePinCorrect()
144                         else:
145                                 self.keyHome()
146                                 self.decTries()
147                                 if self.getTries() == 0:
148                                         self.closePinWrong()
149                 else:
150                         pin = self["input"].getText()
151                         if pin and pin.isdigit():
152                                 self.close(int(pin))
153                         else:
154                                 self.close(None)
155
156         def closePinWrong(self, *args):
157                 print "args:", args
158                 self.close(False)
159
160         def closePinCorrect(self, *args):
161                 self.setTries(3)
162                 self.close(True)
163
164         def closePinCancel(self, *args):
165                 self.close(None)
166
167         def cancel(self):
168                 self.closePinCancel()
169
170         def getTries(self):
171                 return self.triesEntry and self.triesEntry.tries.value
172
173         def decTries(self):
174                 self.setTries(self.triesEntry.tries.value - 1)
175                 self.showTries()
176
177         def setTries(self, tries):
178                 self.triesEntry.tries.value = tries
179                 self.triesEntry.tries.save()
180
181         def showTries(self):
182                 self["tries"].setText(self.triesEntry and _("Tries left:") + " " + str(self.getTries() or ""))
183
184         def keyRight(self):
185                 pass