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 <com/sun/star/lang/IndexOutOfBoundsException.hpp>
24 #include <o3tl/safeint.hxx>
25 #include <vcl/svapp.hxx>
26 #include <svx/svdpage.hxx>
27 #include <cppuhelper/supportsservice.hxx>
29 #include <createunocustomshow.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 ::com::sun::star
;
39 uno::Reference
< uno::XInterface
> createUnoCustomShow( SdCustomShow
* pShow
)
41 return static_cast<cppu::OWeakObject
*>(new SdXCustomPresentation( pShow
));
44 SdXCustomPresentation::SdXCustomPresentation() noexcept
45 : mpSdCustomShow(nullptr), mpModel(nullptr),
50 SdXCustomPresentation::SdXCustomPresentation( SdCustomShow
* pShow
) noexcept
51 : mpSdCustomShow(pShow
), mpModel(nullptr),
56 SdXCustomPresentation::~SdXCustomPresentation() noexcept
61 OUString SAL_CALL
SdXCustomPresentation::getImplementationName()
63 return u
"SdXCustomPresentation"_ustr
;
66 sal_Bool SAL_CALL
SdXCustomPresentation::supportsService( const OUString
& ServiceName
)
68 return cppu::supportsService( this, ServiceName
);
71 uno::Sequence
< OUString
> SAL_CALL
SdXCustomPresentation::getSupportedServiceNames()
73 return { u
"com.sun.star.presentation.CustomPresentation"_ustr
};
77 void SAL_CALL
SdXCustomPresentation::insertByIndex( sal_Int32 Index
, const uno::Any
& Element
)
79 SolarMutexGuard aGuard
;
82 throw lang::DisposedException();
84 if( Index
< 0 || o3tl::make_unsigned(Index
) > ( mpSdCustomShow
? mpSdCustomShow
->PagesVector().size() : 0 ) )
85 throw lang::IndexOutOfBoundsException();
87 uno::Reference
< drawing::XDrawPage
> xPage
;
91 throw lang::IllegalArgumentException();
93 SdDrawPage
* pPage
= comphelper::getFromUnoTunnel
<SdDrawPage
>( xPage
);
98 mpModel
= pPage
->GetModel();
101 mpSdCustomShow
= new SdCustomShow
;
103 mpSdCustomShow
->PagesVector().insert(mpSdCustomShow
->PagesVector().begin() + Index
,
104 static_cast<SdPage
*>(pPage
->GetSdrPage()));
108 mpModel
->SetModified();
111 void SAL_CALL
SdXCustomPresentation::removeByIndex( sal_Int32 Index
)
113 SolarMutexGuard aGuard
;
116 throw lang::DisposedException();
120 uno::Reference
< drawing::XDrawPage
> xPage
;
121 getByIndex( Index
) >>= xPage
;
125 SvxDrawPage
* pPage
= comphelper::getFromUnoTunnel
<SvxDrawPage
>( xPage
);
128 SdCustomShow::PageVec::iterator it
= std::find(
129 mpSdCustomShow
->PagesVector().begin(),
130 mpSdCustomShow
->PagesVector().end(),
131 pPage
->GetSdrPage());
132 if (it
!= mpSdCustomShow
->PagesVector().end())
133 mpSdCustomShow
->PagesVector().erase(it
);
139 mpModel
->SetModified();
143 void SAL_CALL
SdXCustomPresentation::replaceByIndex( sal_Int32 Index
, const uno::Any
& Element
)
145 removeByIndex( Index
);
146 insertByIndex( Index
, Element
);
150 uno::Type SAL_CALL
SdXCustomPresentation::getElementType()
152 return cppu::UnoType
<drawing::XDrawPage
>::get();
155 sal_Bool SAL_CALL
SdXCustomPresentation::hasElements()
157 SolarMutexGuard aGuard
;
160 throw lang::DisposedException();
162 return getCount() > 0;
166 sal_Int32 SAL_CALL
SdXCustomPresentation::getCount()
168 SolarMutexGuard aGuard
;
170 throw lang::DisposedException();
172 return mpSdCustomShow
? mpSdCustomShow
->PagesVector().size() : 0;
175 uno::Any SAL_CALL
SdXCustomPresentation::getByIndex( sal_Int32 Index
)
177 SolarMutexGuard aGuard
;
180 throw lang::DisposedException();
182 if (Index
< 0 || !mpSdCustomShow
|| o3tl::make_unsigned(Index
) >= mpSdCustomShow
->PagesVector().size())
183 throw lang::IndexOutOfBoundsException();
186 SdrPage
* pPage
= const_cast<SdPage
*>(mpSdCustomShow
->PagesVector()[Index
]);
190 uno::Reference
< drawing::XDrawPage
> xRef( pPage
->getUnoPage(), uno::UNO_QUERY
);
198 OUString SAL_CALL
SdXCustomPresentation::getName()
200 SolarMutexGuard aGuard
;
203 throw lang::DisposedException();
206 return mpSdCustomShow
->GetName();
211 void SAL_CALL
SdXCustomPresentation::setName( const OUString
& aName
)
213 SolarMutexGuard aGuard
;
216 throw lang::DisposedException();
219 mpSdCustomShow
->SetName( aName
);
223 void SAL_CALL
SdXCustomPresentation::dispose()
225 SolarMutexGuard aGuard
;
228 return; // caught a recursion
232 uno::Reference
< uno::XInterface
> xSource( static_cast<cppu::OWeakObject
*>(this) );
234 std::unique_lock
aGuard2(aDisposeContainerMutex
);
235 lang::EventObject aEvt
;
236 aEvt
.Source
= std::move(xSource
);
237 aDisposeListeners
.disposeAndClear(aGuard2
, aEvt
);
239 mpSdCustomShow
= nullptr;
242 void SAL_CALL
SdXCustomPresentation::addEventListener( const uno::Reference
< lang::XEventListener
>& xListener
)
245 throw lang::DisposedException();
247 std::unique_lock
aGuard(aDisposeContainerMutex
);
248 aDisposeListeners
.addInterface(aGuard
, xListener
);
251 void SAL_CALL
SdXCustomPresentation::removeEventListener( const uno::Reference
< lang::XEventListener
>& aListener
)
255 std::unique_lock
aGuard(aDisposeContainerMutex
);
256 aDisposeListeners
.removeInterface(aGuard
, aListener
);
260 /*===========================================================================*
261 * class SdXCustomPresentationAccess : public XCustomPresentationAccess, *
263 *===========================================================================*/
265 SdXCustomPresentationAccess::SdXCustomPresentationAccess(SdXImpressDocument
& rMyModel
) noexcept
270 SdXCustomPresentationAccess::~SdXCustomPresentationAccess() noexcept
275 OUString SAL_CALL
SdXCustomPresentationAccess::getImplementationName()
277 return u
"SdXCustomPresentationAccess"_ustr
;
280 sal_Bool SAL_CALL
SdXCustomPresentationAccess::supportsService( const OUString
& ServiceName
)
282 return cppu::supportsService( this, ServiceName
);
285 uno::Sequence
< OUString
> SAL_CALL
SdXCustomPresentationAccess::getSupportedServiceNames()
287 return { u
"com.sun.star.presentation.CustomPresentationAccess"_ustr
};
290 // XSingleServiceFactory
291 uno::Reference
< uno::XInterface
> SAL_CALL
SdXCustomPresentationAccess::createInstance()
293 uno::Reference
< uno::XInterface
> xRef( static_cast<cppu::OWeakObject
*>(new SdXCustomPresentation()) );
297 uno::Reference
< uno::XInterface
> SAL_CALL
SdXCustomPresentationAccess::createInstanceWithArguments( const uno::Sequence
< uno::Any
>& )
299 return createInstance();
303 void SAL_CALL
SdXCustomPresentationAccess::insertByName( const OUString
& aName
, const uno::Any
& aElement
)
305 SolarMutexGuard aGuard
;
307 // get the documents custom show list
308 SdCustomShowList
* pList
= nullptr;
310 pList
= mrModel
.GetDoc()->GetCustomShowList(true);
312 // no list, no cookies
313 if( nullptr == pList
)
314 throw uno::RuntimeException();
316 // do we have a container::XIndexContainer?
317 SdXCustomPresentation
* pXShow
= nullptr;
319 uno::Reference
< container::XIndexContainer
> xContainer
;
320 if( (aElement
>>= xContainer
) && xContainer
.is() )
321 pXShow
= dynamic_cast<SdXCustomPresentation
*>(xContainer
.get());
323 if( nullptr == pXShow
)
324 throw lang::IllegalArgumentException();
326 // get the internal custom show from the api wrapper
327 SdCustomShow
* pShow
= pXShow
->GetSdCustomShow();
328 if( nullptr == pShow
)
330 pShow
= new SdCustomShow( xContainer
);
331 pXShow
->SetSdCustomShow( pShow
);
335 if( nullptr == pXShow
->GetModel() || *pXShow
->GetModel() != mrModel
)
336 throw lang::IllegalArgumentException();
340 pShow
->SetName( aName
);
342 // check if this or another customshow with the same name already exists
343 for( SdCustomShow
* pCompare
= pList
->First();
345 pCompare
= pList
->Next() )
347 if( pCompare
== pShow
|| pCompare
->GetName() == pShow
->GetName() )
348 throw container::ElementExistException();
351 pList
->push_back(std::unique_ptr
<SdCustomShow
>(pShow
));
353 mrModel
.SetModified();
356 void SAL_CALL
SdXCustomPresentationAccess::removeByName( const OUString
& Name
)
358 SolarMutexGuard aGuard
;
360 SdCustomShow
* pShow
= getSdCustomShow(Name
);
362 SdCustomShowList
* pList
= GetCustomShowList();
364 throw container::NoSuchElementException();
366 pList
->erase( pShow
);
368 mrModel
.SetModified();
372 void SAL_CALL
SdXCustomPresentationAccess::replaceByName( const OUString
& aName
, const uno::Any
& aElement
)
374 removeByName( aName
);
375 insertByName( aName
, aElement
);
379 uno::Any SAL_CALL
SdXCustomPresentationAccess::getByName( const OUString
& aName
)
381 SolarMutexGuard aGuard
;
383 SdCustomShow
* pShow
= getSdCustomShow(aName
);
386 throw container::NoSuchElementException();
389 uno::Reference
< container::XIndexContainer
> xRef( pShow
->getUnoCustomShow(), uno::UNO_QUERY
);
390 return uno::Any(xRef
);
393 uno::Sequence
< OUString
> SAL_CALL
SdXCustomPresentationAccess::getElementNames()
395 SolarMutexGuard aGuard
;
397 SdCustomShowList
* pList
= GetCustomShowList();
398 const sal_uInt32 nCount
= pList
? pList
->size() : 0;
400 uno::Sequence
< OUString
> aSequence( nCount
);
401 OUString
* pStringList
= aSequence
.getArray();
404 while( nIdx
< nCount
)
406 const SdCustomShow
* pShow
= (*pList
)[nIdx
].get();
407 pStringList
[nIdx
] = pShow
->GetName();
414 sal_Bool SAL_CALL
SdXCustomPresentationAccess::hasByName( const OUString
& aName
)
416 SolarMutexGuard aGuard
;
417 return getSdCustomShow(aName
) != nullptr;
421 uno::Type SAL_CALL
SdXCustomPresentationAccess::getElementType()
423 return cppu::UnoType
<container::XIndexContainer
>::get();
426 sal_Bool SAL_CALL
SdXCustomPresentationAccess::hasElements()
428 SolarMutexGuard aGuard
;
430 SdCustomShowList
* pList
= GetCustomShowList();
431 return pList
&& !pList
->empty();
434 SdCustomShow
* SdXCustomPresentationAccess::getSdCustomShow( std::u16string_view rName
) const noexcept
438 SdCustomShowList
* pList
= GetCustomShowList();
439 const sal_uInt32 nCount
= pList
? pList
->size() : 0;
441 while( nIdx
< nCount
)
443 SdCustomShow
* pShow
= (*pList
)[nIdx
].get();
444 if( pShow
->GetName() == rName
)
451 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */