Stop leaking all ScPostIt instances.
[LibreOffice.git] / sc / source / ui / unoobj / miscuno.cxx
blobf5f6719b1123eb89fd89807d2fd8262a8f623566
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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;
47 if ( xProp.is() )
49 try
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&)
62 // keep default
65 return bRet;
68 sal_Int32 ScUnoHelpFunctions::GetLongProperty( const uno::Reference<beans::XPropertySet>& xProp,
69 const OUString& rName, long nDefault )
71 sal_Int32 nRet = nDefault;
72 if ( xProp.is() )
74 try
76 //! type conversion???
77 xProp->getPropertyValue( rName ) >>= nRet;
79 catch(uno::Exception&)
81 // keep default
84 return nRet;
87 sal_Int32 ScUnoHelpFunctions::GetEnumProperty( const uno::Reference<beans::XPropertySet>& xProp,
88 const OUString& rName, long nDefault )
90 sal_Int32 nRet = nDefault;
91 if ( xProp.is() )
93 try
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();
102 else
104 //! type conversion???
105 aAny >>= nRet;
108 catch(uno::Exception&)
110 // keep default
113 return nRet;
116 OUString ScUnoHelpFunctions::GetStringProperty(
117 const Reference<beans::XPropertySet>& xProp, const OUString& rName, const OUString& rDefault )
119 OUString aRet = rDefault;
120 if (!xProp.is())
121 return aRet;
125 Any any = xProp->getPropertyValue(rName);
126 any >>= aRet;
128 catch (const uno::Exception&)
132 return aRet;
135 sal_Bool ScUnoHelpFunctions::GetBoolFromAny( const uno::Any& aAny )
137 if ( aAny.getValueTypeClass() == uno::TypeClass_BOOLEAN )
138 return *(sal_Bool*)aAny.getValue();
139 return false;
142 sal_Int16 ScUnoHelpFunctions::GetInt16FromAny( const uno::Any& aAny )
144 sal_Int16 nRet = 0;
145 if ( aAny >>= nRet )
146 return nRet;
147 return 0;
150 sal_Int32 ScUnoHelpFunctions::GetInt32FromAny( const uno::Any& aAny )
152 sal_Int32 nRet = 0;
153 if ( aAny >>= nRet )
154 return nRet;
155 return 0;
158 sal_Int32 ScUnoHelpFunctions::GetEnumFromAny( const uno::Any& aAny )
160 sal_Int32 nRet = 0;
161 if ( aAny.getValueTypeClass() == uno::TypeClass_ENUM )
162 nRet = *(sal_Int32*)aAny.getValue();
163 else
164 aAny >>= nRet;
165 return nRet;
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) :
190 xIndex( rInd ),
191 sServiceName(rServiceName),
192 nPos( 0 )
196 ScIndexEnumeration::~ScIndexEnumeration()
200 // XEnumeration
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;
212 uno::Any aReturn;
215 aReturn = xIndex->getByIndex(nPos++);
217 catch (lang::IndexOutOfBoundsException&)
219 throw container::NoSuchElementException();
221 return aReturn;
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;
243 return aRet;
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()
264 // XIndexAccess
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();
282 // XElementAccess
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();
289 else
290 return uno::Type();
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: */