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