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