xref: /trunk/main/sd/source/ui/unoidl/unocpres.cxx (revision 78190a370f7d7129fed9a7e70ca122eaae71ce1d)
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 
44 uno::Reference< uno::XInterface > createUnoCustomShow( SdCustomShow* pShow )
45 {
46     return (cppu::OWeakObject*)new SdXCustomPresentation( pShow, NULL );
47 }
48 
49 SdXCustomPresentation::SdXCustomPresentation() throw()
50 :   mpSdCustomShow(NULL), mpModel(NULL),
51     aDisposeListeners( aDisposeContainerMutex ),
52     bDisposing( sal_False )
53 {
54 }
55 
56 SdXCustomPresentation::SdXCustomPresentation( SdCustomShow* pShow, SdXImpressDocument* pMyModel) throw()
57 :   mpSdCustomShow(pShow), mpModel(pMyModel),
58     aDisposeListeners( aDisposeContainerMutex ),
59     bDisposing( sal_False )
60 {
61 }
62 
63 SdXCustomPresentation::~SdXCustomPresentation() throw()
64 {
65 }
66 
67 UNO3_GETIMPLEMENTATION_IMPL( SdXCustomPresentation );
68 
69 // XServiceInfo
70 OUString SAL_CALL SdXCustomPresentation::getImplementationName()
71     throw(uno::RuntimeException)
72 {
73     return OUString( RTL_CONSTASCII_USTRINGPARAM("SdXCustomPresentation") );
74 }
75 
76 sal_Bool SAL_CALL SdXCustomPresentation::supportsService( const OUString& ServiceName )
77     throw(uno::RuntimeException)
78 {
79     return comphelper::ServiceInfoHelper::supportsService( ServiceName, getSupportedServiceNames() );
80 }
81 
82 uno::Sequence< OUString > SAL_CALL SdXCustomPresentation::getSupportedServiceNames()
83     throw(uno::RuntimeException)
84 {
85     OUString aSN( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.presentation.CustomPresentation") );
86     uno::Sequence< OUString > aSeq( &aSN, 1 );
87     return aSeq;
88 }
89 
90 // XIndexContainer
91 void SAL_CALL SdXCustomPresentation::insertByIndex( sal_Int32 Index, const uno::Any& Element )
92     throw(lang::IllegalArgumentException, lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException)
93 {
94     OGuard aGuard( Application::GetSolarMutex() );
95 
96     if( bDisposing )
97         throw lang::DisposedException();
98 
99     if( Index < 0 || Index > (sal_Int32)( mpSdCustomShow ? mpSdCustomShow->Count() : 0 ) )
100         throw lang::IndexOutOfBoundsException();
101 
102     uno::Reference< drawing::XDrawPage > xPage;
103     Element >>= xPage;
104 
105     if(!xPage.is())
106         throw lang::IllegalArgumentException();
107 
108     SdDrawPage* pPage = SdDrawPage::getImplementation( xPage );
109 
110     if(pPage)
111     {
112         if( NULL == mpModel )
113             mpModel = pPage->GetModel();
114 
115         if( NULL != mpModel && NULL == mpSdCustomShow && mpModel->GetDoc() )
116             mpSdCustomShow = new SdCustomShow( mpModel->GetDoc() );
117 
118         mpSdCustomShow->Insert(pPage->GetSdrPage(), Index);
119     }
120 
121     if( mpModel )
122         mpModel->SetModified();
123 }
124 
125 void SAL_CALL SdXCustomPresentation::removeByIndex( sal_Int32 Index )
126     throw(lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException)
127 {
128     OGuard aGuard( Application::GetSolarMutex() );
129 
130     if( bDisposing )
131         throw lang::DisposedException();
132 
133     if(mpSdCustomShow)
134     {
135         uno::Reference< drawing::XDrawPage > xPage;
136         getByIndex( Index ) >>= xPage;
137 
138         if( xPage.is() )
139         {
140             SvxDrawPage* pPage = SvxDrawPage::getImplementation(  xPage );
141             if(pPage)
142                 mpSdCustomShow->Remove(pPage->GetSdrPage());
143         }
144     }
145 
146     if( mpModel )
147         mpModel->SetModified();
148 }
149 
150 // XIndexReplace
151 void SAL_CALL SdXCustomPresentation::replaceByIndex( sal_Int32 Index, const uno::Any& Element )
152     throw(lang::IllegalArgumentException, lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException)
153 {
154     removeByIndex( Index );
155     insertByIndex( Index, Element );
156 }
157 
158 // XElementAccess
159 uno::Type SAL_CALL SdXCustomPresentation::getElementType()
160     throw(uno::RuntimeException)
161 {
162     return ITYPE( drawing::XDrawPage );
163 }
164 
165 sal_Bool SAL_CALL SdXCustomPresentation::hasElements()
166     throw(uno::RuntimeException)
167 {
168     OGuard aGuard( Application::GetSolarMutex() );
169 
170     if( bDisposing )
171         throw lang::DisposedException();
172 
173     return getCount() > 0;
174 }
175 
176 // XIndexAccess
177 sal_Int32 SAL_CALL SdXCustomPresentation::getCount()
178     throw(uno::RuntimeException)
179 {
180     OGuard aGuard( Application::GetSolarMutex() );
181     if( bDisposing )
182         throw lang::DisposedException();
183 
184     return mpSdCustomShow?mpSdCustomShow->Count():0;
185 }
186 
187 uno::Any SAL_CALL SdXCustomPresentation::getByIndex( sal_Int32 Index )
188     throw(lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException)
189 {
190     OGuard aGuard( Application::GetSolarMutex() );
191 
192     if( bDisposing )
193         throw lang::DisposedException();
194 
195     if( Index < 0 || Index >= (sal_Int32)mpSdCustomShow->Count() )
196         throw lang::IndexOutOfBoundsException();
197 
198     uno::Any aAny;
199     if(mpSdCustomShow )
200     {
201         SdrPage* pPage = (SdrPage*)mpSdCustomShow->GetObject(Index);
202 
203         if( pPage )
204         {
205             uno::Reference< drawing::XDrawPage > xRef( pPage->getUnoPage(), uno::UNO_QUERY );
206             aAny <<= xRef;
207         }
208     }
209 
210     return aAny;
211 }
212 
213 // XNamed
214 OUString SAL_CALL SdXCustomPresentation::getName()
215     throw(uno::RuntimeException)
216 {
217     OGuard aGuard( Application::GetSolarMutex() );
218 
219     if( bDisposing )
220         throw lang::DisposedException();
221 
222     if(mpSdCustomShow)
223         return mpSdCustomShow->GetName();
224 
225     return OUString();
226 }
227 
228 void SAL_CALL SdXCustomPresentation::setName( const OUString& aName )
229     throw(uno::RuntimeException)
230 {
231     OGuard aGuard( Application::GetSolarMutex() );
232 
233     if( bDisposing )
234         throw lang::DisposedException();
235 
236     if(mpSdCustomShow)
237         mpSdCustomShow->SetName( aName );
238 }
239 
240 // XComponent
241 void SAL_CALL SdXCustomPresentation::dispose() throw(uno::RuntimeException)
242 {
243     OGuard aGuard( Application::GetSolarMutex() );
244 
245     if( bDisposing )
246         return; // caught a recursion
247 
248     bDisposing = sal_True;
249 
250     uno::Reference< uno::XInterface > xSource( (cppu::OWeakObject*)this );
251 
252     lang::EventObject aEvt;
253     aEvt.Source = xSource;
254     aDisposeListeners.disposeAndClear(aEvt);
255 
256     mpSdCustomShow = NULL;
257 }
258 
259 //----------------------------------------------------------------------
260 void SAL_CALL SdXCustomPresentation::addEventListener( const uno::Reference< lang::XEventListener >& xListener )
261     throw(uno::RuntimeException)
262 {
263     if( bDisposing )
264         throw lang::DisposedException();
265 
266     aDisposeListeners.addInterface(xListener);
267 }
268 
269 //----------------------------------------------------------------------
270 void SAL_CALL SdXCustomPresentation::removeEventListener( const uno::Reference< lang::XEventListener >& aListener ) throw(uno::RuntimeException)
271 {
272     if( !bDisposing )
273         aDisposeListeners.removeInterface(aListener);
274 }
275 
276 /*===========================================================================*
277  *  class SdXCustomPresentationAccess : public XCustomPresentationAccess,    *
278  *                                      public UsrObject                     *
279  *===========================================================================*/
280 
281 SdXCustomPresentationAccess::SdXCustomPresentationAccess(SdXImpressDocument& rMyModel) throw()
282 : mrModel(rMyModel)
283 {
284 }
285 
286 SdXCustomPresentationAccess::~SdXCustomPresentationAccess() throw()
287 {
288 }
289 
290 // XServiceInfo
291 OUString SAL_CALL SdXCustomPresentationAccess::getImplementationName()
292     throw(uno::RuntimeException)
293 {
294     return OUString( RTL_CONSTASCII_USTRINGPARAM("SdXCustomPresentationAccess") );
295 }
296 
297 sal_Bool SAL_CALL SdXCustomPresentationAccess::supportsService( const OUString& ServiceName )
298     throw(uno::RuntimeException)
299 {
300     return comphelper::ServiceInfoHelper::supportsService( ServiceName, getSupportedServiceNames() );
301 }
302 
303 uno::Sequence< OUString > SAL_CALL SdXCustomPresentationAccess::getSupportedServiceNames()
304     throw(uno::RuntimeException)
305 {
306     const OUString aNS( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.presentation.CustomPresentationAccess") );
307     uno::Sequence< OUString > aSeq( &aNS, 1 );
308     return aSeq;
309 }
310 
311 // XSingleServiceFactory
312 uno::Reference< uno::XInterface > SAL_CALL SdXCustomPresentationAccess::createInstance()
313     throw(uno::Exception, uno::RuntimeException)
314 {
315     uno::Reference< uno::XInterface >  xRef( (::cppu::OWeakObject*)new SdXCustomPresentation() );
316     return xRef;
317 }
318 
319 uno::Reference< uno::XInterface > SAL_CALL SdXCustomPresentationAccess::createInstanceWithArguments( const uno::Sequence< uno::Any >& )
320     throw(uno::Exception, uno::RuntimeException)
321 {
322     return createInstance();
323 }
324 
325 // XNameContainer
326 void SAL_CALL SdXCustomPresentationAccess::insertByName( const OUString& aName, const uno::Any& aElement )
327     throw(lang::IllegalArgumentException, container::ElementExistException, lang::WrappedTargetException, uno::RuntimeException)
328 {
329     OGuard aGuard( Application::GetSolarMutex() );
330 
331     // get the documents custom show list
332     List* pList = 0;
333     if(mrModel.GetDoc())
334         pList = mrModel.GetDoc()->GetCustomShowList(sal_True);
335 
336     // no list, no cookies
337     if( NULL == pList)
338         throw uno::RuntimeException();
339 
340     // do we have an container::XIndexContainer?
341     SdXCustomPresentation* pXShow = NULL;
342 
343     uno::Reference< container::XIndexContainer > xContainer;
344     if( (aElement >>= xContainer) && xContainer.is() )
345         pXShow = SdXCustomPresentation::getImplementation(xContainer);
346 
347     if( NULL == pXShow )
348         throw lang::IllegalArgumentException();
349 
350     // get the internal custom show from the api wrapper
351     SdCustomShow* pShow = pXShow->GetSdCustomShow();
352     if( NULL == pShow )
353     {
354         pShow = new SdCustomShow( mrModel.GetDoc(), xContainer );
355         pXShow->SetSdCustomShow( pShow );
356     }
357     else
358     {
359         if( NULL == pXShow->GetModel() || *pXShow->GetModel() != mrModel )
360             throw lang::IllegalArgumentException();
361     }
362 
363     // give it a name
364     pShow->SetName( aName);
365 
366     // check if this or another customshow with the same name already exists
367     for( SdCustomShow* pCompare = (SdCustomShow*)pList->First();
368          pCompare;
369          pCompare = (SdCustomShow*)pList->Next() )
370     {
371         if( pCompare == pShow || pCompare->GetName() == pShow->GetName() )
372             throw container::ElementExistException();
373     }
374 
375     pList->Insert(pShow);
376 
377     mrModel.SetModified();
378 }
379 
380 void SAL_CALL SdXCustomPresentationAccess::removeByName( const OUString& Name )
381     throw(container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
382 {
383     OGuard aGuard( Application::GetSolarMutex() );
384 
385     SdCustomShow* pShow = getSdCustomShow(Name);
386 
387     List* pList = GetCustomShowList();
388     if(pList && pShow)
389         delete (SdCustomShow*)pList->Remove( pShow );
390     else
391         throw container::NoSuchElementException();
392 
393     mrModel.SetModified();
394 }
395 
396 // XNameReplace
397 void SAL_CALL SdXCustomPresentationAccess::replaceByName( const OUString& aName, const uno::Any& aElement )
398     throw(lang::IllegalArgumentException, container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
399 {
400     removeByName( aName );
401     insertByName( aName, aElement );
402 }
403 
404 // XNameAccess
405 uno::Any SAL_CALL SdXCustomPresentationAccess::getByName( const OUString& aName )
406     throw(container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
407 {
408     OGuard aGuard( Application::GetSolarMutex() );
409 
410     uno::Any aAny;
411 
412     SdCustomShow* pShow = getSdCustomShow(aName);
413     if(pShow)
414     {
415         uno::Reference< container::XIndexContainer >  xRef( pShow->getUnoCustomShow(), uno::UNO_QUERY );
416         aAny <<= xRef;
417     }
418     else
419     {
420         throw container::NoSuchElementException();
421     }
422 
423     return aAny;
424 }
425 
426 uno::Sequence< OUString > SAL_CALL SdXCustomPresentationAccess::getElementNames()
427     throw(uno::RuntimeException)
428 {
429     OGuard aGuard( Application::GetSolarMutex() );
430 
431     List* pList = GetCustomShowList();
432     const sal_uInt32 nCount = pList?pList->Count():0;
433 
434     uno::Sequence< OUString > aSequence( nCount );
435     OUString* pStringList = aSequence.getArray();
436 
437     sal_uInt32 nIdx = 0;
438     while( nIdx < nCount )
439     {
440         const SdCustomShow* pShow = (const SdCustomShow*)pList->GetObject(nIdx);
441         pStringList[nIdx] = pShow->GetName();
442         nIdx++;
443     }
444 
445     return aSequence;
446 }
447 
448 
449 sal_Bool SAL_CALL SdXCustomPresentationAccess::hasByName( const OUString& aName )
450     throw(uno::RuntimeException)
451 {
452     OGuard aGuard( Application::GetSolarMutex() );
453     return getSdCustomShow(aName) != NULL;
454 }
455 
456 // XElementAccess
457 uno::Type SAL_CALL SdXCustomPresentationAccess::getElementType()
458     throw(uno::RuntimeException)
459 {
460     return ITYPE( container::XIndexContainer );
461 }
462 
463 sal_Bool SAL_CALL SdXCustomPresentationAccess::hasElements()
464     throw(uno::RuntimeException)
465 {
466     OGuard aGuard( Application::GetSolarMutex() );
467 
468     List* pList = GetCustomShowList();
469     return pList && pList->Count() > 0;
470 }
471 
472 SdCustomShow * SdXCustomPresentationAccess::getSdCustomShow( const OUString& Name ) const throw()
473 {
474     sal_uInt32 nIdx = 0;
475 
476     List* pList = GetCustomShowList();
477     const sal_uInt32 nCount = pList?pList->Count():0;
478 
479     const String aName( Name );
480 
481     while( nIdx < nCount )
482     {
483         SdCustomShow* pShow = (SdCustomShow*)pList->GetObject(nIdx);
484         if( pShow->GetName() == aName )
485             return pShow;
486         nIdx++;
487     }
488     return NULL;
489 }
490 
491 /* vim: set noet sw=4 ts=4: */
492