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