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 self
.operation
= g
.combo_box_new_text()
33 self
.operation
= g
.OptionMenu()
34 self
.vbox
.pack_start(self
.operation
, False, True, 0)
36 self
.operation
.set_border_width(5)
39 self
.delete_choice
= g
.CheckButton(_('Delete file afterwards?'))
40 self
.vbox
.pack_start(self
.delete_choice
, True, True, 0)
43 self
.ops_menu
= g
.Menu()
44 self
.operation
.set_menu(self
.ops_menu
)
48 def set_data(self
, data
):
52 self
.save_mode
= data
.mode
53 ops
= [op
for op
in formats
.operations
if op
.can_handle(data
)]
55 name
= data
.default_name
56 self
.save_area
.entry
.set_text(name
)
57 start
= name
.rfind('/') + 1
58 #self.save_area.entry.select_region(start, -1)
59 g
.Editable
.select_region(self
.save_area
.entry
, start
, -1)
61 # Add all supported ops to the menu.
65 self
.operation
.append_text(str(op
))
68 self
.operation
.set_active(self
.ops
.index(data
.default
))
70 print >>sys
.stderr
, "Warning: %s not in ops list!" % data
.default
75 if last
and last
!= op
.__class
__:
78 self
.ops_menu
.append(item
)
81 item
= g
.MenuItem(str(op
))
83 self
.ops_menu
.append(item
)
88 self
.operation
.set_history(self
.ops
.index(data
.default
))
90 print >>sys
.stderr
, "Warning: %s not in ops list!" % data
.default
92 self
.operation
.connect('changed', self
.op_changed
)
93 self
.save_area
.entry
.connect('changed', self
.name_changed
)
95 self
.save_area
.set_type(data
.default
.type)
97 if isinstance(data
, formats
.FileData
) and data
.path
!= '-':
98 self
.delete_choice
.show()
100 def name_changed(self
, entry
):
105 name
= entry
.get_text()
108 if op
and name
.endswith('.' + op
.extension
):
109 self
.operation
.set_history(i
)
115 def get_selected_op(self
):
117 return self
.ops
[self
.operation
.get_active()]
119 return self
.ops
[self
.operation
.get_history()]
121 def op_changed(self
, operation
):
122 op
= self
.get_selected_op()
124 self
.set_type(op
.type)
130 name
= self
.save_area
.entry
.get_text()
132 if op2
and name
.endswith('.' + op2
.extension
):
133 name
= name
[:-len(op2
.extension
)-1]
136 name
+= '.' + op
.extension
137 self
.save_area
.entry
.set_text(name
)
138 start
= name
.rfind('/') + 1
139 #self.save_area.entry.select_region(start, -1)
140 g
.Editable
.select_region(self
.save_area
.entry
, start
, -1)
144 def save_to_file(self
, path
):
145 op
= self
.get_selected_op()
146 if hasattr(op
, 'save_to_file'):
147 op
.save_to_file(self
.data
, path
)
149 saving
.Saveable
.save_to_file(self
, path
)
151 #If the delete_choice is set, try to remove the file
152 if self
.delete_choice
.get_active():
154 os
.remove(sys
.argv
[1])
156 rox
.report_exception()
158 def can_save_to_selection(self
):
160 op
= self
.get_selected_op()
162 if op
.type == 'inode/directory':
164 return saving
.Saveable
.can_save_to_selection(self
)
166 def save_to_stream(self
, stream
):
167 op
= self
.get_selected_op()
168 op
.save_to_stream(self
.data
, stream
)