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 :
27 : using com::sun::star::uno::TypeClass;
28 : using com::sun::star::uno::Type;
29 : using com::sun::star::uno::RuntimeException;
30 : using com::sun::star::uno::Any;
31 : using com::sun::star::uno::XInterface;
32 : using com::sun::star::uno::Reference;
33 : using com::sun::star::uno::TypeDescription;
34 :
35 : namespace pyuno
36 : {
37 23 : const char *typeClassToString( TypeClass t )
38 : {
39 23 : const char * ret = 0;
40 23 : switch (t)
41 : {
42 : case com::sun::star::uno::TypeClass_VOID:
43 0 : ret = "VOID"; break;
44 : case com::sun::star::uno::TypeClass_CHAR:
45 0 : ret = "CHAR"; break;
46 : case com::sun::star::uno::TypeClass_BOOLEAN:
47 0 : ret = "BOOLEAN"; break;
48 : case com::sun::star::uno::TypeClass_BYTE:
49 0 : ret = "BYTE"; break;
50 : case com::sun::star::uno::TypeClass_SHORT:
51 0 : ret = "SHORT"; break;
52 : case com::sun::star::uno::TypeClass_UNSIGNED_SHORT:
53 0 : ret = "UNSIGNED_SHORT"; break;
54 : case com::sun::star::uno::TypeClass_LONG:
55 0 : ret = "LONG"; break;
56 : case com::sun::star::uno::TypeClass_UNSIGNED_LONG:
57 0 : ret = "UNSIGNED_LONG"; break;
58 : case com::sun::star::uno::TypeClass_HYPER:
59 0 : ret = "HYPER"; break;
60 : case com::sun::star::uno::TypeClass_UNSIGNED_HYPER:
61 0 : ret = "UNSIGNED_HYPER"; break;
62 : case com::sun::star::uno::TypeClass_FLOAT:
63 0 : ret = "FLOAT"; break;
64 : case com::sun::star::uno::TypeClass_DOUBLE:
65 0 : ret = "DOUBLE"; break;
66 : case com::sun::star::uno::TypeClass_STRING:
67 0 : ret = "STRING"; break;
68 : case com::sun::star::uno::TypeClass_TYPE:
69 0 : ret = "TYPE"; break;
70 : case com::sun::star::uno::TypeClass_ANY:
71 0 : ret = "ANY";break;
72 : case com::sun::star::uno::TypeClass_ENUM:
73 0 : ret = "ENUM";break;
74 : case com::sun::star::uno::TypeClass_STRUCT:
75 0 : ret = "STRUCT"; break;
76 : case com::sun::star::uno::TypeClass_EXCEPTION:
77 0 : ret = "EXCEPTION"; break;
78 : case com::sun::star::uno::TypeClass_SEQUENCE:
79 0 : ret = "SEQUENCE"; break;
80 : case com::sun::star::uno::TypeClass_INTERFACE:
81 23 : ret = "INTERFACE"; break;
82 : case com::sun::star::uno::TypeClass_TYPEDEF:
83 0 : ret = "TYPEDEF"; break;
84 : case com::sun::star::uno::TypeClass_SERVICE:
85 0 : ret = "SERVICE"; break;
86 : case com::sun::star::uno::TypeClass_MODULE:
87 0 : ret = "MODULE"; break;
88 : case com::sun::star::uno::TypeClass_INTERFACE_METHOD:
89 0 : ret = "INTERFACE_METHOD"; break;
90 : case com::sun::star::uno::TypeClass_INTERFACE_ATTRIBUTE:
91 0 : ret = "INTERFACE_ATTRIBUTE"; break;
92 : default:
93 0 : ret = "UNKNOWN"; break;
94 : }
95 23 : return ret;
96 : }
97 :
98 462 : static PyRef getClass( const Runtime & r , const char * name)
99 : {
100 462 : return PyRef( PyDict_GetItemString( r.getImpl()->cargo->getUnoModule().get(), name ) );
101 : }
102 :
103 169 : PyRef getTypeClass( const Runtime & r )
104 : {
105 169 : return getClass( r , "Type" );
106 : }
107 :
108 92 : PyRef getEnumClass( const Runtime & r )
109 : {
110 92 : return getClass( r , "Enum" );
111 : }
112 :
113 16 : PyRef getCharClass( const Runtime & r )
114 : {
115 16 : return getClass( r , "Char" );
116 : }
117 :
118 169 : PyRef getByteSequenceClass( const Runtime & r )
119 : {
120 169 : return getClass( r , "ByteSequence" );
121 : }
122 :
123 16 : PyRef getAnyClass( const Runtime & r )
124 : {
125 16 : return getClass( r , "Any" );
126 : }
127 :
128 :
129 0 : sal_Unicode PyChar2Unicode( PyObject *obj ) throw ( RuntimeException )
130 : {
131 0 : PyRef value( PyObject_GetAttrString( obj, "value" ), SAL_NO_ACQUIRE );
132 0 : if( ! PyUnicode_Check( value.get() ) )
133 : {
134 : throw RuntimeException(
135 0 : "attribute value of uno.Char is not a unicode string" );
136 : }
137 :
138 0 : if( PyUnicode_GetSize( value.get() ) < 1 )
139 : {
140 : throw RuntimeException(
141 0 : "uno.Char contains an empty unicode string");
142 : }
143 :
144 0 : sal_Unicode c = (sal_Unicode)PyUnicode_AsUnicode( value.get() )[0];
145 0 : return c;
146 : }
147 :
148 268 : Any PyEnum2Enum( PyObject *obj ) throw ( RuntimeException )
149 : {
150 268 : Any ret;
151 536 : PyRef typeName( PyObject_GetAttrString( obj,"typeName" ), SAL_NO_ACQUIRE);
152 536 : PyRef value( PyObject_GetAttrString( obj, "value" ), SAL_NO_ACQUIRE);
153 268 : if( !PyStr_Check( typeName.get() ) || ! PyStr_Check( value.get() ) )
154 : {
155 : throw RuntimeException(
156 0 : "attributes typeName and/or value of uno.Enum are not strings" );
157 : }
158 :
159 536 : OUString strTypeName( OUString::createFromAscii( PyStr_AsString( typeName.get() ) ) );
160 268 : char *stringValue = PyStr_AsString( value.get() );
161 :
162 536 : TypeDescription desc( strTypeName );
163 268 : if( desc.is() )
164 : {
165 158 : if(desc.get()->eTypeClass != typelib_TypeClass_ENUM )
166 : {
167 0 : OUStringBuffer buf;
168 0 : buf.appendAscii( "pyuno.checkEnum: " ).append(strTypeName).appendAscii( "is a " );
169 : buf.appendAscii(
170 0 : typeClassToString( (com::sun::star::uno::TypeClass) desc.get()->eTypeClass));
171 0 : buf.appendAscii( ", expected ENUM" );
172 0 : throw RuntimeException( buf.makeStringAndClear() );
173 : }
174 :
175 158 : desc.makeComplete();
176 :
177 158 : typelib_EnumTypeDescription *pEnumDesc = reinterpret_cast<typelib_EnumTypeDescription*>(desc.get());
178 158 : int i = 0;
179 2902 : for( i = 0; i < pEnumDesc->nEnumValues ; i ++ )
180 : {
181 2902 : if( OUString::unacquired(&pEnumDesc->ppEnumNames[i]).equalsAscii( stringValue ) )
182 : {
183 158 : break;
184 : }
185 : }
186 158 : if( i == pEnumDesc->nEnumValues )
187 : {
188 0 : OUStringBuffer buf;
189 0 : buf.appendAscii( "value " ).appendAscii( stringValue ).appendAscii( "is unknown in enum " );
190 0 : buf.appendAscii( PyStr_AsString( typeName.get() ) );
191 0 : throw RuntimeException( buf.makeStringAndClear() );
192 : }
193 158 : ret = Any( &pEnumDesc->pEnumValues[i], desc.get()->pWeakRef );
194 : }
195 : else
196 : {
197 110 : OUStringBuffer buf;
198 110 : buf.appendAscii( "enum " ).appendAscii( PyStr_AsString(typeName.get()) ).appendAscii( " is unknown" );
199 110 : throw RuntimeException( buf.makeStringAndClear() );
200 : }
201 316 : return ret;
202 : }
203 :
204 :
205 100 : Type PyType2Type( PyObject * o ) throw(RuntimeException )
206 : {
207 100 : PyRef pyName( PyObject_GetAttrString( o, "typeName" ), SAL_NO_ACQUIRE);
208 100 : if( !PyStr_Check( pyName.get() ) )
209 : {
210 : throw RuntimeException(
211 0 : "type object does not have typeName property" );
212 : }
213 :
214 200 : PyRef pyTC( PyObject_GetAttrString( o, "typeClass" ), SAL_NO_ACQUIRE );
215 200 : Any enumValue = PyEnum2Enum( pyTC.get() );
216 :
217 200 : OUString name( OUString::createFromAscii( PyStr_AsString( pyName.get() ) ) );
218 200 : TypeDescription desc( name );
219 100 : if( ! desc.is() )
220 : {
221 0 : OUStringBuffer buf;
222 0 : buf.appendAscii( "type " ).append(name).appendAscii( " is unknown" );
223 0 : throw RuntimeException( buf.makeStringAndClear() );
224 : }
225 100 : if( desc.get()->eTypeClass != (typelib_TypeClass) *static_cast<sal_Int32 const *>(enumValue.getValue()) )
226 : {
227 0 : OUStringBuffer buf;
228 0 : buf.appendAscii( "pyuno.checkType: " ).append(name).appendAscii( " is a " );
229 0 : buf.appendAscii( typeClassToString( (TypeClass) desc.get()->eTypeClass) );
230 0 : buf.appendAscii( ", but type got construct with typeclass " );
231 0 : buf.appendAscii( typeClassToString( (TypeClass) *static_cast<sal_Int32 const *>(enumValue.getValue()) ) );
232 0 : throw RuntimeException( buf.makeStringAndClear() );
233 : }
234 200 : return desc.get()->pWeakRef;
235 : }
236 :
237 53 : static PyObject* callCtor( const Runtime &r , const char * clazz, const PyRef & args )
238 : {
239 53 : PyRef code( PyDict_GetItemString( r.getImpl()->cargo->getUnoModule().get(), clazz ) );
240 53 : if( ! code.is() )
241 : {
242 0 : OStringBuffer buf;
243 0 : buf.append( "couldn't access uno." );
244 0 : buf.append( clazz );
245 0 : PyErr_SetString( PyExc_RuntimeError, buf.getStr() );
246 0 : return NULL;
247 : }
248 106 : PyRef instance( PyObject_CallObject( code.get(), args.get() ), SAL_NO_ACQUIRE);
249 53 : Py_XINCREF( instance.get() );
250 106 : return instance.get();
251 :
252 : }
253 :
254 :
255 28 : PyObject *PyUNO_Enum_new( const char *enumBase, const char *enumValue, const Runtime &r )
256 : {
257 28 : PyRef args( PyTuple_New( 2 ), SAL_NO_ACQUIRE, NOT_NULL );
258 28 : PyTuple_SetItem( args.get() , 0 , PyStr_FromString( enumBase ) );
259 28 : PyTuple_SetItem( args.get() , 1 , PyStr_FromString( enumValue ) );
260 :
261 28 : return callCtor( r, "Enum" , args );
262 : }
263 :
264 :
265 23 : PyObject* PyUNO_Type_new (const char *typeName , TypeClass t , const Runtime &r )
266 : {
267 : // retrieve type object
268 23 : PyRef args(PyTuple_New( 2 ), SAL_NO_ACQUIRE, NOT_NULL);
269 :
270 23 : PyTuple_SetItem( args.get() , 0 , PyStr_FromString( typeName ) );
271 23 : PyObject *typeClass = PyUNO_Enum_new( "com.sun.star.uno.TypeClass" , typeClassToString(t), r );
272 23 : if( ! typeClass )
273 0 : return NULL;
274 23 : PyTuple_SetItem( args.get() , 1 , typeClass);
275 :
276 23 : return callCtor( r, "Type" , args );
277 : }
278 :
279 0 : PyObject* PyUNO_char_new ( sal_Unicode val , const Runtime &r )
280 : {
281 : // retrieve type object
282 0 : PyRef args( PyTuple_New( 1 ), SAL_NO_ACQUIRE, NOT_NULL );
283 :
284 : Py_UNICODE u[2];
285 0 : u[0] = val;
286 0 : u[1] = 0;
287 0 : PyTuple_SetItem( args.get() , 0 , PyUnicode_FromUnicode( u ,1) );
288 :
289 0 : return callCtor( r, "Char" , args );
290 : }
291 :
292 2 : PyObject *PyUNO_ByteSequence_new(
293 : const com::sun::star::uno::Sequence< sal_Int8 > &byteSequence, const Runtime &r )
294 : {
295 : PyRef str(
296 4 : PyStrBytes_FromStringAndSize( reinterpret_cast<char const *>(byteSequence.getConstArray()), byteSequence.getLength()),
297 4 : SAL_NO_ACQUIRE );
298 4 : PyRef args( PyTuple_New( 1 ), SAL_NO_ACQUIRE, NOT_NULL );
299 2 : PyTuple_SetItem( args.get() , 0 , str.getAcquired() );
300 4 : return callCtor( r, "ByteSequence" , args );
301 :
302 : }
303 : }
304 :
305 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|