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/view/XSelectionSupplier.hpp>
23 
24 #include <vcl/svapp.hxx>
25 #include <sfx2/viewfrm.hxx>
26 #include <sfx2/dispatch.hxx>
29 #include <unodispatch.hxx>
30 #include <unobaseclass.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 static const char cURLFormLetter[] = ".uno:DataSourceBrowser/FormLetter";
39 static const char cURLInsertContent[] = ".uno:DataSourceBrowser/InsertContent";//data into fields
40 static const char cURLInsertColumns[] = ".uno:DataSourceBrowser/InsertColumns";//data into text
41 static const char cURLDocumentDataSource[] = ".uno:DataSourceBrowser/DocumentDataSource";//current data source of the document
42 static const sal_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 namespace
154 {
155  class theSwXDispatchProviderInterceptorUnoTunnelId : public rtl::Static< UnoTunnelIdInit, theSwXDispatchProviderInterceptorUnoTunnelId > {};
156 }
157 
158 const uno::Sequence< sal_Int8 > & SwXDispatchProviderInterceptor::getUnoTunnelId()
159 {
161 }
162 
164  const uno::Sequence< sal_Int8 >& aIdentifier )
165 {
166  if( isUnoTunnelId<SwXDispatchProviderInterceptor>(aIdentifier) )
167  {
168  return sal::static_int_cast< sal_Int64 >( reinterpret_cast< sal_IntPtr >( this ));
169  }
170  return 0;
171 }
172 
174 {
176  if (m_xIntercepted.is())
177  {
178  m_xIntercepted->releaseDispatchProviderInterceptor(static_cast<frame::XDispatchProviderInterceptor*>(this));
179  uno::Reference< lang::XComponent> xInterceptedComponent(m_xIntercepted, uno::UNO_QUERY);
180  if (xInterceptedComponent.is())
181  xInterceptedComponent->removeEventListener(static_cast<lang::XEventListener*>(this));
182  m_xDispatch = nullptr;
183  }
184  m_xIntercepted = nullptr;
185  m_pView = nullptr;
186 }
187 
189  m_pView(&rVw),
190  m_bOldEnable(false),
191  m_bListenerAdded(false)
192 {
193 }
194 
196 {
198  {
199  uno::Reference<view::XSelectionSupplier> xSupplier = m_pView->GetUNOObject();
200  uno::Reference<view::XSelectionChangeListener> xThis = this;
201  xSupplier->removeSelectionChangeListener(xThis);
202  }
203 }
204 
205 void SwXDispatch::dispatch(const util::URL& aURL,
206  const uno::Sequence< beans::PropertyValue >& aArgs)
207 {
208  if(!m_pView)
209  throw uno::RuntimeException();
210 #if !HAVE_FEATURE_DBCONNECTIVITY
211  (void) aArgs;
212  if (false)
213  {
214  }
215 #else
216  SwWrtShell& rSh = m_pView->GetWrtShell();
217  SwDBManager* pDBManager = rSh.GetDBManager();
218  if(aURL.Complete == cURLInsertContent)
219  {
220  svx::ODataAccessDescriptor aDescriptor(aArgs);
221  SwMergeDescriptor aMergeDesc( DBMGR_MERGE, rSh, aDescriptor );
222  pDBManager->Merge(aMergeDesc);
223  }
224  else if(aURL.Complete == cURLInsertColumns)
225  {
226  SwDBManager::InsertText(rSh, aArgs);
227  }
228  else if(aURL.Complete == cURLFormLetter)
229  {
230  SfxUnoAnyItem aDBProperties(FN_PARAM_DATABASE_PROPERTIES, uno::makeAny(aArgs));
233  SfxCallMode::ASYNCHRON,
234  { &aDBProperties });
235  }
236 #endif
237  else if(aURL.Complete == cURLDocumentDataSource)
238  {
239  OSL_FAIL("SwXDispatch::dispatch: this URL is not to be dispatched!");
240  }
241  else if(aURL.Complete == cInternalDBChangeNotification)
242  {
243  frame::FeatureStateEvent aEvent;
244  aEvent.Source = *static_cast<cppu::OWeakObject*>(this);
245 
246  const SwDBData& rData = m_pView->GetWrtShell().GetDBDesc();
247  svx::ODataAccessDescriptor aDescriptor;
248  aDescriptor.setDataSource(rData.sDataSource);
251 
252  aEvent.State <<= aDescriptor.createPropertyValueSequence();
253  aEvent.IsEnabled = !rData.sDataSource.isEmpty();
254 
255  // calls to statusChanged may call addStatusListener or removeStatusListener
256  // so copy m_aStatusListenerVector on stack
257  auto copyStatusListenerVector = m_aStatusListenerVector;
258  for (auto & status : copyStatusListenerVector)
259  {
260  if(status.aURL.Complete == cURLDocumentDataSource)
261  {
262  aEvent.FeatureURL = status.aURL;
263  status.xListener->statusChanged( aEvent );
264  }
265  }
266  }
267  else
268  throw uno::RuntimeException();
269 
270 }
271 
273  const uno::Reference< frame::XStatusListener >& xControl, const util::URL& aURL )
274 {
275  if(!m_pView)
276  throw uno::RuntimeException();
277  ShellMode eMode = m_pView->GetShellMode();
278  bool bEnable = ShellMode::Text == eMode ||
279  ShellMode::ListText == eMode ||
280  ShellMode::TableText == eMode ||
281  ShellMode::TableListText == eMode;
282 
283  m_bOldEnable = bEnable;
284  frame::FeatureStateEvent aEvent;
285  aEvent.IsEnabled = bEnable;
286  aEvent.Source = *static_cast<cppu::OWeakObject*>(this);
287  aEvent.FeatureURL = aURL;
288 
289  // one of the URLs requires a special state...
290  if (aURL.Complete == cURLDocumentDataSource)
291  {
292  const SwDBData& rData = m_pView->GetWrtShell().GetDBDesc();
293 
294  svx::ODataAccessDescriptor aDescriptor;
295  aDescriptor.setDataSource(rData.sDataSource);
298 
299  aEvent.State <<= aDescriptor.createPropertyValueSequence();
300  aEvent.IsEnabled = !rData.sDataSource.isEmpty();
301  }
302 
303  xControl->statusChanged( aEvent );
304 
305  StatusStruct_Impl aStatus;
306  aStatus.xListener = xControl;
307  aStatus.aURL = aURL;
308  m_aStatusListenerVector.emplace_back(aStatus);
309 
310  if(!m_bListenerAdded)
311  {
312  uno::Reference<view::XSelectionSupplier> xSupplier = m_pView->GetUNOObject();
313  uno::Reference<view::XSelectionChangeListener> xThis = this;
314  xSupplier->addSelectionChangeListener(xThis);
315  m_bListenerAdded = true;
316  }
317 }
318 
320  const uno::Reference< frame::XStatusListener >& xControl, const util::URL& )
321 {
323  std::remove_if(m_aStatusListenerVector.begin(), m_aStatusListenerVector.end(),
324  [&](const StatusStruct_Impl& status) { return status.xListener.get() == xControl.get(); }),
326  if(m_aStatusListenerVector.empty() && m_pView)
327  {
328  uno::Reference<view::XSelectionSupplier> xSupplier = m_pView->GetUNOObject();
329  uno::Reference<view::XSelectionChangeListener> xThis = this;
330  xSupplier->removeSelectionChangeListener(xThis);
331  m_bListenerAdded = false;
332  }
333 }
334 
335 void SwXDispatch::selectionChanged( const lang::EventObject& )
336 {
337  ShellMode eMode = m_pView->GetShellMode();
338  bool bEnable = ShellMode::Text == eMode ||
339  ShellMode::ListText == eMode ||
340  ShellMode::TableText == eMode ||
341  ShellMode::TableListText == eMode;
342  if(bEnable != m_bOldEnable)
343  {
344  m_bOldEnable = bEnable;
345  frame::FeatureStateEvent aEvent;
346  aEvent.IsEnabled = bEnable;
347  aEvent.Source = *static_cast<cppu::OWeakObject*>(this);
348 
349  // calls to statusChanged may call addStatusListener or removeStatusListener
350  // so copy m_aStatusListenerVector on stack
351  auto copyStatusListenerVector = m_aStatusListenerVector;
352  for (auto & status : copyStatusListenerVector)
353  {
354  aEvent.FeatureURL = status.aURL;
355  if (status.aURL.Complete != cURLDocumentDataSource)
356  // the document's data source does not depend on the selection, so it's state does not change here
357  status.xListener->statusChanged( aEvent );
358  }
359  }
360 }
361 
362 void SwXDispatch::disposing( const lang::EventObject& rSource )
363 {
364  uno::Reference<view::XSelectionSupplier> xSupplier(rSource.Source, uno::UNO_QUERY);
365  uno::Reference<view::XSelectionChangeListener> xThis = this;
366  xSupplier->removeSelectionChangeListener(xThis);
367  m_bListenerAdded = false;
368 
369  lang::EventObject aObject;
370  aObject.Source = static_cast<cppu::OWeakObject*>(this);
371  // calls to statusChanged may call addStatusListener or removeStatusListener
372  // so copy m_aStatusListenerVector on stack
373  auto copyStatusListenerVector = m_aStatusListenerVector;
374  for (auto & status : copyStatusListenerVector)
375  {
376  status.xListener->disposing(aObject);
377  }
378  m_pView = nullptr;
379 }
380 
382 {
384 }
385 
387 {
388 }
389 
391 {
392 }
393 
394 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Reference< css::linguistic2::XProofreadingIterator > get(css::uno::Reference< css::uno::XComponentContext > const &context)
static 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 * >())
sal_Int32 nCommandType
Definition: swdbdata.hxx:32
static 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
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
virtual void SAL_CALL setSlaveDispatchProvider(const css::uno::Reference< css::frame::XDispatchProvider > &xNewDispatchProvider) override
ShellMode GetShellMode()
Definition: view0.cxx:114
Data records in fields.
Definition: dbmgr.hxx:87
css::uno::Sequence< css::beans::PropertyValue > const & createPropertyValueSequence()
static const sal_Char * GetDBChangeURL()
static const css::uno::Sequence< sal_Int8 > & getUnoTunnelId()
SwWrtShell & GetWrtShell() const
Definition: view.hxx:400
#define FN_PARAM_DATABASE_PROPERTIES
Definition: cmdid.h:795
css::uno::Reference< css::frame::XStatusListener > xListener
Definition: unodispatch.hxx:95
Used by the UI to modify the document model.
Definition: wrtsh.hxx:88
virtual css::uno::Sequence< css::uno::Reference< css::frame::XDispatch > > SAL_CALL queryDispatches(const css::uno::Sequence< css::frame::DispatchDescriptor > &aDescripts) override
char sal_Char
SfxFrame & GetFrame() const
ShellMode
Definition: view.hxx:87
std::vector< StatusStruct_Impl > m_aStatusListenerVector
static 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:433
SwDBManager * GetDBManager() const
For evaluation of DB fields (new DB-manager).
Definition: edfld.cxx:340
virtual void SAL_CALL disposing(const css::lang::EventObject &Source) override
css::util::URL aURL
Definition: unodispatch.hxx:96
virtual css::uno::Sequence< OUString > SAL_CALL getInterceptedURLs() override
Definition: unodispatch.cxx:92
bool m_bListenerAdded
virtual ~SwXDispatch() override
static void InsertText(SwWrtShell &rSh, const css::uno::Sequence< css::beans::PropertyValue > &rProperties)
Definition: dbmgr.cxx:3111
const SwDBData & GetDBDesc() const
Definition: edfld.cxx:303
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
static const sal_Char cInternalDBChangeNotification[]
Definition: unodispatch.cxx:42
virtual sal_Int64 SAL_CALL getSomething(const css::uno::Sequence< sal_Int8 > &aIdentifier) override
static const char cURLFormLetter[]
Definition: unodispatch.cxx:38
css::view::XSelectionSupplier * GetUNOObject()
Definition: view0.cxx:119
SwView * m_pView
virtual void SAL_CALL removeStatusListener(const css::uno::Reference< css::frame::XStatusListener > &xControl, const css::util::URL &aURL) override
OUString sCommand
Definition: swdbdata.hxx:31
virtual css::uno::Reference< css::frame::XDispatchProvider > SAL_CALL getMasterDispatchProvider() override
#define FN_MAILMERGE_WIZARD
Definition: cmdid.h:220
Definition: view.hxx:146
typedef void(CALLTYPE *GetFuncDataPtr)(sal_uInt16 &nNo