Ref the key. This actually is necessary.
[python-gnt.git] / example / xmms / gx
blob8daa0c06fa98f8fc4dfc787c42bb3c67bef9970b
1 #!/usr/bin/env python
3 import xmmsclient
4 import xmmsclient.glib as xg
5 import xmmsclient.collections as xc
6 import os
7 import os.path as path
9 import sys
10 from xml.dom.ext.reader import Sax2
11 from xml.dom.NodeFilter import NodeFilter
12 import gnt
14 from xmms_search import XSearch
15 from xmms_eq import XQSettings
16 from xmms_pl import XPList, XPListCurrent
17 from xmms_status import XStatus
18 from xmms_config import XConfig
20 import common
22 gui = """
23 <windows>
24 <window name='Main Window' vertical='True' homo='False' maximize='vert' start='True'>
25 <title>XMMS2</title>
26 <widgets>
27 <status title='show' quality='show' album='show'/>
28 <playlist current='True' />
29 <box vertical='False'>
30 <widgets>
31 <search />
32 <config />
33 </widgets>
34 </box>
35 </widgets>
36 </window>
37 <window name='Basic Search' vertical='True' homo='False' start='False'>
38 <title>Search...</title>
39 <widgets>
40 <box vertical='False'>
41 <widgets>
42 <search />
43 <equalizer />
44 </widgets>
45 </box>
46 </widgets>
47 </window>
48 <window name='Equalizer'>
49 <title>Equalizer</title>
50 <widgets>
51 <equalizer />
52 </widgets>
53 </window>
54 <window name='Playlist Editor' maximize='vert'>
55 <title>Playlist Editor</title>
56 <widgets>
57 <playlist position='hide' album='hide'/>
58 </widgets>
59 </window>
60 <window name='Configuration Editor' maximize='vert' start='False'>
61 <title>Configuration Editor</title>
62 <widgets>
63 <config />
64 </widgets>
65 </window>
66 </windows>
67 """
69 def setup_xmms():
70 try:
71 xmms = xmmsclient.XMMS("uber-cool")
72 xmms.connect(os.getenv("XMMS_PATH"))
73 conn = xg.GLibConnector(xmms)
74 return xmms
75 except IOError, detail:
76 common.show_error("Connection failed: " + str(detail))
77 return None
79 xmms = setup_xmms()
81 windows = []
83 class WidgetNode:
84 def __init__(self, node, parent = None):
85 self.children = None
86 self.parent = parent
87 self.node = node
88 self.widget = None
90 def process(self):
91 if self.widget is not None:
92 return
93 self.widget = self.create_widget()
94 def reset_widget(w):
95 self.widget = None
96 self.widget.connect('destroy', reset_widget)
97 for attrnode in self.node.attributes:
98 value = attrnode.nodeValue
99 if value == 'False':
100 value = False
101 self.process_attrib(attrnode.nodeName, value)
102 for child in self.node.childNodes:
103 self.process_child(child)
105 def create_widget(self):
106 return None
108 def process_attrib(self, name, value):
109 pass
111 def process_child(self, child):
112 pass
114 def done(self):
115 pass
117 class BoxNode(WidgetNode):
118 def create_widget(self):
119 return gnt.Box(False, False)
121 def process_attrib(self, name, value):
122 if name == 'vertical':
123 self.widget.set_property('vertical', bool(value))
124 elif name == 'homo':
125 self.widget.set_property('homogeneous', bool(value))
127 def process_child(self, child):
128 if child.nodeName == 'title':
129 self.widget.set_title(child.firstChild.nodeValue)
130 elif child.nodeName == 'widgets':
131 for widget in child.childNodes:
132 wn = process_node(widget, self)
133 if wn is not None:
134 wn.process()
135 if wn.widget is not None:
136 self.widget.add_widget(wn.widget)
138 class WindowNode(BoxNode):
139 def __init__(self, node, parent):
140 self.start = False
141 BoxNode.__init__(self, node, parent)
142 for attrnode in self.node.attributes:
143 value = attrnode.nodeValue
144 if attrnode.nodeName == 'start':
145 if value != 'False':
146 self.process()
147 elif attrnode.nodeName == 'name':
148 self.name = value
150 def create_widget(self):
151 return gnt.Window()
153 def process_attrib(self, name, value):
154 BoxNode.process_attrib(self, name, value)
155 if name == 'maximize':
156 if value == 'both':
157 self.widget.set_maximize(gnt.WINDOW_MAXIMIZE_Y | gnt.WINDOW_MAXIMIZE_X)
158 elif value == 'horiz':
159 self.widget.set_maximize(gnt.WINDOW_MAXIMIZE_X)
160 elif value == 'vert':
161 self.widget.set_maximize(gnt.WINDOW_MAXIMIZE_Y)
162 elif name == 'name':
163 self.name = value
164 elif name == 'start':
165 self.start = value
167 def done(self):
168 windows.append(self)
170 class SearchNode(WidgetNode):
171 def create_widget(self):
172 return XSearch(xmms)
174 class EqualizerNode(WidgetNode):
175 def __init__(self, node, parent):
176 WidgetNode.__init__(self, node, parent)
177 while parent.parent is not None:
178 parent = parent.parent
179 self.widget.win = parent.widget
181 def create_widget(self):
182 return XQSettings(xmms)
184 class PlaylistNode(WidgetNode):
185 def process_attrib(self, name, value):
186 hash = {'position': XPList.POSITION,
187 'title' : XPList.TITLE,
188 'album' : XPList.ALBUM,
189 'artist' : XPList.ARTIST,
190 'time' : XPList.TIME,
192 if name in hash:
193 col = hash[name]
194 if value == 'hide':
195 self.widget.hide_column(col)
197 def create_widget(self):
198 for attr in self.node.attributes:
199 if attr.nodeName == 'current':
200 return XPListCurrent(xmms)
201 return XPList(xmms)
203 class StatusNode(WidgetNode):
204 def create_widget(self):
205 return XStatus(xmms)
207 def process_attrib(self, name, value):
208 hash = {'title': XStatus.PROP_TITLE,
209 'album' : XStatus.PROP_ALBUM,
210 'artist' : XStatus.PROP_ARTIST,
211 'time' : XStatus.PROP_DURATION,
212 'quality' : XStatus.PROP_QUALITY,
213 'url' : XStatus.PROP_URL
215 if name in hash:
216 field = hash[name]
217 if value == 'hide':
218 self.widget.fields = self.widget.fields & ~field
219 elif value == 'show':
220 self.widget.fields = self.widget.fields | field
221 self.widget.update_fields()
223 class ConfigNode(WidgetNode):
224 def create_widget(self):
225 return XConfig(xmms)
227 maps = {
228 'window' : WindowNode,
229 'search' : SearchNode,
230 'equalizer' : EqualizerNode,
231 'playlist' : PlaylistNode,
232 'status' : StatusNode,
233 'box' : BoxNode,
234 'config' : ConfigNode,
237 def process_node(node, parent, dep = 0):
238 if node.nodeType in [node.DOCUMENT_NODE]:
239 for child in node.childNodes:
240 process_node(child, parent, dep)
241 elif node.nodeType == node.ELEMENT_NODE:
242 if node.nodeName in maps:
243 type = maps[node.nodeName]
244 ret = type(node, parent)
245 ret.done()
246 return ret
247 print "\t" * dep, node.nodeName,
248 print "[",
249 for attr in node.attributes.keys():
250 print node.attributes[attr].nodeValue,
251 print "]"
252 for child in node.childNodes:
253 process_node(child, parent, dep + 1)
254 return None
256 def read_gui(gui):
257 xml = path.expanduser("~/.config/xmms2/clients/pygnt.xml")
258 if not path.exists(xml): return gui
259 file = open(xml, "r")
260 if file:
261 gui = file.read()
262 file.close()
263 return gui
265 if xmms:
266 gui = read_gui(gui)
267 reader = Sax2.Reader()
268 doc = reader.fromString(gui)
269 process_node(doc, None)
271 def create_menu(winnode):
272 def show_winnode(item, win):
273 win.process()
274 win.widget.set_menu(create_menu(win))
275 win.widget.show()
276 win.widget.present()
278 menu = gnt.Menu(gnt.MENU_TOPLEVEL)
279 item = gnt.MenuItem("Windows")
280 menu.add_item(item)
281 sub = gnt.Menu(gnt.MENU_POPUP)
282 item.set_submenu(sub)
283 for window in windows:
284 if window == winnode:
285 continue
286 item = gnt.MenuItem(window.name)
287 sub.add_item(item)
288 item.connect('activate', show_winnode, window)
289 return menu
291 for window in windows:
292 if window.start:
293 window.widget.set_menu(create_menu(window))
294 window.widget.show()
296 gnt.gnt_main()
297 gnt.gnt_quit()