Don't report an error twice if nothing was extracted.
[rox-archive.git] / box.py
blobff6f08b498aaa2c594ab45318ecfbccaa0129051
1 import sys, os
3 import rox
4 from rox import saving, g
6 import formats
8 class ArchiveBox(saving.SaveBox, saving.Saveable):
9 "A savebox with an operation selector."
10 def __init__(self):
11 self.data = None
12 self.operation = None
13 saving.SaveBox.__init__(self, self, '', 'text/plain')
15 def save_cancelled(self):
16 assert formats.current_command
17 formats.current_command.kill()
19 def set_save_in_progress(self, busy):
20 if self.operation:
21 self.operation.set_sensitive(not busy)
22 saving.SaveBox.set_save_in_progress(self, busy)
24 def build_main_area(self):
25 self.vbox.add(self.save_area)
27 self.operation = g.OptionMenu()
28 self.vbox.pack_start(self.operation, False, True, 0)
29 self.operation.show()
30 self.operation.set_border_width(5)
31 self.updating = 0
33 self.delete_choice = g.CheckButton('Delete file afterwards?')
34 self.vbox.pack_start(self.delete_choice, True, True, 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 self.save_mode = data.mode
44 ops = [op for op in formats.operations if op.can_handle(data)]
46 name = data.default_name
47 self.save_area.entry.set_text(name)
48 start = name.rfind('/') + 1
49 #self.save_area.entry.select_region(start, -1)
50 g.Editable.select_region(self.save_area.entry, start, -1)
52 # Add all supported ops to the menu.
53 self.ops = []
54 last = None
55 for op in ops:
56 if last and last != op.__class__:
57 item = g.MenuItem()
58 item.show()
59 self.ops_menu.append(item)
60 self.ops.append(None)
61 last = op.__class__
62 item = g.MenuItem(str(op))
63 item.show()
64 self.ops_menu.append(item)
65 self.ops.append(op)
67 # Select the default
68 try:
69 self.operation.set_history(self.ops.index(data.default))
70 except ValueError:
71 print >>sys.stderr, "Warning: %s not in ops list!" % data.default
73 self.operation.connect('changed', self.op_changed)
74 self.save_area.entry.connect('changed', self.name_changed)
76 self.save_area.set_type(data.default.type)
78 if isinstance(data, formats.FileData) and data.path != '-':
79 self.delete_choice.show()
81 def name_changed(self, entry):
82 if self.updating:
83 return
84 self.updating = 1
86 name = entry.get_text()
87 i = 0
88 for op in self.ops:
89 if op and name.endswith('.' + op.extension):
90 self.operation.set_history(i)
91 break
92 i += 1
94 self.updating = 0
96 def op_changed(self, operation):
97 op = self.ops[operation.get_history()]
98 assert op
99 self.set_type(op.type)
101 if self.updating:
102 return
103 self.updating = 1
105 name = self.save_area.entry.get_text()
106 for op2 in self.ops:
107 if op2 and name.endswith('.' + op2.extension):
108 name = name[:-len(op2.extension)-1]
109 break
110 if op.add_extension:
111 name += '.' + op.extension
112 self.save_area.entry.set_text(name)
113 start = name.rfind('/') + 1
114 #self.save_area.entry.select_region(start, -1)
115 g.Editable.select_region(self.save_area.entry, start, -1)
117 self.updating = 0
119 def save_to_file(self, path):
120 op = self.ops[self.operation.get_history()]
121 if hasattr(op, 'save_to_file'):
122 op.save_to_file(self.data, path)
123 else:
124 saving.Saveable.save_to_file(self, path)
126 #If the delete_choice is set, try to remove the file
127 if self.delete_choice.get_active():
128 try:
129 os.remove(sys.argv[1])
130 except:
131 rox.report_exception()
133 def save_to_stream(self, stream):
134 op = self.ops[self.operation.get_history()]
135 op.save_to_stream(self.data, stream)