Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / embedserv / source / embed / intercept.cxx
blob2f412d338db2d0fb09d37729c75cd0046ac969ac
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #include <cppuhelper/weak.hxx>
20 #include <cppuhelper/interfacecontainer.hxx>
22 #include <embeddoc.hxx>
23 #include <docholder.hxx>
24 #include <intercept.hxx>
26 using namespace ::com::sun::star;
29 #define IUL 6
32 uno::Sequence< OUString > Interceptor::m_aInterceptedURL(IUL);
34 class StatusChangeListenerContainer
35 : public cppu::OMultiTypeInterfaceContainerHelperVar<OUString>
37 public:
38 explicit StatusChangeListenerContainer(osl::Mutex& aMutex)
39 : cppu::OMultiTypeInterfaceContainerHelperVar<OUString>(aMutex)
45 void SAL_CALL
46 Interceptor::addEventListener(
47 const uno::Reference<lang::XEventListener >& Listener )
48 throw( uno::RuntimeException )
50 osl::MutexGuard aGuard( m_aMutex );
52 if ( ! m_pDisposeEventListeners )
53 m_pDisposeEventListeners =
54 new comphelper::OInterfaceContainerHelper2( m_aMutex );
56 m_pDisposeEventListeners->addInterface( Listener );
60 void SAL_CALL
61 Interceptor::removeEventListener(
62 const uno::Reference< lang::XEventListener >& Listener )
63 throw( uno::RuntimeException )
65 osl::MutexGuard aGuard( m_aMutex );
67 if ( m_pDisposeEventListeners )
68 m_pDisposeEventListeners->removeInterface( Listener );
72 void SAL_CALL Interceptor::dispose()
73 throw(css::uno::RuntimeException)
75 lang::EventObject aEvt;
76 aEvt.Source = static_cast< frame::XDispatch* >( this );
78 osl::MutexGuard aGuard(m_aMutex);
80 if ( m_pDisposeEventListeners && m_pDisposeEventListeners->getLength() )
81 m_pDisposeEventListeners->disposeAndClear( aEvt );
83 if(m_pStatCL)
84 m_pStatCL->disposeAndClear( aEvt );
86 m_xSlaveDispatchProvider = 0;
87 m_xMasterDispatchProvider = 0;
91 Interceptor::Interceptor(
92 const ::rtl::Reference< EmbeddedDocumentInstanceAccess_Impl >& xOleAccess,
93 DocumentHolder* pDocH,
94 sal_Bool bLink )
95 : m_xOleAccess( xOleAccess ),
96 m_xDocHLocker( static_cast< ::cppu::OWeakObject* >( pDocH ) ),
97 m_pDocH(pDocH),
98 m_pDisposeEventListeners(0),
99 m_pStatCL(0),
100 m_bLink( bLink )
102 m_aInterceptedURL[0] = ".uno:Save";
103 m_aInterceptedURL[1] = ".uno:SaveAll";
104 m_aInterceptedURL[2] = ".uno:CloseDoc";
105 m_aInterceptedURL[3] = ".uno:CloseWin";
106 m_aInterceptedURL[4] = ".uno:CloseFrame";
107 m_aInterceptedURL[5] = ".uno:SaveAs";
111 Interceptor::~Interceptor()
113 delete m_pDisposeEventListeners;
114 delete m_pStatCL;
116 DocumentHolder* pTmpDocH = NULL;
117 uno::Reference< uno::XInterface > xLock;
119 osl::MutexGuard aGuard(m_aMutex);
120 xLock = m_xDocHLocker.get();
121 if ( xLock.is() )
122 pTmpDocH = m_pDocH;
125 if ( pTmpDocH )
126 pTmpDocH->ClearInterceptor();
129 void Interceptor::DisconnectDocHolder()
131 osl::MutexGuard aGuard(m_aMutex);
132 m_xDocHLocker.clear();
133 m_pDocH = NULL;
134 m_xOleAccess = NULL;
137 //XDispatch
138 void SAL_CALL
139 Interceptor::dispatch(
140 const util::URL& URL,
141 const uno::Sequence<
142 beans::PropertyValue >& Arguments )
143 throw (uno::RuntimeException)
145 ::rtl::Reference< EmbeddedDocumentInstanceAccess_Impl > xOleAccess;
147 osl::MutexGuard aGuard(m_aMutex);
148 xOleAccess = m_xOleAccess;
151 if ( xOleAccess.is() )
153 LockedEmbedDocument_Impl aDocLock = xOleAccess->GetEmbedDocument();
154 if ( aDocLock.GetEmbedDocument() )
156 if( !m_bLink && URL.Complete == m_aInterceptedURL[0])
157 aDocLock.GetEmbedDocument()->SaveObject();
158 else if(!m_bLink
159 && ( URL.Complete == m_aInterceptedURL[2] ||
160 URL.Complete == m_aInterceptedURL[3] ||
161 URL.Complete == m_aInterceptedURL[4] ) )
162 aDocLock.GetEmbedDocument()->Close( 0 );
163 else if ( URL.Complete == m_aInterceptedURL[5] )
165 uno::Sequence< beans::PropertyValue > aNewArgs = Arguments;
166 sal_Int32 nInd = 0;
168 while( nInd < aNewArgs.getLength() )
170 if ( aNewArgs[nInd].Name == "SaveTo" )
172 aNewArgs[nInd].Value <<= sal_True;
173 break;
175 nInd++;
178 if ( nInd == aNewArgs.getLength() )
180 aNewArgs.realloc( nInd + 1 );
181 aNewArgs[nInd].Name = "SaveTo";
182 aNewArgs[nInd].Value <<= sal_True;
185 uno::Reference< frame::XDispatch > xDispatch = m_xSlaveDispatchProvider->queryDispatch(
186 URL, OUString( "_self" ), 0 );
187 if ( xDispatch.is() )
188 xDispatch->dispatch( URL, aNewArgs );
195 void Interceptor::generateFeatureStateEvent()
197 if( m_pStatCL )
199 DocumentHolder* pTmpDocH = NULL;
200 uno::Reference< uno::XInterface > xLock;
202 osl::MutexGuard aGuard(m_aMutex);
203 xLock = m_xDocHLocker.get();
204 if ( xLock.is() )
205 pTmpDocH = m_pDocH;
208 OUString aTitle;
209 if ( pTmpDocH )
210 aTitle = pTmpDocH->getTitle();
212 for(int i = 0; i < IUL; ++i)
214 if( i == 1 || (m_bLink && i != 5) )
215 continue;
217 cppu::OInterfaceContainerHelper* pICH =
218 m_pStatCL->getContainer(m_aInterceptedURL[i]);
219 uno::Sequence<uno::Reference<uno::XInterface> > aSeq;
220 if(pICH)
221 aSeq = pICH->getElements();
222 if(!aSeq.getLength())
223 continue;
225 frame::FeatureStateEvent aStateEvent;
226 aStateEvent.IsEnabled = sal_True;
227 aStateEvent.Requery = sal_False;
228 if(i == 0)
231 aStateEvent.FeatureURL.Complete = m_aInterceptedURL[0];
232 aStateEvent.FeatureDescriptor = "Update";
233 aStateEvent.State <<= (OUString("($1) ") + aTitle);
236 else if ( i == 5 )
238 aStateEvent.FeatureURL.Complete = m_aInterceptedURL[5];
239 aStateEvent.FeatureDescriptor = "SaveCopyTo";
240 aStateEvent.State <<= (OUString("($3)"));
242 else
244 aStateEvent.FeatureURL.Complete = m_aInterceptedURL[i];
245 aStateEvent.FeatureDescriptor = "Close and Return";
246 aStateEvent.State <<= (OUString("($2) ") + aTitle);
250 for(sal_Int32 k = 0; k < aSeq.getLength(); ++k)
252 uno::Reference<frame::XStatusListener>
253 Control(aSeq[k],uno::UNO_QUERY);
254 if(Control.is())
255 Control->statusChanged(aStateEvent);
263 void SAL_CALL
264 Interceptor::addStatusListener(
265 const uno::Reference<
266 frame::XStatusListener >& Control,
267 const util::URL& URL )
268 throw (
269 uno::RuntimeException
272 if(!Control.is())
273 return;
275 if( !m_bLink && URL.Complete == m_aInterceptedURL[0] )
276 { // Save
277 DocumentHolder* pTmpDocH = NULL;
278 uno::Reference< uno::XInterface > xLock;
280 osl::MutexGuard aGuard(m_aMutex);
281 xLock = m_xDocHLocker.get();
282 if ( xLock.is() )
283 pTmpDocH = m_pDocH;
286 OUString aTitle;
287 if ( pTmpDocH )
288 aTitle = pTmpDocH->getTitle();
290 frame::FeatureStateEvent aStateEvent;
291 aStateEvent.FeatureURL.Complete = m_aInterceptedURL[0];
292 aStateEvent.FeatureDescriptor = "Update";
293 aStateEvent.IsEnabled = sal_True;
294 aStateEvent.Requery = sal_False;
295 aStateEvent.State <<= (OUString("($1) ") + aTitle );
296 Control->statusChanged(aStateEvent);
299 osl::MutexGuard aGuard(m_aMutex);
300 if(!m_pStatCL)
301 m_pStatCL =
302 new StatusChangeListenerContainer(m_aMutex);
305 m_pStatCL->addInterface(URL.Complete,Control);
306 return;
309 sal_Int32 i = 2;
310 if ( !m_bLink
311 && ( URL.Complete == m_aInterceptedURL[i] ||
312 URL.Complete == m_aInterceptedURL[++i] ||
313 URL.Complete == m_aInterceptedURL[++i] ) )
314 { // Close and return
315 DocumentHolder* pTmpDocH = NULL;
316 uno::Reference< uno::XInterface > xLock;
318 osl::MutexGuard aGuard(m_aMutex);
319 xLock = m_xDocHLocker.get();
320 if ( xLock.is() )
321 pTmpDocH = m_pDocH;
324 OUString aTitle;
325 if ( pTmpDocH )
326 aTitle = pTmpDocH->getTitle();
328 frame::FeatureStateEvent aStateEvent;
329 aStateEvent.FeatureURL.Complete = m_aInterceptedURL[i];
330 aStateEvent.FeatureDescriptor = "Close and Return";
331 aStateEvent.IsEnabled = sal_True;
332 aStateEvent.Requery = sal_False;
333 aStateEvent.State <<= (OUString("($2) ") + aTitle );
334 Control->statusChanged(aStateEvent);
338 osl::MutexGuard aGuard(m_aMutex);
339 if(!m_pStatCL)
340 m_pStatCL =
341 new StatusChangeListenerContainer(m_aMutex);
344 m_pStatCL->addInterface(URL.Complete,Control);
345 return;
348 if(URL.Complete == m_aInterceptedURL[5])
349 { // SaveAs
350 frame::FeatureStateEvent aStateEvent;
351 aStateEvent.FeatureURL.Complete = m_aInterceptedURL[5];
352 aStateEvent.FeatureDescriptor = "SaveCopyTo";
353 aStateEvent.IsEnabled = sal_True;
354 aStateEvent.Requery = sal_False;
355 aStateEvent.State <<= (OUString("($3)"));
356 Control->statusChanged(aStateEvent);
359 osl::MutexGuard aGuard(m_aMutex);
360 if(!m_pStatCL)
361 m_pStatCL =
362 new StatusChangeListenerContainer(m_aMutex);
365 m_pStatCL->addInterface(URL.Complete,Control);
366 return;
372 void SAL_CALL
373 Interceptor::removeStatusListener(
374 const uno::Reference<
375 frame::XStatusListener >& Control,
376 const util::URL& URL )
377 throw (
378 uno::RuntimeException
381 if(!(Control.is() && m_pStatCL))
382 return;
383 else {
384 m_pStatCL->removeInterface(URL.Complete,Control);
385 return;
390 //XInterceptorInfo
391 uno::Sequence< OUString >
392 SAL_CALL
393 Interceptor::getInterceptedURLs( )
394 throw (
395 uno::RuntimeException
398 // now implemented as update
399 if ( m_bLink )
401 uno::Sequence< OUString > aResult( 2 );
402 aResult[0] = m_aInterceptedURL[1];
403 aResult[1] = m_aInterceptedURL[5];
405 return aResult;
408 return m_aInterceptedURL;
412 // XDispatchProvider
414 uno::Reference< frame::XDispatch > SAL_CALL
415 Interceptor::queryDispatch(
416 const util::URL& URL,
417 const OUString& TargetFrameName,
418 sal_Int32 SearchFlags )
419 throw (
420 uno::RuntimeException
423 osl::MutexGuard aGuard(m_aMutex);
424 if( !m_bLink && URL.Complete == m_aInterceptedURL[0] )
425 return (frame::XDispatch*)this;
426 else if(URL.Complete == m_aInterceptedURL[1])
427 return (frame::XDispatch*)0 ;
428 else if( !m_bLink && URL.Complete == m_aInterceptedURL[2] )
429 return (frame::XDispatch*)this;
430 else if( !m_bLink && URL.Complete == m_aInterceptedURL[3] )
431 return (frame::XDispatch*)this;
432 else if( !m_bLink && URL.Complete == m_aInterceptedURL[4] )
433 return (frame::XDispatch*)this;
434 else if(URL.Complete == m_aInterceptedURL[5])
435 return (frame::XDispatch*)this;
436 else {
437 if(m_xSlaveDispatchProvider.is())
438 return m_xSlaveDispatchProvider->queryDispatch(
439 URL,TargetFrameName,SearchFlags);
440 else
441 return uno::Reference<frame::XDispatch>(0);
445 uno::Sequence< uno::Reference< frame::XDispatch > > SAL_CALL
446 Interceptor::queryDispatches(
447 const uno::Sequence<frame::DispatchDescriptor >& Requests )
448 throw (
449 uno::RuntimeException
452 uno::Sequence< uno::Reference< frame::XDispatch > > aRet;
453 osl::MutexGuard aGuard(m_aMutex);
454 if(m_xSlaveDispatchProvider.is())
455 aRet = m_xSlaveDispatchProvider->queryDispatches(Requests);
456 else
457 aRet.realloc(Requests.getLength());
459 for(sal_Int32 i = 0; i < Requests.getLength(); ++i)
460 if ( !m_bLink && m_aInterceptedURL[0] == Requests[i].FeatureURL.Complete )
461 aRet[i] = (frame::XDispatch*) this;
462 else if(m_aInterceptedURL[1] == Requests[i].FeatureURL.Complete)
463 aRet[i] = (frame::XDispatch*) 0;
464 else if( !m_bLink && m_aInterceptedURL[2] == Requests[i].FeatureURL.Complete )
465 aRet[i] = (frame::XDispatch*) this;
466 else if( !m_bLink && m_aInterceptedURL[3] == Requests[i].FeatureURL.Complete )
467 aRet[i] = (frame::XDispatch*) this;
468 else if( !m_bLink && m_aInterceptedURL[4] == Requests[i].FeatureURL.Complete )
469 aRet[i] = (frame::XDispatch*) this;
470 else if(m_aInterceptedURL[5] == Requests[i].FeatureURL.Complete)
471 aRet[i] = (frame::XDispatch*) this;
473 return aRet;
477 //XDispatchProviderInterceptor
479 uno::Reference< frame::XDispatchProvider > SAL_CALL
480 Interceptor::getSlaveDispatchProvider( )
481 throw (
482 uno::RuntimeException
485 osl::MutexGuard aGuard(m_aMutex);
486 return m_xSlaveDispatchProvider;
489 void SAL_CALL
490 Interceptor::setSlaveDispatchProvider(
491 const uno::Reference< frame::XDispatchProvider >& NewDispatchProvider )
492 throw (
493 uno::RuntimeException
496 osl::MutexGuard aGuard(m_aMutex);
497 m_xSlaveDispatchProvider = NewDispatchProvider;
501 uno::Reference< frame::XDispatchProvider > SAL_CALL
502 Interceptor::getMasterDispatchProvider( )
503 throw (
504 uno::RuntimeException
507 osl::MutexGuard aGuard(m_aMutex);
508 return m_xMasterDispatchProvider;
512 void SAL_CALL
513 Interceptor::setMasterDispatchProvider(
514 const uno::Reference< frame::XDispatchProvider >& NewSupplier )
515 throw (
516 uno::RuntimeException
519 osl::MutexGuard aGuard(m_aMutex);
520 m_xMasterDispatchProvider = NewSupplier;
523 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */