Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <com/sun/star/lang/DisposedException.hpp>
21 : #include <com/sun/star/lang/IllegalAccessException.hpp>
22 : #include <cppuhelper/supportsservice.hxx>
23 :
24 : #include <osl/mutex.hxx>
25 : #include <vcl/svapp.hxx>
26 :
27 : #include <svl/style.hxx>
28 :
29 : #include <svx/unoprov.hxx>
30 :
31 : #include "../ui/inc/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"
38 :
39 : #include <map>
40 : #include <memory>
41 :
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;
47 :
48 : typedef std::map< OUString, rtl::Reference< SdStyleSheet > > PresStyleMap;
49 :
50 531 : struct SdStyleFamilyImpl
51 : {
52 : SdrPageWeakRef mxMasterPage;
53 : OUString maLayoutName;
54 :
55 : PresStyleMap& getStyleSheets();
56 : rtl::Reference< SfxStyleSheetPool > mxPool;
57 :
58 : private:
59 : PresStyleMap maStyleSheets;
60 : };
61 :
62 4668 : PresStyleMap& SdStyleFamilyImpl::getStyleSheets()
63 : {
64 4668 : if( mxMasterPage.is() && (mxMasterPage->GetLayoutName() != maLayoutName) )
65 : {
66 138 : maLayoutName = mxMasterPage->GetLayoutName();
67 :
68 138 : OUString aLayoutName( maLayoutName );
69 138 : const sal_Int32 nLen = aLayoutName.indexOf(SD_LT_SEPARATOR ) + 4;
70 138 : aLayoutName = aLayoutName.copy(0, nLen );
71 :
72 138 : if( (maStyleSheets.empty()) || !(*maStyleSheets.begin()).second->GetName().startsWith( aLayoutName) )
73 : {
74 138 : maStyleSheets.clear();
75 :
76 : // The iterator will return only style sheets of family master page
77 138 : SfxStyleSheetIteratorPtr aSSSIterator = std::make_shared<SfxStyleSheetIterator>(mxPool.get(), SD_STYLE_FAMILY_MASTERPAGE);
78 2154 : for ( SfxStyleSheetBase* pStyle = aSSSIterator->First(); pStyle;
79 2016 : pStyle = aSSSIterator->Next() )
80 : {
81 : // we assume that we have only SdStyleSheets
82 2016 : SdStyleSheet* pSdStyle = static_cast< SdStyleSheet* >( pStyle );
83 2016 : if (pSdStyle->GetName().startsWith(aLayoutName))
84 : {
85 1932 : maStyleSheets[ pSdStyle->GetApiName() ] = rtl::Reference< SdStyleSheet >( pSdStyle );
86 : }
87 138 : }
88 138 : }
89 : }
90 :
91 4668 : return maStyleSheets;
92 : }
93 :
94 640 : SdStyleFamily::SdStyleFamily( const rtl::Reference< SfxStyleSheetPool >& xPool, SfxStyleFamily nFamily )
95 : : mnFamily( nFamily )
96 : , mxPool( xPool )
97 640 : , mpImpl( 0 )
98 : {
99 640 : }
100 :
101 268 : SdStyleFamily::SdStyleFamily( const rtl::Reference< SfxStyleSheetPool >& xPool, const SdPage* pMasterPage )
102 : : mnFamily( SD_STYLE_FAMILY_MASTERPAGE )
103 : , mxPool( xPool )
104 268 : , mpImpl( new SdStyleFamilyImpl() )
105 : {
106 268 : mpImpl->mxMasterPage.reset( const_cast< SdPage* >( pMasterPage ) );
107 268 : mpImpl->mxPool = xPool;
108 268 : }
109 :
110 2679 : SdStyleFamily::~SdStyleFamily()
111 : {
112 : DBG_ASSERT( !mxPool.is(), "SdStyleFamily::~SdStyleFamily(), dispose me first!" );
113 893 : delete mpImpl;
114 1786 : }
115 :
116 8899 : void SdStyleFamily::throwIfDisposed() const throw(RuntimeException)
117 : {
118 8899 : if( !mxPool.is() )
119 0 : throw DisposedException();
120 8899 : }
121 :
122 3 : SdStyleSheet* SdStyleFamily::GetValidNewSheet( const Any& rElement ) throw(IllegalArgumentException)
123 : {
124 3 : Reference< XStyle > xStyle( rElement, UNO_QUERY );
125 3 : SdStyleSheet* pStyle = static_cast< SdStyleSheet* >( xStyle.get() );
126 :
127 3 : if( pStyle == 0 || (pStyle->GetFamily() != mnFamily) || (&pStyle->GetPool() != mxPool.get()) || (mxPool->Find( pStyle->GetName(), mnFamily) != 0) )
128 0 : throw IllegalArgumentException();
129 :
130 3 : return pStyle;
131 : }
132 :
133 6596 : SdStyleSheet* SdStyleFamily::GetSheetByName( const OUString& rName ) throw(NoSuchElementException, WrappedTargetException )
134 : {
135 6596 : SdStyleSheet* pRet = 0;
136 6596 : if( !rName.isEmpty() )
137 : {
138 6596 : if( mnFamily == SD_STYLE_FAMILY_MASTERPAGE )
139 : {
140 3134 : PresStyleMap& rStyleMap = mpImpl->getStyleSheets();
141 3134 : PresStyleMap::iterator iter( rStyleMap.find(rName) );
142 3134 : if( iter != rStyleMap.end() )
143 3134 : pRet = (*iter).second.get();
144 : }
145 : else
146 : {
147 3462 : SfxStyleSheetIteratorPtr aSSSIterator = std::make_shared<SfxStyleSheetIterator>(mxPool.get(), mnFamily);
148 24408 : for ( SfxStyleSheetBase* pStyle = aSSSIterator->First(); pStyle;
149 20946 : pStyle = aSSSIterator->Next() )
150 : {
151 : // we assume that we have only SdStyleSheets
152 24408 : SdStyleSheet* pSdStyle = static_cast< SdStyleSheet* >( pStyle );
153 24408 : if( pSdStyle && pSdStyle->GetApiName() == rName)
154 : {
155 3462 : pRet = pSdStyle;
156 3462 : break;
157 : }
158 3462 : }
159 : }
160 : }
161 6596 : if( pRet )
162 13192 : return pRet;
163 :
164 0 : throw NoSuchElementException();
165 : }
166 :
167 : // XServiceInfo
168 0 : OUString SAL_CALL SdStyleFamily::getImplementationName() throw(RuntimeException, std::exception)
169 : {
170 0 : return OUString( "SdStyleFamily" );
171 : }
172 :
173 0 : sal_Bool SAL_CALL SdStyleFamily::supportsService( const OUString& ServiceName ) throw(RuntimeException, std::exception)
174 : {
175 0 : return cppu::supportsService( this, ServiceName );
176 : }
177 :
178 0 : Sequence< OUString > SAL_CALL SdStyleFamily::getSupportedServiceNames() throw(RuntimeException, std::exception)
179 : {
180 0 : OUString aServiceName( "com.sun.star.style.StyleFamily" );
181 0 : Sequence< OUString > aSeq( &aServiceName, 1 );
182 0 : return aSeq;
183 : }
184 :
185 : // XNamed
186 10512 : OUString SAL_CALL SdStyleFamily::getName() throw (RuntimeException, std::exception)
187 : {
188 10512 : if( mnFamily == SD_STYLE_FAMILY_MASTERPAGE )
189 : {
190 2805 : SdPage* pPage = static_cast< SdPage* >( mpImpl->mxMasterPage.get() );
191 2805 : if( pPage == 0 )
192 0 : throw DisposedException();
193 :
194 2805 : OUString aLayoutName( pPage->GetLayoutName() );
195 5610 : const OUString aSep( SD_LT_SEPARATOR );
196 2805 : sal_Int32 nIndex = aLayoutName.indexOf(aSep);
197 2805 : if( nIndex != -1 )
198 2805 : aLayoutName = aLayoutName.copy(0, nIndex);
199 :
200 5610 : return OUString( aLayoutName );
201 : }
202 : else
203 : {
204 7707 : return SdStyleSheet::GetFamilyString( mnFamily );
205 : }
206 : }
207 :
208 0 : void SAL_CALL SdStyleFamily::setName( const OUString& ) throw (RuntimeException, std::exception)
209 : {
210 0 : }
211 :
212 : // XNameAccess
213 :
214 6596 : Any SAL_CALL SdStyleFamily::getByName( const OUString& rName ) throw(NoSuchElementException, WrappedTargetException, RuntimeException, std::exception)
215 : {
216 6596 : SolarMutexGuard aGuard;
217 6596 : throwIfDisposed();
218 6596 : return Any( Reference< XStyle >( static_cast<SfxUnoStyleSheet*>(GetSheetByName( rName )) ) );
219 : }
220 :
221 38 : Sequence< OUString > SAL_CALL SdStyleFamily::getElementNames() throw(RuntimeException, std::exception)
222 : {
223 38 : SolarMutexGuard aGuard;
224 :
225 38 : throwIfDisposed();
226 :
227 38 : if( mnFamily == SD_STYLE_FAMILY_MASTERPAGE )
228 : {
229 7 : PresStyleMap& rStyleMap = mpImpl->getStyleSheets();
230 7 : Sequence< OUString > aNames( rStyleMap.size() );
231 :
232 7 : PresStyleMap::iterator iter( rStyleMap.begin() );
233 7 : OUString* pNames = aNames.getArray();
234 112 : while( iter != rStyleMap.end() )
235 : {
236 98 : rtl::Reference< SdStyleSheet > xStyle( (*iter++).second );
237 98 : if( xStyle.is() )
238 : {
239 98 : *pNames++ = xStyle->GetApiName();
240 : }
241 98 : }
242 :
243 7 : return aNames;
244 : }
245 : else
246 : {
247 31 : std::vector< OUString > aNames;
248 62 : SfxStyleSheetIteratorPtr aSSSIterator = std::make_shared<SfxStyleSheetIterator>(mxPool.get(), mnFamily);
249 797 : for ( SfxStyleSheetBase* pStyle = aSSSIterator->First(); pStyle;
250 766 : pStyle = aSSSIterator->Next() )
251 : {
252 : // we assume that we have only SdStyleSheets
253 766 : SdStyleSheet* pSdStyle = static_cast< SdStyleSheet* >( pStyle );
254 766 : if( pSdStyle )
255 : {
256 766 : aNames.push_back( pSdStyle->GetApiName() );
257 : }
258 : }
259 62 : return Sequence< OUString >( &(*aNames.begin()), aNames.size() );
260 38 : }
261 : }
262 :
263 2259 : sal_Bool SAL_CALL SdStyleFamily::hasByName( const OUString& aName ) throw(RuntimeException, std::exception)
264 : {
265 2259 : SolarMutexGuard aGuard;
266 2259 : throwIfDisposed();
267 :
268 2259 : if( !aName.isEmpty() )
269 : {
270 2259 : if( mnFamily == SD_STYLE_FAMILY_MASTERPAGE )
271 : {
272 1527 : PresStyleMap& rStyleSheets = mpImpl->getStyleSheets();
273 1527 : PresStyleMap::iterator iter( rStyleSheets.find(aName) );
274 1527 : return ( iter != rStyleSheets.end() ) ? sal_True : sal_False;
275 : }
276 : else
277 : {
278 732 : SfxStyleSheetIteratorPtr aSSSIterator = std::make_shared<SfxStyleSheetIterator>(mxPool.get(), mnFamily);
279 5989 : for ( SfxStyleSheetBase* pStyle = aSSSIterator->First(); pStyle;
280 5257 : pStyle = aSSSIterator->Next() )
281 : {
282 : // we assume that we have only SdStyleSheets
283 5986 : SdStyleSheet* pSdStyle = static_cast< SdStyleSheet* >( pStyle );
284 5986 : if( pSdStyle )
285 : {
286 5986 : if (pSdStyle->GetApiName() == aName)
287 : {
288 729 : return sal_True;
289 : }
290 : }
291 3 : }
292 : }
293 : }
294 :
295 3 : return sal_False;
296 : }
297 :
298 : // XElementAccess
299 :
300 0 : Type SAL_CALL SdStyleFamily::getElementType() throw(RuntimeException, std::exception)
301 : {
302 0 : return cppu::UnoType<XStyle>::get();
303 : }
304 :
305 0 : sal_Bool SAL_CALL SdStyleFamily::hasElements() throw(RuntimeException, std::exception)
306 : {
307 0 : SolarMutexGuard aGuard;
308 0 : throwIfDisposed();
309 :
310 0 : if( mnFamily == SD_STYLE_FAMILY_MASTERPAGE )
311 : {
312 0 : return sal_True;
313 : }
314 : else
315 : {
316 0 : SfxStyleSheetIteratorPtr aSSSIterator = std::make_shared<SfxStyleSheetIterator>(mxPool.get(), mnFamily);
317 0 : if (aSSSIterator->First())
318 : {
319 0 : return sal_True;
320 0 : }
321 : }
322 :
323 0 : return sal_False;
324 : }
325 :
326 : // XIndexAccess
327 :
328 0 : sal_Int32 SAL_CALL SdStyleFamily::getCount() throw(RuntimeException, std::exception)
329 : {
330 0 : SolarMutexGuard aGuard;
331 0 : throwIfDisposed();
332 :
333 0 : sal_Int32 nCount = 0;
334 0 : if( mnFamily == SD_STYLE_FAMILY_MASTERPAGE )
335 : {
336 0 : return mpImpl->getStyleSheets().size();
337 : }
338 : else
339 : {
340 0 : SfxStyleSheetIteratorPtr aSSSIterator = std::make_shared<SfxStyleSheetIterator>(mxPool.get(), mnFamily);
341 0 : for ( SfxStyleSheetBase* pStyle = aSSSIterator->First(); pStyle;
342 0 : pStyle = aSSSIterator->Next() )
343 : {
344 0 : nCount++;
345 0 : }
346 : }
347 :
348 0 : return nCount;
349 : }
350 :
351 0 : Any SAL_CALL SdStyleFamily::getByIndex( sal_Int32 Index ) throw(IndexOutOfBoundsException, WrappedTargetException, RuntimeException, std::exception)
352 : {
353 0 : SolarMutexGuard aGuard;
354 0 : throwIfDisposed();
355 :
356 0 : if( Index >= 0 )
357 : {
358 0 : if( mnFamily == SD_STYLE_FAMILY_MASTERPAGE )
359 : {
360 0 : PresStyleMap& rStyleSheets = mpImpl->getStyleSheets();
361 0 : if( !rStyleSheets.empty() )
362 : {
363 0 : PresStyleMap::iterator iter( rStyleSheets.begin() );
364 0 : while( Index-- && (iter != rStyleSheets.end()) )
365 0 : ++iter;
366 :
367 0 : if( (Index==-1) && (iter != rStyleSheets.end()) )
368 0 : return Any( Reference< XStyle >( (*iter).second.get() ) );
369 : }
370 : }
371 : else
372 : {
373 0 : SfxStyleSheetIteratorPtr aSSSIterator = std::make_shared<SfxStyleSheetIterator>(mxPool.get(), mnFamily);
374 0 : for ( SfxStyleSheetBase* pStyle = aSSSIterator->First(); pStyle;
375 0 : pStyle = aSSSIterator->Next() )
376 : {
377 : // we assume that we have only SdStyleSheets
378 0 : SdStyleSheet* pSdStyle = static_cast< SdStyleSheet* >( pStyle );
379 0 : if( Index-- == 0 )
380 : {
381 0 : return Any( Reference< XStyle >( pSdStyle ) );
382 : }
383 0 : }
384 : }
385 : }
386 :
387 0 : throw IndexOutOfBoundsException();
388 : }
389 :
390 : // XNameContainer
391 :
392 3 : void SAL_CALL SdStyleFamily::insertByName( const OUString& rName, const Any& rElement ) throw(IllegalArgumentException, ElementExistException, WrappedTargetException, RuntimeException, std::exception)
393 : {
394 3 : SolarMutexGuard aGuard;
395 3 : throwIfDisposed();
396 :
397 3 : if(rName.isEmpty())
398 0 : throw IllegalArgumentException();
399 :
400 3 : SdStyleSheet* pStyle = GetValidNewSheet( rElement );
401 3 : if( !pStyle->SetName( rName ) )
402 0 : throw ElementExistException();
403 :
404 3 : pStyle->SetApiName( rName );
405 3 : mxPool->Insert( pStyle );
406 3 : }
407 :
408 0 : void SAL_CALL SdStyleFamily::removeByName( const OUString& rName ) throw(NoSuchElementException, WrappedTargetException, RuntimeException, std::exception)
409 : {
410 0 : SolarMutexGuard aGuard;
411 0 : throwIfDisposed();
412 :
413 0 : SdStyleSheet* pStyle = GetSheetByName( rName );
414 :
415 0 : if( !pStyle->IsUserDefined() )
416 0 : throw WrappedTargetException();
417 :
418 0 : mxPool->Remove( pStyle );
419 0 : }
420 :
421 : // XNameReplace
422 :
423 0 : void SAL_CALL SdStyleFamily::replaceByName( const OUString& rName, const Any& aElement ) throw(IllegalArgumentException, NoSuchElementException, WrappedTargetException, RuntimeException, std::exception)
424 : {
425 0 : SolarMutexGuard aGuard;
426 0 : throwIfDisposed();
427 :
428 0 : SdStyleSheet* pOldStyle = GetSheetByName( rName );
429 0 : SdStyleSheet* pNewStyle = GetValidNewSheet( aElement );
430 :
431 0 : mxPool->Remove( pOldStyle );
432 0 : mxPool->Insert( pNewStyle );
433 0 : }
434 :
435 : // XSingleServiceFactory
436 :
437 3 : Reference< XInterface > SAL_CALL SdStyleFamily::createInstance() throw(Exception, RuntimeException, std::exception)
438 : {
439 3 : SolarMutexGuard aGuard;
440 3 : throwIfDisposed();
441 :
442 3 : if( mnFamily == SD_STYLE_FAMILY_MASTERPAGE )
443 : {
444 0 : throw IllegalAccessException();
445 : }
446 : else
447 : {
448 6 : return Reference< XInterface >( static_cast< XStyle* >( SdStyleSheet::CreateEmptyUserStyle( *mxPool.get(), mnFamily ) ) );
449 3 : }
450 : }
451 :
452 0 : Reference< XInterface > SAL_CALL SdStyleFamily::createInstanceWithArguments( const Sequence< Any >& ) throw(Exception, RuntimeException, std::exception)
453 : {
454 0 : return createInstance();
455 : }
456 :
457 : // XComponent
458 :
459 893 : void SAL_CALL SdStyleFamily::dispose( ) throw (RuntimeException, std::exception)
460 : {
461 893 : if( mxPool.is() )
462 893 : mxPool.clear();
463 :
464 893 : if( mpImpl )
465 : {
466 263 : delete mpImpl;
467 263 : mpImpl = 0;
468 : }
469 893 : }
470 :
471 0 : void SAL_CALL SdStyleFamily::addEventListener( const Reference< XEventListener >& ) throw (RuntimeException, std::exception)
472 : {
473 0 : }
474 :
475 0 : void SAL_CALL SdStyleFamily::removeEventListener( const Reference< XEventListener >& ) throw (RuntimeException, std::exception)
476 : {
477 0 : }
478 :
479 : // XPropertySet
480 :
481 0 : Reference<XPropertySetInfo> SdStyleFamily::getPropertySetInfo() throw (RuntimeException, std::exception)
482 : {
483 : OSL_FAIL( "###unexpected!" );
484 0 : return Reference<XPropertySetInfo>();
485 : }
486 :
487 0 : void SdStyleFamily::setPropertyValue( const OUString& , const Any& ) throw (UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException, RuntimeException, std::exception)
488 : {
489 : OSL_FAIL( "###unexpected!" );
490 0 : }
491 :
492 0 : Any SdStyleFamily::getPropertyValue( const OUString& PropertyName ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException, std::exception)
493 : {
494 0 : if ( PropertyName == "DisplayName" )
495 : {
496 0 : SolarMutexGuard aGuard;
497 0 : OUString sDisplayName;
498 0 : switch( mnFamily )
499 : {
500 0 : case SD_STYLE_FAMILY_MASTERPAGE: sDisplayName = getName(); break;
501 0 : case SD_STYLE_FAMILY_CELL: sDisplayName = SD_RESSTR(STR_CELL_STYLE_FAMILY); break;
502 0 : default: sDisplayName = SD_RESSTR(STR_GRAPHICS_STYLE_FAMILY); break;
503 : }
504 0 : return Any( sDisplayName );
505 : }
506 : else
507 : {
508 0 : throw UnknownPropertyException( "unknown property: " + PropertyName, static_cast<OWeakObject *>(this) );
509 : }
510 : }
511 :
512 0 : void SdStyleFamily::addPropertyChangeListener( const OUString& , const Reference<XPropertyChangeListener>& ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException, std::exception)
513 : {
514 : OSL_FAIL( "###unexpected!" );
515 0 : }
516 :
517 0 : void SdStyleFamily::removePropertyChangeListener( const OUString& , const Reference<XPropertyChangeListener>& ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException, std::exception)
518 : {
519 : OSL_FAIL( "###unexpected!" );
520 0 : }
521 :
522 0 : void SdStyleFamily::addVetoableChangeListener( const OUString& , const Reference<XVetoableChangeListener>& ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException, std::exception)
523 : {
524 : OSL_FAIL( "###unexpected!" );
525 0 : }
526 :
527 0 : void SdStyleFamily::removeVetoableChangeListener( const OUString& , const Reference<XVetoableChangeListener>& ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException, std::exception)
528 : {
529 : OSL_FAIL( "###unexpected!" );
530 66 : }
531 :
532 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|