xref: /trunk/main/sd/source/core/stlfamily.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sd.hxx"
30 
31 #include <com/sun/star/lang/DisposedException.hpp>
32 #include <com/sun/star/lang/IllegalAccessException.hpp>
33 #include <comphelper/serviceinfohelper.hxx>
34 
35 #include <vos/mutex.hxx>
36 #include <vcl/svapp.hxx>
37 
38 #include <svl/style.hxx>
39 
40 #include <svx/unoprov.hxx>
41 
42 #include "../ui/inc/strings.hrc"
43 #include "stlfamily.hxx"
44 #include "stlsheet.hxx"
45 #include "sdresid.hxx"
46 #include "drawdoc.hxx"
47 #include "sdpage.hxx"
48 #include "glob.hxx"
49 
50 #include <map>
51 
52 using ::rtl::OUString;
53 using namespace ::vos;
54 using namespace ::com::sun::star::uno;
55 using namespace ::com::sun::star::lang;
56 using namespace ::com::sun::star::container;
57 using namespace ::com::sun::star::style;
58 using namespace ::com::sun::star::beans;
59 
60 // ----------------------------------------------------------
61 
62 typedef std::map< rtl::OUString, rtl::Reference< SdStyleSheet > > PresStyleMap;
63 
64 struct SdStyleFamilyImpl
65 {
66     SdrPageWeakRef mxMasterPage;
67     String maLayoutName;
68 
69     PresStyleMap& getStyleSheets();
70     rtl::Reference< SfxStyleSheetPool > mxPool;
71 
72 private:
73     PresStyleMap maStyleSheets;
74 };
75 
76 PresStyleMap& SdStyleFamilyImpl::getStyleSheets()
77 {
78     if( mxMasterPage.is() && (mxMasterPage->GetLayoutName() != maLayoutName) )
79     {
80         maLayoutName = mxMasterPage->GetLayoutName();
81 
82         String aLayoutName( maLayoutName );
83         const sal_uInt16 nLen = aLayoutName.Search(String( RTL_CONSTASCII_USTRINGPARAM(SD_LT_SEPARATOR)))+4;
84         aLayoutName.Erase( nLen );
85 
86         if( (maStyleSheets.size() == 0) || !((*maStyleSheets.begin()).second->GetName().Equals( aLayoutName, 0, nLen )) )
87         {
88             maStyleSheets.clear();
89 
90             const SfxStyles& rStyles = mxPool->GetStyles();
91             for( SfxStyles::const_iterator iter( rStyles.begin() ); iter != rStyles.end(); iter++ )
92             {
93                 SdStyleSheet* pStyle = static_cast< SdStyleSheet* >( (*iter).get() );
94                 if( pStyle && (pStyle->GetFamily() == SD_STYLE_FAMILY_MASTERPAGE) && (pStyle->GetName().Equals( aLayoutName, 0, nLen )) )
95                     maStyleSheets[ pStyle->GetApiName() ] = rtl::Reference< SdStyleSheet >( pStyle );
96             }
97         }
98     }
99 
100     return maStyleSheets;
101 }
102 
103 // ----------------------------------------------------------
104 
105 SdStyleFamily::SdStyleFamily( const rtl::Reference< SfxStyleSheetPool >& xPool, SfxStyleFamily nFamily )
106 : mnFamily( nFamily )
107 , mxPool( xPool )
108 , mpImpl( 0 )
109 {
110 }
111 
112 // ----------------------------------------------------------
113 
114 SdStyleFamily::SdStyleFamily( const rtl::Reference< SfxStyleSheetPool >& xPool, const SdPage* pMasterPage )
115 : mnFamily( SD_STYLE_FAMILY_MASTERPAGE )
116 , mxPool( xPool )
117 , mpImpl( new SdStyleFamilyImpl() )
118 {
119     mpImpl->mxMasterPage.reset( const_cast< SdPage* >( pMasterPage ) );
120     mpImpl->mxPool = xPool;
121 }
122 
123 // ----------------------------------------------------------
124 
125 SdStyleFamily::~SdStyleFamily()
126 {
127     DBG_ASSERT( !mxPool.is(), "SdStyleFamily::~SdStyleFamily(), dispose me first!" );
128     delete mpImpl;
129 }
130 
131 // ----------------------------------------------------------
132 
133 void SdStyleFamily::throwIfDisposed() const throw(RuntimeException)
134 {
135     if( !mxPool.is() )
136         throw DisposedException();
137 }
138 
139 // ----------------------------------------------------------
140 
141 SdStyleSheet* SdStyleFamily::GetValidNewSheet( const Any& rElement ) throw(IllegalArgumentException)
142 {
143     Reference< XStyle > xStyle( rElement, UNO_QUERY );
144     SdStyleSheet* pStyle = static_cast< SdStyleSheet* >( xStyle.get() );
145 
146     if( pStyle == 0 || (pStyle->GetFamily() != mnFamily) || (&pStyle->GetPool() != mxPool.get()) || (mxPool->Find( pStyle->GetName(), mnFamily) != 0) )
147         throw IllegalArgumentException();
148 
149     return pStyle;
150 }
151 
152 // ----------------------------------------------------------
153 
154 SdStyleSheet* SdStyleFamily::GetSheetByName( const OUString& rName ) throw(NoSuchElementException, WrappedTargetException )
155 {
156     SdStyleSheet* pRet = 0;
157     if( rName.getLength() )
158     {
159         if( mnFamily == SD_STYLE_FAMILY_MASTERPAGE )
160         {
161             PresStyleMap& rStyleMap = mpImpl->getStyleSheets();
162             PresStyleMap::iterator iter( rStyleMap.find(rName) );
163             if( iter != rStyleMap.end() )
164                 pRet = (*iter).second.get();
165         }
166         else
167         {
168             const SfxStyles& rStyles = mxPool->GetStyles();
169             for( SfxStyles::const_iterator iter( rStyles.begin() ); iter != rStyles.end(); iter++ )
170             {
171                 SdStyleSheet* pStyle = static_cast< SdStyleSheet* >( (*iter).get() );
172                 if( pStyle && (pStyle->GetFamily() == mnFamily) && (pStyle->GetApiName() == rName) )
173                 {
174                     pRet = pStyle;
175                     break;
176                 }
177             }
178         }
179     }
180     if( pRet )
181         return pRet;
182 
183     throw NoSuchElementException();
184 }
185 
186 // ----------------------------------------------------------
187 // XServiceInfo
188 // ----------------------------------------------------------
189 
190 OUString SAL_CALL SdStyleFamily::getImplementationName() throw(RuntimeException)
191 {
192     return OUString( RTL_CONSTASCII_USTRINGPARAM("SdStyleFamily") );
193 }
194 
195 // ----------------------------------------------------------
196 
197 sal_Bool SAL_CALL SdStyleFamily::supportsService( const OUString& ServiceName ) throw(RuntimeException)
198 {
199     return comphelper::ServiceInfoHelper::supportsService( ServiceName, getSupportedServiceNames() );
200 }
201 
202 // ----------------------------------------------------------
203 
204 Sequence< OUString > SAL_CALL SdStyleFamily::getSupportedServiceNames() throw(RuntimeException)
205 {
206     OUString aServiceName( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.style.StyleFamily") );
207     Sequence< OUString > aSeq( &aServiceName, 1 );
208     return aSeq;
209 }
210 
211 // ----------------------------------------------------------
212 // XNamed
213 // ----------------------------------------------------------
214 
215 OUString SAL_CALL SdStyleFamily::getName() throw (RuntimeException)
216 {
217     if( mnFamily == SD_STYLE_FAMILY_MASTERPAGE )
218     {
219         SdPage* pPage = static_cast< SdPage* >( mpImpl->mxMasterPage.get() );
220         if( pPage == 0 )
221             throw DisposedException();
222 
223         String aLayoutName( pPage->GetLayoutName() );
224         const String aSep( RTL_CONSTASCII_USTRINGPARAM( SD_LT_SEPARATOR ));
225         aLayoutName.Erase(aLayoutName.Search(aSep));
226 
227         return OUString( aLayoutName );
228     }
229     else
230     {
231         return SdStyleSheet::GetFamilyString( mnFamily );
232     }
233 }
234 
235 // ----------------------------------------------------------
236 
237 void SAL_CALL SdStyleFamily::setName( const ::rtl::OUString& ) throw (RuntimeException)
238 {
239 }
240 
241 // ----------------------------------------------------------
242 // XNameAccess
243 // ----------------------------------------------------------
244 
245 Any SAL_CALL SdStyleFamily::getByName( const OUString& rName ) throw(NoSuchElementException, WrappedTargetException, RuntimeException)
246 {
247     OGuard aGuard( Application::GetSolarMutex() );
248     throwIfDisposed();
249     return Any( Reference< XStyle >( static_cast<SfxUnoStyleSheet*>(GetSheetByName( rName )) ) );
250 }
251 
252 // ----------------------------------------------------------
253 
254 Sequence< OUString > SAL_CALL SdStyleFamily::getElementNames() throw(RuntimeException)
255 {
256     OGuard aGuard( Application::GetSolarMutex() );
257 
258     throwIfDisposed();
259 
260     if( mnFamily == SD_STYLE_FAMILY_MASTERPAGE )
261     {
262         PresStyleMap& rStyleMap = mpImpl->getStyleSheets();
263         Sequence< OUString > aNames( rStyleMap.size() );
264 
265         PresStyleMap::iterator iter( rStyleMap.begin() );
266         OUString* pNames = aNames.getArray();
267         while( iter != rStyleMap.end() )
268         {
269             const OUString sName( (*iter).first );
270             rtl::Reference< SdStyleSheet > xStyle( (*iter++).second );
271             if( xStyle.is() )
272             {
273                 *pNames++ = xStyle->GetApiName();
274             }
275             else
276             {
277                 int i = 0;
278                 i++;
279             }
280         }
281 
282 //              *pNames++ = (*iter++).second->GetApiName();
283         return aNames;
284     }
285     else
286     {
287         std::vector< OUString > aNames;
288         const SfxStyles& rStyles = mxPool->GetStyles();
289         for( SfxStyles::const_iterator iter( rStyles.begin() ); iter != rStyles.end(); iter++ )
290         {
291             SdStyleSheet* pStyle = static_cast< SdStyleSheet* >( (*iter).get() );
292             if( pStyle && (pStyle->GetFamily() == mnFamily) )
293                 aNames.push_back( pStyle->GetApiName() );
294         }
295         return Sequence< OUString >( &(*aNames.begin()), aNames.size() );
296     }
297 }
298 
299 // ----------------------------------------------------------
300 
301 sal_Bool SAL_CALL SdStyleFamily::hasByName( const OUString& aName ) throw(RuntimeException)
302 {
303     OGuard aGuard( Application::GetSolarMutex() );
304     throwIfDisposed();
305 
306     if( aName.getLength() )
307     {
308         if( mnFamily == SD_STYLE_FAMILY_MASTERPAGE )
309         {
310             PresStyleMap& rStyleSheets = mpImpl->getStyleSheets();
311             PresStyleMap::iterator iter( rStyleSheets.find(aName) );
312             return ( iter != rStyleSheets.end() ) ? sal_True : sal_False;
313         }
314         else
315         {
316             const SfxStyles& rStyles = mxPool->GetStyles();
317             for( SfxStyles::const_iterator iter( rStyles.begin() ); iter != rStyles.end(); iter++ )
318             {
319                 SdStyleSheet* pStyle = static_cast< SdStyleSheet* >( (*iter).get() );
320                 if( pStyle && (pStyle->GetFamily() == mnFamily) && ( pStyle->GetApiName() == aName ) )
321                     return sal_True;
322             }
323         }
324     }
325 
326     return sal_False;
327 }
328 
329 // ----------------------------------------------------------
330 // XElementAccess
331 // ----------------------------------------------------------
332 
333 Type SAL_CALL SdStyleFamily::getElementType() throw(RuntimeException)
334 {
335     return XStyle::static_type();
336 }
337 
338 // ----------------------------------------------------------
339 
340 sal_Bool SAL_CALL SdStyleFamily::hasElements() throw(RuntimeException)
341 {
342     OGuard aGuard( Application::GetSolarMutex() );
343     throwIfDisposed();
344 
345     if( mnFamily == SD_STYLE_FAMILY_MASTERPAGE )
346     {
347         return sal_True;
348     }
349     else
350     {
351         const SfxStyles& rStyles = mxPool->GetStyles();
352         for( SfxStyles::const_iterator iter( rStyles.begin() ); iter != rStyles.end(); iter++ )
353         {
354             SdStyleSheet* pStyle = static_cast< SdStyleSheet* >( (*iter).get() );
355             if( pStyle && (pStyle->GetFamily() == mnFamily) )
356                 return sal_True;
357         }
358     }
359 
360     return sal_False;
361 }
362 
363 // ----------------------------------------------------------
364 // XIndexAccess
365 // ----------------------------------------------------------
366 
367 sal_Int32 SAL_CALL SdStyleFamily::getCount() throw(RuntimeException)
368 {
369     OGuard aGuard( Application::GetSolarMutex() );
370     throwIfDisposed();
371 
372     sal_Int32 nCount = 0;
373     if( mnFamily == SD_STYLE_FAMILY_MASTERPAGE )
374     {
375         return mpImpl->getStyleSheets().size();
376     }
377     else
378     {
379         const SfxStyles& rStyles = mxPool->GetStyles();
380         for( SfxStyles::const_iterator iter( rStyles.begin() ); iter != rStyles.end(); iter++ )
381         {
382             SdStyleSheet* pStyle = static_cast< SdStyleSheet* >( (*iter).get() );
383             if( pStyle && (pStyle->GetFamily() == mnFamily) )
384                 nCount++;
385         }
386     }
387 
388     return nCount;
389 }
390 
391 // ----------------------------------------------------------
392 
393 Any SAL_CALL SdStyleFamily::getByIndex( sal_Int32 Index ) throw(IndexOutOfBoundsException, WrappedTargetException, RuntimeException)
394 {
395     OGuard aGuard( Application::GetSolarMutex() );
396     throwIfDisposed();
397 
398     if( Index >= 0 )
399     {
400         if( mnFamily == SD_STYLE_FAMILY_MASTERPAGE )
401         {
402             PresStyleMap& rStyleSheets = mpImpl->getStyleSheets();
403             if( !rStyleSheets.empty() )
404             {
405                 PresStyleMap::iterator iter( rStyleSheets.begin() );
406                 while( Index-- && (iter != rStyleSheets.end()) )
407                     iter++;
408 
409                 if( (Index==-1) && (iter != rStyleSheets.end()) )
410                     return Any( Reference< XStyle >( (*iter).second.get() ) );
411             }
412         }
413         else
414         {
415             const SfxStyles& rStyles = mxPool->GetStyles();
416             for( SfxStyles::const_iterator iter( rStyles.begin() ); iter != rStyles.end(); iter++ )
417             {
418                 SdStyleSheet* pStyle = static_cast< SdStyleSheet* >( (*iter).get() );
419                 if( pStyle && (pStyle->GetFamily() == mnFamily) )
420                 {
421                     if( Index-- == 0 )
422                         return Any( Reference< XStyle >( pStyle ) );
423                 }
424             }
425         }
426     }
427 
428     throw IndexOutOfBoundsException();
429 }
430 
431 // ----------------------------------------------------------
432 // XNameContainer
433 // ----------------------------------------------------------
434 
435 void SAL_CALL SdStyleFamily::insertByName( const OUString& rName, const Any& rElement ) throw(IllegalArgumentException, ElementExistException, WrappedTargetException, RuntimeException)
436 {
437     OGuard aGuard( Application::GetSolarMutex() );
438     throwIfDisposed();
439 
440     if(rName.getLength() == 0)
441         throw IllegalArgumentException();
442 
443     SdStyleSheet* pStyle = GetValidNewSheet( rElement );
444     if( !pStyle->SetName( rName ) )
445         throw ElementExistException();
446 
447     pStyle->SetApiName( rName );
448     mxPool->Insert( pStyle );
449 }
450 
451 // ----------------------------------------------------------
452 
453 void SAL_CALL SdStyleFamily::removeByName( const OUString& rName ) throw(NoSuchElementException, WrappedTargetException, RuntimeException)
454 {
455     OGuard aGuard( Application::GetSolarMutex() );
456     throwIfDisposed();
457 
458     SdStyleSheet* pStyle = GetSheetByName( rName );
459 
460     if( !pStyle->IsUserDefined() )
461         throw WrappedTargetException();
462 
463     mxPool->Remove( pStyle );
464 }
465 
466 // ----------------------------------------------------------
467 // XNameReplace
468 // ----------------------------------------------------------
469 
470 void SAL_CALL SdStyleFamily::replaceByName( const OUString& rName, const Any& aElement ) throw(IllegalArgumentException, NoSuchElementException, WrappedTargetException, RuntimeException)
471 {
472     OGuard aGuard( Application::GetSolarMutex() );
473     throwIfDisposed();
474 
475     SdStyleSheet* pOldStyle = GetSheetByName( rName );
476     SdStyleSheet* pNewStyle = GetValidNewSheet( aElement );
477 
478     mxPool->Remove( pOldStyle );
479     mxPool->Insert( pNewStyle );
480 }
481 
482 // ----------------------------------------------------------
483 // XSingleServiceFactory
484 // ----------------------------------------------------------
485 
486 Reference< XInterface > SAL_CALL SdStyleFamily::createInstance() throw(Exception, RuntimeException)
487 {
488     OGuard aGuard( Application::GetSolarMutex() );
489     throwIfDisposed();
490 
491     if( mnFamily == SD_STYLE_FAMILY_MASTERPAGE )
492     {
493         throw IllegalAccessException();
494     }
495     else
496     {
497         return Reference< XInterface >( static_cast< XStyle* >( SdStyleSheet::CreateEmptyUserStyle( *mxPool.get(), mnFamily ) ) );
498     }
499 }
500 
501 // ----------------------------------------------------------
502 
503 Reference< XInterface > SAL_CALL SdStyleFamily::createInstanceWithArguments( const Sequence< Any >&  ) throw(Exception, RuntimeException)
504 {
505     return createInstance();
506 }
507 
508 // ----------------------------------------------------------
509 // XComponent
510 // ----------------------------------------------------------
511 
512 void SAL_CALL SdStyleFamily::dispose(  ) throw (RuntimeException)
513 {
514     if( mxPool.is() )
515         mxPool.clear();
516 
517     if( mpImpl )
518     {
519         delete mpImpl;
520         mpImpl = 0;
521     }
522 }
523 
524 // ----------------------------------------------------------
525 
526 void SAL_CALL SdStyleFamily::addEventListener( const Reference< XEventListener >&  ) throw (RuntimeException)
527 {
528 }
529 
530 // ----------------------------------------------------------
531 
532 void SAL_CALL SdStyleFamily::removeEventListener( const Reference< XEventListener >&  ) throw (RuntimeException)
533 {
534 }
535 
536 // ----------------------------------------------------------
537 // XPropertySet
538 // ----------------------------------------------------------
539 
540 Reference<XPropertySetInfo> SdStyleFamily::getPropertySetInfo() throw (RuntimeException)
541 {
542     OSL_ENSURE( 0, "###unexpected!" );
543     return Reference<XPropertySetInfo>();
544 }
545 
546 // ----------------------------------------------------------
547 
548 void SdStyleFamily::setPropertyValue( const OUString& , const Any&  ) throw (UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException, RuntimeException)
549 {
550     OSL_ENSURE( 0, "###unexpected!" );
551 }
552 
553 // ----------------------------------------------------------
554 
555 Any SdStyleFamily::getPropertyValue( const OUString& PropertyName ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException)
556 {
557     if (PropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("DisplayName") ))
558     {
559         OGuard aGuard( Application::GetSolarMutex() );
560         OUString sDisplayName;
561         switch( mnFamily )
562         {
563             case SD_STYLE_FAMILY_MASTERPAGE:    sDisplayName = getName(); break;
564             case SD_STYLE_FAMILY_CELL:          sDisplayName = String( SdResId(STR_CELL_STYLE_FAMILY) ); break;
565 //          case SD_STYLE_FAMILY_GRAPHICS:
566             default:                            sDisplayName = String( SdResId(STR_GRAPHICS_STYLE_FAMILY) ); break;
567         }
568         return Any( sDisplayName );
569     }
570     else
571     {
572         throw UnknownPropertyException( OUString( RTL_CONSTASCII_USTRINGPARAM("unknown property: ") ) + PropertyName, static_cast<OWeakObject *>(this) );
573     }
574 }
575 
576 // ----------------------------------------------------------
577 
578 void SdStyleFamily::addPropertyChangeListener( const OUString& , const Reference<XPropertyChangeListener>&  ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException)
579 {
580     OSL_ENSURE( 0, "###unexpected!" );
581 }
582 
583 // ----------------------------------------------------------
584 
585 void SdStyleFamily::removePropertyChangeListener( const OUString& , const Reference<XPropertyChangeListener>&  ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException)
586 {
587     OSL_ENSURE( 0, "###unexpected!" );
588 }
589 
590 // ----------------------------------------------------------
591 
592 void SdStyleFamily::addVetoableChangeListener( const OUString& , const Reference<XVetoableChangeListener>& ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException)
593 {
594     OSL_ENSURE( 0, "###unexpected!" );
595 }
596 
597 // ----------------------------------------------------------
598 
599 void SdStyleFamily::removeVetoableChangeListener( const OUString& , const Reference<XVetoableChangeListener>&  ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException)
600 {
601     OSL_ENSURE( 0, "###unexpected!" );
602 }
603 
604