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