[IMP] rephrase some error and warning messages in base_action_rule, base_calendar...
[odoo/odoo.git] / addons / base_report_designer / plugin / openerp_report_designer / bin / script / ExportToRML.py
1 ##########################################################################
2 #
3 # Portions of this file are under the following copyright and license:
4 #
5 #
6 #   Copyright (c) 2003-2004 Danny Brewer
7 #   d29583@groovegarden.com
8 #
9 #   This library is free software; you can redistribute it and/or
10 #   modify it under the terms of the GNU Lesser General Public
11 #   License as published by the Free Software Foundation; either
12 #   version 2.1 of the License, or (at your option) any later version.
13 #
14 #   This library is distributed in the hope that it will be useful,
15 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
16 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 #   Lesser General Public License for more details.
18 #
19 #   You should have received a copy of the GNU Lesser General Public
20 #   License along with this library; if not, write to the Free Software
21 #   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22 #
23 #   See:  http://www.gnu.org/licenses/lgpl.html
24 #
25 #
26 # and other portions are under the following copyright and license:
27 #
28 #
29 #    OpenERP, Open Source Management Solution>..
30 #    Copyright (C) 2004-2010 OpenERP SA (<http://openerp.com>).
31 #
32 #    This program is free software: you can redistribute it and/or modify
33 #    it under the terms of the GNU Affero General Public License as published by
34 #    the Free Software Foundation, either version 3 of the License, or
35 #    (at your option) any later version.
36 #
37 #    This program is distributed in the hope that it will be useful,
38 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
39 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
40 #    GNU Affero General Public License for more details.
41 #
42 #    You should have received a copy of the GNU Affero General Public License
43 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
44 #
45 #
46 ##############################################################################
47 import os
48 import uno
49 import unohelper
50 import string
51 import tempfile
52 import base64
53 import sys
54
55 reload(sys)
56 sys.setdefaultencoding("utf8")
57 from com.sun.star.task import XJobExecutor
58 if __name__<>"package":
59     from lib.gui import *
60     from LoginTest import *
61     from lib.error import *
62     from lib.tools import *
63     from lib.logreport import *
64     from lib.rpc import *
65     database="test"
66     uid = 3
67
68
69 class ExportToRML( unohelper.Base, XJobExecutor ):
70     def __init__(self,ctx):
71         self.ctx     = ctx
72         self.module  = "openerp_report"
73         self.version = "0.1"
74         LoginTest()
75         if not loginstatus and __name__=="package":
76             exit(1)
77
78         desktop=getDesktop()
79         doc = desktop.getCurrentComponent()
80         docinfo=doc.getDocumentInfo()
81         global url
82         self.sock=RPCSession(url)
83
84         # Read Data from sxw file
85         tmpsxw = tempfile.mktemp('.'+"sxw")
86
87         if not doc.hasLocation():
88             mytype = Array(makePropertyValue("MediaType","application/vnd.sun.xml.writer"),)
89             doc.storeAsURL("file://"+tmpsxw,mytype)
90         data = read_data_from_file( get_absolute_file_path( doc.getURL()[7:] ) )
91         file_type = doc.getURL()[7:].split(".")[-1]
92         if docinfo.getUserFieldValue(2) == "":
93             ErrorDialog("Please Save this file on server","Use Send To Server Option in OpenERP Report Menu","Error")
94             exit(1)
95         filename = self.GetAFileName()
96         if not filename:
97             exit(1)
98         global passwd
99         self.password = passwd
100         try:
101
102             res = self.sock.execute(database, uid, self.password, 'ir.actions.report.xml', 'sxwtorml',base64.encodestring(data),file_type)
103             if res['report_rml_content']:
104                 write_data_to_file( get_absolute_file_path( filename[7:] ), res['report_rml_content'] )
105         except Exception,e:
106             import traceback,sys
107             info = reduce(lambda x, y: x+y, traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback))
108             self.logobj.log_write('ExportToRML',LOG_ERROR, info)
109             ErrorDialog("Cannot save the file to the hard drive.", "Exception: %s." % e, "Error" )
110
111     def GetAFileName(self):
112         sFilePickerArgs = Array(10)
113         oFileDialog = createUnoService("com.sun.star.ui.dialogs.FilePicker")
114         oFileDialog.initialize(sFilePickerArgs)
115         oFileDialog.appendFilter("OpenERP Report File Save To ....","*.rml")
116
117         f_path = "OpenERP-"+ os.path.basename( tempfile.mktemp("","") ) + ".rml"
118         initPath = tempfile.gettempdir()
119         oUcb = createUnoService("com.sun.star.ucb.SimpleFileAccess")
120         if oUcb.exists(initPath):
121             oFileDialog.setDisplayDirectory('file://' + ( os.name == 'nt' and '/' or '' ) + initPath )
122
123         oFileDialog.setDefaultName(f_path )
124
125         sPath = oFileDialog.execute() == 1 and oFileDialog.Files[0] or None
126         oFileDialog.dispose()
127         return sPath
128
129 if __name__<>"package" and __name__=="__main__":
130     ExportToRML(None)
131 elif __name__=="package":
132     g_ImplementationHelper.addImplementation( ExportToRML, "org.openoffice.openerp.report.exporttorml", ("com.sun.star.task.Job",),)
133
134 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: