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 <sal/config.h>
22 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
23 #include <cppuhelper/supportsservice.hxx>
24 #include <o3tl/any.hxx>
26 #include <vcl/svapp.hxx>
28 #include <miscuno.hxx>
30 using namespace com::sun::star
;
31 using ::com::sun::star::uno::Reference
;
32 using ::com::sun::star::uno::Any
;
34 SC_SIMPLE_SERVICE_INFO( ScNameToIndexAccess
, u
"ScNameToIndexAccess"_ustr
, u
"stardiv.unknown"_ustr
)
36 bool ScUnoHelpFunctions::GetBoolProperty( const uno::Reference
<beans::XPropertySet
>& xProp
,
37 const OUString
& rName
, bool bDefault
)
44 xProp
->getPropertyValue( rName
) >>= bRet
;
46 catch(uno::Exception
&)
54 sal_Int16
ScUnoHelpFunctions::GetShortProperty( const css::uno::Reference
< css::beans::XPropertySet
>& xProp
,
55 const OUString
& rName
, sal_Int16 nDefault
)
57 sal_Int16 nRet
= nDefault
;
62 xProp
->getPropertyValue( rName
) >>= nRet
;
64 catch(uno::Exception
&)
72 sal_Int32
ScUnoHelpFunctions::GetLongProperty( const uno::Reference
<beans::XPropertySet
>& xProp
,
73 const OUString
& rName
)
80 //! type conversion???
81 xProp
->getPropertyValue( rName
) >>= nRet
;
83 catch(uno::Exception
&)
91 sal_Int32
ScUnoHelpFunctions::GetEnumPropertyImpl( const uno::Reference
<beans::XPropertySet
>& xProp
,
92 const OUString
& rName
, sal_Int32 nDefault
)
94 sal_Int32 nRet
= nDefault
;
99 uno::Any
aAny(xProp
->getPropertyValue( rName
));
101 if ( aAny
.getValueTypeClass() == uno::TypeClass_ENUM
)
103 //! get enum value from any???
104 nRet
= *static_cast<sal_Int32
const *>(aAny
.getValue());
108 //! type conversion???
112 catch(uno::Exception
&)
120 OUString
ScUnoHelpFunctions::GetStringProperty(
121 const Reference
<beans::XPropertySet
>& xProp
, const OUString
& rName
, const OUString
& rDefault
)
123 OUString aRet
= rDefault
;
129 Any any
= xProp
->getPropertyValue(rName
);
132 catch (const uno::Exception
&)
139 bool ScUnoHelpFunctions::GetBoolFromAny( const uno::Any
& aAny
)
141 auto b
= o3tl::tryAccess
<bool>(aAny
);
142 return b
.has_value() && *b
;
145 sal_Int16
ScUnoHelpFunctions::GetInt16FromAny( const uno::Any
& aAny
)
153 sal_Int32
ScUnoHelpFunctions::GetInt32FromAny( const uno::Any
& aAny
)
161 sal_Int32
ScUnoHelpFunctions::GetEnumFromAny( const uno::Any
& aAny
)
164 if ( aAny
.getValueTypeClass() == uno::TypeClass_ENUM
)
165 nRet
= *static_cast<sal_Int32
const *>(aAny
.getValue());
171 void ScUnoHelpFunctions::SetOptionalPropertyValue(
172 const Reference
<beans::XPropertySet
>& rPropSet
, const char* pPropName
, const Any
& rVal
)
174 SetOptionalPropertyValue(rPropSet
, OUString::createFromAscii(pPropName
), rVal
);
177 void ScUnoHelpFunctions::SetOptionalPropertyValue(
178 const Reference
<beans::XPropertySet
>& rPropSet
, const OUString
& sPropName
, const Any
& rVal
)
182 rPropSet
->setPropertyValue(sPropName
, rVal
);
184 catch (const beans::UnknownPropertyException
&)
186 // ignored - not supported.
190 ScIndexEnumeration::ScIndexEnumeration(uno::Reference
<container::XIndexAccess
> xInd
,
191 OUString aServiceName
) :
192 xIndex(std::move( xInd
)),
193 sServiceName(std::move(aServiceName
)),
198 ScIndexEnumeration::~ScIndexEnumeration()
204 sal_Bool SAL_CALL
ScIndexEnumeration::hasMoreElements()
206 SolarMutexGuard aGuard
;
207 return ( nPos
< xIndex
->getCount() );
210 uno::Any SAL_CALL
ScIndexEnumeration::nextElement()
212 SolarMutexGuard aGuard
;
216 aReturn
= xIndex
->getByIndex(nPos
++);
218 catch (lang::IndexOutOfBoundsException
&)
220 throw container::NoSuchElementException();
225 OUString SAL_CALL
ScIndexEnumeration::getImplementationName()
227 return u
"ScIndexEnumeration"_ustr
;
230 sal_Bool SAL_CALL
ScIndexEnumeration::supportsService( const OUString
& ServiceName
)
232 return cppu::supportsService(this, ServiceName
);
235 css::uno::Sequence
< OUString
>
236 SAL_CALL
ScIndexEnumeration::getSupportedServiceNames()
238 return { sServiceName
};
241 ScNameToIndexAccess::ScNameToIndexAccess( css::uno::Reference
<
242 css::container::XNameAccess
> xNameObj
) :
243 xNameAccess(std::move( xNameObj
))
245 //! test for XIndexAccess interface at rNameObj, use that instead!
247 if ( xNameAccess
.is() )
248 aNames
= xNameAccess
->getElementNames();
251 ScNameToIndexAccess::~ScNameToIndexAccess()
257 sal_Int32 SAL_CALL
ScNameToIndexAccess::getCount( )
259 return aNames
.getLength();
262 css::uno::Any SAL_CALL
ScNameToIndexAccess::getByIndex( sal_Int32 nIndex
)
264 if ( xNameAccess
.is() && nIndex
>= 0 && nIndex
< aNames
.getLength() )
265 return xNameAccess
->getByName( aNames
.getConstArray()[nIndex
] );
267 throw lang::IndexOutOfBoundsException();
272 css::uno::Type SAL_CALL
ScNameToIndexAccess::getElementType( )
274 if ( xNameAccess
.is() )
275 return xNameAccess
->getElementType();
280 sal_Bool SAL_CALL
ScNameToIndexAccess::hasElements( )
282 return getCount() > 0;
285 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */