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