1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # Copyright (c) 2004-2008 TINY SPRL. (http://tiny.be) All Rights Reserved.
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
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 ##############################################################################
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()
59 img
.set_from_stock( 'gtk-execute', gtk
.ICON_SIZE_BUTTON
)
60 label
= gtk
.Label( ('_Open' ))
61 label
.set_use_underline( True )
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()
73 img
.set_from_stock( 'gtk-open', gtk
.ICON_SIZE_BUTTON
)
74 label
= gtk
.Label( _('_Select') )
75 label
.set_use_underline( True )
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' )
96 def _readonly_set(self
, value
):
99 self
.but_remove
.hide()
102 self
.but_remove
.show()
104 def sig_execute(self
,widget
=None):
106 filename
= self
._view
.model
.value
.get( self
.has_filename
) or self
._view
.model
.value
['name']
108 id = int(self
._view
.model
.get(includeid
=True)['id'])
109 data
= rpc
.session
.rpc_exec_auth('/object', 'execute', 'ir.attachment', 'read', [id])
112 ext
= os
.path
.splitext( filename
)[1][1:]
113 (fileno
, fp_name
) = tempfile
.mkstemp('.'+ext
, 'tinyerp_')
115 fp
= file( fp_name
, 'wb+')
116 fp
.write( base64
.decodestring( data
[0]['datas'] ) )
120 printer
.printer
.print_file( fp_name
, ext
, preview
=True )
121 except Exception, ex
:
122 common
.message(_('Error reading the file'))
124 def sig_select(self
, widget
=None):
126 # Add the filename from the field with the filename attribute in the view
127 filename
= common
.file_selection(_('Select a file...'), parent
=self
._window
)
129 self
.model_field
.set_client(self
._view
.model
, base64
.encodestring(file(filename
, 'rb').read()))
130 fname
= self
.attrs
.get('fname_widget', False)
131 filename
= os
.path
.basename( filename
)
133 self
.parent
.value
= {fname
:filename
}
134 self
._view
.model
.set( { 'name': filename
, 'title' : filename
} )
135 self
._view
.display(self
._view
.model
)
136 except Exception, ex
:
137 common
.message(_('Error reading the file'))
139 def sig_save_as(self
, widget
=None):
141 id = int(self
._view
.model
.get(includeid
=True)['id'])
142 data
= rpc
.session
.rpc_exec_auth('/object', 'execute', 'ir.attachment', 'read', [id])
145 # Add the filename from the field with the filename attribute in the view
146 filename
= common
.file_selection(
149 action
=gtk
.FILE_CHOOSER_ACTION_SAVE
,
150 filename
=data
[0]['name']
153 fp
= file(filename
,'wb+')
154 fp
.write(base64
.decodestring(data
[0]['datas']))
157 common
.message(_('Error writing the file!'))
159 def sig_remove(self
, widget
=None):
160 self
.model_field
.set_client(self
._view
.model
, False)
161 fname
= self
.attrs
.get('fname_widget', False)
163 self
.parent
.value
= {fname
:False}
164 self
.display(self
._view
.model
, self
.model_field
)
166 def display(self
, model
, model_field
):
168 self
.wid_text
.set_text('')
170 super(wid_binary
, self
).display(model
, model_field
)
171 self
.model_field
= model_field
172 self
.wid_text
.set_text(self
._size
_get
(model_field
.get(model
)))
175 def _size_get(self
, l
):
176 return l
and _('%d bytes') % len(l
) or ''
178 def set_value(self
, model
, model_field
):
181 def _color_widget(self
):
184 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: