fix debug
[enigma2.git] / lib / python / Screens / Setup.py
1 from Screen import Screen
2 from MessageBox import MessageBox
3 from Components.ActionMap import NumberActionMap
4 from Components.config import config, KEY_LEFT, KEY_RIGHT, KEY_OK
5 from Components.ConfigList import ConfigList, ConfigListScreen
6 from Components.Label import Label
7 from Components.Pixmap import Pixmap
8
9 import xml.dom.minidom
10 from xml.dom import EMPTY_NAMESPACE
11 from skin import elementsWithTag
12
13 from Tools import XMLTools
14
15 # FIXME: use resolveFile!
16 # read the setupmenu
17 try:
18         # first we search in the current path
19         setupfile = file('data/setup.xml', 'r')
20 except:
21         # if not found in the current path, we use the global datadir-path
22         setupfile = file('/usr/share/enigma2/setup.xml', 'r')
23 setupdom = xml.dom.minidom.parseString(setupfile.read())
24 setupfile.close()
25
26 class SetupSummary(Screen):
27         skin = """
28         <screen position="6,0" size="120,64">
29                 <widget name="SetupTitle" position="6,0" size="120,16" font="Regular;12" />
30                 <widget name="SetupEntry" position="6,16" size="120,32" font="Regular;12" />
31                 <widget name="SetupValue" position="6,48" size="120,16" font="Regular;12" />
32         </screen>"""
33
34         def __init__(self, session, parent):
35                 Screen.__init__(self, session)
36                 self["SetupTitle"] = Label(_(parent.setup_title))
37                 self["SetupEntry"] = Label("")
38                 self["SetupValue"] = Label("")
39                 self.parent = parent
40                 self.onShow.append(self.addWatcher)
41                 self.onHide.append(self.removeWatcher)
42                 
43         def addWatcher(self):
44                 self.parent.onChangedEntry.append(self.selectionChanged)
45                 self.parent["config"].onSelectionChanged.append(self.selectionChanged)
46                 self.selectionChanged()
47         
48         def removeWatcher(self):
49                 self.parent.onChangedEntry.remove(self.selectionChanged)
50                 self.parent["config"].onSelectionChanged.remove(self.selectionChanged)
51
52         def selectionChanged(self):
53                 self["SetupEntry"].text = self.parent.getCurrentEntry()
54                 self["SetupValue"].text = self.parent.getCurrentValue()
55
56 class Setup(ConfigListScreen, Screen):
57
58         ALLOW_SUSPEND = True
59
60         def __init__(self, session, setup):
61                 Screen.__init__(self, session)
62
63                 xmldata = setupdom.childNodes[0]
64                 
65                 entries = xmldata.childNodes
66
67                 self.onChangedEntry = [ ]
68                 list = []
69                                 
70                 for x in entries:             #walk through the actual nodelist
71                         if x.nodeType != xml.dom.minidom.Element.nodeType:
72                                 continue
73                         elif x.tagName == 'setup':
74                                 if x.getAttribute("key") != setup:
75                                         continue
76                                 self.addItems(list, x.childNodes);
77                                 myTitle = x.getAttribute("title").encode("UTF-8")
78
79                 #check for list.entries > 0 else self.close
80                 
81                 self.setup_title = myTitle
82                 self["title"] = Label(_(self.setup_title))
83
84                 self["oktext"] = Label(_("OK"))
85                 self["canceltext"] = Label(_("Cancel"))
86                 self["ok"] = Pixmap()
87                 self["cancel"] = Pixmap()
88                 
89                 self["actions"] = NumberActionMap(["SetupActions"], 
90                         {
91                                 "cancel": self.keyCancel,
92                                 "save": self.keySave,
93                         }, -2)
94
95                 ConfigListScreen.__init__(self, list, session = session, on_change = self.changedEntry)
96
97                 self.changedEntry()
98
99         # for summary:
100         def changedEntry(self):
101                 for x in self.onChangedEntry:
102                         x()
103
104         def getCurrentEntry(self):
105                 return self["config"].getCurrent()[0]
106
107         def getCurrentValue(self):
108                 return str(self["config"].getCurrent()[1].getText())
109
110         def createSummary(self):
111                 return SetupSummary
112
113         def addItems(self, list, childNode):
114                 for x in childNode:
115                         if x.nodeType != xml.dom.minidom.Element.nodeType:
116                                 continue
117                         elif x.tagName == 'item':
118                                 item_text = _(x.getAttribute("text").encode("UTF-8") or "??")
119                                 b = eval(XMLTools.mergeText(x.childNodes));
120                                 if b == "":
121                                         continue
122                                 #add to configlist
123                                 item = b
124                                 # the first b is the item itself, ignored by the configList.
125                                 # the second one is converted to string.
126                                 list.append( (item_text, item) )
127
128         def keySave(self):
129                 print "save requested"
130                 for x in self["config"].list:
131                         x[1].save()
132                 self.close()
133         
134         def cancelConfirm(self, result):
135                 if not result:
136                         return
137
138                 print "cancel requested"
139                 
140                 for x in self["config"].list:
141                         x[1].cancel()
142                 self.close()
143
144         def keyCancel(self):
145                 is_changed = False
146                 for x in self["config"].list:
147                         is_changed |= x[1].isChanged()
148                 
149                 if is_changed:
150                         self.session.openWithCallback(self.cancelConfirm, MessageBox, _("Really close without saving settings?"))
151                 else:
152                         self.close()
153
154 def getSetupTitle(id):
155         xmldata = setupdom.childNodes[0].childNodes
156         for x in elementsWithTag(xmldata, "setup"):
157                 if x.getAttribute("key") == id:
158                         return x.getAttribute("title").encode("UTF-8")