Coming from the stable 4.2, this patch allows the user to associate an application...
[openerp-client.git] / bin / widget / view / form_gtk / binary.py
blob1ec77f3689180340d1c669e428533db6869d804e
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' )
96 def _readonly_set(self, value):
97 if value:
98 self.but_new.hide()
99 self.but_remove.hide()
100 else:
101 self.but_new.show()
102 self.but_remove.show()
104 def sig_execute(self,widget=None):
105 try:
106 filename = self._view.model.value.get( self.has_filename ) or self._view.model.value['name']
107 if filename:
108 id = int(self._view.model.get(includeid=True)['id'])
109 data = rpc.session.rpc_exec_auth('/object', 'execute', 'ir.attachment', 'read', [id])
110 if not len(data):
111 return None
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'] ) )
117 fp.close()
118 os.close(fileno)
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):
125 try:
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)
128 if filename:
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 )
132 if fname:
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):
140 try:
141 id = int(self._view.model.get(includeid=True)['id'])
142 data = rpc.session.rpc_exec_auth('/object', 'execute', 'ir.attachment', 'read', [id])
143 if not len(data):
144 return None
145 # Add the filename from the field with the filename attribute in the view
146 filename = common.file_selection(
147 _('Save As...'),
148 parent=self._window,
149 action=gtk.FILE_CHOOSER_ACTION_SAVE,
150 filename=data[0]['name']
152 if filename:
153 fp = file(filename,'wb+')
154 fp.write(base64.decodestring(data[0]['datas']))
155 fp.close()
156 except:
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)
162 if fname:
163 self.parent.value = {fname:False}
164 self.display(self._view.model, self.model_field)
166 def display(self, model, model_field):
167 if not model_field:
168 self.wid_text.set_text('')
169 return False
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)))
173 return True
175 def _size_get(self, l):
176 return l and _('%d bytes') % len(l) or ''
178 def set_value(self, model, model_field):
179 return
181 def _color_widget(self):
182 return self.wid_text
184 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: