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 <cppuhelper/supportsservice.hxx>
21 #include <vcl/svapp.hxx>
23 #include "miscuno.hxx"
25 using namespace com::sun::star
;
26 using ::com::sun::star::uno::Reference
;
27 using ::com::sun::star::uno::Any
;
29 SC_SIMPLE_SERVICE_INFO( ScNameToIndexAccess
, "ScNameToIndexAccess", "stardiv.unknown" )
31 uno::Reference
<uno::XInterface
> ScUnoHelpFunctions::AnyToInterface( const uno::Any
& rAny
)
33 if ( rAny
.getValueTypeClass() == uno::TypeClass_INTERFACE
)
35 return uno::Reference
<uno::XInterface
>(rAny
, uno::UNO_QUERY
);
37 return uno::Reference
<uno::XInterface
>(); //! Exception?
40 bool ScUnoHelpFunctions::GetBoolProperty( const uno::Reference
<beans::XPropertySet
>& xProp
,
41 const OUString
& rName
, bool bDefault
)
48 uno::Any
aAny(xProp
->getPropertyValue( rName
));
49 //! type conversion???
50 // operator >>= shouldn't be used for bool (?)
51 if ( aAny
.getValueTypeClass() == uno::TypeClass_BOOLEAN
)
53 //! safe way to get bool value from any???
54 bRet
= *static_cast<sal_Bool
const *>(aAny
.getValue());
57 catch(uno::Exception
&)
65 sal_Int32
ScUnoHelpFunctions::GetLongProperty( const uno::Reference
<beans::XPropertySet
>& xProp
,
66 const OUString
& rName
, long nDefault
)
68 sal_Int32 nRet
= nDefault
;
73 //! type conversion???
74 xProp
->getPropertyValue( rName
) >>= nRet
;
76 catch(uno::Exception
&)
84 sal_Int32
ScUnoHelpFunctions::GetEnumProperty( const uno::Reference
<beans::XPropertySet
>& xProp
,
85 const OUString
& rName
, long nDefault
)
87 sal_Int32 nRet
= nDefault
;
92 uno::Any
aAny(xProp
->getPropertyValue( rName
));
94 if ( aAny
.getValueTypeClass() == uno::TypeClass_ENUM
)
96 //! get enum value from any???
97 nRet
= *static_cast<sal_Int32
const *>(aAny
.getValue());
101 //! type conversion???
105 catch(uno::Exception
&)
113 OUString
ScUnoHelpFunctions::GetStringProperty(
114 const Reference
<beans::XPropertySet
>& xProp
, const OUString
& rName
, const OUString
& rDefault
)
116 OUString aRet
= rDefault
;
122 Any any
= xProp
->getPropertyValue(rName
);
125 catch (const uno::Exception
&)
132 bool ScUnoHelpFunctions::GetBoolFromAny( const uno::Any
& aAny
)
134 if ( aAny
.getValueTypeClass() == uno::TypeClass_BOOLEAN
)
135 return *static_cast<sal_Bool
const *>(aAny
.getValue());
139 sal_Int16
ScUnoHelpFunctions::GetInt16FromAny( const uno::Any
& aAny
)
147 sal_Int32
ScUnoHelpFunctions::GetInt32FromAny( const uno::Any
& aAny
)
155 sal_Int32
ScUnoHelpFunctions::GetEnumFromAny( const uno::Any
& aAny
)
158 if ( aAny
.getValueTypeClass() == uno::TypeClass_ENUM
)
159 nRet
= *static_cast<sal_Int32
const *>(aAny
.getValue());
165 void ScUnoHelpFunctions::SetBoolInAny( uno::Any
& rAny
, bool bValue
)
167 sal_Bool bTemp
= bValue
? 1 : 0;
168 rAny
.setValue( &bTemp
, cppu::UnoType
<bool>::get() );
171 void ScUnoHelpFunctions::SetOptionalPropertyValue(
172 Reference
<beans::XPropertySet
>& rPropSet
, const sal_Char
* pPropName
, const Any
& rVal
)
176 rPropSet
->setPropertyValue(OUString::createFromAscii(pPropName
), rVal
);
178 catch (const beans::UnknownPropertyException
&)
180 // ignored - not supported.
184 ScIndexEnumeration::ScIndexEnumeration(const uno::Reference
<container::XIndexAccess
>& rInd
,
185 const OUString
& rServiceName
) :
187 sServiceName(rServiceName
),
192 ScIndexEnumeration::~ScIndexEnumeration()
198 sal_Bool SAL_CALL
ScIndexEnumeration::hasMoreElements() throw(uno::RuntimeException
, std::exception
)
200 SolarMutexGuard aGuard
;
201 return ( nPos
< xIndex
->getCount() );
204 uno::Any SAL_CALL
ScIndexEnumeration::nextElement() throw(container::NoSuchElementException
,
205 lang::WrappedTargetException
, uno::RuntimeException
, std::exception
)
207 SolarMutexGuard aGuard
;
211 aReturn
= xIndex
->getByIndex(nPos
++);
213 catch (lang::IndexOutOfBoundsException
&)
215 throw container::NoSuchElementException();
220 OUString SAL_CALL
ScIndexEnumeration::getImplementationName()
221 throw(::com::sun::star::uno::RuntimeException
, std::exception
)
223 return OUString("ScIndexEnumeration");
226 sal_Bool SAL_CALL
ScIndexEnumeration::supportsService( const OUString
& ServiceName
)
227 throw(::com::sun::star::uno::RuntimeException
, std::exception
)
229 return cppu::supportsService(this, ServiceName
);
232 ::com::sun::star::uno::Sequence
< OUString
>
233 SAL_CALL
ScIndexEnumeration::getSupportedServiceNames()
234 throw(::com::sun::star::uno::RuntimeException
, std::exception
)
236 ::com::sun::star::uno::Sequence
< OUString
> aRet(1);
237 OUString
* pArray
= aRet
.getArray();
238 pArray
[0] = sServiceName
;
242 ScNameToIndexAccess::ScNameToIndexAccess( const com::sun::star::uno::Reference
<
243 com::sun::star::container::XNameAccess
>& rNameObj
) :
244 xNameAccess( rNameObj
)
246 //! test for XIndexAccess interface at rNameObj, use that instead!
248 if ( xNameAccess
.is() )
249 aNames
= xNameAccess
->getElementNames();
252 ScNameToIndexAccess::~ScNameToIndexAccess()
258 sal_Int32 SAL_CALL
ScNameToIndexAccess::getCount( ) throw(::com::sun::star::uno::RuntimeException
, std::exception
)
260 return aNames
.getLength();
263 ::com::sun::star::uno::Any SAL_CALL
ScNameToIndexAccess::getByIndex( sal_Int32 nIndex
)
264 throw(::com::sun::star::lang::IndexOutOfBoundsException
,
265 ::com::sun::star::lang::WrappedTargetException
,
266 ::com::sun::star::uno::RuntimeException
, std::exception
)
268 if ( xNameAccess
.is() && nIndex
>= 0 && nIndex
< aNames
.getLength() )
269 return xNameAccess
->getByName( aNames
.getConstArray()[nIndex
] );
271 throw lang::IndexOutOfBoundsException();
276 ::com::sun::star::uno::Type SAL_CALL
ScNameToIndexAccess::getElementType( )
277 throw(::com::sun::star::uno::RuntimeException
, std::exception
)
279 if ( xNameAccess
.is() )
280 return xNameAccess
->getElementType();
285 sal_Bool SAL_CALL
ScNameToIndexAccess::hasElements( ) throw(::com::sun::star::uno::RuntimeException
, std::exception
)
287 return getCount() > 0;
290 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */