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