167c7d1c1SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
367c7d1c1SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
467c7d1c1SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
567c7d1c1SAndrew Rist  * distributed with this work for additional information
667c7d1c1SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
767c7d1c1SAndrew Rist  * to you under the Apache License, Version 2.0 (the
867c7d1c1SAndrew Rist  * "License"); you may not use this file except in compliance
967c7d1c1SAndrew Rist  * with the License.  You may obtain a copy of the License at
1067c7d1c1SAndrew Rist  *
1167c7d1c1SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
1267c7d1c1SAndrew Rist  *
1367c7d1c1SAndrew Rist  * Unless required by applicable law or agreed to in writing,
1467c7d1c1SAndrew Rist  * software distributed under the License is distributed on an
1567c7d1c1SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1667c7d1c1SAndrew Rist  * KIND, either express or implied.  See the License for the
1767c7d1c1SAndrew Rist  * specific language governing permissions and limitations
1867c7d1c1SAndrew Rist  * under the License.
1967c7d1c1SAndrew Rist  *
2067c7d1c1SAndrew Rist  *************************************************************/
2167c7d1c1SAndrew Rist 
2267c7d1c1SAndrew Rist 
23cdf0e10cSrcweir #include "pyuno_impl.hxx"
24cdf0e10cSrcweir 
25cdf0e10cSrcweir #include <osl/thread.h>
26cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
27cdf0e10cSrcweir 
28cdf0e10cSrcweir using rtl::OUStringToOString;
29cdf0e10cSrcweir using rtl::OUString;
30cdf0e10cSrcweir using com::sun::star::uno::Sequence;
31cdf0e10cSrcweir using com::sun::star::uno::Reference;
32cdf0e10cSrcweir using com::sun::star::uno::XInterface;
33cdf0e10cSrcweir using com::sun::star::uno::Any;
34cdf0e10cSrcweir using com::sun::star::uno::Type;
35cdf0e10cSrcweir using com::sun::star::uno::TypeClass;
36cdf0e10cSrcweir using com::sun::star::uno::RuntimeException;
37cdf0e10cSrcweir using com::sun::star::uno::XComponentContext;
38cdf0e10cSrcweir using com::sun::star::lang::XSingleServiceFactory;
39cdf0e10cSrcweir using com::sun::star::script::XTypeConverter;
40cdf0e10cSrcweir using com::sun::star::script::XInvocation2;
41cdf0e10cSrcweir 
42cdf0e10cSrcweir namespace pyuno
43cdf0e10cSrcweir {
44cdf0e10cSrcweir typedef struct
45cdf0e10cSrcweir {
46cdf0e10cSrcweir     Reference<XInvocation2> xInvocation;
47cdf0e10cSrcweir     Reference<XSingleServiceFactory> xInvocationFactory;
48cdf0e10cSrcweir     Reference<XTypeConverter> xTypeConverter;
49cdf0e10cSrcweir     OUString methodName;
50cdf0e10cSrcweir     ConversionMode mode;
51cdf0e10cSrcweir } PyUNO_callable_Internals;
52cdf0e10cSrcweir 
53cdf0e10cSrcweir typedef struct
54cdf0e10cSrcweir {
55cdf0e10cSrcweir     PyObject_HEAD
56cdf0e10cSrcweir     PyUNO_callable_Internals* members;
57cdf0e10cSrcweir } PyUNO_callable;
58cdf0e10cSrcweir 
59cdf0e10cSrcweir void PyUNO_callable_del (PyObject* self)
60cdf0e10cSrcweir {
61cdf0e10cSrcweir     PyUNO_callable* me;
62cdf0e10cSrcweir 
63cdf0e10cSrcweir     me = (PyUNO_callable*) self;
64cdf0e10cSrcweir     delete me->members;
65cdf0e10cSrcweir     PyObject_Del (self);
66cdf0e10cSrcweir 
67cdf0e10cSrcweir     return;
68cdf0e10cSrcweir }
69cdf0e10cSrcweir 
70cdf0e10cSrcweir PyObject* PyUNO_callable_call (PyObject* self, PyObject* args, PyObject*)
71cdf0e10cSrcweir {
72cdf0e10cSrcweir     PyUNO_callable* me;
73cdf0e10cSrcweir 
74cdf0e10cSrcweir     Sequence<short> aOutParamIndex;
75cdf0e10cSrcweir     Sequence<Any> aOutParam;
76cdf0e10cSrcweir     Sequence<Any> aParams;
77cdf0e10cSrcweir     Sequence<Type> aParamTypes;
78cdf0e10cSrcweir     Any any_params;
79cdf0e10cSrcweir     Any out_params;
80cdf0e10cSrcweir     Any ret_value;
81cdf0e10cSrcweir     RuntimeCargo *cargo = 0;
82cdf0e10cSrcweir     me = (PyUNO_callable*) self;
83cdf0e10cSrcweir 
84cdf0e10cSrcweir     PyRef ret;
85cdf0e10cSrcweir     try
86cdf0e10cSrcweir     {
87cdf0e10cSrcweir         Runtime runtime;
88cdf0e10cSrcweir         cargo = runtime.getImpl()->cargo;
89cdf0e10cSrcweir         any_params = runtime.pyObject2Any (args, me->members->mode);
90cdf0e10cSrcweir 
91cdf0e10cSrcweir         if (any_params.getValueTypeClass () == com::sun::star::uno::TypeClass_SEQUENCE)
92cdf0e10cSrcweir         {
93cdf0e10cSrcweir             any_params >>= aParams;
94cdf0e10cSrcweir         }
95cdf0e10cSrcweir         else
96cdf0e10cSrcweir         {
97cdf0e10cSrcweir             aParams.realloc (1);
98cdf0e10cSrcweir             aParams [0] <<= any_params;
99cdf0e10cSrcweir         }
100cdf0e10cSrcweir 
101cdf0e10cSrcweir         {
102cdf0e10cSrcweir             PyThreadDetach antiguard; //pyhton free zone
103cdf0e10cSrcweir 
104cdf0e10cSrcweir             // do some logging if desired ...
105cdf0e10cSrcweir             if( isLog( cargo, LogLevel::CALL ) )
106cdf0e10cSrcweir             {
107cdf0e10cSrcweir                 logCall( cargo, "try     py->uno[0x", me->members->xInvocation.get(),
108cdf0e10cSrcweir                          me->members->methodName, aParams );
109cdf0e10cSrcweir             }
110cdf0e10cSrcweir 
111cdf0e10cSrcweir             // do the call
112cdf0e10cSrcweir             ret_value = me->members->xInvocation->invoke (
113cdf0e10cSrcweir                 me->members->methodName, aParams, aOutParamIndex, aOutParam);
114cdf0e10cSrcweir 
115cdf0e10cSrcweir             // log the reply, if desired
116cdf0e10cSrcweir             if( isLog( cargo, LogLevel::CALL ) )
117cdf0e10cSrcweir             {
118cdf0e10cSrcweir                 logReply( cargo, "success py->uno[0x", me->members->xInvocation.get(),
119cdf0e10cSrcweir                           me->members->methodName, ret_value, aOutParam);
120cdf0e10cSrcweir             }
121cdf0e10cSrcweir         }
122cdf0e10cSrcweir 
123cdf0e10cSrcweir 
124cdf0e10cSrcweir         PyRef temp = runtime.any2PyObject (ret_value);
125cdf0e10cSrcweir         if( aOutParam.getLength() )
126cdf0e10cSrcweir         {
127cdf0e10cSrcweir             PyRef return_list( PyTuple_New (1+aOutParam.getLength()), SAL_NO_ACQUIRE );
128cdf0e10cSrcweir             PyTuple_SetItem (return_list.get(), 0, temp.getAcquired());
129cdf0e10cSrcweir 
130cdf0e10cSrcweir             // initialize with defaults in case of exceptions
131cdf0e10cSrcweir             int i;
132cdf0e10cSrcweir             for( i = 1 ; i < 1+aOutParam.getLength() ; i ++ )
133cdf0e10cSrcweir             {
134cdf0e10cSrcweir                 Py_INCREF( Py_None );
135cdf0e10cSrcweir                 PyTuple_SetItem( return_list.get() , i , Py_None );
136cdf0e10cSrcweir             }
137cdf0e10cSrcweir 
138cdf0e10cSrcweir             for( i = 0 ; i < aOutParam.getLength() ; i ++ )
139cdf0e10cSrcweir             {
140cdf0e10cSrcweir                 PyRef ref = runtime.any2PyObject( aOutParam[i] );
141cdf0e10cSrcweir                 PyTuple_SetItem (return_list.get(), 1+i, ref.getAcquired());
142cdf0e10cSrcweir             }
143cdf0e10cSrcweir             ret = return_list;
144cdf0e10cSrcweir         }
145cdf0e10cSrcweir         else
146cdf0e10cSrcweir         {
147cdf0e10cSrcweir             ret = temp;
148cdf0e10cSrcweir         }
149cdf0e10cSrcweir     }
150cdf0e10cSrcweir     catch( com::sun::star::reflection::InvocationTargetException & e )
151cdf0e10cSrcweir     {
152cdf0e10cSrcweir 
153cdf0e10cSrcweir         if( isLog( cargo, LogLevel::CALL ) )
154cdf0e10cSrcweir         {
155cdf0e10cSrcweir             logException( cargo, "except  py->uno[0x", me->members->xInvocation.get() ,
156cdf0e10cSrcweir                           me->members->methodName, e.TargetException.getValue(), e.TargetException.getValueTypeRef());
157cdf0e10cSrcweir         }
158cdf0e10cSrcweir         raisePyExceptionWithAny( e.TargetException );
159cdf0e10cSrcweir     }
160cdf0e10cSrcweir     catch( com::sun::star::script::CannotConvertException &e )
161cdf0e10cSrcweir     {
162cdf0e10cSrcweir         if( isLog( cargo, LogLevel::CALL ) )
163cdf0e10cSrcweir         {
164cdf0e10cSrcweir             logException( cargo, "error  py->uno[0x", me->members->xInvocation.get() ,
165cdf0e10cSrcweir                           me->members->methodName, &e, getCppuType(&e).getTypeLibType());
166cdf0e10cSrcweir         }
167cdf0e10cSrcweir         raisePyExceptionWithAny( com::sun::star::uno::makeAny( e ) );
168cdf0e10cSrcweir     }
169cdf0e10cSrcweir     catch( com::sun::star::lang::IllegalArgumentException &e )
170cdf0e10cSrcweir     {
171cdf0e10cSrcweir         if( isLog( cargo, LogLevel::CALL ) )
172cdf0e10cSrcweir         {
173cdf0e10cSrcweir             logException( cargo, "error  py->uno[0x", me->members->xInvocation.get() ,
174cdf0e10cSrcweir                           me->members->methodName, &e, getCppuType(&e).getTypeLibType());
175cdf0e10cSrcweir         }
176cdf0e10cSrcweir         raisePyExceptionWithAny( com::sun::star::uno::makeAny( e ) );
177cdf0e10cSrcweir     }
178cdf0e10cSrcweir     catch (::com::sun::star::uno::RuntimeException &e)
179cdf0e10cSrcweir     {
180cdf0e10cSrcweir         if( cargo && isLog( cargo, LogLevel::CALL ) )
181cdf0e10cSrcweir         {
182cdf0e10cSrcweir             logException( cargo, "error  py->uno[0x", me->members->xInvocation.get() ,
183cdf0e10cSrcweir                           me->members->methodName, &e, getCppuType(&e).getTypeLibType());
184cdf0e10cSrcweir         }
185cdf0e10cSrcweir         raisePyExceptionWithAny( com::sun::star::uno::makeAny( e ) );
186cdf0e10cSrcweir     }
187cdf0e10cSrcweir 
188cdf0e10cSrcweir     return ret.getAcquired();
189cdf0e10cSrcweir }
190cdf0e10cSrcweir 
191cdf0e10cSrcweir 
192cdf0e10cSrcweir static PyTypeObject PyUNO_callable_Type =
193cdf0e10cSrcweir {
194*5c3821d8SPedro Giffuni     PyVarObject_HEAD_INIT(&PyType_Type, 0)
195cdf0e10cSrcweir     const_cast< char * >("PyUNO_callable"),
196cdf0e10cSrcweir     sizeof (PyUNO_callable),
197cdf0e10cSrcweir     0,
198cdf0e10cSrcweir     (destructor) ::pyuno::PyUNO_callable_del,
199cdf0e10cSrcweir     (printfunc) 0,
200cdf0e10cSrcweir     (getattrfunc) 0,
201cdf0e10cSrcweir     (setattrfunc) 0,
202cdf0e10cSrcweir     (cmpfunc) 0,
203cdf0e10cSrcweir     (reprfunc) 0,
204cdf0e10cSrcweir     0,
205cdf0e10cSrcweir     0,
206cdf0e10cSrcweir     0,
207cdf0e10cSrcweir     (hashfunc) 0,
208cdf0e10cSrcweir     (ternaryfunc) ::pyuno::PyUNO_callable_call,
209cdf0e10cSrcweir     (reprfunc) 0,
210cdf0e10cSrcweir         (getattrofunc)0,
211cdf0e10cSrcweir     (setattrofunc)0,
212cdf0e10cSrcweir     NULL,
213cdf0e10cSrcweir     0,
214cdf0e10cSrcweir     NULL,
215cdf0e10cSrcweir     (traverseproc)0,
216cdf0e10cSrcweir     (inquiry)0,
217cdf0e10cSrcweir     (richcmpfunc)0,
218cdf0e10cSrcweir     0,
219cdf0e10cSrcweir     (getiterfunc)0,
220cdf0e10cSrcweir     (iternextfunc)0,
221cdf0e10cSrcweir     NULL,
222cdf0e10cSrcweir     NULL,
223cdf0e10cSrcweir     NULL,
224cdf0e10cSrcweir     NULL,
225cdf0e10cSrcweir     NULL,
226cdf0e10cSrcweir     (descrgetfunc)0,
227cdf0e10cSrcweir     (descrsetfunc)0,
228cdf0e10cSrcweir     0,
229cdf0e10cSrcweir     (initproc)0,
230cdf0e10cSrcweir     (allocfunc)0,
231cdf0e10cSrcweir     (newfunc)0,
232cdf0e10cSrcweir     (freefunc)0,
233cdf0e10cSrcweir     (inquiry)0,
234cdf0e10cSrcweir     NULL,
235cdf0e10cSrcweir     NULL,
236cdf0e10cSrcweir     NULL,
237cdf0e10cSrcweir     NULL,
238cdf0e10cSrcweir     NULL,
239cdf0e10cSrcweir     (destructor)0
240cdf0e10cSrcweir #if PY_VERSION_HEX >= 0x02060000
241cdf0e10cSrcweir     , 0
242cdf0e10cSrcweir #endif
243cdf0e10cSrcweir };
244cdf0e10cSrcweir 
245cdf0e10cSrcweir PyRef PyUNO_callable_new (
246cdf0e10cSrcweir     const Reference<XInvocation2> &my_inv,
247cdf0e10cSrcweir     const OUString & methodName,
248cdf0e10cSrcweir     const Reference<XSingleServiceFactory> &xInvocationFactory,
249cdf0e10cSrcweir     const Reference<XTypeConverter> &tc,
250cdf0e10cSrcweir     enum ConversionMode mode )
251cdf0e10cSrcweir {
252cdf0e10cSrcweir     PyUNO_callable* self;
253cdf0e10cSrcweir 
254cdf0e10cSrcweir     self = PyObject_New (PyUNO_callable, &PyUNO_callable_Type);
255cdf0e10cSrcweir     if (self == NULL)
256cdf0e10cSrcweir         return NULL; //NULL == Error!
257cdf0e10cSrcweir 
258cdf0e10cSrcweir     self->members = new PyUNO_callable_Internals;
259cdf0e10cSrcweir     self->members->xInvocation = my_inv;
260cdf0e10cSrcweir     self->members->methodName = methodName;
261cdf0e10cSrcweir     self->members->xInvocationFactory = xInvocationFactory;
262cdf0e10cSrcweir     self->members->xTypeConverter = tc;
263cdf0e10cSrcweir     self->members->mode = mode;
264cdf0e10cSrcweir 
265cdf0e10cSrcweir     return PyRef( (PyObject*)self, SAL_NO_ACQUIRE );
266cdf0e10cSrcweir }
267cdf0e10cSrcweir 
268cdf0e10cSrcweir }
269