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