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