Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <sfx2/viewfrm.hxx>
21 : #include <comphelper/uno3.hxx>
22 : #include <svx/dataaccessdescriptor.hxx>
23 : #include <svl/smplhint.hxx>
24 : #include <vcl/svapp.hxx>
25 :
26 : #include <com/sun/star/frame/XDispatchProviderInterception.hpp>
27 : #include <com/sun/star/view/XSelectionSupplier.hpp>
28 : #include <com/sun/star/sdb/CommandType.hpp>
29 :
30 : #include "dispuno.hxx"
31 : #include "tabvwsh.hxx"
32 : #include "dbdocfun.hxx"
33 : #include "dbdata.hxx"
34 :
35 : using namespace com::sun::star;
36 :
37 : static const char* cURLInsertColumns = ".uno:DataSourceBrowser/InsertColumns"; //data into text
38 : static const char* cURLDocDataSource = ".uno:DataSourceBrowser/DocumentDataSource";
39 :
40 0 : static uno::Reference<view::XSelectionSupplier> lcl_GetSelectionSupplier( SfxViewShell* pViewShell )
41 : {
42 0 : if ( pViewShell )
43 : {
44 0 : SfxViewFrame* pViewFrame = pViewShell->GetViewFrame();
45 0 : if (pViewFrame)
46 : {
47 0 : return uno::Reference<view::XSelectionSupplier>( pViewFrame->GetFrame().GetController(), uno::UNO_QUERY );
48 : }
49 : }
50 0 : return uno::Reference<view::XSelectionSupplier>();
51 : }
52 :
53 :
54 0 : ScDispatchProviderInterceptor::ScDispatchProviderInterceptor(ScTabViewShell* pViewSh) :
55 0 : pViewShell( pViewSh )
56 : {
57 0 : if ( pViewShell )
58 : {
59 0 : m_xIntercepted.set(uno::Reference<frame::XDispatchProviderInterception>(pViewShell->GetViewFrame()->GetFrame().GetFrameInterface(), uno::UNO_QUERY));
60 0 : if (m_xIntercepted.is())
61 : {
62 0 : comphelper::increment( m_refCount );
63 :
64 0 : m_xIntercepted->registerDispatchProviderInterceptor(
65 0 : static_cast<frame::XDispatchProviderInterceptor*>(this));
66 : // this should make us the top-level dispatch-provider for the component, via a call to our
67 : // setDispatchProvider we should have got an fallback for requests we (i.e. our master) cannot fullfill
68 0 : uno::Reference<lang::XComponent> xInterceptedComponent(m_xIntercepted, uno::UNO_QUERY);
69 0 : if (xInterceptedComponent.is())
70 0 : xInterceptedComponent->addEventListener(static_cast<lang::XEventListener*>(this));
71 :
72 0 : comphelper::decrement( m_refCount );
73 : }
74 :
75 0 : StartListening(*pViewShell);
76 : }
77 0 : }
78 :
79 0 : ScDispatchProviderInterceptor::~ScDispatchProviderInterceptor()
80 : {
81 0 : if (pViewShell)
82 0 : EndListening(*pViewShell);
83 0 : }
84 :
85 0 : void ScDispatchProviderInterceptor::Notify( SfxBroadcaster&, const SfxHint& rHint )
86 : {
87 0 : if ( rHint.ISA( SfxSimpleHint ) &&
88 0 : ((const SfxSimpleHint&)rHint).GetId() == SFX_HINT_DYING )
89 0 : pViewShell = NULL;
90 0 : }
91 :
92 : // XDispatchProvider
93 :
94 0 : uno::Reference<frame::XDispatch> SAL_CALL ScDispatchProviderInterceptor::queryDispatch(
95 : const util::URL& aURL, const OUString& aTargetFrameName,
96 : sal_Int32 nSearchFlags )
97 : throw(uno::RuntimeException, std::exception)
98 : {
99 0 : SolarMutexGuard aGuard;
100 :
101 0 : uno::Reference<frame::XDispatch> xResult;
102 : // create some dispatch ...
103 0 : if ( pViewShell && (
104 0 : aURL.Complete.equalsAscii(cURLInsertColumns) ||
105 0 : aURL.Complete.equalsAscii(cURLDocDataSource) ) )
106 : {
107 0 : if (!m_xMyDispatch.is())
108 0 : m_xMyDispatch = new ScDispatch( pViewShell );
109 0 : xResult = m_xMyDispatch;
110 : }
111 :
112 : // ask our slave provider
113 0 : if (!xResult.is() && m_xSlaveDispatcher.is())
114 0 : xResult = m_xSlaveDispatcher->queryDispatch(aURL, aTargetFrameName, nSearchFlags);
115 :
116 0 : return xResult;
117 : }
118 :
119 : uno::Sequence< uno::Reference<frame::XDispatch> > SAL_CALL
120 0 : ScDispatchProviderInterceptor::queryDispatches(
121 : const uno::Sequence<frame::DispatchDescriptor>& aDescripts )
122 : throw(uno::RuntimeException, std::exception)
123 : {
124 0 : SolarMutexGuard aGuard;
125 :
126 0 : uno::Sequence< uno::Reference< frame::XDispatch> > aReturn(aDescripts.getLength());
127 0 : uno::Reference< frame::XDispatch>* pReturn = aReturn.getArray();
128 0 : const frame::DispatchDescriptor* pDescripts = aDescripts.getConstArray();
129 0 : for (sal_Int16 i=0; i<aDescripts.getLength(); ++i, ++pReturn, ++pDescripts)
130 : {
131 0 : *pReturn = queryDispatch(pDescripts->FeatureURL,
132 0 : pDescripts->FrameName, pDescripts->SearchFlags);
133 : }
134 0 : return aReturn;
135 : }
136 :
137 : // XDispatchProviderInterceptor
138 :
139 : uno::Reference<frame::XDispatchProvider> SAL_CALL
140 0 : ScDispatchProviderInterceptor::getSlaveDispatchProvider()
141 : throw(uno::RuntimeException, std::exception)
142 : {
143 0 : SolarMutexGuard aGuard;
144 0 : return m_xSlaveDispatcher;
145 : }
146 :
147 0 : void SAL_CALL ScDispatchProviderInterceptor::setSlaveDispatchProvider(
148 : const uno::Reference<frame::XDispatchProvider>& xNewDispatchProvider )
149 : throw(uno::RuntimeException, std::exception)
150 : {
151 0 : SolarMutexGuard aGuard;
152 0 : m_xSlaveDispatcher.set(xNewDispatchProvider);
153 0 : }
154 :
155 : uno::Reference<frame::XDispatchProvider> SAL_CALL
156 0 : ScDispatchProviderInterceptor::getMasterDispatchProvider()
157 : throw(uno::RuntimeException, std::exception)
158 : {
159 0 : SolarMutexGuard aGuard;
160 0 : return m_xMasterDispatcher;
161 : }
162 :
163 0 : void SAL_CALL ScDispatchProviderInterceptor::setMasterDispatchProvider(
164 : const uno::Reference<frame::XDispatchProvider>& xNewSupplier )
165 : throw(uno::RuntimeException, std::exception)
166 : {
167 0 : SolarMutexGuard aGuard;
168 0 : m_xMasterDispatcher.set(xNewSupplier);
169 0 : }
170 :
171 : // XEventListener
172 :
173 0 : void SAL_CALL ScDispatchProviderInterceptor::disposing( const lang::EventObject& /* Source */ )
174 : throw(::com::sun::star::uno::RuntimeException, std::exception)
175 : {
176 0 : SolarMutexGuard aGuard;
177 :
178 0 : if (m_xIntercepted.is())
179 : {
180 0 : m_xIntercepted->releaseDispatchProviderInterceptor(
181 0 : static_cast<frame::XDispatchProviderInterceptor*>(this));
182 0 : uno::Reference<lang::XComponent> xInterceptedComponent(m_xIntercepted, uno::UNO_QUERY);
183 0 : if (xInterceptedComponent.is())
184 0 : xInterceptedComponent->removeEventListener(static_cast<lang::XEventListener*>(this));
185 :
186 0 : m_xMyDispatch = NULL;
187 : }
188 0 : m_xIntercepted = NULL;
189 0 : }
190 :
191 0 : ScDispatch::ScDispatch(ScTabViewShell* pViewSh) :
192 : pViewShell( pViewSh ),
193 0 : bListeningToView( false )
194 : {
195 0 : if (pViewShell)
196 0 : StartListening(*pViewShell);
197 0 : }
198 :
199 0 : ScDispatch::~ScDispatch()
200 : {
201 0 : if (pViewShell)
202 0 : EndListening(*pViewShell);
203 :
204 0 : if (bListeningToView && pViewShell)
205 : {
206 0 : uno::Reference<view::XSelectionSupplier> xSupplier(lcl_GetSelectionSupplier( pViewShell ));
207 0 : if ( xSupplier.is() )
208 0 : xSupplier->removeSelectionChangeListener(this);
209 : }
210 0 : }
211 :
212 0 : void ScDispatch::Notify( SfxBroadcaster&, const SfxHint& rHint )
213 : {
214 0 : if ( rHint.ISA( SfxSimpleHint ) &&
215 0 : ((const SfxSimpleHint&)rHint).GetId() == SFX_HINT_DYING )
216 0 : pViewShell = NULL;
217 0 : }
218 :
219 : // XDispatch
220 :
221 0 : void SAL_CALL ScDispatch::dispatch( const util::URL& aURL,
222 : const uno::Sequence<beans::PropertyValue>& aArgs )
223 : throw(uno::RuntimeException, std::exception)
224 : {
225 0 : SolarMutexGuard aGuard;
226 :
227 0 : sal_Bool bDone = false;
228 0 : if ( pViewShell && aURL.Complete.equalsAscii(cURLInsertColumns) )
229 : {
230 0 : ScViewData* pViewData = pViewShell->GetViewData();
231 0 : ScAddress aPos( pViewData->GetCurX(), pViewData->GetCurY(), pViewData->GetTabNo() );
232 :
233 0 : ScDBDocFunc aFunc( *pViewData->GetDocShell() );
234 0 : bDone = aFunc.DoImportUno( aPos, aArgs );
235 : }
236 : // cURLDocDataSource is never dispatched
237 :
238 0 : if (!bDone)
239 0 : throw uno::RuntimeException();
240 0 : }
241 :
242 0 : static void lcl_FillDataSource( frame::FeatureStateEvent& rEvent, const ScImportParam& rParam )
243 : {
244 0 : rEvent.IsEnabled = rParam.bImport;
245 :
246 0 : ::svx::ODataAccessDescriptor aDescriptor;
247 0 : if ( rParam.bImport )
248 : {
249 : sal_Int32 nType = rParam.bSql ? sdb::CommandType::COMMAND :
250 0 : ( (rParam.nType == ScDbQuery) ? sdb::CommandType::QUERY :
251 0 : sdb::CommandType::TABLE );
252 :
253 0 : aDescriptor.setDataSource(rParam.aDBName);
254 0 : aDescriptor[svx::daCommand] <<= rParam.aStatement;
255 0 : aDescriptor[svx::daCommandType] <<= nType;
256 : }
257 : else
258 : {
259 : // descriptor has to be complete anyway
260 :
261 0 : OUString aEmpty;
262 0 : aDescriptor[svx::daDataSource] <<= aEmpty;
263 0 : aDescriptor[svx::daCommand] <<= aEmpty;
264 0 : aDescriptor[svx::daCommandType] <<= (sal_Int32)sdb::CommandType::TABLE;
265 : }
266 0 : rEvent.State <<= aDescriptor.createPropertyValueSequence();
267 0 : }
268 :
269 0 : void SAL_CALL ScDispatch::addStatusListener(
270 : const uno::Reference<frame::XStatusListener>& xListener,
271 : const util::URL& aURL)
272 : throw(uno::RuntimeException, std::exception)
273 : {
274 0 : SolarMutexGuard aGuard;
275 :
276 0 : if (!pViewShell)
277 0 : throw uno::RuntimeException();
278 :
279 : // initial state
280 0 : frame::FeatureStateEvent aEvent;
281 0 : aEvent.IsEnabled = sal_True;
282 0 : aEvent.Source.set(static_cast<cppu::OWeakObject*>(this));
283 0 : aEvent.FeatureURL = aURL;
284 :
285 0 : if ( aURL.Complete.equalsAscii(cURLDocDataSource) )
286 : {
287 : uno::Reference<frame::XStatusListener>* pObj =
288 0 : new uno::Reference<frame::XStatusListener>( xListener );
289 0 : aDataSourceListeners.push_back( pObj );
290 :
291 0 : if (!bListeningToView)
292 : {
293 0 : uno::Reference<view::XSelectionSupplier> xSupplier(lcl_GetSelectionSupplier( pViewShell ));
294 0 : if ( xSupplier.is() )
295 0 : xSupplier->addSelectionChangeListener(this);
296 0 : bListeningToView = true;
297 : }
298 :
299 0 : ScDBData* pDBData = pViewShell->GetDBData(false,SC_DB_OLD);
300 0 : if ( pDBData )
301 0 : pDBData->GetImportParam( aLastImport );
302 0 : lcl_FillDataSource( aEvent, aLastImport ); // modifies State, IsEnabled
303 : }
304 : //! else add to listener for "enabled" changes?
305 :
306 0 : xListener->statusChanged( aEvent );
307 0 : }
308 :
309 0 : void SAL_CALL ScDispatch::removeStatusListener(
310 : const uno::Reference<frame::XStatusListener>& xListener,
311 : const util::URL& aURL )
312 : throw(uno::RuntimeException, std::exception)
313 : {
314 0 : SolarMutexGuard aGuard;
315 :
316 0 : if ( aURL.Complete.equalsAscii(cURLDocDataSource) )
317 : {
318 0 : sal_uInt16 nCount = aDataSourceListeners.size();
319 0 : for ( sal_uInt16 n=nCount; n--; )
320 : {
321 0 : uno::Reference<frame::XStatusListener>& rObj = aDataSourceListeners[n];
322 0 : if ( rObj == xListener )
323 : {
324 0 : aDataSourceListeners.erase( aDataSourceListeners.begin() + n );
325 0 : break;
326 : }
327 : }
328 :
329 0 : if ( aDataSourceListeners.empty() && pViewShell )
330 : {
331 0 : uno::Reference<view::XSelectionSupplier> xSupplier(lcl_GetSelectionSupplier( pViewShell ));
332 0 : if ( xSupplier.is() )
333 0 : xSupplier->removeSelectionChangeListener(this);
334 0 : bListeningToView = false;
335 : }
336 0 : }
337 0 : }
338 :
339 : // XSelectionChangeListener
340 :
341 0 : void SAL_CALL ScDispatch::selectionChanged( const ::com::sun::star::lang::EventObject& /* aEvent */ )
342 : throw (::com::sun::star::uno::RuntimeException, std::exception)
343 : {
344 : // currently only called for URL cURLDocDataSource
345 :
346 0 : if ( pViewShell )
347 : {
348 0 : ScImportParam aNewImport;
349 0 : ScDBData* pDBData = pViewShell->GetDBData(false,SC_DB_OLD);
350 0 : if ( pDBData )
351 0 : pDBData->GetImportParam( aNewImport );
352 :
353 : // notify listeners only if data source has changed
354 0 : if ( aNewImport.bImport != aLastImport.bImport ||
355 0 : aNewImport.aDBName != aLastImport.aDBName ||
356 0 : aNewImport.aStatement != aLastImport.aStatement ||
357 0 : aNewImport.bSql != aLastImport.bSql ||
358 0 : aNewImport.nType != aLastImport.nType )
359 : {
360 0 : frame::FeatureStateEvent aEvent;
361 0 : aEvent.Source.set(static_cast<cppu::OWeakObject*>(this));
362 0 : aEvent.FeatureURL.Complete = OUString::createFromAscii( cURLDocDataSource );
363 :
364 0 : lcl_FillDataSource( aEvent, aNewImport ); // modifies State, IsEnabled
365 :
366 0 : for ( sal_uInt16 n=0; n<aDataSourceListeners.size(); n++ )
367 0 : aDataSourceListeners[n]->statusChanged( aEvent );
368 :
369 0 : aLastImport = aNewImport;
370 0 : }
371 : }
372 0 : }
373 :
374 : // XEventListener
375 :
376 0 : void SAL_CALL ScDispatch::disposing( const ::com::sun::star::lang::EventObject& rSource )
377 : throw (::com::sun::star::uno::RuntimeException, std::exception)
378 : {
379 0 : uno::Reference<view::XSelectionSupplier> xSupplier(rSource.Source, uno::UNO_QUERY);
380 0 : xSupplier->removeSelectionChangeListener(this);
381 0 : bListeningToView = false;
382 :
383 0 : lang::EventObject aEvent;
384 0 : aEvent.Source.set(static_cast<cppu::OWeakObject*>(this));
385 0 : for ( sal_uInt16 n=0; n<aDataSourceListeners.size(); n++ )
386 0 : aDataSourceListeners[n]->disposing( aEvent );
387 :
388 0 : pViewShell = NULL;
389 0 : }
390 :
391 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|