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 <boost/make_shared.hpp>
40 : #include <map>
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 788 : 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 4172 : PresStyleMap& SdStyleFamilyImpl::getStyleSheets()
63 : {
64 4172 : if( mxMasterPage.is() && (mxMasterPage->GetLayoutName() != maLayoutName) )
65 : {
66 154 : maLayoutName = mxMasterPage->GetLayoutName();
67 :
68 154 : OUString aLayoutName( maLayoutName );
69 154 : const sal_Int32 nLen = aLayoutName.indexOf(SD_LT_SEPARATOR ) + 4;
70 154 : aLayoutName = aLayoutName.copy(0, nLen );
71 :
72 154 : if( (maStyleSheets.empty()) || !(*maStyleSheets.begin()).second->GetName().startsWith( aLayoutName) )
73 : {
74 154 : maStyleSheets.clear();
75 :
76 : // The iterator will return only style sheets of family master page
77 154 : SfxStyleSheetIteratorPtr aSSSIterator = boost::make_shared<SfxStyleSheetIterator>(mxPool.get(), SD_STYLE_FAMILY_MASTERPAGE);
78 2450 : for ( SfxStyleSheetBase* pStyle = aSSSIterator->First(); pStyle;
79 2296 : pStyle = aSSSIterator->Next() )
80 : {
81 : // we assume that we have only SdStyleSheets
82 2296 : SdStyleSheet* pSdStyle = static_cast< SdStyleSheet* >( pStyle );
83 2296 : if (pSdStyle->GetName().startsWith(aLayoutName))
84 : {
85 2156 : maStyleSheets[ pSdStyle->GetApiName() ] = rtl::Reference< SdStyleSheet >( pSdStyle );
86 : }
87 154 : }
88 154 : }
89 : }
90 :
91 4172 : return maStyleSheets;
92 : }
93 :
94 884 : SdStyleFamily::SdStyleFamily( const rtl::Reference< SfxStyleSheetPool >& xPool, SfxStyleFamily nFamily )
95 : : mnFamily( nFamily )
96 : , mxPool( xPool )
97 884 : , mpImpl( 0 )
98 : {
99 884 : }
100 :
101 394 : SdStyleFamily::SdStyleFamily( const rtl::Reference< SfxStyleSheetPool >& xPool, const SdPage* pMasterPage )
102 : : mnFamily( SD_STYLE_FAMILY_MASTERPAGE )
103 : , mxPool( xPool )
104 394 : , mpImpl( new SdStyleFamilyImpl() )
105 : {
106 394 : mpImpl->mxMasterPage.reset( const_cast< SdPage* >( pMasterPage ) );
107 394 : mpImpl->mxPool = xPool;
108 394 : }
109 :
110 3834 : SdStyleFamily::~SdStyleFamily()
111 : {
112 : DBG_ASSERT( !mxPool.is(), "SdStyleFamily::~SdStyleFamily(), dispose me first!" );
113 1278 : delete mpImpl;
114 2556 : }
115 :
116 9480 : void SdStyleFamily::throwIfDisposed() const throw(RuntimeException)
117 : {
118 9480 : if( !mxPool.is() )
119 0 : throw DisposedException();
120 9480 : }
121 :
122 6 : SdStyleSheet* SdStyleFamily::GetValidNewSheet( const Any& rElement ) throw(IllegalArgumentException)
123 : {
124 6 : Reference< XStyle > xStyle( rElement, UNO_QUERY );
125 6 : SdStyleSheet* pStyle = static_cast< SdStyleSheet* >( xStyle.get() );
126 :
127 6 : if( pStyle == 0 || (pStyle->GetFamily() != mnFamily) || (&pStyle->GetPool() != mxPool.get()) || (mxPool->Find( pStyle->GetName(), mnFamily) != 0) )
128 0 : throw IllegalArgumentException();
129 :
130 6 : return pStyle;
131 : }
132 :
133 7340 : SdStyleSheet* SdStyleFamily::GetSheetByName( const OUString& rName ) throw(NoSuchElementException, WrappedTargetException )
134 : {
135 7340 : SdStyleSheet* pRet = 0;
136 7340 : if( !rName.isEmpty() )
137 : {
138 7340 : if( mnFamily == SD_STYLE_FAMILY_MASTERPAGE )
139 : {
140 2698 : PresStyleMap& rStyleMap = mpImpl->getStyleSheets();
141 2698 : PresStyleMap::iterator iter( rStyleMap.find(rName) );
142 2698 : if( iter != rStyleMap.end() )
143 2698 : pRet = (*iter).second.get();
144 : }
145 : else
146 : {
147 4642 : SfxStyleSheetIteratorPtr aSSSIterator = boost::make_shared<SfxStyleSheetIterator>(mxPool.get(), mnFamily);
148 28812 : for ( SfxStyleSheetBase* pStyle = aSSSIterator->First(); pStyle;
149 24170 : pStyle = aSSSIterator->Next() )
150 : {
151 : // we assume that we have only SdStyleSheets
152 28812 : SdStyleSheet* pSdStyle = static_cast< SdStyleSheet* >( pStyle );
153 28812 : if( pSdStyle && pSdStyle->GetApiName() == rName)
154 : {
155 4642 : pRet = pSdStyle;
156 4642 : break;
157 : }
158 4642 : }
159 : }
160 : }
161 7340 : if( pRet )
162 14680 : 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 11833 : OUString SAL_CALL SdStyleFamily::getName() throw (RuntimeException, std::exception)
187 : {
188 11833 : if( mnFamily == SD_STYLE_FAMILY_MASTERPAGE )
189 : {
190 2773 : SdPage* pPage = static_cast< SdPage* >( mpImpl->mxMasterPage.get() );
191 2773 : if( pPage == 0 )
192 0 : throw DisposedException();
193 :
194 2773 : OUString aLayoutName( pPage->GetLayoutName() );
195 5546 : const OUString aSep( SD_LT_SEPARATOR );
196 2773 : sal_Int32 nIndex = aLayoutName.indexOf(aSep);
197 2773 : if( nIndex != -1 )
198 2773 : aLayoutName = aLayoutName.copy(0, nIndex);
199 :
200 5546 : return OUString( aLayoutName );
201 : }
202 : else
203 : {
204 9060 : 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 7340 : Any SAL_CALL SdStyleFamily::getByName( const OUString& rName ) throw(NoSuchElementException, WrappedTargetException, RuntimeException, std::exception)
215 : {
216 7340 : SolarMutexGuard aGuard;
217 7340 : throwIfDisposed();
218 7340 : return Any( Reference< XStyle >( static_cast<SfxUnoStyleSheet*>(GetSheetByName( rName )) ) );
219 : }
220 :
221 52 : Sequence< OUString > SAL_CALL SdStyleFamily::getElementNames() throw(RuntimeException, std::exception)
222 : {
223 52 : SolarMutexGuard aGuard;
224 :
225 52 : throwIfDisposed();
226 :
227 52 : if( mnFamily == SD_STYLE_FAMILY_MASTERPAGE )
228 : {
229 6 : PresStyleMap& rStyleMap = mpImpl->getStyleSheets();
230 6 : Sequence< OUString > aNames( rStyleMap.size() );
231 :
232 6 : PresStyleMap::iterator iter( rStyleMap.begin() );
233 6 : OUString* pNames = aNames.getArray();
234 96 : while( iter != rStyleMap.end() )
235 : {
236 84 : rtl::Reference< SdStyleSheet > xStyle( (*iter++).second );
237 84 : if( xStyle.is() )
238 : {
239 84 : *pNames++ = xStyle->GetApiName();
240 : }
241 84 : }
242 :
243 6 : return aNames;
244 : }
245 : else
246 : {
247 46 : std::vector< OUString > aNames;
248 92 : SfxStyleSheetIteratorPtr aSSSIterator = boost::make_shared<SfxStyleSheetIterator>(mxPool.get(), mnFamily);
249 1178 : for ( SfxStyleSheetBase* pStyle = aSSSIterator->First(); pStyle;
250 1132 : pStyle = aSSSIterator->Next() )
251 : {
252 : // we assume that we have only SdStyleSheets
253 1132 : SdStyleSheet* pSdStyle = static_cast< SdStyleSheet* >( pStyle );
254 1132 : if( pSdStyle )
255 : {
256 1132 : aNames.push_back( pSdStyle->GetApiName() );
257 : }
258 : }
259 92 : return Sequence< OUString >( &(*aNames.begin()), aNames.size() );
260 52 : }
261 : }
262 :
263 2076 : sal_Bool SAL_CALL SdStyleFamily::hasByName( const OUString& aName ) throw(RuntimeException, std::exception)
264 : {
265 2076 : SolarMutexGuard aGuard;
266 2076 : throwIfDisposed();
267 :
268 2076 : if( !aName.isEmpty() )
269 : {
270 2076 : if( mnFamily == SD_STYLE_FAMILY_MASTERPAGE )
271 : {
272 1468 : PresStyleMap& rStyleSheets = mpImpl->getStyleSheets();
273 1468 : PresStyleMap::iterator iter( rStyleSheets.find(aName) );
274 1468 : return ( iter != rStyleSheets.end() ) ? sal_True : sal_False;
275 : }
276 : else
277 : {
278 608 : SfxStyleSheetIteratorPtr aSSSIterator = boost::make_shared<SfxStyleSheetIterator>(mxPool.get(), mnFamily);
279 5082 : for ( SfxStyleSheetBase* pStyle = aSSSIterator->First(); pStyle;
280 4474 : pStyle = aSSSIterator->Next() )
281 : {
282 : // we assume that we have only SdStyleSheets
283 5076 : SdStyleSheet* pSdStyle = static_cast< SdStyleSheet* >( pStyle );
284 5076 : if( pSdStyle )
285 : {
286 5076 : if (pSdStyle->GetApiName() == aName)
287 : {
288 602 : return sal_True;
289 : }
290 : }
291 6 : }
292 : }
293 : }
294 :
295 6 : 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 = boost::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 = boost::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 = boost::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 6 : void SAL_CALL SdStyleFamily::insertByName( const OUString& rName, const Any& rElement ) throw(IllegalArgumentException, ElementExistException, WrappedTargetException, RuntimeException, std::exception)
393 : {
394 6 : SolarMutexGuard aGuard;
395 6 : throwIfDisposed();
396 :
397 6 : if(rName.isEmpty())
398 0 : throw IllegalArgumentException();
399 :
400 6 : SdStyleSheet* pStyle = GetValidNewSheet( rElement );
401 6 : if( !pStyle->SetName( rName ) )
402 0 : throw ElementExistException();
403 :
404 6 : pStyle->SetApiName( rName );
405 6 : mxPool->Insert( pStyle );
406 6 : }
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 6 : Reference< XInterface > SAL_CALL SdStyleFamily::createInstance() throw(Exception, RuntimeException, std::exception)
438 : {
439 6 : SolarMutexGuard aGuard;
440 6 : throwIfDisposed();
441 :
442 6 : if( mnFamily == SD_STYLE_FAMILY_MASTERPAGE )
443 : {
444 0 : throw IllegalAccessException();
445 : }
446 : else
447 : {
448 12 : return Reference< XInterface >( static_cast< XStyle* >( SdStyleSheet::CreateEmptyUserStyle( *mxPool.get(), mnFamily ) ) );
449 6 : }
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 1278 : void SAL_CALL SdStyleFamily::dispose( ) throw (RuntimeException, std::exception)
460 : {
461 1278 : if( mxPool.is() )
462 1278 : mxPool.clear();
463 :
464 1278 : if( mpImpl )
465 : {
466 394 : delete mpImpl;
467 394 : mpImpl = 0;
468 : }
469 1278 : }
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 114 : }
531 :
532 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|