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