1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sd.hxx"
26 #include <com/sun/star/util/XCloneable.hpp>
27 #include <com/sun/star/util/XMacroExpander.hpp>
28 #include <com/sun/star/animations/XAnimationNodeSupplier.hpp>
29 #include <com/sun/star/container/XNameAccess.hpp>
30 #include <com/sun/star/xml/sax/InputSource.hpp>
31 #include <com/sun/star/xml/sax/XParser.hpp>
32 #include <com/sun/star/xml/sax/SAXParseException.hpp>
33 #include <com/sun/star/beans/PropertyValue.hpp>
34 #include <com/sun/star/beans/XPropertySet.hpp>
35 #include <com/sun/star/presentation/EffectPresetClass.hpp>
36 #include <com/sun/star/beans/NamedValue.hpp>
37 #include <tools/urlobj.hxx>
38 #include <unotools/streamwrap.hxx>
39 #include <comphelper/processfactory.hxx>
40 #include <unotools/pathoptions.hxx>
41 #include <tools/stream.hxx>
42 
43 #include <tools/debug.hxx>
44 #include <rtl/uri.hxx>
45 #include <vcl/svapp.hxx>
46 #include <vos/mutex.hxx>
47 #include <unotools/ucbstreamhelper.hxx>
48 #include <CustomAnimationPreset.hxx>
49 
50 #include <algorithm>
51 
52 using namespace ::vos;
53 using namespace ::com::sun::star;
54 using namespace ::com::sun::star::animations;
55 using namespace ::com::sun::star::presentation;
56 
57 using ::rtl::OUString;
58 using ::com::sun::star::uno::UNO_QUERY;
59 using ::com::sun::star::uno::UNO_QUERY_THROW;
60 using ::com::sun::star::uno::Any;
61 using ::com::sun::star::uno::Sequence;
62 using ::com::sun::star::uno::Reference;
63 using ::com::sun::star::uno::Exception;
64 using ::com::sun::star::io::XInputStream;
65 using ::com::sun::star::lang::XMultiServiceFactory;
66 using ::com::sun::star::container::XNameAccess;
67 using ::com::sun::star::beans::PropertyValue;
68 using ::com::sun::star::util::XCloneable;
69 using ::com::sun::star::beans::NamedValue;
70 
71 namespace sd {
72 
73 static Reference< XNameAccess > getNodeAccess( const Reference< XMultiServiceFactory >& xConfigProvider, const OUString& rNodePath )
74 {
75 	Reference< XNameAccess > xConfigAccess;
76 
77 	try
78 	{
79 		Sequence< Any > aArgs( 1 );
80 		PropertyValue   aPropValue;
81 		aPropValue.Name  = OUString( RTL_CONSTASCII_USTRINGPARAM( "nodepath" ));
82 		aPropValue.Value <<= rNodePath;
83 		aArgs[0] <<= aPropValue;
84 
85 		xConfigAccess = Reference< XNameAccess >::query(
86 			xConfigProvider->createInstanceWithArguments(
87 				OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.configuration.ConfigurationAccess" )),
88 				aArgs ));
89 	}
90 	catch( Exception& e )
91 	{
92 		(void)e;
93 		DBG_ERROR( "sd::getNodeAccess(), Exception catched!" );
94 	}
95 
96 	return xConfigAccess;
97 }
98 
99 void implImportLabels( const Reference< XMultiServiceFactory >& xConfigProvider, const OUString& rNodePath, UStringMap& rStringMap )
100 {
101 	try
102 	{
103 		Reference< XNameAccess > xConfigAccess( getNodeAccess( xConfigProvider, rNodePath ) );
104 		if( xConfigAccess.is() )
105 		{
106 			OUString aLabel( RTL_CONSTASCII_USTRINGPARAM( "Label" ) );
107 			Reference< XNameAccess > xNameAccess;
108 			Sequence< OUString > aNames( xConfigAccess->getElementNames() );
109 			const OUString* p = aNames.getConstArray();
110 			sal_Int32 n = aNames.getLength();
111 			while(n--)
112 			{
113 				xConfigAccess->getByName( *p ) >>= xNameAccess;
114 				if( xNameAccess.is() )
115 				{
116 					OUString aUIName;
117 					xNameAccess->getByName( aLabel ) >>= aUIName;
118 					if( aUIName.getLength() )
119 					{
120 						rStringMap[ *p ] = aUIName;
121 					}
122 				}
123 
124 				p++;
125 			}
126 		}
127 	}
128 	catch( lang::WrappedTargetException& e )
129 	{
130 		(void)e;
131 		DBG_ERROR( "sd::implImportLabels(), WrappedTargetException catched!" );
132 	}
133 	catch( Exception& e )
134 	{
135 		(void)e;
136 		DBG_ERROR( "sd::implImportLabels(), Exception catched!" );
137 	}
138 }
139 
140 CustomAnimationPreset::CustomAnimationPreset( CustomAnimationEffectPtr pEffect )
141 {
142 	maPresetId = pEffect->getPresetId();
143 	maProperty = pEffect->getProperty();
144 	mnPresetClass = pEffect->getPresetClass();
145 
146 	add( pEffect );
147 
148 	mfDuration = pEffect->getDuration();
149 	maDefaultSubTyp = pEffect->getPresetSubType();
150 
151 	mbIsTextOnly = false;
152 
153 	Sequence< NamedValue > aUserData( pEffect->getNode()->getUserData() );
154 	sal_Int32 nLength = aUserData.getLength();
155 	const NamedValue* p = aUserData.getConstArray();
156 
157 	while( nLength-- )
158 	{
159 		if( p->Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "text-only" ) ) )
160 		{
161 			mbIsTextOnly = true;
162 			break;
163 		}
164 		p++;
165 	}
166 
167 }
168 
169 void CustomAnimationPreset::add( CustomAnimationEffectPtr pEffect )
170 {
171 	maSubTypes[ pEffect->getPresetSubType() ] = pEffect;
172 }
173 
174 UStringList CustomAnimationPreset::getSubTypes()
175 {
176 	UStringList aSubTypes;
177 
178 	if( maSubTypes.size() > 1 )
179 	{
180 		EffectsSubTypeMap::iterator aIter( maSubTypes.begin() );
181 		const EffectsSubTypeMap::iterator aEnd( maSubTypes.end() );
182 		while( aIter != aEnd )
183 			aSubTypes.push_back( (*aIter++).first );
184 	}
185 
186 	return aSubTypes;
187 }
188 
189 Reference< XAnimationNode > CustomAnimationPreset::create( const rtl::OUString& rstrSubType )
190 {
191 	try
192 	{
193 		OUString strSubType( rstrSubType );
194 		if( strSubType.getLength() == 0 )
195 			strSubType = maDefaultSubTyp;
196 
197 		CustomAnimationEffectPtr pEffect = maSubTypes[strSubType];
198 		if( pEffect.get() )
199 		{
200 			Reference< XCloneable > xCloneable( pEffect->getNode(), UNO_QUERY_THROW );
201 			Reference< XAnimationNode > xNode( xCloneable->createClone(), UNO_QUERY_THROW );
202 			return xNode;
203 		}
204 	}
205 	catch( Exception& e )
206 	{
207 		(void)e;
208 		DBG_ERROR( "sd::CustomAnimationPresets::create(), exception catched!" );
209 	}
210 
211 	Reference< XAnimationNode > xNode;
212 	return xNode;
213 }
214 
215 UStringList CustomAnimationPreset::getProperties() const
216 {
217 	String aProperties( maProperty );
218 	sal_uInt16 nTokens = aProperties.GetTokenCount();
219 	sal_uInt16 nToken;
220 	UStringList aPropertyList;
221 	for( nToken = 0; nToken < nTokens; nToken++ )
222 		aPropertyList.push_back( aProperties.GetToken( nToken ) );
223 
224 	return aPropertyList;
225 
226 }
227 
228 bool CustomAnimationPreset::hasProperty( const OUString& rProperty )const
229 {
230 	String aProperties( maProperty );
231 	String aProperty( rProperty );
232 	sal_uInt16 nTokens = aProperties.GetTokenCount();
233 	sal_uInt16 nToken;
234 	for( nToken = 0; nToken < nTokens; nToken++ )
235 	{
236 		if( aProperties.GetToken( nToken ) == aProperty )
237 			return true;
238 	}
239 
240 	return false;
241 }
242 
243 CustomAnimationPresets::CustomAnimationPresets()
244 {
245 }
246 
247 CustomAnimationPresets::~CustomAnimationPresets()
248 {
249 }
250 
251 void CustomAnimationPresets::init()
252 {
253 	importResources();
254 }
255 
256 Reference< XAnimationNode > implImportEffects( const Reference< XMultiServiceFactory >& xServiceFactory, const OUString& rPath )
257 {
258 	Reference< XAnimationNode > xRootNode;
259 
260 	try
261 	{
262 		// create stream
263 		SvStream*	pIStm = ::utl::UcbStreamHelper::CreateStream( rPath, STREAM_READ );
264 		Reference<XInputStream> xInputStream( new utl::OInputStreamWrapper( pIStm, sal_True ) );
265 
266 		// prepare ParserInputSrouce
267 		xml::sax::InputSource aParserInput;
268 		aParserInput.sSystemId = rPath;
269 		aParserInput.aInputStream = xInputStream;
270 
271 		// get parser
272 		Reference< xml::sax::XParser > xParser(
273 			xServiceFactory->createInstance(
274 				OUString::createFromAscii("com.sun.star.xml.sax.Parser") ),
275 			UNO_QUERY );
276 
277 		DBG_ASSERT( xParser.is(), "Can't create parser" );
278 		if( !xParser.is() )
279 			return xRootNode;
280 
281 		// get filter
282 		Reference< xml::sax::XDocumentHandler > xFilter(
283 			xServiceFactory->createInstance(
284 				OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.Xmloff.AnimationsImport" ) ) ), UNO_QUERY );
285 
286 		DBG_ASSERT( xFilter.is(), "Can't instantiate filter component." );
287 		if( !xFilter.is() )
288 			return xRootNode;
289 
290 		// connect parser and filter
291 		xParser->setDocumentHandler( xFilter );
292 
293 		// finally, parser the stream
294 		xParser->parseStream( aParserInput );
295 
296 		Reference< XAnimationNodeSupplier > xAnimationNodeSupplier( xFilter, UNO_QUERY );
297 		if( xAnimationNodeSupplier.is() )
298 			xRootNode = xAnimationNodeSupplier->getAnimationNode();
299 	}
300 	catch( xml::sax::SAXParseException& r )
301 	{
302         (void)r;
303 		DBG_ERROR( "sd::implImportEffects(), SAXParseException catched!" );
304 	}
305 	catch( xml::sax::SAXException& r )
306 	{
307         (void)r;
308 		DBG_ERROR( "sd::implImportEffects(), SAXException catched!" );
309 	}
310 	catch( io::IOException& r )
311 	{
312         (void)r;
313 		DBG_ERROR( "sd::implImportEffects(), IOException catched!" );
314 	}
315 	catch( Exception& r )
316 	{
317         (void)r;
318 		DBG_ERROR( "sd::importEffects(), Exception catched!" );
319 	}
320 
321 	return xRootNode;
322 }
323 
324 #define EXPAND_PROTOCOL "vnd.sun.star.expand:"
325 
326 void CustomAnimationPresets::importEffects()
327 {
328 	try
329 	{
330 		// Get service factory
331 		Reference< XMultiServiceFactory > xServiceFactory( comphelper::getProcessServiceFactory() );
332 		DBG_ASSERT( xServiceFactory.is(), "sd::CustomAnimationPresets::import(), got no service manager" );
333 		if( !xServiceFactory.is() )
334 			return;
335 
336         uno::Reference< beans::XPropertySet > xProps( xServiceFactory, UNO_QUERY );
337         uno::Reference< uno::XComponentContext > xContext;
338         xProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DefaultContext" ))) >>= xContext;
339 
340         uno::Reference< util::XMacroExpander > xMacroExpander;
341         if( xContext.is() )
342             xMacroExpander.set( xContext->getValueByName(
343                                     rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/singletons/com.sun.star.util.theMacroExpander"))),
344                                 UNO_QUERY );
345 
346 		Reference< XMultiServiceFactory > xConfigProvider(
347 			xServiceFactory->createInstance(
348 				OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.configuration.ConfigurationProvider" ))),
349 			UNO_QUERY_THROW );
350 
351         // read path to transition effects files from config
352         Any propValue = uno::makeAny(
353             beans::PropertyValue(
354 				OUString( RTL_CONSTASCII_USTRINGPARAM( "nodepath" )), -1,
355                 uno::makeAny( OUString( RTL_CONSTASCII_USTRINGPARAM("/org.openoffice.Office.Impress/Misc") )),
356                 beans::PropertyState_DIRECT_VALUE ) );
357 
358         Reference<container::XNameAccess> xNameAccess(
359             xConfigProvider->createInstanceWithArguments(
360                 OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.configuration.ConfigurationAccess")),
361                 Sequence<Any>( &propValue, 1 ) ), UNO_QUERY_THROW );
362         uno::Sequence< rtl::OUString > aFiles;
363         xNameAccess->getByName(
364             OUString( RTL_CONSTASCII_USTRINGPARAM("EffectFiles"))) >>= aFiles;
365 
366         for( sal_Int32 i=0; i<aFiles.getLength(); ++i )
367         {
368             rtl::OUString aURL = aFiles[i];
369             if( aURL.compareToAscii( RTL_CONSTASCII_STRINGPARAM( EXPAND_PROTOCOL )) == 0 )
370             {
371                 // cut protocol
372                 rtl::OUString aMacro( aURL.copy( sizeof ( EXPAND_PROTOCOL ) -1 ) );
373                 // decode uric class chars
374                 aMacro = rtl::Uri::decode( aMacro, rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8 );
375                 // expand macro string
376                 aURL = xMacroExpander->expandMacros( aMacro );
377             }
378 
379             mxRootNode = implImportEffects( xServiceFactory, aURL );
380 
381             if( mxRootNode.is() )
382             {
383                 Reference< XTimeContainer > xRootContainer( mxRootNode, UNO_QUERY_THROW );
384                 EffectSequenceHelper aSequence( xRootContainer );
385 
386                 EffectSequence::iterator aIter( aSequence.getBegin() );
387                 const EffectSequence::iterator aEnd( aSequence.getEnd() );
388 
389                 while( aIter != aEnd )
390                 {
391                     CustomAnimationEffectPtr pEffect = (*aIter);
392 
393                     const OUString aPresetId( pEffect->getPresetId() );
394                     CustomAnimationPresetPtr pDescriptor = getEffectDescriptor( aPresetId );
395                     if( pDescriptor.get() )
396                         pDescriptor->add( pEffect );
397                     else
398                     {
399                         pDescriptor.reset( new CustomAnimationPreset( pEffect ) );
400                         pDescriptor->maLabel = getUINameForPresetId( pEffect->getPresetId() );
401                         maEffectDiscriptorMap[aPresetId] = pDescriptor;
402                     }
403 
404                     aIter++;
405                 }
406             }
407         }
408     }
409 	catch( xml::sax::SAXParseException& r )
410 	{
411         (void)r;
412 		DBG_ERROR( "sd::CustomAnimationPresets::importEffects(), SAXParseException catched!" );
413 	}
414 	catch( xml::sax::SAXException& r )
415 	{
416         (void)r;
417 		DBG_ERROR( "sd::CustomAnimationPresets::importEffects(), SAXException catched!" );
418 	}
419 	catch( io::IOException& r )
420 	{
421         (void)r;
422 		DBG_ERROR( "sd::CustomAnimationPresets::importEffects(), IOException catched!" );
423 	}
424 	catch( Exception& r )
425 	{
426         (void)r;
427 		DBG_ERROR( "sd::CustomAnimationPresets::importEffects(), Exception catched!" );
428 	}
429 }
430 
431 void CustomAnimationPresets::importResources()
432 {
433 	try
434 	{
435 		// Get service factory
436 		Reference< XMultiServiceFactory > xServiceFactory( comphelper::getProcessServiceFactory() );
437 		DBG_ASSERT( xServiceFactory.is(), "sd::CustomAnimationPresets::import(), got no service manager" );
438 		if( !xServiceFactory.is() )
439 			return;
440 
441 		Reference< XMultiServiceFactory > xConfigProvider(
442 			xServiceFactory->createInstance(
443 				OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.configuration.ConfigurationProvider" ))),
444 			UNO_QUERY );
445 
446 		const OUString aPropertyPath( RTL_CONSTASCII_USTRINGPARAM( "/org.openoffice.Office.UI.Effects/UserInterface/Properties" ) );
447 		implImportLabels( xConfigProvider, aPropertyPath, maPropertyNameMap );
448 
449 		const OUString aEffectsPath( RTL_CONSTASCII_USTRINGPARAM( "/org.openoffice.Office.UI.Effects/UserInterface/Effects" ) );
450 		implImportLabels( xConfigProvider, aEffectsPath, maEffectNameMap );
451 
452 		importEffects();
453 
454 		const OUString aEntrancePath( RTL_CONSTASCII_USTRINGPARAM( "/org.openoffice.Office.UI.Effects/Presets/Entrance" ) );
455 		importPresets( xConfigProvider, aEntrancePath, maEntrancePresets );
456 
457 		const OUString aEmphasisPath( RTL_CONSTASCII_USTRINGPARAM( "/org.openoffice.Office.UI.Effects/Presets/Emphasis" ) );
458 		importPresets( xConfigProvider, aEmphasisPath, maEmphasisPresets );
459 
460 		const OUString aExitPath( RTL_CONSTASCII_USTRINGPARAM( "/org.openoffice.Office.UI.Effects/Presets/Exit" ) );
461 		importPresets( xConfigProvider, aExitPath, maExitPresets );
462 
463 		const OUString aMotionPathsPath( RTL_CONSTASCII_USTRINGPARAM( "/org.openoffice.Office.UI.Effects/Presets/MotionPaths" ) );
464 		importPresets( xConfigProvider, aMotionPathsPath, maMotionPathsPresets );
465 
466 		const OUString aMiscPath( RTL_CONSTASCII_USTRINGPARAM( "/org.openoffice.Office.UI.Effects/Presets/Misc" ) );
467 		importPresets( xConfigProvider, aMiscPath, maMiscPresets );
468 	}
469 	catch( lang::WrappedTargetException& e )
470 	{
471 		(void)e;
472 		DBG_ERROR( "sd::CustomAnimationPresets::importResources(), WrappedTargetException catched!" );
473 	}
474 	catch( Exception& e )
475 	{
476 		(void)e;
477 		DBG_ERROR( "sd::CustomAnimationPresets::importResources(), Exception catched!" );
478 	}
479 }
480 
481 void CustomAnimationPresets::importPresets( const Reference< XMultiServiceFactory >& xConfigProvider, const OUString& rNodePath, PresetCategoryList& rPresetMap  )
482 {
483 #ifdef DEBUG
484 	String aMissedPresetIds;
485 #endif
486 
487 	try
488 	{
489 		Reference< XNameAccess > xTypeAccess( getNodeAccess( xConfigProvider, rNodePath ) );
490 		if( xTypeAccess.is() )
491 		{
492 			Reference< XNameAccess > xCategoryAccess;
493 			const OUString aEffectsName( RTL_CONSTASCII_USTRINGPARAM( "Effects" ) );
494 			const OUString aLabelName( RTL_CONSTASCII_USTRINGPARAM( "Label" ) );
495 
496 			Sequence< OUString > aNames( xTypeAccess->getElementNames() );
497 			const OUString* p = aNames.getConstArray();
498 			sal_Int32 n = aNames.getLength();
499 			while(n--)
500 			{
501 				xTypeAccess->getByName( *p ) >>= xCategoryAccess;
502 
503 				if( xCategoryAccess.is() && xCategoryAccess->hasByName( aLabelName ) && xCategoryAccess->hasByName( aEffectsName ) )
504 				{
505 					OUString aLabel;
506 					xCategoryAccess->getByName( aLabelName ) >>= aLabel;
507 
508 					Sequence< OUString > aEffects;
509 					xCategoryAccess->getByName( aEffectsName ) >>= aEffects;
510 
511 					EffectDescriptorList aEffectsList;
512 
513 					const OUString* pEffectNames = aEffects.getConstArray();
514 					sal_Int32 nEffectCount = aEffects.getLength();
515 					while( nEffectCount-- )
516 					{
517 						CustomAnimationPresetPtr pEffect = getEffectDescriptor( *pEffectNames );
518 						if( pEffect.get() )
519 						{
520 							aEffectsList.push_back( pEffect );
521 						}
522 #ifdef DEBUG
523 						else
524 						{
525 							aMissedPresetIds += String(*pEffectNames);
526 							aMissedPresetIds += String( RTL_CONSTASCII_USTRINGPARAM("\n") );
527 						}
528 #endif
529 						pEffectNames++;
530 					}
531 					rPresetMap.push_back( PresetCategoryPtr( new PresetCategory( aLabel, aEffectsList ) ) );
532 				}
533 
534 				p++;
535 			}
536 		}
537 	}
538 	catch( Exception& e )
539 	{
540 		(void)e;
541 		DBG_ERROR( "sd::CustomAnimationPresets::importPresets(), Exception catched!" );
542 	}
543 
544 #ifdef DEBUG
545 	if( aMissedPresetIds.Len() )
546 	{
547 		ByteString aTmp( "sd::CustomAnimationPresets::importPresets(), invalid preset id!\n" );
548 		aTmp += ByteString( aMissedPresetIds, RTL_TEXTENCODING_ASCII_US );
549 		DBG_ERROR( aTmp.GetBuffer() );
550 	}
551 #endif
552 }
553 
554 CustomAnimationPresetPtr CustomAnimationPresets::getEffectDescriptor( const rtl::OUString& rPresetId ) const
555 {
556 	EffectDescriptorMap::const_iterator aIter( maEffectDiscriptorMap.find( rPresetId ) );
557 
558 	if( aIter != maEffectDiscriptorMap.end() )
559 	{
560 		return (*aIter).second;
561 	}
562 	else
563 	{
564 		return CustomAnimationPresetPtr((CustomAnimationPreset*)0);
565 	}
566 }
567 
568 const rtl::OUString& CustomAnimationPresets::getUINameForPresetId( const rtl::OUString& rPresetId ) const
569 {
570 	return translateName( rPresetId, maEffectNameMap );
571 }
572 
573 const rtl::OUString& CustomAnimationPresets::getUINameForProperty( const rtl::OUString& rPresetId ) const
574 {
575 	return translateName( rPresetId, maPropertyNameMap );
576 }
577 
578 const rtl::OUString& CustomAnimationPresets::translateName( const rtl::OUString& rId, const UStringMap& rNameMap ) const
579 {
580 	UStringMap::const_iterator aIter( rNameMap.find( rId ) );
581 
582 	if( aIter != rNameMap.end() )
583 	{
584 		return (*aIter).second;
585 	}
586 	else
587 	{
588 		return rId;
589 	}
590 }
591 void CustomAnimationPresets::changePresetSubType( CustomAnimationEffectPtr pEffect, const rtl::OUString& rPresetSubType ) const
592 {
593 	if( pEffect.get() && pEffect->getPresetSubType() != rPresetSubType )
594 	{
595 		CustomAnimationPresetPtr pDescriptor( getEffectDescriptor( pEffect->getPresetId() ) );
596 
597 		if( pDescriptor.get() )
598 		{
599 			Reference< XAnimationNode > xNewNode( pDescriptor->create( rPresetSubType ) );
600 			if( xNewNode.is() )
601 				pEffect->replaceNode( xNewNode );
602 		}
603 	}
604 }
605 
606 CustomAnimationPresets* CustomAnimationPresets::mpCustomAnimationPresets = 0;
607 
608 const CustomAnimationPresets& CustomAnimationPresets::getCustomAnimationPresets()
609 {
610 	if( !mpCustomAnimationPresets )
611 	{
612 		OGuard aGuard( Application::GetSolarMutex() );
613 
614 		if( !mpCustomAnimationPresets )
615 		{
616 			mpCustomAnimationPresets = new sd::CustomAnimationPresets();
617 			mpCustomAnimationPresets->init();
618 		}
619 	}
620 
621 	return *mpCustomAnimationPresets;
622 }
623 
624 Reference< XAnimationNode > CustomAnimationPresets::getRandomPreset( sal_Int16 nPresetClass ) const
625 {
626 	Reference< XAnimationNode > xNode;
627 
628 	const PresetCategoryList* pCategoryList = 0;
629 	switch( nPresetClass )
630 	{
631 	case EffectPresetClass::ENTRANCE:	pCategoryList = &maEntrancePresets; break;
632 	case EffectPresetClass::EXIT:		pCategoryList = &maExitPresets; break;
633 	case EffectPresetClass::EMPHASIS:	pCategoryList = &maEmphasisPresets; break;
634 	case EffectPresetClass::MOTIONPATH:	pCategoryList = &maMotionPathsPresets; break;
635 	default:
636 		pCategoryList = 0;
637 	}
638 
639 	if( pCategoryList && pCategoryList->size() )
640 	{
641 		sal_Int32 nCategory = (rand() * pCategoryList->size() / RAND_MAX);
642 
643 		PresetCategoryPtr pCategory = (*pCategoryList)[nCategory];
644 		if( pCategory.get() && !pCategory->maEffects.empty() )
645 		{
646 			sal_Int32 nDescriptor = (rand() * pCategory->maEffects.size() / RAND_MAX);
647 			CustomAnimationPresetPtr pPreset = pCategory->maEffects[nDescriptor];
648 			if( pPreset.get() )
649 			{
650 				UStringList aSubTypes = pPreset->getSubTypes();
651 
652 				OUString aSubType;
653 				if( !aSubTypes.empty() )
654 				{
655 					sal_Int32 nSubType = (rand() * aSubTypes.size() / RAND_MAX);
656 					aSubType = aSubTypes[nSubType];
657 				}
658 				xNode = pPreset->create( aSubType );
659 			}
660 		}
661 	}
662 
663 	return xNode;
664 }
665 
666 
667 }
668 
669