Update ooo320-m1
[ooovba.git] / extensions / source / resource / resource.cxx
blob088b31b81ec39193f6b7fbbb982be8ae02caff14
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: resource.cxx,v $
10 * $Revision: 1.14 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_extensions.hxx"
33 #include "res_services.hxx"
35 #include <vos/mutex.hxx>
36 #include <uno/lbnames.h> // CPPU_CURRENT_LANGUAGE_BINDING_NAME macro, which specify the environment type
37 #include <cppuhelper/factory.hxx> // helper for factories
38 #include <cppuhelper/implbase3.hxx> // helper for implementations
40 #include <com/sun/star/lang/XServiceInfo.hpp>
41 #include <com/sun/star/script/XInvocation.hpp>
42 #include <com/sun/star/script/XTypeConverter.hpp>
43 #include <com/sun/star/reflection/InvocationTargetException.hpp>
44 #include <com/sun/star/beans/XExactName.hpp>
45 #include <com/sun/star/beans/PropertyValue.hpp>
46 #include <com/sun/star/beans/PropertyState.hpp>
48 #include <tools/resmgr.hxx>
49 #include <tools/rcid.h>
50 #include <tools/resary.hxx>
51 #include <vcl/svapp.hxx>
53 #include <rtl/ustring.hxx>
54 #include <rtl/strbuf.hxx>
56 using namespace vos;
57 using namespace rtl;
58 using namespace com::sun::star::uno;
59 using namespace com::sun::star::lang;
60 using namespace com::sun::star::registry;
61 using namespace com::sun::star::script;
62 using namespace com::sun::star::beans;
63 using namespace com::sun::star::reflection;
65 //------------------------------------------------------------------------
66 //------------------------------------------------------------------------
67 //------------------------------------------------------------------------
68 class ResourceService : public cppu::WeakImplHelper3< XInvocation, XExactName, XServiceInfo >
70 public:
71 ResourceService( const Reference< XMultiServiceFactory > & );
72 ~ResourceService();
74 // XServiceInfo
75 OUString SAL_CALL getImplementationName() throw();
76 BOOL SAL_CALL supportsService(const OUString& ServiceName) throw();
77 Sequence< OUString > SAL_CALL getSupportedServiceNames(void) throw();
79 static Sequence< OUString > getSupportedServiceNames_Static(void) throw();
80 static OUString getImplementationName_Static() throw()
82 return OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.extensions.ResourceService"));
84 static Reference< XInterface > Create( const Reference< XComponentContext >& _rxContext );
86 // XExactName
87 OUString SAL_CALL getExactName( const OUString & ApproximateName ) throw(RuntimeException);
89 // XInvokation
90 Reference< XIntrospectionAccess > SAL_CALL getIntrospection(void) throw(RuntimeException);
91 Any SAL_CALL invoke(const OUString& FunctionName, const Sequence< Any >& Params, Sequence< sal_Int16 >& OutParamIndex, Sequence< Any >& OutParam) throw(IllegalArgumentException, CannotConvertException, InvocationTargetException, RuntimeException);
92 void SAL_CALL setValue(const OUString& PropertyName, const Any& Value) throw(UnknownPropertyException, CannotConvertException, InvocationTargetException, RuntimeException);
93 Any SAL_CALL getValue(const OUString& PropertyName) throw(UnknownPropertyException, RuntimeException);
94 BOOL SAL_CALL hasMethod(const OUString& Name) throw(RuntimeException);
95 BOOL SAL_CALL hasProperty(const OUString& Name) throw(RuntimeException);
96 private:
97 Reference< XTypeConverter > getTypeConverter() const;
98 Reference< XInvocation > getDefaultInvocation() const;
100 Reference< XMultiServiceFactory > xSMgr;
101 Reference< XInvocation > xDefaultInvocation;
102 Reference< XTypeConverter > xTypeConverter;
103 OUString aFileName;
104 ResMgr * pResMgr;
108 //-----------------------------------------------------------------------------
109 ResourceService::ResourceService( const Reference< XMultiServiceFactory > & rSMgr )
110 : xSMgr( rSMgr )
111 , pResMgr( NULL )
115 //-----------------------------------------------------------------------------
116 Reference< XInterface > ResourceService::Create( const Reference< XComponentContext >& _rxContext )
118 Reference< XMultiServiceFactory > xFactory( _rxContext->getServiceManager(), UNO_QUERY_THROW );
119 return *( new ResourceService( xFactory ) );
122 //-----------------------------------------------------------------------------
123 ResourceService::~ResourceService()
125 delete pResMgr;
128 // XServiceInfo
129 OUString ResourceService::getImplementationName() throw()
131 return getImplementationName_Static();
134 // XServiceInfo
135 BOOL SAL_CALL ResourceService::supportsService(const OUString& ServiceName) throw()
137 Sequence< OUString > aSNL = getSupportedServiceNames();
138 const OUString * pArray = aSNL.getConstArray();
139 for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
140 if( pArray[i] == ServiceName )
141 return TRUE;
142 return FALSE;
145 // XServiceInfo
146 Sequence< OUString > SAL_CALL ResourceService::getSupportedServiceNames(void) throw()
148 return getSupportedServiceNames_Static();
151 // ResourceService
152 Sequence< OUString > ResourceService::getSupportedServiceNames_Static(void) throw()
154 Sequence< OUString > aSNS( 1 );
155 aSNS.getArray()[0] = OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.resource.VclStringResourceLoader"));
156 return aSNS;
159 // ResourceService
160 Reference< XTypeConverter > ResourceService::getTypeConverter() const
162 OGuard aGuard( Application::GetSolarMutex() );
163 if( xSMgr.is() )
165 Reference< XTypeConverter > xConv( xSMgr->createInstance( OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.script.Converter" ))), UNO_QUERY );
166 ((ResourceService*)this)->xTypeConverter = xConv;
168 return xTypeConverter;
171 // ResourceService
172 Reference< XInvocation > ResourceService::getDefaultInvocation() const
174 OGuard aGuard( Application::GetSolarMutex() );
175 /* f�hrt zur Zeit noch zu einer rekursion
176 if( xSMgr.is() )
178 Reference< XSingleServiceFactory > xFact( xSMgr->createInstance( OUString::createFromAscii("com.sun.star.script.Invocation") ), UNO_QUERY );
179 if( xFact.is() )
181 Sequence< Any > aArgs( 1 );
182 Reference< XInterface > xThis( *this );
183 aArgs.getArray()[0].set( &xThis, XInterface_Reference< get >lection() );
184 Reference< XInvokation > xI( xFact->createInstanceWithArguments( aArgs ), UNO_QUERY );
185 ((ResourceService*)this)->xDefaultInvocation = xI;
189 return xDefaultInvocation;
192 // XExactName
193 OUString SAL_CALL ResourceService::getExactName( const OUString & ApproximateName ) throw(RuntimeException)
195 OUString aName( ApproximateName );
196 aName = aName.toAsciiLowerCase();
197 if( aName.equalsAscii("filename") )
198 return OUString(RTL_CONSTASCII_USTRINGPARAM("FileName"));
199 else if( aName.equalsAscii("getstring" ))
200 return OUString(RTL_CONSTASCII_USTRINGPARAM("getString"));
201 else if( aName.equalsAscii("getstrings" ))
202 return OUString(RTL_CONSTASCII_USTRINGPARAM("getStrings"));
203 else if( aName.equalsAscii("hasstring") )
204 return OUString(RTL_CONSTASCII_USTRINGPARAM("hasString"));
205 else if( aName.equalsAscii("hasstrings") )
206 return OUString(RTL_CONSTASCII_USTRINGPARAM("hasStrings"));
207 else if( aName.equalsAscii("getstringlist") )
208 return OUString(RTL_CONSTASCII_USTRINGPARAM("getStringList"));
209 else if( aName.equalsAscii("hasStringList") )
210 return OUString(RTL_CONSTASCII_USTRINGPARAM("hasStringList"));
211 Reference< XExactName > xEN( getDefaultInvocation(), UNO_QUERY );
212 if( xEN.is() )
213 return xEN->getExactName( ApproximateName );
214 return OUString();
217 // XInvokation
218 Reference< XIntrospectionAccess > SAL_CALL ResourceService::getIntrospection(void)
219 throw(RuntimeException)
221 Reference< XInvocation > xI = getDefaultInvocation();
222 if( xI.is() )
223 return xI->getIntrospection();
224 return Reference< XIntrospectionAccess >();
227 // XInvokation
228 Any SAL_CALL ResourceService::invoke
230 const OUString& FunctionName,
231 const Sequence< Any >& Params,
232 Sequence< sal_Int16 >& OutParamIndex,
233 Sequence< Any >& OutParam
235 throw(IllegalArgumentException, CannotConvertException, InvocationTargetException, RuntimeException)
237 Any aRet;
238 if( FunctionName.equalsAscii("getString")
239 || FunctionName.equalsAscii("getStrings" )
240 || FunctionName.equalsAscii("hasString" )
241 || FunctionName.equalsAscii("hasStrings" )
244 sal_Int32 nElements = Params.getLength();
245 if( nElements < 1 )
246 throw IllegalArgumentException();
247 if( nElements > 1 && (FunctionName.equalsAscii("getString") || FunctionName.equalsAscii("hasString") ) )
248 throw IllegalArgumentException();
249 if( !pResMgr )
250 throw IllegalArgumentException();
252 Sequence< OUString > aStrings( Params.getLength() );
253 Sequence< sal_Bool > aBools( Params.getLength() );
254 const Any* pIn = Params.getConstArray();
255 OUString* pOutString = aStrings.getArray();
256 sal_Bool* pOutBool = aBools.getArray();
258 Reference< XTypeConverter > xC = getTypeConverter();
259 bool bGetBranch = FunctionName.equalsAscii( "getString" ) || FunctionName.equalsAscii( "getStrings" );
261 OGuard aGuard( Application::GetSolarMutex() );
262 for( sal_Int32 n = 0; n < nElements; n++ )
264 sal_Int32 nId = 0;
265 if( !(pIn[n] >>= nId) )
267 if( xC.is() )
269 xC->convertToSimpleType( pIn[n], TypeClass_LONG ) >>= nId;
271 else
272 throw CannotConvertException();
274 if( nId > 0xFFFF || nId < 0 )
275 throw IllegalArgumentException();
277 if( bGetBranch )
279 ResId aId( (USHORT)nId, *pResMgr );
280 aId.SetRT( RSC_STRING );
281 if( pResMgr->IsAvailable( aId ) )
283 String aStr( aId );
284 pOutString[n] = aStr;
286 else
287 throw IllegalArgumentException();
289 else // hasString(s)
291 sal_Bool bRet = sal_False;
292 if( pResMgr )
294 ResId aId( (USHORT)nId, *pResMgr );
295 aId.SetRT( RSC_STRING );
296 bRet = pResMgr->IsAvailable( aId );
298 pOutBool[n] = bRet;
301 if( FunctionName.equalsAscii("getString") )
302 aRet <<= pOutString[0];
303 else if( FunctionName.equalsAscii("getStrings" ) )
304 aRet <<= aStrings;
305 else if( FunctionName.equalsAscii("hasString" ) )
306 aRet <<= pOutBool[0];
307 else
308 aRet <<= aBools;
310 else if( FunctionName.equalsAscii("getStringList") || FunctionName.equalsAscii("hasStringList" ) )
312 if( Params.getLength() != 1 )
313 throw IllegalArgumentException();
314 Reference< XTypeConverter > xC = getTypeConverter();
315 OGuard aGuard( Application::GetSolarMutex() );
317 sal_Int32 nId = 0;
318 if( !(Params.getConstArray()[0] >>= nId) )
320 if( xC.is() )
322 xC->convertToSimpleType( Params.getConstArray()[0], TypeClass_LONG ) >>= nId;
324 else
325 throw CannotConvertException();
328 if( FunctionName.equalsAscii("getStringList") )
330 ResId aId( (USHORT)nId, *pResMgr );
331 aId.SetRT( RSC_STRINGARRAY );
332 if( pResMgr->IsAvailable( aId ) )
334 ResStringArray aStr( aId );
335 int nEntries = aStr.Count();
336 Sequence< PropertyValue > aPropSeq( nEntries );
337 PropertyValue* pOut = aPropSeq.getArray();
338 for( int i = 0; i < nEntries; i++ )
340 pOut[i].Name = aStr.GetString( i );
341 pOut[i].Handle = -1;
342 pOut[i].Value <<= aStr.GetValue( i );
343 pOut[i].State = PropertyState_DIRECT_VALUE;
345 aRet <<= aPropSeq;
347 else
348 throw IllegalArgumentException();
350 else // hasStringList
352 sal_Bool bRet = sal_False;
353 if( pResMgr )
355 ResId aId( (USHORT)nId, *pResMgr );
356 aId.SetRT( RSC_STRINGARRAY );
357 bRet = pResMgr->IsAvailable( aId );
359 aRet <<= bRet;
362 else
364 Reference< XInvocation > xI = getDefaultInvocation();
365 if( xI.is() )
366 return xI->invoke( FunctionName, Params, OutParamIndex, OutParam );
367 else
368 throw IllegalArgumentException();
370 return aRet;
373 // XInvokation
374 void SAL_CALL ResourceService::setValue(const OUString& PropertyName, const Any& Value)
375 throw(UnknownPropertyException, CannotConvertException, InvocationTargetException, RuntimeException)
377 if( PropertyName.equalsAscii("FileName") )
379 OUString aName;
380 if( !(Value >>= aName) )
382 Reference< XTypeConverter > xC = getTypeConverter();
383 if( xC.is() )
384 xC->convertToSimpleType( Value, TypeClass_STRING ) >>= aName;
385 else
386 throw CannotConvertException();
389 OGuard aGuard( Application::GetSolarMutex() );
390 OStringBuffer aBuf( aName.getLength()+8 );
391 aBuf.append( OUStringToOString( aName, osl_getThreadTextEncoding() ) );
392 ResMgr * pRM = ResMgr::CreateResMgr( aBuf.getStr() );
393 if( !pRM )
394 throw InvocationTargetException();
395 if( pResMgr )
396 delete pResMgr;
397 pResMgr = pRM;
398 aFileName = OStringToOUString( aBuf.makeStringAndClear(), osl_getThreadTextEncoding() );
400 else
402 Reference< XInvocation > xI = getDefaultInvocation();
403 if( xI.is() )
404 xI->setValue( PropertyName, Value );
405 else
406 throw UnknownPropertyException();
410 // XInvokation
411 Any SAL_CALL ResourceService::getValue(const OUString& PropertyName)
412 throw(UnknownPropertyException, RuntimeException)
414 OGuard aGuard( Application::GetSolarMutex() );
415 if( PropertyName.equalsAscii("FileName" ))
416 return makeAny( aFileName );
418 Reference< XInvocation > xI = getDefaultInvocation();
419 if( xI.is() )
420 return xI->getValue( PropertyName );
422 throw UnknownPropertyException();
425 // XInvokation
426 BOOL SAL_CALL ResourceService::hasMethod(const OUString& Name)
427 throw(RuntimeException)
429 if( Name.equalsAscii("getString") ||
430 Name.equalsAscii("getStrings") ||
431 Name.equalsAscii("hasString") ||
432 Name.equalsAscii("hasStrings") ||
433 Name.equalsAscii("getStringList") ||
434 Name.equalsAscii("hasStringList")
436 return TRUE;
437 else
439 Reference< XInvocation > xI = getDefaultInvocation();
440 if( xI.is() )
441 return xI->hasMethod( Name );
442 else
443 return FALSE;
447 // XInvokation
448 BOOL SAL_CALL ResourceService::hasProperty(const OUString& Name)
449 throw(RuntimeException)
451 if( Name.equalsAscii("FileName") )
452 return TRUE;
453 else
455 Reference< XInvocation > xI = getDefaultInvocation();
456 if( xI.is() )
457 return xI->hasProperty( Name );
458 else
459 return FALSE;
463 namespace res
465 ComponentInfo getComponentInfo_VclStringResourceLoader()
467 ComponentInfo aInfo;
468 aInfo.aSupportedServices = ResourceService::getSupportedServiceNames_Static();
469 aInfo.sImplementationName = ResourceService::getImplementationName_Static();
470 aInfo.pFactory = &ResourceService::Create;
471 return aInfo;