build fix: no comphelper/profilezone.hxx in this branch
[LibreOffice.git] / dbaccess / source / core / dataaccess / intercept.cxx
bloba768615455c06da1232cd3586b0e4bc0baf56f8c
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 .
21 #include "intercept.hxx"
22 #include "dbastrings.hrc"
24 #include <com/sun/star/embed/EmbedStates.hpp>
25 #include <com/sun/star/util/XModifiable.hpp>
26 #include <cppuhelper/weak.hxx>
28 #include <comphelper/types.hxx>
29 #include <tools/debug.hxx>
30 #include <tools/diagnose_ex.h>
32 #include <memory>
34 namespace dbaccess
36 using namespace ::com::sun::star::uno;
37 using namespace ::com::sun::star::util;
38 using namespace ::com::sun::star::ucb;
39 using namespace ::com::sun::star::beans;
40 using namespace ::com::sun::star::lang;
41 using namespace ::com::sun::star::sdbc;
42 using namespace ::com::sun::star::frame;
43 using namespace ::com::sun::star::io;
44 using namespace ::com::sun::star::embed;
45 using namespace ::com::sun::star::container;
46 using namespace ::comphelper;
47 using namespace ::cppu;
49 #define DISPATCH_SAVEAS 0
50 #define DISPATCH_SAVE 1
51 #define DISPATCH_CLOSEDOC 2
52 #define DISPATCH_CLOSEWIN 3
53 #define DISPATCH_CLOSEFRAME 4
54 #define DISPATCH_RELOAD 5
55 // the OSL_ENSURE in CTOR has to be changed too, when adding new defines
57 void SAL_CALL OInterceptor::dispose()
58 throw( RuntimeException )
60 EventObject aEvt( *this );
62 osl::MutexGuard aGuard(m_aMutex);
64 if ( m_pDisposeEventListeners && m_pDisposeEventListeners->getLength() )
65 m_pDisposeEventListeners->disposeAndClear( aEvt );
67 if ( m_pStatCL )
68 m_pStatCL->disposeAndClear( aEvt );
70 m_xSlaveDispatchProvider.clear();
71 m_xMasterDispatchProvider.clear();
73 m_pContentHolder = nullptr;
77 OInterceptor::OInterceptor( ODocumentDefinition* _pContentHolder )
78 :m_pContentHolder( _pContentHolder )
79 ,m_aInterceptedURL(7)
80 ,m_pDisposeEventListeners(nullptr)
81 ,m_pStatCL(nullptr)
84 OSL_ENSURE(DISPATCH_RELOAD < m_aInterceptedURL.getLength(),"Illegal size.");
86 m_aInterceptedURL[DISPATCH_SAVEAS] = ".uno:SaveAs";
87 m_aInterceptedURL[DISPATCH_SAVE] = ".uno:Save";
88 m_aInterceptedURL[DISPATCH_CLOSEDOC] = ".uno:CloseDoc";
89 m_aInterceptedURL[DISPATCH_CLOSEWIN] = ".uno:CloseWin";
90 m_aInterceptedURL[DISPATCH_CLOSEFRAME] = ".uno:CloseFrame";
91 m_aInterceptedURL[DISPATCH_RELOAD] = ".uno:Reload";
95 OInterceptor::~OInterceptor()
97 delete m_pDisposeEventListeners;
98 delete m_pStatCL;
101 struct DispatchHelper
103 URL aURL;
104 Sequence<PropertyValue > aArguments;
107 //XDispatch
108 void SAL_CALL OInterceptor::dispatch( const URL& URL,const Sequence<PropertyValue >& Arguments ) throw (RuntimeException, std::exception)
110 ::osl::MutexGuard aGuard( m_aMutex );
111 if ( !m_pContentHolder )
112 return;
114 if ( URL.Complete == m_aInterceptedURL[ DISPATCH_SAVE ] )
116 m_pContentHolder->save( false );
117 return;
120 if ( URL.Complete == m_aInterceptedURL[ DISPATCH_RELOAD ] )
122 ODocumentDefinition::fillReportData(
123 m_pContentHolder->getContext(),
124 m_pContentHolder->getComponent(),
125 m_pContentHolder->getConnection()
127 return;
130 if( URL.Complete == m_aInterceptedURL[ DISPATCH_SAVEAS ] )
132 if ( m_pContentHolder->isNewReport() )
134 m_pContentHolder->saveAs();
136 else if ( m_xSlaveDispatchProvider.is() )
138 Sequence< PropertyValue > aNewArgs = Arguments;
139 sal_Int32 nInd = 0;
141 while( nInd < aNewArgs.getLength() )
143 if ( aNewArgs[nInd].Name == "SaveTo" )
145 aNewArgs[nInd].Value <<= true;
146 break;
148 nInd++;
151 if ( nInd == aNewArgs.getLength() )
153 aNewArgs.realloc( nInd + 1 );
154 aNewArgs[nInd].Name = "SaveTo";
155 aNewArgs[nInd].Value <<= true;
158 Reference< XDispatch > xDispatch = m_xSlaveDispatchProvider->queryDispatch(URL, "_self", 0 );
159 if ( xDispatch.is() )
160 xDispatch->dispatch( URL, aNewArgs );
162 return;
165 if ( URL.Complete == m_aInterceptedURL[ DISPATCH_CLOSEDOC ]
166 || URL.Complete == m_aInterceptedURL[ DISPATCH_CLOSEWIN ]
167 || URL.Complete == m_aInterceptedURL[ DISPATCH_CLOSEFRAME ]
170 DispatchHelper* pHelper = new DispatchHelper;
171 pHelper->aArguments = Arguments;
172 pHelper->aURL = URL;
173 Application::PostUserEvent( LINK( this, OInterceptor, OnDispatch ), pHelper );
174 return;
178 IMPL_LINK( OInterceptor, OnDispatch, void*, _pDispatcher, void )
180 std::unique_ptr<DispatchHelper> pHelper( static_cast< DispatchHelper* >( _pDispatcher ) );
183 if ( m_pContentHolder && m_pContentHolder->prepareClose() && m_xSlaveDispatchProvider.is() )
185 Reference< XDispatch > xDispatch = m_xSlaveDispatchProvider->queryDispatch(pHelper->aURL, "_self", 0 );
186 if ( xDispatch.is() )
188 Reference< XInterface > xKeepContentHolderAlive( *m_pContentHolder );
189 xDispatch->dispatch( pHelper->aURL,pHelper->aArguments);
193 catch ( const Exception& )
195 DBG_UNHANDLED_EXCEPTION();
199 void SAL_CALL OInterceptor::addStatusListener(
200 const Reference<
201 XStatusListener >& Control,
202 const URL& URL )
203 throw (
204 RuntimeException, std::exception
207 if(!Control.is())
208 return;
210 if ( m_pContentHolder && URL.Complete == m_aInterceptedURL[DISPATCH_SAVEAS] )
211 { // SaveAs
213 if ( !m_pContentHolder->isNewReport() )
215 FeatureStateEvent aStateEvent;
216 aStateEvent.FeatureURL.Complete = m_aInterceptedURL[DISPATCH_SAVEAS];
217 aStateEvent.FeatureDescriptor = "SaveCopyTo";
218 aStateEvent.IsEnabled = true;
219 aStateEvent.Requery = false;
220 aStateEvent.State <<= OUString("($3)");
221 Control->statusChanged(aStateEvent);
225 osl::MutexGuard aGuard(m_aMutex);
226 if(!m_pStatCL)
227 m_pStatCL = new PropertyChangeListenerContainer(m_aMutex);
230 m_pStatCL->addInterface(URL.Complete,Control);
232 else if ( m_pContentHolder && URL.Complete == m_aInterceptedURL[DISPATCH_SAVE] )
233 { // Save
234 FeatureStateEvent aStateEvent;
235 aStateEvent.FeatureURL.Complete = m_aInterceptedURL[DISPATCH_SAVE];
236 aStateEvent.FeatureDescriptor = "Update";
237 aStateEvent.IsEnabled = true;
238 aStateEvent.Requery = false;
240 Control->statusChanged(aStateEvent);
242 osl::MutexGuard aGuard(m_aMutex);
243 if(!m_pStatCL)
244 m_pStatCL = new PropertyChangeListenerContainer(m_aMutex);
247 m_pStatCL->addInterface(URL.Complete,Control);
249 else
251 sal_Int32 i = 2;
252 if(URL.Complete == m_aInterceptedURL[i] ||
253 URL.Complete == m_aInterceptedURL[++i] ||
254 URL.Complete == m_aInterceptedURL[++i] ||
255 URL.Complete == m_aInterceptedURL[i = DISPATCH_RELOAD] )
256 { // Close and return
257 FeatureStateEvent aStateEvent;
258 aStateEvent.FeatureURL.Complete = m_aInterceptedURL[i];
259 aStateEvent.FeatureDescriptor = "Close and Return";
260 aStateEvent.IsEnabled = true;
261 aStateEvent.Requery = false;
262 Control->statusChanged(aStateEvent);
266 osl::MutexGuard aGuard(m_aMutex);
267 if(!m_pStatCL)
268 m_pStatCL = new PropertyChangeListenerContainer(m_aMutex);
271 m_pStatCL->addInterface(URL.Complete,Control);
272 return;
278 void SAL_CALL OInterceptor::removeStatusListener(
279 const Reference<
280 XStatusListener >& Control,
281 const URL& URL )
282 throw (
283 RuntimeException, std::exception
286 if(!(Control.is() && m_pStatCL))
287 return;
288 else
290 m_pStatCL->removeInterface(URL.Complete,Control);
291 return;
296 //XInterceptorInfo
297 Sequence< OUString > SAL_CALL OInterceptor::getInterceptedURLs( ) throw ( RuntimeException, std::exception )
299 // now implemented as update
300 return m_aInterceptedURL;
304 // XDispatchProvider
306 Reference< XDispatch > SAL_CALL OInterceptor::queryDispatch( const URL& URL,const OUString& TargetFrameName,sal_Int32 SearchFlags )
307 throw (RuntimeException, std::exception)
309 osl::MutexGuard aGuard(m_aMutex);
310 const OUString* pIter = m_aInterceptedURL.getConstArray();
311 const OUString* pEnd = pIter + m_aInterceptedURL.getLength();
312 for(;pIter != pEnd;++pIter)
314 if ( URL.Complete == *pIter )
315 return static_cast<XDispatch*>(this);
318 if(m_xSlaveDispatchProvider.is())
319 return m_xSlaveDispatchProvider->queryDispatch(URL,TargetFrameName,SearchFlags);
320 else
321 return Reference<XDispatch>();
324 Sequence< Reference< XDispatch > > SAL_CALL OInterceptor::queryDispatches( const Sequence<DispatchDescriptor >& Requests ) throw ( RuntimeException, std::exception )
326 Sequence< Reference< XDispatch > > aRet;
327 osl::MutexGuard aGuard(m_aMutex);
328 if(m_xSlaveDispatchProvider.is())
329 aRet = m_xSlaveDispatchProvider->queryDispatches(Requests);
330 else
331 aRet.realloc(Requests.getLength());
333 for(sal_Int32 i = 0; i < Requests.getLength(); ++i)
335 const OUString* pIter = m_aInterceptedURL.getConstArray();
336 const OUString* pEnd = pIter + m_aInterceptedURL.getLength();
337 for(;pIter != pEnd;++pIter)
339 if ( Requests[i].FeatureURL.Complete == *pIter )
341 aRet[i] = static_cast<XDispatch*>(this);
342 break;
347 return aRet;
351 //XDispatchProviderInterceptor
353 Reference< XDispatchProvider > SAL_CALL OInterceptor::getSlaveDispatchProvider( ) throw ( RuntimeException, std::exception )
355 osl::MutexGuard aGuard(m_aMutex);
356 return m_xSlaveDispatchProvider;
359 void SAL_CALL
360 OInterceptor::setSlaveDispatchProvider( const Reference< XDispatchProvider >& NewDispatchProvider )
361 throw ( RuntimeException, std::exception )
363 osl::MutexGuard aGuard(m_aMutex);
364 m_xSlaveDispatchProvider = NewDispatchProvider;
368 Reference< XDispatchProvider > SAL_CALL OInterceptor::getMasterDispatchProvider( )
369 throw (
370 RuntimeException, std::exception
373 osl::MutexGuard aGuard(m_aMutex);
374 return m_xMasterDispatchProvider;
378 void SAL_CALL OInterceptor::setMasterDispatchProvider(
379 const Reference< XDispatchProvider >& NewSupplier )
380 throw (
381 RuntimeException, std::exception
384 osl::MutexGuard aGuard(m_aMutex);
385 m_xMasterDispatchProvider = NewSupplier;
388 } // namespace dbaccess
390 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */