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 <config_features.h>
21 :
22 : #include <vcl/svapp.hxx>
23 : #include <sfx2/viewfrm.hxx>
24 : #include <sfx2/dispatch.hxx>
25 : #include <svx/dataaccessdescriptor.hxx>
26 : #include <comphelper/servicehelper.hxx>
27 : #include <unodispatch.hxx>
28 : #include <unobaseclass.hxx>
29 : #include <view.hxx>
30 : #include <cmdid.h>
31 : #include "wrtsh.hxx"
32 : #include "dbmgr.hxx"
33 :
34 : using namespace ::com::sun::star;
35 :
36 : static const char* cURLFormLetter = ".uno:DataSourceBrowser/FormLetter";
37 : static const char* cURLInsertContent = ".uno:DataSourceBrowser/InsertContent";//data into fields
38 : static const char* cURLInsertColumns = ".uno:DataSourceBrowser/InsertColumns";//data into text
39 : static const char* cURLDocumentDataSource = ".uno:DataSourceBrowser/DocumentDataSource";//current data source of the document
40 : static const sal_Char* cInternalDBChangeNotification = ".uno::Writer/DataSourceChanged";
41 :
42 4708 : SwXDispatchProviderInterceptor::SwXDispatchProviderInterceptor(SwView& rVw) :
43 4708 : m_pView(&rVw)
44 : {
45 4708 : uno::Reference< frame::XFrame> xUnoFrame = m_pView->GetViewFrame()->GetFrame().GetFrameInterface();
46 4708 : m_xIntercepted = uno::Reference< frame::XDispatchProviderInterception>(xUnoFrame, uno::UNO_QUERY);
47 4708 : if(m_xIntercepted.is())
48 : {
49 4708 : m_refCount++;
50 4708 : m_xIntercepted->registerDispatchProviderInterceptor((frame::XDispatchProviderInterceptor*)this);
51 : // this should make us the top-level dispatch-provider for the component, via a call to our
52 : // setDispatchProvider we should have got an fallback for requests we (i.e. our master) cannot fulfill
53 4708 : uno::Reference< lang::XComponent> xInterceptedComponent(m_xIntercepted, uno::UNO_QUERY);
54 4708 : if (xInterceptedComponent.is())
55 4708 : xInterceptedComponent->addEventListener((lang::XEventListener*)this);
56 4708 : m_refCount--;
57 4708 : }
58 4708 : }
59 :
60 9412 : SwXDispatchProviderInterceptor::~SwXDispatchProviderInterceptor()
61 : {
62 9412 : }
63 :
64 228471 : uno::Reference< frame::XDispatch > SwXDispatchProviderInterceptor::queryDispatch(
65 : const util::URL& aURL, const OUString& aTargetFrameName, sal_Int32 nSearchFlags )
66 : throw(uno::RuntimeException, std::exception)
67 : {
68 228471 : DispatchMutexLock_Impl aLock(*this);
69 228471 : uno::Reference< frame::XDispatch> xResult;
70 : // create some dispatch ...
71 228471 : if(m_pView && aURL.Complete.startsWith(".uno:DataSourceBrowser/"))
72 : {
73 22 : if(aURL.Complete.equalsAscii(cURLFormLetter) ||
74 10 : aURL.Complete.equalsAscii(cURLInsertContent) ||
75 14 : aURL.Complete.equalsAscii(cURLInsertColumns)||
76 2 : aURL.Complete.equalsAscii(cURLDocumentDataSource))
77 : {
78 8 : if(!m_xDispatch.is())
79 2 : m_xDispatch = new SwXDispatch(*m_pView);
80 8 : xResult = m_xDispatch;
81 : }
82 : }
83 :
84 : // ask our slave provider
85 228471 : if (!xResult.is() && m_xSlaveDispatcher.is())
86 228463 : xResult = m_xSlaveDispatcher->queryDispatch(aURL, aTargetFrameName, nSearchFlags);
87 :
88 228471 : return xResult;
89 : }
90 :
91 0 : uno::Sequence< uno::Reference< frame::XDispatch > > SwXDispatchProviderInterceptor::queryDispatches(
92 : const uno::Sequence< frame::DispatchDescriptor >& aDescripts ) throw(uno::RuntimeException, std::exception)
93 : {
94 0 : DispatchMutexLock_Impl aLock(*this);
95 0 : uno::Sequence< uno::Reference< frame::XDispatch> > aReturn(aDescripts.getLength());
96 0 : uno::Reference< frame::XDispatch>* pReturn = aReturn.getArray();
97 0 : const frame::DispatchDescriptor* pDescripts = aDescripts.getConstArray();
98 0 : for (sal_Int16 i=0; i<aDescripts.getLength(); ++i, ++pReturn, ++pDescripts)
99 : {
100 0 : *pReturn = queryDispatch(pDescripts->FeatureURL,
101 0 : pDescripts->FrameName, pDescripts->SearchFlags);
102 : }
103 0 : return aReturn;
104 : }
105 :
106 4706 : uno::Reference< frame::XDispatchProvider > SwXDispatchProviderInterceptor::getSlaveDispatchProvider( )
107 : throw(uno::RuntimeException, std::exception)
108 : {
109 4706 : DispatchMutexLock_Impl aLock(*this);
110 4706 : return m_xSlaveDispatcher;
111 : }
112 :
113 9414 : void SwXDispatchProviderInterceptor::setSlaveDispatchProvider(
114 : const uno::Reference< frame::XDispatchProvider >& xNewDispatchProvider ) throw(uno::RuntimeException, std::exception)
115 : {
116 9414 : DispatchMutexLock_Impl aLock(*this);
117 9414 : m_xSlaveDispatcher = xNewDispatchProvider;
118 9414 : }
119 :
120 4706 : uno::Reference< frame::XDispatchProvider > SwXDispatchProviderInterceptor::getMasterDispatchProvider( )
121 : throw(uno::RuntimeException, std::exception)
122 : {
123 4706 : DispatchMutexLock_Impl aLock(*this);
124 4706 : return m_xMasterDispatcher;
125 : }
126 :
127 9414 : void SwXDispatchProviderInterceptor::setMasterDispatchProvider(
128 : const uno::Reference< frame::XDispatchProvider >& xNewSupplier ) throw(uno::RuntimeException, std::exception)
129 : {
130 9414 : DispatchMutexLock_Impl aLock(*this);
131 9414 : m_xMasterDispatcher = xNewSupplier;
132 9414 : }
133 :
134 0 : void SwXDispatchProviderInterceptor::disposing( const lang::EventObject& )
135 : throw(uno::RuntimeException, std::exception)
136 : {
137 0 : DispatchMutexLock_Impl aLock(*this);
138 0 : if (m_xIntercepted.is())
139 : {
140 0 : m_xIntercepted->releaseDispatchProviderInterceptor((frame::XDispatchProviderInterceptor*)this);
141 0 : uno::Reference< lang::XComponent> xInterceptedComponent(m_xIntercepted, uno::UNO_QUERY);
142 0 : if (xInterceptedComponent.is())
143 0 : xInterceptedComponent->removeEventListener((lang::XEventListener*)this);
144 0 : m_xDispatch = 0;
145 : }
146 0 : m_xIntercepted = NULL;
147 0 : }
148 :
149 : namespace
150 : {
151 : class theSwXDispatchProviderInterceptorUnoTunnelId : public rtl::Static< UnoTunnelIdInit, theSwXDispatchProviderInterceptorUnoTunnelId > {};
152 : }
153 :
154 9412 : const uno::Sequence< sal_Int8 > & SwXDispatchProviderInterceptor::getUnoTunnelId()
155 : {
156 9412 : return theSwXDispatchProviderInterceptorUnoTunnelId::get().getSeq();
157 : }
158 :
159 4706 : sal_Int64 SwXDispatchProviderInterceptor::getSomething(
160 : const uno::Sequence< sal_Int8 >& aIdentifier )
161 : throw(uno::RuntimeException, std::exception)
162 : {
163 9412 : if( aIdentifier.getLength() == 16
164 14118 : && 0 == memcmp( getUnoTunnelId().getConstArray(),
165 9412 : aIdentifier.getConstArray(), 16 ) )
166 : {
167 4706 : return sal::static_int_cast< sal_Int64 >( reinterpret_cast< sal_IntPtr >( this ));
168 : }
169 0 : return 0;
170 : }
171 :
172 4706 : void SwXDispatchProviderInterceptor::Invalidate()
173 : {
174 4706 : DispatchMutexLock_Impl aLock(*this);
175 4706 : if (m_xIntercepted.is())
176 : {
177 4706 : m_xIntercepted->releaseDispatchProviderInterceptor((frame::XDispatchProviderInterceptor*)this);
178 4706 : uno::Reference< lang::XComponent> xInterceptedComponent(m_xIntercepted, uno::UNO_QUERY);
179 4706 : if (xInterceptedComponent.is())
180 4706 : xInterceptedComponent->removeEventListener((lang::XEventListener*)this);
181 4706 : m_xDispatch = 0;
182 : }
183 4706 : m_xIntercepted = NULL;
184 4706 : m_pView = 0;
185 4706 : }
186 :
187 2 : SwXDispatch::SwXDispatch(SwView& rVw) :
188 : m_pView(&rVw),
189 : m_bOldEnable(false),
190 2 : m_bListenerAdded(false)
191 : {
192 2 : }
193 :
194 6 : SwXDispatch::~SwXDispatch()
195 : {
196 2 : if(m_bListenerAdded && m_pView)
197 : {
198 0 : uno::Reference<view::XSelectionSupplier> xSupplier = m_pView->GetUNOObject();
199 0 : uno::Reference<view::XSelectionChangeListener> xThis = this;
200 0 : xSupplier->removeSelectionChangeListener(xThis);
201 : }
202 4 : }
203 :
204 0 : void SwXDispatch::dispatch(const util::URL& aURL,
205 : const uno::Sequence< beans::PropertyValue >& aArgs)
206 : throw (uno::RuntimeException, std::exception)
207 : {
208 0 : if(!m_pView)
209 0 : throw uno::RuntimeException();
210 : #if !HAVE_FEATURE_DBCONNECTIVITY
211 : (void) aArgs;
212 : if (false)
213 : {
214 : }
215 : #else
216 0 : SwWrtShell& rSh = m_pView->GetWrtShell();
217 0 : SwDBManager* pDBManager = rSh.GetDBManager();
218 0 : if(aURL.Complete.equalsAscii(cURLInsertContent))
219 : {
220 0 : ::svx::ODataAccessDescriptor aDescriptor(aArgs);
221 0 : SwMergeDescriptor aMergeDesc( DBMGR_MERGE, rSh, aDescriptor );
222 0 : pDBManager->MergeNew(aMergeDesc);
223 : }
224 0 : else if(aURL.Complete.equalsAscii(cURLInsertColumns))
225 : {
226 0 : pDBManager->InsertText(rSh, aArgs);
227 : }
228 0 : else if(aURL.Complete.equalsAscii(cURLFormLetter))
229 : {
230 0 : SfxUsrAnyItem aDBProperties(FN_PARAM_DATABASE_PROPERTIES, uno::makeAny(aArgs));
231 : m_pView->GetViewFrame()->GetDispatcher()->Execute(
232 : FN_MAILMERGE_WIZARD,
233 : SfxCallMode::ASYNCHRON,
234 0 : &aDBProperties, 0L);
235 : }
236 : #endif
237 0 : else if(aURL.Complete.equalsAscii(cURLDocumentDataSource))
238 : {
239 : OSL_FAIL("SwXDispatch::dispatch: this URL is not to be dispatched!");
240 : }
241 0 : else if(aURL.Complete.equalsAscii(cInternalDBChangeNotification))
242 : {
243 0 : frame::FeatureStateEvent aEvent;
244 0 : aEvent.IsEnabled = sal_True;
245 0 : aEvent.Source = *(cppu::OWeakObject*)this;
246 :
247 0 : const SwDBData& rData = m_pView->GetWrtShell().GetDBDesc();
248 0 : ::svx::ODataAccessDescriptor aDescriptor;
249 0 : aDescriptor.setDataSource(rData.sDataSource);
250 0 : aDescriptor[::svx::daCommand] <<= rData.sCommand;
251 0 : aDescriptor[::svx::daCommandType] <<= rData.nCommandType;
252 :
253 0 : aEvent.State <<= aDescriptor.createPropertyValueSequence();
254 0 : aEvent.IsEnabled = !rData.sDataSource.isEmpty();
255 :
256 0 : StatusListenerList::iterator aListIter = m_aListenerList.begin();
257 0 : for(aListIter = m_aListenerList.begin(); aListIter != m_aListenerList.end(); ++aListIter)
258 : {
259 0 : StatusStruct_Impl aStatus = *aListIter;
260 0 : if(aStatus.aURL.Complete.equalsAscii(cURLDocumentDataSource))
261 : {
262 0 : aEvent.FeatureURL = aStatus.aURL;
263 0 : aStatus.xListener->statusChanged( aEvent );
264 : }
265 0 : }
266 : }
267 : else
268 0 : throw uno::RuntimeException();
269 :
270 0 : }
271 :
272 8 : void SwXDispatch::addStatusListener(
273 : const uno::Reference< frame::XStatusListener >& xControl, const util::URL& aURL ) throw(uno::RuntimeException, std::exception)
274 : {
275 8 : if(!m_pView)
276 0 : throw uno::RuntimeException();
277 8 : ShellModes eMode = m_pView->GetShellMode();
278 0 : bool bEnable = SHELL_MODE_TEXT == eMode ||
279 0 : SHELL_MODE_LIST_TEXT == eMode ||
280 8 : SHELL_MODE_TABLE_TEXT == eMode ||
281 8 : SHELL_MODE_TABLE_LIST_TEXT == eMode;
282 :
283 8 : m_bOldEnable = bEnable;
284 8 : frame::FeatureStateEvent aEvent;
285 8 : aEvent.IsEnabled = bEnable;
286 8 : aEvent.Source = *(cppu::OWeakObject*)this;
287 8 : aEvent.FeatureURL = aURL;
288 :
289 : // one of the URLs requires a special state ....
290 8 : if (aURL.Complete.equalsAscii(cURLDocumentDataSource))
291 : {
292 2 : const SwDBData& rData = m_pView->GetWrtShell().GetDBDesc();
293 :
294 2 : ::svx::ODataAccessDescriptor aDescriptor;
295 2 : aDescriptor.setDataSource(rData.sDataSource);
296 2 : aDescriptor[::svx::daCommand] <<= rData.sCommand;
297 2 : aDescriptor[::svx::daCommandType] <<= rData.nCommandType;
298 :
299 2 : aEvent.State <<= aDescriptor.createPropertyValueSequence();
300 2 : aEvent.IsEnabled = !rData.sDataSource.isEmpty();
301 : }
302 :
303 8 : xControl->statusChanged( aEvent );
304 :
305 8 : StatusListenerList::iterator aListIter = m_aListenerList.begin();
306 16 : StatusStruct_Impl aStatus;
307 8 : aStatus.xListener = xControl;
308 8 : aStatus.aURL = aURL;
309 8 : m_aListenerList.insert(aListIter, aStatus);
310 :
311 8 : if(!m_bListenerAdded)
312 : {
313 2 : uno::Reference<view::XSelectionSupplier> xSupplier = m_pView->GetUNOObject();
314 4 : uno::Reference<view::XSelectionChangeListener> xThis = this;
315 2 : xSupplier->addSelectionChangeListener(xThis);
316 4 : m_bListenerAdded = true;
317 8 : }
318 8 : }
319 :
320 8 : void SwXDispatch::removeStatusListener(
321 : const uno::Reference< frame::XStatusListener >& xControl, const util::URL& ) throw(uno::RuntimeException, std::exception)
322 : {
323 8 : StatusListenerList::iterator aListIter = m_aListenerList.begin();
324 8 : for(aListIter = m_aListenerList.begin(); aListIter != m_aListenerList.end(); ++aListIter)
325 : {
326 8 : StatusStruct_Impl aStatus = *aListIter;
327 8 : if(aStatus.xListener.get() == xControl.get())
328 : {
329 8 : m_aListenerList.erase(aListIter);
330 8 : break;
331 : }
332 0 : }
333 8 : if(m_aListenerList.empty() && m_pView)
334 : {
335 2 : uno::Reference<view::XSelectionSupplier> xSupplier = m_pView->GetUNOObject();
336 4 : uno::Reference<view::XSelectionChangeListener> xThis = this;
337 2 : xSupplier->removeSelectionChangeListener(xThis);
338 4 : m_bListenerAdded = false;
339 : }
340 8 : }
341 :
342 0 : void SwXDispatch::selectionChanged( const lang::EventObject& ) throw(uno::RuntimeException, std::exception)
343 : {
344 0 : ShellModes eMode = m_pView->GetShellMode();
345 0 : bool bEnable = SHELL_MODE_TEXT == eMode ||
346 0 : SHELL_MODE_LIST_TEXT == eMode ||
347 0 : SHELL_MODE_TABLE_TEXT == eMode ||
348 0 : SHELL_MODE_TABLE_LIST_TEXT == eMode;
349 0 : if(bEnable != m_bOldEnable)
350 : {
351 0 : m_bOldEnable = bEnable;
352 0 : frame::FeatureStateEvent aEvent;
353 0 : aEvent.IsEnabled = bEnable;
354 0 : aEvent.Source = *(cppu::OWeakObject*)this;
355 :
356 0 : StatusListenerList::iterator aListIter = m_aListenerList.begin();
357 0 : for(aListIter = m_aListenerList.begin(); aListIter != m_aListenerList.end(); ++aListIter)
358 : {
359 0 : StatusStruct_Impl aStatus = *aListIter;
360 0 : aEvent.FeatureURL = aStatus.aURL;
361 0 : if (!aStatus.aURL.Complete.equalsAscii(cURLDocumentDataSource))
362 : // the document's data source does not depend on the selection, so it's state does not change here
363 0 : aStatus.xListener->statusChanged( aEvent );
364 0 : }
365 : }
366 0 : }
367 :
368 0 : void SwXDispatch::disposing( const lang::EventObject& rSource ) throw(uno::RuntimeException, std::exception)
369 : {
370 0 : uno::Reference<view::XSelectionSupplier> xSupplier(rSource.Source, uno::UNO_QUERY);
371 0 : uno::Reference<view::XSelectionChangeListener> xThis = this;
372 0 : xSupplier->removeSelectionChangeListener(xThis);
373 0 : m_bListenerAdded = false;
374 :
375 0 : lang::EventObject aObject;
376 0 : aObject.Source = (cppu::OWeakObject*)this;
377 0 : StatusListenerList::iterator aListIter = m_aListenerList.begin();
378 0 : for(; aListIter != m_aListenerList.end(); ++aListIter)
379 : {
380 0 : StatusStruct_Impl aStatus = *aListIter;
381 0 : aStatus.xListener->disposing(aObject);
382 0 : }
383 0 : m_pView = 0;
384 0 : }
385 :
386 4 : const sal_Char* SwXDispatch::GetDBChangeURL()
387 : {
388 4 : return cInternalDBChangeNotification;
389 : }
390 :
391 261417 : SwXDispatchProviderInterceptor::DispatchMutexLock_Impl::DispatchMutexLock_Impl(
392 261417 : SwXDispatchProviderInterceptor& )
393 : {
394 261417 : }
395 :
396 261417 : SwXDispatchProviderInterceptor::DispatchMutexLock_Impl::~DispatchMutexLock_Impl()
397 : {
398 261687 : }
399 :
400 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|