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