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