Merge commit 'origin/bug_351_softwaremanager_save_config_before_backup' into experimental
[enigma2.git] / lib / 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, eRCInput, eTimer
6 from Screens.MessageBox import MessageBox
7
8 class ConfigList(HTMLComponent, GUIComponent, object):
9         def __init__(self, list, session = None):
10                 GUIComponent.__init__(self)
11                 self.l = eListboxPythonConfigContent()
12                 self.l.setSeperation(200)
13                 self.timer = eTimer()
14                 self.list = list
15                 self.onSelectionChanged = [ ]
16                 self.current = None
17                 self.session = session
18
19         def execBegin(self):
20                 rcinput = eRCInput.getInstance()
21                 rcinput.setKeyboardMode(rcinput.kmAscii)
22                 self.timer.callback.append(self.timeout)
23
24         def execEnd(self):
25                 rcinput = eRCInput.getInstance()
26                 rcinput.setKeyboardMode(rcinput.kmNone)
27                 self.timer.callback.remove(self.timeout)
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
55         def invalidate(self, entry):
56                 # when the entry to invalidate does not exist, just ignore the request.
57                 # this eases up conditional setup screens a lot.
58                 if entry in self.__list:
59                         self.l.invalidateEntry(self.__list.index(entry))
60
61         GUI_WIDGET = eListbox
62         
63         def selectionChanged(self):
64                 if self.current:
65                         self.current[1].onDeselect(self.session)
66                 self.current = self.getCurrent()
67                 if self.current:
68                         self.current[1].onSelect(self.session)
69
70                 for x in self.onSelectionChanged:
71                         x()
72
73         def postWidgetCreate(self, instance):
74                 instance.selectionChanged.get().append(self.selectionChanged)
75                 instance.setContent(self.l)
76         
77         def preWidgetRemove(self, instance):
78                 if self.current:
79                         self.current[1].onDeselect(self.session)
80                 instance.selectionChanged.get().remove(self.selectionChanged)
81                 instance.setContent(None)
82         
83         def setList(self, l):
84                 self.timer.stop()
85                 self.__list = l
86                 self.l.setList(self.__list)
87
88                 if l is not None:
89                         for x in l:
90                                 assert isinstance(x[1], ConfigElement), "entry in ConfigList " + str(x[1]) + " must be a ConfigElement"
91
92         def getList(self):
93                 return self.__list
94
95         list = property(getList, setList)
96
97         def timeout(self):
98                 self.handleKey(KEY_TIMEOUT)
99
100         def isChanged(self):
101                 is_changed = False
102                 for x in self.list:
103                         is_changed |= x[1].isChanged()
104
105                 return is_changed
106
107 class ConfigListScreen:
108         def __init__(self, list, session = None, on_change = None):
109                 self["config_actions"] = NumberActionMap(["SetupActions", "InputAsciiActions", "KeyboardInputActions"],
110                 {
111                         "gotAsciiCode": self.keyGotAscii,
112                         "ok": self.keyOK,
113                         "left": self.keyLeft,
114                         "right": self.keyRight,
115                         "home": self.keyHome,
116                         "end": self.keyEnd,
117                         "deleteForward": self.keyDelete,
118                         "deleteBackward": self.keyBackspace,
119                         "toggleOverwrite": self.keyToggleOW,
120                         "1": self.keyNumberGlobal,
121                         "2": self.keyNumberGlobal,
122                         "3": self.keyNumberGlobal,
123                         "4": self.keyNumberGlobal,
124                         "5": self.keyNumberGlobal,
125                         "6": self.keyNumberGlobal,
126                         "7": self.keyNumberGlobal,
127                         "8": self.keyNumberGlobal,
128                         "9": self.keyNumberGlobal,
129                         "0": self.keyNumberGlobal
130                 }, -1) # to prevent left/right overriding the listbox
131
132                 self["VirtualKB"] = ActionMap(["VirtualKeyboardActions"],
133                 {
134                         "showVirtualKeyboard": self.KeyText,
135                 }, -2)
136                 self["VirtualKB"].setEnabled(False)
137                 
138                 self["config"] = ConfigList(list, session = session)
139                 
140                 if on_change is not None:
141                         self.__changed = on_change
142                 else:
143                         self.__changed = lambda: None
144                 
145                 if not self.handleInputHelpers in self["config"].onSelectionChanged:
146                         self["config"].onSelectionChanged.append(self.handleInputHelpers)
147
148         def handleInputHelpers(self):
149                 if self["config"].getCurrent() is not None:
150                         if isinstance(self["config"].getCurrent()[1], ConfigText) or isinstance(self["config"].getCurrent()[1], ConfigPassword):
151                                 if self.has_key("VKeyIcon"):
152                                         self["VirtualKB"].setEnabled(True)
153                                         self["VKeyIcon"].boolean = True
154                                 if self.has_key("HelpWindow"):
155                                         if self["config"].getCurrent()[1].help_window.instance is not None:
156                                                 helpwindowpos = self["HelpWindow"].getPosition()
157                                                 from enigma import ePoint
158                                                 self["config"].getCurrent()[1].help_window.instance.move(ePoint(helpwindowpos[0],helpwindowpos[1]))
159                         else:
160                                 if self.has_key("VKeyIcon"):
161                                         self["VirtualKB"].setEnabled(False)
162                                         self["VKeyIcon"].boolean = False
163                 else:
164                         if self.has_key("VKeyIcon"):
165                                 self["VirtualKB"].setEnabled(False)
166                                 self["VKeyIcon"].boolean = False
167
168         def KeyText(self):
169                 from Screens.VirtualKeyBoard import VirtualKeyBoard
170                 self.session.openWithCallback(self.VirtualKeyBoardCallback, VirtualKeyBoard, title = self["config"].getCurrent()[0], text = self["config"].getCurrent()[1].getValue())
171
172         def VirtualKeyBoardCallback(self, callback = None):
173                 if callback is not None and len(callback):
174                         self["config"].getCurrent()[1].setValue(callback)
175                         self["config"].invalidate(self["config"].getCurrent())
176                         
177         def keyOK(self):
178                 self["config"].handleKey(KEY_OK)
179
180         def keyLeft(self):
181                 self["config"].handleKey(KEY_LEFT)
182                 self.__changed()
183
184         def keyRight(self):
185                 self["config"].handleKey(KEY_RIGHT)
186                 self.__changed()
187
188         def keyHome(self):
189                 self["config"].handleKey(KEY_HOME)
190                 self.__changed()
191
192         def keyEnd(self):
193                 self["config"].handleKey(KEY_END)
194                 self.__changed()
195
196         def keyDelete(self):
197                 self["config"].handleKey(KEY_DELETE)
198                 self.__changed()
199
200         def keyBackspace(self):
201                 self["config"].handleKey(KEY_BACKSPACE)
202                 self.__changed()
203
204         def keyToggleOW(self):
205                 self["config"].handleKey(KEY_TOGGLEOW)
206                 self.__changed()
207
208         def keyGotAscii(self):
209                 self["config"].handleKey(KEY_ASCII)
210                 self.__changed()
211
212         def keyNumberGlobal(self, number):
213                 self["config"].handleKey(KEY_0 + number)
214                 self.__changed()
215                 
216         def saveAll(self):
217                 for x in self["config"].list:
218                         x[1].save()
219
220         # keySave and keyCancel are just provided in case you need them.
221         # you have to call them by yourself.
222         def keySave(self):
223                 self.saveAll()
224                 self.close()
225         
226         def cancelConfirm(self, result):
227                 if not result:
228                         return
229
230                 for x in self["config"].list:
231                         x[1].cancel()
232                 self.close()
233
234         def keyCancel(self):
235                 if self["config"].isChanged():
236                         self.session.openWithCallback(self.cancelConfirm, MessageBox, _("Really close without saving settings?"))
237                 else:
238                         self.close()