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