bump product version to 4.1.6.2
[LibreOffice.git] / sd / source / ui / unoidl / unocpres.cxx
blobf0f0117a0e66fae258a8be60d6be2018a78a1bc7
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <algorithm>
22 #include <com/sun/star/lang/DisposedException.hpp>
23 #include <osl/mutex.hxx>
24 #include <vcl/svapp.hxx>
25 #include <svx/svdpage.hxx>
26 #include <comphelper/extract.hxx>
27 #include <comphelper/serviceinfohelper.hxx>
29 #include "unohelp.hxx"
30 #include "unomodel.hxx"
31 #include "drawdoc.hxx"
32 #include "unocpres.hxx"
33 #include "cusshow.hxx"
34 #include "unopage.hxx"
35 #include "customshowlist.hxx"
37 using namespace ::rtl;
38 using namespace ::com::sun::star;
41 uno::Reference< uno::XInterface > createUnoCustomShow( SdCustomShow* pShow )
43 return (cppu::OWeakObject*)new SdXCustomPresentation( pShow, NULL );
46 SdXCustomPresentation::SdXCustomPresentation() throw()
47 : mpSdCustomShow(NULL), mpModel(NULL),
48 aDisposeListeners( aDisposeContainerMutex ),
49 bDisposing( sal_False )
53 SdXCustomPresentation::SdXCustomPresentation( SdCustomShow* pShow, SdXImpressDocument* pMyModel) throw()
54 : mpSdCustomShow(pShow), mpModel(pMyModel),
55 aDisposeListeners( aDisposeContainerMutex ),
56 bDisposing( sal_False )
60 SdXCustomPresentation::~SdXCustomPresentation() throw()
64 UNO3_GETIMPLEMENTATION_IMPL( SdXCustomPresentation );
66 // XServiceInfo
67 OUString SAL_CALL SdXCustomPresentation::getImplementationName()
68 throw(uno::RuntimeException)
70 return OUString( "SdXCustomPresentation" ) ;
73 sal_Bool SAL_CALL SdXCustomPresentation::supportsService( const OUString& ServiceName )
74 throw(uno::RuntimeException)
76 return comphelper::ServiceInfoHelper::supportsService( ServiceName, getSupportedServiceNames() );
79 uno::Sequence< OUString > SAL_CALL SdXCustomPresentation::getSupportedServiceNames()
80 throw(uno::RuntimeException)
82 OUString aSN( "com.sun.star.presentation.CustomPresentation" );
83 uno::Sequence< OUString > aSeq( &aSN, 1 );
84 return aSeq;
87 // XIndexContainer
88 void SAL_CALL SdXCustomPresentation::insertByIndex( sal_Int32 Index, const uno::Any& Element )
89 throw(lang::IllegalArgumentException, lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException)
91 SolarMutexGuard aGuard;
93 if( bDisposing )
94 throw lang::DisposedException();
96 if( Index < 0 || Index > (sal_Int32)( mpSdCustomShow ? mpSdCustomShow->PagesVector().size() : 0 ) )
97 throw lang::IndexOutOfBoundsException();
99 uno::Reference< drawing::XDrawPage > xPage;
100 Element >>= xPage;
102 if(!xPage.is())
103 throw lang::IllegalArgumentException();
105 SdDrawPage* pPage = SdDrawPage::getImplementation( xPage );
107 if(pPage)
109 if( NULL == mpModel )
110 mpModel = pPage->GetModel();
112 if( NULL != mpModel && NULL == mpSdCustomShow && mpModel->GetDoc() )
113 mpSdCustomShow = new SdCustomShow( mpModel->GetDoc() );
115 mpSdCustomShow->PagesVector().insert(mpSdCustomShow->PagesVector().begin() + Index,
116 (SdPage*) pPage->GetSdrPage());
119 if( mpModel )
120 mpModel->SetModified();
123 void SAL_CALL SdXCustomPresentation::removeByIndex( sal_Int32 Index )
124 throw(lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException)
126 SolarMutexGuard aGuard;
128 if( bDisposing )
129 throw lang::DisposedException();
131 if(mpSdCustomShow)
133 uno::Reference< drawing::XDrawPage > xPage;
134 getByIndex( Index ) >>= xPage;
136 if( xPage.is() )
138 SvxDrawPage* pPage = SvxDrawPage::getImplementation( xPage );
139 if(pPage)
141 SdCustomShow::PageVec::iterator it = std::find(
142 mpSdCustomShow->PagesVector().begin(),
143 mpSdCustomShow->PagesVector().end(),
144 pPage->GetSdrPage());
145 if (it != mpSdCustomShow->PagesVector().end())
146 mpSdCustomShow->PagesVector().erase(it);
151 if( mpModel )
152 mpModel->SetModified();
155 // XIndexReplace
156 void SAL_CALL SdXCustomPresentation::replaceByIndex( sal_Int32 Index, const uno::Any& Element )
157 throw(lang::IllegalArgumentException, lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException)
159 removeByIndex( Index );
160 insertByIndex( Index, Element );
163 // XElementAccess
164 uno::Type SAL_CALL SdXCustomPresentation::getElementType()
165 throw(uno::RuntimeException)
167 return ITYPE( drawing::XDrawPage );
170 sal_Bool SAL_CALL SdXCustomPresentation::hasElements()
171 throw(uno::RuntimeException)
173 SolarMutexGuard aGuard;
175 if( bDisposing )
176 throw lang::DisposedException();
178 return getCount() > 0;
181 // XIndexAccess
182 sal_Int32 SAL_CALL SdXCustomPresentation::getCount()
183 throw(uno::RuntimeException)
185 SolarMutexGuard aGuard;
186 if( bDisposing )
187 throw lang::DisposedException();
189 return mpSdCustomShow ? mpSdCustomShow->PagesVector().size() : 0;
192 uno::Any SAL_CALL SdXCustomPresentation::getByIndex( sal_Int32 Index )
193 throw(lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException)
195 SolarMutexGuard aGuard;
197 if( bDisposing )
198 throw lang::DisposedException();
200 if( Index < 0 || Index >= (sal_Int32)mpSdCustomShow->PagesVector().size() )
201 throw lang::IndexOutOfBoundsException();
203 uno::Any aAny;
204 if(mpSdCustomShow )
206 SdrPage* pPage = (SdrPage*)mpSdCustomShow->PagesVector()[Index];
208 if( pPage )
210 uno::Reference< drawing::XDrawPage > xRef( pPage->getUnoPage(), uno::UNO_QUERY );
211 aAny <<= xRef;
215 return aAny;
218 // XNamed
219 OUString SAL_CALL SdXCustomPresentation::getName()
220 throw(uno::RuntimeException)
222 SolarMutexGuard aGuard;
224 if( bDisposing )
225 throw lang::DisposedException();
227 if(mpSdCustomShow)
228 return mpSdCustomShow->GetName();
230 return OUString();
233 void SAL_CALL SdXCustomPresentation::setName( const OUString& aName )
234 throw(uno::RuntimeException)
236 SolarMutexGuard aGuard;
238 if( bDisposing )
239 throw lang::DisposedException();
241 if(mpSdCustomShow)
242 mpSdCustomShow->SetName( aName );
245 // XComponent
246 void SAL_CALL SdXCustomPresentation::dispose() throw(uno::RuntimeException)
248 SolarMutexGuard aGuard;
250 if( bDisposing )
251 return; // catched a recursion
253 bDisposing = sal_True;
255 uno::Reference< uno::XInterface > xSource( (cppu::OWeakObject*)this );
257 lang::EventObject aEvt;
258 aEvt.Source = xSource;
259 aDisposeListeners.disposeAndClear(aEvt);
261 mpSdCustomShow = NULL;
264 //----------------------------------------------------------------------
265 void SAL_CALL SdXCustomPresentation::addEventListener( const uno::Reference< lang::XEventListener >& xListener )
266 throw(uno::RuntimeException)
268 if( bDisposing )
269 throw lang::DisposedException();
271 aDisposeListeners.addInterface(xListener);
274 //----------------------------------------------------------------------
275 void SAL_CALL SdXCustomPresentation::removeEventListener( const uno::Reference< lang::XEventListener >& aListener ) throw(uno::RuntimeException)
277 if( !bDisposing )
278 aDisposeListeners.removeInterface(aListener);
281 /*===========================================================================*
282 * class SdXCustomPresentationAccess : public XCustomPresentationAccess, *
283 * public UsrObject *
284 *===========================================================================*/
286 SdXCustomPresentationAccess::SdXCustomPresentationAccess(SdXImpressDocument& rMyModel) throw()
287 : mrModel(rMyModel)
291 SdXCustomPresentationAccess::~SdXCustomPresentationAccess() throw()
295 // XServiceInfo
296 OUString SAL_CALL SdXCustomPresentationAccess::getImplementationName()
297 throw(uno::RuntimeException)
299 return OUString( "SdXCustomPresentationAccess" );
302 sal_Bool SAL_CALL SdXCustomPresentationAccess::supportsService( const OUString& ServiceName )
303 throw(uno::RuntimeException)
305 return comphelper::ServiceInfoHelper::supportsService( ServiceName, getSupportedServiceNames() );
308 uno::Sequence< OUString > SAL_CALL SdXCustomPresentationAccess::getSupportedServiceNames()
309 throw(uno::RuntimeException)
311 const OUString aNS( "com.sun.star.presentation.CustomPresentationAccess" );
312 uno::Sequence< OUString > aSeq( &aNS, 1 );
313 return aSeq;
316 // XSingleServiceFactory
317 uno::Reference< uno::XInterface > SAL_CALL SdXCustomPresentationAccess::createInstance()
318 throw(uno::Exception, uno::RuntimeException)
320 uno::Reference< uno::XInterface > xRef( (::cppu::OWeakObject*)new SdXCustomPresentation() );
321 return xRef;
324 uno::Reference< uno::XInterface > SAL_CALL SdXCustomPresentationAccess::createInstanceWithArguments( const uno::Sequence< uno::Any >& )
325 throw(uno::Exception, uno::RuntimeException)
327 return createInstance();
330 // XNameContainer
331 void SAL_CALL SdXCustomPresentationAccess::insertByName( const OUString& aName, const uno::Any& aElement )
332 throw(lang::IllegalArgumentException, container::ElementExistException, lang::WrappedTargetException, uno::RuntimeException)
334 SolarMutexGuard aGuard;
336 // get the documents custom show list
337 SdCustomShowList* pList = 0;
338 if(mrModel.GetDoc())
339 pList = mrModel.GetDoc()->GetCustomShowList(sal_True);
341 // no list, no cookies
342 if( NULL == pList)
343 throw uno::RuntimeException();
345 // do we have an container::XIndexContainer?
346 SdXCustomPresentation* pXShow = NULL;
348 uno::Reference< container::XIndexContainer > xContainer;
349 if( (aElement >>= xContainer) && xContainer.is() )
350 pXShow = SdXCustomPresentation::getImplementation(xContainer);
352 if( NULL == pXShow )
353 throw lang::IllegalArgumentException();
355 // get the internal custom show from the api wrapper
356 SdCustomShow* pShow = pXShow->GetSdCustomShow();
357 if( NULL == pShow )
359 pShow = new SdCustomShow( mrModel.GetDoc(), xContainer );
360 pXShow->SetSdCustomShow( pShow );
362 else
364 if( NULL == pXShow->GetModel() || *pXShow->GetModel() != mrModel )
365 throw lang::IllegalArgumentException();
368 // give it a name
369 pShow->SetName( aName);
371 // check if this or another customshow with the same name already exists
372 for( SdCustomShow* pCompare = pList->First();
373 pCompare;
374 pCompare = pList->Next() )
376 if( pCompare == pShow || pCompare->GetName() == pShow->GetName() )
377 throw container::ElementExistException();
380 pList->push_back(pShow);
382 mrModel.SetModified();
385 void SAL_CALL SdXCustomPresentationAccess::removeByName( const OUString& Name )
386 throw(container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
388 SolarMutexGuard aGuard;
390 SdCustomShow* pShow = getSdCustomShow(Name);
392 SdCustomShowList* pList = GetCustomShowList();
393 if(pList && pShow)
394 delete pList->Remove( pShow );
395 else
396 throw container::NoSuchElementException();
398 mrModel.SetModified();
401 // XNameReplace
402 void SAL_CALL SdXCustomPresentationAccess::replaceByName( const OUString& aName, const uno::Any& aElement )
403 throw(lang::IllegalArgumentException, container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
405 removeByName( aName );
406 insertByName( aName, aElement );
409 // XNameAccess
410 uno::Any SAL_CALL SdXCustomPresentationAccess::getByName( const OUString& aName )
411 throw(container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
413 SolarMutexGuard aGuard;
415 uno::Any aAny;
417 SdCustomShow* pShow = getSdCustomShow(aName);
418 if(pShow)
420 uno::Reference< container::XIndexContainer > xRef( pShow->getUnoCustomShow(), uno::UNO_QUERY );
421 aAny <<= xRef;
423 else
425 throw container::NoSuchElementException();
428 return aAny;
431 uno::Sequence< OUString > SAL_CALL SdXCustomPresentationAccess::getElementNames()
432 throw(uno::RuntimeException)
434 SolarMutexGuard aGuard;
436 SdCustomShowList* pList = GetCustomShowList();
437 const sal_uInt32 nCount = pList ? pList->size() : 0;
439 uno::Sequence< OUString > aSequence( nCount );
440 OUString* pStringList = aSequence.getArray();
442 sal_uInt32 nIdx = 0;
443 while( nIdx < nCount )
445 const SdCustomShow* pShow = (*pList)[nIdx];
446 pStringList[nIdx] = pShow->GetName();
447 nIdx++;
450 return aSequence;
454 sal_Bool SAL_CALL SdXCustomPresentationAccess::hasByName( const OUString& aName )
455 throw(uno::RuntimeException)
457 SolarMutexGuard aGuard;
458 return getSdCustomShow(aName) != NULL;
461 // XElementAccess
462 uno::Type SAL_CALL SdXCustomPresentationAccess::getElementType()
463 throw(uno::RuntimeException)
465 return ITYPE( container::XIndexContainer );
468 sal_Bool SAL_CALL SdXCustomPresentationAccess::hasElements()
469 throw(uno::RuntimeException)
471 SolarMutexGuard aGuard;
473 SdCustomShowList* pList = GetCustomShowList();
474 return pList && !pList->empty();
477 SdCustomShow * SdXCustomPresentationAccess::getSdCustomShow( const OUString& Name ) const throw()
479 sal_uInt32 nIdx = 0;
481 SdCustomShowList* pList = GetCustomShowList();
482 const sal_uInt32 nCount = pList ? pList->size() : 0;
484 const String aName( Name );
486 while( nIdx < nCount )
488 SdCustomShow* pShow = (*pList)[nIdx];
489 if( pShow->GetName() == aName )
490 return pShow;
491 nIdx++;
493 return NULL;
496 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */