LibreOffice Module sd (master) 1
unocpres.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <algorithm>
21
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>
28
30#include <unomodel.hxx>
31#include <drawdoc.hxx>
32#include "unocpres.hxx"
33#include <cusshow.hxx>
34#include <unopage.hxx>
35#include <customshowlist.hxx>
36
37using namespace ::com::sun::star;
38
39uno::Reference< uno::XInterface > createUnoCustomShow( SdCustomShow* pShow )
40{
41 return static_cast<cppu::OWeakObject*>(new SdXCustomPresentation( pShow ));
42}
43
45: mpSdCustomShow(nullptr), mpModel(nullptr),
46 bDisposing( false )
47{
48}
49
51: mpSdCustomShow(pShow), mpModel(nullptr),
52 bDisposing( false )
53{
54}
55
57{
58}
59
60// XServiceInfo
62{
63 return "SdXCustomPresentation" ;
64}
65
66sal_Bool SAL_CALL SdXCustomPresentation::supportsService( const OUString& ServiceName )
67{
68 return cppu::supportsService( this, ServiceName );
69}
70
71uno::Sequence< OUString > SAL_CALL SdXCustomPresentation::getSupportedServiceNames()
72{
73 return { "com.sun.star.presentation.CustomPresentation" };
74}
75
76// XIndexContainer
77void SAL_CALL SdXCustomPresentation::insertByIndex( sal_Int32 Index, const uno::Any& Element )
78{
79 SolarMutexGuard aGuard;
80
81 if( bDisposing )
82 throw lang::DisposedException();
83
85 throw lang::IndexOutOfBoundsException();
86
87 uno::Reference< drawing::XDrawPage > xPage;
88 Element >>= xPage;
89
90 if(!xPage.is())
91 throw lang::IllegalArgumentException();
92
93 SdDrawPage* pPage = comphelper::getFromUnoTunnel<SdDrawPage>( xPage );
94
95 if(pPage)
96 {
97 if (!mpModel)
98 mpModel = pPage->GetModel();
99
100 if (!mpSdCustomShow)
102
104 static_cast<SdPage*>(pPage->GetSdrPage()));
105 }
106
107 if( mpModel )
109}
110
112{
113 SolarMutexGuard aGuard;
114
115 if( bDisposing )
116 throw lang::DisposedException();
117
119 {
120 uno::Reference< drawing::XDrawPage > xPage;
121 getByIndex( Index ) >>= xPage;
122
123 if( xPage.is() )
124 {
125 SvxDrawPage* pPage = comphelper::getFromUnoTunnel<SvxDrawPage>( xPage );
126 if(pPage)
127 {
128 SdCustomShow::PageVec::iterator it = std::find(
129 mpSdCustomShow->PagesVector().begin(),
131 pPage->GetSdrPage());
132 if (it != mpSdCustomShow->PagesVector().end())
133 mpSdCustomShow->PagesVector().erase(it);
134 }
135 }
136 }
137
138 if( mpModel )
140}
141
142// XIndexReplace
143void SAL_CALL SdXCustomPresentation::replaceByIndex( sal_Int32 Index, const uno::Any& Element )
144{
146 insertByIndex( Index, Element );
147}
148
149// XElementAccess
151{
153}
154
156{
157 SolarMutexGuard aGuard;
158
159 if( bDisposing )
160 throw lang::DisposedException();
161
162 return getCount() > 0;
163}
164
165// XIndexAccess
167{
168 SolarMutexGuard aGuard;
169 if( bDisposing )
170 throw lang::DisposedException();
171
172 return mpSdCustomShow ? mpSdCustomShow->PagesVector().size() : 0;
173}
174
176{
177 SolarMutexGuard aGuard;
178
179 if( bDisposing )
180 throw lang::DisposedException();
181
183 throw lang::IndexOutOfBoundsException();
184
185 uno::Any aAny;
186 SdrPage * pPage = const_cast<SdPage *>(mpSdCustomShow->PagesVector()[Index]);
187
188 if( pPage )
189 {
190 uno::Reference< drawing::XDrawPage > xRef( pPage->getUnoPage(), uno::UNO_QUERY );
191 aAny <<= xRef;
192 }
193
194 return aAny;
195}
196
197// XNamed
199{
200 SolarMutexGuard aGuard;
201
202 if( bDisposing )
203 throw lang::DisposedException();
204
206 return mpSdCustomShow->GetName();
207
208 return OUString();
209}
210
211void SAL_CALL SdXCustomPresentation::setName( const OUString& aName )
212{
213 SolarMutexGuard aGuard;
214
215 if( bDisposing )
216 throw lang::DisposedException();
217
220}
221
222// XComponent
224{
225 SolarMutexGuard aGuard;
226
227 if( bDisposing )
228 return; // caught a recursion
229
230 bDisposing = true;
231
232 uno::Reference< uno::XInterface > xSource( static_cast<cppu::OWeakObject*>(this) );
233
234 std::unique_lock aGuard2(aDisposeContainerMutex);
235 lang::EventObject aEvt;
236 aEvt.Source = xSource;
237 aDisposeListeners.disposeAndClear(aGuard2, aEvt);
238
239 mpSdCustomShow = nullptr;
240}
241
242void SAL_CALL SdXCustomPresentation::addEventListener( const uno::Reference< lang::XEventListener >& xListener )
243{
244 if( bDisposing )
245 throw lang::DisposedException();
246
247 std::unique_lock aGuard(aDisposeContainerMutex);
248 aDisposeListeners.addInterface(aGuard, xListener);
249}
250
251void SAL_CALL SdXCustomPresentation::removeEventListener( const uno::Reference< lang::XEventListener >& aListener )
252{
253 if( !bDisposing )
254 {
255 std::unique_lock aGuard(aDisposeContainerMutex);
256 aDisposeListeners.removeInterface(aGuard, aListener);
257 }
258}
259
260/*===========================================================================*
261 * class SdXCustomPresentationAccess : public XCustomPresentationAccess, *
262 * public UsrObject *
263 *===========================================================================*/
264
266: mrModel(rMyModel)
267{
268}
269
271{
272}
273
274// XServiceInfo
276{
277 return "SdXCustomPresentationAccess";
278}
279
280sal_Bool SAL_CALL SdXCustomPresentationAccess::supportsService( const OUString& ServiceName )
281{
282 return cppu::supportsService( this, ServiceName );
283}
284
286{
287 return { "com.sun.star.presentation.CustomPresentationAccess" };
288}
289
290// XSingleServiceFactory
291uno::Reference< uno::XInterface > SAL_CALL SdXCustomPresentationAccess::createInstance()
292{
293 uno::Reference< uno::XInterface > xRef( static_cast<cppu::OWeakObject*>(new SdXCustomPresentation()) );
294 return xRef;
295}
296
297uno::Reference< uno::XInterface > SAL_CALL SdXCustomPresentationAccess::createInstanceWithArguments( const uno::Sequence< uno::Any >& )
298{
299 return createInstance();
300}
301
302// XNameContainer
303void SAL_CALL SdXCustomPresentationAccess::insertByName( const OUString& aName, const uno::Any& aElement )
304{
305 SolarMutexGuard aGuard;
306
307 // get the documents custom show list
308 SdCustomShowList* pList = nullptr;
309 if(mrModel.GetDoc())
310 pList = mrModel.GetDoc()->GetCustomShowList(true);
311
312 // no list, no cookies
313 if( nullptr == pList)
314 throw uno::RuntimeException();
315
316 // do we have a container::XIndexContainer?
317 SdXCustomPresentation* pXShow = nullptr;
318
319 uno::Reference< container::XIndexContainer > xContainer;
320 if( (aElement >>= xContainer) && xContainer.is() )
321 pXShow = dynamic_cast<SdXCustomPresentation*>(xContainer.get());
322
323 if( nullptr == pXShow )
324 throw lang::IllegalArgumentException();
325
326 // get the internal custom show from the api wrapper
327 SdCustomShow* pShow = pXShow->GetSdCustomShow();
328 if( nullptr == pShow )
329 {
330 pShow = new SdCustomShow( xContainer );
331 pXShow->SetSdCustomShow( pShow );
332 }
333 else
334 {
335 if( nullptr == pXShow->GetModel() || *pXShow->GetModel() != mrModel )
336 throw lang::IllegalArgumentException();
337 }
338
339 // give it a name
340 pShow->SetName( aName);
341
342 // check if this or another customshow with the same name already exists
343 for( SdCustomShow* pCompare = pList->First();
344 pCompare;
345 pCompare = pList->Next() )
346 {
347 if( pCompare == pShow || pCompare->GetName() == pShow->GetName() )
348 throw container::ElementExistException();
349 }
350
351 pList->push_back(std::unique_ptr<SdCustomShow>(pShow));
352
354}
355
356void SAL_CALL SdXCustomPresentationAccess::removeByName( const OUString& Name )
357{
358 SolarMutexGuard aGuard;
359
361
363 if(!pList || !pShow)
364 throw container::NoSuchElementException();
365
366 pList->erase( pShow );
367
369}
370
371// XNameReplace
372void SAL_CALL SdXCustomPresentationAccess::replaceByName( const OUString& aName, const uno::Any& aElement )
373{
375 insertByName( aName, aElement );
376}
377
378// XNameAccess
379uno::Any SAL_CALL SdXCustomPresentationAccess::getByName( const OUString& aName )
380{
381 SolarMutexGuard aGuard;
382
384 if(!pShow)
385 {
386 throw container::NoSuchElementException();
387 }
388
389 uno::Reference< container::XIndexContainer > xRef( pShow->getUnoCustomShow(), uno::UNO_QUERY );
390 return uno::Any(xRef);
391}
392
393uno::Sequence< OUString > SAL_CALL SdXCustomPresentationAccess::getElementNames()
394{
395 SolarMutexGuard aGuard;
396
398 const sal_uInt32 nCount = pList ? pList->size() : 0;
399
400 uno::Sequence< OUString > aSequence( nCount );
401 OUString* pStringList = aSequence.getArray();
402
403 sal_uInt32 nIdx = 0;
404 while( nIdx < nCount )
405 {
406 const SdCustomShow* pShow = (*pList)[nIdx].get();
407 pStringList[nIdx] = pShow->GetName();
408 nIdx++;
409 }
410
411 return aSequence;
412}
413
414sal_Bool SAL_CALL SdXCustomPresentationAccess::hasByName( const OUString& aName )
415{
416 SolarMutexGuard aGuard;
417 return getSdCustomShow(aName) != nullptr;
418}
419
420// XElementAccess
422{
424}
425
427{
428 SolarMutexGuard aGuard;
429
431 return pList && !pList->empty();
432}
433
434SdCustomShow * SdXCustomPresentationAccess::getSdCustomShow( std::u16string_view rName ) const noexcept
435{
436 sal_uInt32 nIdx = 0;
437
438 SdCustomShowList* pList = GetCustomShowList();
439 const sal_uInt32 nCount = pList ? pList->size() : 0;
440
441 while( nIdx < nCount )
442 {
443 SdCustomShow* pShow = (*pList)[nIdx].get();
444 if( pShow->GetName() == rName )
445 return pShow;
446 nIdx++;
447 }
448 return nullptr;
449}
450
451/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
struct _ADOIndex Index
ChartModel & mrModel
bool empty() const
void push_back(std::unique_ptr< SdCustomShow > p)
SdCustomShow * Next()
void erase(std::vector< std::unique_ptr< SdCustomShow > >::iterator it)
Definition: cusshow.cxx:96
size_t size() const
SdCustomShow * First()
css::uno::Reference< css::uno::XInterface > getUnoCustomShow()
Definition: cusshow.cxx:66
const OUString & GetName() const
Definition: cusshow.hxx:60
void SetName(const OUString &rName)
Definition: cusshow.cxx:91
PageVec & PagesVector()
Provides a direct access to the collection of the SdPage objects.
Definition: cusshow.hxx:52
SdCustomShowList * GetCustomShowList(bool bCreate=false)
Definition: drawdoc3.cxx:1161
SdXImpressDocument * GetModel() const
Definition: unopage.cxx:365
SdCustomShow * getSdCustomShow(std::u16string_view Name) const noexcept
Definition: unocpres.cxx:434
SdXImpressDocument & mrModel
Definition: unocpres.hxx:98
virtual css::uno::Any SAL_CALL getByName(const OUString &aName) override
Definition: unocpres.cxx:379
virtual void SAL_CALL removeByName(const OUString &Name) override
Definition: unocpres.cxx:356
virtual ~SdXCustomPresentationAccess() noexcept override
Definition: unocpres.cxx:270
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Definition: unocpres.cxx:285
virtual OUString SAL_CALL getImplementationName() override
Definition: unocpres.cxx:275
SdXCustomPresentationAccess(SdXImpressDocument &rMyModel) noexcept
Definition: unocpres.cxx:265
virtual void SAL_CALL insertByName(const OUString &aName, const css::uno::Any &aElement) override
Definition: unocpres.cxx:303
virtual css::uno::Type SAL_CALL getElementType() override
Definition: unocpres.cxx:421
virtual css::uno::Sequence< OUString > SAL_CALL getElementNames() override
Definition: unocpres.cxx:393
virtual sal_Bool SAL_CALL hasElements() override
Definition: unocpres.cxx:426
virtual void SAL_CALL replaceByName(const OUString &aName, const css::uno::Any &aElement) override
Definition: unocpres.cxx:372
SdCustomShowList * GetCustomShowList() const noexcept
Definition: unocpres.hxx:134
virtual sal_Bool SAL_CALL hasByName(const OUString &aName) override
Definition: unocpres.cxx:414
virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstanceWithArguments(const css::uno::Sequence< css::uno::Any > &aArguments) override
Definition: unocpres.cxx:297
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
Definition: unocpres.cxx:280
virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstance() override
Definition: unocpres.cxx:291
virtual void SAL_CALL dispose() override
Definition: unocpres.cxx:223
::comphelper::OInterfaceContainerHelper4< css::lang::XEventListener > aDisposeListeners
Definition: unocpres.hxx:50
virtual ~SdXCustomPresentation() noexcept override
Definition: unocpres.cxx:56
virtual OUString SAL_CALL getImplementationName() override
Definition: unocpres.cxx:61
virtual css::uno::Type SAL_CALL getElementType() override
Definition: unocpres.cxx:150
SdXImpressDocument * GetModel() const noexcept
Definition: unocpres.hxx:61
SdXImpressDocument * mpModel
Definition: unocpres.hxx:46
SdCustomShow * mpSdCustomShow
Definition: unocpres.hxx:45
virtual void SAL_CALL setName(const OUString &aName) override
Definition: unocpres.cxx:211
virtual sal_Bool SAL_CALL hasElements() override
Definition: unocpres.cxx:155
virtual OUString SAL_CALL getName() override
Definition: unocpres.cxx:198
virtual void SAL_CALL removeByIndex(sal_Int32 Index) override
Definition: unocpres.cxx:111
SdCustomShow * GetSdCustomShow() const noexcept
Definition: unocpres.hxx:59
void SetSdCustomShow(SdCustomShow *pShow) noexcept
Definition: unocpres.hxx:60
std::mutex aDisposeContainerMutex
Definition: unocpres.hxx:49
SdXCustomPresentation() noexcept
Definition: unocpres.cxx:44
virtual sal_Int32 SAL_CALL getCount() override
Definition: unocpres.cxx:166
virtual css::uno::Any SAL_CALL getByIndex(sal_Int32 Index) override
Definition: unocpres.cxx:175
virtual void SAL_CALL addEventListener(const css::uno::Reference< css::lang::XEventListener > &xListener) override
Definition: unocpres.cxx:242
virtual void SAL_CALL replaceByIndex(sal_Int32 Index, const css::uno::Any &Element) override
Definition: unocpres.cxx:143
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Definition: unocpres.cxx:71
virtual void SAL_CALL insertByIndex(sal_Int32 Index, const css::uno::Any &Element) override
Definition: unocpres.cxx:77
virtual void SAL_CALL removeEventListener(const css::uno::Reference< css::lang::XEventListener > &aListener) override
Definition: unocpres.cxx:251
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
Definition: unocpres.cxx:66
SdDrawDocument * GetDoc() const
Definition: unomodel.hxx:141
void SetModified() noexcept
Definition: unomodel.cxx:593
css::uno::Reference< css::uno::XInterface > const & getUnoPage()
SdrPage * GetSdrPage() const
sal_Int32 addInterface(std::unique_lock< std::mutex > &rGuard, const css::uno::Reference< ListenerT > &rxIFace)
void disposeAndClear(::std::unique_lock<::std::mutex > &rGuard, const css::lang::EventObject &rEvt)
sal_Int32 removeInterface(std::unique_lock< std::mutex > &rGuard, const css::uno::Reference< ListenerT > &rxIFace)
css::uno::Type const & get()
int nCount
OUString aName
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
OUString Name
unsigned char sal_Bool
uno::Reference< uno::XInterface > createUnoCustomShow(SdCustomShow *pShow)
Definition: unocpres.cxx:39