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_sd.hxx"
30 #include <com/sun/star/animations/XTimeContainer.hpp>
31 #include <com/sun/star/animations/XTransitionFilter.hpp>
32 #include <com/sun/star/container/XEnumerationAccess.hpp>
33 #include <com/sun/star/container/XNameAccess.hpp>
34 #include <com/sun/star/beans/NamedValue.hpp>
35 #include <com/sun/star/beans/XPropertySet.hpp>
36 #include <com/sun/star/util/XMacroExpander.hpp>
37 #include <com/sun/star/animations/AnimationNodeType.hpp>
38 #include <vcl/svapp.hxx>
39 #include <vos/mutex.hxx>
40 #include <tools/urlobj.hxx>
41 #include <unotools/streamwrap.hxx>
42 #include <comphelper/processfactory.hxx>
43 #include <unotools/pathoptions.hxx>
44 #include <tools/stream.hxx>
45 
46 #include <rtl/uri.hxx>
47 #include <tools/debug.hxx>
48 
49 #ifndef _SD_CUSTOMANIMATIONPRESET_HXX
50 #include <TransitionPreset.hxx>
51 #endif
52 #include <unotools/ucbstreamhelper.hxx>
53 
54 #include <algorithm>
55 
56 #include "sdpage.hxx"
57 
58 using namespace ::vos;
59 using namespace ::com::sun::star;
60 using namespace ::com::sun::star::animations;
61 
62 using ::rtl::OUString;
63 using ::com::sun::star::uno::UNO_QUERY;
64 using ::com::sun::star::uno::UNO_QUERY_THROW;
65 using ::com::sun::star::uno::Any;
66 using ::com::sun::star::uno::Sequence;
67 using ::com::sun::star::uno::Reference;
68 using ::com::sun::star::uno::Exception;
69 using ::com::sun::star::lang::XMultiServiceFactory;
70 using ::com::sun::star::container::XEnumerationAccess;
71 using ::com::sun::star::container::XEnumeration;
72 using ::com::sun::star::beans::NamedValue;
73 
74 namespace sd {
75 
76 extern Reference< XAnimationNode > implImportEffects( const Reference< XMultiServiceFactory >& xConfigProvider, const OUString& rPath );
77 extern void implImportLabels( const Reference< XMultiServiceFactory >& xConfigProvider, const OUString& rNodePath, UStringMap& rStringMap );
78 
79 TransitionPreset::TransitionPreset( const ::com::sun::star::uno::Reference< ::com::sun::star::animations::XAnimationNode >& xNode )
80 {
81 	// first locate preset id
82 	Sequence< NamedValue > aUserData( xNode->getUserData() );
83 	sal_Int32 nLength = aUserData.getLength();
84 	const NamedValue* p = aUserData.getConstArray();
85 	while( nLength-- )
86 	{
87 		if( p->Name.equalsAscii( "preset-id" ) )
88 		{
89 			p->Value >>= maPresetId;
90 			break;
91 		}
92 	}
93 
94 	// second, locate transition filter element
95 	Reference< XEnumerationAccess > xEnumerationAccess( xNode, UNO_QUERY_THROW );
96 	Reference< XEnumeration > xEnumeration( xEnumerationAccess->createEnumeration(), UNO_QUERY_THROW );
97 	Reference< XTransitionFilter > xTransition( xEnumeration->nextElement(), UNO_QUERY_THROW );
98 
99 	mnTransition = xTransition->getTransition();
100 	mnSubtype = xTransition->getSubtype();
101 	mbDirection = xTransition->getDirection();
102 	mnFadeColor = xTransition->getFadeColor();
103 }
104 
105 bool TransitionPreset::importTransitionsFile( TransitionPresetList& rList,
106                                               Reference< XMultiServiceFactory >& xServiceFactory,
107                                               UStringMap& rTransitionNameMape,
108                                               String aURL )
109 {
110     // import transition presets
111     Reference< XAnimationNode > xAnimationNode;
112 
113     try {
114         xAnimationNode = implImportEffects( xServiceFactory, aURL );
115         Reference< XEnumerationAccess > xEnumerationAccess( xAnimationNode, UNO_QUERY_THROW );
116 		Reference< XEnumeration > xEnumeration( xEnumerationAccess->createEnumeration(), UNO_QUERY_THROW );
117 
118 		while( xEnumeration->hasMoreElements() )
119         {
120             Reference< XAnimationNode > xChildNode( xEnumeration->nextElement(), UNO_QUERY_THROW );
121             if( xChildNode->getType() == AnimationNodeType::PAR )
122             {
123                 // create it
124                 TransitionPresetPtr pPreset( new TransitionPreset( xChildNode ) );
125 
126                 // name it
127                 OUString aPresetId( pPreset->getPresetId() );
128                 if( aPresetId.getLength() )
129                 {
130                     UStringMap::const_iterator aIter( rTransitionNameMape.find( aPresetId ) );
131                     if( aIter != rTransitionNameMape.end() )
132                         pPreset->maUIName = (*aIter).second;
133 
134                                 // add it
135                     rList.push_back( pPreset );
136                 }
137             }
138             else
139                 {
140                     DBG_ERROR( "sd::TransitionPreset::importTransitionPresetList(), missformed xml configuration file, giving up!" );
141                     break;
142                 }
143         }
144     } catch( Exception& ) {
145         return false;
146     }
147 
148     return true;
149 }
150 
151 #define EXPAND_PROTOCOL "vnd.sun.star.expand:"
152 
153 bool TransitionPreset::importTransitionPresetList( TransitionPresetList& rList )
154 {
155     bool bRet = false;
156 
157 	try
158 	{
159 		// Get service factory
160 		Reference< XMultiServiceFactory > xServiceFactory( comphelper::getProcessServiceFactory() );
161 		DBG_ASSERT( xServiceFactory.is(), "sd::CustomAnimationPresets::import(), got no service manager" );
162 		if( !xServiceFactory.is() )
163 			return false;
164 
165         uno::Reference< beans::XPropertySet > xProps( xServiceFactory, UNO_QUERY );
166         uno::Reference< uno::XComponentContext > xContext;
167         xProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DefaultContext" ))) >>= xContext;
168 
169         uno::Reference< util::XMacroExpander > xMacroExpander;
170         if( xContext.is() )
171             xMacroExpander.set( xContext->getValueByName(
172                                     rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/singletons/com.sun.star.util.theMacroExpander"))),
173                                 UNO_QUERY );
174 
175 		// import ui strings
176 		Reference< XMultiServiceFactory > xConfigProvider(
177 			xServiceFactory->createInstance(
178 				OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.configuration.ConfigurationProvider" ))),
179 			UNO_QUERY_THROW );
180 
181 		UStringMap aTransitionNameMape;
182 		const OUString aTransitionPath( RTL_CONSTASCII_USTRINGPARAM( "/org.openoffice.Office.UI.Effects/UserInterface/Transitions" ) );
183 		implImportLabels( xConfigProvider, aTransitionPath, aTransitionNameMape );
184 
185         // read path to transition effects files from config
186         Any propValue = uno::makeAny(
187             beans::PropertyValue(
188                 OUString( RTL_CONSTASCII_USTRINGPARAM("nodepath")), -1,
189                 uno::makeAny( OUString( RTL_CONSTASCII_USTRINGPARAM("/org.openoffice.Office.Impress/Misc"))),
190                 beans::PropertyState_DIRECT_VALUE ) );
191 
192         Reference<container::XNameAccess> xNameAccess(
193             xConfigProvider->createInstanceWithArguments(
194                 OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.configuration.ConfigurationAccess")),
195                 Sequence<Any>( &propValue, 1 ) ), UNO_QUERY_THROW );
196         uno::Sequence< rtl::OUString > aFiles;
197         xNameAccess->getByName(
198             OUString( RTL_CONSTASCII_USTRINGPARAM("TransitionFiles"))) >>= aFiles;
199 
200         for( sal_Int32 i=0; i<aFiles.getLength(); ++i )
201         {
202             rtl::OUString aURL = aFiles[i];
203             if( aURL.compareToAscii( RTL_CONSTASCII_STRINGPARAM( EXPAND_PROTOCOL )) == 0 )
204             {
205                 // cut protocol
206                 rtl::OUString aMacro( aURL.copy( sizeof ( EXPAND_PROTOCOL ) -1 ) );
207                 // decode uric class chars
208                 aMacro = rtl::Uri::decode( aMacro, rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8 );
209                 // expand macro string
210                 aURL = xMacroExpander->expandMacros( aMacro );
211             }
212 
213             bRet |= importTransitionsFile( rList,
214                                            xServiceFactory,
215                                            aTransitionNameMape,
216                                            aURL );
217         }
218 
219 		return bRet;
220 	}
221 	catch( Exception& e )
222 	{
223 		(void)e;
224 		DBG_ERROR( "sd::TransitionPreset::importResources(), Exception cought!" );
225 	}
226 
227 	return bRet;
228 }
229 
230 TransitionPresetList* TransitionPreset::mpTransitionPresetList = 0;
231 
232 const TransitionPresetList& TransitionPreset::getTransitionPresetList()
233 {
234 	if( !mpTransitionPresetList )
235 	{
236 		OGuard aGuard( Application::GetSolarMutex() );
237 		if( !mpTransitionPresetList )
238 		{
239 			mpTransitionPresetList = new sd::TransitionPresetList();
240 			sd::TransitionPreset::importTransitionPresetList( *mpTransitionPresetList );
241 		}
242 	}
243 
244 	return *mpTransitionPresetList;
245 }
246 
247 void TransitionPreset::apply( SdPage* pSlide ) const
248 {
249 	if( pSlide )
250 	{
251 		pSlide->setTransitionType( mnTransition );
252 		pSlide->setTransitionSubtype( mnSubtype );
253 		pSlide->setTransitionDirection( mbDirection );
254 		pSlide->setTransitionFadeColor( mnFadeColor );
255 	}
256 }
257 
258 }
259