add parental control (still somehow buggy and some minor features missing... don...
[enigma2.git] / lib / python / Screens / InputBox.py
1 from enigma import *
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 Components.GUIComponent import *
8 from Tools.BoundFunction import boundFunction
9
10 import os
11
12 class InputBox(Screen):
13         def __init__(self, session, title = "", windowTitle = _("Input"), **kwargs):
14                 Screen.__init__(self, session)
15
16                 self["text"] = Label(title)
17                 self["input"] = Input(**kwargs)
18                 self.onShown.append(boundFunction(self.setTitle, windowTitle))
19
20                 self["actions"] = NumberActionMap(["WizardActions", "InputBoxActions", "InputAsciiActions", "KeyboardInputActions"], 
21                 {
22                         "gotAsciiCode": self.gotAsciiCode,
23                         "ok": self.go,
24                         "back": self.cancel,
25                         "left": self.keyLeft,
26                         "right": self.keyRight,
27                         "delete": self.keyDelete,
28                         "moveLeft": self.keyLeft,
29                         "moveRight": self.keyRight,
30                         "moveHome": self.keyHome,
31                         "moveEnd": self.keyEnd,
32                         "deleteForward": self.keyDelete,
33                         "deleteBackward": self.keyBackspace,
34                         "tab": self.keyTab,
35                         "toggleOverwrite": self.keyInsert,
36                         "accept": self.go,
37                         "1": self.keyNumberGlobal,
38                         "2": self.keyNumberGlobal,
39                         "3": self.keyNumberGlobal,
40                         "4": self.keyNumberGlobal,
41                         "5": self.keyNumberGlobal,
42                         "6": self.keyNumberGlobal,
43                         "7": self.keyNumberGlobal,
44                         "8": self.keyNumberGlobal,
45                         "9": self.keyNumberGlobal,
46                         "0": self.keyNumberGlobal
47                 }, -1)
48                 rcinput = eRCInput.getInstance()
49                 rcinput.setKeyboardMode(rcinput.kmAscii)
50
51         def gotAsciiCode(self):
52                 self["input"].handleAscii(getPrevAsciiCode())
53
54         def keyLeft(self):
55                 self["input"].left()
56
57         def keyRight(self):
58                 self["input"].right()
59
60         def keyNumberGlobal(self, number):
61                 self["input"].number(number)
62
63         def keyDelete(self):
64                 self["input"].delete()
65
66         def go(self):
67                 rcinput = eRCInput.getInstance()
68                 rcinput.setKeyboardMode(rcinput.kmNone)
69                 self.close(self["input"].getText())
70
71         def cancel(self):
72                 rcinput = eRCInput.getInstance()
73                 rcinput.setKeyboardMode(rcinput.kmNone)
74                 self.close(None)
75
76         def keyHome(self):
77                 self["input"].home()
78
79         def keyEnd(self):
80                 self["input"].end()
81
82         def keyBackspace(self):
83                 self["input"].deleteBackward()
84
85         def keyTab(self):
86                 self["input"].tab()
87
88         def keyInsert(self):
89                 self["input"].toggleOverwrite()
90
91 class PinInput(InputBox):
92         def __init__(self, session, service = "", tries = 1, pinList = [], *args, **kwargs):
93                 InputBox.__init__(self, session = session, text="9876", maxSize=True, type=Input.PIN, *args, **kwargs)
94                 
95                 self.showTries = True
96                 if tries == 1:
97                         self.showTries = False
98
99                 self.pinList = pinList
100                 self["service"] = Label(service)
101                 
102                 self["tries"] = Label("")
103                 self.onShown.append(boundFunction(self.setTries, tries))
104                                 
105         def keyNumberGlobal(self, number):
106                 if self["input"].currPos == len(self["input"]) - 1:
107                         InputBox.keyNumberGlobal(self, number)
108                         self.go()
109                 else:
110                         InputBox.keyNumberGlobal(self, number)
111                 
112         def checkPin(self, pin):
113                 if pin is not None and int(pin) in self.pinList:
114                         return True
115                 return False
116                 
117         def go(self):
118                 if self.checkPin(self["input"].getText()):
119                         self.close((True, self.tries))
120                 else:
121                         self.keyHome()
122                         self.setTries(self.tries - 1)
123                         if self.tries == 0:
124                                 self.close((False, self.tries))
125                         else:
126                                 pass
127                         
128         def cancel(self):
129                 rcinput = eRCInput.getInstance()
130                 rcinput.setKeyboardMode(rcinput.kmNone)
131                 self.close((None, self.tries))
132         
133         def setTries(self, tries):
134                 self.tries = tries
135                 if self.showTries:
136                         self["tries"].setText(_("Tries left:") + " " + str(tries))