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 <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 
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 
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 
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  {
80  xProp->getPropertyValue( rName ) >>= nRet;
81  }
82  catch(uno::Exception&)
83  {
84  // keep default
85  }
86  }
87  return nRet;
88 }
89 
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  {
103  nRet = *static_cast<sal_Int32 const *>(aAny.getValue());
104  }
105  else
106  {
108  aAny >>= nRet;
109  }
110  }
111  catch(uno::Exception&)
112  {
113  // keep default
114  }
115  }
116  return nRet;
117 }
118 
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 
139 {
140  auto b = o3tl::tryAccess<bool>(aAny);
141  return b && *b;
142 }
143 
145 {
146  sal_Int16 nRet = 0;
147  if ( aAny >>= nRet )
148  return nRet;
149  return 0;
150 }
151 
153 {
154  sal_Int32 nRet = 0;
155  if ( aAny >>= nRet )
156  return nRet;
157  return 0;
158 }
159 
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 
171  const Reference<beans::XPropertySet>& rPropSet, const char* pPropName, const Any& rVal )
172 {
173  SetOptionalPropertyValue(rPropSet, OUString::createFromAscii(pPropName), rVal);
174 }
175 
177  const Reference<beans::XPropertySet>& rPropSet, const OUString& sPropName, const Any& rVal )
178 {
179  try
180  {
181  rPropSet->setPropertyValue(sPropName, rVal);
182  }
183  catch (const beans::UnknownPropertyException&)
184  {
185  // ignored - not supported.
186  }
187 }
188 
189 ScIndexEnumeration::ScIndexEnumeration(const uno::Reference<container::XIndexAccess>& rInd,
190  const OUString& rServiceName) :
191  xIndex( rInd ),
192  sServiceName(rServiceName),
193  nPos( 0 )
194 {
195 }
196 
198 {
199 }
200 
201 // XEnumeration
202 
204 {
205  SolarMutexGuard aGuard;
206  return ( nPos < xIndex->getCount() );
207 }
208 
210 {
211  SolarMutexGuard aGuard;
212  uno::Any aReturn;
213  try
214  {
215  aReturn = xIndex->getByIndex(nPos++);
216  }
217  catch (lang::IndexOutOfBoundsException&)
218  {
219  throw container::NoSuchElementException();
220  }
221  return aReturn;
222 }
223 
225 {
226  return "ScIndexEnumeration";
227 }
228 
229 sal_Bool SAL_CALL ScIndexEnumeration::supportsService( const OUString& ServiceName )
230 {
231  return cppu::supportsService(this, ServiceName);
232 }
233 
234 css::uno::Sequence< OUString >
236 {
237  return { sServiceName };
238 }
239 
240 ScNameToIndexAccess::ScNameToIndexAccess( const css::uno::Reference<
241  css::container::XNameAccess>& rNameObj ) :
242  xNameAccess( rNameObj )
243 {
245 
246  if ( xNameAccess.is() )
247  aNames = xNameAccess->getElementNames();
248 }
249 
251 {
252 }
253 
254 // XIndexAccess
255 
256 sal_Int32 SAL_CALL ScNameToIndexAccess::getCount( )
257 {
258  return aNames.getLength();
259 }
260 
261 css::uno::Any SAL_CALL ScNameToIndexAccess::getByIndex( sal_Int32 nIndex )
262 {
263  if ( xNameAccess.is() && nIndex >= 0 && nIndex < aNames.getLength() )
264  return xNameAccess->getByName( aNames.getConstArray()[nIndex] );
265 
266  throw lang::IndexOutOfBoundsException();
267 }
268 
269 // XElementAccess
270 
271 css::uno::Type SAL_CALL ScNameToIndexAccess::getElementType( )
272 {
273  if ( xNameAccess.is() )
274  return xNameAccess->getElementType();
275  else
276  return uno::Type();
277 }
278 
280 {
281  return getCount() > 0;
282 }
283 
284 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual css::uno::Any SAL_CALL getByIndex(sal_Int32 Index) override
Definition: miscuno.cxx:261
virtual sal_Int32 SAL_CALL getCount() override
Definition: miscuno.cxx:256
static sal_Int32 GetEnumFromAny(const css::uno::Any &aAny)
Definition: miscuno.cxx:160
static sal_Int32 GetLongProperty(const css::uno::Reference< css::beans::XPropertySet > &xProp, const OUString &rName)
Definition: miscuno.cxx:71
virtual sal_Bool SAL_CALL hasElements() override
Definition: miscuno.cxx:279
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
Definition: miscuno.cxx:229
OUString sServiceName
Definition: miscuno.hxx:103
virtual ~ScNameToIndexAccess() override
Definition: miscuno.cxx:250
static OUString GetStringProperty(const css::uno::Reference< css::beans::XPropertySet > &xProp, const OUString &rName, const OUString &rDefault)
Definition: miscuno.cxx:119
virtual css::uno::Type SAL_CALL getElementType() override
Definition: miscuno.cxx:271
virtual OUString SAL_CALL getImplementationName() override
Definition: miscuno.cxx:224
constexpr OUStringLiteral sServiceName
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
virtual sal_Bool SAL_CALL hasMoreElements() override
Definition: miscuno.cxx:203
sal_Int32 nPos
Definition: miscuno.hxx:104
css::uno::Reference< css::container::XNameAccess > xNameAccess
Definition: miscuno.hxx:127
virtual ~ScIndexEnumeration() override
Definition: miscuno.cxx:197
static sal_Int32 GetInt32FromAny(const css::uno::Any &aAny)
Definition: miscuno.cxx:152
const Any & any
unsigned char sal_Bool
#define SC_SIMPLE_SERVICE_INFO(ClassName, ClassNameAscii, ServiceAscii)
Definition: miscuno.hxx:63
static void SetOptionalPropertyValue(const css::uno::Reference< css::beans::XPropertySet > &rPropSet, const char *pPropName, const css::uno::Any &rVal)
ScIndexEnumeration(const css::uno::Reference< css::container::XIndexAccess > &rInd, const OUString &rServiceName)
Definition: miscuno.cxx:189
static sal_Int16 GetInt16FromAny(const css::uno::Any &aAny)
Definition: miscuno.cxx:144
ScNameToIndexAccess(const css::uno::Reference< css::container::XNameAccess > &rNameObj)
Definition: miscuno.cxx:240
static sal_Int32 GetEnumPropertyImpl(const css::uno::Reference< css::beans::XPropertySet > &xProp, const OUString &rName, sal_Int32 nDefault)
Definition: miscuno.cxx:90
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Definition: miscuno.cxx:235
virtual css::uno::Any SAL_CALL nextElement() override
Definition: miscuno.cxx:209
css::uno::Reference< css::container::XIndexAccess > xIndex
Definition: miscuno.hxx:102
static sal_Int16 GetShortProperty(const css::uno::Reference< css::beans::XPropertySet > &xProp, const OUString &rName, sal_Int16 nDefault)
Definition: miscuno.cxx:53
detail::Optional< bool >::type tryAccess< bool >(css::uno::Any const &any)
static bool GetBoolFromAny(const css::uno::Any &aAny)
Definition: miscuno.cxx:138
css::uno::Sequence< OUString > aNames
Definition: miscuno.hxx:128
sal_uInt16 nPos