xref: /aoo41x/main/sc/source/ui/unoobj/miscuno.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sc.hxx"
30 
31 
32 
33 #include <tools/debug.hxx>
34 
35 #include "miscuno.hxx"
36 #include "unoguard.hxx"
37 
38 using namespace com::sun::star;
39 using ::com::sun::star::uno::Reference;
40 using ::com::sun::star::uno::Any;
41 using ::rtl::OUString;
42 
43 //------------------------------------------------------------------------
44 
45 //UNUSED2008-05  SC_SIMPLE_SERVICE_INFO( ScEmptyEnumeration, "ScEmptyEnumeration", "stardiv.unknown" )
46 //UNUSED2008-05  SC_SIMPLE_SERVICE_INFO( ScEmptyEnumerationAccess, "ScEmptyEnumerationAccess", "stardiv.unknown" )
47 //UNUSED2008-05  SC_SIMPLE_SERVICE_INFO( ScIndexEnumeration, "ScIndexEnumeration", "stardiv.unknown" )
48 //UNUSED2008-05  SC_SIMPLE_SERVICE_INFO( ScPrintSettingsObj, "ScPrintSettingsObj", "stardiv.unknown" )
49 
50 SC_SIMPLE_SERVICE_INFO( ScNameToIndexAccess, "ScNameToIndexAccess", "stardiv.unknown" )
51 
52 //------------------------------------------------------------------------
53 
54 //	static
55 uno::Reference<uno::XInterface> ScUnoHelpFunctions::AnyToInterface( const uno::Any& rAny )
56 {
57 	if ( rAny.getValueTypeClass() == uno::TypeClass_INTERFACE )
58 	{
59 		return uno::Reference<uno::XInterface>(rAny, uno::UNO_QUERY);
60 	}
61 	return uno::Reference<uno::XInterface>();	//! Exception?
62 }
63 
64 //	static
65 sal_Bool ScUnoHelpFunctions::GetBoolProperty( const uno::Reference<beans::XPropertySet>& xProp,
66 											const rtl::OUString& rName, sal_Bool bDefault )
67 {
68 	sal_Bool bRet = bDefault;
69 	if ( xProp.is() )
70 	{
71 		try
72 		{
73 			uno::Any aAny(xProp->getPropertyValue( rName ));
74 			//!	type conversion???
75 			//	operator >>= shouldn't be used for bool (?)
76 			if ( aAny.getValueTypeClass() == uno::TypeClass_BOOLEAN )
77 			{
78 				//!	safe way to get bool value from any???
79 				bRet = *(sal_Bool*)aAny.getValue();
80 			}
81 		}
82 		catch(uno::Exception&)
83 		{
84 			// keep default
85 		}
86 	}
87 	return bRet;
88 }
89 
90 //	static
91 sal_Int32 ScUnoHelpFunctions::GetLongProperty( const uno::Reference<beans::XPropertySet>& xProp,
92 											const rtl::OUString& rName, long nDefault )
93 {
94 	sal_Int32 nRet = nDefault;
95 	if ( xProp.is() )
96 	{
97 		try
98 		{
99 			//!	type conversion???
100 			xProp->getPropertyValue( rName ) >>= nRet;
101 		}
102 		catch(uno::Exception&)
103 		{
104 			// keep default
105 		}
106 	}
107 	return nRet;
108 }
109 
110 //	static
111 sal_Int32 ScUnoHelpFunctions::GetEnumProperty( const uno::Reference<beans::XPropertySet>& xProp,
112 											const rtl::OUString& rName, long nDefault )
113 {
114 	sal_Int32 nRet = nDefault;
115 	if ( xProp.is() )
116 	{
117 		try
118 		{
119 			uno::Any aAny(xProp->getPropertyValue( rName ));
120 
121 			if ( aAny.getValueTypeClass() == uno::TypeClass_ENUM )
122 			{
123 				//!	get enum value from any???
124 				nRet = *(sal_Int32*)aAny.getValue();
125 			}
126 			else
127 			{
128 				//!	type conversion???
129 				aAny >>= nRet;
130 			}
131 		}
132 		catch(uno::Exception&)
133 		{
134 			// keep default
135 		}
136 	}
137 	return nRet;
138 }
139 
140 // static
141 OUString ScUnoHelpFunctions::GetStringProperty(
142     const Reference<beans::XPropertySet>& xProp, const OUString& rName, const OUString& rDefault )
143 {
144     OUString aRet = rDefault;
145     if (!xProp.is())
146         return aRet;
147 
148     try
149     {
150         Any any = xProp->getPropertyValue(rName);
151         any >>= aRet;
152     }
153     catch (const uno::Exception&)
154     {
155     }
156 
157     return aRet;
158 }
159 
160 //	static
161 sal_Bool ScUnoHelpFunctions::GetBoolFromAny( const uno::Any& aAny )
162 {
163 	if ( aAny.getValueTypeClass() == uno::TypeClass_BOOLEAN )
164 		return *(sal_Bool*)aAny.getValue();
165 	return sal_False;
166 }
167 
168 //	static
169 sal_Int16 ScUnoHelpFunctions::GetInt16FromAny( const uno::Any& aAny )
170 {
171 	sal_Int16 nRet = 0;
172 	if ( aAny >>= nRet )
173 		return nRet;
174 	return 0;
175 }
176 
177 //	static
178 sal_Int32 ScUnoHelpFunctions::GetInt32FromAny( const uno::Any& aAny )
179 {
180 	sal_Int32 nRet = 0;
181 	if ( aAny >>= nRet )
182 		return nRet;
183 	return 0;
184 }
185 
186 //	static
187 sal_Int32 ScUnoHelpFunctions::GetEnumFromAny( const uno::Any& aAny )
188 {
189 	sal_Int32 nRet = 0;
190 	if ( aAny.getValueTypeClass() == uno::TypeClass_ENUM )
191 		nRet = *(sal_Int32*)aAny.getValue();
192 	else
193 		aAny >>= nRet;
194 	return nRet;
195 }
196 
197 //	static
198 void ScUnoHelpFunctions::SetBoolInAny( uno::Any& rAny, sal_Bool bValue )
199 {
200 	rAny.setValue( &bValue, getBooleanCppuType() );
201 }
202 
203 //  static
204 void ScUnoHelpFunctions::SetOptionalPropertyValue(
205     Reference<beans::XPropertySet>& rPropSet, const sal_Char* pPropName, const Any& rVal )
206 {
207     try
208     {
209         rPropSet->setPropertyValue(OUString::createFromAscii(pPropName), rVal);
210     }
211     catch (const beans::UnknownPropertyException&)
212     {
213         // ignored - not supported.
214     }
215 }
216 
217 //------------------------------------------------------------------------
218 
219 ScIndexEnumeration::ScIndexEnumeration(const uno::Reference<container::XIndexAccess>& rInd,
220                                        const rtl::OUString& rServiceName) :
221 	xIndex( rInd ),
222     sServiceName(rServiceName),
223 	nPos( 0 )
224 {
225 }
226 
227 ScIndexEnumeration::~ScIndexEnumeration()
228 {
229 }
230 
231 // XEnumeration
232 
233 sal_Bool SAL_CALL ScIndexEnumeration::hasMoreElements() throw(uno::RuntimeException)
234 {
235 	ScUnoGuard aGuard;
236 	return ( nPos < xIndex->getCount() );
237 }
238 
239 uno::Any SAL_CALL ScIndexEnumeration::nextElement() throw(container::NoSuchElementException,
240 										lang::WrappedTargetException, uno::RuntimeException)
241 {
242 	ScUnoGuard aGuard;
243     uno::Any aReturn;
244     try
245     {
246         aReturn = xIndex->getByIndex(nPos++);
247     }
248     catch (lang::IndexOutOfBoundsException&)
249     {
250         throw container::NoSuchElementException();
251     }
252 	return aReturn;
253 }
254 
255 ::rtl::OUString SAL_CALL ScIndexEnumeration::getImplementationName()
256 	throw(::com::sun::star::uno::RuntimeException)
257 {
258 	return ::rtl::OUString::createFromAscii("ScIndexEnumeration");
259 }
260 
261 sal_Bool SAL_CALL ScIndexEnumeration::supportsService( const ::rtl::OUString& ServiceName )
262 	throw(::com::sun::star::uno::RuntimeException)
263 {
264 	return sServiceName == ServiceName;
265 }
266 
267 ::com::sun::star::uno::Sequence< ::rtl::OUString >
268 	SAL_CALL ScIndexEnumeration::getSupportedServiceNames(void)
269 	throw(::com::sun::star::uno::RuntimeException)
270 {
271 	::com::sun::star::uno::Sequence< ::rtl::OUString > aRet(1);
272 	::rtl::OUString* pArray = aRet.getArray();
273 	pArray[0] = sServiceName;
274 	return aRet;
275 }
276 
277 //------------------------------------------------------------------------
278 
279 //UNUSED2008-05  ScEmptyEnumerationAccess::ScEmptyEnumerationAccess()
280 //UNUSED2008-05  {
281 //UNUSED2008-05  }
282 //UNUSED2008-05
283 //UNUSED2008-05  ScEmptyEnumerationAccess::~ScEmptyEnumerationAccess()
284 //UNUSED2008-05  {
285 //UNUSED2008-05  }
286 //UNUSED2008-05
287 //UNUSED2008-05  // XEnumerationAccess
288 //UNUSED2008-05
289 //UNUSED2008-05  uno::Reference<container::XEnumeration> SAL_CALL ScEmptyEnumerationAccess::createEnumeration()
290 //UNUSED2008-05                                                      throw(uno::RuntimeException)
291 //UNUSED2008-05  {
292 //UNUSED2008-05      ScUnoGuard aGuard;
293 //UNUSED2008-05      return new ScEmptyEnumeration;
294 //UNUSED2008-05  }
295 //UNUSED2008-05
296 //UNUSED2008-05  uno::Type SAL_CALL ScEmptyEnumerationAccess::getElementType() throw(uno::RuntimeException)
297 //UNUSED2008-05  {
298 //UNUSED2008-05      ScUnoGuard aGuard;
299 //UNUSED2008-05      return getCppuType((uno::Reference<uno::XInterface>*)0);    // or what?
300 //UNUSED2008-05  }
301 //UNUSED2008-05
302 //UNUSED2008-05  sal_Bool SAL_CALL ScEmptyEnumerationAccess::hasElements() throw(uno::RuntimeException)
303 //UNUSED2008-05  {
304 //UNUSED2008-05      return sal_False;
305 //UNUSED2008-05  }
306 
307 //------------------------------------------------------------------------
308 
309 //UNUSED2008-05  ScEmptyEnumeration::ScEmptyEnumeration()
310 //UNUSED2008-05  {
311 //UNUSED2008-05  }
312 //UNUSED2008-05
313 //UNUSED2008-05  ScEmptyEnumeration::~ScEmptyEnumeration()
314 //UNUSED2008-05  {
315 //UNUSED2008-05  }
316 //UNUSED2008-05
317 //UNUSED2008-05  // XEnumeration
318 //UNUSED2008-05
319 //UNUSED2008-05  sal_Bool SAL_CALL ScEmptyEnumeration::hasMoreElements() throw(uno::RuntimeException)
320 //UNUSED2008-05  {
321 //UNUSED2008-05      ScUnoGuard aGuard;
322 //UNUSED2008-05      return sal_False;
323 //UNUSED2008-05  }
324 //UNUSED2008-05
325 //UNUSED2008-05  uno::Any SAL_CALL ScEmptyEnumeration::nextElement() throw(container::NoSuchElementException,
326 //UNUSED2008-05                                          lang::WrappedTargetException, uno::RuntimeException)
327 //UNUSED2008-05  {
328 //UNUSED2008-05      ScUnoGuard aGuard;
329 //UNUSED2008-05      return uno::Any();
330 //UNUSED2008-05  }
331 
332 //------------------------------------------------------------------------
333 
334 ScNameToIndexAccess::ScNameToIndexAccess( const com::sun::star::uno::Reference<
335 											com::sun::star::container::XNameAccess>& rNameObj ) :
336 	xNameAccess( rNameObj )
337 {
338 	//!	test for XIndexAccess interface at rNameObj, use that instead!
339 
340 	if ( xNameAccess.is() )
341 		aNames = xNameAccess->getElementNames();
342 }
343 
344 ScNameToIndexAccess::~ScNameToIndexAccess()
345 {
346 }
347 
348 // XIndexAccess
349 
350 sal_Int32 SAL_CALL ScNameToIndexAccess::getCount(  ) throw(::com::sun::star::uno::RuntimeException)
351 {
352 	return aNames.getLength();
353 }
354 
355 ::com::sun::star::uno::Any SAL_CALL ScNameToIndexAccess::getByIndex( sal_Int32 nIndex )
356 								throw(::com::sun::star::lang::IndexOutOfBoundsException,
357 										::com::sun::star::lang::WrappedTargetException,
358 										::com::sun::star::uno::RuntimeException)
359 {
360 	if ( xNameAccess.is() && nIndex >= 0 && nIndex < aNames.getLength() )
361 		return xNameAccess->getByName( aNames.getConstArray()[nIndex] );
362 
363 	throw lang::IndexOutOfBoundsException();
364 //    return uno::Any();
365 }
366 
367 // XElementAccess
368 
369 ::com::sun::star::uno::Type SAL_CALL ScNameToIndexAccess::getElementType(  )
370 								throw(::com::sun::star::uno::RuntimeException)
371 {
372 	if ( xNameAccess.is() )
373 		return xNameAccess->getElementType();
374 	else
375 		return uno::Type();
376 }
377 
378 sal_Bool SAL_CALL ScNameToIndexAccess::hasElements(  ) throw(::com::sun::star::uno::RuntimeException)
379 {
380 	return getCount() > 0;
381 }
382 
383 //------------------------------------------------------------------------
384 
385 //UNUSED2008-05  ScPrintSettingsObj::ScPrintSettingsObj()
386 //UNUSED2008-05  {
387 //UNUSED2008-05  }
388 //UNUSED2008-05
389 //UNUSED2008-05  ScPrintSettingsObj::~ScPrintSettingsObj()
390 //UNUSED2008-05  {
391 //UNUSED2008-05  }
392 //UNUSED2008-05
393 //UNUSED2008-05  // XPropertySet
394 //UNUSED2008-05
395 //UNUSED2008-05  uno::Reference<beans::XPropertySetInfo> SAL_CALL ScPrintSettingsObj::getPropertySetInfo()
396 //UNUSED2008-05                                                          throw(uno::RuntimeException)
397 //UNUSED2008-05  {
398 //UNUSED2008-05      return NULL;
399 //UNUSED2008-05  }
400 //UNUSED2008-05
401 //UNUSED2008-05  void SAL_CALL ScPrintSettingsObj::setPropertyValue(
402 //UNUSED2008-05                          const rtl::OUString& /* aPropertyName */, const uno::Any& /* aValue */ )
403 //UNUSED2008-05                  throw(beans::UnknownPropertyException, beans::PropertyVetoException,
404 //UNUSED2008-05                          lang::IllegalArgumentException, lang::WrappedTargetException,
405 //UNUSED2008-05                          uno::RuntimeException)
406 //UNUSED2008-05  {
407 //UNUSED2008-05      //! later...
408 //UNUSED2008-05  }
409 //UNUSED2008-05
410 //UNUSED2008-05  uno::Any SAL_CALL ScPrintSettingsObj::getPropertyValue( const rtl::OUString& /* aPropertyName */ )
411 //UNUSED2008-05                  throw(beans::UnknownPropertyException, lang::WrappedTargetException,
412 //UNUSED2008-05                          uno::RuntimeException)
413 //UNUSED2008-05  {
414 //UNUSED2008-05      //! later...
415 //UNUSED2008-05      return uno::Any();
416 //UNUSED2008-05  }
417 //UNUSED2008-05
418 //UNUSED2008-05  SC_IMPL_DUMMY_PROPERTY_LISTENER( ScPrintSettingsObj )
419 
420 
421 //------------------------------------------------------------------------
422 
423 
424 
425