Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / embeddedobj / source / general / intercept.cxx
blob0f328cb2765297bfd822cf57c9663498b886e494
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 .
20 #include <com/sun/star/embed/EmbedStates.hpp>
21 #include <cppuhelper/weak.hxx>
23 #include "intercept.hxx"
24 #include "docholder.hxx"
25 #include "commonembobj.hxx"
27 using namespace ::com::sun::star;
29 #define IUL 6
32 uno::Sequence< OUString > Interceptor::m_aInterceptedURL(IUL);
34 struct equalOUString
36 bool operator()(
37 const OUString& rKey1,
38 const OUString& rKey2 ) const
40 return !!( rKey1 == rKey2 );
45 struct hashOUString
47 size_t operator()( const OUString& rName ) const
49 return rName.hashCode();
55 class StatusChangeListenerContainer
56 : public ::cppu::OMultiTypeInterfaceContainerHelperVar<
57 OUString,hashOUString,equalOUString>
59 public:
60 StatusChangeListenerContainer( ::osl::Mutex& aMutex )
61 : cppu::OMultiTypeInterfaceContainerHelperVar<
62 OUString,hashOUString,equalOUString>(aMutex)
68 void Interceptor::DisconnectDocHolder()
70 osl::MutexGuard aGuard( m_aMutex );
71 m_pDocHolder = NULL;
74 void SAL_CALL
75 Interceptor::addEventListener(
76 const uno::Reference<lang::XEventListener >& Listener )
77 throw( uno::RuntimeException )
79 osl::MutexGuard aGuard( m_aMutex );
81 if ( ! m_pDisposeEventListeners )
82 m_pDisposeEventListeners =
83 new cppu::OInterfaceContainerHelper( m_aMutex );
85 m_pDisposeEventListeners->addInterface( Listener );
89 void SAL_CALL
90 Interceptor::removeEventListener(
91 const uno::Reference< lang::XEventListener >& Listener )
92 throw( uno::RuntimeException )
94 osl::MutexGuard aGuard( m_aMutex );
96 if ( m_pDisposeEventListeners )
97 m_pDisposeEventListeners->removeInterface( Listener );
101 Interceptor::Interceptor( DocumentHolder* pDocHolder )
102 : m_pDocHolder( pDocHolder ),
103 m_pDisposeEventListeners(0),
104 m_pStatCL(0)
106 m_aInterceptedURL[0] = ".uno:Save";
107 m_aInterceptedURL[1] = ".uno:SaveAll";
108 m_aInterceptedURL[2] = ".uno:CloseDoc";
109 m_aInterceptedURL[3] = ".uno:CloseWin";
110 m_aInterceptedURL[4] = ".uno:CloseFrame";
111 m_aInterceptedURL[5] = ".uno:SaveAs";
116 Interceptor::~Interceptor()
118 if( m_pDisposeEventListeners )
119 delete m_pDisposeEventListeners;
121 if(m_pStatCL)
122 delete m_pStatCL;
127 //XDispatch
128 void SAL_CALL
129 Interceptor::dispatch(
130 const util::URL& URL,
131 const uno::Sequence<
132 beans::PropertyValue >& Arguments )
133 throw (uno::RuntimeException)
135 osl::MutexGuard aGuard(m_aMutex);
136 if( m_pDocHolder )
138 if(URL.Complete == m_aInterceptedURL[0])
139 m_pDocHolder->GetEmbedObject()->SaveObject_Impl();
140 else if(URL.Complete == m_aInterceptedURL[2] ||
141 URL.Complete == m_aInterceptedURL[3] ||
142 URL.Complete == m_aInterceptedURL[4])
144 try {
145 m_pDocHolder->GetEmbedObject()->changeState( embed::EmbedStates::RUNNING );
147 catch( const uno::Exception& )
151 else if ( URL.Complete == m_aInterceptedURL[5] )
153 uno::Sequence< beans::PropertyValue > aNewArgs = Arguments;
154 sal_Int32 nInd = 0;
156 while( nInd < aNewArgs.getLength() )
158 if ( aNewArgs[nInd].Name == "SaveTo" )
160 aNewArgs[nInd].Value <<= sal_True;
161 break;
163 nInd++;
166 if ( nInd == aNewArgs.getLength() )
168 aNewArgs.realloc( nInd + 1 );
169 aNewArgs[nInd].Name = "SaveTo";
170 aNewArgs[nInd].Value <<= sal_True;
173 uno::Reference< frame::XDispatch > xDispatch = m_xSlaveDispatchProvider->queryDispatch(
174 URL, OUString( "_self" ), 0 );
175 if ( xDispatch.is() )
176 xDispatch->dispatch( URL, aNewArgs );
181 void SAL_CALL
182 Interceptor::addStatusListener(
183 const uno::Reference<
184 frame::XStatusListener >& Control,
185 const util::URL& URL )
186 throw (
187 uno::RuntimeException
190 if(!Control.is())
191 return;
193 if(URL.Complete == m_aInterceptedURL[0])
194 { // Save
195 frame::FeatureStateEvent aStateEvent;
196 aStateEvent.FeatureURL.Complete = m_aInterceptedURL[0];
197 aStateEvent.FeatureDescriptor = "Update";
198 aStateEvent.IsEnabled = sal_True;
199 aStateEvent.Requery = sal_False;
200 aStateEvent.State <<= (OUString( "($1) ") + m_pDocHolder->GetTitle() );
201 Control->statusChanged(aStateEvent);
204 osl::MutexGuard aGuard(m_aMutex);
205 if(!m_pStatCL)
206 m_pStatCL =
207 new StatusChangeListenerContainer(m_aMutex);
210 m_pStatCL->addInterface(URL.Complete,Control);
211 return;
214 sal_Int32 i = 2;
215 if(URL.Complete == m_aInterceptedURL[i] ||
216 URL.Complete == m_aInterceptedURL[++i] ||
217 URL.Complete == m_aInterceptedURL[++i] )
218 { // Close and return
219 frame::FeatureStateEvent aStateEvent;
220 aStateEvent.FeatureURL.Complete = m_aInterceptedURL[i];
221 aStateEvent.FeatureDescriptor = "Close and Return";
222 aStateEvent.IsEnabled = sal_True;
223 aStateEvent.Requery = sal_False;
224 aStateEvent.State <<= (OUString( "($2) ") + m_pDocHolder->GetTitle() );
225 Control->statusChanged(aStateEvent);
229 osl::MutexGuard aGuard(m_aMutex);
230 if(!m_pStatCL)
231 m_pStatCL =
232 new StatusChangeListenerContainer(m_aMutex);
235 m_pStatCL->addInterface(URL.Complete,Control);
236 return;
239 if(URL.Complete == m_aInterceptedURL[5])
240 { // SaveAs
241 frame::FeatureStateEvent aStateEvent;
242 aStateEvent.FeatureURL.Complete = m_aInterceptedURL[5];
243 aStateEvent.FeatureDescriptor = "SaveCopyTo";
244 aStateEvent.IsEnabled = sal_True;
245 aStateEvent.Requery = sal_False;
246 aStateEvent.State <<= (OUString( "($3)"));
247 Control->statusChanged(aStateEvent);
250 osl::MutexGuard aGuard(m_aMutex);
251 if(!m_pStatCL)
252 m_pStatCL =
253 new StatusChangeListenerContainer(m_aMutex);
256 m_pStatCL->addInterface(URL.Complete,Control);
257 return;
263 void SAL_CALL
264 Interceptor::removeStatusListener(
265 const uno::Reference<
266 frame::XStatusListener >& Control,
267 const util::URL& URL )
268 throw (
269 uno::RuntimeException
272 if(!(Control.is() && m_pStatCL))
273 return;
274 else {
275 m_pStatCL->removeInterface(URL.Complete,Control);
276 return;
281 //XInterceptorInfo
282 uno::Sequence< OUString >
283 SAL_CALL
284 Interceptor::getInterceptedURLs( )
285 throw (
286 uno::RuntimeException
289 // now implemented as update
291 return m_aInterceptedURL;
295 // XDispatchProvider
297 uno::Reference< frame::XDispatch > SAL_CALL
298 Interceptor::queryDispatch(
299 const util::URL& URL,
300 const OUString& TargetFrameName,
301 sal_Int32 SearchFlags )
302 throw (
303 uno::RuntimeException
306 osl::MutexGuard aGuard(m_aMutex);
307 if(URL.Complete == m_aInterceptedURL[0])
308 return (frame::XDispatch*)this;
309 else if(URL.Complete == m_aInterceptedURL[1])
310 return (frame::XDispatch*)0 ;
311 else if(URL.Complete == m_aInterceptedURL[2])
312 return (frame::XDispatch*)this;
313 else if(URL.Complete == m_aInterceptedURL[3])
314 return (frame::XDispatch*)this;
315 else if(URL.Complete == m_aInterceptedURL[4])
316 return (frame::XDispatch*)this;
317 else if(URL.Complete == m_aInterceptedURL[5])
318 return (frame::XDispatch*)this;
319 else {
320 if(m_xSlaveDispatchProvider.is())
321 return m_xSlaveDispatchProvider->queryDispatch(
322 URL,TargetFrameName,SearchFlags);
323 else
324 return uno::Reference<frame::XDispatch>(0);
328 uno::Sequence< uno::Reference< frame::XDispatch > > SAL_CALL
329 Interceptor::queryDispatches(
330 const uno::Sequence<frame::DispatchDescriptor >& Requests )
331 throw (
332 uno::RuntimeException
335 uno::Sequence< uno::Reference< frame::XDispatch > > aRet;
336 osl::MutexGuard aGuard(m_aMutex);
337 if(m_xSlaveDispatchProvider.is())
338 aRet = m_xSlaveDispatchProvider->queryDispatches(Requests);
339 else
340 aRet.realloc(Requests.getLength());
342 for(sal_Int32 i = 0; i < Requests.getLength(); ++i)
343 if(m_aInterceptedURL[0] == Requests[i].FeatureURL.Complete)
344 aRet[i] = (frame::XDispatch*) this;
345 else if(m_aInterceptedURL[1] == Requests[i].FeatureURL.Complete)
346 aRet[i] = (frame::XDispatch*) 0;
347 else if(m_aInterceptedURL[2] == Requests[i].FeatureURL.Complete)
348 aRet[i] = (frame::XDispatch*) this;
349 else if(m_aInterceptedURL[3] == Requests[i].FeatureURL.Complete)
350 aRet[i] = (frame::XDispatch*) this;
351 else if(m_aInterceptedURL[4] == Requests[i].FeatureURL.Complete)
352 aRet[i] = (frame::XDispatch*) this;
353 else if(m_aInterceptedURL[5] == Requests[i].FeatureURL.Complete)
354 aRet[i] = (frame::XDispatch*) this;
356 return aRet;
361 //XDispatchProviderInterceptor
363 uno::Reference< frame::XDispatchProvider > SAL_CALL
364 Interceptor::getSlaveDispatchProvider( )
365 throw (
366 uno::RuntimeException
369 osl::MutexGuard aGuard(m_aMutex);
370 return m_xSlaveDispatchProvider;
373 void SAL_CALL
374 Interceptor::setSlaveDispatchProvider(
375 const uno::Reference< frame::XDispatchProvider >& NewDispatchProvider )
376 throw (
377 uno::RuntimeException
380 osl::MutexGuard aGuard(m_aMutex);
381 m_xSlaveDispatchProvider = NewDispatchProvider;
385 uno::Reference< frame::XDispatchProvider > SAL_CALL
386 Interceptor::getMasterDispatchProvider( )
387 throw (
388 uno::RuntimeException
391 osl::MutexGuard aGuard(m_aMutex);
392 return m_xMasterDispatchProvider;
396 void SAL_CALL
397 Interceptor::setMasterDispatchProvider(
398 const uno::Reference< frame::XDispatchProvider >& NewSupplier )
399 throw (
400 uno::RuntimeException
403 osl::MutexGuard aGuard(m_aMutex);
404 m_xMasterDispatchProvider = NewSupplier;
407 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */