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