LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / sd / source / core / stlfamily.cxx
bloba396bd1701bafb91bfeabf4aec301a6e06e9bd08
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 <tools/debug.hxx>
30 #include <unotools/weakref.hxx>
32 #include <strings.hrc>
33 #include <stlfamily.hxx>
34 #include <stlsheet.hxx>
35 #include <sdresid.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 unotools::WeakReference<SdPage> mxMasterPage;
53 OUString maLayoutName;
55 PresStyleMap& getStyleSheets();
56 rtl::Reference< SfxStyleSheetPool > mxPool;
58 private:
59 PresStyleMap maStyleSheets;
62 PresStyleMap& SdStyleFamilyImpl::getStyleSheets()
64 auto pMasterPage = mxMasterPage.get();
65 if (!pMasterPage)
66 return maStyleSheets;
68 if (pMasterPage->GetLayoutName() != maLayoutName )
70 maLayoutName = pMasterPage->GetLayoutName();
72 OUString aLayoutName( maLayoutName );
73 const sal_Int32 nLen = aLayoutName.indexOf(SD_LT_SEPARATOR ) + 4;
74 aLayoutName = aLayoutName.copy(0, nLen );
76 if( (maStyleSheets.empty()) || !(*maStyleSheets.begin()).second->GetName().startsWith( aLayoutName) )
78 maStyleSheets.clear();
80 // The iterator will return only style sheets of family master page
81 std::shared_ptr<SfxStyleSheetIterator> aSSSIterator = std::make_shared<SfxStyleSheetIterator>(mxPool.get(), SfxStyleFamily::Page);
82 for ( SfxStyleSheetBase* pStyle = aSSSIterator->First(); pStyle;
83 pStyle = aSSSIterator->Next() )
85 // we assume that we have only SdStyleSheets
86 SdStyleSheet* pSdStyle = static_cast< SdStyleSheet* >( pStyle );
87 if (pSdStyle->GetName().startsWith(aLayoutName))
89 maStyleSheets[ pSdStyle->GetApiName() ].set( pSdStyle );
95 return maStyleSheets;
98 SdStyleFamily::SdStyleFamily( const rtl::Reference< SfxStyleSheetPool >& xPool, SfxStyleFamily nFamily )
99 : mnFamily( nFamily )
100 , mxPool( xPool )
104 SdStyleFamily::SdStyleFamily( const rtl::Reference< SfxStyleSheetPool >& xPool, const SdPage* pMasterPage )
105 : mnFamily( SfxStyleFamily::Page )
106 , mxPool( xPool )
107 , mpImpl( new SdStyleFamilyImpl )
109 mpImpl->mxMasterPage = const_cast< SdPage* >( pMasterPage );
110 mpImpl->mxPool = xPool;
113 SdStyleFamily::~SdStyleFamily()
115 DBG_ASSERT( !mxPool.is(), "SdStyleFamily::~SdStyleFamily(), dispose me first!" );
118 void SdStyleFamily::throwIfDisposed() const
120 if( !mxPool.is() )
121 throw DisposedException();
124 SdStyleSheet* SdStyleFamily::GetValidNewSheet( const Any& rElement )
126 Reference< XStyle > xStyle( rElement, UNO_QUERY );
127 SdStyleSheet* pStyle = static_cast< SdStyleSheet* >( xStyle.get() );
129 if( pStyle == nullptr || (pStyle->GetFamily() != mnFamily) || (pStyle->GetPool() != mxPool.get()) || (mxPool->Find( pStyle->GetName(), mnFamily) != nullptr) )
130 throw IllegalArgumentException();
132 return pStyle;
135 SdStyleSheet* SdStyleFamily::GetSheetByName( const OUString& rName )
137 SdStyleSheet* pRet = nullptr;
138 if( !rName.isEmpty() )
140 if( mnFamily == SfxStyleFamily::Page )
142 PresStyleMap& rStyleMap = mpImpl->getStyleSheets();
143 PresStyleMap::iterator iter( rStyleMap.find(rName) );
144 if( iter != rStyleMap.end() )
145 pRet = (*iter).second.get();
147 else
149 std::shared_ptr<SfxStyleSheetIterator> aSSSIterator = std::make_shared<SfxStyleSheetIterator>(mxPool.get(), mnFamily);
150 for ( SfxStyleSheetBase* pStyle = aSSSIterator->First(); pStyle;
151 pStyle = aSSSIterator->Next() )
153 // we assume that we have only SdStyleSheets
154 SdStyleSheet* pSdStyle = static_cast< SdStyleSheet* >( pStyle );
155 if (pSdStyle->GetApiName() == rName)
157 pRet = pSdStyle;
158 break;
163 if( pRet )
164 return pRet;
166 throw NoSuchElementException();
169 // XServiceInfo
170 OUString SAL_CALL SdStyleFamily::getImplementationName()
172 return "SdStyleFamily";
175 sal_Bool SAL_CALL SdStyleFamily::supportsService( const OUString& ServiceName )
177 return cppu::supportsService( this, ServiceName );
180 Sequence< OUString > SAL_CALL SdStyleFamily::getSupportedServiceNames()
182 return { "com.sun.star.style.StyleFamily" };
185 // XNamed
186 OUString SAL_CALL SdStyleFamily::getName()
188 if( mnFamily == SfxStyleFamily::Page )
190 rtl::Reference<SdPage> pPage = mpImpl->mxMasterPage.get();
191 if( pPage == nullptr )
192 throw DisposedException();
194 OUString aLayoutName( pPage->GetLayoutName() );
195 sal_Int32 nIndex = aLayoutName.indexOf(SD_LT_SEPARATOR);
196 if( nIndex != -1 )
197 aLayoutName = aLayoutName.copy(0, nIndex);
199 return aLayoutName;
201 else
203 return SdStyleSheet::GetFamilyString( mnFamily );
207 void SAL_CALL SdStyleFamily::setName( const OUString& )
211 // XNameAccess
213 Any SAL_CALL SdStyleFamily::getByName( const OUString& rName )
215 SolarMutexGuard aGuard;
216 throwIfDisposed();
217 return Any( Reference< XStyle >( static_cast<SfxUnoStyleSheet*>(GetSheetByName( rName )) ) );
220 Sequence< OUString > SAL_CALL SdStyleFamily::getElementNames()
222 SolarMutexGuard aGuard;
224 throwIfDisposed();
226 if( mnFamily == SfxStyleFamily::Page )
228 PresStyleMap& rStyleMap = mpImpl->getStyleSheets();
229 Sequence< OUString > aNames( rStyleMap.size() );
231 OUString* pNames = aNames.getArray();
232 for( const auto& rEntry : rStyleMap )
234 rtl::Reference< SdStyleSheet > xStyle( rEntry.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 aNames.push_back(pSdStyle->GetApiName());
254 return Sequence< OUString >( &(*aNames.begin()), aNames.size() );
258 sal_Bool SAL_CALL SdStyleFamily::hasByName( const OUString& aName )
260 SolarMutexGuard aGuard;
261 throwIfDisposed();
263 if( !aName.isEmpty() )
265 if( mnFamily == SfxStyleFamily::Page )
267 PresStyleMap& rStyleSheets = mpImpl->getStyleSheets();
268 PresStyleMap::iterator iter( rStyleSheets.find(aName) );
269 return iter != rStyleSheets.end();
271 else
273 std::shared_ptr<SfxStyleSheetIterator> aSSSIterator = std::make_shared<SfxStyleSheetIterator>(mxPool.get(), mnFamily);
274 for ( SfxStyleSheetBase* pStyle = aSSSIterator->First(); pStyle;
275 pStyle = aSSSIterator->Next() )
277 // we assume that we have only SdStyleSheets
278 SdStyleSheet* pSdStyle = static_cast< SdStyleSheet* >( pStyle );
279 if (pSdStyle->GetApiName() == aName)
281 return true;
287 return false;
290 // XElementAccess
292 Type SAL_CALL SdStyleFamily::getElementType()
294 return cppu::UnoType<XStyle>::get();
297 sal_Bool SAL_CALL SdStyleFamily::hasElements()
299 SolarMutexGuard aGuard;
300 throwIfDisposed();
302 if( mnFamily == SfxStyleFamily::Page )
304 return true;
306 else
308 std::shared_ptr<SfxStyleSheetIterator> aSSSIterator = std::make_shared<SfxStyleSheetIterator>(mxPool.get(), mnFamily);
309 if (aSSSIterator->First())
311 return true;
315 return false;
318 // XIndexAccess
320 sal_Int32 SAL_CALL SdStyleFamily::getCount()
322 SolarMutexGuard aGuard;
323 throwIfDisposed();
325 sal_Int32 nCount = 0;
326 if( mnFamily == SfxStyleFamily::Page )
328 return mpImpl->getStyleSheets().size();
330 else
332 std::shared_ptr<SfxStyleSheetIterator> aSSSIterator = std::make_shared<SfxStyleSheetIterator>(mxPool.get(), mnFamily);
333 for ( SfxStyleSheetBase* pStyle = aSSSIterator->First(); pStyle;
334 pStyle = aSSSIterator->Next() )
336 nCount++;
340 return nCount;
343 Any SAL_CALL SdStyleFamily::getByIndex( sal_Int32 Index )
345 SolarMutexGuard aGuard;
346 throwIfDisposed();
348 if( Index >= 0 )
350 if( mnFamily == SfxStyleFamily::Page )
352 PresStyleMap& rStyleSheets = mpImpl->getStyleSheets();
353 if( Index < static_cast<sal_Int32>(rStyleSheets.size()) )
355 PresStyleMap::iterator iter( rStyleSheets.begin() );
356 std::advance(iter, Index);
357 return Any( Reference< XStyle >( (*iter).second ) );
360 else
362 std::shared_ptr<SfxStyleSheetIterator> aSSSIterator = std::make_shared<SfxStyleSheetIterator>(mxPool.get(), mnFamily);
363 for ( SfxStyleSheetBase* pStyle = aSSSIterator->First(); pStyle;
364 pStyle = aSSSIterator->Next() )
366 // we assume that we have only SdStyleSheets
367 SdStyleSheet* pSdStyle = static_cast< SdStyleSheet* >( pStyle );
368 if( Index-- == 0 )
370 return Any( Reference< XStyle >( pSdStyle ) );
376 throw IndexOutOfBoundsException();
379 // XNameContainer
381 void SAL_CALL SdStyleFamily::insertByName( const OUString& rName, const Any& rElement )
383 SolarMutexGuard aGuard;
384 throwIfDisposed();
386 if(rName.isEmpty())
387 throw IllegalArgumentException();
389 SdStyleSheet* pStyle = GetValidNewSheet( rElement );
390 if( !pStyle->SetName( rName ) )
391 throw ElementExistException();
393 pStyle->SetApiName( rName );
394 mxPool->Insert( pStyle );
397 void SAL_CALL SdStyleFamily::removeByName( const OUString& rName )
399 SolarMutexGuard aGuard;
400 throwIfDisposed();
402 SdStyleSheet* pStyle = GetSheetByName( rName );
404 if( !pStyle->IsUserDefined() )
405 throw WrappedTargetException();
407 mxPool->Remove( pStyle );
410 // XNameReplace
412 void SAL_CALL SdStyleFamily::replaceByName( const OUString& rName, const Any& aElement )
414 SolarMutexGuard aGuard;
415 throwIfDisposed();
417 SdStyleSheet* pOldStyle = GetSheetByName( rName );
418 SdStyleSheet* pNewStyle = GetValidNewSheet( aElement );
420 mxPool->Remove( pOldStyle );
421 mxPool->Insert( pNewStyle );
424 // XSingleServiceFactory
426 Reference< XInterface > SAL_CALL SdStyleFamily::createInstance()
428 SolarMutexGuard aGuard;
429 throwIfDisposed();
431 if( mnFamily == SfxStyleFamily::Page )
433 throw IllegalAccessException();
435 return Reference<XInterface>(
436 static_cast<XStyle*>(SdStyleSheet::CreateEmptyUserStyle(*mxPool, mnFamily).get()));
439 Reference< XInterface > SAL_CALL SdStyleFamily::createInstanceWithArguments( const Sequence< Any >& )
441 return createInstance();
444 // XComponent
446 void SAL_CALL SdStyleFamily::dispose( )
448 if( mxPool.is() )
449 mxPool.clear();
451 mpImpl.reset();
454 void SAL_CALL SdStyleFamily::addEventListener( const Reference< XEventListener >& )
458 void SAL_CALL SdStyleFamily::removeEventListener( const Reference< XEventListener >& )
462 // XPropertySet
464 Reference<XPropertySetInfo> SdStyleFamily::getPropertySetInfo()
466 OSL_FAIL( "###unexpected!" );
467 return Reference<XPropertySetInfo>();
470 void SdStyleFamily::setPropertyValue( const OUString& , const Any& )
472 OSL_FAIL( "###unexpected!" );
475 Any SdStyleFamily::getPropertyValue( const OUString& PropertyName )
477 if ( PropertyName != "DisplayName" )
479 throw UnknownPropertyException( "unknown property: " + PropertyName, static_cast<OWeakObject *>(this) );
482 SolarMutexGuard aGuard;
483 OUString sDisplayName;
484 switch( mnFamily )
486 case SfxStyleFamily::Page: sDisplayName = getName(); break;
487 case SfxStyleFamily::Frame: sDisplayName = SdResId(STR_CELL_STYLE_FAMILY); break;
488 default: sDisplayName = SdResId(STR_GRAPHICS_STYLE_FAMILY); break;
490 return Any( sDisplayName );
493 void SdStyleFamily::addPropertyChangeListener( const OUString& , const Reference<XPropertyChangeListener>& )
495 OSL_FAIL( "###unexpected!" );
498 void SdStyleFamily::removePropertyChangeListener( const OUString& , const Reference<XPropertyChangeListener>& )
500 OSL_FAIL( "###unexpected!" );
503 void SdStyleFamily::addVetoableChangeListener( const OUString& , const Reference<XVetoableChangeListener>& )
505 OSL_FAIL( "###unexpected!" );
508 void SdStyleFamily::removeVetoableChangeListener( const OUString& , const Reference<XVetoableChangeListener>& )
510 OSL_FAIL( "###unexpected!" );
513 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */