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 #include "pyuno_impl.hxx" 28*cdf0e10cSrcweir 29*cdf0e10cSrcweir #include <osl/thread.h> 30*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx> 31*cdf0e10cSrcweir 32*cdf0e10cSrcweir using rtl::OUStringToOString; 33*cdf0e10cSrcweir using rtl::OUString; 34*cdf0e10cSrcweir using com::sun::star::uno::Sequence; 35*cdf0e10cSrcweir using com::sun::star::uno::Reference; 36*cdf0e10cSrcweir using com::sun::star::uno::XInterface; 37*cdf0e10cSrcweir using com::sun::star::uno::Any; 38*cdf0e10cSrcweir using com::sun::star::uno::Type; 39*cdf0e10cSrcweir using com::sun::star::uno::TypeClass; 40*cdf0e10cSrcweir using com::sun::star::uno::RuntimeException; 41*cdf0e10cSrcweir using com::sun::star::uno::XComponentContext; 42*cdf0e10cSrcweir using com::sun::star::lang::XSingleServiceFactory; 43*cdf0e10cSrcweir using com::sun::star::script::XTypeConverter; 44*cdf0e10cSrcweir using com::sun::star::script::XInvocation2; 45*cdf0e10cSrcweir 46*cdf0e10cSrcweir namespace pyuno 47*cdf0e10cSrcweir { 48*cdf0e10cSrcweir typedef struct 49*cdf0e10cSrcweir { 50*cdf0e10cSrcweir Reference<XInvocation2> xInvocation; 51*cdf0e10cSrcweir Reference<XSingleServiceFactory> xInvocationFactory; 52*cdf0e10cSrcweir Reference<XTypeConverter> xTypeConverter; 53*cdf0e10cSrcweir OUString methodName; 54*cdf0e10cSrcweir ConversionMode mode; 55*cdf0e10cSrcweir } PyUNO_callable_Internals; 56*cdf0e10cSrcweir 57*cdf0e10cSrcweir typedef struct 58*cdf0e10cSrcweir { 59*cdf0e10cSrcweir PyObject_HEAD 60*cdf0e10cSrcweir PyUNO_callable_Internals* members; 61*cdf0e10cSrcweir } PyUNO_callable; 62*cdf0e10cSrcweir 63*cdf0e10cSrcweir void PyUNO_callable_del (PyObject* self) 64*cdf0e10cSrcweir { 65*cdf0e10cSrcweir PyUNO_callable* me; 66*cdf0e10cSrcweir 67*cdf0e10cSrcweir me = (PyUNO_callable*) self; 68*cdf0e10cSrcweir delete me->members; 69*cdf0e10cSrcweir PyObject_Del (self); 70*cdf0e10cSrcweir 71*cdf0e10cSrcweir return; 72*cdf0e10cSrcweir } 73*cdf0e10cSrcweir 74*cdf0e10cSrcweir PyObject* PyUNO_callable_call (PyObject* self, PyObject* args, PyObject*) 75*cdf0e10cSrcweir { 76*cdf0e10cSrcweir PyUNO_callable* me; 77*cdf0e10cSrcweir 78*cdf0e10cSrcweir Sequence<short> aOutParamIndex; 79*cdf0e10cSrcweir Sequence<Any> aOutParam; 80*cdf0e10cSrcweir Sequence<Any> aParams; 81*cdf0e10cSrcweir Sequence<Type> aParamTypes; 82*cdf0e10cSrcweir Any any_params; 83*cdf0e10cSrcweir Any out_params; 84*cdf0e10cSrcweir Any ret_value; 85*cdf0e10cSrcweir RuntimeCargo *cargo = 0; 86*cdf0e10cSrcweir me = (PyUNO_callable*) self; 87*cdf0e10cSrcweir 88*cdf0e10cSrcweir PyRef ret; 89*cdf0e10cSrcweir try 90*cdf0e10cSrcweir { 91*cdf0e10cSrcweir Runtime runtime; 92*cdf0e10cSrcweir cargo = runtime.getImpl()->cargo; 93*cdf0e10cSrcweir any_params = runtime.pyObject2Any (args, me->members->mode); 94*cdf0e10cSrcweir 95*cdf0e10cSrcweir if (any_params.getValueTypeClass () == com::sun::star::uno::TypeClass_SEQUENCE) 96*cdf0e10cSrcweir { 97*cdf0e10cSrcweir any_params >>= aParams; 98*cdf0e10cSrcweir } 99*cdf0e10cSrcweir else 100*cdf0e10cSrcweir { 101*cdf0e10cSrcweir aParams.realloc (1); 102*cdf0e10cSrcweir aParams [0] <<= any_params; 103*cdf0e10cSrcweir } 104*cdf0e10cSrcweir 105*cdf0e10cSrcweir { 106*cdf0e10cSrcweir PyThreadDetach antiguard; //pyhton free zone 107*cdf0e10cSrcweir 108*cdf0e10cSrcweir // do some logging if desired ... 109*cdf0e10cSrcweir if( isLog( cargo, LogLevel::CALL ) ) 110*cdf0e10cSrcweir { 111*cdf0e10cSrcweir logCall( cargo, "try py->uno[0x", me->members->xInvocation.get(), 112*cdf0e10cSrcweir me->members->methodName, aParams ); 113*cdf0e10cSrcweir } 114*cdf0e10cSrcweir 115*cdf0e10cSrcweir // do the call 116*cdf0e10cSrcweir ret_value = me->members->xInvocation->invoke ( 117*cdf0e10cSrcweir me->members->methodName, aParams, aOutParamIndex, aOutParam); 118*cdf0e10cSrcweir 119*cdf0e10cSrcweir // log the reply, if desired 120*cdf0e10cSrcweir if( isLog( cargo, LogLevel::CALL ) ) 121*cdf0e10cSrcweir { 122*cdf0e10cSrcweir logReply( cargo, "success py->uno[0x", me->members->xInvocation.get(), 123*cdf0e10cSrcweir me->members->methodName, ret_value, aOutParam); 124*cdf0e10cSrcweir } 125*cdf0e10cSrcweir } 126*cdf0e10cSrcweir 127*cdf0e10cSrcweir 128*cdf0e10cSrcweir PyRef temp = runtime.any2PyObject (ret_value); 129*cdf0e10cSrcweir if( aOutParam.getLength() ) 130*cdf0e10cSrcweir { 131*cdf0e10cSrcweir PyRef return_list( PyTuple_New (1+aOutParam.getLength()), SAL_NO_ACQUIRE ); 132*cdf0e10cSrcweir PyTuple_SetItem (return_list.get(), 0, temp.getAcquired()); 133*cdf0e10cSrcweir 134*cdf0e10cSrcweir // initialize with defaults in case of exceptions 135*cdf0e10cSrcweir int i; 136*cdf0e10cSrcweir for( i = 1 ; i < 1+aOutParam.getLength() ; i ++ ) 137*cdf0e10cSrcweir { 138*cdf0e10cSrcweir Py_INCREF( Py_None ); 139*cdf0e10cSrcweir PyTuple_SetItem( return_list.get() , i , Py_None ); 140*cdf0e10cSrcweir } 141*cdf0e10cSrcweir 142*cdf0e10cSrcweir for( i = 0 ; i < aOutParam.getLength() ; i ++ ) 143*cdf0e10cSrcweir { 144*cdf0e10cSrcweir PyRef ref = runtime.any2PyObject( aOutParam[i] ); 145*cdf0e10cSrcweir PyTuple_SetItem (return_list.get(), 1+i, ref.getAcquired()); 146*cdf0e10cSrcweir } 147*cdf0e10cSrcweir ret = return_list; 148*cdf0e10cSrcweir } 149*cdf0e10cSrcweir else 150*cdf0e10cSrcweir { 151*cdf0e10cSrcweir ret = temp; 152*cdf0e10cSrcweir } 153*cdf0e10cSrcweir } 154*cdf0e10cSrcweir catch( com::sun::star::reflection::InvocationTargetException & e ) 155*cdf0e10cSrcweir { 156*cdf0e10cSrcweir 157*cdf0e10cSrcweir if( isLog( cargo, LogLevel::CALL ) ) 158*cdf0e10cSrcweir { 159*cdf0e10cSrcweir logException( cargo, "except py->uno[0x", me->members->xInvocation.get() , 160*cdf0e10cSrcweir me->members->methodName, e.TargetException.getValue(), e.TargetException.getValueTypeRef()); 161*cdf0e10cSrcweir } 162*cdf0e10cSrcweir raisePyExceptionWithAny( e.TargetException ); 163*cdf0e10cSrcweir } 164*cdf0e10cSrcweir catch( com::sun::star::script::CannotConvertException &e ) 165*cdf0e10cSrcweir { 166*cdf0e10cSrcweir if( isLog( cargo, LogLevel::CALL ) ) 167*cdf0e10cSrcweir { 168*cdf0e10cSrcweir logException( cargo, "error py->uno[0x", me->members->xInvocation.get() , 169*cdf0e10cSrcweir me->members->methodName, &e, getCppuType(&e).getTypeLibType()); 170*cdf0e10cSrcweir } 171*cdf0e10cSrcweir raisePyExceptionWithAny( com::sun::star::uno::makeAny( e ) ); 172*cdf0e10cSrcweir } 173*cdf0e10cSrcweir catch( com::sun::star::lang::IllegalArgumentException &e ) 174*cdf0e10cSrcweir { 175*cdf0e10cSrcweir if( isLog( cargo, LogLevel::CALL ) ) 176*cdf0e10cSrcweir { 177*cdf0e10cSrcweir logException( cargo, "error py->uno[0x", me->members->xInvocation.get() , 178*cdf0e10cSrcweir me->members->methodName, &e, getCppuType(&e).getTypeLibType()); 179*cdf0e10cSrcweir } 180*cdf0e10cSrcweir raisePyExceptionWithAny( com::sun::star::uno::makeAny( e ) ); 181*cdf0e10cSrcweir } 182*cdf0e10cSrcweir catch (::com::sun::star::uno::RuntimeException &e) 183*cdf0e10cSrcweir { 184*cdf0e10cSrcweir if( cargo && isLog( cargo, LogLevel::CALL ) ) 185*cdf0e10cSrcweir { 186*cdf0e10cSrcweir logException( cargo, "error py->uno[0x", me->members->xInvocation.get() , 187*cdf0e10cSrcweir me->members->methodName, &e, getCppuType(&e).getTypeLibType()); 188*cdf0e10cSrcweir } 189*cdf0e10cSrcweir raisePyExceptionWithAny( com::sun::star::uno::makeAny( e ) ); 190*cdf0e10cSrcweir } 191*cdf0e10cSrcweir 192*cdf0e10cSrcweir return ret.getAcquired(); 193*cdf0e10cSrcweir } 194*cdf0e10cSrcweir 195*cdf0e10cSrcweir 196*cdf0e10cSrcweir static PyTypeObject PyUNO_callable_Type = 197*cdf0e10cSrcweir { 198*cdf0e10cSrcweir PyObject_HEAD_INIT (&PyType_Type) 199*cdf0e10cSrcweir 0, 200*cdf0e10cSrcweir const_cast< char * >("PyUNO_callable"), 201*cdf0e10cSrcweir sizeof (PyUNO_callable), 202*cdf0e10cSrcweir 0, 203*cdf0e10cSrcweir (destructor) ::pyuno::PyUNO_callable_del, 204*cdf0e10cSrcweir (printfunc) 0, 205*cdf0e10cSrcweir (getattrfunc) 0, 206*cdf0e10cSrcweir (setattrfunc) 0, 207*cdf0e10cSrcweir (cmpfunc) 0, 208*cdf0e10cSrcweir (reprfunc) 0, 209*cdf0e10cSrcweir 0, 210*cdf0e10cSrcweir 0, 211*cdf0e10cSrcweir 0, 212*cdf0e10cSrcweir (hashfunc) 0, 213*cdf0e10cSrcweir (ternaryfunc) ::pyuno::PyUNO_callable_call, 214*cdf0e10cSrcweir (reprfunc) 0, 215*cdf0e10cSrcweir (getattrofunc)0, 216*cdf0e10cSrcweir (setattrofunc)0, 217*cdf0e10cSrcweir NULL, 218*cdf0e10cSrcweir 0, 219*cdf0e10cSrcweir NULL, 220*cdf0e10cSrcweir (traverseproc)0, 221*cdf0e10cSrcweir (inquiry)0, 222*cdf0e10cSrcweir (richcmpfunc)0, 223*cdf0e10cSrcweir 0, 224*cdf0e10cSrcweir (getiterfunc)0, 225*cdf0e10cSrcweir (iternextfunc)0, 226*cdf0e10cSrcweir NULL, 227*cdf0e10cSrcweir NULL, 228*cdf0e10cSrcweir NULL, 229*cdf0e10cSrcweir NULL, 230*cdf0e10cSrcweir NULL, 231*cdf0e10cSrcweir (descrgetfunc)0, 232*cdf0e10cSrcweir (descrsetfunc)0, 233*cdf0e10cSrcweir 0, 234*cdf0e10cSrcweir (initproc)0, 235*cdf0e10cSrcweir (allocfunc)0, 236*cdf0e10cSrcweir (newfunc)0, 237*cdf0e10cSrcweir (freefunc)0, 238*cdf0e10cSrcweir (inquiry)0, 239*cdf0e10cSrcweir NULL, 240*cdf0e10cSrcweir NULL, 241*cdf0e10cSrcweir NULL, 242*cdf0e10cSrcweir NULL, 243*cdf0e10cSrcweir NULL, 244*cdf0e10cSrcweir (destructor)0 245*cdf0e10cSrcweir #if PY_VERSION_HEX >= 0x02060000 246*cdf0e10cSrcweir , 0 247*cdf0e10cSrcweir #endif 248*cdf0e10cSrcweir }; 249*cdf0e10cSrcweir 250*cdf0e10cSrcweir PyRef PyUNO_callable_new ( 251*cdf0e10cSrcweir const Reference<XInvocation2> &my_inv, 252*cdf0e10cSrcweir const OUString & methodName, 253*cdf0e10cSrcweir const Reference<XSingleServiceFactory> &xInvocationFactory, 254*cdf0e10cSrcweir const Reference<XTypeConverter> &tc, 255*cdf0e10cSrcweir enum ConversionMode mode ) 256*cdf0e10cSrcweir { 257*cdf0e10cSrcweir PyUNO_callable* self; 258*cdf0e10cSrcweir 259*cdf0e10cSrcweir self = PyObject_New (PyUNO_callable, &PyUNO_callable_Type); 260*cdf0e10cSrcweir if (self == NULL) 261*cdf0e10cSrcweir return NULL; //NULL == Error! 262*cdf0e10cSrcweir 263*cdf0e10cSrcweir self->members = new PyUNO_callable_Internals; 264*cdf0e10cSrcweir self->members->xInvocation = my_inv; 265*cdf0e10cSrcweir self->members->methodName = methodName; 266*cdf0e10cSrcweir self->members->xInvocationFactory = xInvocationFactory; 267*cdf0e10cSrcweir self->members->xTypeConverter = tc; 268*cdf0e10cSrcweir self->members->mode = mode; 269*cdf0e10cSrcweir 270*cdf0e10cSrcweir return PyRef( (PyObject*)self, SAL_NO_ACQUIRE ); 271*cdf0e10cSrcweir } 272*cdf0e10cSrcweir 273*cdf0e10cSrcweir } 274