merge the formfield patch from ooo-build
[ooovba.git] / basic / source / app / process.cxx
blob01491f9e9d948865f2a3c96ee4d2af6a2e1074e4
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: process.cxx,v $
10 * $Revision: 1.16 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_basic.hxx"
35 #ifdef WNT
36 #include <tools/prewin.h>
37 #include "winbase.h"
38 #include <tools/postwin.h>
39 #endif
40 #include <tools/errcode.hxx>
41 #include <vos/process.hxx>
42 #include <basic/sbxcore.hxx>
43 #include <tools/string.hxx>
44 #include <osl/file.hxx>
46 #ifndef _BASIC_TTRESHLP_HXX
47 #include <basic/ttstrhlp.hxx>
48 #endif
50 //#ifndef _BYTE_STRING_LIST
51 //DECLARE_LIST( ByteStringList, ByteString * );
52 //#define _BYTE_STRING_LIST
53 //#endif
55 #include <basic/process.hxx>
57 Process::Process()
58 : pArgumentList( NULL )
59 , pEnvList( NULL )
60 , pProcess( NULL )
61 , bWasGPF( FALSE )
62 , bHasBeenStarted( FALSE )
66 Process::~Process()
68 // delete pArgumentList;
69 // delete pEnvList;
70 delete pProcess;
74 BOOL Process::ImplIsRunning()
76 if ( pProcess && bHasBeenStarted )
78 NAMESPACE_VOS(OProcess::TProcessInfo) aProcessInfo;
79 pProcess->getInfo( NAMESPACE_VOS(OProcess::TData_ExitCode), &aProcessInfo );
80 if ( !(aProcessInfo.Fields & NAMESPACE_VOS(OProcess::TData_ExitCode)) )
81 return TRUE;
82 else
83 return FALSE;
85 else
86 return FALSE;
89 long Process::ImplGetExitCode()
91 if ( pProcess )
93 NAMESPACE_VOS(OProcess::TProcessInfo) aProcessInfo;
94 pProcess->getInfo( NAMESPACE_VOS(OProcess::TData_ExitCode), &aProcessInfo );
95 if ( !(aProcessInfo.Fields & NAMESPACE_VOS(OProcess::TData_ExitCode)) )
96 SbxBase::SetError( SbxERR_NO_ACTIVE_OBJECT );
97 return aProcessInfo.Code;
99 else
100 SbxBase::SetError( SbxERR_NO_ACTIVE_OBJECT );
101 return 0;
105 ////////////////////////////////////////////////////////////////////////////
107 void Process::SetImage( const String &aAppPath, const String &aAppParams, const Environment *pEnv )
108 { // Set image file of executable
109 if ( pProcess && ImplIsRunning() )
110 SbxBase::SetError( SbxERR_NO_ACTIVE_OBJECT );
111 else
113 delete pArgumentList; pArgumentList = NULL;
114 delete pEnvList; pEnvList = NULL;
115 delete pProcess; pProcess = NULL;
117 xub_StrLen i, nCount = aAppParams.GetQuotedTokenCount( CUniString("\"\"" ), ' ' );
118 ::rtl::OUString *pParamList = new ::rtl::OUString[nCount];
120 xub_StrLen nParamCount = 0;
121 for ( i = 0 ; i < nCount ; i++ )
123 ::rtl::OUString aTemp = ::rtl::OUString(aAppParams.GetQuotedToken( i, CUniString("\"\"''" ), ' ' ));
124 if ( aTemp.getLength() )
126 pParamList[nParamCount] = aTemp;
127 nParamCount++;
130 pArgumentList = new NAMESPACE_VOS(OArgumentList)( pParamList, nCount );
133 ::rtl::OUString *pEnvArray = NULL;
134 if ( pEnv )
136 pEnvArray = new ::rtl::OUString[pEnv->size()];
138 xub_StrLen nEnvCount = 0;
139 Environment::const_iterator aIter = pEnv->begin();
140 while ( aIter != pEnv->end() )
142 ::rtl::OUString aTemp = ::rtl::OUString( (*aIter).first );
143 aTemp += ::rtl::OUString::createFromAscii( "=" );
144 aTemp += ::rtl::OUString( (*aIter).second );
145 pEnvArray[nEnvCount] = aTemp;
146 nEnvCount++;
147 aIter++;
149 pEnvList = new NAMESPACE_VOS(OEnvironment)( pEnvArray, nEnvCount );
152 ::rtl::OUString aNormalizedAppPath;
153 osl::FileBase::getFileURLFromSystemPath( ::rtl::OUString(aAppPath), aNormalizedAppPath );
154 pProcess = new NAMESPACE_VOS(OProcess)( aNormalizedAppPath );
155 bHasBeenStarted = FALSE;
157 delete [] pParamList;
158 delete [] pEnvArray;
162 BOOL Process::Start()
163 { // Start program
164 BOOL bSuccess=FALSE;
165 if ( pProcess && !ImplIsRunning() )
167 bWasGPF = FALSE;
168 #ifdef WNT
169 // sal_uInt32 nErrorMode = SetErrorMode(SEM_NOOPENFILEERRORBOX | SEM_NOALIGNMENTFAULTEXCEPT | SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
170 sal_uInt32 nErrorMode = SetErrorMode(SEM_NOOPENFILEERRORBOX | SEM_NOALIGNMENTFAULTEXCEPT | SEM_NOGPFAULTERRORBOX);
173 #endif
174 if ( pEnvList )
176 bSuccess = pProcess->execute( (NAMESPACE_VOS(OProcess)::TProcessOption)
177 ( NAMESPACE_VOS(OProcess)::TOption_SearchPath
178 /*| NAMESPACE_VOS(OProcess)::TOption_Detached*/
179 /*| NAMESPACE_VOS(OProcess)::TOption_Wait*/ ),
180 *pArgumentList,
181 *pEnvList ) == NAMESPACE_VOS(OProcess)::E_None;
183 else
185 bSuccess = pProcess->execute( (NAMESPACE_VOS(OProcess)::TProcessOption)
186 ( NAMESPACE_VOS(OProcess)::TOption_SearchPath
187 /*| NAMESPACE_VOS(OProcess)::TOption_Detached*/
188 /*| NAMESPACE_VOS(OProcess)::TOption_Wait*/ ),
189 *pArgumentList ) == NAMESPACE_VOS(OProcess)::E_None;
191 #ifdef WNT
193 catch( ... )
195 bWasGPF = TRUE;
196 // TODO: Output debug message !!
198 nErrorMode = SetErrorMode(nErrorMode);
199 #endif
200 bHasBeenStarted = bSuccess;
202 else
203 SbxBase::SetError( SbxERR_NO_ACTIVE_OBJECT );
204 return bSuccess;
207 ULONG Process::GetExitCode()
208 { // ExitCode of program after execution
209 return ImplGetExitCode();
212 BOOL Process::IsRunning()
214 return ImplIsRunning();
217 BOOL Process::WasGPF()
218 { // Did the process fail?
219 #ifdef WNT
220 return ImplGetExitCode() == 3221225477;
221 #else
222 return bWasGPF;
223 #endif
226 BOOL Process::Terminate()
228 if ( ImplIsRunning() )
229 return pProcess->terminate() == vos::OProcess::E_None;
230 return TRUE;