Start development series 2.3-post
[rox-archive.git] / box.py
blob7076067f4d2b90897fb9ba9180249288cb2dafb3
1 import sys, os
3 import rox
4 from rox import saving, g
6 import formats
8 use_combo_box = hasattr(g, 'ComboBox')
10 class ArchiveBox(saving.SaveBox, saving.Saveable):
11 "A savebox with an operation selector."
12 def __init__(self):
13 self.data = None
14 self.operation = None
15 self.ops = None
16 saving.SaveBox.__init__(self, self, '', 'text/plain')
18 def save_cancelled(self):
19 assert formats.current_command
20 formats.current_command.kill()
22 def set_save_in_progress(self, busy):
23 if self.operation:
24 self.operation.set_sensitive(not busy)
25 saving.SaveBox.set_save_in_progress(self, busy)
27 def build_main_area(self):
28 self.vbox.add(self.save_area)
30 frame = g.Frame(None)
31 frame.set_shadow_type(g.SHADOW_NONE)
32 if use_combo_box:
33 self.operation = g.combo_box_new_text()
34 else:
35 self.operation = g.OptionMenu()
36 frame.add(self.operation)
37 self.vbox.pack_start(frame, False, True, 0)
38 frame.set_border_width(3)
39 frame.show_all()
40 self.updating = 0
42 self.delete_choice = g.CheckButton(_('_Delete file afterwards?'))
43 self.vbox.pack_start(self.delete_choice, True, True, 0)
45 if not use_combo_box:
46 self.ops_menu = g.Menu()
47 self.operation.set_menu(self.ops_menu)
48 else:
49 self.ops_menu = None
51 def set_data(self, data):
52 assert not self.data
54 self.data = data
55 self.save_mode = data.mode
56 ops = [op for op in formats.operations if op.can_handle(data)]
58 name = data.default_name
59 self.save_area.entry.set_text(name)
60 start = name.rfind('/') + 1
61 #self.save_area.entry.select_region(start, -1)
62 g.Editable.select_region(self.save_area.entry, start, -1)
64 # Add all supported ops to the menu.
65 self.ops = []
66 if use_combo_box:
67 for op in ops:
68 self.operation.append_text(str(op))
69 self.ops.append(op)
70 try:
71 self.operation.set_active(self.ops.index(data.default))
72 except ValueError:
73 print >>sys.stderr, "Warning: %s not in ops list!" % data.default
75 else:
76 last = None
77 for op in ops:
78 if last and last != op.__class__:
79 item = g.MenuItem()
80 item.show()
81 self.ops_menu.append(item)
82 self.ops.append(None)
83 last = op.__class__
84 item = g.MenuItem(str(op))
85 item.show()
86 self.ops_menu.append(item)
87 self.ops.append(op)
89 # Select the default
90 try:
91 self.operation.set_history(self.ops.index(data.default))
92 except ValueError:
93 print >>sys.stderr, "Warning: %s not in ops list!" % data.default
95 self.operation.connect('changed', self.op_changed)
96 self.save_area.entry.connect('changed', self.name_changed)
98 self.save_area.set_type(data.default.type)
100 if isinstance(data, formats.FileData) and data.path != '-':
101 self.delete_choice.show()
103 def name_changed(self, entry):
104 if self.updating:
105 return
106 self.updating = 1
108 name = entry.get_text()
109 i = 0
110 for op in self.ops:
111 if op and name.endswith('.' + op.extension):
112 if use_combo_box:
113 self.operation.set_active(i)
114 else:
115 self.operation.set_history(i)
116 break
117 i += 1
119 self.updating = 0
121 def get_selected_op(self):
122 if use_combo_box:
123 return self.ops[self.operation.get_active()]
124 else:
125 return self.ops[self.operation.get_history()]
127 def op_changed(self, operation):
128 op = self.get_selected_op()
129 assert op
130 self.set_type(op.type)
132 if self.updating:
133 return
134 self.updating = 1
136 name = self.save_area.entry.get_text()
137 for op2 in self.ops:
138 if op2 and name.endswith('.' + op2.extension):
139 name = name[:-len(op2.extension)-1]
140 break
141 if op.add_extension:
142 name += '.' + op.extension
143 self.save_area.entry.set_text(name)
144 start = name.rfind('/') + 1
145 #self.save_area.entry.select_region(start, -1)
146 g.Editable.select_region(self.save_area.entry, start, -1)
148 self.updating = 0
150 def save_to_file(self, path):
151 op = self.get_selected_op()
152 if hasattr(op, 'save_to_file'):
153 op.save_to_file(self.data, path)
154 else:
155 saving.Saveable.save_to_file(self, path)
157 #If the delete_choice is set, try to remove the file
158 if self.delete_choice.get_active():
159 try:
160 os.remove(sys.argv[1])
161 except:
162 rox.report_exception()
164 def can_save_to_selection(self):
165 if self.ops:
166 op = self.get_selected_op()
168 if op.type == 'inode/directory':
169 return False
170 return saving.Saveable.can_save_to_selection(self)
172 def save_to_stream(self, stream):
173 op = self.get_selected_op()
174 op.save_to_stream(self.data, stream)