merge the formfield patch from ooo-build
[ooovba.git] / embeddedobj / source / general / intercept.cxx
blobfcbf2d130c22ce758b1d417f96767b7c194d6811
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: intercept.cxx,v $
10 * $Revision: 1.8 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_embeddedobj.hxx"
33 #include <com/sun/star/embed/EmbedStates.hpp>
34 #include <cppuhelper/weak.hxx>
36 #include "intercept.hxx"
37 #include "docholder.hxx"
38 #include "commonembobj.hxx"
40 using namespace ::com::sun::star;
43 #define IUL 6
46 uno::Sequence< ::rtl::OUString > Interceptor::m_aInterceptedURL(IUL);
48 struct equalOUString
50 bool operator()(
51 const rtl::OUString& rKey1,
52 const rtl::OUString& rKey2 ) const
54 return !!( rKey1 == rKey2 );
59 struct hashOUString
61 size_t operator()( const rtl::OUString& rName ) const
63 return rName.hashCode();
69 class StatusChangeListenerContainer
70 : public ::cppu::OMultiTypeInterfaceContainerHelperVar<
71 rtl::OUString,hashOUString,equalOUString>
73 public:
74 StatusChangeListenerContainer( ::osl::Mutex& aMutex )
75 : cppu::OMultiTypeInterfaceContainerHelperVar<
76 rtl::OUString,hashOUString,equalOUString>(aMutex)
82 void Interceptor::DisconnectDocHolder()
84 osl::MutexGuard aGuard( m_aMutex );
85 m_pDocHolder = NULL;
88 void SAL_CALL
89 Interceptor::addEventListener(
90 const uno::Reference<lang::XEventListener >& Listener )
91 throw( uno::RuntimeException )
93 osl::MutexGuard aGuard( m_aMutex );
95 if ( ! m_pDisposeEventListeners )
96 m_pDisposeEventListeners =
97 new cppu::OInterfaceContainerHelper( m_aMutex );
99 m_pDisposeEventListeners->addInterface( Listener );
103 void SAL_CALL
104 Interceptor::removeEventListener(
105 const uno::Reference< lang::XEventListener >& Listener )
106 throw( uno::RuntimeException )
108 osl::MutexGuard aGuard( m_aMutex );
110 if ( m_pDisposeEventListeners )
111 m_pDisposeEventListeners->removeInterface( Listener );
115 Interceptor::Interceptor( DocumentHolder* pDocHolder )
116 : m_pDocHolder( pDocHolder ),
117 m_pDisposeEventListeners(0),
118 m_pStatCL(0)
120 m_aInterceptedURL[0] = rtl::OUString(
121 RTL_CONSTASCII_USTRINGPARAM(".uno:Save"));
122 m_aInterceptedURL[1] = rtl::OUString(
123 RTL_CONSTASCII_USTRINGPARAM(".uno:SaveAll"));
124 m_aInterceptedURL[2] = rtl::OUString(
125 RTL_CONSTASCII_USTRINGPARAM(".uno:CloseDoc"));
126 m_aInterceptedURL[3] = rtl::OUString(
127 RTL_CONSTASCII_USTRINGPARAM(".uno:CloseWin"));
128 m_aInterceptedURL[4] = rtl::OUString(
129 RTL_CONSTASCII_USTRINGPARAM(".uno:CloseFrame"));
130 m_aInterceptedURL[5] = rtl::OUString(
131 RTL_CONSTASCII_USTRINGPARAM(".uno:SaveAs"));
136 Interceptor::~Interceptor()
138 if( m_pDisposeEventListeners )
139 delete m_pDisposeEventListeners;
141 if(m_pStatCL)
142 delete m_pStatCL;
147 //XDispatch
148 void SAL_CALL
149 Interceptor::dispatch(
150 const util::URL& URL,
151 const uno::Sequence<
152 beans::PropertyValue >& Arguments )
153 throw (uno::RuntimeException)
155 osl::MutexGuard aGuard(m_aMutex);
156 if( m_pDocHolder )
158 if(URL.Complete == m_aInterceptedURL[0])
159 m_pDocHolder->GetEmbedObject()->SaveObject_Impl();
160 else if(URL.Complete == m_aInterceptedURL[2] ||
161 URL.Complete == m_aInterceptedURL[3] ||
162 URL.Complete == m_aInterceptedURL[4])
164 try {
165 m_pDocHolder->GetEmbedObject()->changeState( embed::EmbedStates::RUNNING );
167 catch( uno::Exception& )
171 else if ( URL.Complete == m_aInterceptedURL[5] )
173 uno::Sequence< beans::PropertyValue > aNewArgs = Arguments;
174 sal_Int32 nInd = 0;
176 while( nInd < aNewArgs.getLength() )
178 if ( aNewArgs[nInd].Name.equalsAscii( "SaveTo" ) )
180 aNewArgs[nInd].Value <<= sal_True;
181 break;
183 nInd++;
186 if ( nInd == aNewArgs.getLength() )
188 aNewArgs.realloc( nInd + 1 );
189 aNewArgs[nInd].Name = ::rtl::OUString::createFromAscii( "SaveTo" );
190 aNewArgs[nInd].Value <<= sal_True;
193 uno::Reference< frame::XDispatch > xDispatch = m_xSlaveDispatchProvider->queryDispatch(
194 URL, ::rtl::OUString::createFromAscii( "_self" ), 0 );
195 if ( xDispatch.is() )
196 xDispatch->dispatch( URL, aNewArgs );
201 void SAL_CALL
202 Interceptor::addStatusListener(
203 const uno::Reference<
204 frame::XStatusListener >& Control,
205 const util::URL& URL )
206 throw (
207 uno::RuntimeException
210 if(!Control.is())
211 return;
213 if(URL.Complete == m_aInterceptedURL[0])
214 { // Save
215 frame::FeatureStateEvent aStateEvent;
216 aStateEvent.FeatureURL.Complete = m_aInterceptedURL[0];
217 aStateEvent.FeatureDescriptor = rtl::OUString(
218 RTL_CONSTASCII_USTRINGPARAM("Update"));
219 aStateEvent.IsEnabled = sal_True;
220 aStateEvent.Requery = sal_False;
221 aStateEvent.State <<= (rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("($1) ")) + m_pDocHolder->GetTitle() );
222 Control->statusChanged(aStateEvent);
225 osl::MutexGuard aGuard(m_aMutex);
226 if(!m_pStatCL)
227 m_pStatCL =
228 new StatusChangeListenerContainer(m_aMutex);
231 m_pStatCL->addInterface(URL.Complete,Control);
232 return;
235 sal_Int32 i = 2;
236 if(URL.Complete == m_aInterceptedURL[i] ||
237 URL.Complete == m_aInterceptedURL[++i] ||
238 URL.Complete == m_aInterceptedURL[++i] )
239 { // Close and return
240 frame::FeatureStateEvent aStateEvent;
241 aStateEvent.FeatureURL.Complete = m_aInterceptedURL[i];
242 aStateEvent.FeatureDescriptor = rtl::OUString(
243 RTL_CONSTASCII_USTRINGPARAM("Close and Return"));
244 aStateEvent.IsEnabled = sal_True;
245 aStateEvent.Requery = sal_False;
246 aStateEvent.State <<= (rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("($2) ")) + m_pDocHolder->GetTitle() );
247 Control->statusChanged(aStateEvent);
251 osl::MutexGuard aGuard(m_aMutex);
252 if(!m_pStatCL)
253 m_pStatCL =
254 new StatusChangeListenerContainer(m_aMutex);
257 m_pStatCL->addInterface(URL.Complete,Control);
258 return;
261 if(URL.Complete == m_aInterceptedURL[5])
262 { // SaveAs
263 frame::FeatureStateEvent aStateEvent;
264 aStateEvent.FeatureURL.Complete = m_aInterceptedURL[5];
265 aStateEvent.FeatureDescriptor = rtl::OUString(
266 RTL_CONSTASCII_USTRINGPARAM("SaveCopyTo"));
267 aStateEvent.IsEnabled = sal_True;
268 aStateEvent.Requery = sal_False;
269 aStateEvent.State <<= (rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("($3)")));
270 Control->statusChanged(aStateEvent);
273 osl::MutexGuard aGuard(m_aMutex);
274 if(!m_pStatCL)
275 m_pStatCL =
276 new StatusChangeListenerContainer(m_aMutex);
279 m_pStatCL->addInterface(URL.Complete,Control);
280 return;
286 void SAL_CALL
287 Interceptor::removeStatusListener(
288 const uno::Reference<
289 frame::XStatusListener >& Control,
290 const util::URL& URL )
291 throw (
292 uno::RuntimeException
295 if(!(Control.is() && m_pStatCL))
296 return;
297 else {
298 m_pStatCL->removeInterface(URL.Complete,Control);
299 return;
304 //XInterceptorInfo
305 uno::Sequence< ::rtl::OUString >
306 SAL_CALL
307 Interceptor::getInterceptedURLs( )
308 throw (
309 uno::RuntimeException
312 // now implemented as update
314 return m_aInterceptedURL;
318 // XDispatchProvider
320 uno::Reference< frame::XDispatch > SAL_CALL
321 Interceptor::queryDispatch(
322 const util::URL& URL,
323 const ::rtl::OUString& TargetFrameName,
324 sal_Int32 SearchFlags )
325 throw (
326 uno::RuntimeException
329 osl::MutexGuard aGuard(m_aMutex);
330 if(URL.Complete == m_aInterceptedURL[0])
331 return (frame::XDispatch*)this;
332 else if(URL.Complete == m_aInterceptedURL[1])
333 return (frame::XDispatch*)0 ;
334 else if(URL.Complete == m_aInterceptedURL[2])
335 return (frame::XDispatch*)this;
336 else if(URL.Complete == m_aInterceptedURL[3])
337 return (frame::XDispatch*)this;
338 else if(URL.Complete == m_aInterceptedURL[4])
339 return (frame::XDispatch*)this;
340 else if(URL.Complete == m_aInterceptedURL[5])
341 return (frame::XDispatch*)this;
342 else {
343 if(m_xSlaveDispatchProvider.is())
344 return m_xSlaveDispatchProvider->queryDispatch(
345 URL,TargetFrameName,SearchFlags);
346 else
347 return uno::Reference<frame::XDispatch>(0);
351 uno::Sequence< uno::Reference< frame::XDispatch > > SAL_CALL
352 Interceptor::queryDispatches(
353 const uno::Sequence<frame::DispatchDescriptor >& Requests )
354 throw (
355 uno::RuntimeException
358 uno::Sequence< uno::Reference< frame::XDispatch > > aRet;
359 osl::MutexGuard aGuard(m_aMutex);
360 if(m_xSlaveDispatchProvider.is())
361 aRet = m_xSlaveDispatchProvider->queryDispatches(Requests);
362 else
363 aRet.realloc(Requests.getLength());
365 for(sal_Int32 i = 0; i < Requests.getLength(); ++i)
366 if(m_aInterceptedURL[0] == Requests[i].FeatureURL.Complete)
367 aRet[i] = (frame::XDispatch*) this;
368 else if(m_aInterceptedURL[1] == Requests[i].FeatureURL.Complete)
369 aRet[i] = (frame::XDispatch*) 0;
370 else if(m_aInterceptedURL[2] == Requests[i].FeatureURL.Complete)
371 aRet[i] = (frame::XDispatch*) this;
372 else if(m_aInterceptedURL[3] == Requests[i].FeatureURL.Complete)
373 aRet[i] = (frame::XDispatch*) this;
374 else if(m_aInterceptedURL[4] == Requests[i].FeatureURL.Complete)
375 aRet[i] = (frame::XDispatch*) this;
376 else if(m_aInterceptedURL[5] == Requests[i].FeatureURL.Complete)
377 aRet[i] = (frame::XDispatch*) this;
379 return aRet;
384 //XDispatchProviderInterceptor
386 uno::Reference< frame::XDispatchProvider > SAL_CALL
387 Interceptor::getSlaveDispatchProvider( )
388 throw (
389 uno::RuntimeException
392 osl::MutexGuard aGuard(m_aMutex);
393 return m_xSlaveDispatchProvider;
396 void SAL_CALL
397 Interceptor::setSlaveDispatchProvider(
398 const uno::Reference< frame::XDispatchProvider >& NewDispatchProvider )
399 throw (
400 uno::RuntimeException
403 osl::MutexGuard aGuard(m_aMutex);
404 m_xSlaveDispatchProvider = NewDispatchProvider;
408 uno::Reference< frame::XDispatchProvider > SAL_CALL
409 Interceptor::getMasterDispatchProvider( )
410 throw (
411 uno::RuntimeException
414 osl::MutexGuard aGuard(m_aMutex);
415 return m_xMasterDispatchProvider;
419 void SAL_CALL
420 Interceptor::setMasterDispatchProvider(
421 const uno::Reference< frame::XDispatchProvider >& NewSupplier )
422 throw (
423 uno::RuntimeException
426 osl::MutexGuard aGuard(m_aMutex);
427 m_xMasterDispatchProvider = NewSupplier;