Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / sd / source / core / stlfamily.cxx
blob413699e3a533cccb4ebda9e1a9b57abaf40ce946
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 <com/sun/star/lang/DisposedException.hpp>
21 #include <com/sun/star/lang/IllegalAccessException.hpp>
22 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
23 #include <cppuhelper/supportsservice.hxx>
25 #include <vcl/svapp.hxx>
27 #include <svl/style.hxx>
29 #include <svx/unoprov.hxx>
31 #include <strings.hrc>
32 #include <stlfamily.hxx>
33 #include <stlsheet.hxx>
34 #include <sdresid.hxx>
35 #include <drawdoc.hxx>
36 #include <sdpage.hxx>
37 #include <glob.hxx>
39 #include <map>
40 #include <memory>
42 using namespace ::com::sun::star::uno;
43 using namespace ::com::sun::star::lang;
44 using namespace ::com::sun::star::container;
45 using namespace ::com::sun::star::style;
46 using namespace ::com::sun::star::beans;
48 typedef std::map< OUString, rtl::Reference< SdStyleSheet > > PresStyleMap;
50 struct SdStyleFamilyImpl
52 tools::WeakReference<SdPage> mxMasterPage;
53 OUString maLayoutName;
55 PresStyleMap& getStyleSheets();
56 rtl::Reference< SfxStyleSheetPool > mxPool;
58 private:
59 PresStyleMap maStyleSheets;
62 PresStyleMap& SdStyleFamilyImpl::getStyleSheets()
64 if( mxMasterPage.is() && (mxMasterPage->GetLayoutName() != maLayoutName) )
66 maLayoutName = mxMasterPage->GetLayoutName();
68 OUString aLayoutName( maLayoutName );
69 const sal_Int32 nLen = aLayoutName.indexOf(SD_LT_SEPARATOR ) + 4;
70 aLayoutName = aLayoutName.copy(0, nLen );
72 if( (maStyleSheets.empty()) || !(*maStyleSheets.begin()).second->GetName().startsWith( aLayoutName) )
74 maStyleSheets.clear();
76 // The iterator will return only style sheets of family master page
77 std::shared_ptr<SfxStyleSheetIterator> aSSSIterator = std::make_shared<SfxStyleSheetIterator>(mxPool.get(), SfxStyleFamily::Page);
78 for ( SfxStyleSheetBase* pStyle = aSSSIterator->First(); pStyle;
79 pStyle = aSSSIterator->Next() )
81 // we assume that we have only SdStyleSheets
82 SdStyleSheet* pSdStyle = static_cast< SdStyleSheet* >( pStyle );
83 if (pSdStyle->GetName().startsWith(aLayoutName))
85 maStyleSheets[ pSdStyle->GetApiName() ].set( pSdStyle );
91 return maStyleSheets;
94 SdStyleFamily::SdStyleFamily( const rtl::Reference< SfxStyleSheetPool >& xPool, SfxStyleFamily nFamily )
95 : mnFamily( nFamily )
96 , mxPool( xPool )
100 SdStyleFamily::SdStyleFamily( const rtl::Reference< SfxStyleSheetPool >& xPool, const SdPage* pMasterPage )
101 : mnFamily( SfxStyleFamily::Page )
102 , mxPool( xPool )
103 , mpImpl( new SdStyleFamilyImpl )
105 mpImpl->mxMasterPage.reset( const_cast< SdPage* >( pMasterPage ) );
106 mpImpl->mxPool = xPool;
109 SdStyleFamily::~SdStyleFamily()
111 DBG_ASSERT( !mxPool.is(), "SdStyleFamily::~SdStyleFamily(), dispose me first!" );
114 void SdStyleFamily::throwIfDisposed() const
116 if( !mxPool.is() )
117 throw DisposedException();
120 SdStyleSheet* SdStyleFamily::GetValidNewSheet( const Any& rElement )
122 Reference< XStyle > xStyle( rElement, UNO_QUERY );
123 SdStyleSheet* pStyle = static_cast< SdStyleSheet* >( xStyle.get() );
125 if( pStyle == nullptr || (pStyle->GetFamily() != mnFamily) || (pStyle->GetPool() != mxPool.get()) || (mxPool->Find( pStyle->GetName(), mnFamily) != nullptr) )
126 throw IllegalArgumentException();
128 return pStyle;
131 SdStyleSheet* SdStyleFamily::GetSheetByName( const OUString& rName )
133 SdStyleSheet* pRet = nullptr;
134 if( !rName.isEmpty() )
136 if( mnFamily == SfxStyleFamily::Page )
138 PresStyleMap& rStyleMap = mpImpl->getStyleSheets();
139 PresStyleMap::iterator iter( rStyleMap.find(rName) );
140 if( iter != rStyleMap.end() )
141 pRet = (*iter).second.get();
143 else
145 std::shared_ptr<SfxStyleSheetIterator> aSSSIterator = std::make_shared<SfxStyleSheetIterator>(mxPool.get(), mnFamily);
146 for ( SfxStyleSheetBase* pStyle = aSSSIterator->First(); pStyle;
147 pStyle = aSSSIterator->Next() )
149 // we assume that we have only SdStyleSheets
150 SdStyleSheet* pSdStyle = static_cast< SdStyleSheet* >( pStyle );
151 if( pSdStyle && pSdStyle->GetApiName() == rName)
153 pRet = pSdStyle;
154 break;
159 if( pRet )
160 return pRet;
162 throw NoSuchElementException();
165 // XServiceInfo
166 OUString SAL_CALL SdStyleFamily::getImplementationName()
168 return OUString( "SdStyleFamily" );
171 sal_Bool SAL_CALL SdStyleFamily::supportsService( const OUString& ServiceName )
173 return cppu::supportsService( this, ServiceName );
176 Sequence< OUString > SAL_CALL SdStyleFamily::getSupportedServiceNames()
178 OUString aServiceName( "com.sun.star.style.StyleFamily" );
179 Sequence< OUString > aSeq( &aServiceName, 1 );
180 return aSeq;
183 // XNamed
184 OUString SAL_CALL SdStyleFamily::getName()
186 if( mnFamily == SfxStyleFamily::Page )
188 SdPage* pPage = mpImpl->mxMasterPage.get();
189 if( pPage == nullptr )
190 throw DisposedException();
192 OUString aLayoutName( pPage->GetLayoutName() );
193 const OUString aSep( SD_LT_SEPARATOR );
194 sal_Int32 nIndex = aLayoutName.indexOf(aSep);
195 if( nIndex != -1 )
196 aLayoutName = aLayoutName.copy(0, nIndex);
198 return aLayoutName;
200 else
202 return SdStyleSheet::GetFamilyString( mnFamily );
206 void SAL_CALL SdStyleFamily::setName( const OUString& )
210 // XNameAccess
212 Any SAL_CALL SdStyleFamily::getByName( const OUString& rName )
214 SolarMutexGuard aGuard;
215 throwIfDisposed();
216 return Any( Reference< XStyle >( static_cast<SfxUnoStyleSheet*>(GetSheetByName( rName )) ) );
219 Sequence< OUString > SAL_CALL SdStyleFamily::getElementNames()
221 SolarMutexGuard aGuard;
223 throwIfDisposed();
225 if( mnFamily == SfxStyleFamily::Page )
227 PresStyleMap& rStyleMap = mpImpl->getStyleSheets();
228 Sequence< OUString > aNames( rStyleMap.size() );
230 PresStyleMap::iterator iter( rStyleMap.begin() );
231 OUString* pNames = aNames.getArray();
232 while( iter != rStyleMap.end() )
234 rtl::Reference< SdStyleSheet > xStyle( (*iter++).second );
235 if( xStyle.is() )
237 *pNames++ = xStyle->GetApiName();
241 return aNames;
243 else
245 std::vector< OUString > aNames;
246 std::shared_ptr<SfxStyleSheetIterator> aSSSIterator = std::make_shared<SfxStyleSheetIterator>(mxPool.get(), mnFamily);
247 for ( SfxStyleSheetBase* pStyle = aSSSIterator->First(); pStyle;
248 pStyle = aSSSIterator->Next() )
250 // we assume that we have only SdStyleSheets
251 SdStyleSheet* pSdStyle = static_cast< SdStyleSheet* >( pStyle );
252 if( pSdStyle )
254 aNames.push_back( pSdStyle->GetApiName() );
257 return Sequence< OUString >( &(*aNames.begin()), aNames.size() );
261 sal_Bool SAL_CALL SdStyleFamily::hasByName( const OUString& aName )
263 SolarMutexGuard aGuard;
264 throwIfDisposed();
266 if( !aName.isEmpty() )
268 if( mnFamily == SfxStyleFamily::Page )
270 PresStyleMap& rStyleSheets = mpImpl->getStyleSheets();
271 PresStyleMap::iterator iter( rStyleSheets.find(aName) );
272 return iter != rStyleSheets.end();
274 else
276 std::shared_ptr<SfxStyleSheetIterator> aSSSIterator = std::make_shared<SfxStyleSheetIterator>(mxPool.get(), mnFamily);
277 for ( SfxStyleSheetBase* pStyle = aSSSIterator->First(); pStyle;
278 pStyle = aSSSIterator->Next() )
280 // we assume that we have only SdStyleSheets
281 SdStyleSheet* pSdStyle = static_cast< SdStyleSheet* >( pStyle );
282 if( pSdStyle )
284 if (pSdStyle->GetApiName() == aName)
286 return true;
293 return false;
296 // XElementAccess
298 Type SAL_CALL SdStyleFamily::getElementType()
300 return cppu::UnoType<XStyle>::get();
303 sal_Bool SAL_CALL SdStyleFamily::hasElements()
305 SolarMutexGuard aGuard;
306 throwIfDisposed();
308 if( mnFamily == SfxStyleFamily::Page )
310 return true;
312 else
314 std::shared_ptr<SfxStyleSheetIterator> aSSSIterator = std::make_shared<SfxStyleSheetIterator>(mxPool.get(), mnFamily);
315 if (aSSSIterator->First())
317 return true;
321 return false;
324 // XIndexAccess
326 sal_Int32 SAL_CALL SdStyleFamily::getCount()
328 SolarMutexGuard aGuard;
329 throwIfDisposed();
331 sal_Int32 nCount = 0;
332 if( mnFamily == SfxStyleFamily::Page )
334 return mpImpl->getStyleSheets().size();
336 else
338 std::shared_ptr<SfxStyleSheetIterator> aSSSIterator = std::make_shared<SfxStyleSheetIterator>(mxPool.get(), mnFamily);
339 for ( SfxStyleSheetBase* pStyle = aSSSIterator->First(); pStyle;
340 pStyle = aSSSIterator->Next() )
342 nCount++;
346 return nCount;
349 Any SAL_CALL SdStyleFamily::getByIndex( sal_Int32 Index )
351 SolarMutexGuard aGuard;
352 throwIfDisposed();
354 if( Index >= 0 )
356 if( mnFamily == SfxStyleFamily::Page )
358 PresStyleMap& rStyleSheets = mpImpl->getStyleSheets();
359 if( !rStyleSheets.empty() )
361 PresStyleMap::iterator iter( rStyleSheets.begin() );
362 while( Index-- && (iter != rStyleSheets.end()) )
363 ++iter;
365 if( (Index==-1) && (iter != rStyleSheets.end()) )
366 return Any( Reference< XStyle >( (*iter).second.get() ) );
369 else
371 std::shared_ptr<SfxStyleSheetIterator> aSSSIterator = std::make_shared<SfxStyleSheetIterator>(mxPool.get(), mnFamily);
372 for ( SfxStyleSheetBase* pStyle = aSSSIterator->First(); pStyle;
373 pStyle = aSSSIterator->Next() )
375 // we assume that we have only SdStyleSheets
376 SdStyleSheet* pSdStyle = static_cast< SdStyleSheet* >( pStyle );
377 if( Index-- == 0 )
379 return Any( Reference< XStyle >( pSdStyle ) );
385 throw IndexOutOfBoundsException();
388 // XNameContainer
390 void SAL_CALL SdStyleFamily::insertByName( const OUString& rName, const Any& rElement )
392 SolarMutexGuard aGuard;
393 throwIfDisposed();
395 if(rName.isEmpty())
396 throw IllegalArgumentException();
398 SdStyleSheet* pStyle = GetValidNewSheet( rElement );
399 if( !pStyle->SetName( rName ) )
400 throw ElementExistException();
402 pStyle->SetApiName( rName );
403 mxPool->Insert( pStyle );
406 void SAL_CALL SdStyleFamily::removeByName( const OUString& rName )
408 SolarMutexGuard aGuard;
409 throwIfDisposed();
411 SdStyleSheet* pStyle = GetSheetByName( rName );
413 if( !pStyle->IsUserDefined() )
414 throw WrappedTargetException();
416 mxPool->Remove( pStyle );
419 // XNameReplace
421 void SAL_CALL SdStyleFamily::replaceByName( const OUString& rName, const Any& aElement )
423 SolarMutexGuard aGuard;
424 throwIfDisposed();
426 SdStyleSheet* pOldStyle = GetSheetByName( rName );
427 SdStyleSheet* pNewStyle = GetValidNewSheet( aElement );
429 mxPool->Remove( pOldStyle );
430 mxPool->Insert( pNewStyle );
433 // XSingleServiceFactory
435 Reference< XInterface > SAL_CALL SdStyleFamily::createInstance()
437 SolarMutexGuard aGuard;
438 throwIfDisposed();
440 if( mnFamily == SfxStyleFamily::Page )
442 throw IllegalAccessException();
444 return Reference< XInterface >( static_cast< XStyle* >( SdStyleSheet::CreateEmptyUserStyle( *mxPool.get(), mnFamily ) ) );
447 Reference< XInterface > SAL_CALL SdStyleFamily::createInstanceWithArguments( const Sequence< Any >& )
449 return createInstance();
452 // XComponent
454 void SAL_CALL SdStyleFamily::dispose( )
456 if( mxPool.is() )
457 mxPool.clear();
459 mpImpl.reset();
462 void SAL_CALL SdStyleFamily::addEventListener( const Reference< XEventListener >& )
466 void SAL_CALL SdStyleFamily::removeEventListener( const Reference< XEventListener >& )
470 // XPropertySet
472 Reference<XPropertySetInfo> SdStyleFamily::getPropertySetInfo()
474 OSL_FAIL( "###unexpected!" );
475 return Reference<XPropertySetInfo>();
478 void SdStyleFamily::setPropertyValue( const OUString& , const Any& )
480 OSL_FAIL( "###unexpected!" );
483 Any SdStyleFamily::getPropertyValue( const OUString& PropertyName )
485 if ( PropertyName != "DisplayName" )
487 throw UnknownPropertyException( "unknown property: " + PropertyName, static_cast<OWeakObject *>(this) );
490 SolarMutexGuard aGuard;
491 OUString sDisplayName;
492 switch( mnFamily )
494 case SfxStyleFamily::Page: sDisplayName = getName(); break;
495 case SfxStyleFamily::Frame: sDisplayName = SdResId(STR_CELL_STYLE_FAMILY); break;
496 default: sDisplayName = SdResId(STR_GRAPHICS_STYLE_FAMILY); break;
498 return Any( sDisplayName );
501 void SdStyleFamily::addPropertyChangeListener( const OUString& , const Reference<XPropertyChangeListener>& )
503 OSL_FAIL( "###unexpected!" );
506 void SdStyleFamily::removePropertyChangeListener( const OUString& , const Reference<XPropertyChangeListener>& )
508 OSL_FAIL( "###unexpected!" );
511 void SdStyleFamily::addVetoableChangeListener( const OUString& , const Reference<XVetoableChangeListener>& )
513 OSL_FAIL( "###unexpected!" );
516 void SdStyleFamily::removeVetoableChangeListener( const OUString& , const Reference<XVetoableChangeListener>& )
518 OSL_FAIL( "###unexpected!" );
521 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */