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 :
20 :
21 : #include "propacc.hxx"
22 :
23 : #include <basic/sbstar.hxx>
24 : #include <sbunoobj.hxx>
25 :
26 : #include <limits.h> // USHRT_MAX
27 :
28 : using com::sun::star::uno::Reference;
29 : using namespace com::sun::star;
30 : using namespace com::sun::star::uno;
31 : using namespace com::sun::star::lang;
32 : using namespace com::sun::star::beans;
33 : using namespace cppu;
34 :
35 :
36 : //========================================================================
37 :
38 : // Declaration conversion from Sbx to UNO with known target type
39 : Any sbxToUnoValue( SbxVariable* pVar, const Type& rType, Property* pUnoProperty = NULL );
40 :
41 : //========================================================================
42 :
43 : #ifdef WNT
44 : #define CDECL _cdecl
45 : #endif
46 : #if defined(UNX)
47 : #define CDECL
48 : #endif
49 :
50 0 : int CDECL SbCompare_PropertyValues_Impl( const void *arg1, const void *arg2 )
51 : {
52 0 : return ((PropertyValue*)arg1)->Name.compareTo( ((PropertyValue*)arg2)->Name );
53 : }
54 :
55 : struct SbCompare_UString_PropertyValue_Impl
56 : {
57 : bool operator() (const OUString& lhs, PropertyValue const & rhs)
58 : {
59 : return lhs.compareTo(rhs.Name) < 0;
60 : }
61 0 : bool operator() (PropertyValue const & lhs, const OUString& rhs)
62 : {
63 0 : return lhs.Name.compareTo(rhs) < 0;
64 : }
65 : #ifdef DBG_UTIL
66 : bool operator() (PropertyValue const & lhs, const PropertyValue& rhs)
67 : {
68 : return lhs.Name.compareTo(rhs.Name) < 0;
69 : }
70 : #endif
71 : };
72 :
73 0 : int CDECL SbCompare_Properties_Impl( const void *arg1, const void *arg2 )
74 : {
75 0 : return ((Property*)arg1)->Name.compareTo( ((Property*)arg2)->Name );
76 : }
77 :
78 0 : extern "C" int CDECL SbCompare_UString_Property_Impl( const void *arg1, const void *arg2 )
79 : {
80 0 : const OUString *pArg1 = (OUString*) arg1;
81 0 : const Property *pArg2 = (Property*) arg2;
82 0 : return pArg1->compareTo( pArg2->Name );
83 : }
84 :
85 : //----------------------------------------------------------------------------
86 :
87 0 : SbPropertyValues::SbPropertyValues()
88 : {
89 0 : }
90 :
91 : //----------------------------------------------------------------------------
92 :
93 0 : SbPropertyValues::~SbPropertyValues()
94 : {
95 0 : m_xInfo.clear();
96 0 : }
97 :
98 : //----------------------------------------------------------------------------
99 :
100 0 : Reference< XPropertySetInfo > SbPropertyValues::getPropertySetInfo(void) throw( RuntimeException )
101 : {
102 : // create on demand?
103 0 : if (!m_xInfo.is())
104 : {
105 0 : SbPropertySetInfo *pInfo = new SbPropertySetInfo( m_aPropVals );
106 0 : m_xInfo.set(pInfo);
107 : }
108 0 : return m_xInfo;
109 : }
110 :
111 : //-------------------------------------------------------------------------
112 :
113 0 : size_t SbPropertyValues::GetIndex_Impl( const OUString &rPropName ) const
114 : {
115 : SbPropertyValueArr_Impl::const_iterator it = std::lower_bound(
116 : m_aPropVals.begin(), m_aPropVals.end(), rPropName,
117 0 : SbCompare_UString_PropertyValue_Impl() );
118 0 : if (it == m_aPropVals.end())
119 : {
120 : throw beans::UnknownPropertyException(
121 0 : "Property not found: " + rPropName,
122 0 : const_cast<SbPropertyValues&>(*this));
123 : }
124 0 : return it - m_aPropVals.begin();
125 : }
126 :
127 : //----------------------------------------------------------------------------
128 :
129 0 : void SbPropertyValues::setPropertyValue(
130 : const OUString& aPropertyName,
131 : const Any& aValue)
132 : throw (::com::sun::star::beans::UnknownPropertyException,
133 : ::com::sun::star::beans::PropertyVetoException,
134 : ::com::sun::star::lang::IllegalArgumentException,
135 : ::com::sun::star::lang::WrappedTargetException,
136 : ::com::sun::star::uno::RuntimeException)
137 : {
138 0 : size_t const nIndex = GetIndex_Impl( aPropertyName );
139 0 : PropertyValue & rPropVal = m_aPropVals[nIndex];
140 0 : rPropVal.Value = aValue;
141 0 : }
142 :
143 : //----------------------------------------------------------------------------
144 :
145 0 : Any SbPropertyValues::getPropertyValue(
146 : const OUString& aPropertyName)
147 : throw(::com::sun::star::beans::UnknownPropertyException,
148 : ::com::sun::star::lang::WrappedTargetException,
149 : ::com::sun::star::uno::RuntimeException)
150 : {
151 0 : size_t const nIndex = GetIndex_Impl( aPropertyName );
152 0 : return m_aPropVals[nIndex].Value;
153 : }
154 :
155 : //----------------------------------------------------------------------------
156 :
157 0 : void SbPropertyValues::addPropertyChangeListener(
158 : const OUString& aPropertyName,
159 : const Reference< XPropertyChangeListener >& )
160 : throw ()
161 : {
162 : (void)aPropertyName;
163 0 : }
164 :
165 : //----------------------------------------------------------------------------
166 :
167 0 : void SbPropertyValues::removePropertyChangeListener(
168 : const OUString& aPropertyName,
169 : const Reference< XPropertyChangeListener >& )
170 : throw ()
171 : {
172 : (void)aPropertyName;
173 0 : }
174 :
175 : //----------------------------------------------------------------------------
176 :
177 0 : void SbPropertyValues::addVetoableChangeListener(
178 : const OUString& aPropertyName,
179 : const Reference< XVetoableChangeListener >& )
180 : throw()
181 : {
182 : (void)aPropertyName;
183 0 : }
184 :
185 : //----------------------------------------------------------------------------
186 :
187 0 : void SbPropertyValues::removeVetoableChangeListener(
188 : const OUString& aPropertyName,
189 : const Reference< XVetoableChangeListener >& )
190 : throw()
191 : {
192 : (void)aPropertyName;
193 0 : }
194 :
195 : //----------------------------------------------------------------------------
196 :
197 0 : Sequence< PropertyValue > SbPropertyValues::getPropertyValues(void) throw (::com::sun::star::uno::RuntimeException)
198 : {
199 0 : Sequence<PropertyValue> aRet( m_aPropVals.size() );
200 0 : for (size_t n = 0; n < m_aPropVals.size(); ++n)
201 0 : aRet.getArray()[n] = m_aPropVals[n];
202 0 : return aRet;
203 : }
204 :
205 : //----------------------------------------------------------------------------
206 :
207 0 : void SbPropertyValues::setPropertyValues(const Sequence< PropertyValue >& rPropertyValues )
208 : throw (::com::sun::star::beans::UnknownPropertyException,
209 : ::com::sun::star::beans::PropertyVetoException,
210 : ::com::sun::star::lang::IllegalArgumentException,
211 : ::com::sun::star::lang::WrappedTargetException,
212 : ::com::sun::star::uno::RuntimeException)
213 : {
214 0 : if ( !m_aPropVals.empty() )
215 0 : throw PropertyExistException();
216 :
217 0 : const PropertyValue *pPropVals = rPropertyValues.getConstArray();
218 0 : for (sal_Int32 n = 0; n < rPropertyValues.getLength(); ++n)
219 : {
220 0 : PropertyValue *pPropVal = new PropertyValue(pPropVals[n]);
221 0 : m_aPropVals.push_back( pPropVal );
222 : }
223 0 : }
224 :
225 : //============================================================================
226 : //PropertySetInfoImpl
227 :
228 0 : PropertySetInfoImpl::PropertySetInfoImpl()
229 : {
230 0 : }
231 :
232 0 : sal_Int32 PropertySetInfoImpl::GetIndex_Impl( const OUString &rPropName ) const
233 : {
234 : Property *pP;
235 : pP = (Property*)
236 0 : bsearch( &rPropName, _aProps.getConstArray(), _aProps.getLength(),
237 : sizeof( Property ),
238 0 : SbCompare_UString_Property_Impl );
239 0 : return pP ? sal::static_int_cast<sal_Int32>( pP - _aProps.getConstArray() ) : -1;
240 : }
241 :
242 0 : Sequence< Property > PropertySetInfoImpl::getProperties(void) throw()
243 : {
244 0 : return _aProps;
245 : }
246 :
247 0 : Property PropertySetInfoImpl::getPropertyByName(const OUString& Name) throw( RuntimeException )
248 : {
249 0 : sal_Int32 nIndex = GetIndex_Impl( Name );
250 0 : if( USHRT_MAX != nIndex )
251 0 : return _aProps.getConstArray()[ nIndex ];
252 0 : return Property();
253 : }
254 :
255 0 : sal_Bool PropertySetInfoImpl::hasPropertyByName(const OUString& Name) throw( RuntimeException )
256 : {
257 0 : sal_Int32 nIndex = GetIndex_Impl( Name );
258 0 : return USHRT_MAX != nIndex;
259 : }
260 :
261 :
262 : //----------------------------------------------------------------------------
263 :
264 0 : SbPropertySetInfo::SbPropertySetInfo( const SbPropertyValueArr_Impl &rPropVals )
265 : {
266 0 : aImpl._aProps.realloc( rPropVals.size() );
267 0 : for ( sal_uInt16 n = 0; n < rPropVals.size(); ++n )
268 : {
269 0 : Property &rProp = aImpl._aProps.getArray()[n];
270 0 : const PropertyValue &rPropVal = rPropVals[n];
271 0 : rProp.Name = rPropVal.Name;
272 0 : rProp.Handle = rPropVal.Handle;
273 0 : rProp.Type = getCppuVoidType();
274 0 : rProp.Attributes = 0;
275 : }
276 0 : }
277 :
278 : //----------------------------------------------------------------------------
279 :
280 0 : SbPropertySetInfo::~SbPropertySetInfo()
281 : {
282 0 : }
283 :
284 : //-------------------------------------------------------------------------
285 :
286 0 : Sequence< Property > SbPropertySetInfo::getProperties(void) throw( RuntimeException )
287 : {
288 0 : return aImpl.getProperties();
289 : }
290 :
291 0 : Property SbPropertySetInfo::getPropertyByName(const OUString& Name)
292 : throw( RuntimeException )
293 : {
294 0 : return aImpl.getPropertyByName( Name );
295 : }
296 :
297 0 : sal_Bool SbPropertySetInfo::hasPropertyByName(const OUString& Name)
298 : throw( RuntimeException )
299 : {
300 0 : return aImpl.hasPropertyByName( Name );
301 : }
302 :
303 : //----------------------------------------------------------------------------
304 :
305 0 : void RTL_Impl_CreatePropertySet( StarBASIC* pBasic, SbxArray& rPar, sal_Bool bWrite )
306 : {
307 : (void)pBasic;
308 : (void)bWrite;
309 :
310 : // We need at least one parameter
311 : // TODO: In this case < 2 is not correct ;-)
312 0 : if ( rPar.Count() < 2 )
313 : {
314 0 : StarBASIC::Error( SbERR_BAD_ARGUMENT );
315 0 : return;
316 : }
317 :
318 : // Get class names of struct
319 0 : OUString aServiceName( "stardiv.uno.beans.PropertySet");
320 :
321 0 : Reference< XInterface > xInterface = (OWeakObject*) new SbPropertyValues();
322 :
323 0 : SbxVariableRef refVar = rPar.Get(0);
324 0 : if( xInterface.is() )
325 : {
326 : // Set PropertyValues
327 : Any aArgAsAny = sbxToUnoValue( rPar.Get(1),
328 0 : getCppuType( (Sequence<PropertyValue>*)0 ) );
329 : Sequence<PropertyValue> *pArg =
330 0 : (Sequence<PropertyValue>*) aArgAsAny.getValue();
331 0 : Reference< XPropertyAccess > xPropAcc = Reference< XPropertyAccess >::query( xInterface );
332 0 : xPropAcc->setPropertyValues( *pArg );
333 :
334 : // Build a SbUnoObject and return it
335 0 : Any aAny;
336 0 : aAny <<= xInterface;
337 0 : SbUnoObjectRef xUnoObj = new SbUnoObject( aServiceName, aAny );
338 0 : if( xUnoObj->getUnoAny().getValueType().getTypeClass() != TypeClass_VOID )
339 : {
340 : // Return object
341 0 : refVar->PutObject( (SbUnoObject*)xUnoObj );
342 0 : return;
343 0 : }
344 : }
345 :
346 : // Object could not be created
347 0 : refVar->PutObject( NULL );
348 : }
349 :
350 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|