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 an 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( aIdentifier.getLength() == 16
167  && 0 == memcmp( getUnoTunnelId().getConstArray(),
168  aIdentifier.getConstArray(), 16 ) )
169  {
170  return sal::static_int_cast< sal_Int64 >( reinterpret_cast< sal_IntPtr >( this ));
171  }
172  return 0;
173 }
174 
176 {
178  if (m_xIntercepted.is())
179  {
180  m_xIntercepted->releaseDispatchProviderInterceptor(static_cast<frame::XDispatchProviderInterceptor*>(this));
181  uno::Reference< lang::XComponent> xInterceptedComponent(m_xIntercepted, uno::UNO_QUERY);
182  if (xInterceptedComponent.is())
183  xInterceptedComponent->removeEventListener(static_cast<lang::XEventListener*>(this));
184  m_xDispatch = nullptr;
185  }
186  m_xIntercepted = nullptr;
187  m_pView = nullptr;
188 }
189 
191  m_pView(&rVw),
192  m_bOldEnable(false),
193  m_bListenerAdded(false)
194 {
195 }
196 
198 {
200  {
201  uno::Reference<view::XSelectionSupplier> xSupplier = m_pView->GetUNOObject();
202  uno::Reference<view::XSelectionChangeListener> xThis = this;
203  xSupplier->removeSelectionChangeListener(xThis);
204  }
205 }
206 
207 void SwXDispatch::dispatch(const util::URL& aURL,
208  const uno::Sequence< beans::PropertyValue >& aArgs)
209 {
210  if(!m_pView)
211  throw uno::RuntimeException();
212 #if !HAVE_FEATURE_DBCONNECTIVITY
213  (void) aArgs;
214  if (false)
215  {
216  }
217 #else
218  SwWrtShell& rSh = m_pView->GetWrtShell();
219  SwDBManager* pDBManager = rSh.GetDBManager();
220  if(aURL.Complete == cURLInsertContent)
221  {
222  svx::ODataAccessDescriptor aDescriptor(aArgs);
223  SwMergeDescriptor aMergeDesc( DBMGR_MERGE, rSh, aDescriptor );
224  pDBManager->Merge(aMergeDesc);
225  }
226  else if(aURL.Complete == cURLInsertColumns)
227  {
228  SwDBManager::InsertText(rSh, aArgs);
229  }
230  else if(aURL.Complete == cURLFormLetter)
231  {
232  SfxUnoAnyItem aDBProperties(FN_PARAM_DATABASE_PROPERTIES, uno::makeAny(aArgs));
235  SfxCallMode::ASYNCHRON,
236  { &aDBProperties });
237  }
238 #endif
239  else if(aURL.Complete == cURLDocumentDataSource)
240  {
241  OSL_FAIL("SwXDispatch::dispatch: this URL is not to be dispatched!");
242  }
243  else if(aURL.Complete == cInternalDBChangeNotification)
244  {
245  frame::FeatureStateEvent aEvent;
246  aEvent.Source = *static_cast<cppu::OWeakObject*>(this);
247 
248  const SwDBData& rData = m_pView->GetWrtShell().GetDBDesc();
249  svx::ODataAccessDescriptor aDescriptor;
250  aDescriptor.setDataSource(rData.sDataSource);
253 
254  aEvent.State <<= aDescriptor.createPropertyValueSequence();
255  aEvent.IsEnabled = !rData.sDataSource.isEmpty();
256 
257  // calls to statusChanged may call addStatusListener or removeStatusListener
258  // so copy m_aStatusListenerVector on stack
259  auto copyStatusListenerVector = m_aStatusListenerVector;
260  for (auto & status : copyStatusListenerVector)
261  {
262  if(status.aURL.Complete == cURLDocumentDataSource)
263  {
264  aEvent.FeatureURL = status.aURL;
265  status.xListener->statusChanged( aEvent );
266  }
267  }
268  }
269  else
270  throw uno::RuntimeException();
271 
272 }
273 
275  const uno::Reference< frame::XStatusListener >& xControl, const util::URL& aURL )
276 {
277  if(!m_pView)
278  throw uno::RuntimeException();
279  ShellMode eMode = m_pView->GetShellMode();
280  bool bEnable = ShellMode::Text == eMode ||
281  ShellMode::ListText == eMode ||
282  ShellMode::TableText == eMode ||
283  ShellMode::TableListText == eMode;
284 
285  m_bOldEnable = bEnable;
286  frame::FeatureStateEvent aEvent;
287  aEvent.IsEnabled = bEnable;
288  aEvent.Source = *static_cast<cppu::OWeakObject*>(this);
289  aEvent.FeatureURL = aURL;
290 
291  // one of the URLs requires a special state ....
292  if (aURL.Complete == cURLDocumentDataSource)
293  {
294  const SwDBData& rData = m_pView->GetWrtShell().GetDBDesc();
295 
296  svx::ODataAccessDescriptor aDescriptor;
297  aDescriptor.setDataSource(rData.sDataSource);
300 
301  aEvent.State <<= aDescriptor.createPropertyValueSequence();
302  aEvent.IsEnabled = !rData.sDataSource.isEmpty();
303  }
304 
305  xControl->statusChanged( aEvent );
306 
307  StatusStruct_Impl aStatus;
308  aStatus.xListener = xControl;
309  aStatus.aURL = aURL;
310  m_aStatusListenerVector.emplace_back(aStatus);
311 
312  if(!m_bListenerAdded)
313  {
314  uno::Reference<view::XSelectionSupplier> xSupplier = m_pView->GetUNOObject();
315  uno::Reference<view::XSelectionChangeListener> xThis = this;
316  xSupplier->addSelectionChangeListener(xThis);
317  m_bListenerAdded = true;
318  }
319 }
320 
322  const uno::Reference< frame::XStatusListener >& xControl, const util::URL& )
323 {
325  std::remove_if(m_aStatusListenerVector.begin(), m_aStatusListenerVector.end(),
326  [&](const StatusStruct_Impl& status) { return status.xListener.get() == xControl.get(); }),
328  if(m_aStatusListenerVector.empty() && m_pView)
329  {
330  uno::Reference<view::XSelectionSupplier> xSupplier = m_pView->GetUNOObject();
331  uno::Reference<view::XSelectionChangeListener> xThis = this;
332  xSupplier->removeSelectionChangeListener(xThis);
333  m_bListenerAdded = false;
334  }
335 }
336 
337 void SwXDispatch::selectionChanged( const lang::EventObject& )
338 {
339  ShellMode eMode = m_pView->GetShellMode();
340  bool bEnable = ShellMode::Text == eMode ||
341  ShellMode::ListText == eMode ||
342  ShellMode::TableText == eMode ||
343  ShellMode::TableListText == eMode;
344  if(bEnable != m_bOldEnable)
345  {
346  m_bOldEnable = bEnable;
347  frame::FeatureStateEvent aEvent;
348  aEvent.IsEnabled = bEnable;
349  aEvent.Source = *static_cast<cppu::OWeakObject*>(this);
350 
351  // calls to statusChanged may call addStatusListener or removeStatusListener
352  // so copy m_aStatusListenerVector on stack
353  auto copyStatusListenerVector = m_aStatusListenerVector;
354  for (auto & status : copyStatusListenerVector)
355  {
356  aEvent.FeatureURL = status.aURL;
357  if (status.aURL.Complete != cURLDocumentDataSource)
358  // the document's data source does not depend on the selection, so it's state does not change here
359  status.xListener->statusChanged( aEvent );
360  }
361  }
362 }
363 
364 void SwXDispatch::disposing( const lang::EventObject& rSource )
365 {
366  uno::Reference<view::XSelectionSupplier> xSupplier(rSource.Source, uno::UNO_QUERY);
367  uno::Reference<view::XSelectionChangeListener> xThis = this;
368  xSupplier->removeSelectionChangeListener(xThis);
369  m_bListenerAdded = false;
370 
371  lang::EventObject aObject;
372  aObject.Source = static_cast<cppu::OWeakObject*>(this);
373  // calls to statusChanged may call addStatusListener or removeStatusListener
374  // so copy m_aStatusListenerVector on stack
375  auto copyStatusListenerVector = m_aStatusListenerVector;
376  for (auto & status : copyStatusListenerVector)
377  {
378  status.xListener->disposing(aObject);
379  }
380  m_pView = nullptr;
381 }
382 
384 {
386 }
387 
389 {
390 }
391 
393 {
394 }
395 
396 /* 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:62
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:56
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:793
css::uno::Reference< css::frame::XStatusListener > xListener
Definition: unodispatch.hxx:96
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:60
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:97
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:3114
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:59
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:219
Definition: view.hxx:146
typedef void(CALLTYPE *GetFuncDataPtr)(sal_uInt16 &nNo