00001 ############################################################################## 00002 # 00003 # Copyright (c) 2004 TINY SPRL. (http://tiny.be) All Rights Reserved. 00004 # Fabien Pinckaers <fp@tiny.Be> 00005 # Copyright (c) 2007-2008 Albert Cervera i Areny <albert@nan-tic.com> 00006 # 00007 # WARNING: This program as such is intended to be used by professional 00008 # programmers who take the whole responsability of assessing all potential 00009 # consequences resulting from its eventual inadequacies and bugs 00010 # End users who are looking for a ready-to-use solution with commercial 00011 # garantees and support are strongly adviced to contract a Free Software 00012 # Service Company 00013 # 00014 # This program is Free Software; you can redistribute it and/or 00015 # modify it under the terms of the GNU General Public License 00016 # as published by the Free Software Foundation; either version 2 00017 # of the License, or (at your option) any later version. 00018 # 00019 # This program is distributed in the hope that it will be useful, 00020 # but WITHOUT ANY WARRANTY; without even the implied warranty of 00021 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00022 # GNU General Public License for more details. 00023 # 00024 # You should have received a copy of the GNU General Public License 00025 # along with this program; if not, write to the Free Software 00026 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00027 # 00028 ############################################################################## 00029 00030 from PyQt4.QtCore import * 00031 from Koo.Common import Notifier 00032 from Koo.Common import Common 00033 from Koo.Common import Semantic 00034 from Koo.Common.Settings import * 00035 import os 00036 import base64 00037 import tempfile 00038 00039 ## @brief Provides various static functions to easily print files and/or data 00040 # comming from the server. 00041 # 00042 # It encapsulates system dependent code and handles user preferences, such as 00043 # sending directly to the printer if the system allows that. 00044 class Printer(object): 00045 ## @brief Opens the specified file with system's default application 00046 @staticmethod 00047 def open(fileName): 00048 Common.openFile( fileName ) 00049 00050 ## @brief Sends the specified file directly to the printer. 00051 # 00052 # Note that on non-windows systems it will try to execute system's lpr application. 00053 @staticmethod 00054 def sendToPrinter(fileName): 00055 if os.name == 'nt': 00056 import win32api 00057 win32api.ShellExecute (0, "print", fileName, None, ".", 0) 00058 else: 00059 os.spawnlp(os.P_NOWAIT, 'lpr', 'lpr', fileName) 00060 00061 ## @brief Sends the specified file to printer or opens it with the default 00062 # application depending on user settings. 00063 @staticmethod 00064 def printFile(fileName, fileType): 00065 if Settings.value('koo.print_directly'): 00066 Printer.sendToPrinter( fileName ) 00067 else: 00068 Printer.open( fileName ) 00069 00070 ## @brief Prints report information contained in the data parameter. Which will 00071 # typically be received from the server. 00072 @staticmethod 00073 def printData(data, model, ids): 00074 if 'result' not in data: 00075 Notifier.notifyWarning( _('Report error'), _('There was an error trying to create the report.') ) 00076 return 00077 00078 if data.get('code','normal')=='zlib': 00079 import zlib 00080 content = zlib.decompress(base64.decodestring(data['result'])) 00081 else: 00082 content = base64.decodestring(data['result']) 00083 00084 # We'll always try to open the file and won't limit ourselves to 00085 # doc, html and pdf. For example, one might get odt, ods, etc. Before 00086 # we stored the report in a file if it wasn't one of the first three 00087 # formats. 00088 if data['format']=='html' and os.name=='nt': 00089 data['format']='doc' 00090 00091 fp, fileName = tempfile.mkstemp( '.%s' % data['format'] ) 00092 fp = os.fdopen( fp, 'wb+' ) 00093 try: 00094 fp.write(content) 00095 finally: 00096 fp.close() 00097 # Add semantic information before printing file because otherwise 00098 # it raises an exception in some systems. 00099 Semantic.addInformationToFile( fileName, model, ids ) 00100 Printer.printFile( fileName, data['format'] ) 00101