xref: /aoo42x/main/svx/source/unodraw/unoshcol.cxx (revision 86e1cf34)
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 	throw(::com::sun::star::uno::RuntimeException)
103 {
104 	// An frequently programming error is to release the last
105 	// reference to this object in the disposing message.
106 	// Make it rubust, hold a self Reference.
107 	uno::Reference< lang::XComponent > xSelf( this );
108 
109 	// Guard dispose against multible threading
110 	// Remark: It is an error to call dispose more than once
111 	sal_Bool bDoDispose = sal_False;
112 	{
113 	osl::MutexGuard aGuard( mrBHelper.rMutex );
114 	if( !mrBHelper.bDisposed && !mrBHelper.bInDispose )
115 	{
116 		// only one call go into this section
117 		mrBHelper.bInDispose = sal_True;
118 		bDoDispose = sal_True;
119 	}
120 	}
121 
122 	// Do not hold the mutex because we are broadcasting
123 	if( bDoDispose )
124 	{
125 		// Create an event with this as sender
126 		try
127 		{
128 			uno::Reference< uno::XInterface > xSource( uno::Reference< uno::XInterface >::query( (lang::XComponent *)this ) );
129 			document::EventObject aEvt;
130 			aEvt.Source = xSource;
131 			// inform all listeners to release this object
132 			// The listener container are automatically cleared
133 			mrBHelper.aLC.disposeAndClear( aEvt );
134 			// notify subclasses to do their dispose
135 			disposing();
136 		}
137 		catch(::com::sun::star::uno::Exception& e)
138 		{
139 			// catch exception and throw again but signal that
140 			// the object was disposed. Dispose should be called
141 			// only once.
142 			mrBHelper.bDisposed = sal_True;
143 			mrBHelper.bInDispose = sal_False;
144 			throw e;
145 		}
146 
147 		// the values bDispose and bInDisposing must set in this order.
148 		// No multithread call overcome the "!rBHelper.bDisposed && !rBHelper.bInDispose" guard.
149 		mrBHelper.bDisposed = sal_True;
150 		mrBHelper.bInDispose = sal_False;
151 	}
152 	else
153 	{
154 		// in a multithreaded environment, it can't be avoided, that dispose is called twice.
155 		// However this condition is traced, because it MAY indicate an error.
156 		OSL_TRACE( "OComponentHelper::dispose() - dispose called twice" );
157 	}
158 }
159 
160 // XComponent
addEventListener(const::com::sun::star::uno::Reference<::com::sun::star::lang::XEventListener> & aListener)161 void SAL_CALL SvxShapeCollection::addEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener >& aListener ) throw(::com::sun::star::uno::RuntimeException)
162 {
163 	mrBHelper.addListener( ::getCppuType( &aListener ) , aListener );
164 }
165 
166 // XComponent
removeEventListener(const::com::sun::star::uno::Reference<::com::sun::star::lang::XEventListener> & aListener)167 void SAL_CALL SvxShapeCollection::removeEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener >& aListener ) throw(::com::sun::star::uno::RuntimeException)
168 {
169 	mrBHelper.removeListener( ::getCppuType( &aListener ) , aListener );
170 }
171 
172 // XShapes
173 //----------------------------------------------------------------------
add(const Reference<drawing::XShape> & xShape)174 void SAL_CALL SvxShapeCollection::add( const Reference< drawing::XShape >& xShape ) throw( uno::RuntimeException )
175 {
176 	maShapeContainer.addInterface( xShape );
177 }
178 
179 //----------------------------------------------------------------------
remove(const uno::Reference<drawing::XShape> & xShape)180 void SAL_CALL SvxShapeCollection::remove( const uno::Reference< drawing::XShape >& xShape ) throw( uno::RuntimeException )
181 {
182 	maShapeContainer.removeInterface( xShape );
183 }
184 
185 //----------------------------------------------------------------------
getCount()186 sal_Int32 SAL_CALL SvxShapeCollection::getCount() throw( uno::RuntimeException )
187 {
188 	return maShapeContainer.getLength();
189 }
190 
191 //----------------------------------------------------------------------
getByIndex(sal_Int32 Index)192 uno::Any SAL_CALL SvxShapeCollection::getByIndex( sal_Int32 Index )
193 	throw( lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException )
194 {
195 	if( Index < 0 || Index >= getCount() )
196 		throw lang::IndexOutOfBoundsException();
197 
198 	uno::Sequence< Reference< uno::XInterface> > xElements( maShapeContainer.getElements() );
199 
200 
201 	return uno::makeAny( Reference< XShape>(static_cast< drawing::XShape* >( xElements.getArray()[Index].get())) );
202 }
203 
204 // XElementAccess
205 
206 //----------------------------------------------------------------------
getElementType()207 uno::Type SAL_CALL SvxShapeCollection::getElementType() throw( uno::RuntimeException )
208 {
209 	return ::getCppuType(( const Reference< drawing::XShape >*)0);
210 }
211 
212 //----------------------------------------------------------------------
hasElements()213 sal_Bool SAL_CALL SvxShapeCollection::hasElements() throw( uno::RuntimeException )
214 {
215 	return getCount() != 0;
216 }
217 
218 //----------------------------------------------------------------------
219 // XServiceInfo
220 //----------------------------------------------------------------------
getImplementationName()221 ::rtl::OUString SAL_CALL SvxShapeCollection::getImplementationName()
222 	throw( uno::RuntimeException )
223 {
224 	return getImplementationName_Static();
225 }
226 
getImplementationName_Static()227 ::rtl::OUString SvxShapeCollection::getImplementationName_Static()
228 {
229 	return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.drawing.SvxShapeCollection") );
230 }
231 
supportsService(const::rtl::OUString & ServiceName)232 sal_Bool SAL_CALL SvxShapeCollection::supportsService( const ::rtl::OUString& ServiceName )
233 	throw( uno::RuntimeException )
234 {
235 	return comphelper::ServiceInfoHelper::supportsService( ServiceName, getSupportedServiceNames() );
236 }
237 
getSupportedServiceNames()238 uno::Sequence< ::rtl::OUString > SAL_CALL SvxShapeCollection::getSupportedServiceNames() throw( uno::RuntimeException )
239 {
240 	return getSupportedServiceNames_Static();
241 }
242 
getSupportedServiceNames_Static()243 uno::Sequence< ::rtl::OUString > SvxShapeCollection::getSupportedServiceNames_Static()
244 {
245 	uno::Sequence< ::rtl::OUString > aSeq(2);
246 	aSeq.getArray()[1] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.drawing.Shapes") );
247 	aSeq.getArray()[1] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.drawing.ShapeCollection") );
248 	return aSeq;
249 }
250 
SvxShapeCollection_createInstance(const Reference<::com::sun::star::lang::XMultiServiceFactory> &)251 Reference< XInterface > SAL_CALL SvxShapeCollection_createInstance( const Reference< ::com::sun::star::lang::XMultiServiceFactory >& )
252 {
253 	return *( new SvxShapeCollection() );
254 }
255