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 #include <sal/config.h>
21 
22 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
23 #include <cppuhelper/supportsservice.hxx>
24 #include <o3tl/any.hxx>
25 #include <vcl/svapp.hxx>
26 
27 #include <miscuno.hxx>
28 
29 using namespace com::sun::star;
30 using ::com::sun::star::uno::Reference;
31 using ::com::sun::star::uno::Any;
32 
33 SC_SIMPLE_SERVICE_INFO( ScNameToIndexAccess, "ScNameToIndexAccess", "stardiv.unknown" )
34 
GetBoolProperty(const uno::Reference<beans::XPropertySet> & xProp,const OUString & rName,bool bDefault)35 bool ScUnoHelpFunctions::GetBoolProperty( const uno::Reference<beans::XPropertySet>& xProp,
36                                             const OUString& rName, bool bDefault )
37 {
38     bool bRet = bDefault;
39     if ( xProp.is() )
40     {
41         try
42         {
43             xProp->getPropertyValue( rName ) >>= bRet;
44         }
45         catch(uno::Exception&)
46         {
47             // keep default
48         }
49     }
50     return bRet;
51 }
52 
GetShortProperty(const css::uno::Reference<css::beans::XPropertySet> & xProp,const OUString & rName,sal_Int16 nDefault)53 sal_Int16 ScUnoHelpFunctions::GetShortProperty( const css::uno::Reference< css::beans::XPropertySet>& xProp,
54                                                 const OUString& rName, sal_Int16 nDefault )
55 {
56     sal_Int16 nRet = nDefault;
57     if ( xProp.is() )
58     {
59         try
60         {
61             xProp->getPropertyValue( rName ) >>= nRet;
62         }
63         catch(uno::Exception&)
64         {
65             // keep default
66         }
67     }
68     return nRet;
69 }
70 
GetLongProperty(const uno::Reference<beans::XPropertySet> & xProp,const OUString & rName)71 sal_Int32 ScUnoHelpFunctions::GetLongProperty( const uno::Reference<beans::XPropertySet>& xProp,
72                                             const OUString& rName )
73 {
74     sal_Int32 nRet = 0;
75     if ( xProp.is() )
76     {
77         try
78         {
79             //! type conversion???
80             xProp->getPropertyValue( rName ) >>= nRet;
81         }
82         catch(uno::Exception&)
83         {
84             // keep default
85         }
86     }
87     return nRet;
88 }
89 
GetEnumPropertyImpl(const uno::Reference<beans::XPropertySet> & xProp,const OUString & rName,sal_Int32 nDefault)90 sal_Int32 ScUnoHelpFunctions::GetEnumPropertyImpl( const uno::Reference<beans::XPropertySet>& xProp,
91                                             const OUString& rName, sal_Int32 nDefault )
92 {
93     sal_Int32 nRet = nDefault;
94     if ( xProp.is() )
95     {
96         try
97         {
98             uno::Any aAny(xProp->getPropertyValue( rName ));
99 
100             if ( aAny.getValueTypeClass() == uno::TypeClass_ENUM )
101             {
102                 //! get enum value from any???
103                 nRet = *static_cast<sal_Int32 const *>(aAny.getValue());
104             }
105             else
106             {
107                 //! type conversion???
108                 aAny >>= nRet;
109             }
110         }
111         catch(uno::Exception&)
112         {
113             // keep default
114         }
115     }
116     return nRet;
117 }
118 
GetStringProperty(const Reference<beans::XPropertySet> & xProp,const OUString & rName,const OUString & rDefault)119 OUString ScUnoHelpFunctions::GetStringProperty(
120     const Reference<beans::XPropertySet>& xProp, const OUString& rName, const OUString& rDefault )
121 {
122     OUString aRet = rDefault;
123     if (!xProp.is())
124         return aRet;
125 
126     try
127     {
128         Any any = xProp->getPropertyValue(rName);
129         any >>= aRet;
130     }
131     catch (const uno::Exception&)
132     {
133     }
134 
135     return aRet;
136 }
137 
GetBoolFromAny(const uno::Any & aAny)138 bool ScUnoHelpFunctions::GetBoolFromAny( const uno::Any& aAny )
139 {
140     auto b = o3tl::tryAccess<bool>(aAny);
141     return b && *b;
142 }
143 
GetInt16FromAny(const uno::Any & aAny)144 sal_Int16 ScUnoHelpFunctions::GetInt16FromAny( const uno::Any& aAny )
145 {
146     sal_Int16 nRet = 0;
147     if ( aAny >>= nRet )
148         return nRet;
149     return 0;
150 }
151 
GetInt32FromAny(const uno::Any & aAny)152 sal_Int32 ScUnoHelpFunctions::GetInt32FromAny( const uno::Any& aAny )
153 {
154     sal_Int32 nRet = 0;
155     if ( aAny >>= nRet )
156         return nRet;
157     return 0;
158 }
159 
GetEnumFromAny(const uno::Any & aAny)160 sal_Int32 ScUnoHelpFunctions::GetEnumFromAny( const uno::Any& aAny )
161 {
162     sal_Int32 nRet = 0;
163     if ( aAny.getValueTypeClass() == uno::TypeClass_ENUM )
164         nRet = *static_cast<sal_Int32 const *>(aAny.getValue());
165     else
166         aAny >>= nRet;
167     return nRet;
168 }
169 
SetOptionalPropertyValue(const Reference<beans::XPropertySet> & rPropSet,const char * pPropName,const Any & rVal)170 void ScUnoHelpFunctions::SetOptionalPropertyValue(
171     const Reference<beans::XPropertySet>& rPropSet, const char* pPropName, const Any& rVal )
172 {
173     try
174     {
175         rPropSet->setPropertyValue(OUString::createFromAscii(pPropName), rVal);
176     }
177     catch (const beans::UnknownPropertyException&)
178     {
179         // ignored - not supported.
180     }
181 }
182 
ScIndexEnumeration(const uno::Reference<container::XIndexAccess> & rInd,const OUString & rServiceName)183 ScIndexEnumeration::ScIndexEnumeration(const uno::Reference<container::XIndexAccess>& rInd,
184                                        const OUString& rServiceName) :
185     xIndex( rInd ),
186     sServiceName(rServiceName),
187     nPos( 0 )
188 {
189 }
190 
~ScIndexEnumeration()191 ScIndexEnumeration::~ScIndexEnumeration()
192 {
193 }
194 
195 // XEnumeration
196 
hasMoreElements()197 sal_Bool SAL_CALL ScIndexEnumeration::hasMoreElements()
198 {
199     SolarMutexGuard aGuard;
200     return ( nPos < xIndex->getCount() );
201 }
202 
nextElement()203 uno::Any SAL_CALL ScIndexEnumeration::nextElement()
204 {
205     SolarMutexGuard aGuard;
206     uno::Any aReturn;
207     try
208     {
209         aReturn = xIndex->getByIndex(nPos++);
210     }
211     catch (lang::IndexOutOfBoundsException&)
212     {
213         throw container::NoSuchElementException();
214     }
215     return aReturn;
216 }
217 
getImplementationName()218 OUString SAL_CALL ScIndexEnumeration::getImplementationName()
219 {
220     return "ScIndexEnumeration";
221 }
222 
supportsService(const OUString & ServiceName)223 sal_Bool SAL_CALL ScIndexEnumeration::supportsService( const OUString& ServiceName )
224 {
225     return cppu::supportsService(this, ServiceName);
226 }
227 
228 css::uno::Sequence< OUString >
getSupportedServiceNames()229     SAL_CALL ScIndexEnumeration::getSupportedServiceNames()
230 {
231     return { sServiceName };
232 }
233 
ScNameToIndexAccess(const css::uno::Reference<css::container::XNameAccess> & rNameObj)234 ScNameToIndexAccess::ScNameToIndexAccess( const css::uno::Reference<
235                                             css::container::XNameAccess>& rNameObj ) :
236     xNameAccess( rNameObj )
237 {
238     //! test for XIndexAccess interface at rNameObj, use that instead!
239 
240     if ( xNameAccess.is() )
241         aNames = xNameAccess->getElementNames();
242 }
243 
~ScNameToIndexAccess()244 ScNameToIndexAccess::~ScNameToIndexAccess()
245 {
246 }
247 
248 // XIndexAccess
249 
getCount()250 sal_Int32 SAL_CALL ScNameToIndexAccess::getCount(  )
251 {
252     return aNames.getLength();
253 }
254 
getByIndex(sal_Int32 nIndex)255 css::uno::Any SAL_CALL ScNameToIndexAccess::getByIndex( sal_Int32 nIndex )
256 {
257     if ( xNameAccess.is() && nIndex >= 0 && nIndex < aNames.getLength() )
258         return xNameAccess->getByName( aNames.getConstArray()[nIndex] );
259 
260     throw lang::IndexOutOfBoundsException();
261 }
262 
263 // XElementAccess
264 
getElementType()265 css::uno::Type SAL_CALL ScNameToIndexAccess::getElementType(  )
266 {
267     if ( xNameAccess.is() )
268         return xNameAccess->getElementType();
269     else
270         return uno::Type();
271 }
272 
hasElements()273 sal_Bool SAL_CALL ScNameToIndexAccess::hasElements(  )
274 {
275     return getCount() > 0;
276 }
277 
278 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
279