xref: /aoo41x/main/sd/source/ui/unoidl/unocpres.cxx (revision 79aad27f)
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 #include <com/sun/star/lang/DisposedException.hpp>
27 #include <osl/mutex.hxx>
28 #include <vos/mutex.hxx>
29 #include <vcl/svapp.hxx>
30 #include <svx/svdpage.hxx>
31 #include <comphelper/extract.hxx>
32 #include <comphelper/serviceinfohelper.hxx>
33 
34 #include "unohelp.hxx"
35 #include "unomodel.hxx"
36 #include "drawdoc.hxx"
37 #include "unocpres.hxx"
38 #include "cusshow.hxx"
39 #include "unopage.hxx"
40 
41 using namespace ::rtl;
42 using namespace ::vos;
43 using namespace ::com::sun::star;
44 
45 
createUnoCustomShow(SdCustomShow * pShow)46 uno::Reference< uno::XInterface > createUnoCustomShow( SdCustomShow* pShow )
47 {
48 	return (cppu::OWeakObject*)new SdXCustomPresentation( pShow, NULL );
49 }
50 
SdXCustomPresentation()51 SdXCustomPresentation::SdXCustomPresentation() throw()
52 :	mpSdCustomShow(NULL), mpModel(NULL),
53 	aDisposeListeners( aDisposeContainerMutex ),
54 	bDisposing( sal_False )
55 {
56 }
57 
SdXCustomPresentation(SdCustomShow * pShow,SdXImpressDocument * pMyModel)58 SdXCustomPresentation::SdXCustomPresentation( SdCustomShow* pShow, SdXImpressDocument* pMyModel) throw()
59 :	mpSdCustomShow(pShow), mpModel(pMyModel),
60 	aDisposeListeners( aDisposeContainerMutex ),
61 	bDisposing( sal_False )
62 {
63 }
64 
~SdXCustomPresentation()65 SdXCustomPresentation::~SdXCustomPresentation() throw()
66 {
67 }
68 
69 UNO3_GETIMPLEMENTATION_IMPL( SdXCustomPresentation );
70 
71 // XServiceInfo
getImplementationName()72 OUString SAL_CALL SdXCustomPresentation::getImplementationName()
73 	throw(uno::RuntimeException)
74 {
75 	return OUString( RTL_CONSTASCII_USTRINGPARAM("SdXCustomPresentation") );
76 }
77 
supportsService(const OUString & ServiceName)78 sal_Bool SAL_CALL SdXCustomPresentation::supportsService( const OUString& ServiceName )
79 	throw(uno::RuntimeException)
80 {
81 	return comphelper::ServiceInfoHelper::supportsService( ServiceName, getSupportedServiceNames() );
82 }
83 
getSupportedServiceNames()84 uno::Sequence< OUString > SAL_CALL SdXCustomPresentation::getSupportedServiceNames()
85 	throw(uno::RuntimeException)
86 {
87 	OUString aSN( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.presentation.CustomPresentation") );
88 	uno::Sequence< OUString > aSeq( &aSN, 1 );
89 	return aSeq;
90 }
91 
92 // XIndexContainer
insertByIndex(sal_Int32 Index,const uno::Any & Element)93 void SAL_CALL SdXCustomPresentation::insertByIndex( sal_Int32 Index, const uno::Any& Element )
94 	throw(lang::IllegalArgumentException, lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException)
95 {
96 	OGuard aGuard( Application::GetSolarMutex() );
97 
98 	if( bDisposing )
99 		throw lang::DisposedException();
100 
101 	if( Index < 0 || Index > (sal_Int32)( mpSdCustomShow ? mpSdCustomShow->Count() : 0 ) )
102 		throw lang::IndexOutOfBoundsException();
103 
104 	uno::Reference< drawing::XDrawPage > xPage;
105 	Element >>= xPage;
106 
107 	if(!xPage.is())
108 		throw lang::IllegalArgumentException();
109 
110 	SdDrawPage* pPage = SdDrawPage::getImplementation( xPage );
111 
112 	if(pPage)
113 	{
114 		if( NULL == mpModel )
115 			mpModel = pPage->GetModel();
116 
117 		if( NULL != mpModel && NULL == mpSdCustomShow && mpModel->GetDoc() )
118 			mpSdCustomShow = new SdCustomShow( mpModel->GetDoc() );
119 
120 		mpSdCustomShow->Insert(pPage->GetSdrPage(), Index);
121 	}
122 
123 	if( mpModel )
124 		mpModel->SetModified();
125 }
126 
removeByIndex(sal_Int32 Index)127 void SAL_CALL SdXCustomPresentation::removeByIndex( sal_Int32 Index )
128 	throw(lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException)
129 {
130 	OGuard aGuard( Application::GetSolarMutex() );
131 
132 	if( bDisposing )
133 		throw lang::DisposedException();
134 
135 	if(mpSdCustomShow)
136 	{
137 		uno::Reference< drawing::XDrawPage > xPage;
138 		getByIndex( Index ) >>= xPage;
139 
140 		if( xPage.is() )
141 		{
142 			SvxDrawPage* pPage = SvxDrawPage::getImplementation(  xPage );
143 			if(pPage)
144 				mpSdCustomShow->Remove(pPage->GetSdrPage());
145 		}
146 	}
147 
148 	if( mpModel )
149 		mpModel->SetModified();
150 }
151 
152 // XIndexReplace
replaceByIndex(sal_Int32 Index,const uno::Any & Element)153 void SAL_CALL SdXCustomPresentation::replaceByIndex( sal_Int32 Index, const uno::Any& Element )
154 	throw(lang::IllegalArgumentException, lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException)
155 {
156 	removeByIndex( Index );
157 	insertByIndex( Index, Element );
158 }
159 
160 // XElementAccess
getElementType()161 uno::Type SAL_CALL SdXCustomPresentation::getElementType()
162 	throw(uno::RuntimeException)
163 {
164 	return ITYPE( drawing::XDrawPage );
165 }
166 
hasElements()167 sal_Bool SAL_CALL SdXCustomPresentation::hasElements()
168 	throw(uno::RuntimeException)
169 {
170 	OGuard aGuard( Application::GetSolarMutex() );
171 
172 	if( bDisposing )
173 		throw lang::DisposedException();
174 
175 	return getCount() > 0;
176 }
177 
178 // XIndexAccess
getCount()179 sal_Int32 SAL_CALL SdXCustomPresentation::getCount()
180 	throw(uno::RuntimeException)
181 {
182 	OGuard aGuard( Application::GetSolarMutex() );
183 	if( bDisposing )
184 		throw lang::DisposedException();
185 
186 	return mpSdCustomShow?mpSdCustomShow->Count():0;
187 }
188 
getByIndex(sal_Int32 Index)189 uno::Any SAL_CALL SdXCustomPresentation::getByIndex( sal_Int32 Index )
190 	throw(lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException)
191 {
192 	OGuard aGuard( Application::GetSolarMutex() );
193 
194 	if( bDisposing )
195 		throw lang::DisposedException();
196 
197 	if( Index < 0 || Index >= (sal_Int32)mpSdCustomShow->Count() )
198 		throw lang::IndexOutOfBoundsException();
199 
200 	uno::Any aAny;
201 	if(mpSdCustomShow )
202 	{
203 		SdrPage* pPage = (SdrPage*)mpSdCustomShow->GetObject(Index);
204 
205 		if( pPage )
206 		{
207 			uno::Reference< drawing::XDrawPage > xRef( pPage->getUnoPage(), uno::UNO_QUERY );
208 			aAny <<= xRef;
209 		}
210 	}
211 
212 	return aAny;
213 }
214 
215 // XNamed
getName()216 OUString SAL_CALL SdXCustomPresentation::getName()
217 	throw(uno::RuntimeException)
218 {
219 	OGuard aGuard( Application::GetSolarMutex() );
220 
221 	if( bDisposing )
222 		throw lang::DisposedException();
223 
224 	if(mpSdCustomShow)
225 		return mpSdCustomShow->GetName();
226 
227 	return OUString();
228 }
229 
setName(const OUString & aName)230 void SAL_CALL SdXCustomPresentation::setName( const OUString& aName )
231 	throw(uno::RuntimeException)
232 {
233 	OGuard aGuard( Application::GetSolarMutex() );
234 
235 	if( bDisposing )
236 		throw lang::DisposedException();
237 
238 	if(mpSdCustomShow)
239 		mpSdCustomShow->SetName( aName );
240 }
241 
242 // XComponent
dispose()243 void SAL_CALL SdXCustomPresentation::dispose() throw(uno::RuntimeException)
244 {
245 	OGuard aGuard( Application::GetSolarMutex() );
246 
247 	if( bDisposing )
248 		return;	// catched a recursion
249 
250 	bDisposing = sal_True;
251 
252 	uno::Reference< uno::XInterface > xSource( (cppu::OWeakObject*)this );
253 
254 	lang::EventObject aEvt;
255 	aEvt.Source = xSource;
256 	aDisposeListeners.disposeAndClear(aEvt);
257 
258 	mpSdCustomShow = NULL;
259 }
260 
261 //----------------------------------------------------------------------
addEventListener(const uno::Reference<lang::XEventListener> & xListener)262 void SAL_CALL SdXCustomPresentation::addEventListener( const uno::Reference< lang::XEventListener >& xListener )
263 	throw(uno::RuntimeException)
264 {
265 	if( bDisposing )
266 		throw lang::DisposedException();
267 
268 	aDisposeListeners.addInterface(xListener);
269 }
270 
271 //----------------------------------------------------------------------
removeEventListener(const uno::Reference<lang::XEventListener> & aListener)272 void SAL_CALL SdXCustomPresentation::removeEventListener( const uno::Reference< lang::XEventListener >& aListener ) throw(uno::RuntimeException)
273 {
274 	if( !bDisposing )
275 		aDisposeListeners.removeInterface(aListener);
276 }
277 
278 /*===========================================================================*
279  *  class SdXCustomPresentationAccess : public XCustomPresentationAccess,	 *
280  * 										public UsrObject					 *
281  *===========================================================================*/
282 
SdXCustomPresentationAccess(SdXImpressDocument & rMyModel)283 SdXCustomPresentationAccess::SdXCustomPresentationAccess(SdXImpressDocument& rMyModel) throw()
284 : mrModel(rMyModel)
285 {
286 }
287 
~SdXCustomPresentationAccess()288 SdXCustomPresentationAccess::~SdXCustomPresentationAccess() throw()
289 {
290 }
291 
292 // XServiceInfo
getImplementationName()293 OUString SAL_CALL SdXCustomPresentationAccess::getImplementationName()
294 	throw(uno::RuntimeException)
295 {
296 	return OUString( RTL_CONSTASCII_USTRINGPARAM("SdXCustomPresentationAccess") );
297 }
298 
supportsService(const OUString & ServiceName)299 sal_Bool SAL_CALL SdXCustomPresentationAccess::supportsService( const OUString& ServiceName )
300 	throw(uno::RuntimeException)
301 {
302 	return comphelper::ServiceInfoHelper::supportsService( ServiceName, getSupportedServiceNames() );
303 }
304 
getSupportedServiceNames()305 uno::Sequence< OUString > SAL_CALL SdXCustomPresentationAccess::getSupportedServiceNames()
306 	throw(uno::RuntimeException)
307 {
308 	const OUString aNS( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.presentation.CustomPresentationAccess") );
309 	uno::Sequence< OUString > aSeq( &aNS, 1 );
310 	return aSeq;
311 }
312 
313 // XSingleServiceFactory
createInstance()314 uno::Reference< uno::XInterface > SAL_CALL SdXCustomPresentationAccess::createInstance()
315 	throw(uno::Exception, uno::RuntimeException)
316 {
317 	uno::Reference< uno::XInterface >  xRef( (::cppu::OWeakObject*)new SdXCustomPresentation() );
318 	return xRef;
319 }
320 
createInstanceWithArguments(const uno::Sequence<uno::Any> &)321 uno::Reference< uno::XInterface > SAL_CALL SdXCustomPresentationAccess::createInstanceWithArguments( const uno::Sequence< uno::Any >& )
322 	throw(uno::Exception, uno::RuntimeException)
323 {
324 	return createInstance();
325 }
326 
327 // XNameContainer
insertByName(const OUString & aName,const uno::Any & aElement)328 void SAL_CALL SdXCustomPresentationAccess::insertByName( const OUString& aName, const uno::Any& aElement )
329 	throw(lang::IllegalArgumentException, container::ElementExistException, lang::WrappedTargetException, uno::RuntimeException)
330 {
331 	OGuard aGuard( Application::GetSolarMutex() );
332 
333 	// get the documents custom show list
334 	List* pList = 0;
335 	if(mrModel.GetDoc())
336 		pList = mrModel.GetDoc()->GetCustomShowList(sal_True);
337 
338 	// no list, no cookies
339 	if( NULL == pList)
340 		throw uno::RuntimeException();
341 
342 	// do we have an container::XIndexContainer?
343 	SdXCustomPresentation* pXShow = NULL;
344 
345 	uno::Reference< container::XIndexContainer > xContainer;
346 	if( (aElement >>= xContainer) && xContainer.is() )
347 		pXShow = SdXCustomPresentation::getImplementation(xContainer);
348 
349 	if( NULL == pXShow )
350 		throw lang::IllegalArgumentException();
351 
352 	// get the internal custom show from the api wrapper
353 	SdCustomShow* pShow = pXShow->GetSdCustomShow();
354 	if( NULL == pShow )
355 	{
356 		pShow = new SdCustomShow( mrModel.GetDoc(), xContainer );
357 		pXShow->SetSdCustomShow( pShow );
358 	}
359 	else
360 	{
361 		if( NULL == pXShow->GetModel() || *pXShow->GetModel() != mrModel )
362 			throw lang::IllegalArgumentException();
363 	}
364 
365 	// give it a name
366 	pShow->SetName( aName);
367 
368 	// check if this or another customshow with the same name already exists
369 	for( SdCustomShow* pCompare = (SdCustomShow*)pList->First();
370 		 pCompare;
371 		 pCompare = (SdCustomShow*)pList->Next() )
372 	{
373 		if( pCompare == pShow || pCompare->GetName() == pShow->GetName() )
374 			throw container::ElementExistException();
375 	}
376 
377 	pList->Insert(pShow);
378 
379 	mrModel.SetModified();
380 }
381 
removeByName(const OUString & Name)382 void SAL_CALL SdXCustomPresentationAccess::removeByName( const OUString& Name )
383 	throw(container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
384 {
385 	OGuard aGuard( Application::GetSolarMutex() );
386 
387 	SdCustomShow* pShow = getSdCustomShow(Name);
388 
389 	List* pList = GetCustomShowList();
390 	if(pList && pShow)
391 		delete (SdCustomShow*)pList->Remove( pShow );
392 	else
393 		throw container::NoSuchElementException();
394 
395 	mrModel.SetModified();
396 }
397 
398 // XNameReplace
replaceByName(const OUString & aName,const uno::Any & aElement)399 void SAL_CALL SdXCustomPresentationAccess::replaceByName( const OUString& aName, const uno::Any& aElement )
400 	throw(lang::IllegalArgumentException, container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
401 {
402 	removeByName( aName );
403 	insertByName( aName, aElement );
404 }
405 
406 // XNameAccess
getByName(const OUString & aName)407 uno::Any SAL_CALL SdXCustomPresentationAccess::getByName( const OUString& aName )
408 	throw(container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
409 {
410 	OGuard aGuard( Application::GetSolarMutex() );
411 
412 	uno::Any aAny;
413 
414 	SdCustomShow* pShow = getSdCustomShow(aName);
415 	if(pShow)
416 	{
417 		uno::Reference< container::XIndexContainer >  xRef( pShow->getUnoCustomShow(), uno::UNO_QUERY );
418 		aAny <<= xRef;
419 	}
420 	else
421 	{
422 		throw container::NoSuchElementException();
423 	}
424 
425 	return aAny;
426 }
427 
getElementNames()428 uno::Sequence< OUString > SAL_CALL SdXCustomPresentationAccess::getElementNames()
429 	throw(uno::RuntimeException)
430 {
431 	OGuard aGuard( Application::GetSolarMutex() );
432 
433 	List* pList = GetCustomShowList();
434 	const sal_uInt32 nCount = pList?pList->Count():0;
435 
436 	uno::Sequence< OUString > aSequence( nCount );
437 	OUString* pStringList = aSequence.getArray();
438 
439 	sal_uInt32 nIdx = 0;
440 	while( nIdx < nCount )
441 	{
442 		const SdCustomShow* pShow = (const SdCustomShow*)pList->GetObject(nIdx);
443 		pStringList[nIdx] = pShow->GetName();
444 		nIdx++;
445 	}
446 
447 	return aSequence;
448 }
449 
450 
hasByName(const OUString & aName)451 sal_Bool SAL_CALL SdXCustomPresentationAccess::hasByName( const OUString& aName )
452 	throw(uno::RuntimeException)
453 {
454 	OGuard aGuard( Application::GetSolarMutex() );
455 	return getSdCustomShow(aName) != NULL;
456 }
457 
458 // XElementAccess
getElementType()459 uno::Type SAL_CALL SdXCustomPresentationAccess::getElementType()
460 	throw(uno::RuntimeException)
461 {
462 	return ITYPE( container::XIndexContainer );
463 }
464 
hasElements()465 sal_Bool SAL_CALL SdXCustomPresentationAccess::hasElements()
466 	throw(uno::RuntimeException)
467 {
468 	OGuard aGuard( Application::GetSolarMutex() );
469 
470 	List* pList = GetCustomShowList();
471 	return pList && pList->Count() > 0;
472 }
473 
getSdCustomShow(const OUString & Name) const474 SdCustomShow * SdXCustomPresentationAccess::getSdCustomShow( const OUString& Name ) const throw()
475 {
476 	sal_uInt32 nIdx = 0;
477 
478 	List* pList = GetCustomShowList();
479 	const sal_uInt32 nCount = pList?pList->Count():0;
480 
481 	const String aName( Name );
482 
483 	while( nIdx < nCount )
484 	{
485 		SdCustomShow* pShow = (SdCustomShow*)pList->GetObject(nIdx);
486 		if( pShow->GetName() == aName )
487 			return pShow;
488 		nIdx++;
489 	}
490 	return NULL;
491 }
492