4.3.0r12
[enigma2.git] / usr / lib / enigma2 / python / Components / ConfigList.py
1 from HTMLComponent import HTMLComponent
2 from GUIComponent import GUIComponent
3 from config import KEY_LEFT, KEY_RIGHT, KEY_HOME, KEY_END, KEY_0, KEY_DELETE, KEY_BACKSPACE, KEY_OK, KEY_TOGGLEOW, KEY_ASCII, KEY_TIMEOUT, KEY_NUMBERS, ConfigElement, ConfigText, ConfigPassword
4 from Components.ActionMap import NumberActionMap, ActionMap
5 from enigma import eListbox, eListboxPythonConfigContent, eTimer
6 from Screens.MessageBox import MessageBox
7 from skin import componentSizes
8
9 class ConfigList(HTMLComponent, GUIComponent, object):
10         def __init__(self, list, session = None):
11                 GUIComponent.__init__(self)
12                 self.l = eListboxPythonConfigContent()
13                 sizes = componentSizes[componentSizes.CONFIG_LIST]
14                 self.l.setSeperation(sizes.get("seperation", 400))
15                 self.l.setDividerHeight(sizes.get("dividerHeight", 1))
16                 self.timer = eTimer()
17                 self._headers = []
18                 self.list = list
19                 self.onSelectionChanged = [ ]
20                 self.current = None
21                 self.session = session
22
23         def execBegin(self):
24                 self.timer_conn = self.timer.timeout.connect(self.timeout)
25
26         def execEnd(self):
27                 self.timer_conn = None
28
29         def toggle(self):
30                 selection = self.getCurrent()
31                 selection[1].toggle()
32                 self.invalidateCurrent()
33
34         def handleKey(self, key):
35                 selection = self.getCurrent()
36                 if selection and selection[1].enabled:
37                         selection[1].handleKey(key)
38                         self.invalidateCurrent()
39                         if key in KEY_NUMBERS:
40                                 self.timer.start(1000, 1)
41
42         def getCurrent(self):
43                 return self.l.getCurrentSelection()
44         
45         def getCurrentIndex(self):
46                 return self.l.getCurrentSelectionIndex()
47         
48         def setCurrentIndex(self, index):
49                 if self.instance is not None:
50                         self.instance.moveSelectionTo(index)
51         
52         def invalidateCurrent(self):
53                 self.l.invalidateEntry(self.l.getCurrentSelectionIndex())
54                 for x in self.onSelectionChanged:
55                         x()
56
57         def invalidate(self, entry):
58                 # when the entry to invalidate does not exist, just ignore the request.
59                 # this eases up conditional setup screens a lot.
60                 if entry in self.__list:
61                         self.l.invalidateEntry(self.__list.index(entry))
62
63         GUI_WIDGET = eListbox
64         
65         def selectionChanged(self):
66                 if isinstance(self.current,tuple) and len(self.current) > 1:
67                         self.current[1].onDeselect(self.session)
68                 self.current = self.getCurrent()
69                 if isinstance(self.current,tuple) and len(self.current) > 1:
70                         self.current[1].onSelect(self.session)
71                 else:
72                         return
73                 for x in self.onSelectionChanged:
74                         x()
75
76         def postWidgetCreate(self, instance):
77                 self.selectionChanged_conn = instance.selectionChanged.connect(self.selectionChanged)
78                 instance.setContent(self.l)
79         
80         def preWidgetRemove(self, instance):
81                 if isinstance(self.current,tuple) and len(self.current) > 1:
82                         self.current[1].onDeselect(self.session)
83                 self.selectionChanged_conn = None
84                 instance.setContent(None)
85
86         def setList(self, l):
87                 self.timer.stop()
88                 self.__list = l
89                 self.l.setList(self.__list)
90                 self._headers = []
91                 if l is not None:
92                         index = 0
93                         for x in l:
94                                 if len(x) < 2:
95                                         self._headers.append(index)
96                                 else:
97                                         assert isinstance(x[1], ConfigElement), "entry in ConfigList " + str(x[1]) + " must be a ConfigElement"
98                                 index += 1
99
100         def pageUp(self):
101                 self.instance.moveSelection(eListbox.pageUp)
102
103         def pageDown(self):
104                 self.instance.moveSelection(eListbox.pageDown)
105
106         def jumpToNextSection(self):
107                 index = self.getCurrentIndex()
108                 maxlen = len(self.__list)
109                 while index < maxlen - 1:
110                         index += 1
111                         if index in self._headers:
112                                 if index + 1 < maxlen:
113                                         self.setCurrentIndex(index + 1)
114                                         return
115                                 else:
116                                         self.setCurrentIndex(index - 1)
117                                         return
118                 self.pageDown()
119
120         def jumpToPreviousSection(self):
121                 index = self.getCurrentIndex() - 1
122                 maxlen = len(self.__list)
123                 while index >= 0 and maxlen > 0:
124                         index -= 1
125                         if index in self._headers:
126                                 if index + 1 < maxlen:
127                                         self.setCurrentIndex(index + 1)
128                                         return
129                                 else:
130                                         self.setCurrentIndex(index - 1)
131                                         return
132                 self.pageUp()
133
134         def getList(self):
135                 return self.__list
136
137         list = property(getList, setList)
138
139         def timeout(self):
140                 self.handleKey(KEY_TIMEOUT)
141
142         def isChanged(self):
143                 is_changed = False
144                 for x in self.list:
145                         if len(x) > 1:
146                                 is_changed |= x[1].isChanged()
147
148                 return is_changed
149
150 class ConfigListScreen(object):
151         def __init__(self, list, session = None, on_change = None):
152                 self["config_actions"] = NumberActionMap(["SetupActions", "InputAsciiActions", "KeyboardInputActions"],
153                 {
154                         "gotAsciiCode": self.keyGotAscii,
155                         "ok": self.keyOK,
156                         "left": self.keyLeft,
157                         "right": self.keyRight,
158                         "home": self.keyHome,
159                         "end": self.keyEnd,
160                         "deleteForward": self.keyDelete,
161                         "deleteBackward": self.keyBackspace,
162                         "toggleOverwrite": self.keyToggleOW,
163                         "previousSection" : self.keyPreviousSection,
164                         "nextSection" : self.keyNextSection,
165                         "1": self.keyNumberGlobal,
166                         "2": self.keyNumberGlobal,
167                         "3": self.keyNumberGlobal,
168                         "4": self.keyNumberGlobal,
169                         "5": self.keyNumberGlobal,
170                         "6": self.keyNumberGlobal,
171                         "7": self.keyNumberGlobal,
172                         "8": self.keyNumberGlobal,
173                         "9": self.keyNumberGlobal,
174                         "0": self.keyNumberGlobal
175                 }, -1) # to prevent left/right overriding the listbox
176
177                 self["VirtualKB"] = ActionMap(["VirtualKeyboardActions"],
178                 {
179                         "showVirtualKeyboard": self.KeyText,
180                 }, -2)
181                 self["VirtualKB"].setEnabled(False)
182                 
183                 self["config"] = ConfigList(list, session = session)
184                 if not hasattr(self, "setup_title"):
185                         self.setup_title = ""
186
187                 self.onConfigEntryChanged = []
188                 if on_change:
189                         self.onConfigEntryChanged.append(on_change)
190
191                 if not self.handleInputHelpers in self["config"].onSelectionChanged:
192                         self["config"].onSelectionChanged.append(self.handleInputHelpers)
193
194         def _changedEntry(self):
195                 for fnc in self.onConfigEntryChanged:
196                         fnc()
197
198         def handleInputHelpers(self):
199                 if self["config"].getCurrent() is not None:
200                         if isinstance(self["config"].getCurrent()[1], ConfigText) or isinstance(self["config"].getCurrent()[1], ConfigPassword):
201                                 if self.has_key("VKeyIcon"):
202                                         self["VirtualKB"].setEnabled(True)
203                                         self["VKeyIcon"].boolean = True
204                                 if self.has_key("HelpWindow"):
205                                         if self["config"].getCurrent()[1].help_window.instance is not None:
206                                                 helpwindowpos = self["HelpWindow"].getPosition()
207                                                 from enigma import ePoint
208                                                 self["config"].getCurrent()[1].help_window.instance.move(ePoint(helpwindowpos[0],helpwindowpos[1]))
209                         else:
210                                 if self.has_key("VKeyIcon"):
211                                         self["VirtualKB"].setEnabled(False)
212                                         self["VKeyIcon"].boolean = False
213                 else:
214                         if self.has_key("VKeyIcon"):
215                                 self["VirtualKB"].setEnabled(False)
216                                 self["VKeyIcon"].boolean = False
217
218         def KeyText(self):
219                 from Screens.VirtualKeyBoard import VirtualKeyBoard
220                 self.session.openWithCallback(self.VirtualKeyBoardCallback, VirtualKeyBoard, title = self["config"].getCurrent()[0], text = self["config"].getCurrent()[1].getValue())
221
222         def VirtualKeyBoardCallback(self, callback = None):
223                 if callback is not None and len(callback):
224                         self["config"].getCurrent()[1].setValue(callback)
225                         self["config"].invalidate(self["config"].getCurrent())
226                         
227         def keyOK(self):
228                 self["config"].handleKey(KEY_OK)
229
230         def keyLeft(self):
231                 self["config"].handleKey(KEY_LEFT)
232                 self._changedEntry()
233
234         def keyRight(self):
235                 self["config"].handleKey(KEY_RIGHT)
236                 self._changedEntry()
237
238         def keyHome(self):
239                 self["config"].handleKey(KEY_HOME)
240                 self._changedEntry()
241
242         def keyEnd(self):
243                 self["config"].handleKey(KEY_END)
244                 self._changedEntry()
245
246         def keyDelete(self):
247                 self["config"].handleKey(KEY_DELETE)
248                 self._changedEntry()
249
250         def keyBackspace(self):
251                 self["config"].handleKey(KEY_BACKSPACE)
252                 self._changedEntry()
253
254         def keyToggleOW(self):
255                 self["config"].handleKey(KEY_TOGGLEOW)
256                 self._changedEntry()
257
258         def keyGotAscii(self):
259                 self["config"].handleKey(KEY_ASCII)
260                 self._changedEntry()
261
262         def keyNumberGlobal(self, number):
263                 self["config"].handleKey(KEY_0 + number)
264                 self._changedEntry()
265
266         def keyPreviousSection(self):
267                 self["config"].jumpToPreviousSection()
268
269         def keyNextSection(self):
270                 self["config"].jumpToNextSection()
271
272         def saveAll(self):
273                 for x in self["config"].list:
274                         if len(x) > 1:
275                                 x[1].save()
276
277         # keySave and keyCancel are just provided in case you need them.
278         # you have to call them by yourself.
279         def keySave(self):
280                 self.saveAll()
281                 self.close()
282         
283         def cancelConfirm(self, result):
284                 if not result:
285                         return
286
287                 for x in self["config"].list:
288                         if len(x) > 1:
289                                 x[1].cancel()
290                 self.close()
291
292         def keyCancel(self):
293                 if self["config"].isChanged():
294                         self.session.openWithCallback(self.cancelConfirm, MessageBox, _("Really close without saving settings?"))
295                 else:
296                         self.close()
297
298         def getCurrentEntry(self):
299                 current = self["config"].getCurrent()
300                 return current and current[0] or ""
301
302         def getCurrentValue(self):
303                 current = self["config"].getCurrent()
304                 return current and str(current[1].getText()) or ""
305
306         def createSummary(self):
307                 from Screens.Setup import SetupSummary
308                 return SetupSummary