4 from rox
import saving
, g
8 class ArchiveBox(saving
.SaveBox
, saving
.Saveable
):
9 "A savebox with an operation selector."
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
):
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)
30 self
.operation
.set_border_width(5)
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
):
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.
56 if last
and last
!= op
.__class
__:
59 self
.ops_menu
.append(item
)
62 item
= g
.MenuItem(str(op
))
64 self
.ops_menu
.append(item
)
69 self
.operation
.set_history(self
.ops
.index(data
.default
))
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
):
86 name
= entry
.get_text()
89 if op
and name
.endswith('.' + op
.extension
):
90 self
.operation
.set_history(i
)
96 def op_changed(self
, operation
):
97 op
= self
.ops
[operation
.get_history()]
99 self
.set_type(op
.type)
105 name
= self
.save_area
.entry
.get_text()
107 if op2
and name
.endswith('.' + op2
.extension
):
108 name
= name
[:-len(op2
.extension
)-1]
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)
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
)
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():
129 os
.remove(sys
.argv
[1])
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
)