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