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