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