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 = (OUString*) arg1;
46 0 : const Property *pArg2 = (Property*) 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(void) 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(void) 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 PropertyExistException();
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 = (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 0 : Sequence< Property > PropertySetInfoImpl::getProperties(void) throw()
208 : {
209 0 : return _aProps;
210 : }
211 :
212 0 : Property PropertySetInfoImpl::getPropertyByName(const OUString& Name) throw( RuntimeException )
213 : {
214 0 : sal_Int32 nIndex = GetIndex_Impl( Name );
215 0 : if( USHRT_MAX != nIndex )
216 0 : return _aProps.getConstArray()[ nIndex ];
217 0 : return Property();
218 : }
219 :
220 0 : bool PropertySetInfoImpl::hasPropertyByName(const OUString& Name) throw( RuntimeException )
221 : {
222 0 : sal_Int32 nIndex = GetIndex_Impl( Name );
223 0 : return USHRT_MAX != nIndex;
224 : }
225 :
226 :
227 :
228 :
229 0 : SbPropertySetInfo::SbPropertySetInfo( const SbPropertyValueArr_Impl &rPropVals )
230 : {
231 0 : aImpl._aProps.realloc( rPropVals.size() );
232 0 : for ( sal_uInt16 n = 0; n < rPropVals.size(); ++n )
233 : {
234 0 : Property &rProp = aImpl._aProps.getArray()[n];
235 0 : const PropertyValue &rPropVal = rPropVals[n];
236 0 : rProp.Name = rPropVal.Name;
237 0 : rProp.Handle = rPropVal.Handle;
238 0 : rProp.Type = getCppuVoidType();
239 0 : rProp.Attributes = 0;
240 : }
241 0 : }
242 :
243 :
244 :
245 0 : SbPropertySetInfo::~SbPropertySetInfo()
246 : {
247 0 : }
248 :
249 :
250 :
251 0 : Sequence< Property > SbPropertySetInfo::getProperties(void) throw( RuntimeException, std::exception )
252 : {
253 0 : return aImpl.getProperties();
254 : }
255 :
256 0 : Property SbPropertySetInfo::getPropertyByName(const OUString& Name)
257 : throw( RuntimeException, std::exception )
258 : {
259 0 : return aImpl.getPropertyByName( Name );
260 : }
261 :
262 0 : sal_Bool SbPropertySetInfo::hasPropertyByName(const OUString& Name)
263 : throw( RuntimeException, std::exception )
264 : {
265 0 : return aImpl.hasPropertyByName( Name );
266 : }
267 :
268 :
269 :
270 0 : void RTL_Impl_CreatePropertySet( StarBASIC* pBasic, SbxArray& rPar, bool bWrite )
271 : {
272 : (void)pBasic;
273 : (void)bWrite;
274 :
275 : // We need at least one parameter
276 : // TODO: In this case < 2 is not correct ;-)
277 0 : if ( rPar.Count() < 2 )
278 : {
279 0 : StarBASIC::Error( SbERR_BAD_ARGUMENT );
280 0 : return;
281 : }
282 :
283 : // Get class names of struct
284 0 : OUString aServiceName( "stardiv.uno.beans.PropertySet");
285 :
286 0 : Reference< XInterface > xInterface = (OWeakObject*) new SbPropertyValues();
287 :
288 0 : SbxVariableRef refVar = rPar.Get(0);
289 0 : if( xInterface.is() )
290 : {
291 : // Set PropertyValues
292 0 : Any aArgAsAny = sbxToUnoValue( rPar.Get(1),
293 0 : getCppuType( (Sequence<PropertyValue>*)0 ) );
294 : Sequence<PropertyValue> *pArg =
295 0 : (Sequence<PropertyValue>*) aArgAsAny.getValue();
296 0 : Reference< XPropertyAccess > xPropAcc = Reference< XPropertyAccess >::query( xInterface );
297 0 : xPropAcc->setPropertyValues( *pArg );
298 :
299 : // Build a SbUnoObject and return it
300 0 : Any aAny;
301 0 : aAny <<= xInterface;
302 0 : SbUnoObjectRef xUnoObj = new SbUnoObject( aServiceName, aAny );
303 0 : if( xUnoObj->getUnoAny().getValueType().getTypeClass() != TypeClass_VOID )
304 : {
305 : // Return object
306 0 : refVar->PutObject( (SbUnoObject*)xUnoObj );
307 0 : return;
308 0 : }
309 : }
310 :
311 : // Object could not be created
312 0 : refVar->PutObject( NULL );
313 : }
314 :
315 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|