1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_sd.hxx"
30*cdf0e10cSrcweir #include <com/sun/star/animations/XTimeContainer.hpp>
31*cdf0e10cSrcweir #include <com/sun/star/animations/XTransitionFilter.hpp>
32*cdf0e10cSrcweir #include <com/sun/star/container/XEnumerationAccess.hpp>
33*cdf0e10cSrcweir #include <com/sun/star/container/XNameAccess.hpp>
34*cdf0e10cSrcweir #include <com/sun/star/beans/NamedValue.hpp>
35*cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
36*cdf0e10cSrcweir #include <com/sun/star/util/XMacroExpander.hpp>
37*cdf0e10cSrcweir #include <com/sun/star/animations/AnimationNodeType.hpp>
38*cdf0e10cSrcweir #include <vcl/svapp.hxx>
39*cdf0e10cSrcweir #include <vos/mutex.hxx>
40*cdf0e10cSrcweir #include <tools/urlobj.hxx>
41*cdf0e10cSrcweir #include <unotools/streamwrap.hxx>
42*cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
43*cdf0e10cSrcweir #include <unotools/pathoptions.hxx>
44*cdf0e10cSrcweir #include <tools/stream.hxx>
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir #include <rtl/uri.hxx>
47*cdf0e10cSrcweir #include <tools/debug.hxx>
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir #ifndef _SD_CUSTOMANIMATIONPRESET_HXX
50*cdf0e10cSrcweir #include <TransitionPreset.hxx>
51*cdf0e10cSrcweir #endif
52*cdf0e10cSrcweir #include <unotools/ucbstreamhelper.hxx>
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir #include <algorithm>
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir #include "sdpage.hxx"
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir using namespace ::vos;
59*cdf0e10cSrcweir using namespace ::com::sun::star;
60*cdf0e10cSrcweir using namespace ::com::sun::star::animations;
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir using ::rtl::OUString;
63*cdf0e10cSrcweir using ::com::sun::star::uno::UNO_QUERY;
64*cdf0e10cSrcweir using ::com::sun::star::uno::UNO_QUERY_THROW;
65*cdf0e10cSrcweir using ::com::sun::star::uno::Any;
66*cdf0e10cSrcweir using ::com::sun::star::uno::Sequence;
67*cdf0e10cSrcweir using ::com::sun::star::uno::Reference;
68*cdf0e10cSrcweir using ::com::sun::star::uno::Exception;
69*cdf0e10cSrcweir using ::com::sun::star::lang::XMultiServiceFactory;
70*cdf0e10cSrcweir using ::com::sun::star::container::XEnumerationAccess;
71*cdf0e10cSrcweir using ::com::sun::star::container::XEnumeration;
72*cdf0e10cSrcweir using ::com::sun::star::beans::NamedValue;
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir namespace sd {
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir extern Reference< XAnimationNode > implImportEffects( const Reference< XMultiServiceFactory >& xConfigProvider, const OUString& rPath );
77*cdf0e10cSrcweir extern void implImportLabels( const Reference< XMultiServiceFactory >& xConfigProvider, const OUString& rNodePath, UStringMap& rStringMap );
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir TransitionPreset::TransitionPreset( const ::com::sun::star::uno::Reference< ::com::sun::star::animations::XAnimationNode >& xNode )
80*cdf0e10cSrcweir {
81*cdf0e10cSrcweir 	// first locate preset id
82*cdf0e10cSrcweir 	Sequence< NamedValue > aUserData( xNode->getUserData() );
83*cdf0e10cSrcweir 	sal_Int32 nLength = aUserData.getLength();
84*cdf0e10cSrcweir 	const NamedValue* p = aUserData.getConstArray();
85*cdf0e10cSrcweir 	while( nLength-- )
86*cdf0e10cSrcweir 	{
87*cdf0e10cSrcweir 		if( p->Name.equalsAscii( "preset-id" ) )
88*cdf0e10cSrcweir 		{
89*cdf0e10cSrcweir 			p->Value >>= maPresetId;
90*cdf0e10cSrcweir 			break;
91*cdf0e10cSrcweir 		}
92*cdf0e10cSrcweir 	}
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir 	// second, locate transition filter element
95*cdf0e10cSrcweir 	Reference< XEnumerationAccess > xEnumerationAccess( xNode, UNO_QUERY_THROW );
96*cdf0e10cSrcweir 	Reference< XEnumeration > xEnumeration( xEnumerationAccess->createEnumeration(), UNO_QUERY_THROW );
97*cdf0e10cSrcweir 	Reference< XTransitionFilter > xTransition( xEnumeration->nextElement(), UNO_QUERY_THROW );
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir 	mnTransition = xTransition->getTransition();
100*cdf0e10cSrcweir 	mnSubtype = xTransition->getSubtype();
101*cdf0e10cSrcweir 	mbDirection = xTransition->getDirection();
102*cdf0e10cSrcweir 	mnFadeColor = xTransition->getFadeColor();
103*cdf0e10cSrcweir }
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir bool TransitionPreset::importTransitionsFile( TransitionPresetList& rList,
106*cdf0e10cSrcweir                                               Reference< XMultiServiceFactory >& xServiceFactory,
107*cdf0e10cSrcweir                                               UStringMap& rTransitionNameMape,
108*cdf0e10cSrcweir                                               String aURL )
109*cdf0e10cSrcweir {
110*cdf0e10cSrcweir     // import transition presets
111*cdf0e10cSrcweir     Reference< XAnimationNode > xAnimationNode;
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir     try {
114*cdf0e10cSrcweir         xAnimationNode = implImportEffects( xServiceFactory, aURL );
115*cdf0e10cSrcweir         Reference< XEnumerationAccess > xEnumerationAccess( xAnimationNode, UNO_QUERY_THROW );
116*cdf0e10cSrcweir 		Reference< XEnumeration > xEnumeration( xEnumerationAccess->createEnumeration(), UNO_QUERY_THROW );
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir 		while( xEnumeration->hasMoreElements() )
119*cdf0e10cSrcweir         {
120*cdf0e10cSrcweir             Reference< XAnimationNode > xChildNode( xEnumeration->nextElement(), UNO_QUERY_THROW );
121*cdf0e10cSrcweir             if( xChildNode->getType() == AnimationNodeType::PAR )
122*cdf0e10cSrcweir             {
123*cdf0e10cSrcweir                 // create it
124*cdf0e10cSrcweir                 TransitionPresetPtr pPreset( new TransitionPreset( xChildNode ) );
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir                 // name it
127*cdf0e10cSrcweir                 OUString aPresetId( pPreset->getPresetId() );
128*cdf0e10cSrcweir                 if( aPresetId.getLength() )
129*cdf0e10cSrcweir                 {
130*cdf0e10cSrcweir                     UStringMap::const_iterator aIter( rTransitionNameMape.find( aPresetId ) );
131*cdf0e10cSrcweir                     if( aIter != rTransitionNameMape.end() )
132*cdf0e10cSrcweir                         pPreset->maUIName = (*aIter).second;
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir                                 // add it
135*cdf0e10cSrcweir                     rList.push_back( pPreset );
136*cdf0e10cSrcweir                 }
137*cdf0e10cSrcweir             }
138*cdf0e10cSrcweir             else
139*cdf0e10cSrcweir                 {
140*cdf0e10cSrcweir                     DBG_ERROR( "sd::TransitionPreset::importTransitionPresetList(), missformed xml configuration file, giving up!" );
141*cdf0e10cSrcweir                     break;
142*cdf0e10cSrcweir                 }
143*cdf0e10cSrcweir         }
144*cdf0e10cSrcweir     } catch( Exception& ) {
145*cdf0e10cSrcweir         return false;
146*cdf0e10cSrcweir     }
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir     return true;
149*cdf0e10cSrcweir }
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir #define EXPAND_PROTOCOL "vnd.sun.star.expand:"
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir bool TransitionPreset::importTransitionPresetList( TransitionPresetList& rList )
154*cdf0e10cSrcweir {
155*cdf0e10cSrcweir     bool bRet = false;
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir 	try
158*cdf0e10cSrcweir 	{
159*cdf0e10cSrcweir 		// Get service factory
160*cdf0e10cSrcweir 		Reference< XMultiServiceFactory > xServiceFactory( comphelper::getProcessServiceFactory() );
161*cdf0e10cSrcweir 		DBG_ASSERT( xServiceFactory.is(), "sd::CustomAnimationPresets::import(), got no service manager" );
162*cdf0e10cSrcweir 		if( !xServiceFactory.is() )
163*cdf0e10cSrcweir 			return false;
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir         uno::Reference< beans::XPropertySet > xProps( xServiceFactory, UNO_QUERY );
166*cdf0e10cSrcweir         uno::Reference< uno::XComponentContext > xContext;
167*cdf0e10cSrcweir         xProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DefaultContext" ))) >>= xContext;
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir         uno::Reference< util::XMacroExpander > xMacroExpander;
170*cdf0e10cSrcweir         if( xContext.is() )
171*cdf0e10cSrcweir             xMacroExpander.set( xContext->getValueByName(
172*cdf0e10cSrcweir                                     rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/singletons/com.sun.star.util.theMacroExpander"))),
173*cdf0e10cSrcweir                                 UNO_QUERY );
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir 		// import ui strings
176*cdf0e10cSrcweir 		Reference< XMultiServiceFactory > xConfigProvider(
177*cdf0e10cSrcweir 			xServiceFactory->createInstance(
178*cdf0e10cSrcweir 				OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.configuration.ConfigurationProvider" ))),
179*cdf0e10cSrcweir 			UNO_QUERY_THROW );
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir 		UStringMap aTransitionNameMape;
182*cdf0e10cSrcweir 		const OUString aTransitionPath( RTL_CONSTASCII_USTRINGPARAM( "/org.openoffice.Office.UI.Effects/UserInterface/Transitions" ) );
183*cdf0e10cSrcweir 		implImportLabels( xConfigProvider, aTransitionPath, aTransitionNameMape );
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir         // read path to transition effects files from config
186*cdf0e10cSrcweir         Any propValue = uno::makeAny(
187*cdf0e10cSrcweir             beans::PropertyValue(
188*cdf0e10cSrcweir                 OUString( RTL_CONSTASCII_USTRINGPARAM("nodepath")), -1,
189*cdf0e10cSrcweir                 uno::makeAny( OUString( RTL_CONSTASCII_USTRINGPARAM("/org.openoffice.Office.Impress/Misc"))),
190*cdf0e10cSrcweir                 beans::PropertyState_DIRECT_VALUE ) );
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir         Reference<container::XNameAccess> xNameAccess(
193*cdf0e10cSrcweir             xConfigProvider->createInstanceWithArguments(
194*cdf0e10cSrcweir                 OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.configuration.ConfigurationAccess")),
195*cdf0e10cSrcweir                 Sequence<Any>( &propValue, 1 ) ), UNO_QUERY_THROW );
196*cdf0e10cSrcweir         uno::Sequence< rtl::OUString > aFiles;
197*cdf0e10cSrcweir         xNameAccess->getByName(
198*cdf0e10cSrcweir             OUString( RTL_CONSTASCII_USTRINGPARAM("TransitionFiles"))) >>= aFiles;
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir         for( sal_Int32 i=0; i<aFiles.getLength(); ++i )
201*cdf0e10cSrcweir         {
202*cdf0e10cSrcweir             rtl::OUString aURL = aFiles[i];
203*cdf0e10cSrcweir             if( aURL.compareToAscii( RTL_CONSTASCII_STRINGPARAM( EXPAND_PROTOCOL )) == 0 )
204*cdf0e10cSrcweir             {
205*cdf0e10cSrcweir                 // cut protocol
206*cdf0e10cSrcweir                 rtl::OUString aMacro( aURL.copy( sizeof ( EXPAND_PROTOCOL ) -1 ) );
207*cdf0e10cSrcweir                 // decode uric class chars
208*cdf0e10cSrcweir                 aMacro = rtl::Uri::decode( aMacro, rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8 );
209*cdf0e10cSrcweir                 // expand macro string
210*cdf0e10cSrcweir                 aURL = xMacroExpander->expandMacros( aMacro );
211*cdf0e10cSrcweir             }
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir             bRet |= importTransitionsFile( rList,
214*cdf0e10cSrcweir                                            xServiceFactory,
215*cdf0e10cSrcweir                                            aTransitionNameMape,
216*cdf0e10cSrcweir                                            aURL );
217*cdf0e10cSrcweir         }
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir 		return bRet;
220*cdf0e10cSrcweir 	}
221*cdf0e10cSrcweir 	catch( Exception& e )
222*cdf0e10cSrcweir 	{
223*cdf0e10cSrcweir 		(void)e;
224*cdf0e10cSrcweir 		DBG_ERROR( "sd::TransitionPreset::importResources(), Exception cought!" );
225*cdf0e10cSrcweir 	}
226*cdf0e10cSrcweir 
227*cdf0e10cSrcweir 	return bRet;
228*cdf0e10cSrcweir }
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir TransitionPresetList* TransitionPreset::mpTransitionPresetList = 0;
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir const TransitionPresetList& TransitionPreset::getTransitionPresetList()
233*cdf0e10cSrcweir {
234*cdf0e10cSrcweir 	if( !mpTransitionPresetList )
235*cdf0e10cSrcweir 	{
236*cdf0e10cSrcweir 		OGuard aGuard( Application::GetSolarMutex() );
237*cdf0e10cSrcweir 		if( !mpTransitionPresetList )
238*cdf0e10cSrcweir 		{
239*cdf0e10cSrcweir 			mpTransitionPresetList = new sd::TransitionPresetList();
240*cdf0e10cSrcweir 			sd::TransitionPreset::importTransitionPresetList( *mpTransitionPresetList );
241*cdf0e10cSrcweir 		}
242*cdf0e10cSrcweir 	}
243*cdf0e10cSrcweir 
244*cdf0e10cSrcweir 	return *mpTransitionPresetList;
245*cdf0e10cSrcweir }
246*cdf0e10cSrcweir 
247*cdf0e10cSrcweir void TransitionPreset::apply( SdPage* pSlide ) const
248*cdf0e10cSrcweir {
249*cdf0e10cSrcweir 	if( pSlide )
250*cdf0e10cSrcweir 	{
251*cdf0e10cSrcweir 		pSlide->setTransitionType( mnTransition );
252*cdf0e10cSrcweir 		pSlide->setTransitionSubtype( mnSubtype );
253*cdf0e10cSrcweir 		pSlide->setTransitionDirection( mbDirection );
254*cdf0e10cSrcweir 		pSlide->setTransitionFadeColor( mnFadeColor );
255*cdf0e10cSrcweir 	}
256*cdf0e10cSrcweir }
257*cdf0e10cSrcweir 
258*cdf0e10cSrcweir }
259