Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 : #include "pyuno_impl.hxx"
20 :
21 : #include <rtl/ustrbuf.hxx>
22 : #include <rtl/strbuf.hxx>
23 :
24 : #include <typelib/typedescription.hxx>
25 :
26 : using rtl::OString;
27 : using rtl::OUString;
28 : using rtl::OUStringBuffer;
29 : using rtl::OUStringToOString;
30 : using rtl::OStringBuffer;
31 :
32 : using com::sun::star::uno::TypeClass;
33 : using com::sun::star::uno::Type;
34 : using com::sun::star::uno::RuntimeException;
35 : using com::sun::star::uno::Any;
36 : using com::sun::star::uno::XInterface;
37 : using com::sun::star::uno::Reference;
38 : using com::sun::star::uno::TypeDescription;
39 :
40 : #define USTR_ASCII(x) rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( x ) )
41 : namespace pyuno
42 : {
43 0 : const char *typeClassToString( TypeClass t )
44 : {
45 0 : const char * ret = 0;
46 0 : switch (t)
47 : {
48 : case com::sun::star::uno::TypeClass_VOID:
49 0 : ret = "VOID"; break;
50 : case com::sun::star::uno::TypeClass_CHAR:
51 0 : ret = "CHAR"; break;
52 : case com::sun::star::uno::TypeClass_BOOLEAN:
53 0 : ret = "BOOLEAN"; break;
54 : case com::sun::star::uno::TypeClass_BYTE:
55 0 : ret = "BYTE"; break;
56 : case com::sun::star::uno::TypeClass_SHORT:
57 0 : ret = "SHORT"; break;
58 : case com::sun::star::uno::TypeClass_UNSIGNED_SHORT:
59 0 : ret = "UNSIGNED_SHORT"; break;
60 : case com::sun::star::uno::TypeClass_LONG:
61 0 : ret = "LONG"; break;
62 : case com::sun::star::uno::TypeClass_UNSIGNED_LONG:
63 0 : ret = "UNSIGNED_LONG"; break;
64 : case com::sun::star::uno::TypeClass_HYPER:
65 0 : ret = "HYPER"; break;
66 : case com::sun::star::uno::TypeClass_UNSIGNED_HYPER:
67 0 : ret = "UNSIGNED_HYPER"; break;
68 : case com::sun::star::uno::TypeClass_FLOAT:
69 0 : ret = "FLOAT"; break;
70 : case com::sun::star::uno::TypeClass_DOUBLE:
71 0 : ret = "DOUBLE"; break;
72 : case com::sun::star::uno::TypeClass_STRING:
73 0 : ret = "STRING"; break;
74 : case com::sun::star::uno::TypeClass_TYPE:
75 0 : ret = "TYPE"; break;
76 : case com::sun::star::uno::TypeClass_ANY:
77 0 : ret = "ANY";break;
78 : case com::sun::star::uno::TypeClass_ENUM:
79 0 : ret = "ENUM";break;
80 : case com::sun::star::uno::TypeClass_STRUCT:
81 0 : ret = "STRUCT"; break;
82 : case com::sun::star::uno::TypeClass_EXCEPTION:
83 0 : ret = "EXCEPTION"; break;
84 : case com::sun::star::uno::TypeClass_SEQUENCE:
85 0 : ret = "SEQUENCE"; break;
86 : case com::sun::star::uno::TypeClass_INTERFACE:
87 0 : ret = "INTERFACE"; break;
88 : case com::sun::star::uno::TypeClass_TYPEDEF:
89 0 : ret = "TYPEDEF"; break;
90 : case com::sun::star::uno::TypeClass_UNION:
91 0 : ret = "UNION"; break;
92 : case com::sun::star::uno::TypeClass_ARRAY:
93 0 : ret = "ARRAY"; break;
94 : case com::sun::star::uno::TypeClass_SERVICE:
95 0 : ret = "SERVICE"; break;
96 : case com::sun::star::uno::TypeClass_MODULE:
97 0 : ret = "MODULE"; break;
98 : case com::sun::star::uno::TypeClass_INTERFACE_METHOD:
99 0 : ret = "INTERFACE_METHOD"; break;
100 : case com::sun::star::uno::TypeClass_INTERFACE_ATTRIBUTE:
101 0 : ret = "INTERFACE_ATTRIBUTE"; break;
102 : default:
103 0 : ret = "UNKNOWN"; break;
104 : }
105 0 : return ret;
106 : }
107 :
108 0 : static PyRef getClass( const Runtime & r , const char * name)
109 : {
110 0 : return PyRef( PyDict_GetItemString( r.getImpl()->cargo->getUnoModule().get(), (char*) name ) );
111 : }
112 :
113 0 : PyRef getTypeClass( const Runtime & r )
114 : {
115 0 : return getClass( r , "Type" );
116 : }
117 :
118 0 : PyRef getEnumClass( const Runtime & r )
119 : {
120 0 : return getClass( r , "Enum" );
121 : }
122 :
123 0 : PyRef getCharClass( const Runtime & r )
124 : {
125 0 : return getClass( r , "Char" );
126 : }
127 :
128 0 : PyRef getByteSequenceClass( const Runtime & r )
129 : {
130 0 : return getClass( r , "ByteSequence" );
131 : }
132 :
133 0 : PyRef getAnyClass( const Runtime & r )
134 : {
135 0 : return getClass( r , "Any" );
136 : }
137 :
138 :
139 0 : sal_Unicode PyChar2Unicode( PyObject *obj ) throw ( RuntimeException )
140 : {
141 0 : PyRef value( PyObject_GetAttrString( obj, "value" ), SAL_NO_ACQUIRE );
142 0 : if( ! PyUnicode_Check( value.get() ) )
143 : {
144 : throw RuntimeException(
145 : USTR_ASCII( "attribute value of uno.Char is not a unicode string" ),
146 0 : Reference< XInterface > () );
147 : }
148 :
149 0 : if( PyUnicode_GetSize( value.get() ) < 1 )
150 : {
151 : throw RuntimeException(
152 : USTR_ASCII( "uno.Char contains an empty unicode string" ),
153 0 : Reference< XInterface > () );
154 : }
155 :
156 0 : sal_Unicode c = (sal_Unicode)PyUnicode_AsUnicode( value.get() )[0];
157 0 : return c;
158 : }
159 :
160 0 : Any PyEnum2Enum( PyObject *obj ) throw ( RuntimeException )
161 : {
162 0 : Any ret;
163 0 : PyRef typeName( PyObject_GetAttrString( obj,"typeName" ), SAL_NO_ACQUIRE);
164 0 : PyRef value( PyObject_GetAttrString( obj, "value" ), SAL_NO_ACQUIRE);
165 0 : if( !PyStr_Check( typeName.get() ) || ! PyStr_Check( value.get() ) )
166 : {
167 : throw RuntimeException(
168 : USTR_ASCII( "attributes typeName and/or value of uno.Enum are not strings" ),
169 0 : Reference< XInterface > () );
170 : }
171 :
172 0 : OUString strTypeName( OUString::createFromAscii( PyStr_AsString( typeName.get() ) ) );
173 0 : char *stringValue = PyStr_AsString( value.get() );
174 :
175 0 : TypeDescription desc( strTypeName );
176 0 : if( desc.is() )
177 : {
178 0 : if(desc.get()->eTypeClass != typelib_TypeClass_ENUM )
179 : {
180 0 : OUStringBuffer buf;
181 0 : buf.appendAscii( "pyuno.checkEnum: " ).append(strTypeName).appendAscii( "is a " );
182 : buf.appendAscii(
183 0 : typeClassToString( (com::sun::star::uno::TypeClass) desc.get()->eTypeClass));
184 0 : buf.appendAscii( ", expected ENUM" );
185 0 : throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface> () );
186 : }
187 :
188 0 : desc.makeComplete();
189 :
190 0 : typelib_EnumTypeDescription *pEnumDesc = (typelib_EnumTypeDescription*) desc.get();
191 0 : int i = 0;
192 0 : for( i = 0; i < pEnumDesc->nEnumValues ; i ++ )
193 : {
194 0 : if( (*((OUString *)&pEnumDesc->ppEnumNames[i])).compareToAscii( stringValue ) == 0 )
195 : {
196 0 : break;
197 : }
198 : }
199 0 : if( i == pEnumDesc->nEnumValues )
200 : {
201 0 : OUStringBuffer buf;
202 0 : buf.appendAscii( "value " ).appendAscii( stringValue ).appendAscii( "is unknown in enum " );
203 0 : buf.appendAscii( PyStr_AsString( typeName.get() ) );
204 0 : throw RuntimeException( buf.makeStringAndClear(), Reference<XInterface> () );
205 : }
206 0 : ret = Any( &pEnumDesc->pEnumValues[i], desc.get()->pWeakRef );
207 : }
208 : else
209 : {
210 0 : OUStringBuffer buf;
211 0 : buf.appendAscii( "enum " ).appendAscii( PyStr_AsString(typeName.get()) ).appendAscii( " is unknown" );
212 0 : throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface> () );
213 : }
214 0 : return ret;
215 : }
216 :
217 :
218 0 : Type PyType2Type( PyObject * o ) throw(RuntimeException )
219 : {
220 0 : PyRef pyName( PyObject_GetAttrString( o, "typeName" ), SAL_NO_ACQUIRE);
221 0 : if( !PyStr_Check( pyName.get() ) )
222 : {
223 : throw RuntimeException(
224 : USTR_ASCII( "type object does not have typeName property" ),
225 0 : Reference< XInterface > () );
226 : }
227 :
228 0 : PyRef pyTC( PyObject_GetAttrString( o, "typeClass" ), SAL_NO_ACQUIRE );
229 0 : Any enumValue = PyEnum2Enum( pyTC.get() );
230 :
231 0 : OUString name( OUString::createFromAscii( PyStr_AsString( pyName.get() ) ) );
232 0 : TypeDescription desc( name );
233 0 : if( ! desc.is() )
234 : {
235 0 : OUStringBuffer buf;
236 0 : buf.appendAscii( "type " ).append(name).appendAscii( " is unknown" );
237 : throw RuntimeException(
238 0 : buf.makeStringAndClear(), Reference< XInterface > () );
239 : }
240 0 : if( desc.get()->eTypeClass != (typelib_TypeClass) *(sal_Int32*)enumValue.getValue() )
241 : {
242 0 : OUStringBuffer buf;
243 0 : buf.appendAscii( "pyuno.checkType: " ).append(name).appendAscii( " is a " );
244 0 : buf.appendAscii( typeClassToString( (TypeClass) desc.get()->eTypeClass) );
245 0 : buf.appendAscii( ", but type got construct with typeclass " );
246 0 : buf.appendAscii( typeClassToString( (TypeClass) *(sal_Int32*)enumValue.getValue() ) );
247 : throw RuntimeException(
248 0 : buf.makeStringAndClear(), Reference< XInterface > () );
249 : }
250 0 : return desc.get()->pWeakRef;
251 : }
252 :
253 0 : static PyObject* callCtor( const Runtime &r , const char * clazz, const PyRef & args )
254 : {
255 0 : PyRef code( PyDict_GetItemString( r.getImpl()->cargo->getUnoModule().get(), (char*)clazz ) );
256 0 : if( ! code.is() )
257 : {
258 0 : OStringBuffer buf;
259 0 : buf.append( "couldn't access uno." );
260 0 : buf.append( clazz );
261 0 : PyErr_SetString( PyExc_RuntimeError, buf.getStr() );
262 0 : return NULL;
263 : }
264 0 : PyRef instance( PyObject_CallObject( code.get(), args.get() ), SAL_NO_ACQUIRE);
265 0 : Py_XINCREF( instance.get() );
266 0 : return instance.get();
267 :
268 : }
269 :
270 :
271 0 : PyObject *PyUNO_Enum_new( const char *enumBase, const char *enumValue, const Runtime &r )
272 : {
273 0 : PyRef args( PyTuple_New( 2 ), SAL_NO_ACQUIRE );
274 0 : PyTuple_SetItem( args.get() , 0 , PyStr_FromString( enumBase ) );
275 0 : PyTuple_SetItem( args.get() , 1 , PyStr_FromString( enumValue ) );
276 :
277 0 : return callCtor( r, "Enum" , args );
278 : }
279 :
280 :
281 0 : PyObject* PyUNO_Type_new (const char *typeName , TypeClass t , const Runtime &r )
282 : {
283 : // retrieve type object
284 0 : PyRef args( PyTuple_New( 2 ), SAL_NO_ACQUIRE );
285 :
286 0 : PyTuple_SetItem( args.get() , 0 , PyStr_FromString( typeName ) );
287 0 : PyObject *typeClass = PyUNO_Enum_new( "com.sun.star.uno.TypeClass" , typeClassToString(t), r );
288 0 : if( ! typeClass )
289 0 : return NULL;
290 0 : PyTuple_SetItem( args.get() , 1 , typeClass);
291 :
292 0 : return callCtor( r, "Type" , args );
293 : }
294 :
295 0 : PyObject* PyUNO_char_new ( sal_Unicode val , const Runtime &r )
296 : {
297 : // retrieve type object
298 0 : PyRef args( PyTuple_New( 1 ), SAL_NO_ACQUIRE );
299 :
300 : Py_UNICODE u[2];
301 0 : u[0] = val;
302 0 : u[1] = 0;
303 0 : PyTuple_SetItem( args.get() , 0 , PyUnicode_FromUnicode( u ,1) );
304 :
305 0 : return callCtor( r, "Char" , args );
306 : }
307 :
308 0 : PyObject *PyUNO_ByteSequence_new(
309 : const com::sun::star::uno::Sequence< sal_Int8 > &byteSequence, const Runtime &r )
310 : {
311 : PyRef str(
312 0 : PyStrBytes_FromStringAndSize( (char*)byteSequence.getConstArray(), byteSequence.getLength()),
313 0 : SAL_NO_ACQUIRE );
314 0 : PyRef args( PyTuple_New( 1 ), SAL_NO_ACQUIRE );
315 0 : PyTuple_SetItem( args.get() , 0 , str.getAcquired() );
316 0 : return callCtor( r, "ByteSequence" , args );
317 :
318 : }
319 : }
320 :
321 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|