1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <vcl/svapp.hxx>
22 #include "miscuno.hxx"
24 using namespace com::sun::star
;
25 using ::com::sun::star::uno::Reference
;
26 using ::com::sun::star::uno::Any
;
28 //------------------------------------------------------------------------
30 SC_SIMPLE_SERVICE_INFO( ScNameToIndexAccess
, "ScNameToIndexAccess", "stardiv.unknown" )
32 //------------------------------------------------------------------------
34 uno::Reference
<uno::XInterface
> ScUnoHelpFunctions::AnyToInterface( const uno::Any
& rAny
)
36 if ( rAny
.getValueTypeClass() == uno::TypeClass_INTERFACE
)
38 return uno::Reference
<uno::XInterface
>(rAny
, uno::UNO_QUERY
);
40 return uno::Reference
<uno::XInterface
>(); //! Exception?
43 sal_Bool
ScUnoHelpFunctions::GetBoolProperty( const uno::Reference
<beans::XPropertySet
>& xProp
,
44 const OUString
& rName
, sal_Bool bDefault
)
46 sal_Bool bRet
= bDefault
;
51 uno::Any
aAny(xProp
->getPropertyValue( rName
));
52 //! type conversion???
53 // operator >>= shouldn't be used for bool (?)
54 if ( aAny
.getValueTypeClass() == uno::TypeClass_BOOLEAN
)
56 //! safe way to get bool value from any???
57 bRet
= *(sal_Bool
*)aAny
.getValue();
60 catch(uno::Exception
&)
68 sal_Int32
ScUnoHelpFunctions::GetLongProperty( const uno::Reference
<beans::XPropertySet
>& xProp
,
69 const OUString
& rName
, long nDefault
)
71 sal_Int32 nRet
= nDefault
;
76 //! type conversion???
77 xProp
->getPropertyValue( rName
) >>= nRet
;
79 catch(uno::Exception
&)
87 sal_Int32
ScUnoHelpFunctions::GetEnumProperty( const uno::Reference
<beans::XPropertySet
>& xProp
,
88 const OUString
& rName
, long nDefault
)
90 sal_Int32 nRet
= nDefault
;
95 uno::Any
aAny(xProp
->getPropertyValue( rName
));
97 if ( aAny
.getValueTypeClass() == uno::TypeClass_ENUM
)
99 //! get enum value from any???
100 nRet
= *(sal_Int32
*)aAny
.getValue();
104 //! type conversion???
108 catch(uno::Exception
&)
116 OUString
ScUnoHelpFunctions::GetStringProperty(
117 const Reference
<beans::XPropertySet
>& xProp
, const OUString
& rName
, const OUString
& rDefault
)
119 OUString aRet
= rDefault
;
125 Any any
= xProp
->getPropertyValue(rName
);
128 catch (const uno::Exception
&)
135 sal_Bool
ScUnoHelpFunctions::GetBoolFromAny( const uno::Any
& aAny
)
137 if ( aAny
.getValueTypeClass() == uno::TypeClass_BOOLEAN
)
138 return *(sal_Bool
*)aAny
.getValue();
142 sal_Int16
ScUnoHelpFunctions::GetInt16FromAny( const uno::Any
& aAny
)
150 sal_Int32
ScUnoHelpFunctions::GetInt32FromAny( const uno::Any
& aAny
)
158 sal_Int32
ScUnoHelpFunctions::GetEnumFromAny( const uno::Any
& aAny
)
161 if ( aAny
.getValueTypeClass() == uno::TypeClass_ENUM
)
162 nRet
= *(sal_Int32
*)aAny
.getValue();
168 void ScUnoHelpFunctions::SetBoolInAny( uno::Any
& rAny
, sal_Bool bValue
)
170 rAny
.setValue( &bValue
, getBooleanCppuType() );
173 void ScUnoHelpFunctions::SetOptionalPropertyValue(
174 Reference
<beans::XPropertySet
>& rPropSet
, const sal_Char
* pPropName
, const Any
& rVal
)
178 rPropSet
->setPropertyValue(OUString::createFromAscii(pPropName
), rVal
);
180 catch (const beans::UnknownPropertyException
&)
182 // ignored - not supported.
186 //------------------------------------------------------------------------
188 ScIndexEnumeration::ScIndexEnumeration(const uno::Reference
<container::XIndexAccess
>& rInd
,
189 const OUString
& rServiceName
) :
191 sServiceName(rServiceName
),
196 ScIndexEnumeration::~ScIndexEnumeration()
202 sal_Bool SAL_CALL
ScIndexEnumeration::hasMoreElements() throw(uno::RuntimeException
)
204 SolarMutexGuard aGuard
;
205 return ( nPos
< xIndex
->getCount() );
208 uno::Any SAL_CALL
ScIndexEnumeration::nextElement() throw(container::NoSuchElementException
,
209 lang::WrappedTargetException
, uno::RuntimeException
)
211 SolarMutexGuard aGuard
;
215 aReturn
= xIndex
->getByIndex(nPos
++);
217 catch (lang::IndexOutOfBoundsException
&)
219 throw container::NoSuchElementException();
224 OUString SAL_CALL
ScIndexEnumeration::getImplementationName()
225 throw(::com::sun::star::uno::RuntimeException
)
227 return OUString("ScIndexEnumeration");
230 sal_Bool SAL_CALL
ScIndexEnumeration::supportsService( const OUString
& ServiceName
)
231 throw(::com::sun::star::uno::RuntimeException
)
233 return sServiceName
== ServiceName
;
236 ::com::sun::star::uno::Sequence
< OUString
>
237 SAL_CALL
ScIndexEnumeration::getSupportedServiceNames(void)
238 throw(::com::sun::star::uno::RuntimeException
)
240 ::com::sun::star::uno::Sequence
< OUString
> aRet(1);
241 OUString
* pArray
= aRet
.getArray();
242 pArray
[0] = sServiceName
;
246 //------------------------------------------------------------------------
248 //------------------------------------------------------------------------
250 ScNameToIndexAccess::ScNameToIndexAccess( const com::sun::star::uno::Reference
<
251 com::sun::star::container::XNameAccess
>& rNameObj
) :
252 xNameAccess( rNameObj
)
254 //! test for XIndexAccess interface at rNameObj, use that instead!
256 if ( xNameAccess
.is() )
257 aNames
= xNameAccess
->getElementNames();
260 ScNameToIndexAccess::~ScNameToIndexAccess()
266 sal_Int32 SAL_CALL
ScNameToIndexAccess::getCount( ) throw(::com::sun::star::uno::RuntimeException
)
268 return aNames
.getLength();
271 ::com::sun::star::uno::Any SAL_CALL
ScNameToIndexAccess::getByIndex( sal_Int32 nIndex
)
272 throw(::com::sun::star::lang::IndexOutOfBoundsException
,
273 ::com::sun::star::lang::WrappedTargetException
,
274 ::com::sun::star::uno::RuntimeException
)
276 if ( xNameAccess
.is() && nIndex
>= 0 && nIndex
< aNames
.getLength() )
277 return xNameAccess
->getByName( aNames
.getConstArray()[nIndex
] );
279 throw lang::IndexOutOfBoundsException();
284 ::com::sun::star::uno::Type SAL_CALL
ScNameToIndexAccess::getElementType( )
285 throw(::com::sun::star::uno::RuntimeException
)
287 if ( xNameAccess
.is() )
288 return xNameAccess
->getElementType();
293 sal_Bool SAL_CALL
ScNameToIndexAccess::hasElements( ) throw(::com::sun::star::uno::RuntimeException
)
295 return getCount() > 0;
298 //------------------------------------------------------------------------
302 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */