work on binary files:
[openerp-client.git] / bin / widget / view / form_gtk / binary.py
blob00161ce7a2ace3145b9115edddf20743cb01050a
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # Copyright (c) 2004-2008 TINY SPRL. (http://tiny.be) All Rights Reserved.
6 # $Id$
8 # WARNING: This program as such is intended to be used by professional
9 # programmers who take the whole responsability of assessing all potential
10 # consequences resulting from its eventual inadequacies and bugs
11 # End users who are looking for a ready-to-use solution with commercial
12 # garantees and support are strongly adviced to contract a Free Software
13 # Service Company
15 # This program is Free Software; you can redistribute it and/or
16 # modify it under the terms of the GNU General Public License
17 # as published by the Free Software Foundation; either version 2
18 # of the License, or (at your option) any later version.
20 # This program is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 ##############################################################################
31 import os
32 import sys
33 import tempfile
34 import time
36 import base64
38 import gtk
39 import gettext
41 import rpc
42 import interface
43 import common
44 import options
45 import printer
48 class wid_binary(interface.widget_interface):
49 def __init__(self, window, parent, model, attrs={}):
50 interface.widget_interface.__init__(self, window, parent, model, attrs)
52 self.widget = gtk.HBox(spacing=5)
53 self.wid_text = gtk.Entry()
54 self.wid_text.set_property('activates_default', True)
55 self.widget.pack_start(self.wid_text, expand=True, fill=True)
57 self.but_new = gtk.Button()
58 img = gtk.Image()
59 img.set_from_stock( 'gtk-execute', gtk.ICON_SIZE_BUTTON )
60 label = gtk.Label( ('_Open' ))
61 label.set_use_underline( True )
62 hbox = gtk.HBox()
63 hbox.set_spacing( 2 )
64 hbox.pack_start( img, expand=False, fill=False )
65 hbox.pack_end( label, expand=False, fill=False )
67 self.but_new.add( hbox )
68 self.but_new.connect('clicked', self.sig_execute)
69 self.widget.pack_start(self.but_new, expand=False, fill=False)
71 self.but_new = gtk.Button()
72 img = gtk.Image()
73 img.set_from_stock( 'gtk-open', gtk.ICON_SIZE_BUTTON )
74 label = gtk.Label( _('_Select') )
75 label.set_use_underline( True )
76 hbox = gtk.HBox()
77 hbox.set_spacing( 2 )
78 hbox.pack_start( img, expand=False, fill=False )
79 hbox.pack_end( label, expand=False, fill=False )
81 self.but_new.add( hbox )
82 self.but_new.connect('clicked', self.sig_select)
83 self.widget.pack_start(self.but_new, expand=False, fill=False)
85 self.but_save_as = gtk.Button(stock='gtk-save-as')
86 self.but_save_as.connect('clicked', self.sig_save_as)
87 self.widget.pack_start(self.but_save_as, expand=False, fill=False)
89 self.but_remove = gtk.Button(stock='gtk-clear')
90 self.but_remove.connect('clicked', self.sig_remove)
91 self.widget.pack_start(self.but_remove, expand=False, fill=False)
93 self.model_field = False
94 self.has_filename = attrs.get('filename')
95 self.data_field_name = attrs.get('name')
97 def _readonly_set(self, value):
98 if value:
99 self.but_new.hide()
100 self.but_remove.hide()
101 else:
102 self.but_new.show()
103 self.but_remove.show()
105 def _get_filename(self):
106 return self._view.model.value.get(self.has_filename) or self._view.model.value.get('name', self.data_field_name)
108 def sig_execute(self,widget=None):
109 try:
110 filename = self._get_filename()
111 if filename:
112 data = self._view.model.value.get(self.data_field_name)
113 if not data:
114 data = self._view.model.get(self.data_field_name)[self.data_field_name]
115 if not data:
116 raise Exception(_("Unable to read the file data"))
118 ext = os.path.splitext(filename)[1][1:]
119 (fileno, fp_name) = tempfile.mkstemp('.'+ext, 'tinyerp_')
121 os.write(fileno, base64.decodestring(data))
122 os.close(fileno)
124 printer.printer.print_file(fp_name, ext, preview=True)
125 except Exception, ex:
126 common.message(_('Error reading the file: %s') % str(ex))
128 def sig_select(self, widget=None):
129 try:
130 # Add the filename from the field with the filename attribute in the view
131 filename = common.file_selection(_('Select a file...'), parent=self._window)
132 if filename:
133 self.model_field.set_client(self._view.model, base64.encodestring(file(filename, 'rb').read()))
134 if self.has_filename:
135 self._view.model.set({self.has_filename: os.path.basename(filename)})
136 self._view.display(self._view.model)
137 except Exception, ex:
138 common.message(_('Error reading the file'))
140 def sig_save_as(self, widget=None):
141 try:
142 data = self._view.model.value.get(self.data_field_name)
143 if not data:
144 data = self._view.model.get(self.data_field_name)[self.data_field_name]
145 if not data:
146 raise Exception(_("Unable to read the file data"))
148 # Add the filename from the field with the filename attribute in the view
149 filename = common.file_selection(
150 _('Save As...'),
151 parent=self._window,
152 action=gtk.FILE_CHOOSER_ACTION_SAVE,
153 filename=self._get_filename()
155 if filename:
156 fp = file(filename,'wb+')
157 fp.write(base64.decodestring(data))
158 fp.close()
159 except:
160 common.message(_('Error writing the file: %s') % str(ex))
162 def sig_remove(self, widget=None):
163 self.model_field.set_client(self._view.model, False)
164 if self.has_filename:
165 self._view.model.set({self.has_filename: False})
166 self.display(self._view.model, self.model_field)
168 def display(self, model, model_field):
169 if not model_field:
170 self.wid_text.set_text('')
171 return False
172 super(wid_binary, self).display(model, model_field)
173 self.model_field = model_field
174 self.wid_text.set_text(model_field.get_client(model) or '')
175 return True
177 #def _size_get(self, l):
178 # return l and _('%d bytes') % len(l) or ''
180 def set_value(self, model, model_field):
181 return
183 def _color_widget(self):
184 return self.wid_text
186 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: