LibreOffice Module sw (master) 1
unodispatch.cxx
Go to the documentation of this file.
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#include <config_fuzzers.h>
22
23#include <com/sun/star/frame/XFrame.hpp>
24#include <com/sun/star/view/XSelectionSupplier.hpp>
25
26#include <sfx2/viewfrm.hxx>
27#include <sfx2/dispatch.hxx>
30#include <osl/diagnose.h>
31#include <unodispatch.hxx>
32#include <view.hxx>
33#include <cmdid.h>
34#include <wrtsh.hxx>
35#include <dbmgr.hxx>
36
37using namespace ::com::sun::star;
38
39const char cURLFormLetter[] = ".uno:DataSourceBrowser/FormLetter";
40const char cURLInsertContent[] = ".uno:DataSourceBrowser/InsertContent";//data into fields
41const char cURLInsertColumns[] = ".uno:DataSourceBrowser/InsertColumns";//data into text
42const char cURLDocumentDataSource[] = ".uno:DataSourceBrowser/DocumentDataSource";//current data source of the document
43const char cInternalDBChangeNotification[] = ".uno::Writer/DataSourceChanged";
44
46 m_pView(&rVw)
47{
48 uno::Reference< frame::XFrame> xUnoFrame = m_pView->GetViewFrame().GetFrame().GetFrameInterface();
49 m_xIntercepted.set(xUnoFrame, uno::UNO_QUERY);
50 if(m_xIntercepted.is())
51 {
52 osl_atomic_increment(&m_refCount);
53 m_xIntercepted->registerDispatchProviderInterceptor(static_cast<frame::XDispatchProviderInterceptor*>(this));
54 // this should make us the top-level dispatch-provider for the component, via a call to our
55 // setDispatchProvider we should have got a fallback for requests we (i.e. our master) cannot fulfill
56 uno::Reference< lang::XComponent> xInterceptedComponent(m_xIntercepted, uno::UNO_QUERY);
57 if (xInterceptedComponent.is())
58 xInterceptedComponent->addEventListener(static_cast<lang::XEventListener*>(this));
59 osl_atomic_decrement(&m_refCount);
60 }
61}
62
64{
65}
66
67uno::Reference< frame::XDispatch > SwXDispatchProviderInterceptor::queryDispatch(
68 const util::URL& aURL, const OUString& aTargetFrameName, sal_Int32 nSearchFlags )
69{
71 uno::Reference< frame::XDispatch> xResult;
72 // create some dispatch ...
73 if(m_pView && aURL.Complete.startsWith(".uno:DataSourceBrowser/"))
74 {
75 if(aURL.Complete == cURLFormLetter ||
76 aURL.Complete == cURLInsertContent ||
77 aURL.Complete == cURLInsertColumns ||
78 aURL.Complete == cURLDocumentDataSource)
79 {
80 if(!m_xDispatch.is())
82 xResult = m_xDispatch;
83 }
84 }
85
86 // ask our slave provider
87 if (!xResult.is() && m_xSlaveDispatcher.is())
88 xResult = m_xSlaveDispatcher->queryDispatch(aURL, aTargetFrameName, nSearchFlags);
89
90 return xResult;
91}
92
94{
95 uno::Sequence<OUString> aRet =
96 {
97 OUString(".uno:DataSourceBrowser/*")
98 };
99
100 return aRet;
101}
102
103uno::Sequence< uno::Reference< frame::XDispatch > > SwXDispatchProviderInterceptor::queryDispatches(
104 const uno::Sequence< frame::DispatchDescriptor >& aDescripts )
105{
107 uno::Sequence< uno::Reference< frame::XDispatch> > aReturn(aDescripts.getLength());
108 std::transform(aDescripts.begin(), aDescripts.end(), aReturn.getArray(),
109 [this](const frame::DispatchDescriptor& rDescr) -> uno::Reference<frame::XDispatch> {
110 return queryDispatch(rDescr.FeatureURL, rDescr.FrameName, rDescr.SearchFlags); });
111 return aReturn;
112}
113
114uno::Reference< frame::XDispatchProvider > SwXDispatchProviderInterceptor::getSlaveDispatchProvider( )
115{
117 return m_xSlaveDispatcher;
118}
119
121 const uno::Reference< frame::XDispatchProvider >& xNewDispatchProvider )
122{
124 m_xSlaveDispatcher = xNewDispatchProvider;
125}
126
127uno::Reference< frame::XDispatchProvider > SwXDispatchProviderInterceptor::getMasterDispatchProvider( )
128{
130 return m_xMasterDispatcher;
131}
132
134 const uno::Reference< frame::XDispatchProvider >& xNewSupplier )
135{
137 m_xMasterDispatcher = xNewSupplier;
138}
139
140void SwXDispatchProviderInterceptor::disposing( const lang::EventObject& )
141{
143 if (m_xIntercepted.is())
144 {
145 m_xIntercepted->releaseDispatchProviderInterceptor(static_cast<frame::XDispatchProviderInterceptor*>(this));
146 uno::Reference< lang::XComponent> xInterceptedComponent(m_xIntercepted, uno::UNO_QUERY);
147 if (xInterceptedComponent.is())
148 xInterceptedComponent->removeEventListener(static_cast<lang::XEventListener*>(this));
149 m_xDispatch = nullptr;
150 }
151 m_xIntercepted = nullptr;
152}
153
155{
157 if (m_xIntercepted.is())
158 {
159 m_xIntercepted->releaseDispatchProviderInterceptor(static_cast<frame::XDispatchProviderInterceptor*>(this));
160 uno::Reference< lang::XComponent> xInterceptedComponent(m_xIntercepted, uno::UNO_QUERY);
161 if (xInterceptedComponent.is())
162 xInterceptedComponent->removeEventListener(static_cast<lang::XEventListener*>(this));
163 m_xDispatch = nullptr;
164 }
165 m_xIntercepted = nullptr;
166 m_pView = nullptr;
167}
168
170 m_pView(&rVw),
171 m_bOldEnable(false),
172 m_bListenerAdded(false)
173{
174}
175
177{
179 {
180 uno::Reference<view::XSelectionSupplier> xSupplier = m_pView->GetUNOObject();
181 uno::Reference<view::XSelectionChangeListener> xThis = this;
182 xSupplier->removeSelectionChangeListener(xThis);
183 }
184}
185
186void SwXDispatch::dispatch(const util::URL& aURL,
187 const uno::Sequence< beans::PropertyValue >& aArgs)
188{
189 if(!m_pView)
190 throw uno::RuntimeException();
191#if !HAVE_FEATURE_DBCONNECTIVITY || ENABLE_FUZZERS
192 (void) aArgs;
193 if (false)
194 {
195 }
196#else
198 SwDBManager* pDBManager = rSh.GetDBManager();
199 if(aURL.Complete == cURLInsertContent)
200 {
201 svx::ODataAccessDescriptor aDescriptor(aArgs);
202 SwMergeDescriptor aMergeDesc( DBMGR_MERGE, rSh, aDescriptor );
203 pDBManager->Merge(aMergeDesc);
204 }
205 else if(aURL.Complete == cURLInsertColumns)
206 {
207 SwDBManager::InsertText(rSh, aArgs);
208 }
209 else if(aURL.Complete == cURLFormLetter)
210 {
214 SfxCallMode::ASYNCHRON,
215 { &aDBProperties });
216 }
217#endif
218 else if(aURL.Complete == cURLDocumentDataSource)
219 {
220 OSL_FAIL("SwXDispatch::dispatch: this URL is not to be dispatched!");
221 }
222 else if(aURL.Complete == cInternalDBChangeNotification)
223 {
224 frame::FeatureStateEvent aEvent;
225 aEvent.Source = *static_cast<cppu::OWeakObject*>(this);
226
227 const SwDBData& rData = m_pView->GetWrtShell().GetDBData();
228 svx::ODataAccessDescriptor aDescriptor;
229 aDescriptor.setDataSource(rData.sDataSource);
232
233 aEvent.State <<= aDescriptor.createPropertyValueSequence();
234 aEvent.IsEnabled = !rData.sDataSource.isEmpty();
235
236 // calls to statusChanged may call addStatusListener or removeStatusListener
237 // so copy m_aStatusListenerVector on stack
238 auto copyStatusListenerVector = m_aStatusListenerVector;
239 for (auto & status : copyStatusListenerVector)
240 {
241 if(status.aURL.Complete == cURLDocumentDataSource)
242 {
243 aEvent.FeatureURL = status.aURL;
244 status.xListener->statusChanged( aEvent );
245 }
246 }
247 }
248 else
249 throw uno::RuntimeException();
250
251}
252
254 const uno::Reference< frame::XStatusListener >& xControl, const util::URL& aURL )
255{
256 if(!m_pView)
257 throw uno::RuntimeException();
259 bool bEnable = ShellMode::Text == eMode ||
263
264 m_bOldEnable = bEnable;
265 frame::FeatureStateEvent aEvent;
266 aEvent.IsEnabled = bEnable;
267 aEvent.Source = *static_cast<cppu::OWeakObject*>(this);
268 aEvent.FeatureURL = aURL;
269
270 // one of the URLs requires a special state...
271 if (aURL.Complete == cURLDocumentDataSource)
272 {
273 const SwDBData& rData = m_pView->GetWrtShell().GetDBData();
274
275 svx::ODataAccessDescriptor aDescriptor;
276 aDescriptor.setDataSource(rData.sDataSource);
279
280 aEvent.State <<= aDescriptor.createPropertyValueSequence();
281 aEvent.IsEnabled = !rData.sDataSource.isEmpty();
282 }
283
284 xControl->statusChanged( aEvent );
285
286 StatusStruct_Impl aStatus;
287 aStatus.xListener = xControl;
288 aStatus.aURL = aURL;
289 m_aStatusListenerVector.emplace_back(aStatus);
290
292 {
293 uno::Reference<view::XSelectionSupplier> xSupplier = m_pView->GetUNOObject();
294 uno::Reference<view::XSelectionChangeListener> xThis = this;
295 xSupplier->addSelectionChangeListener(xThis);
296 m_bListenerAdded = true;
297 }
298}
299
301 const uno::Reference< frame::XStatusListener >& xControl, const util::URL& )
302{
304 std::remove_if(m_aStatusListenerVector.begin(), m_aStatusListenerVector.end(),
305 [&](const StatusStruct_Impl& status) { return status.xListener.get() == xControl.get(); }),
307 if(m_aStatusListenerVector.empty() && m_pView)
308 {
309 uno::Reference<view::XSelectionSupplier> xSupplier = m_pView->GetUNOObject();
310 uno::Reference<view::XSelectionChangeListener> xThis = this;
311 xSupplier->removeSelectionChangeListener(xThis);
312 m_bListenerAdded = false;
313 }
314}
315
316void SwXDispatch::selectionChanged( const lang::EventObject& )
317{
319 bool bEnable = ShellMode::Text == eMode ||
323 if(bEnable == m_bOldEnable)
324 return;
325
326 m_bOldEnable = bEnable;
327 frame::FeatureStateEvent aEvent;
328 aEvent.IsEnabled = bEnable;
329 aEvent.Source = *static_cast<cppu::OWeakObject*>(this);
330
331 // calls to statusChanged may call addStatusListener or removeStatusListener
332 // so copy m_aStatusListenerVector on stack
333 auto copyStatusListenerVector = m_aStatusListenerVector;
334 for (auto & status : copyStatusListenerVector)
335 {
336 aEvent.FeatureURL = status.aURL;
337 if (status.aURL.Complete != cURLDocumentDataSource)
338 // the document's data source does not depend on the selection, so it's state does not change here
339 status.xListener->statusChanged( aEvent );
340 }
341}
342
343void SwXDispatch::disposing( const lang::EventObject& rSource )
344{
345 uno::Reference<view::XSelectionSupplier> xSupplier(rSource.Source, uno::UNO_QUERY);
346 uno::Reference<view::XSelectionChangeListener> xThis = this;
347 xSupplier->removeSelectionChangeListener(xThis);
348 m_bListenerAdded = false;
349
350 lang::EventObject aObject;
351 aObject.Source = static_cast<cppu::OWeakObject*>(this);
352 // calls to statusChanged may call addStatusListener or removeStatusListener
353 // so copy m_aStatusListenerVector on stack
354 auto copyStatusListenerVector = m_aStatusListenerVector;
355 for (auto & status : copyStatusListenerVector)
356 {
357 status.xListener->disposing(aObject);
358 }
359 m_pView = nullptr;
360}
361
363{
365}
366
368{
369}
370
372{
373}
374
375/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
AnyEventRef aEvent
const SfxPoolItem * ExecuteList(sal_uInt16 nSlot, SfxCallMode nCall, std::initializer_list< SfxPoolItem const * > args, std::initializer_list< SfxPoolItem const * > internalargs=std::initializer_list< SfxPoolItem const * >())
const css::uno::Reference< css::frame::XFrame > & GetFrameInterface() const
SfxDispatcher * GetDispatcher()
SfxFrame & GetFrame() const
SfxViewFrame & GetViewFrame() const
bool Merge(const SwMergeDescriptor &rMergeDesc)
Merging of data records into fields.
Definition: dbmgr.cxx:431
static void InsertText(SwWrtShell &rSh, const css::uno::Sequence< css::beans::PropertyValue > &rProperties)
Definition: dbmgr.cxx:3030
SwDBManager * GetDBManager() const
For evaluation of DB fields (new DB-manager).
Definition: edfld.cxx:329
SwDBData const & GetDBData() const
Database information.
Definition: edfld.cxx:292
Definition: view.hxx:146
SwWrtShell & GetWrtShell() const
Definition: view.hxx:423
css::view::XSelectionSupplier * GetUNOObject()
Definition: view0.cxx:113
ShellMode GetShellMode() const
Definition: view0.cxx:108
Used by the UI to modify the document model.
Definition: wrtsh.hxx:97
virtual css::uno::Sequence< css::uno::Reference< css::frame::XDispatch > > SAL_CALL queryDispatches(const css::uno::Sequence< css::frame::DispatchDescriptor > &aDescripts) override
virtual ~SwXDispatchProviderInterceptor() override
Definition: unodispatch.cxx:63
css::uno::Reference< css::frame::XDispatchProviderInterception > m_xIntercepted
Definition: unodispatch.hxx:53
SwXDispatchProviderInterceptor(SwView &rView)
Definition: unodispatch.cxx:45
virtual void SAL_CALL setMasterDispatchProvider(const css::uno::Reference< css::frame::XDispatchProvider > &xNewSupplier) override
css::uno::Reference< css::frame::XDispatch > m_xDispatch
Definition: unodispatch.hxx:59
virtual void SAL_CALL disposing(const css::lang::EventObject &Source) override
virtual void SAL_CALL setSlaveDispatchProvider(const css::uno::Reference< css::frame::XDispatchProvider > &xNewDispatchProvider) override
css::uno::Reference< css::frame::XDispatchProvider > m_xMasterDispatcher
Definition: unodispatch.hxx:57
css::uno::Reference< css::frame::XDispatchProvider > m_xSlaveDispatcher
Definition: unodispatch.hxx:56
virtual css::uno::Reference< css::frame::XDispatchProvider > SAL_CALL getMasterDispatchProvider() override
virtual css::uno::Reference< css::frame::XDispatch > SAL_CALL queryDispatch(const css::util::URL &aURL, const OUString &aTargetFrameName, sal_Int32 nSearchFlags) override
Definition: unodispatch.cxx:67
virtual css::uno::Reference< css::frame::XDispatchProvider > SAL_CALL getSlaveDispatchProvider() override
virtual css::uno::Sequence< OUString > SAL_CALL getInterceptedURLs() override
Definition: unodispatch.cxx:93
bool m_bListenerAdded
static const char * GetDBChangeURL()
virtual void SAL_CALL selectionChanged(const css::lang::EventObject &aEvent) override
virtual ~SwXDispatch() override
virtual void SAL_CALL dispatch(const css::util::URL &aURL, const css::uno::Sequence< css::beans::PropertyValue > &aArgs) override
SwView * m_pView
Definition: unodispatch.hxx:98
virtual void SAL_CALL addStatusListener(const css::uno::Reference< css::frame::XStatusListener > &xControl, const css::util::URL &aURL) override
std::vector< StatusStruct_Impl > m_aStatusListenerVector
Definition: unodispatch.hxx:99
virtual void SAL_CALL disposing(const css::lang::EventObject &Source) override
SwXDispatch(SwView &rView)
virtual void SAL_CALL removeStatusListener(const css::uno::Reference< css::frame::XStatusListener > &xControl, const css::util::URL &aURL) override
css::uno::Sequence< css::beans::PropertyValue > const & createPropertyValueSequence()
void setDataSource(const OUString &_sDataSourceNameOrLocation)
#define FN_PARAM_DATABASE_PROPERTIES
Definition: cmdid.h:853
#define FN_MAILMERGE_WIZARD
Definition: cmdid.h:248
@ DBMGR_MERGE
Data records in fields.
Definition: dbmgr.hxx:89
URL aURL
ULONG m_refCount
Mode eMode
css::util::URL aURL
Definition: unodispatch.hxx:90
css::uno::Reference< css::frame::XStatusListener > xListener
Definition: unodispatch.hxx:89
sal_Int32 nCommandType
Definition: swdbdata.hxx:32
OUString sDataSource
Definition: swdbdata.hxx:30
OUString sCommand
Definition: swdbdata.hxx:31
const char cURLInsertContent[]
Definition: unodispatch.cxx:40
const char cURLFormLetter[]
Definition: unodispatch.cxx:39
const char cURLInsertColumns[]
Definition: unodispatch.cxx:41
const char cInternalDBChangeNotification[]
Definition: unodispatch.cxx:43
const char cURLDocumentDataSource[]
Definition: unodispatch.cxx:42
ShellMode
Definition: view.hxx:87