Allow operations to be aborted.
[rox-archive.git] / box.py
blob2f80e1fb19c68fb8c16380b2441171921e99feb8
1 import sys
3 import rox
4 from rox import saving, g, TRUE, FALSE
6 import formats
8 class ArchiveBox(saving.SaveBox, saving.Saveable):
9 "A savebox with an operation selector."
10 def __init__(self):
11 saving.SaveBox.__init__(self, self, '', 'text/plain')
12 self.data = None
14 def destroyed(self):
15 self.operation = None
16 if formats.current_command:
17 formats.current_command.kill()
18 self.connect('destroy', destroyed)
20 def set_in_progress(self, busy):
21 if self.operation:
22 self.operation.set_sensitive(not busy)
23 self.set_response_sensitive(g.RESPONSE_OK, not busy)
25 in_progress = property(None, set_in_progress, None)
27 def build_main_area(self):
28 self.vbox.add(self.save_area)
30 self.operation = g.OptionMenu()
31 self.vbox.pack_start(self.operation, FALSE, TRUE, 0)
32 self.operation.show()
33 self.operation.set_border_width(5)
34 self.updating = 0
36 self.ops_menu = g.Menu()
37 self.operation.set_menu(self.ops_menu)
39 def set_data(self, data):
40 assert not self.data
42 self.data = data
43 ops = [op for op in formats.operations if op.can_handle(data)]
45 name = data.default_name
46 self.save_area.entry.set_text(name)
47 start = name.rfind('/') + 1
48 #self.save_area.entry.select_region(start, -1)
49 g.Editable.select_region(self.save_area.entry, start, -1)
51 # Add all supported ops to the menu.
52 self.ops = []
53 last = None
54 for op in ops:
55 if last and last != op.__class__:
56 item = g.MenuItem()
57 item.show()
58 self.ops_menu.append(item)
59 self.ops.append(None)
60 last = op.__class__
61 item = g.MenuItem(str(op))
62 item.show()
63 self.ops_menu.append(item)
64 self.ops.append(op)
66 # Select the default
67 try:
68 self.operation.set_history(self.ops.index(data.default))
69 except ValueError:
70 print >>sys.stderr, "Warning: %s not in ops list!" % data.default
72 self.operation.connect('changed', self.op_changed)
73 self.save_area.entry.connect('changed', self.name_changed)
75 self.save_area.set_type(data.default.type)
77 def name_changed(self, entry):
78 if self.updating:
79 return
80 self.updating = 1
82 name = entry.get_text()
83 i = 0
84 for op in self.ops:
85 if op and name.endswith('.' + op.extension):
86 self.operation.set_history(i)
87 break
88 i += 1
90 self.updating = 0
92 def op_changed(self, operation):
93 op = self.ops[operation.get_history()]
94 assert op
95 self.set_type(op.type)
97 if self.updating:
98 return
99 self.updating = 1
101 name = self.save_area.entry.get_text()
102 for op2 in self.ops:
103 if op2 and name.endswith('.' + op2.extension):
104 name = name[:-len(op2.extension)-1]
105 break
106 if op.add_extension:
107 name += '.' + op.extension
108 self.save_area.entry.set_text(name)
110 self.updating = 0
112 def save_to_file(self, path):
113 op = self.ops[self.operation.get_history()]
114 if hasattr(op, 'save_to_file'):
115 self.in_progress = 1
116 try:
117 op.save_to_file(self.data, path)
118 finally:
119 self.in_progress = 0
120 else:
121 saving.Saveable.save_to_file(self, path)
123 def save_to_stream(self, stream):
124 self.in_progress = 1
125 try:
126 op = self.ops[self.operation.get_history()]
127 op.save_to_stream(self.data, stream)
128 finally:
129 self.in_progress = 0