LibreOffice Module svx (master)  1
fmtools.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 
21 #include <fmprop.hxx>
22 #include <fmservs.hxx>
23 #include <svx/fmtools.hxx>
24 #include <svx/svdobjkind.hxx>
25 
26 #include <com/sun/star/beans/XPropertySet.hpp>
27 #include <com/sun/star/container/XIndexAccess.hpp>
28 #include <com/sun/star/io/XPersistObject.hpp>
29 #include <com/sun/star/lang/XServiceInfo.hpp>
30 #include <com/sun/star/sdb/ErrorCondition.hpp>
31 #include <com/sun/star/sdb/ErrorMessageDialog.hpp>
32 #include <com/sun/star/sdb/SQLContext.hpp>
33 #include <com/sun/star/sdb/SQLErrorEvent.hpp>
34 #include <com/sun/star/sdb/XCompletedConnection.hpp>
35 #include <com/sun/star/sdb/XResultSetAccess.hpp>
36 #include <com/sun/star/sdbc/XRowSet.hpp>
37 #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
38 #include <com/sun/star/util/Language.hpp>
39 
41 #include <comphelper/property.hxx>
42 #include <comphelper/types.hxx>
43 #include <tools/debug.hxx>
44 #include <tools/diagnose_ex.h>
45 
46 using namespace ::com::sun::star::uno;
47 using namespace ::com::sun::star::util;
48 using namespace ::com::sun::star::lang;
49 using namespace ::com::sun::star::awt;
50 using namespace ::com::sun::star::beans;
51 using namespace ::com::sun::star::container;
52 using namespace ::com::sun::star::ui::dialogs;
53 using namespace ::com::sun::star::sdbc;
54 using namespace ::com::sun::star::sdbcx;
55 using namespace ::com::sun::star::sdb;
56 using namespace ::com::sun::star::task;
57 using namespace ::com::sun::star::form;
58 using namespace ::svxform;
59 
60 
61 namespace
62 {
63  bool lcl_shouldDisplayError( const Any& _rError )
64  {
65  SQLException aError;
66  if ( !( _rError >>= aError ) )
67  return true;
68 
69  if ( ! aError.Message.startsWith( "[OOoBase]" ) )
70  // it is an exception *not* thrown by an OOo Base core component
71  return true;
72 
73  // the only exception we do not display ATM is a RowSetVetoException, which
74  // has been raised because an XRowSetApprovalListener vetoed a change
75  if ( aError.ErrorCode + ErrorCondition::ROW_SET_OPERATION_VETOED == 0 )
76  return false;
77 
78  // everything else is to be displayed
79  return true;
80  }
81 }
82 
83 void displayException(const Any& _rExcept, const css::uno::Reference<css::awt::XWindow>& rParent)
84 {
85  // check whether we need to display it
86  if ( !lcl_shouldDisplayError( _rExcept ) )
87  return;
88 
89  try
90  {
91  Reference< XExecutableDialog > xErrorDialog = ErrorMessageDialog::create(::comphelper::getProcessComponentContext(), "", rParent, _rExcept);
92  xErrorDialog->execute();
93  }
94  catch(const Exception&)
95  {
96  TOOLS_WARN_EXCEPTION("svx.form", "could not display the error message!");
97  }
98 }
99 
100 void displayException(const css::sdbc::SQLException& _rExcept, const css::uno::Reference<css::awt::XWindow>& rParent)
101 {
102  displayException(Any(_rExcept), rParent);
103 }
104 
105 void displayException(const css::sdb::SQLContext& _rExcept, const css::uno::Reference<css::awt::XWindow>& rParent)
106 {
107  displayException(Any(_rExcept), rParent);
108 }
109 
110 void displayException(const css::sdb::SQLErrorEvent& _rEvent, const css::uno::Reference<css::awt::XWindow>& rParent)
111 {
112  displayException(_rEvent.Reason, rParent);
113 }
114 
115 sal_Int32 getElementPos(const Reference< css::container::XIndexAccess>& xCont, const Reference< XInterface >& xElement)
116 {
117  sal_Int32 nIndex = -1;
118  if (!xCont.is())
119  return nIndex;
120 
121 
122  Reference< XInterface > xNormalized( xElement, UNO_QUERY );
123  DBG_ASSERT( xNormalized.is(), "getElementPos: invalid element!" );
124  if ( xNormalized.is() )
125  {
126  // find child position
127  nIndex = xCont->getCount();
128  while (nIndex--)
129  {
130  try
131  {
132  Reference< XInterface > xCurrent(xCont->getByIndex( nIndex ),UNO_QUERY);
133  DBG_ASSERT( xCurrent.get() == Reference< XInterface >( xCurrent, UNO_QUERY ).get(),
134  "getElementPos: container element not normalized!" );
135  if ( xNormalized.get() == xCurrent.get() )
136  break;
137  }
138  catch(Exception&)
139  {
140  TOOLS_WARN_EXCEPTION( "svx", "getElementPos" );
141  }
142 
143  }
144  }
145  return nIndex;
146 }
147 
148 
149 OUString getLabelName(const Reference< css::beans::XPropertySet>& xControlModel)
150 {
151  if (!xControlModel.is())
152  return OUString();
153 
154  if (::comphelper::hasProperty(FM_PROP_CONTROLLABEL, xControlModel))
155  {
156  Reference< css::beans::XPropertySet> xLabelSet;
157  xControlModel->getPropertyValue(FM_PROP_CONTROLLABEL) >>= xLabelSet;
158  if (xLabelSet.is() && ::comphelper::hasProperty(FM_PROP_LABEL, xLabelSet))
159  {
160  Any aLabel( xLabelSet->getPropertyValue(FM_PROP_LABEL) );
161  if ((aLabel.getValueTypeClass() == TypeClass_STRING) && !::comphelper::getString(aLabel).isEmpty())
162  return ::comphelper::getString(aLabel);
163  }
164  }
165 
166  return ::comphelper::getString(xControlModel->getPropertyValue(FM_PROP_CONTROLSOURCE));
167 }
168 
169 
170 // = CursorWrapper
171 
172 CursorWrapper::CursorWrapper(const Reference< css::sdbc::XRowSet>& _rxCursor, bool bUseCloned)
173 {
174  ImplConstruct(Reference< css::sdbc::XResultSet>(_rxCursor), bUseCloned);
175 }
176 
177 
178 CursorWrapper::CursorWrapper(const Reference< css::sdbc::XResultSet>& _rxCursor, bool bUseCloned)
179 {
180  ImplConstruct(_rxCursor, bUseCloned);
181 }
182 
183 
184 void CursorWrapper::ImplConstruct(const Reference< css::sdbc::XResultSet>& _rxCursor, bool bUseCloned)
185 {
186  if (bUseCloned)
187  {
188  Reference< css::sdb::XResultSetAccess> xAccess(_rxCursor, UNO_QUERY);
189  try
190  {
191  m_xMoveOperations = xAccess.is() ? xAccess->createResultSet() : Reference< css::sdbc::XResultSet>();
192  }
193  catch(Exception&)
194  {
195  }
196  }
197  else
198  m_xMoveOperations = _rxCursor;
199 
200  m_xBookmarkOperations.set(m_xMoveOperations, css::uno::UNO_QUERY);
201  m_xColumnsSupplier.set(m_xMoveOperations, css::uno::UNO_QUERY);
202  m_xPropertyAccess.set(m_xMoveOperations, css::uno::UNO_QUERY);
203 
204  if ( !m_xMoveOperations.is() || !m_xBookmarkOperations.is() || !m_xColumnsSupplier.is() || !m_xPropertyAccess.is() )
205  { // all or nothing !!
206  m_xMoveOperations = nullptr;
207  m_xBookmarkOperations = nullptr;
208  m_xColumnsSupplier = nullptr;
209  }
210  else
212 }
213 
214 CursorWrapper& CursorWrapper::operator=(const Reference< css::sdbc::XRowSet>& _rxCursor)
215 {
216  m_xMoveOperations.set(_rxCursor);
217  m_xBookmarkOperations.set(_rxCursor, UNO_QUERY);
218  m_xColumnsSupplier.set(_rxCursor, UNO_QUERY);
219  if (!m_xMoveOperations.is() || !m_xBookmarkOperations.is() || !m_xColumnsSupplier.is())
220  { // all or nothing !!
221  m_xMoveOperations = nullptr;
222  m_xBookmarkOperations = nullptr;
223  m_xColumnsSupplier = nullptr;
224  }
225  return *this;
226 }
227 
229 {
230  setAdapter(nullptr);
231 }
232 
234 {
235  std::scoped_lock aGuard(m_aMutex);
236  m_pAdapter = pAdapter;
237 }
238 
239 FmXDisposeMultiplexer::FmXDisposeMultiplexer(FmXDisposeListener* _pListener, const Reference< css::lang::XComponent>& _rxObject)
240  :m_xObject(_rxObject)
241  ,m_pListener(_pListener)
242 {
243  m_pListener->setAdapter(this);
244 
245  if (m_xObject.is())
246  m_xObject->addEventListener(this);
247 }
248 
250 {
251 }
252 
253 // css::lang::XEventListener
254 
255 void FmXDisposeMultiplexer::disposing(const css::lang::EventObject& /*Source*/)
256 {
257  Reference< css::lang::XEventListener> xPreventDelete(this);
258 
259  if (m_pListener)
260  {
262  m_pListener->setAdapter(nullptr);
263  m_pListener = nullptr;
264  }
265  m_xObject = nullptr;
266 }
267 
268 
270 {
271  if (m_xObject.is())
272  {
273  Reference< css::lang::XEventListener> xPreventDelete(this);
274 
275  m_xObject->removeEventListener(this);
276  m_xObject = nullptr;
277 
278  m_pListener->setAdapter(nullptr);
279  m_pListener = nullptr;
280  }
281 }
282 
283 
284 SdrObjKind getControlTypeByObject(const Reference< css::lang::XServiceInfo>& _rxObject)
285 {
286  // ask for the persistent service name
287  Reference< css::io::XPersistObject> xPersistence(_rxObject, UNO_QUERY);
288  DBG_ASSERT(xPersistence.is(), "::getControlTypeByObject : argument should be a css::io::XPersistObject !");
289  if (!xPersistence.is())
291 
292  OUString sPersistentServiceName = xPersistence->getServiceName();
293  if (sPersistentServiceName == FM_COMPONENT_EDIT) // 5.0-Name
294  {
295  // may be a simple edit field or a formatted field, dependent of the supported services
296  if (_rxObject->supportsService(FM_SUN_COMPONENT_FORMATTEDFIELD))
298  return SdrObjKind::FormEdit;
299  }
300  if (sPersistentServiceName == FM_COMPONENT_TEXTFIELD)
301  return SdrObjKind::FormEdit;
302  if (sPersistentServiceName == FM_COMPONENT_COMMANDBUTTON)
303  return SdrObjKind::FormButton;
304  if (sPersistentServiceName == FM_COMPONENT_FIXEDTEXT)
306  if (sPersistentServiceName == FM_COMPONENT_LISTBOX)
308  if (sPersistentServiceName == FM_COMPONENT_CHECKBOX)
310  if (sPersistentServiceName == FM_COMPONENT_RADIOBUTTON)
312  if (sPersistentServiceName == FM_COMPONENT_GROUPBOX)
314  if (sPersistentServiceName == FM_COMPONENT_COMBOBOX)
316  if (sPersistentServiceName == FM_COMPONENT_GRID) // 5.0-Name
317  return SdrObjKind::FormGrid;
318  if (sPersistentServiceName == FM_COMPONENT_GRIDCONTROL)
319  return SdrObjKind::FormGrid;
320  if (sPersistentServiceName == FM_COMPONENT_IMAGEBUTTON)
322  if (sPersistentServiceName == FM_COMPONENT_FILECONTROL)
324  if (sPersistentServiceName == FM_COMPONENT_DATEFIELD)
326  if (sPersistentServiceName == FM_COMPONENT_TIMEFIELD)
328  if (sPersistentServiceName == FM_COMPONENT_NUMERICFIELD)
330  if (sPersistentServiceName == FM_COMPONENT_CURRENCYFIELD)
332  if (sPersistentServiceName == FM_COMPONENT_PATTERNFIELD)
334  if (sPersistentServiceName == FM_COMPONENT_HIDDEN) // 5.0-Name
335  return SdrObjKind::FormHidden;
336  if (sPersistentServiceName == FM_COMPONENT_HIDDENCONTROL)
337  return SdrObjKind::FormHidden;
338  if (sPersistentServiceName == FM_COMPONENT_IMAGECONTROL)
340  if (sPersistentServiceName == FM_COMPONENT_FORMATTEDFIELD)
341  {
342  OSL_FAIL("::getControlTypeByObject : suspicious persistent service name (formatted field) !");
343  // objects with that service name should exist as they aren't compatible with older versions
345  }
346  if ( sPersistentServiceName == FM_SUN_COMPONENT_SCROLLBAR )
348  if ( sPersistentServiceName == FM_SUN_COMPONENT_SPINBUTTON )
350  if ( sPersistentServiceName == FM_SUN_COMPONENT_NAVIGATIONBAR )
352 
353  OSL_FAIL("::getControlTypeByObject : unknown object type !");
355 }
356 
357 
358 bool isRowSetAlive(const Reference< XInterface >& _rxRowSet)
359 {
360  bool bIsAlive = false;
361  Reference< css::sdbcx::XColumnsSupplier> xSupplyCols(_rxRowSet, UNO_QUERY);
362  Reference< css::container::XIndexAccess> xCols;
363  if (xSupplyCols.is())
364  xCols.set(xSupplyCols->getColumns(), UNO_QUERY);
365  if (xCols.is() && (xCols->getCount() > 0))
366  bIsAlive = true;
367 
368  return bIsAlive;
369 }
370 
371 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual void disposing(sal_Int16 _nId)=0
constexpr OUStringLiteral FM_COMPONENT_CHECKBOX
Definition: fmservs.hxx:37
css::uno::Reference< css::sdbc::XResultSet > m_xMoveOperations
Definition: fmtools.hxx:70
constexpr OUStringLiteral FM_COMPONENT_IMAGEBUTTON
Definition: fmservs.hxx:40
std::mutex m_aMutex
Definition: fmtools.hxx:136
constexpr OUStringLiteral FM_COMPONENT_FIXEDTEXT
Definition: fmservs.hxx:35
sal_Int32 nIndex
constexpr OUStringLiteral FM_COMPONENT_COMBOBOX
Definition: fmservs.hxx:32
virtual ~FmXDisposeListener()
Definition: fmtools.cxx:228
constexpr OUStringLiteral FM_COMPONENT_COMMANDBUTTON
Definition: fmservs.hxx:36
constexpr OUStringLiteral FM_COMPONENT_HIDDENCONTROL
Definition: fmservs.hxx:49
css::uno::Reference< css::sdbcx::XRowLocate > m_xBookmarkOperations
Definition: fmtools.hxx:71
css::uno::Reference< css::beans::XPropertySet > m_xPropertyAccess
Definition: fmtools.hxx:73
sal_Int32 getElementPos(const Reference< css::container::XIndexAccess > &xCont, const Reference< XInterface > &xElement)
Definition: fmtools.cxx:115
constexpr OUStringLiteral FM_SUN_COMPONENT_FORMATTEDFIELD
Definition: fmservs.hxx:73
constexpr OUStringLiteral FM_SUN_COMPONENT_SCROLLBAR
Definition: fmservs.hxx:74
void ImplConstruct(const css::uno::Reference< css::sdbc::XResultSet > &_rxCursor, bool bUseCloned)
Definition: fmtools.cxx:184
css::uno::Reference< css::uno::XInterface > m_xGeneric
Definition: fmtools.hxx:69
bool isRowSetAlive(const Reference< XInterface > &_rxRowSet)
Definition: fmtools.cxx:358
SdrObjKind
Definition: svdobjkind.hxx:24
CursorWrapper(const css::uno::Reference< css::sdbc::XRowSet > &_rxCursor, bool bUseCloned=false)
Definition: fmtools.cxx:172
constexpr OUStringLiteral FM_SUN_COMPONENT_SPINBUTTON
Definition: fmservs.hxx:75
constexpr OUStringLiteral FM_PROP_CONTROLLABEL
Definition: fmprop.hxx:112
constexpr OUStringLiteral FM_PROP_LABEL
Definition: fmprop.hxx:42
FmXDisposeMultiplexer(FmXDisposeListener *_pListener, const css::uno::Reference< css::lang::XComponent > &_rxObject)
Definition: fmtools.cxx:239
rtl::Reference< FmXDisposeMultiplexer > m_pAdapter
Definition: fmtools.hxx:135
constexpr OUStringLiteral FM_COMPONENT_IMAGECONTROL
Definition: fmservs.hxx:50
css::uno::Reference< css::lang::XComponent > m_xObject
Definition: fmtools.hxx:150
#define TOOLS_WARN_EXCEPTION(area, stream)
#define DBG_ASSERT(sCon, aError)
constexpr OUStringLiteral FM_COMPONENT_GRIDCONTROL
Definition: fmservs.hxx:39
constexpr OUStringLiteral FM_COMPONENT_TIMEFIELD
Definition: fmservs.hxx:42
constexpr OUStringLiteral FM_COMPONENT_LISTBOX
Definition: fmservs.hxx:31
void setAdapter(FmXDisposeMultiplexer *pAdapter)
Definition: fmtools.cxx:233
constexpr OUStringLiteral FM_COMPONENT_RADIOBUTTON
Definition: fmservs.hxx:33
constexpr OUStringLiteral FM_COMPONENT_EDIT
Definition: fmservs.hxx:29
virtual void SAL_CALL disposing(const css::lang::EventObject &Source) override
Definition: fmtools.cxx:255
constexpr OUStringLiteral FM_COMPONENT_DATEFIELD
Definition: fmservs.hxx:43
constexpr OUStringLiteral FM_COMPONENT_NUMERICFIELD
Definition: fmservs.hxx:44
constexpr OUStringLiteral FM_COMPONENT_GRID
Definition: fmservs.hxx:38
constexpr OUStringLiteral FM_COMPONENT_HIDDEN
Definition: fmservs.hxx:48
FmXDisposeListener * m_pListener
Definition: fmtools.hxx:151
SdrObjKind getControlTypeByObject(const Reference< css::lang::XServiceInfo > &_rxObject)
Definition: fmtools.cxx:284
CursorWrapper & operator=(const css::uno::Reference< css::sdbc::XRowSet > &xCursor)
Definition: fmtools.cxx:214
OUString aLabel
Reference< XComponentContext > getProcessComponentContext()
constexpr OUStringLiteral FM_COMPONENT_FILECONTROL
Definition: fmservs.hxx:41
constexpr OUStringLiteral FM_COMPONENT_FORMATTEDFIELD
Definition: fmservs.hxx:47
constexpr OUStringLiteral FM_COMPONENT_PATTERNFIELD
Definition: fmservs.hxx:46
bool hasProperty(const OUString &_rName, const Reference< XPropertySet > &_rxSet)
OUString getLabelName(const Reference< css::beans::XPropertySet > &xControlModel)
Definition: fmtools.cxx:149
class FmSearchEngine - Impl class for FmSearchDialog
constexpr OUStringLiteral FM_PROP_CONTROLSOURCE
Definition: fmprop.hxx:45
constexpr OUStringLiteral FM_SUN_COMPONENT_NAVIGATIONBAR
Definition: fmservs.hxx:76
constexpr OUStringLiteral FM_COMPONENT_CURRENCYFIELD
Definition: fmservs.hxx:45
OUString getString(const Any &_rAny)
void displayException(const Any &_rExcept, const css::uno::Reference< css::awt::XWindow > &rParent)
Definition: fmtools.cxx:83
css::uno::Reference< css::sdbcx::XColumnsSupplier > m_xColumnsSupplier
Definition: fmtools.hxx:72
constexpr OUStringLiteral FM_COMPONENT_GROUPBOX
Definition: fmservs.hxx:34
virtual ~FmXDisposeMultiplexer() override
Definition: fmtools.cxx:249
constexpr OUStringLiteral FM_COMPONENT_TEXTFIELD
Definition: fmservs.hxx:30