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 ##############################################################################
31 # ------------------------------------------------------------------- #
33 # ------------------------------------------------------------------- #
35 # Supported formats: pdf
37 # Print or open a previewer
40 import os
, base64
, options
, sys
46 class Printer(object):
50 'pdf': self
._findPDFOpener
,
51 'html': self
._findHTMLOpener
,
52 'doc': self
._findHTMLOpener
,
53 'xls': self
._findHTMLOpener
,
54 'sxw': self
._findSXWOpener
,
57 def _findInPath(self
, progs
):
60 path
= [dir for dir in os
.environ
['PATH'].split(':')
61 if os
.path
.isdir(dir)]
63 content
= os
.listdir(dir)
66 return os
.path
.join(dir, prog
)#prog
69 found
[prog
] = os
.path
.join(dir, prog
)
75 def _findHTMLOpener(self
):
78 webbrowser
.open('file://'+fn
)
81 def __opener(self
, fnct
):
91 def _findPDFOpener(self
):
92 if os
.uname()[0] == 'Darwin' :
94 self
.__opener
( lambda: os
.system('/usr/bin/open -a Preview ' + fn
) )
97 if options
.options
['printer.preview']:
98 if options
.options
['printer.softpath'] == 'none':
99 return lambda fn
: os
.startfile(fn
)
101 return lambda fn
: os
.system(options
.options
['printer.softpath'] + ' ' + fn
)
103 return lambda fn
: print_w32_filename(fn
)
105 if options
.options
['printer.preview']:
106 if options
.options
['printer.softpath'] == 'none':
107 prog
= self
._findInPath
(['evince', 'xpdf', 'gpdf', 'kpdf', 'epdfview', 'acroread', 'open'])
109 self
.__opener
( lambda: os
.execv(prog
, (os
.path
.basename(prog
), fn
) ))
113 self
.__opener
( lambda: os
.execv(options
.options
['printer.softpath'], (os
.path
.basename(options
.options
['printer.softpath']), fn
)) )
116 return lambda fn
: print_linux_filename(fn
)
118 def _findSXWOpener(self
):
120 return lambda fn
: os
.startfile(fn
)
122 if options
.options
['printer.softpath_html'] == 'none':
123 prog
= self
._findInPath
(['ooffice', 'ooffice2', 'openoffice', 'soffice'])
129 os
.execv(prog
, (os
.path
.basename(prog
),fn
))
140 os
.execv(options
.options
['printer.softpath_html'], (os
.path
.basename(options
.options
['printer.softpath_html']),fn
))
146 def print_file(self
, fname
, ftype
, preview
=False):
149 filetypes
= eval( options
.options
['extensions.filetype'] )
150 (app
, app_print
) = filetypes
[ftype
]
151 if options
.options
['printer.preview'] or preview
:
154 app_to_run
= app_print
159 def open_file( cmd
, filename
):
165 prog
, args
= cmd
.split(' ', 1)
166 args
= [os
.path
.basename(prog
)] + args
.split(' ')
168 os
.execvp(prog
, args
)
175 if sys
.platform
in ['win32','nt']:
177 open_file( app_to_run
, fname
)
179 os
.startfile( fname
)
181 open_file( app_to_run
, fname
)
183 finderfunc
= self
.openers
.get(ftype
)
185 raise Exception(_('Unable to handle %s filetype') % ftype
)
186 opener
= finderfunc()
193 def print_linux_filename(filename
):
194 common
.message(_('Linux Automatic Printing not implemented.\nUse preview option !'))
196 def print_w32_filename(filename
):
198 win32api
.ShellExecute (0, "print", filename
, None, ".", 0)
200 def print_data(data
):
201 if 'result' not in data
:
202 common
.message(_('Error no report'))
203 if data
.get('code','normal')=='zlib':
205 content
= zlib
.decompress(base64
.decodestring(data
['result']))
207 content
= base64
.decodestring(data
['result'])
209 if data
['format'] in printer
.openers
.keys():
211 if data
['format']=='html' and os
.name
=='nt':
213 (fileno
, fp_name
) = tempfile
.mkstemp('.'+data
['format'], 'tinyerp_')
214 fp
= file(fp_name
, 'wb+')
218 printer
.print_file(fp_name
, data
['format'])
220 fname
= common
.file_selection(_('Save As...'), filename
='report.' + data
['format'],
221 action
=gtk
.FILE_CHOOSER_ACTION_SAVE
)
224 fp
= file(fname
,'wb+')
228 common
.message(_('Error writing the file!'))
231 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: