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 2761 : SwXDispatchProviderInterceptor::SwXDispatchProviderInterceptor(SwView& rVw) :
43 2761 : m_pView(&rVw)
44 : {
45 2761 : uno::Reference< frame::XFrame> xUnoFrame = m_pView->GetViewFrame()->GetFrame().GetFrameInterface();
46 2761 : m_xIntercepted = uno::Reference< frame::XDispatchProviderInterception>(xUnoFrame, uno::UNO_QUERY);
47 2761 : if(m_xIntercepted.is())
48 : {
49 2761 : m_refCount++;
50 2761 : m_xIntercepted->registerDispatchProviderInterceptor(static_cast<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 2761 : uno::Reference< lang::XComponent> xInterceptedComponent(m_xIntercepted, uno::UNO_QUERY);
54 2761 : if (xInterceptedComponent.is())
55 2761 : xInterceptedComponent->addEventListener(static_cast<lang::XEventListener*>(this));
56 2761 : m_refCount--;
57 2761 : }
58 2761 : }
59 :
60 5514 : SwXDispatchProviderInterceptor::~SwXDispatchProviderInterceptor()
61 : {
62 5514 : }
63 :
64 137260 : 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 137260 : DispatchMutexLock_Impl aLock(*this);
69 137260 : uno::Reference< frame::XDispatch> xResult;
70 : // create some dispatch ...
71 137260 : if(m_pView && aURL.Complete.startsWith(".uno:DataSourceBrowser/"))
72 : {
73 11 : if(aURL.Complete.equalsAscii(cURLFormLetter) ||
74 5 : aURL.Complete.equalsAscii(cURLInsertContent) ||
75 7 : aURL.Complete.equalsAscii(cURLInsertColumns)||
76 1 : aURL.Complete.equalsAscii(cURLDocumentDataSource))
77 : {
78 4 : if(!m_xDispatch.is())
79 1 : m_xDispatch = new SwXDispatch(*m_pView);
80 4 : xResult = m_xDispatch;
81 : }
82 : }
83 :
84 : // ask our slave provider
85 137260 : if (!xResult.is() && m_xSlaveDispatcher.is())
86 137256 : xResult = m_xSlaveDispatcher->queryDispatch(aURL, aTargetFrameName, nSearchFlags);
87 :
88 137260 : 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_Int32 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 2757 : uno::Reference< frame::XDispatchProvider > SwXDispatchProviderInterceptor::getSlaveDispatchProvider( )
107 : throw(uno::RuntimeException, std::exception)
108 : {
109 2757 : DispatchMutexLock_Impl aLock(*this);
110 2757 : return m_xSlaveDispatcher;
111 : }
112 :
113 5518 : void SwXDispatchProviderInterceptor::setSlaveDispatchProvider(
114 : const uno::Reference< frame::XDispatchProvider >& xNewDispatchProvider ) throw(uno::RuntimeException, std::exception)
115 : {
116 5518 : DispatchMutexLock_Impl aLock(*this);
117 5518 : m_xSlaveDispatcher = xNewDispatchProvider;
118 5518 : }
119 :
120 2757 : uno::Reference< frame::XDispatchProvider > SwXDispatchProviderInterceptor::getMasterDispatchProvider( )
121 : throw(uno::RuntimeException, std::exception)
122 : {
123 2757 : DispatchMutexLock_Impl aLock(*this);
124 2757 : return m_xMasterDispatcher;
125 : }
126 :
127 5518 : void SwXDispatchProviderInterceptor::setMasterDispatchProvider(
128 : const uno::Reference< frame::XDispatchProvider >& xNewSupplier ) throw(uno::RuntimeException, std::exception)
129 : {
130 5518 : DispatchMutexLock_Impl aLock(*this);
131 5518 : m_xMasterDispatcher = xNewSupplier;
132 5518 : }
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(static_cast<frame::XDispatchProviderInterceptor*>(this));
141 0 : uno::Reference< lang::XComponent> xInterceptedComponent(m_xIntercepted, uno::UNO_QUERY);
142 0 : if (xInterceptedComponent.is())
143 0 : xInterceptedComponent->removeEventListener(static_cast<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 5514 : const uno::Sequence< sal_Int8 > & SwXDispatchProviderInterceptor::getUnoTunnelId()
155 : {
156 5514 : return theSwXDispatchProviderInterceptorUnoTunnelId::get().getSeq();
157 : }
158 :
159 2757 : sal_Int64 SwXDispatchProviderInterceptor::getSomething(
160 : const uno::Sequence< sal_Int8 >& aIdentifier )
161 : throw(uno::RuntimeException, std::exception)
162 : {
163 5514 : if( aIdentifier.getLength() == 16
164 8271 : && 0 == memcmp( getUnoTunnelId().getConstArray(),
165 5514 : aIdentifier.getConstArray(), 16 ) )
166 : {
167 2757 : return sal::static_int_cast< sal_Int64 >( reinterpret_cast< sal_IntPtr >( this ));
168 : }
169 0 : return 0;
170 : }
171 :
172 2757 : void SwXDispatchProviderInterceptor::Invalidate()
173 : {
174 2757 : DispatchMutexLock_Impl aLock(*this);
175 2757 : if (m_xIntercepted.is())
176 : {
177 2757 : m_xIntercepted->releaseDispatchProviderInterceptor(static_cast<frame::XDispatchProviderInterceptor*>(this));
178 2757 : uno::Reference< lang::XComponent> xInterceptedComponent(m_xIntercepted, uno::UNO_QUERY);
179 2757 : if (xInterceptedComponent.is())
180 2757 : xInterceptedComponent->removeEventListener(static_cast<lang::XEventListener*>(this));
181 2757 : m_xDispatch = 0;
182 : }
183 2757 : m_xIntercepted = NULL;
184 2757 : m_pView = 0;
185 2757 : }
186 :
187 1 : SwXDispatch::SwXDispatch(SwView& rVw) :
188 : m_pView(&rVw),
189 : m_bOldEnable(false),
190 1 : m_bListenerAdded(false)
191 : {
192 1 : }
193 :
194 3 : SwXDispatch::~SwXDispatch()
195 : {
196 1 : 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 2 : }
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 : SwDBManager::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 = *static_cast<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 4 : void SwXDispatch::addStatusListener(
273 : const uno::Reference< frame::XStatusListener >& xControl, const util::URL& aURL ) throw(uno::RuntimeException, std::exception)
274 : {
275 4 : if(!m_pView)
276 0 : throw uno::RuntimeException();
277 4 : ShellModes eMode = m_pView->GetShellMode();
278 0 : bool bEnable = SHELL_MODE_TEXT == eMode ||
279 0 : SHELL_MODE_LIST_TEXT == eMode ||
280 4 : SHELL_MODE_TABLE_TEXT == eMode ||
281 4 : SHELL_MODE_TABLE_LIST_TEXT == eMode;
282 :
283 4 : m_bOldEnable = bEnable;
284 4 : frame::FeatureStateEvent aEvent;
285 4 : aEvent.IsEnabled = bEnable;
286 4 : aEvent.Source = *static_cast<cppu::OWeakObject*>(this);
287 4 : aEvent.FeatureURL = aURL;
288 :
289 : // one of the URLs requires a special state ....
290 4 : if (aURL.Complete.equalsAscii(cURLDocumentDataSource))
291 : {
292 1 : const SwDBData& rData = m_pView->GetWrtShell().GetDBDesc();
293 :
294 1 : svx::ODataAccessDescriptor aDescriptor;
295 1 : aDescriptor.setDataSource(rData.sDataSource);
296 1 : aDescriptor[svx::daCommand] <<= rData.sCommand;
297 1 : aDescriptor[svx::daCommandType] <<= rData.nCommandType;
298 :
299 1 : aEvent.State <<= aDescriptor.createPropertyValueSequence();
300 1 : aEvent.IsEnabled = !rData.sDataSource.isEmpty();
301 : }
302 :
303 4 : xControl->statusChanged( aEvent );
304 :
305 4 : StatusListenerList::iterator aListIter = m_aListenerList.begin();
306 8 : StatusStruct_Impl aStatus;
307 4 : aStatus.xListener = xControl;
308 4 : aStatus.aURL = aURL;
309 4 : m_aListenerList.insert(aListIter, aStatus);
310 :
311 4 : if(!m_bListenerAdded)
312 : {
313 1 : uno::Reference<view::XSelectionSupplier> xSupplier = m_pView->GetUNOObject();
314 2 : uno::Reference<view::XSelectionChangeListener> xThis = this;
315 1 : xSupplier->addSelectionChangeListener(xThis);
316 2 : m_bListenerAdded = true;
317 4 : }
318 4 : }
319 :
320 4 : void SwXDispatch::removeStatusListener(
321 : const uno::Reference< frame::XStatusListener >& xControl, const util::URL& ) throw(uno::RuntimeException, std::exception)
322 : {
323 4 : StatusListenerList::iterator aListIter = m_aListenerList.begin();
324 4 : for(aListIter = m_aListenerList.begin(); aListIter != m_aListenerList.end(); ++aListIter)
325 : {
326 4 : StatusStruct_Impl aStatus = *aListIter;
327 4 : if(aStatus.xListener.get() == xControl.get())
328 : {
329 4 : m_aListenerList.erase(aListIter);
330 4 : break;
331 : }
332 0 : }
333 4 : if(m_aListenerList.empty() && m_pView)
334 : {
335 1 : uno::Reference<view::XSelectionSupplier> xSupplier = m_pView->GetUNOObject();
336 2 : uno::Reference<view::XSelectionChangeListener> xThis = this;
337 1 : xSupplier->removeSelectionChangeListener(xThis);
338 2 : m_bListenerAdded = false;
339 : }
340 4 : }
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 = *static_cast<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 = static_cast<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 8 : const sal_Char* SwXDispatch::GetDBChangeURL()
387 : {
388 8 : return cInternalDBChangeNotification;
389 : }
390 :
391 156567 : SwXDispatchProviderInterceptor::DispatchMutexLock_Impl::DispatchMutexLock_Impl(
392 156567 : SwXDispatchProviderInterceptor& )
393 : {
394 156567 : }
395 :
396 156567 : SwXDispatchProviderInterceptor::DispatchMutexLock_Impl::~DispatchMutexLock_Impl()
397 : {
398 156744 : }
399 :
400 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|