xref: /trunk/main/pyuno/source/module/pyuno_module.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #include "pyuno_impl.hxx"
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir #include <osl/module.hxx>
31*cdf0e10cSrcweir #include <osl/thread.h>
32*cdf0e10cSrcweir #include <osl/file.hxx>
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir #include <typelib/typedescription.hxx>
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir #include <rtl/strbuf.hxx>
37*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
38*cdf0e10cSrcweir #include <rtl/uuid.h>
39*cdf0e10cSrcweir #include <rtl/bootstrap.hxx>
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir #include <uno/current_context.hxx>
42*cdf0e10cSrcweir #include <cppuhelper/bootstrap.hxx>
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir #include <com/sun/star/reflection/XIdlReflection.hpp>
45*cdf0e10cSrcweir #include <com/sun/star/reflection/XIdlClass.hpp>
46*cdf0e10cSrcweir #include <com/sun/star/registry/InvalidRegistryException.hpp>
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir using osl::Module;
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir using rtl::OString;
51*cdf0e10cSrcweir using rtl::OUString;
52*cdf0e10cSrcweir using rtl::OUStringToOString;
53*cdf0e10cSrcweir using rtl::OUStringBuffer;
54*cdf0e10cSrcweir using rtl::OStringBuffer;
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir using com::sun::star::uno::Sequence;
57*cdf0e10cSrcweir using com::sun::star::uno::Reference;
58*cdf0e10cSrcweir using com::sun::star::uno::XInterface;
59*cdf0e10cSrcweir using com::sun::star::uno::Any;
60*cdf0e10cSrcweir using com::sun::star::uno::makeAny;
61*cdf0e10cSrcweir using com::sun::star::uno::UNO_QUERY;
62*cdf0e10cSrcweir using com::sun::star::uno::RuntimeException;
63*cdf0e10cSrcweir using com::sun::star::uno::TypeDescription;
64*cdf0e10cSrcweir using com::sun::star::uno::XComponentContext;
65*cdf0e10cSrcweir using com::sun::star::container::NoSuchElementException;
66*cdf0e10cSrcweir using com::sun::star::reflection::XIdlReflection;
67*cdf0e10cSrcweir using com::sun::star::reflection::XIdlClass;
68*cdf0e10cSrcweir using com::sun::star::script::XInvocation2;
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir using namespace pyuno;
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir namespace {
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir /**
75*cdf0e10cSrcweir    @ index of the next to be used member in the initializer list !
76*cdf0e10cSrcweir  */
77*cdf0e10cSrcweir sal_Int32 fillStructWithInitializer(
78*cdf0e10cSrcweir     const Reference< XInvocation2 > &inv,
79*cdf0e10cSrcweir     typelib_CompoundTypeDescription *pCompType,
80*cdf0e10cSrcweir     PyObject *initializer,
81*cdf0e10cSrcweir     const Runtime &runtime) throw ( RuntimeException )
82*cdf0e10cSrcweir {
83*cdf0e10cSrcweir     sal_Int32 nIndex = 0;
84*cdf0e10cSrcweir     if( pCompType->pBaseTypeDescription )
85*cdf0e10cSrcweir         nIndex = fillStructWithInitializer(
86*cdf0e10cSrcweir             inv, pCompType->pBaseTypeDescription, initializer, runtime );
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir     sal_Int32 nTupleSize =  PyTuple_Size(initializer);
89*cdf0e10cSrcweir     int i;
90*cdf0e10cSrcweir     for( i = 0 ; i < pCompType->nMembers ; i ++ )
91*cdf0e10cSrcweir     {
92*cdf0e10cSrcweir         if( i + nIndex >= nTupleSize )
93*cdf0e10cSrcweir         {
94*cdf0e10cSrcweir             OUStringBuffer buf;
95*cdf0e10cSrcweir             buf.appendAscii( "pyuno._createUnoStructHelper: too few elements in the initializer tuple,");
96*cdf0e10cSrcweir             buf.appendAscii( "expected at least " ).append( nIndex + pCompType->nMembers );
97*cdf0e10cSrcweir             buf.appendAscii( ", got " ).append( nTupleSize );
98*cdf0e10cSrcweir             throw RuntimeException(buf.makeStringAndClear(), Reference< XInterface > ());
99*cdf0e10cSrcweir         }
100*cdf0e10cSrcweir         PyObject *element = PyTuple_GetItem( initializer, i + nIndex );
101*cdf0e10cSrcweir         Any a = runtime.pyObject2Any( element, ACCEPT_UNO_ANY );
102*cdf0e10cSrcweir         inv->setValue( pCompType->ppMemberNames[i], a );
103*cdf0e10cSrcweir     }
104*cdf0e10cSrcweir     return i+nIndex;
105*cdf0e10cSrcweir }
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir OUString getLibDir()
108*cdf0e10cSrcweir {
109*cdf0e10cSrcweir     static OUString *pLibDir;
110*cdf0e10cSrcweir     if( !pLibDir )
111*cdf0e10cSrcweir     {
112*cdf0e10cSrcweir         osl::MutexGuard guard( osl::Mutex::getGlobalMutex() );
113*cdf0e10cSrcweir         if( ! pLibDir )
114*cdf0e10cSrcweir         {
115*cdf0e10cSrcweir             static OUString libDir;
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir             // workarounds the $(ORIGIN) until it is available
118*cdf0e10cSrcweir             if( Module::getUrlFromAddress(
119*cdf0e10cSrcweir                     reinterpret_cast< oslGenericFunction >(getLibDir), libDir ) )
120*cdf0e10cSrcweir             {
121*cdf0e10cSrcweir                 libDir = OUString( libDir.getStr(), libDir.lastIndexOf('/' ) );
122*cdf0e10cSrcweir                 OUString name ( RTL_CONSTASCII_USTRINGPARAM( "PYUNOLIBDIR" ) );
123*cdf0e10cSrcweir                 rtl_bootstrap_set( name.pData, libDir.pData );
124*cdf0e10cSrcweir             }
125*cdf0e10cSrcweir             pLibDir = &libDir;
126*cdf0e10cSrcweir         }
127*cdf0e10cSrcweir     }
128*cdf0e10cSrcweir     return *pLibDir;
129*cdf0e10cSrcweir }
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir void raisePySystemException( const char * exceptionType, const OUString & message )
132*cdf0e10cSrcweir {
133*cdf0e10cSrcweir     OStringBuffer buf;
134*cdf0e10cSrcweir     buf.append( "Error during bootstrapping uno (");
135*cdf0e10cSrcweir     buf.append( exceptionType );
136*cdf0e10cSrcweir     buf.append( "):" );
137*cdf0e10cSrcweir     buf.append( OUStringToOString( message, osl_getThreadTextEncoding() ) );
138*cdf0e10cSrcweir     PyErr_SetString( PyExc_SystemError, buf.makeStringAndClear().getStr() );
139*cdf0e10cSrcweir }
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir extern "C" {
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir static PyObject* getComponentContext (PyObject*, PyObject*)
144*cdf0e10cSrcweir {
145*cdf0e10cSrcweir     PyRef ret;
146*cdf0e10cSrcweir     try
147*cdf0e10cSrcweir     {
148*cdf0e10cSrcweir         Reference<XComponentContext> ctx;
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir         // getLibDir() must be called in order to set bootstrap variables correctly !
151*cdf0e10cSrcweir         OUString path( getLibDir());
152*cdf0e10cSrcweir         if( Runtime::isInitialized() )
153*cdf0e10cSrcweir         {
154*cdf0e10cSrcweir             Runtime runtime;
155*cdf0e10cSrcweir             ctx = runtime.getImpl()->cargo->xContext;
156*cdf0e10cSrcweir         }
157*cdf0e10cSrcweir         else
158*cdf0e10cSrcweir         {
159*cdf0e10cSrcweir             OUString iniFile;
160*cdf0e10cSrcweir             if( !path.getLength() )
161*cdf0e10cSrcweir             {
162*cdf0e10cSrcweir                 PyErr_SetString(
163*cdf0e10cSrcweir                     PyExc_RuntimeError, "osl_getUrlFromAddress fails, that's why I cannot find ini "
164*cdf0e10cSrcweir                     "file for bootstrapping python uno bridge\n" );
165*cdf0e10cSrcweir                 return NULL;
166*cdf0e10cSrcweir             }
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir             OUStringBuffer iniFileName;
169*cdf0e10cSrcweir             iniFileName.append( path );
170*cdf0e10cSrcweir             iniFileName.appendAscii( "/" );
171*cdf0e10cSrcweir             iniFileName.appendAscii( SAL_CONFIGFILE( "pyuno" ) );
172*cdf0e10cSrcweir             iniFile = iniFileName.makeStringAndClear();
173*cdf0e10cSrcweir             osl::DirectoryItem item;
174*cdf0e10cSrcweir             if( osl::DirectoryItem::get( iniFile, item ) == item.E_None )
175*cdf0e10cSrcweir             {
176*cdf0e10cSrcweir                 // in case pyuno.ini exists, use this file for bootstrapping
177*cdf0e10cSrcweir                 PyThreadDetach antiguard;
178*cdf0e10cSrcweir                 ctx = cppu::defaultBootstrap_InitialComponentContext (iniFile);
179*cdf0e10cSrcweir             }
180*cdf0e10cSrcweir             else
181*cdf0e10cSrcweir             {
182*cdf0e10cSrcweir                 // defaulting to the standard bootstrapping
183*cdf0e10cSrcweir                 PyThreadDetach antiguard;
184*cdf0e10cSrcweir                 ctx = cppu::defaultBootstrap_InitialComponentContext ();
185*cdf0e10cSrcweir             }
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir         }
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir         if( ! Runtime::isInitialized() )
190*cdf0e10cSrcweir         {
191*cdf0e10cSrcweir             Runtime::initialize( ctx );
192*cdf0e10cSrcweir         }
193*cdf0e10cSrcweir         Runtime runtime;
194*cdf0e10cSrcweir         ret = runtime.any2PyObject( makeAny( ctx ) );
195*cdf0e10cSrcweir     }
196*cdf0e10cSrcweir     catch (com::sun::star::registry::InvalidRegistryException &e)
197*cdf0e10cSrcweir     {
198*cdf0e10cSrcweir         // can't use raisePyExceptionWithAny() here, because the function
199*cdf0e10cSrcweir         // does any conversions, which will not work with a
200*cdf0e10cSrcweir         // wrongly bootstrapped pyuno!
201*cdf0e10cSrcweir         raisePySystemException( "InvalidRegistryException", e.Message );
202*cdf0e10cSrcweir     }
203*cdf0e10cSrcweir     catch( com::sun::star::lang::IllegalArgumentException & e)
204*cdf0e10cSrcweir     {
205*cdf0e10cSrcweir         raisePySystemException( "IllegalArgumentException", e.Message );
206*cdf0e10cSrcweir     }
207*cdf0e10cSrcweir     catch( com::sun::star::script::CannotConvertException & e)
208*cdf0e10cSrcweir     {
209*cdf0e10cSrcweir         raisePySystemException( "CannotConvertException", e.Message );
210*cdf0e10cSrcweir     }
211*cdf0e10cSrcweir     catch (com::sun::star::uno::RuntimeException & e)
212*cdf0e10cSrcweir     {
213*cdf0e10cSrcweir         raisePySystemException( "RuntimeException", e.Message );
214*cdf0e10cSrcweir     }
215*cdf0e10cSrcweir     catch (com::sun::star::uno::Exception & e)
216*cdf0e10cSrcweir     {
217*cdf0e10cSrcweir         raisePySystemException( "uno::Exception", e.Message );
218*cdf0e10cSrcweir     }
219*cdf0e10cSrcweir     return ret.getAcquired();
220*cdf0e10cSrcweir }
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir PyObject * extractOneStringArg( PyObject *args, char const *funcName )
223*cdf0e10cSrcweir {
224*cdf0e10cSrcweir     if( !PyTuple_Check( args ) || PyTuple_Size( args) != 1 )
225*cdf0e10cSrcweir     {
226*cdf0e10cSrcweir         OStringBuffer buf;
227*cdf0e10cSrcweir         buf.append( funcName ).append( ": expecting one string argument" );
228*cdf0e10cSrcweir         PyErr_SetString( PyExc_RuntimeError, buf.getStr() );
229*cdf0e10cSrcweir         return NULL;
230*cdf0e10cSrcweir     }
231*cdf0e10cSrcweir     PyObject *obj = PyTuple_GetItem( args, 0 );
232*cdf0e10cSrcweir     if( !PyString_Check( obj ) && ! PyUnicode_Check(obj))
233*cdf0e10cSrcweir     {
234*cdf0e10cSrcweir         OStringBuffer buf;
235*cdf0e10cSrcweir         buf.append( funcName ).append( ": expecting one string argument" );
236*cdf0e10cSrcweir         PyErr_SetString( PyExc_TypeError, buf.getStr());
237*cdf0e10cSrcweir         return NULL;
238*cdf0e10cSrcweir     }
239*cdf0e10cSrcweir     return obj;
240*cdf0e10cSrcweir }
241*cdf0e10cSrcweir 
242*cdf0e10cSrcweir static PyObject *createUnoStructHelper(PyObject *, PyObject* args )
243*cdf0e10cSrcweir {
244*cdf0e10cSrcweir     Any IdlStruct;
245*cdf0e10cSrcweir     PyRef ret;
246*cdf0e10cSrcweir 
247*cdf0e10cSrcweir     try
248*cdf0e10cSrcweir     {
249*cdf0e10cSrcweir         Runtime runtime;
250*cdf0e10cSrcweir         if( PyTuple_Size( args ) == 2 )
251*cdf0e10cSrcweir         {
252*cdf0e10cSrcweir             PyObject *structName = PyTuple_GetItem( args,0 );
253*cdf0e10cSrcweir             PyObject *initializer = PyTuple_GetItem( args ,1 );
254*cdf0e10cSrcweir 
255*cdf0e10cSrcweir             if( PyString_Check( structName ) )
256*cdf0e10cSrcweir             {
257*cdf0e10cSrcweir                 if( PyTuple_Check( initializer ) )
258*cdf0e10cSrcweir                 {
259*cdf0e10cSrcweir                     OUString typeName( OUString::createFromAscii(PyString_AsString(structName)));
260*cdf0e10cSrcweir                     RuntimeCargo *c = runtime.getImpl()->cargo;
261*cdf0e10cSrcweir                     Reference<XIdlClass> idl_class ( c->xCoreReflection->forName (typeName),UNO_QUERY);
262*cdf0e10cSrcweir                     if (idl_class.is ())
263*cdf0e10cSrcweir                     {
264*cdf0e10cSrcweir                         idl_class->createObject (IdlStruct);
265*cdf0e10cSrcweir                         PyUNO *me = (PyUNO*)PyUNO_new_UNCHECKED( IdlStruct, c->xInvocation );
266*cdf0e10cSrcweir                         PyRef returnCandidate( (PyObject*)me, SAL_NO_ACQUIRE );
267*cdf0e10cSrcweir                         if( PyTuple_Size( initializer ) > 0 )
268*cdf0e10cSrcweir                         {
269*cdf0e10cSrcweir                             TypeDescription desc( typeName );
270*cdf0e10cSrcweir                             OSL_ASSERT( desc.is() ); // could already instantiate an XInvocation2 !
271*cdf0e10cSrcweir 
272*cdf0e10cSrcweir                             typelib_CompoundTypeDescription *pCompType =
273*cdf0e10cSrcweir                                 ( typelib_CompoundTypeDescription * ) desc.get();
274*cdf0e10cSrcweir                             sal_Int32 n = fillStructWithInitializer(
275*cdf0e10cSrcweir                                 me->members->xInvocation, pCompType, initializer, runtime );
276*cdf0e10cSrcweir                             if( n != PyTuple_Size(initializer) )
277*cdf0e10cSrcweir                             {
278*cdf0e10cSrcweir                                 OUStringBuffer buf;
279*cdf0e10cSrcweir                                 buf.appendAscii( "pyuno._createUnoStructHelper: wrong number of ");
280*cdf0e10cSrcweir                                 buf.appendAscii( "elements in the initializer list, expected " );
281*cdf0e10cSrcweir                                 buf.append( n );
282*cdf0e10cSrcweir                                 buf.appendAscii( ", got " );
283*cdf0e10cSrcweir                                 buf.append( (sal_Int32) PyTuple_Size(initializer) );
284*cdf0e10cSrcweir                                 throw RuntimeException(
285*cdf0e10cSrcweir                                     buf.makeStringAndClear(), Reference< XInterface > ());
286*cdf0e10cSrcweir                             }
287*cdf0e10cSrcweir                         }
288*cdf0e10cSrcweir                         ret = returnCandidate;
289*cdf0e10cSrcweir                     }
290*cdf0e10cSrcweir                     else
291*cdf0e10cSrcweir                     {
292*cdf0e10cSrcweir                         OStringBuffer buf;
293*cdf0e10cSrcweir                         buf.append( "UNO struct " );
294*cdf0e10cSrcweir                         buf.append( PyString_AsString(structName) );
295*cdf0e10cSrcweir                         buf.append( " is unkown" );
296*cdf0e10cSrcweir                         PyErr_SetString (PyExc_RuntimeError, buf.getStr());
297*cdf0e10cSrcweir                     }
298*cdf0e10cSrcweir                 }
299*cdf0e10cSrcweir                 else
300*cdf0e10cSrcweir                 {
301*cdf0e10cSrcweir                     PyErr_SetString(
302*cdf0e10cSrcweir                         PyExc_RuntimeError,
303*cdf0e10cSrcweir                         "pyuno._createUnoStructHelper: 2nd argument (initializer sequence) is no tuple" );
304*cdf0e10cSrcweir                 }
305*cdf0e10cSrcweir             }
306*cdf0e10cSrcweir             else
307*cdf0e10cSrcweir             {
308*cdf0e10cSrcweir                 PyErr_SetString (PyExc_AttributeError, "createUnoStruct: first argument wasn't a string");
309*cdf0e10cSrcweir             }
310*cdf0e10cSrcweir         }
311*cdf0e10cSrcweir         else
312*cdf0e10cSrcweir         {
313*cdf0e10cSrcweir             PyErr_SetString (PyExc_AttributeError, "1 Arguments: Structure Name");
314*cdf0e10cSrcweir         }
315*cdf0e10cSrcweir     }
316*cdf0e10cSrcweir     catch( com::sun::star::uno::RuntimeException & e )
317*cdf0e10cSrcweir     {
318*cdf0e10cSrcweir         raisePyExceptionWithAny( makeAny( e ) );
319*cdf0e10cSrcweir     }
320*cdf0e10cSrcweir     catch( com::sun::star::script::CannotConvertException & e )
321*cdf0e10cSrcweir     {
322*cdf0e10cSrcweir         raisePyExceptionWithAny( makeAny( e ) );
323*cdf0e10cSrcweir     }
324*cdf0e10cSrcweir     catch( com::sun::star::uno::Exception & e )
325*cdf0e10cSrcweir     {
326*cdf0e10cSrcweir         raisePyExceptionWithAny( makeAny( e ) );
327*cdf0e10cSrcweir     }
328*cdf0e10cSrcweir     return ret.getAcquired();
329*cdf0e10cSrcweir }
330*cdf0e10cSrcweir 
331*cdf0e10cSrcweir static PyObject *getTypeByName( PyObject *, PyObject *args )
332*cdf0e10cSrcweir {
333*cdf0e10cSrcweir     PyObject * ret = NULL;
334*cdf0e10cSrcweir 
335*cdf0e10cSrcweir     try
336*cdf0e10cSrcweir     {
337*cdf0e10cSrcweir         char *name;
338*cdf0e10cSrcweir 
339*cdf0e10cSrcweir         if (PyArg_ParseTuple (args, const_cast< char * >("s"), &name))
340*cdf0e10cSrcweir         {
341*cdf0e10cSrcweir             OUString typeName ( OUString::createFromAscii( name ) );
342*cdf0e10cSrcweir             TypeDescription typeDesc( typeName );
343*cdf0e10cSrcweir             if( typeDesc.is() )
344*cdf0e10cSrcweir             {
345*cdf0e10cSrcweir                 Runtime runtime;
346*cdf0e10cSrcweir                 ret = PyUNO_Type_new(
347*cdf0e10cSrcweir                     name, (com::sun::star::uno::TypeClass)typeDesc.get()->eTypeClass, runtime );
348*cdf0e10cSrcweir             }
349*cdf0e10cSrcweir             else
350*cdf0e10cSrcweir             {
351*cdf0e10cSrcweir                 OStringBuffer buf;
352*cdf0e10cSrcweir                 buf.append( "Type " ).append(name).append( " is unknown" );
353*cdf0e10cSrcweir                 PyErr_SetString( PyExc_RuntimeError, buf.getStr() );
354*cdf0e10cSrcweir             }
355*cdf0e10cSrcweir         }
356*cdf0e10cSrcweir     }
357*cdf0e10cSrcweir     catch ( RuntimeException & e )
358*cdf0e10cSrcweir     {
359*cdf0e10cSrcweir         raisePyExceptionWithAny( makeAny( e ) );
360*cdf0e10cSrcweir     }
361*cdf0e10cSrcweir     return ret;
362*cdf0e10cSrcweir }
363*cdf0e10cSrcweir 
364*cdf0e10cSrcweir static PyObject *getConstantByName( PyObject *, PyObject *args )
365*cdf0e10cSrcweir {
366*cdf0e10cSrcweir     PyObject *ret = 0;
367*cdf0e10cSrcweir     try
368*cdf0e10cSrcweir     {
369*cdf0e10cSrcweir         char *name;
370*cdf0e10cSrcweir 
371*cdf0e10cSrcweir         if (PyArg_ParseTuple (args, const_cast< char * >("s"), &name))
372*cdf0e10cSrcweir         {
373*cdf0e10cSrcweir             OUString typeName ( OUString::createFromAscii( name ) );
374*cdf0e10cSrcweir             Runtime runtime;
375*cdf0e10cSrcweir             Any a = runtime.getImpl()->cargo->xTdMgr->getByHierarchicalName(typeName);
376*cdf0e10cSrcweir             if( a.getValueType().getTypeClass() ==
377*cdf0e10cSrcweir                 com::sun::star::uno::TypeClass_INTERFACE )
378*cdf0e10cSrcweir             {
379*cdf0e10cSrcweir                 // a idl constant cannot be an instance of an uno-object, thus
380*cdf0e10cSrcweir                 // this cannot be a constant
381*cdf0e10cSrcweir                 OUStringBuffer buf;
382*cdf0e10cSrcweir                 buf.appendAscii( "pyuno.getConstantByName: " ).append( typeName );
383*cdf0e10cSrcweir                 buf.appendAscii( "is not a constant" );
384*cdf0e10cSrcweir                 throw RuntimeException(buf.makeStringAndClear(), Reference< XInterface > () );
385*cdf0e10cSrcweir             }
386*cdf0e10cSrcweir             PyRef constant = runtime.any2PyObject( a );
387*cdf0e10cSrcweir             ret = constant.getAcquired();
388*cdf0e10cSrcweir         }
389*cdf0e10cSrcweir     }
390*cdf0e10cSrcweir     catch( NoSuchElementException & e )
391*cdf0e10cSrcweir     {
392*cdf0e10cSrcweir         // to the python programmer, this is a runtime exception,
393*cdf0e10cSrcweir         // do not support tweakings with the type system
394*cdf0e10cSrcweir         RuntimeException runExc( e.Message, Reference< XInterface > () );
395*cdf0e10cSrcweir         raisePyExceptionWithAny( makeAny( runExc ) );
396*cdf0e10cSrcweir     }
397*cdf0e10cSrcweir     catch( com::sun::star::script::CannotConvertException & e)
398*cdf0e10cSrcweir     {
399*cdf0e10cSrcweir         raisePyExceptionWithAny( makeAny( e ) );
400*cdf0e10cSrcweir     }
401*cdf0e10cSrcweir     catch( com::sun::star::lang::IllegalArgumentException & e)
402*cdf0e10cSrcweir     {
403*cdf0e10cSrcweir         raisePyExceptionWithAny( makeAny( e ) );
404*cdf0e10cSrcweir     }
405*cdf0e10cSrcweir     catch( RuntimeException & e )
406*cdf0e10cSrcweir     {
407*cdf0e10cSrcweir         raisePyExceptionWithAny( makeAny(e) );
408*cdf0e10cSrcweir     }
409*cdf0e10cSrcweir     return ret;
410*cdf0e10cSrcweir }
411*cdf0e10cSrcweir 
412*cdf0e10cSrcweir static PyObject *checkType( PyObject *, PyObject *args )
413*cdf0e10cSrcweir {
414*cdf0e10cSrcweir     if( !PyTuple_Check( args ) || PyTuple_Size( args) != 1 )
415*cdf0e10cSrcweir     {
416*cdf0e10cSrcweir         OStringBuffer buf;
417*cdf0e10cSrcweir         buf.append( "pyuno.checkType : expecting one uno.Type argument" );
418*cdf0e10cSrcweir         PyErr_SetString( PyExc_RuntimeError, buf.getStr() );
419*cdf0e10cSrcweir         return NULL;
420*cdf0e10cSrcweir     }
421*cdf0e10cSrcweir     PyObject *obj = PyTuple_GetItem( args, 0 );
422*cdf0e10cSrcweir 
423*cdf0e10cSrcweir     try
424*cdf0e10cSrcweir     {
425*cdf0e10cSrcweir         PyType2Type( obj );
426*cdf0e10cSrcweir     }
427*cdf0e10cSrcweir     catch( RuntimeException & e)
428*cdf0e10cSrcweir     {
429*cdf0e10cSrcweir         raisePyExceptionWithAny( makeAny( e ) );
430*cdf0e10cSrcweir         return NULL;
431*cdf0e10cSrcweir     }
432*cdf0e10cSrcweir     Py_INCREF( Py_None );
433*cdf0e10cSrcweir     return Py_None;
434*cdf0e10cSrcweir }
435*cdf0e10cSrcweir 
436*cdf0e10cSrcweir static PyObject *checkEnum( PyObject *, PyObject *args )
437*cdf0e10cSrcweir {
438*cdf0e10cSrcweir     if( !PyTuple_Check( args ) || PyTuple_Size( args) != 1 )
439*cdf0e10cSrcweir     {
440*cdf0e10cSrcweir         OStringBuffer buf;
441*cdf0e10cSrcweir         buf.append( "pyuno.checkType : expecting one uno.Type argument" );
442*cdf0e10cSrcweir         PyErr_SetString( PyExc_RuntimeError, buf.getStr() );
443*cdf0e10cSrcweir         return NULL;
444*cdf0e10cSrcweir     }
445*cdf0e10cSrcweir     PyObject *obj = PyTuple_GetItem( args, 0 );
446*cdf0e10cSrcweir 
447*cdf0e10cSrcweir     try
448*cdf0e10cSrcweir     {
449*cdf0e10cSrcweir         PyEnum2Enum( obj );
450*cdf0e10cSrcweir     }
451*cdf0e10cSrcweir     catch( RuntimeException & e)
452*cdf0e10cSrcweir     {
453*cdf0e10cSrcweir         raisePyExceptionWithAny( makeAny( e) );
454*cdf0e10cSrcweir         return NULL;
455*cdf0e10cSrcweir     }
456*cdf0e10cSrcweir     Py_INCREF( Py_None );
457*cdf0e10cSrcweir     return Py_None;
458*cdf0e10cSrcweir }
459*cdf0e10cSrcweir 
460*cdf0e10cSrcweir static PyObject *getClass( PyObject *, PyObject *args )
461*cdf0e10cSrcweir {
462*cdf0e10cSrcweir     PyObject *obj = extractOneStringArg( args, "pyuno.getClass");
463*cdf0e10cSrcweir     if( ! obj )
464*cdf0e10cSrcweir         return NULL;
465*cdf0e10cSrcweir 
466*cdf0e10cSrcweir     try
467*cdf0e10cSrcweir     {
468*cdf0e10cSrcweir         Runtime runtime;
469*cdf0e10cSrcweir         PyRef ret = getClass(
470*cdf0e10cSrcweir             OUString( PyString_AsString( obj), strlen(PyString_AsString(obj)),RTL_TEXTENCODING_ASCII_US),
471*cdf0e10cSrcweir             runtime );
472*cdf0e10cSrcweir         Py_XINCREF( ret.get() );
473*cdf0e10cSrcweir         return ret.get();
474*cdf0e10cSrcweir     }
475*cdf0e10cSrcweir     catch( RuntimeException & e)
476*cdf0e10cSrcweir     {
477*cdf0e10cSrcweir         // NOOPT !!!
478*cdf0e10cSrcweir         // gcc 3.2.3 crashes here in the regcomp test scenario
479*cdf0e10cSrcweir         // only since migration to python 2.3.4 ???? strange thing
480*cdf0e10cSrcweir         // optimization switched off for this module !
481*cdf0e10cSrcweir         raisePyExceptionWithAny( makeAny(e) );
482*cdf0e10cSrcweir     }
483*cdf0e10cSrcweir     return NULL;
484*cdf0e10cSrcweir }
485*cdf0e10cSrcweir 
486*cdf0e10cSrcweir static PyObject *isInterface( PyObject *, PyObject *args )
487*cdf0e10cSrcweir {
488*cdf0e10cSrcweir 
489*cdf0e10cSrcweir     if( PyTuple_Check( args ) && PyTuple_Size( args ) == 1 )
490*cdf0e10cSrcweir     {
491*cdf0e10cSrcweir         PyObject *obj = PyTuple_GetItem( args, 0 );
492*cdf0e10cSrcweir         Runtime r;
493*cdf0e10cSrcweir         return PyInt_FromLong( isInterfaceClass( r, obj ) );
494*cdf0e10cSrcweir     }
495*cdf0e10cSrcweir     return PyInt_FromLong( 0 );
496*cdf0e10cSrcweir }
497*cdf0e10cSrcweir 
498*cdf0e10cSrcweir static PyObject * generateUuid( PyObject *, PyObject * )
499*cdf0e10cSrcweir {
500*cdf0e10cSrcweir     Sequence< sal_Int8 > seq( 16 );
501*cdf0e10cSrcweir     rtl_createUuid( (sal_uInt8*)seq.getArray() , 0 , sal_False );
502*cdf0e10cSrcweir     PyRef ret;
503*cdf0e10cSrcweir     try
504*cdf0e10cSrcweir     {
505*cdf0e10cSrcweir         Runtime runtime;
506*cdf0e10cSrcweir         ret = runtime.any2PyObject( makeAny( seq ) );
507*cdf0e10cSrcweir     }
508*cdf0e10cSrcweir     catch( RuntimeException & e )
509*cdf0e10cSrcweir     {
510*cdf0e10cSrcweir         raisePyExceptionWithAny( makeAny(e) );
511*cdf0e10cSrcweir     }
512*cdf0e10cSrcweir     return ret.getAcquired();
513*cdf0e10cSrcweir }
514*cdf0e10cSrcweir 
515*cdf0e10cSrcweir static PyObject *systemPathToFileUrl( PyObject *, PyObject * args )
516*cdf0e10cSrcweir {
517*cdf0e10cSrcweir     PyObject *obj = extractOneStringArg( args, "pyuno.systemPathToFileUrl" );
518*cdf0e10cSrcweir     if( ! obj )
519*cdf0e10cSrcweir         return NULL;
520*cdf0e10cSrcweir 
521*cdf0e10cSrcweir     OUString sysPath = pyString2ustring( obj );
522*cdf0e10cSrcweir     OUString url;
523*cdf0e10cSrcweir     osl::FileBase::RC e = osl::FileBase::getFileURLFromSystemPath( sysPath, url );
524*cdf0e10cSrcweir 
525*cdf0e10cSrcweir     if( e != osl::FileBase::E_None )
526*cdf0e10cSrcweir     {
527*cdf0e10cSrcweir         OUStringBuffer buf;
528*cdf0e10cSrcweir         buf.appendAscii( "Couldn't convert " );
529*cdf0e10cSrcweir         buf.append( sysPath );
530*cdf0e10cSrcweir         buf.appendAscii( " to a file url for reason (" );
531*cdf0e10cSrcweir         buf.append( (sal_Int32) e );
532*cdf0e10cSrcweir         buf.appendAscii( ")" );
533*cdf0e10cSrcweir         raisePyExceptionWithAny(
534*cdf0e10cSrcweir             makeAny( RuntimeException( buf.makeStringAndClear(), Reference< XInterface > () )));
535*cdf0e10cSrcweir         return NULL;
536*cdf0e10cSrcweir     }
537*cdf0e10cSrcweir     return ustring2PyUnicode( url ).getAcquired();
538*cdf0e10cSrcweir }
539*cdf0e10cSrcweir 
540*cdf0e10cSrcweir static PyObject * fileUrlToSystemPath( PyObject *, PyObject * args )
541*cdf0e10cSrcweir {
542*cdf0e10cSrcweir     PyObject *obj = extractOneStringArg( args, "pyuno.fileUrlToSystemPath" );
543*cdf0e10cSrcweir     if( ! obj )
544*cdf0e10cSrcweir         return NULL;
545*cdf0e10cSrcweir 
546*cdf0e10cSrcweir     OUString url = pyString2ustring( obj );
547*cdf0e10cSrcweir     OUString sysPath;
548*cdf0e10cSrcweir     osl::FileBase::RC e = osl::FileBase::getSystemPathFromFileURL( url, sysPath );
549*cdf0e10cSrcweir 
550*cdf0e10cSrcweir     if( e != osl::FileBase::E_None )
551*cdf0e10cSrcweir     {
552*cdf0e10cSrcweir         OUStringBuffer buf;
553*cdf0e10cSrcweir         buf.appendAscii( "Couldn't convert file url " );
554*cdf0e10cSrcweir         buf.append( sysPath );
555*cdf0e10cSrcweir         buf.appendAscii( " to a system path for reason (" );
556*cdf0e10cSrcweir         buf.append( (sal_Int32) e );
557*cdf0e10cSrcweir         buf.appendAscii( ")" );
558*cdf0e10cSrcweir         raisePyExceptionWithAny(
559*cdf0e10cSrcweir             makeAny( RuntimeException( buf.makeStringAndClear(), Reference< XInterface > () )));
560*cdf0e10cSrcweir         return NULL;
561*cdf0e10cSrcweir     }
562*cdf0e10cSrcweir     return ustring2PyUnicode( sysPath ).getAcquired();
563*cdf0e10cSrcweir }
564*cdf0e10cSrcweir 
565*cdf0e10cSrcweir static PyObject * absolutize( PyObject *, PyObject * args )
566*cdf0e10cSrcweir {
567*cdf0e10cSrcweir     if( PyTuple_Check( args ) && PyTuple_Size( args ) == 2 )
568*cdf0e10cSrcweir     {
569*cdf0e10cSrcweir         OUString ouPath = pyString2ustring( PyTuple_GetItem( args , 0 ) );
570*cdf0e10cSrcweir         OUString ouRel = pyString2ustring( PyTuple_GetItem( args, 1 ) );
571*cdf0e10cSrcweir         OUString ret;
572*cdf0e10cSrcweir         oslFileError e = osl_getAbsoluteFileURL( ouPath.pData, ouRel.pData, &(ret.pData) );
573*cdf0e10cSrcweir         if( e != osl_File_E_None )
574*cdf0e10cSrcweir         {
575*cdf0e10cSrcweir             OUStringBuffer buf;
576*cdf0e10cSrcweir             buf.appendAscii( "Couldn't absolutize " );
577*cdf0e10cSrcweir             buf.append( ouRel );
578*cdf0e10cSrcweir             buf.appendAscii( " using root " );
579*cdf0e10cSrcweir             buf.append( ouPath );
580*cdf0e10cSrcweir             buf.appendAscii( " for reason (" );
581*cdf0e10cSrcweir             buf.append( (sal_Int32) e );
582*cdf0e10cSrcweir             buf.appendAscii( ")" );
583*cdf0e10cSrcweir 
584*cdf0e10cSrcweir             PyErr_SetString(
585*cdf0e10cSrcweir                 PyExc_OSError,
586*cdf0e10cSrcweir                 OUStringToOString(buf.makeStringAndClear(),osl_getThreadTextEncoding()));
587*cdf0e10cSrcweir             return 0;
588*cdf0e10cSrcweir         }
589*cdf0e10cSrcweir         return ustring2PyUnicode( ret ).getAcquired();
590*cdf0e10cSrcweir     }
591*cdf0e10cSrcweir     return 0;
592*cdf0e10cSrcweir }
593*cdf0e10cSrcweir 
594*cdf0e10cSrcweir static PyObject * invoke ( PyObject *, PyObject * args )
595*cdf0e10cSrcweir {
596*cdf0e10cSrcweir     PyObject *ret = 0;
597*cdf0e10cSrcweir     if( PyTuple_Check( args ) && PyTuple_Size( args ) == 3 )
598*cdf0e10cSrcweir     {
599*cdf0e10cSrcweir         PyObject *object = PyTuple_GetItem( args, 0 );
600*cdf0e10cSrcweir 
601*cdf0e10cSrcweir         if( PyString_Check( PyTuple_GetItem( args, 1 ) ) )
602*cdf0e10cSrcweir         {
603*cdf0e10cSrcweir             const char *name = PyString_AsString( PyTuple_GetItem( args, 1 ) );
604*cdf0e10cSrcweir             if( PyTuple_Check( PyTuple_GetItem( args , 2 )))
605*cdf0e10cSrcweir             {
606*cdf0e10cSrcweir                 ret = PyUNO_invoke( object, name , PyTuple_GetItem( args, 2 ) );
607*cdf0e10cSrcweir             }
608*cdf0e10cSrcweir             else
609*cdf0e10cSrcweir             {
610*cdf0e10cSrcweir                 OStringBuffer buf;
611*cdf0e10cSrcweir                 buf.append( "uno.invoke expects a tuple as 3rd argument, got " );
612*cdf0e10cSrcweir                 buf.append( PyString_AsString( PyObject_Str( PyTuple_GetItem( args, 2) ) ) );
613*cdf0e10cSrcweir                 PyErr_SetString( PyExc_RuntimeError, buf.makeStringAndClear() );
614*cdf0e10cSrcweir             }
615*cdf0e10cSrcweir         }
616*cdf0e10cSrcweir         else
617*cdf0e10cSrcweir         {
618*cdf0e10cSrcweir             OStringBuffer buf;
619*cdf0e10cSrcweir             buf.append( "uno.invoke expected a string as 2nd argument, got " );
620*cdf0e10cSrcweir             buf.append( PyString_AsString( PyObject_Str( PyTuple_GetItem( args, 1) ) ) );
621*cdf0e10cSrcweir             PyErr_SetString( PyExc_RuntimeError, buf.makeStringAndClear() );
622*cdf0e10cSrcweir         }
623*cdf0e10cSrcweir     }
624*cdf0e10cSrcweir     else
625*cdf0e10cSrcweir     {
626*cdf0e10cSrcweir         OStringBuffer buf;
627*cdf0e10cSrcweir         buf.append( "uno.invoke expects object, name, (arg1, arg2, ... )\n" );
628*cdf0e10cSrcweir         PyErr_SetString( PyExc_RuntimeError, buf.makeStringAndClear() );
629*cdf0e10cSrcweir     }
630*cdf0e10cSrcweir     return ret;
631*cdf0e10cSrcweir }
632*cdf0e10cSrcweir 
633*cdf0e10cSrcweir static PyObject *getCurrentContext( PyObject *, PyObject * )
634*cdf0e10cSrcweir {
635*cdf0e10cSrcweir     PyRef ret;
636*cdf0e10cSrcweir     try
637*cdf0e10cSrcweir     {
638*cdf0e10cSrcweir         Runtime runtime;
639*cdf0e10cSrcweir         ret = runtime.any2PyObject(
640*cdf0e10cSrcweir             makeAny( com::sun::star::uno::getCurrentContext() ) );
641*cdf0e10cSrcweir     }
642*cdf0e10cSrcweir     catch( com::sun::star::uno::Exception & e )
643*cdf0e10cSrcweir     {
644*cdf0e10cSrcweir         raisePyExceptionWithAny( makeAny( e ) );
645*cdf0e10cSrcweir     }
646*cdf0e10cSrcweir     return ret.getAcquired();
647*cdf0e10cSrcweir }
648*cdf0e10cSrcweir 
649*cdf0e10cSrcweir static PyObject *setCurrentContext( PyObject *, PyObject * args )
650*cdf0e10cSrcweir {
651*cdf0e10cSrcweir     PyRef ret;
652*cdf0e10cSrcweir     try
653*cdf0e10cSrcweir     {
654*cdf0e10cSrcweir         if( PyTuple_Check( args ) && PyTuple_Size( args ) == 1 )
655*cdf0e10cSrcweir         {
656*cdf0e10cSrcweir 
657*cdf0e10cSrcweir             Runtime runtime;
658*cdf0e10cSrcweir             Any a = runtime.pyObject2Any( PyTuple_GetItem( args, 0 ) );
659*cdf0e10cSrcweir 
660*cdf0e10cSrcweir             Reference< com::sun::star::uno::XCurrentContext > context;
661*cdf0e10cSrcweir 
662*cdf0e10cSrcweir             if( (a.hasValue() && (a >>= context)) || ! a.hasValue() )
663*cdf0e10cSrcweir             {
664*cdf0e10cSrcweir                 ret = com::sun::star::uno::setCurrentContext( context ) ? Py_True : Py_False;
665*cdf0e10cSrcweir             }
666*cdf0e10cSrcweir             else
667*cdf0e10cSrcweir             {
668*cdf0e10cSrcweir                 OStringBuffer buf;
669*cdf0e10cSrcweir                 buf.append( "uno.setCurrentContext expects an XComponentContext implementation, got " );
670*cdf0e10cSrcweir                 buf.append( PyString_AsString( PyObject_Str( PyTuple_GetItem( args, 0) ) ) );
671*cdf0e10cSrcweir                 PyErr_SetString( PyExc_RuntimeError, buf.makeStringAndClear() );
672*cdf0e10cSrcweir             }
673*cdf0e10cSrcweir         }
674*cdf0e10cSrcweir         else
675*cdf0e10cSrcweir         {
676*cdf0e10cSrcweir             OStringBuffer buf;
677*cdf0e10cSrcweir             buf.append( "uno.setCurrentContext expects exactly one argument (the current Context)\n" );
678*cdf0e10cSrcweir             PyErr_SetString( PyExc_RuntimeError, buf.makeStringAndClear() );
679*cdf0e10cSrcweir         }
680*cdf0e10cSrcweir     }
681*cdf0e10cSrcweir     catch( com::sun::star::uno::Exception & e )
682*cdf0e10cSrcweir     {
683*cdf0e10cSrcweir         raisePyExceptionWithAny( makeAny( e ) );
684*cdf0e10cSrcweir     }
685*cdf0e10cSrcweir     return ret.getAcquired();
686*cdf0e10cSrcweir }
687*cdf0e10cSrcweir 
688*cdf0e10cSrcweir }
689*cdf0e10cSrcweir 
690*cdf0e10cSrcweir struct PyMethodDef PyUNOModule_methods [] =
691*cdf0e10cSrcweir {
692*cdf0e10cSrcweir     {const_cast< char * >("getComponentContext"), getComponentContext, 1, NULL},
693*cdf0e10cSrcweir     {const_cast< char * >("_createUnoStructHelper"), createUnoStructHelper, 2, NULL},
694*cdf0e10cSrcweir     {const_cast< char * >("getTypeByName"), getTypeByName, 1, NULL},
695*cdf0e10cSrcweir     {const_cast< char * >("getConstantByName"), getConstantByName,1, NULL},
696*cdf0e10cSrcweir     {const_cast< char * >("getClass"), getClass,1, NULL},
697*cdf0e10cSrcweir     {const_cast< char * >("checkEnum"), checkEnum, 1, NULL},
698*cdf0e10cSrcweir     {const_cast< char * >("checkType"), checkType, 1, NULL},
699*cdf0e10cSrcweir     {const_cast< char * >("generateUuid"), generateUuid,0, NULL},
700*cdf0e10cSrcweir     {const_cast< char * >("systemPathToFileUrl"),systemPathToFileUrl,1, NULL},
701*cdf0e10cSrcweir     {const_cast< char * >("fileUrlToSystemPath"),fileUrlToSystemPath,1, NULL},
702*cdf0e10cSrcweir     {const_cast< char * >("absolutize"),absolutize,2, NULL},
703*cdf0e10cSrcweir     {const_cast< char * >("isInterface"),isInterface,1, NULL},
704*cdf0e10cSrcweir     {const_cast< char * >("invoke"),invoke, 2, NULL},
705*cdf0e10cSrcweir     {const_cast< char * >("setCurrentContext"),setCurrentContext,1, NULL},
706*cdf0e10cSrcweir     {const_cast< char * >("getCurrentContext"),getCurrentContext,1, NULL},
707*cdf0e10cSrcweir     {NULL, NULL, 0, NULL}
708*cdf0e10cSrcweir };
709*cdf0e10cSrcweir 
710*cdf0e10cSrcweir }
711*cdf0e10cSrcweir 
712*cdf0e10cSrcweir extern "C" PY_DLLEXPORT void initpyuno()
713*cdf0e10cSrcweir {
714*cdf0e10cSrcweir     // noop when called already, otherwise needed to allow multiple threads
715*cdf0e10cSrcweir     PyEval_InitThreads();
716*cdf0e10cSrcweir     Py_InitModule (const_cast< char * >("pyuno"), PyUNOModule_methods);
717*cdf0e10cSrcweir }
718