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