4 from rox
import saving
, g
8 use_combo_box
= hasattr(g
, 'ComboBox')
10 class ArchiveBox(saving
.SaveBox
, saving
.Saveable
):
11 "A savebox with an operation selector."
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
):
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
)
31 frame
.set_shadow_type(g
.SHADOW_NONE
)
33 self
.operation
= g
.combo_box_new_text()
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)
42 self
.delete_choice
= g
.CheckButton(_('Delete file afterwards?'))
43 self
.vbox
.pack_start(self
.delete_choice
, True, True, 0)
46 self
.ops_menu
= g
.Menu()
47 self
.operation
.set_menu(self
.ops_menu
)
51 def set_data(self
, 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.
68 self
.operation
.append_text(str(op
))
71 self
.operation
.set_active(self
.ops
.index(data
.default
))
73 print >>sys
.stderr
, "Warning: %s not in ops list!" % data
.default
78 if last
and last
!= op
.__class
__:
81 self
.ops_menu
.append(item
)
84 item
= g
.MenuItem(str(op
))
86 self
.ops_menu
.append(item
)
91 self
.operation
.set_history(self
.ops
.index(data
.default
))
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
):
108 name
= entry
.get_text()
111 if op
and name
.endswith('.' + op
.extension
):
113 self
.operation
.set_active(i
)
115 self
.operation
.set_history(i
)
121 def get_selected_op(self
):
123 return self
.ops
[self
.operation
.get_active()]
125 return self
.ops
[self
.operation
.get_history()]
127 def op_changed(self
, operation
):
128 op
= self
.get_selected_op()
130 self
.set_type(op
.type)
136 name
= self
.save_area
.entry
.get_text()
138 if op2
and name
.endswith('.' + op2
.extension
):
139 name
= name
[:-len(op2
.extension
)-1]
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)
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
)
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():
160 os
.remove(sys
.argv
[1])
162 rox
.report_exception()
164 def can_save_to_selection(self
):
166 op
= self
.get_selected_op()
168 if op
.type == 'inode/directory':
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
)