1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
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
);
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 );
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
;
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
;
103 throw lang::IllegalArgumentException();
105 SdDrawPage
* pPage
= SdDrawPage::getImplementation( xPage
);
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());
120 mpModel
->SetModified();
123 void SAL_CALL
SdXCustomPresentation::removeByIndex( sal_Int32 Index
)
124 throw(lang::IndexOutOfBoundsException
, lang::WrappedTargetException
, uno::RuntimeException
)
126 SolarMutexGuard aGuard
;
129 throw lang::DisposedException();
133 uno::Reference
< drawing::XDrawPage
> xPage
;
134 getByIndex( Index
) >>= xPage
;
138 SvxDrawPage
* pPage
= SvxDrawPage::getImplementation( xPage
);
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
);
152 mpModel
->SetModified();
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
);
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
;
176 throw lang::DisposedException();
178 return getCount() > 0;
182 sal_Int32 SAL_CALL
SdXCustomPresentation::getCount()
183 throw(uno::RuntimeException
)
185 SolarMutexGuard aGuard
;
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
;
198 throw lang::DisposedException();
200 if( Index
< 0 || Index
>= (sal_Int32
)mpSdCustomShow
->PagesVector().size() )
201 throw lang::IndexOutOfBoundsException();
206 SdrPage
* pPage
= (SdrPage
*)mpSdCustomShow
->PagesVector()[Index
];
210 uno::Reference
< drawing::XDrawPage
> xRef( pPage
->getUnoPage(), uno::UNO_QUERY
);
219 OUString SAL_CALL
SdXCustomPresentation::getName()
220 throw(uno::RuntimeException
)
222 SolarMutexGuard aGuard
;
225 throw lang::DisposedException();
228 return mpSdCustomShow
->GetName();
233 void SAL_CALL
SdXCustomPresentation::setName( const OUString
& aName
)
234 throw(uno::RuntimeException
)
236 SolarMutexGuard aGuard
;
239 throw lang::DisposedException();
242 mpSdCustomShow
->SetName( aName
);
246 void SAL_CALL
SdXCustomPresentation::dispose() throw(uno::RuntimeException
)
248 SolarMutexGuard aGuard
;
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
)
269 throw lang::DisposedException();
271 aDisposeListeners
.addInterface(xListener
);
274 //----------------------------------------------------------------------
275 void SAL_CALL
SdXCustomPresentation::removeEventListener( const uno::Reference
< lang::XEventListener
>& aListener
) throw(uno::RuntimeException
)
278 aDisposeListeners
.removeInterface(aListener
);
281 /*===========================================================================*
282 * class SdXCustomPresentationAccess : public XCustomPresentationAccess, *
284 *===========================================================================*/
286 SdXCustomPresentationAccess::SdXCustomPresentationAccess(SdXImpressDocument
& rMyModel
) throw()
291 SdXCustomPresentationAccess::~SdXCustomPresentationAccess() throw()
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 );
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() );
324 uno::Reference
< uno::XInterface
> SAL_CALL
SdXCustomPresentationAccess::createInstanceWithArguments( const uno::Sequence
< uno::Any
>& )
325 throw(uno::Exception
, uno::RuntimeException
)
327 return createInstance();
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;
339 pList
= mrModel
.GetDoc()->GetCustomShowList(sal_True
);
341 // no list, no cookies
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
);
353 throw lang::IllegalArgumentException();
355 // get the internal custom show from the api wrapper
356 SdCustomShow
* pShow
= pXShow
->GetSdCustomShow();
359 pShow
= new SdCustomShow( mrModel
.GetDoc(), xContainer
);
360 pXShow
->SetSdCustomShow( pShow
);
364 if( NULL
== pXShow
->GetModel() || *pXShow
->GetModel() != mrModel
)
365 throw lang::IllegalArgumentException();
369 pShow
->SetName( aName
);
371 // check if this or another customshow with the same name already exists
372 for( SdCustomShow
* pCompare
= pList
->First();
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();
394 delete pList
->Remove( pShow
);
396 throw container::NoSuchElementException();
398 mrModel
.SetModified();
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
);
410 uno::Any SAL_CALL
SdXCustomPresentationAccess::getByName( const OUString
& aName
)
411 throw(container::NoSuchElementException
, lang::WrappedTargetException
, uno::RuntimeException
)
413 SolarMutexGuard aGuard
;
417 SdCustomShow
* pShow
= getSdCustomShow(aName
);
420 uno::Reference
< container::XIndexContainer
> xRef( pShow
->getUnoCustomShow(), uno::UNO_QUERY
);
425 throw container::NoSuchElementException();
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();
443 while( nIdx
< nCount
)
445 const SdCustomShow
* pShow
= (*pList
)[nIdx
];
446 pStringList
[nIdx
] = pShow
->GetName();
454 sal_Bool SAL_CALL
SdXCustomPresentationAccess::hasByName( const OUString
& aName
)
455 throw(uno::RuntimeException
)
457 SolarMutexGuard aGuard
;
458 return getSdCustomShow(aName
) != NULL
;
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()
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
)
496 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */