Changed 'Dismiss' to 'Close' (Chris Shaffer).
[rox-lib.git] / python / rox / InfoWin.py
blob586516147712a678fb1d2f0b50b30504d22158c8
1 """Provide a standard window for displaying application information."""
3 import os
5 import rox
6 from rox import g
7 import webbrowser
9 class InfoWin(g.Dialog):
10 """Window to show app info"""
11 def __init__(self, program, purpose, version, author, website):
12 g.Dialog.__init__(self)
13 self.website=website
15 def close(iw, event=None, data=None):
16 iw.hide()
18 self.connect("delete_event", close)
20 hbox=g.HBox()
21 self.vbox.pack_start(hbox)
22 hbox.show()
24 try:
25 path=os.path.join(rox.app_dir, '.DirIcon')
26 pixbuf=g.gdk.pixbuf_new_from_file(path)
27 icon=g.Image()
28 icon.set_from_pixbuf(pixbuf)
29 hbox.pack_start(icon)
30 icon.show()
31 except:
32 #rox.report_exception()
33 pass
35 table=g.Table(5, 2)
36 hbox.pack_start(table)
38 label=g.Label("Program")
39 table.attach(label, 0, 1, 0, 1)
41 frame=g.Frame()
42 frame.set_shadow_type(g.SHADOW_IN)
43 table.attach(frame, 1, 2, 0, 1)
45 label=g.Label(program or '')
46 frame.add(label)
48 label=g.Label("Purpose")
49 table.attach(label, 0, 1, 1, 2)
51 frame=g.Frame()
52 frame.set_shadow_type(g.SHADOW_IN)
53 table.attach(frame, 1, 2, 1, 2)
55 label=g.Label(purpose or '')
56 frame.add(label)
58 label=g.Label("Version")
59 table.attach(label, 0, 1, 2, 3)
61 frame=g.Frame()
62 frame.set_shadow_type(g.SHADOW_IN)
63 table.attach(frame, 1, 2, 2, 3)
65 label=g.Label(version or '')
66 frame.add(label)
68 label=g.Label("Authors")
69 table.attach(label, 0, 1, 3, 4)
71 frame=g.Frame()
72 frame.set_shadow_type(g.SHADOW_IN)
73 table.attach(frame, 1, 2, 3, 4)
75 label=g.Label(author or '')
76 frame.add(label)
78 label=g.Label("Web site")
79 table.attach(label, 0, 1, 5, 6)
81 if website:
82 button=g.Button(website)
83 table.attach(button, 1, 2, 5, 6)
85 def goto_website(widget, iw):
86 webbrowser.open(iw.website)
88 button.connect("clicked", goto_website, self)
90 else:
91 frame=g.Frame()
92 frame.set_shadow_type(g.SHADOW_IN)
93 table.attach(frame, 1, 2, 5, 6)
95 hbox=self.action_area
97 button = g.Button(stock = g.STOCK_CLOSE)
98 hbox.pack_start(button)
100 def dismiss(widget, iw):
101 iw.hide()
103 button.connect("clicked", dismiss, self)
104 button.show()
106 self.vbox.show_all()
108 from xml.dom import Node, minidom, XML_NAMESPACE
109 import rox.i18n
111 def data(node):
112 """Return all the text directly inside this DOM Node."""
113 return ''.join([text.nodeValue for text in node.childNodes
114 if text.nodeType == Node.TEXT_NODE])
116 class AppInfo:
117 """Parsed AppInfo.xml file. Current only deals with the <About>
118 element"""
120 def __init__(self, source):
121 """Read the file and parse the <About> element."""
122 self.doc=minidom.parse(source)
123 self.about={}
125 for ab in self.doc.documentElement.getElementsByTagName('About'):
126 lang=ab.getAttributeNS(XML_NAMESPACE, 'lang')
127 self.about[lang]={}
129 for node in ab.childNodes:
130 if node.nodeType != Node.ELEMENT_NODE:
131 continue
132 name=node.localName
133 self.about[lang][name]=data(node)
135 def getAbout(self, elname, langs=None):
136 """Return an entry from the <About> section.
137 elname is the name of the element to return text from
138 langs is a list of acceptable languages, or None to use rox.i18n.langs
140 if langs is None:
141 langs=rox.i18n.langs
143 for lang in langs:
144 if self.about.has_key(lang):
145 if self.about[lang].has_key(elname):
146 return self.about[lang][elname]
148 if self.about[''].has_key(elname):
149 return self.about[''][elname]
151 return None
153 def infowin(pname, info=None):
154 """Open info window for this program. info is a source of the
155 AppInfo.xml file, if None then $APP_DIR/AppInfo.xml is loaded instead"""
157 if info is None:
158 info=os.path.join(rox.app_dir, 'AppInfo.xml')
160 try:
161 app_info=AppInfo(info)
162 except:
163 rox.report_exception()
164 return
166 try:
167 iw=InfoWin(pname, app_info.getAbout('Purpose'),
168 app_info.getAbout('Version'), app_info.getAbout('Authors'),
169 app_info.getAbout('Homepage'))
170 iw.show()
171 return iw
172 except:
173 rox.report_exception()