Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sd / source / core / CustomAnimationPreset.cxx
blob242af4bd3a00a703aee6419e0d6e87b84e3e4919
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
45 #include <algorithm>
46 #include <vector>
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;
59 namespace sd {
61 static Reference< XNameAccess > getNodeAccess( const Reference< XMultiServiceFactory >& xConfigProvider, const OUString& rNodePath )
63 Reference< XNameAccess > xConfigAccess;
65 try
67 Sequence<Any> aArgs(comphelper::InitAnyPropertySequence(
69 {"nodepath", uno::Any(rNodePath)}
70 }));
72 xConfigAccess.set(
73 xConfigProvider->createInstanceWithArguments( "com.sun.star.configuration.ConfigurationAccess", aArgs ),
74 UNO_QUERY);
76 catch (const Exception&)
78 TOOLS_WARN_EXCEPTION( "sd", "sd::getNodeAccess()" );
81 return xConfigAccess;
84 void implImportLabels( const Reference< XMultiServiceFactory >& xConfigProvider, const OUString& rNodePath, UStringMap& rStringMap )
86 try
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() )
98 OUString aUIName;
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();
119 add( pEffect );
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; });
145 return aSubTypes;
148 Reference< XAnimationNode > CustomAnimationPreset::create( const OUString& rstrSubType )
152 OUString strSubType( rstrSubType );
153 if( strSubType.isEmpty() )
154 strSubType = maDefaultSubTyp;
156 CustomAnimationEffectPtr pEffect = maSubTypes[strSubType];
157 if( pEffect )
159 Reference< XCloneable > xCloneable( pEffect->getNode(), UNO_QUERY_THROW );
160 Reference< XAnimationNode > xNode( xCloneable->createClone(), UNO_QUERY_THROW );
161 return xNode;
164 catch (const Exception&)
166 TOOLS_WARN_EXCEPTION( "sd", "sd::CustomAnimationPresets::create()" );
169 Reference< XAnimationNode > xNode;
170 return xNode;
173 std::vector<OUString> CustomAnimationPreset::getProperties() const
175 std::vector<OUString> aPropertyList;
176 if (!maProperty.isEmpty())
178 sal_Int32 nPos = 0;
181 aPropertyList.push_back(maProperty.getToken(0, ';', nPos));
183 while (nPos >= 0);
185 return aPropertyList;
188 bool CustomAnimationPreset::hasProperty( std::u16string_view rProperty )const
190 if (maProperty.isEmpty())
191 return false;
193 sal_Int32 nPos = 0;
196 if (o3tl::getToken(maProperty, 0, ';', nPos) == rProperty)
197 return true;
199 while (nPos >= 0);
201 return false;
204 CustomAnimationPresets::CustomAnimationPresets()
208 CustomAnimationPresets::~CustomAnimationPresets()
212 Reference< XAnimationNode > implImportEffects( const Reference< XMultiServiceFactory >& xServiceFactory, const OUString& rPath )
214 Reference< XAnimationNode > xRootNode;
218 // create stream
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;
227 // get filter
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", "");
240 return xRootNode;
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"))}
259 }));
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 );
287 if( pDescriptor )
288 pDescriptor->add( pEffect );
289 else
291 pDescriptor = std::make_shared<CustomAnimationPreset>( pEffect );
292 pDescriptor->maLabel = getUINameForPresetId( pEffect->getPresetId() );
293 maEffectDescriptorMap[aPresetId] = pDescriptor;
296 ++aIter;
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 );
321 importEffects();
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 )
341 #ifdef DEBUG
342 OUString aMissedPresetIds;
343 #endif
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" ) )
359 OUString aLabel;
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 );
370 if( pEffect )
372 aEffectsList.push_back( pEffect );
374 #ifdef DEBUG
375 else
377 aMissedPresetIds += OUString(rEffectName);
378 aMissedPresetIds += "\n";
380 #endif
382 rPresetMap.push_back( std::make_shared<PresetCategory>( aLabel, std::move(aEffectsList) ) );
387 catch (const Exception&)
389 TOOLS_WARN_EXCEPTION( "sd", "sd::CustomAnimationPresets::importPresets()" );
392 #ifdef DEBUG
393 SAL_WARN_IF(!aMissedPresetIds.isEmpty(), "sd", "sd::CustomAnimationPresets::importPresets(), invalid preset id: "
394 << aMissedPresetIds);
395 #endif
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;
406 else
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;
430 else
432 return rId;
435 void CustomAnimationPresets::changePresetSubType( const CustomAnimationEffectPtr& pEffect, const OUString& rPresetSubType ) const
437 if( pEffect && pEffect->getPresetSubType() != rPresetSubType )
439 CustomAnimationPresetPtr pDescriptor( getEffectDescriptor( pEffect->getPresetId() ) );
441 if( pDescriptor )
443 Reference< XAnimationNode > xNewNode( pDescriptor->create( rPresetSubType ) );
444 if( xNewNode.is() )
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())
463 return it->second;
465 CustomAnimationPresets& rPresets = mPresetsMap[aLang];
466 rPresets.importResources();
467 return rPresets;
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;
481 default:
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];
494 if( pPreset )
496 std::vector<OUString> aSubTypes = pPreset->getSubTypes();
498 OUString aSubType;
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 );
509 return xNode;
514 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */