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 _findPDFOpener(self
):
82 if os
.uname()[0] == 'Darwin' :
88 os
.system('/usr/bin/open -a Preview ' + fn
)
94 if options
.options
['printer.preview']:
95 if options
.options
['printer.softpath'] == 'none':
96 return lambda fn
: os
.startfile(fn
)
98 return lambda fn
: os
.system(options
.options
['printer.softpath'] + ' ' + fn
)
100 return lambda fn
: print_w32_filename(fn
)
102 if options
.options
['printer.preview']:
103 if options
.options
['printer.softpath'] == 'none':
104 prog
= self
._findInPath
(['evince', 'xpdf', 'gpdf', 'kpdf', 'epdfview', 'acroread', 'open'])
110 os
.execv(prog
, (os
.path
.basename(prog
), fn
))
121 os
.execv(options
.options
['printer.softpath'], (os
.path
.basename(options
.options
['printer.softpath']),fn
))
127 return lambda fn
: print_linux_filename(fn
)
129 def _findSXWOpener(self
):
131 return lambda fn
: os
.startfile(fn
)
133 if options
.options
['printer.softpath_html'] == 'none':
134 prog
= self
._findInPath
(['ooffice', 'ooffice2', 'openoffice', 'soffice'])
140 os
.execv(prog
, (os
.path
.basename(prog
),fn
))
151 os
.execv(options
.options
['printer.softpath_html'], (os
.path
.basename(options
.options
['printer.softpath_html']),fn
))
157 def print_file(self
, fname
, ftype
):
158 finderfunc
= self
.openers
[ftype
]
159 opener
= finderfunc()
165 def print_linux_filename(filename
):
166 common
.message(_('Linux Automatic Printing not implemented.\nUse preview option !'))
168 def print_w32_filename(filename
):
170 win32api
.ShellExecute (0, "print", filename
, None, ".", 0)
172 def print_data(data
):
173 if 'result' not in data
:
174 common
.message(_('Error no report'))
175 if data
.get('code','normal')=='zlib':
177 content
= zlib
.decompress(base64
.decodestring(data
['result']))
179 content
= base64
.decodestring(data
['result'])
181 if data
['format'] in printer
.openers
.keys():
183 if data
['format']=='html' and os
.name
=='nt':
185 (fileno
, fp_name
) = tempfile
.mkstemp('.'+data
['format'], 'tinyerp_')
186 fp
= file(fp_name
, 'wb+')
190 printer
.print_file(fp_name
, data
['format'])
192 fname
= common
.file_selection(_('Save As...'), filename
='report.' + data
['format'],
193 action
=gtk
.FILE_CHOOSER_ACTION_SAVE
)
196 fp
= file(fname
,'wb+')
200 common
.message(_('Error writing the file!'))
203 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: