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