3 __all__
= ['infowin', 'InfoWin']
8 from rox
import g
, AppInfo
10 _url_re
= re
.compile(r
'(URL:)?(\w+?://\S+)')
12 def _collect_about(ai
):
13 """Extract about information from AppInfo object in the current language."""
16 for key
in ai
.about
[''].keys():
17 k
, v
= ai
.getAbout(key
)
19 v
= ai
.getAbout(key
, [''])[1]
20 info
.append((k
, v
.strip()))
23 def infowin(pname
, info
=None):
24 """Open info window for this program.
26 info is a source of an AppInfo.xml file, if None then
27 APP_DIR/AppInfo.xml is loaded instead.
31 info
= os
.path
.join(rox
.app_dir
, 'AppInfo.xml')
33 ai
= AppInfo
.AppInfo(info
)
35 rox
.report_exception()
38 info
= _collect_about(ai
)
39 if os
.path
.isdir(os
.path
.join(rox
.app_dir
, 'Help')):
44 iw
= InfoWin(pname
, fields
=info
, help=help)
45 iw
.set_title(_("About"))
49 rox
.report_exception()
53 def key_event(self
, window
, kev
):
54 if kev
.keyval
== g
.keysyms
.Escape
:
57 elif kev
.keyval
in [g
.keysyms
.Return
, g
.keysyms
.KP_Enter
] \
58 and hasattr(self
, '_ok_cb'):
68 def close(self
, widget
=None, *args
):
74 class InfoWin(CommonMixIn
, rox
.Dialog
):
75 """A dialog showing information such as author, version, etc."""
77 def __init__(self
, pname
, fields
=None, icon
=None, help=False, standalone
=False):
78 rox
.Dialog
.__init
__(self
)
82 "'fields' keyword must have at least one label, value pair."
85 self
.standalone
= standalone
87 self
.set_position(g
.WIN_POS_CENTER
)
88 self
.set_default_size(350,-1)
89 self
.connect("delete_event", self
.close
)
90 self
.connect('key-press-event', self
.key_event
)
91 self
.set_title(_("Info"))
92 tooltips
= g
.Tooltips()
95 self
.vbox
.pack_start(hbox
, False, False, 5)
100 icon
= os
.path
.join(rox
.app_dir
, '.DirIcon')
101 pixbuf
= g
.gdk
.pixbuf_new_from_file(icon
)
102 scaled_pixbuf
= pixbuf
.scale_simple(self
.icon_size
,
103 self
.icon_size
, g
.gdk
.INTERP_BILINEAR
)
105 ic
.set_from_pixbuf(scaled_pixbuf
)
113 name
.set_markup('<span size="xx-large" weight="bold">%s</span>' % pname
)
116 # use table to pack labels and descriptions
117 table
= g
.Table(len(fields
), 2)
118 self
.vbox
.pack_start(table
, padding
=3)
120 for i
, (fieldname
, fieldtext
) in enumerate(fields
):
121 label
= g
.Label("%s:" % fieldname
)
122 label
.set_alignment(1,0)
123 table
.attach(label
, 0, 1, i
, i
+1, xpadding
=2)
125 # search for URLs or email addresses
126 if fieldtext
.find('@') > 0:
127 self
.urls
[fieldname
] = []
128 from parseaddrlist
import parseaddrlist
129 for name
, address
in parseaddrlist(fieldtext
):
130 if not address
: continue
133 self
.urls
[fieldname
].append((name
, 'mailto:%s' % address
))
135 ul
= _url_re
.findall(fieldtext
)
137 self
.urls
[fieldname
] = [(fieldtext
, x
[1]) for x
in ul
]
140 frame
.set_shadow_type(g
.SHADOW_IN
)
141 table
.attach(frame
, 1, 2, i
, i
+1, xpadding
=0)
142 urls
= self
.urls
.get(fieldname
)
146 for j
, (text
, url
) in enumerate(urls
):
149 '<span foreground="blue"><u>%s</u></span>' % text
)
152 evb
.set_events(g
.gdk
.BUTTON_PRESS_MASK
)
153 evb
.connect("button_press_event", self
.open_url
,
157 evb
.window
.set_cursor(g
.gdk
.Cursor(g
.gdk
.HAND1
))
158 tooltips
.set_tip(evb
,
159 _("Open URL <%s> with the default URL handler.") % url
,
163 label
.set_text(fieldtext
)
164 label
.set_selectable(True)
166 label
.set_alignment(0.5,0)
167 label
.set_justify(g
.JUSTIFY_CENTER
)
168 label
.set_line_wrap(True)
173 self
.add_button(g
.STOCK_HELP
, g
.RESPONSE_HELP
)
174 self
.add_button(g
.STOCK_CLOSE
, g
.RESPONSE_CLOSE
)
175 self
.connect('response', self
.response_cb
)
176 self
.set_default_response(g
.RESPONSE_CLOSE
)
178 def open_url(self
, widget
, event
, name
, idx
):
179 """Open the url associated with a label with ROX url handler."""
182 url
= self
.urls
[name
][idx
][1]
185 from rox
import basedir
, filer
186 app
= basedir
.load_first_config('rox.sourceforge.net',
187 'MIME-types', 'text_x-uri')
189 if os
.path
.islink(app
):
190 app
= os
.readlink(app
)
191 if os
.path
.isdir(app
):
192 app
= os
.path
.join(app
, 'AppRun')
193 filer
._spawn
((app
, url
))
194 except (OSError, IOError):
195 import threading
, webbrowser
196 t
= threading
.Thread(target
=webbrowser
.open, args
=(url
, 1))
200 rox
.report_exception()
202 def response_cb(self
, widget
, id, *args
):
203 """Callback for dialog buttons."""
205 if id == g
.RESPONSE_CLOSE
:
207 elif id == g
.RESPONSE_HELP
:
210 def open_help(self
, widget
=None, *args
):
211 """Open the Help directory with the filer."""
213 from rox
import filer
214 helpdir
= os
.path
.join(rox
.app_dir
, 'Help')
215 if os
.path
.isdir(helpdir
):
216 filer
.open_dir(helpdir
)