Line data Source code
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 <com/sun/star/document/EventObject.hpp>
21 : #include <com/sun/star/drawing/XShapes.hpp>
22 : #include <com/sun/star/lang/XComponent.hpp>
23 : #include <com/sun/star/lang/XServiceInfo.hpp>
24 : #include <com/sun/star/uno/XComponentContext.hpp>
25 :
26 : #include <cppuhelper/implbase3.hxx>
27 : #include <cppuhelper/interfacecontainer.hxx>
28 : #include <cppuhelper/supportsservice.hxx>
29 : #include <osl/mutex.hxx>
30 : #include <rtl/ref.hxx>
31 : #include <svx/unoprov.hxx>
32 :
33 : using namespace ::com::sun::star;
34 : using namespace ::com::sun::star::uno;
35 :
36 : namespace {
37 :
38 180 : class SvxShapeCollectionMutex
39 : {
40 : public:
41 : ::osl::Mutex maMutex;
42 : };
43 :
44 : class SvxShapeCollection :
45 : public cppu::WeakAggImplHelper3<drawing::XShapes, lang::XServiceInfo, lang::XComponent>,
46 : public SvxShapeCollectionMutex
47 : {
48 : private:
49 : cppu::OInterfaceContainerHelper maShapeContainer;
50 :
51 : cppu::OBroadcastHelper mrBHelper;
52 :
53 : virtual void disposing() throw();
54 :
55 : public:
56 : SvxShapeCollection() throw();
57 : virtual ~SvxShapeCollection() throw();
58 :
59 : // XInterface
60 : virtual void SAL_CALL release() throw() SAL_OVERRIDE;
61 :
62 : // XComponent
63 : virtual void SAL_CALL dispose() throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
64 : virtual void SAL_CALL addEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener >& aListener ) throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
65 : virtual void SAL_CALL removeEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener >& aListener ) throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
66 :
67 : // XIndexAccess
68 : virtual sal_Int32 SAL_CALL getCount() throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE ;
69 : virtual ::com::sun::star::uno::Any SAL_CALL getByIndex( sal_Int32 Index ) throw(::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
70 :
71 : // XElementAccess
72 : virtual ::com::sun::star::uno::Type SAL_CALL getElementType() throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
73 : virtual sal_Bool SAL_CALL hasElements() throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
74 :
75 : // XShapes
76 : virtual void SAL_CALL add( const ::com::sun::star::uno::Reference< ::com::sun::star::drawing::XShape >& xShape ) throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
77 : virtual void SAL_CALL remove( const ::com::sun::star::uno::Reference< ::com::sun::star::drawing::XShape >& xShape ) throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
78 :
79 : // XServiceInfo
80 : virtual OUString SAL_CALL getImplementationName() throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
81 : virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
82 : virtual ::com::sun::star::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
83 : };
84 :
85 98 : SvxShapeCollection::SvxShapeCollection() throw()
86 98 : : maShapeContainer( maMutex ), mrBHelper( maMutex )
87 : {
88 98 : }
89 :
90 :
91 164 : SvxShapeCollection::~SvxShapeCollection() throw()
92 : {
93 164 : }
94 :
95 : // XInterface
96 886 : void SvxShapeCollection::release() throw()
97 : {
98 886 : uno::Reference< uno::XInterface > x( xDelegator );
99 886 : if (! x.is())
100 : {
101 886 : if (osl_atomic_decrement( &m_refCount ) == 0)
102 : {
103 162 : if (! mrBHelper.bDisposed)
104 : {
105 80 : uno::Reference< uno::XInterface > xHoldAlive( (uno::XWeak*)this );
106 : // First dispose
107 : try
108 : {
109 80 : dispose();
110 : }
111 0 : catch(::com::sun::star::uno::Exception&)
112 : {
113 : // release should not throw exceptions
114 : }
115 :
116 : // only the alive ref holds the object
117 : OSL_ASSERT( m_refCount == 1 );
118 : // destroy the object if xHoldAlive decrement the refcount to 0
119 966 : return;
120 : }
121 : }
122 : // restore the reference count
123 806 : osl_atomic_increment( &m_refCount );
124 : }
125 806 : OWeakAggObject::release();
126 : }
127 :
128 : // XComponent
129 82 : void SvxShapeCollection::disposing() throw()
130 : {
131 82 : maShapeContainer.clear();
132 82 : }
133 :
134 : // XComponent
135 82 : void SvxShapeCollection::dispose()
136 : throw(::com::sun::star::uno::RuntimeException, std::exception)
137 : {
138 : // An frequently programming error is to release the last
139 : // reference to this object in the disposing message.
140 : // Make it rubust, hold a self Reference.
141 82 : uno::Reference< lang::XComponent > xSelf( this );
142 :
143 : // Guard dispose against multible threading
144 : // Remark: It is an error to call dispose more than once
145 82 : bool bDoDispose = false;
146 : {
147 82 : osl::MutexGuard aGuard( mrBHelper.rMutex );
148 82 : if( !mrBHelper.bDisposed && !mrBHelper.bInDispose )
149 : {
150 : // only one call go into this section
151 82 : mrBHelper.bInDispose = sal_True;
152 82 : bDoDispose = true;
153 82 : }
154 : }
155 :
156 : // Do not hold the mutex because we are broadcasting
157 82 : if( bDoDispose )
158 : {
159 : // Create an event with this as sender
160 : try
161 : {
162 82 : uno::Reference< uno::XInterface > xSource( uno::Reference< uno::XInterface >::query( (lang::XComponent *)this ) );
163 164 : document::EventObject aEvt;
164 82 : aEvt.Source = xSource;
165 : // inform all listeners to release this object
166 : // The listener container are automatically cleared
167 82 : mrBHelper.aLC.disposeAndClear( aEvt );
168 : // notify subclasses to do their dispose
169 164 : disposing();
170 : }
171 0 : catch(const ::com::sun::star::uno::Exception&)
172 : {
173 : // catch exception and throw again but signal that
174 : // the object was disposed. Dispose should be called
175 : // only once.
176 0 : mrBHelper.bDisposed = sal_True;
177 0 : mrBHelper.bInDispose = sal_False;
178 0 : throw;
179 : }
180 :
181 : // the values bDispose and bInDisposing must set in this order.
182 : // No multithread call overcome the "!rBHelper.bDisposed && !rBHelper.bInDispose" guard.
183 82 : mrBHelper.bDisposed = sal_True;
184 82 : mrBHelper.bInDispose = sal_False;
185 : }
186 : else
187 : {
188 : // in a multithreaded environment, it can't be avoided, that dispose is called twice.
189 : // However this condition is traced, because it MAY indicate an error.
190 : OSL_TRACE( "OComponentHelper::dispose() - dispose called twice" );
191 82 : }
192 82 : }
193 :
194 : // XComponent
195 30 : void SAL_CALL SvxShapeCollection::addEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener >& aListener ) throw(::com::sun::star::uno::RuntimeException, std::exception)
196 : {
197 30 : mrBHelper.addListener( ::getCppuType( &aListener ) , aListener );
198 30 : }
199 :
200 : // XComponent
201 0 : void SAL_CALL SvxShapeCollection::removeEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener >& aListener ) throw(::com::sun::star::uno::RuntimeException, std::exception)
202 : {
203 0 : mrBHelper.removeListener( ::getCppuType( &aListener ) , aListener );
204 0 : }
205 :
206 : // XShapes
207 :
208 290 : void SAL_CALL SvxShapeCollection::add( const Reference< drawing::XShape >& xShape ) throw( uno::RuntimeException, std::exception )
209 : {
210 290 : maShapeContainer.addInterface( xShape );
211 290 : }
212 :
213 :
214 2 : void SAL_CALL SvxShapeCollection::remove( const uno::Reference< drawing::XShape >& xShape ) throw( uno::RuntimeException, std::exception )
215 : {
216 2 : maShapeContainer.removeInterface( xShape );
217 2 : }
218 :
219 :
220 324 : sal_Int32 SAL_CALL SvxShapeCollection::getCount() throw( uno::RuntimeException, std::exception )
221 : {
222 324 : return maShapeContainer.getLength();
223 : }
224 :
225 :
226 250 : uno::Any SAL_CALL SvxShapeCollection::getByIndex( sal_Int32 Index )
227 : throw( lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException, std::exception )
228 : {
229 250 : if( Index < 0 || Index >= getCount() )
230 2 : throw lang::IndexOutOfBoundsException();
231 :
232 248 : uno::Sequence< Reference< uno::XInterface> > xElements( maShapeContainer.getElements() );
233 :
234 :
235 248 : return uno::makeAny( Reference< drawing::XShape>(static_cast< drawing::XShape* >( xElements.getArray()[Index].get())) );
236 : }
237 :
238 : // XElementAccess
239 2 : uno::Type SAL_CALL SvxShapeCollection::getElementType() throw( uno::RuntimeException, std::exception )
240 : {
241 2 : return cppu::UnoType<drawing::XShape>::get();
242 : }
243 :
244 2 : sal_Bool SAL_CALL SvxShapeCollection::hasElements() throw( uno::RuntimeException, std::exception )
245 : {
246 2 : return getCount() != 0;
247 : }
248 :
249 : // XServiceInfo
250 6 : OUString SAL_CALL SvxShapeCollection::getImplementationName()
251 : throw( uno::RuntimeException, std::exception )
252 : {
253 6 : return OUString("com.sun.star.drawing.SvxShapeCollection");
254 : }
255 :
256 0 : sal_Bool SAL_CALL SvxShapeCollection::supportsService( const OUString& ServiceName )
257 : throw( uno::RuntimeException, std::exception )
258 : {
259 0 : return cppu::supportsService( this, ServiceName);
260 : }
261 :
262 0 : uno::Sequence< OUString > SAL_CALL SvxShapeCollection::getSupportedServiceNames() throw( uno::RuntimeException, std::exception )
263 : {
264 0 : uno::Sequence< OUString > aSeq(2);
265 0 : aSeq.getArray()[0] = "com.sun.star.drawing.Shapes";
266 0 : aSeq.getArray()[1] = "com.sun.star.drawing.ShapeCollection";
267 0 : return aSeq;
268 : }
269 :
270 : }
271 :
272 : extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
273 98 : com_sun_star_drawing_SvxShapeCollection_get_implementation(
274 : css::uno::XComponentContext *,
275 : css::uno::Sequence<css::uno::Any> const &)
276 : {
277 98 : return cppu::acquire(new SvxShapeCollection);
278 : }
279 :
280 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|