1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
22 #include <com/sun/star/util/XCloneable.hpp>
23 #include <com/sun/star/animations/XAnimationNodeSupplier.hpp>
24 #include <com/sun/star/container/XNameAccess.hpp>
25 #include <com/sun/star/configuration/theDefaultProvider.hpp>
26 #include <com/sun/star/xml/sax/InputSource.hpp>
27 #include <com/sun/star/xml/sax/XFastParser.hpp>
28 #include <com/sun/star/presentation/EffectPresetClass.hpp>
29 #include <com/sun/star/beans/NamedValue.hpp>
30 #include <unotools/streamwrap.hxx>
31 #include <comphelper/getexpandeduri.hxx>
32 #include <comphelper/processfactory.hxx>
33 #include <comphelper/propertysequence.hxx>
34 #include <comphelper/random.hxx>
35 #include <comphelper/lok.hxx>
36 #include <unotools/syslocaleoptions.hxx>
37 #include <tools/stream.hxx>
38 #include <comphelper/diagnose_ex.hxx>
39 #include <o3tl/string_view.hxx>
41 #include <vcl/svapp.hxx>
42 #include <unotools/ucbstreamhelper.hxx>
43 #include <CustomAnimationPreset.hxx>
48 using namespace ::com::sun::star
;
49 using namespace ::com::sun::star::uno
;
50 using namespace ::com::sun::star::animations
;
51 using namespace ::com::sun::star::presentation
;
53 using ::com::sun::star::io::XInputStream
;
54 using ::com::sun::star::lang::XMultiServiceFactory
;
55 using ::com::sun::star::container::XNameAccess
;
56 using ::com::sun::star::util::XCloneable
;
57 using ::com::sun::star::beans::NamedValue
;
61 static Reference
< XNameAccess
> getNodeAccess( const Reference
< XMultiServiceFactory
>& xConfigProvider
, const OUString
& rNodePath
)
63 Reference
< XNameAccess
> xConfigAccess
;
67 Sequence
<Any
> aArgs(comphelper::InitAnyPropertySequence(
69 {"nodepath", uno::Any(rNodePath
)}
73 xConfigProvider
->createInstanceWithArguments( "com.sun.star.configuration.ConfigurationAccess", aArgs
),
76 catch (const Exception
&)
78 TOOLS_WARN_EXCEPTION( "sd", "sd::getNodeAccess()" );
84 void implImportLabels( const Reference
< XMultiServiceFactory
>& xConfigProvider
, const OUString
& rNodePath
, UStringMap
& rStringMap
)
88 Reference
< XNameAccess
> xConfigAccess( getNodeAccess( xConfigProvider
, rNodePath
) );
89 if( xConfigAccess
.is() )
91 Reference
< XNameAccess
> xNameAccess
;
92 const Sequence
< OUString
> aNames( xConfigAccess
->getElementNames() );
93 for(const OUString
& rName
: aNames
)
95 xConfigAccess
->getByName( rName
) >>= xNameAccess
;
96 if( xNameAccess
.is() )
99 xNameAccess
->getByName( "Label" ) >>= aUIName
;
100 if( !aUIName
.isEmpty() )
102 rStringMap
[ rName
] = aUIName
;
108 catch (const Exception
&)
110 TOOLS_WARN_EXCEPTION( "sd", "sd::implImportLabels()" );
114 CustomAnimationPreset::CustomAnimationPreset( const CustomAnimationEffectPtr
& pEffect
)
116 maPresetId
= pEffect
->getPresetId();
117 maProperty
= pEffect
->getProperty();
121 mfDuration
= pEffect
->getDuration();
122 maDefaultSubTyp
= pEffect
->getPresetSubType();
124 const Sequence
< NamedValue
> aUserData( pEffect
->getNode()->getUserData() );
126 mbIsTextOnly
= std::any_of(aUserData
.begin(), aUserData
.end(),
127 [](const NamedValue
& rProp
) { return rProp
.Name
== "text-only"; });
130 void CustomAnimationPreset::add( const CustomAnimationEffectPtr
& pEffect
)
132 maSubTypes
[ pEffect
->getPresetSubType() ] = pEffect
;
135 std::vector
<OUString
> CustomAnimationPreset::getSubTypes()
137 std::vector
<OUString
> aSubTypes
;
139 if( maSubTypes
.size() > 1 )
141 std::transform(maSubTypes
.begin(), maSubTypes
.end(), std::back_inserter(aSubTypes
),
142 [](EffectsSubTypeMap::value_type
& rEntry
) -> OUString
{ return rEntry
.first
; });
148 Reference
< XAnimationNode
> CustomAnimationPreset::create( const OUString
& rstrSubType
)
152 OUString
strSubType( rstrSubType
);
153 if( strSubType
.isEmpty() )
154 strSubType
= maDefaultSubTyp
;
156 CustomAnimationEffectPtr pEffect
= maSubTypes
[strSubType
];
159 Reference
< XCloneable
> xCloneable( pEffect
->getNode(), UNO_QUERY_THROW
);
160 Reference
< XAnimationNode
> xNode( xCloneable
->createClone(), UNO_QUERY_THROW
);
164 catch (const Exception
&)
166 TOOLS_WARN_EXCEPTION( "sd", "sd::CustomAnimationPresets::create()" );
169 Reference
< XAnimationNode
> xNode
;
173 std::vector
<OUString
> CustomAnimationPreset::getProperties() const
175 std::vector
<OUString
> aPropertyList
;
176 if (!maProperty
.isEmpty())
181 aPropertyList
.push_back(maProperty
.getToken(0, ';', nPos
));
185 return aPropertyList
;
188 bool CustomAnimationPreset::hasProperty( std::u16string_view rProperty
)const
190 if (maProperty
.isEmpty())
196 if (o3tl::getToken(maProperty
, 0, ';', nPos
) == rProperty
)
204 CustomAnimationPresets::CustomAnimationPresets()
208 CustomAnimationPresets::~CustomAnimationPresets()
212 Reference
< XAnimationNode
> implImportEffects( const Reference
< XMultiServiceFactory
>& xServiceFactory
, const OUString
& rPath
)
214 Reference
< XAnimationNode
> xRootNode
;
219 std::unique_ptr
<SvStream
> pIStm
= ::utl::UcbStreamHelper::CreateStream( rPath
, StreamMode::READ
);
220 Reference
<XInputStream
> xInputStream( new utl::OInputStreamWrapper( std::move(pIStm
) ) );
222 // prepare ParserInputSource
223 xml::sax::InputSource aParserInput
;
224 aParserInput
.sSystemId
= rPath
;
225 aParserInput
.aInputStream
= xInputStream
;
228 Reference
< xml::sax::XFastParser
> xFilter( xServiceFactory
->createInstance("com.sun.star.comp.Xmloff.AnimationsImport" ), UNO_QUERY_THROW
);
230 xFilter
->parseStream( aParserInput
);
232 Reference
< XAnimationNodeSupplier
> xAnimationNodeSupplier( xFilter
, UNO_QUERY_THROW
);
233 xRootNode
= xAnimationNodeSupplier
->getAnimationNode();
235 catch (const Exception
&)
237 TOOLS_WARN_EXCEPTION("sd", "");
243 void CustomAnimationPresets::importEffects()
247 uno::Reference
< uno::XComponentContext
> xContext(
248 comphelper::getProcessComponentContext() );
249 Reference
< XMultiServiceFactory
> xServiceFactory(
250 xContext
->getServiceManager(), UNO_QUERY_THROW
);
252 Reference
< XMultiServiceFactory
> xConfigProvider
=
253 configuration::theDefaultProvider::get( xContext
);
255 // read path to transition effects files from config
256 uno::Sequence
<uno::Any
> aArgs(comphelper::InitAnyPropertySequence(
258 {"nodepath", uno::Any(OUString("/org.openoffice.Office.Impress/Misc"))}
260 Reference
<container::XNameAccess
> xNameAccess(
261 xConfigProvider
->createInstanceWithArguments(
262 "com.sun.star.configuration.ConfigurationAccess",
263 aArgs
), UNO_QUERY_THROW
);
264 uno::Sequence
< OUString
> aFiles
;
265 xNameAccess
->getByName( "EffectFiles" ) >>= aFiles
;
267 for( const auto& rFile
: std::as_const(aFiles
) )
269 OUString aURL
= comphelper::getExpandedUri(xContext
, rFile
);
271 mxRootNode
= implImportEffects( xServiceFactory
, aURL
);
273 if( mxRootNode
.is() )
275 Reference
< XTimeContainer
> xRootContainer( mxRootNode
, UNO_QUERY_THROW
);
276 EffectSequenceHelper
aSequence( xRootContainer
);
278 EffectSequence::iterator
aIter( aSequence
.getBegin() );
279 const EffectSequence::iterator
aEnd( aSequence
.getEnd() );
281 while( aIter
!= aEnd
)
283 CustomAnimationEffectPtr pEffect
= *aIter
;
285 const OUString
aPresetId( pEffect
->getPresetId() );
286 CustomAnimationPresetPtr pDescriptor
= getEffectDescriptor( aPresetId
);
288 pDescriptor
->add( pEffect
);
291 pDescriptor
= std::make_shared
<CustomAnimationPreset
>( pEffect
);
292 pDescriptor
->maLabel
= getUINameForPresetId( pEffect
->getPresetId() );
293 maEffectDescriptorMap
[aPresetId
] = pDescriptor
;
301 catch (const Exception
&)
303 TOOLS_WARN_EXCEPTION( "sd", "sd::CustomAnimationPresets::importEffects()" );
307 void CustomAnimationPresets::importResources()
311 // Get service factory
312 Reference
< XComponentContext
> xContext( comphelper::getProcessComponentContext() );
314 Reference
< XMultiServiceFactory
> xConfigProvider
=
315 configuration::theDefaultProvider::get( xContext
);
317 implImportLabels( xConfigProvider
, "/org.openoffice.Office.UI.Effects/UserInterface/Properties", maPropertyNameMap
);
319 implImportLabels( xConfigProvider
, "/org.openoffice.Office.UI.Effects/UserInterface/Effects", maEffectNameMap
);
323 importPresets( xConfigProvider
, "/org.openoffice.Office.UI.Effects/Presets/Entrance", maEntrancePresets
);
325 importPresets( xConfigProvider
, "/org.openoffice.Office.UI.Effects/Presets/Emphasis", maEmphasisPresets
);
327 importPresets( xConfigProvider
, "/org.openoffice.Office.UI.Effects/Presets/Exit", maExitPresets
);
329 importPresets( xConfigProvider
, "/org.openoffice.Office.UI.Effects/Presets/MotionPaths", maMotionPathsPresets
);
331 importPresets( xConfigProvider
, "/org.openoffice.Office.UI.Effects/Presets/Misc", maMiscPresets
);
333 catch (const Exception
&)
335 TOOLS_WARN_EXCEPTION( "sd", "sd::CustomAnimationPresets::importResources()" );
339 void CustomAnimationPresets::importPresets( const Reference
< XMultiServiceFactory
>& xConfigProvider
, const OUString
& rNodePath
, PresetCategoryList
& rPresetMap
)
342 OUString aMissedPresetIds
;
347 Reference
< XNameAccess
> xTypeAccess( getNodeAccess( xConfigProvider
, rNodePath
) );
348 if( xTypeAccess
.is() )
350 Reference
< XNameAccess
> xCategoryAccess
;
352 const Sequence
< OUString
> aNames( xTypeAccess
->getElementNames() );
353 for(const OUString
& rName
: aNames
)
355 xTypeAccess
->getByName( rName
) >>= xCategoryAccess
;
357 if( xCategoryAccess
.is() && xCategoryAccess
->hasByName( "Label" ) && xCategoryAccess
->hasByName( "Effects" ) )
360 xCategoryAccess
->getByName( "Label" ) >>= aLabel
;
362 Sequence
< OUString
> aEffects
;
363 xCategoryAccess
->getByName( "Effects" ) >>= aEffects
;
365 EffectDescriptorList aEffectsList
;
367 for( const OUString
& rEffectName
: std::as_const(aEffects
) )
369 CustomAnimationPresetPtr pEffect
= getEffectDescriptor( rEffectName
);
372 aEffectsList
.push_back( pEffect
);
377 aMissedPresetIds
+= OUString(rEffectName
);
378 aMissedPresetIds
+= "\n";
382 rPresetMap
.push_back( std::make_shared
<PresetCategory
>( aLabel
, std::move(aEffectsList
) ) );
387 catch (const Exception
&)
389 TOOLS_WARN_EXCEPTION( "sd", "sd::CustomAnimationPresets::importPresets()" );
393 SAL_WARN_IF(!aMissedPresetIds
.isEmpty(), "sd", "sd::CustomAnimationPresets::importPresets(), invalid preset id: "
394 << aMissedPresetIds
);
398 CustomAnimationPresetPtr
CustomAnimationPresets::getEffectDescriptor( const OUString
& rPresetId
) const
400 EffectDescriptorMap::const_iterator
aIter( maEffectDescriptorMap
.find( rPresetId
) );
402 if( aIter
!= maEffectDescriptorMap
.end() )
404 return (*aIter
).second
;
408 return CustomAnimationPresetPtr(nullptr);
412 const OUString
& CustomAnimationPresets::getUINameForPresetId( const OUString
& rPresetId
) const
414 return translateName( rPresetId
, maEffectNameMap
);
417 const OUString
& CustomAnimationPresets::getUINameForProperty( const OUString
& rPresetId
) const
419 return translateName( rPresetId
, maPropertyNameMap
);
422 const OUString
& CustomAnimationPresets::translateName( const OUString
& rId
, const UStringMap
& rNameMap
)
424 UStringMap::const_iterator
aIter( rNameMap
.find( rId
) );
426 if( aIter
!= rNameMap
.end() )
428 return (*aIter
).second
;
435 void CustomAnimationPresets::changePresetSubType( const CustomAnimationEffectPtr
& pEffect
, const OUString
& rPresetSubType
) const
437 if( pEffect
&& pEffect
->getPresetSubType() != rPresetSubType
)
439 CustomAnimationPresetPtr
pDescriptor( getEffectDescriptor( pEffect
->getPresetId() ) );
443 Reference
< XAnimationNode
> xNewNode( pDescriptor
->create( rPresetSubType
) );
445 pEffect
->replaceNode( xNewNode
);
450 std::map
<OUString
, CustomAnimationPresets
> CustomAnimationPresets::mPresetsMap
;
452 const CustomAnimationPresets
& CustomAnimationPresets::getCustomAnimationPresets()
454 // Support localization per-view. Currently not useful for Desktop
455 // but very much critical for LOK. The cache now is per-language.
456 const OUString aLang
= comphelper::LibreOfficeKit::isActive()
457 ? comphelper::LibreOfficeKit::getLanguageTag().getBcp47()
458 : SvtSysLocaleOptions().GetLanguageTag().getBcp47();
460 SolarMutexGuard aGuard
;
461 const auto it
= mPresetsMap
.find(aLang
);
462 if (it
!= mPresetsMap
.end())
465 CustomAnimationPresets
& rPresets
= mPresetsMap
[aLang
];
466 rPresets
.importResources();
470 Reference
< XAnimationNode
> CustomAnimationPresets::getRandomPreset( sal_Int16 nPresetClass
) const
472 Reference
< XAnimationNode
> xNode
;
474 const PresetCategoryList
* pCategoryList
= nullptr;
475 switch( nPresetClass
)
477 case EffectPresetClass::ENTRANCE
: pCategoryList
= &maEntrancePresets
; break;
478 case EffectPresetClass::EXIT
: pCategoryList
= &maExitPresets
; break;
479 case EffectPresetClass::EMPHASIS
: pCategoryList
= &maEmphasisPresets
; break;
480 case EffectPresetClass::MOTIONPATH
: pCategoryList
= &maMotionPathsPresets
; break;
482 pCategoryList
= nullptr;
485 if( pCategoryList
&& !pCategoryList
->empty() )
487 sal_Int32 nCategory
= comphelper::rng::uniform_size_distribution(0, pCategoryList
->size()-1);
489 PresetCategoryPtr pCategory
= (*pCategoryList
)[nCategory
];
490 if( pCategory
&& !pCategory
->maEffects
.empty() )
492 sal_Int32 nDescriptor
= comphelper::rng::uniform_size_distribution(0, pCategory
->maEffects
.size()-1);
493 CustomAnimationPresetPtr pPreset
= pCategory
->maEffects
[nDescriptor
];
496 std::vector
<OUString
> aSubTypes
= pPreset
->getSubTypes();
499 if( !aSubTypes
.empty() )
501 size_t nSubType
= comphelper::rng::uniform_size_distribution(0, aSubTypes
.size()-1);
502 aSubType
= aSubTypes
[nSubType
];
504 xNode
= pPreset
->create( aSubType
);
514 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */