More fixes for creating/extracting directories.
[rox-archive.git] / box.py
blob4f1d77a967cb58671768f422e7e4309c3cc81795
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 build_main_area(self):
15 self.vbox.add(self.save_area)
17 self.operation = g.OptionMenu()
18 self.vbox.pack_start(self.operation, FALSE, TRUE, 0)
19 self.operation.show()
20 self.operation.set_border_width(5)
21 self.updating = 0
23 self.ops_menu = g.Menu()
24 self.operation.set_menu(self.ops_menu)
26 def set_data(self, data):
27 assert not self.data
29 self.data = data
30 self.ops = [op for op in formats.operations if op.can_handle(data)]
32 name = data.default_name
33 self.save_area.entry.set_text(name)
34 start = name.rfind('/') + 1
35 #self.save_area.entry.select_region(start, -1)
36 g.Editable.select_region(self.save_area.entry, start, -1)
38 last = None
40 # Add all supported ops to the menu.
41 for op in self.ops:
42 last = op
43 item = g.MenuItem(str(op))
44 item.show()
45 self.ops_menu.append(item)
47 # Select the default
48 try:
49 self.operation.set_history(self.ops.index(data.default))
50 except ValueError:
51 print >>sys.stderr, "Warning: %s not in ops list!" % data.default
52 pass
54 self.operation.connect('changed', self.op_changed)
55 self.save_area.entry.connect('changed', self.name_changed)
57 self.save_area.set_type(data.default.type)
59 def name_changed(self, entry):
60 if self.updating:
61 return
62 self.updating = 1
64 name = entry.get_text()
65 doc = self.save_area.document
66 i = 0
67 for op in self.ops:
68 if name.endswith('.' + op.extension):
69 self.operation.set_history(i)
70 break
71 i += 1
73 self.updating = 0
75 def op_changed(self, operation):
76 op = self.ops[operation.get_history()]
77 self.set_type(op.type)
79 if self.updating:
80 return
81 self.updating = 1
83 name = self.save_area.entry.get_text()
84 for op2 in self.ops:
85 if name.endswith('.' + op2.extension):
86 name = name[:-len(op2.extension)-1]
87 break
88 if op.add_extension:
89 name += '.' + op.extension
90 self.save_area.entry.set_text(name)
92 self.updating = 0
94 def save_to_file(self, path):
95 op = self.ops[self.operation.get_history()]
96 if hasattr(op, 'save_to_file'):
97 op.save_to_file(self.data, path)
98 else:
99 saving.Saveable.save_to_file(self, path)
101 def save_to_stream(self, stream):
102 op = self.ops[self.operation.get_history()]
103 op.save_to_stream(self.data, stream)