Lots of code tidying.
[rox-archive.git] / box.py
blobc7320f9fdf3b85748e23d2f81a6b993c1bcbe8be
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 def name_changed(self, entry):
58 if self.updating:
59 return
60 self.updating = 1
62 name = entry.get_text()
63 doc = self.save_area.document
64 i = 0
65 for op in self.ops:
66 if name.endswith('.' + op.extension):
67 self.operation.set_history(i)
68 break
69 i += 1
71 self.updating = 0
73 def op_changed(self, operation):
74 op = self.ops[operation.get_history()]
75 self.set_type(op.type)
77 if self.updating:
78 return
79 self.updating = 1
81 name = self.save_area.entry.get_text()
82 for op in self.ops:
83 if name.endswith('.' + op.extension):
84 name = name[:-len(op.extension)-1]
85 break
86 if op.add_extension:
87 name += '.' + op.extension
88 self.save_area.entry.set_text(name)
90 self.updating = 0
92 def save_to_file(self, path):
93 op = self.ops[self.operation.get_history()]
94 if hasattr(op, 'save_to_file'):
95 op.save_to_file(self.data, path)
96 else:
97 saving.Saveable.save_to_file(self, path)
99 def save_to_stream(self, stream):
100 op = self.ops[self.operation.get_history()]
101 op.save_to_stream(self.data, stream)