merge the formfield patch from ooo-build
[ooovba.git] / basic / source / app / processw.cxx
blob9caf34148c27ac6524b445f12f2c44c467bffdfc
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: processw.cxx,v $
10 * $Revision: 1.8 $
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 #include <tools/errcode.hxx>
36 #include <basic/sbxobj.hxx>
37 #include <basic/sbx.hxx>
38 #ifndef __SBX_SBXVARIABLE_HXX //autogen
39 #include <basic/sbxvar.hxx>
40 #endif
42 //#include <osl/thread.h>
43 #ifndef _BASIC_TTRESHLP_HXX
44 #include <basic/ttstrhlp.hxx>
45 #endif
47 #include "processw.hxx"
49 // Process has the following elements:
50 // 1) Properties:
51 // none
52 // 2) Methods:
53 // SetImage( Filename )
54 // BOOL Start
55 // USHORT GetExitCode
56 // BOOL IsRunning
57 // BOOL WasGPF
60 // This implementation is a sample for a table driven version that
61 // can contain lots of elements. The elements are taken into the object
62 // when they are needed.
64 // The nArgs field of a table entry is scrambled as follows:
65 #define _ARGSMASK 0x00FF // Up to 255 arguments
66 #define _RWMASK 0x0F00 // Mask for R/W-Bits
67 #define _TYPEMASK 0xF000 // Mask for entry type
69 #define _READ 0x0100 // can be read
70 #define _BWRITE 0x0200 // can be used as Lvaluen
71 #define _LVALUE _BWRITE
72 #define _READWRITE 0x0300 // can read and written
73 #define _OPT 0x0400 // TRUE: optional parameter
74 #define _METHOD 0x1000 // Mask-Bit for a method
75 #define _PROPERTY 0x2000 // Mask-Bit for a property
76 #define _COLL 0x4000 // Mask-Bit for a collection
78 // Combination of the bits above:
79 #define _FUNCTION 0x1100 // Mask for a Function
80 #define _LFUNCTION 0x1300 // Mask for a Function, that can be uses as Lvalue
81 #define _ROPROP 0x2100 // Mask Read Only-Property
82 #define _WOPROP 0x2200 // Mask Write Only-Property
83 #define _RWPROP 0x2300 // Mask Read/Write-Property
84 #define _COLLPROP 0x4100 // Mask Read-Collection-Element
86 #define COLLNAME "Elements" // Name of the collection, hard coded
89 ProcessWrapper::Methods ProcessWrapper::aProcessMethods[] = {
90 // Imagefile of the Executable
91 { "SetImage", SbxEMPTY, &ProcessWrapper::PSetImage, 1 | _FUNCTION },
92 // Two Named Parameter
93 { "Filename", SbxSTRING, NULL, 0 },
94 { "Params", SbxSTRING, NULL, _OPT },
95 // Program is started
96 { "Start", SbxBOOL, &ProcessWrapper::PStart, 0 | _FUNCTION },
97 // ExitCode of the program
98 { "GetExitCode", SbxULONG, &ProcessWrapper::PGetExitCode, 0 | _FUNCTION },
99 // Program is running
100 { "IsRunning", SbxBOOL, &ProcessWrapper::PIsRunning, 0 | _FUNCTION },
101 // Program has faulted with GPF etc.
102 { "WasGPF", SbxBOOL, &ProcessWrapper::PWasGPF, 0 | _FUNCTION },
104 { NULL, SbxNULL, NULL, -1 }}; // End of table
107 ProcessWrapper::ProcessWrapper() : SbxObject( CUniString("Process") )
109 pProcess = new Process();
110 SetName( CUniString("Process") );
111 pMethods = &aProcessMethods[0];
114 ProcessWrapper::~ProcessWrapper()
116 delete pProcess;
119 // Search for an element:
120 // Linear search through the method table until an appropriate one
121 // can be found.
122 // If the the method/property cannot be found, NULL is return
123 // without an error code, so that we can ask the whole
124 // chain of objects for the method/property.
125 SbxVariable* ProcessWrapper::Find( const String& rName, SbxClassType t )
127 // Is the element already available?
128 SbxVariable* pRes = SbxObject::Find( rName, t );
129 if( !pRes && t != SbxCLASS_OBJECT )
131 // otherwise search
132 Methods* p = pMethods;
133 short nIndex = 0;
134 BOOL bFound = FALSE;
135 while( p->nArgs != -1 )
137 if( rName.EqualsIgnoreCaseAscii( p->pName ) )
139 bFound = TRUE; break;
141 nIndex += ( p->nArgs & _ARGSMASK ) + 1;
142 p = pMethods + nIndex;
144 if( bFound )
146 // isolate Args fields:
147 short nAccess = ( p->nArgs & _RWMASK ) >> 8;
148 short nType = ( p->nArgs & _TYPEMASK );
149 String aMethodName( p->pName, RTL_TEXTENCODING_ASCII_US );
150 SbxClassType eCT = SbxCLASS_OBJECT;
151 if( nType & _PROPERTY )
152 eCT = SbxCLASS_PROPERTY;
153 else if( nType & _METHOD )
154 eCT = SbxCLASS_METHOD;
155 pRes = Make( aMethodName, eCT, p->eType );
156 // We set array index + 1 because there are other
157 // default properties that must be activated
158 pRes->SetUserData( nIndex + 1 );
159 pRes->SetFlags( nAccess );
162 return pRes;
165 // Activation of an element or request for an info block
166 void ProcessWrapper::SFX_NOTIFY( SfxBroadcaster& rBC, const TypeId& rBCT,
167 const SfxHint& rHint, const TypeId& rHT )
169 const SbxHint* pHint = PTR_CAST(SbxHint,&rHint);
170 if( pHint )
172 SbxVariable* pVar = pHint->GetVar();
173 SbxArray* pNotifyPar = pVar->GetParameters();
174 USHORT nIndex = (USHORT) pVar->GetUserData();
175 // No index: put through
176 if( nIndex )
178 ULONG t = pHint->GetId();
179 if( t == SBX_HINT_INFOWANTED )
180 pVar->SetInfo( GetInfo( (short) pVar->GetUserData() ) );
181 else
183 BOOL bWrite = FALSE;
184 if( t == SBX_HINT_DATACHANGED )
185 bWrite = TRUE;
186 if( t == SBX_HINT_DATAWANTED || bWrite )
188 // Parameter-Test for methods:
189 USHORT nPar = pMethods[ --nIndex ].nArgs & 0x00FF;
190 // Element 0 is the return value
191 if( ( !pNotifyPar && nPar )
192 || ( pNotifyPar && pNotifyPar->Count() < nPar+1 ) )
193 SetError( SbxERR_WRONG_ARGS );
194 // ready for call
195 else
197 (this->*(pMethods[ nIndex ].pFunc))( pVar, pNotifyPar, bWrite );
202 SbxObject::SFX_NOTIFY( rBC, rBCT, rHint, rHT );
206 // Building the info struct for single elements
207 SbxInfo* ProcessWrapper::GetInfo( short nIdx )
209 Methods* p = &pMethods[ nIdx ];
210 // Wenn mal eine Hilfedatei zur Verfuegung steht:
211 // SbxInfo* pResultInfo = new SbxInfo( Hilfedateiname, p->nHelpId );
212 SbxInfo* pResultInfo = new SbxInfo;
213 short nPar = p->nArgs & _ARGSMASK;
214 for( short i = 0; i < nPar; i++ )
216 p++;
217 String aMethodName( p->pName, RTL_TEXTENCODING_ASCII_US );
218 USHORT nInfoFlags = ( p->nArgs >> 8 ) & 0x03;
219 if( p->nArgs & _OPT )
220 nInfoFlags |= SBX_OPTIONAL;
221 pResultInfo->AddParam( aMethodName, p->eType, nInfoFlags );
223 return pResultInfo;
227 ////////////////////////////////////////////////////////////////////////////
230 ////////////////////////////////////////////////////////////////////////////
232 // Properties and methods save the return value in argv[0] (Get, bPut = FALSE)
233 // and store the value from argv[0] (Put, bPut = TRUE)
235 void ProcessWrapper::PSetImage( SbxVariable* pVar, SbxArray* pMethodePar, BOOL bWriteIt )
236 { // Imagefile of the executable
237 (void) pVar; /* avoid warning about unused parameter */
238 (void) bWriteIt; /* avoid warning about unused parameter */
239 if ( pMethodePar->Count() >= 2 )
240 pProcess->SetImage(pMethodePar->Get( 1 )->GetString(), pMethodePar->Get( 2 )->GetString() );
241 else
242 pProcess->SetImage(pMethodePar->Get( 1 )->GetString(), String() );
245 void ProcessWrapper::PStart( SbxVariable* pVar, SbxArray* pMethodePar, BOOL bWriteIt )
246 { // Program is started
247 (void) pMethodePar; /* avoid warning about unused parameter */
248 (void) bWriteIt; /* avoid warning about unused parameter */
249 pVar->PutBool( pProcess->Start() );
252 void ProcessWrapper::PGetExitCode( SbxVariable* pVar, SbxArray* pMethodePar, BOOL bWriteIt )
253 { // ExitCode of the program after it was finished
254 (void) pMethodePar; /* avoid warning about unused parameter */
255 (void) bWriteIt; /* avoid warning about unused parameter */
256 pVar->PutULong( pProcess->GetExitCode() );
259 void ProcessWrapper::PIsRunning( SbxVariable* pVar, SbxArray* pMethodePar, BOOL bWriteIt )
260 { // Program is still running
261 (void) pMethodePar; /* avoid warning about unused parameter */
262 (void) bWriteIt; /* avoid warning about unused parameter */
263 pVar->PutBool( pProcess->IsRunning() );
266 void ProcessWrapper::PWasGPF( SbxVariable* pVar, SbxArray* pMethodePar, BOOL bWriteIt )
267 { // Program faulted with GPF etc.
268 (void) pMethodePar; /* avoid warning about unused parameter */
269 (void) bWriteIt; /* avoid warning about unused parameter */
270 pVar->PutBool( pProcess->WasGPF() );
278 // The factory creates our object
279 SbxObject* ProcessFactory::CreateObject( const String& rClass )
281 if( rClass.CompareIgnoreCaseToAscii( "Process" ) == COMPARE_EQUAL )
282 return new ProcessWrapper();
283 return NULL;