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  try
174  {
175  rPropSet->setPropertyValue(OUString::createFromAscii(pPropName), rVal);
176  }
177  catch (const beans::UnknownPropertyException&)
178  {
179  // ignored - not supported.
180  }
181 }
182 
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 
192 {
193 }
194 
195 // XEnumeration
196 
198 {
199  SolarMutexGuard aGuard;
200  return ( nPos < xIndex->getCount() );
201 }
202 
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 
219 {
220  return "ScIndexEnumeration";
221 }
222 
223 sal_Bool SAL_CALL ScIndexEnumeration::supportsService( const OUString& ServiceName )
224 {
225  return cppu::supportsService(this, ServiceName);
226 }
227 
228 css::uno::Sequence< OUString >
230 {
231  return { sServiceName };
232 }
233 
234 ScNameToIndexAccess::ScNameToIndexAccess( const css::uno::Reference<
235  css::container::XNameAccess>& rNameObj ) :
236  xNameAccess( rNameObj )
237 {
239 
240  if ( xNameAccess.is() )
241  aNames = xNameAccess->getElementNames();
242 }
243 
245 {
246 }
247 
248 // XIndexAccess
249 
250 sal_Int32 SAL_CALL ScNameToIndexAccess::getCount( )
251 {
252  return aNames.getLength();
253 }
254 
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 
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 
274 {
275  return getCount() > 0;
276 }
277 
278 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual css::uno::Any SAL_CALL getByIndex(sal_Int32 Index) override
Definition: miscuno.cxx:255
virtual sal_Int32 SAL_CALL getCount() override
Definition: miscuno.cxx:250
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:273
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
Definition: miscuno.cxx:223
OUString sServiceName
Definition: miscuno.hxx:104
virtual ~ScNameToIndexAccess() override
Definition: miscuno.cxx:244
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:265
virtual OUString SAL_CALL getImplementationName() override
Definition: miscuno.cxx:218
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
virtual sal_Bool SAL_CALL hasMoreElements() override
Definition: miscuno.cxx:197
sal_Int32 nPos
Definition: miscuno.hxx:105
css::uno::Reference< css::container::XNameAccess > xNameAccess
Definition: miscuno.hxx:128
virtual ~ScIndexEnumeration() override
Definition: miscuno.cxx:191
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:64
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:183
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:234
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:229
virtual css::uno::Any SAL_CALL nextElement() override
Definition: miscuno.cxx:203
css::uno::Reference< css::container::XIndexAccess > xIndex
Definition: miscuno.hxx:103
static sal_Int16 GetShortProperty(const css::uno::Reference< css::beans::XPropertySet > &xProp, const OUString &rName, sal_Int16 nDefault)
Definition: miscuno.cxx:53
const char sServiceName[]
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:129
sal_uInt16 nPos