Removed the pyc files (oops!).
[rox-archive.git] / support.py
blob04905af1b6221c7f8609e296d8c565c355d3e3bc
1 from os import _exit, fork, execvp, waitpid, dup2
3 import sys
4 from traceback import format_exception_only
6 from string import find, lower, join
7 from socket import gethostbyaddr, gethostname
9 from gtk import *
11 bad_xpm = [
12 "12 12 3 1",
13 " c #000000000000",
14 ". c #FFFF00000000",
15 "X c #FFFFFFFFFFFF",
16 " ",
17 " ..XXXXXX.. ",
18 " ...XXXX... ",
19 " X...XX...X ",
20 " XX......XX ",
21 " XXX....XXX ",
22 " XXX....XXX ",
23 " XX......XX ",
24 " X...XX...X ",
25 " ...XXXX... ",
26 " ..XXXXXX.. ",
27 " "]
29 def load_pixmap(window, path):
30 try:
31 p, m = create_pixmap_from_xpm(window, None, path)
32 except:
33 print "Warning: failed to load icon '%s'" % path
34 p, m = create_pixmap_from_xpm_d(window, None, bad_xpm)
35 return p, m
37 _host_name = None
38 def our_host_name():
39 global _host_name
40 if _host_name:
41 return _host_name
42 (host, alias, ips) = gethostbyaddr(gethostname())
43 for name in [host] + alias:
44 if find(name, '.') != -1:
45 _host_name = name
46 return name
47 return name
49 def get_local_path(uri):
50 "Convert uri to a local path and return, if possible. Otherwise,"
51 "return None."
52 host = our_host_name()
54 if not uri:
55 return None
57 if uri[0] == '/':
58 if uri[1] != '/':
59 return uri # A normal Unix pathname
60 i = find(uri, '/', 2)
61 if i == -1:
62 return None # //something
63 if i == 2:
64 return uri[2:] # ///path
65 remote_host = uri[2:i]
66 if remote_host == host:
67 return uri[i:] # //localhost/path
68 # //otherhost/path
69 elif lower(uri[:5]) == 'file:':
70 if uri[5:6] == '/':
71 return get_local_path(uri[5:])
72 elif uri[:2] == './' or uri[:3] == '../':
73 return uri
74 return None
76 # Open a modal dialog box showing a message.
77 # The user can choose from a selection of buttons at the bottom.
78 # Returns -1 if the window is destroyed, or the number of the button
79 # if one is clicked (starting from zero).
81 # If a dialog is already open, returns -1 without waiting AND
82 # brings the current dialog to the front.
83 current_dialog = None
84 def get_choice(message, title, buttons):
85 global current_dialog, choice_return
87 if current_dialog:
88 current_dialog.hide()
89 current_dialog.show()
90 return -1
92 current_dialog = GtkWindow(WINDOW_DIALOG)
93 current_dialog.unset_flags(CAN_FOCUS)
94 current_dialog.set_modal(TRUE)
95 current_dialog.set_title(title)
96 current_dialog.set_position(WIN_POS_CENTER)
97 current_dialog.set_border_width(2)
99 vbox = GtkVBox(FALSE, 0)
100 current_dialog.add(vbox)
101 action_area = GtkHBox(TRUE, 5)
102 action_area.set_border_width(2)
103 vbox.pack_end(action_area, FALSE, TRUE, 0)
104 vbox.pack_end(GtkHSeparator(), FALSE, TRUE, 2)
106 text = GtkLabel(message)
107 text.set_line_wrap(TRUE)
108 text_container = GtkEventBox()
109 text_container.set_border_width(40) # XXX
110 text_container.add(text)
112 vbox.pack_start(text_container, TRUE, TRUE, 0)
114 default_button = None
115 n = 0
116 for b in buttons:
117 label = GtkLabel(b)
118 label.set_padding(16, 2)
119 button = GtkButton()
120 button.add(label)
121 button.set_flags(CAN_DEFAULT)
122 action_area.pack_start(button, TRUE, TRUE, 0)
123 def cb(widget, n = n):
124 global choice_return
125 choice_return = n
126 button.connect('clicked', cb)
127 if not default_button:
128 default_button = button
129 n = n + 1
131 default_button.grab_focus()
132 default_button.grab_default()
133 action_area.set_focus_child(default_button)
135 def cb(widget):
136 global choice_return
137 choice_return = -1
138 current_dialog.connect('destroy', cb)
140 choice_return = -2
142 current_dialog.show_all()
144 while choice_return == -2:
145 mainiteration(TRUE)
147 retval = choice_return
149 if retval != -1:
150 current_dialog.destroy()
152 current_dialog = None
154 return retval
156 def report_error(message, title = 'Error'):
157 get_choice(message, title, ['OK'])
159 def report_exception():
160 ex = format_exception_only(sys.exc_type, sys.exc_value)
161 report_error(join(ex, ''))
163 def child(*argv):
164 """Spawn a new process. Connect stderr to stdout."""
165 child = fork()
166 if child == 0:
167 # We are the child
168 try:
169 dup2(1, 2)
170 execvp(argv[0], argv)
171 except:
172 pass
173 print "Warning: exec('%s') failed!" % argv[0]
174 _exit(1)
175 elif child == -1:
176 print "Error: fork() failed!"