Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / svx / source / form / fmtools.cxx
blobcefb5ce5153f308f4f0c6f62319e1309a6aeecca
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <fmprop.hxx>
22 #include <fmservs.hxx>
23 #include <svx/fmtools.hxx>
24 #include <svx/svdobjkind.hxx>
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>
40 #include <comphelper/processfactory.hxx>
41 #include <comphelper/property.hxx>
42 #include <comphelper/types.hxx>
43 #include <tools/debug.hxx>
44 #include <comphelper/diagnose_ex.hxx>
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;
61 namespace
63 bool lcl_shouldDisplayError( const Any& _rError )
65 SQLException aError;
66 if ( !( _rError >>= aError ) )
67 return true;
69 if ( ! aError.Message.startsWith( "[OOoBase]" ) )
70 // it is an exception *not* thrown by an OOo Base core component
71 return true;
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;
78 // everything else is to be displayed
79 return true;
83 void displayException(const Any& _rExcept, const css::uno::Reference<css::awt::XWindow>& rParent)
85 // check whether we need to display it
86 if ( !lcl_shouldDisplayError( _rExcept ) )
87 return;
89 try
91 Reference< XExecutableDialog > xErrorDialog = ErrorMessageDialog::create(::comphelper::getProcessComponentContext(), "", rParent, _rExcept);
92 xErrorDialog->execute();
94 catch(const Exception&)
96 TOOLS_WARN_EXCEPTION("svx.form", "could not display the error message!");
100 void displayException(const css::sdbc::SQLException& _rExcept, const css::uno::Reference<css::awt::XWindow>& rParent)
102 displayException(Any(_rExcept), rParent);
105 void displayException(const css::sdb::SQLContext& _rExcept, const css::uno::Reference<css::awt::XWindow>& rParent)
107 displayException(Any(_rExcept), rParent);
110 void displayException(const css::sdb::SQLErrorEvent& _rEvent, const css::uno::Reference<css::awt::XWindow>& rParent)
112 displayException(_rEvent.Reason, rParent);
115 sal_Int32 getElementPos(const Reference< css::container::XIndexAccess>& xCont, const Reference< XInterface >& xElement)
117 sal_Int32 nIndex = -1;
118 if (!xCont.is())
119 return nIndex;
122 Reference< XInterface > xNormalized( xElement, UNO_QUERY );
123 DBG_ASSERT( xNormalized.is(), "getElementPos: invalid element!" );
124 if ( xNormalized.is() )
126 // find child position
127 nIndex = xCont->getCount();
128 while (nIndex--)
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;
138 catch(Exception&)
140 TOOLS_WARN_EXCEPTION( "svx", "getElementPos" );
145 return nIndex;
149 OUString getLabelName(const Reference< css::beans::XPropertySet>& xControlModel)
151 if (!xControlModel.is())
152 return OUString();
154 if (::comphelper::hasProperty(FM_PROP_CONTROLLABEL, xControlModel))
156 Reference< css::beans::XPropertySet> xLabelSet;
157 xControlModel->getPropertyValue(FM_PROP_CONTROLLABEL) >>= xLabelSet;
158 if (xLabelSet.is() && ::comphelper::hasProperty(FM_PROP_LABEL, xLabelSet))
160 Any aLabel( xLabelSet->getPropertyValue(FM_PROP_LABEL) );
161 if ((aLabel.getValueTypeClass() == TypeClass_STRING) && !::comphelper::getString(aLabel).isEmpty())
162 return ::comphelper::getString(aLabel);
166 return ::comphelper::getString(xControlModel->getPropertyValue(FM_PROP_CONTROLSOURCE));
170 // = CursorWrapper
172 CursorWrapper::CursorWrapper(const Reference< css::sdbc::XRowSet>& _rxCursor, bool bUseCloned)
174 ImplConstruct(Reference< css::sdbc::XResultSet>(_rxCursor), bUseCloned);
178 CursorWrapper::CursorWrapper(const Reference< css::sdbc::XResultSet>& _rxCursor, bool bUseCloned)
180 ImplConstruct(_rxCursor, bUseCloned);
184 void CursorWrapper::ImplConstruct(const Reference< css::sdbc::XResultSet>& _rxCursor, bool bUseCloned)
186 if (bUseCloned)
188 Reference< css::sdb::XResultSetAccess> xAccess(_rxCursor, UNO_QUERY);
191 m_xMoveOperations = xAccess.is() ? xAccess->createResultSet() : Reference< css::sdbc::XResultSet>();
193 catch(Exception&)
197 else
198 m_xMoveOperations = _rxCursor;
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);
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;
210 else
211 m_xGeneric = m_xMoveOperations.get();
214 CursorWrapper& CursorWrapper::operator=(const Reference< css::sdbc::XRowSet>& _rxCursor)
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;
225 return *this;
228 FmXDisposeListener::~FmXDisposeListener()
230 setAdapter(nullptr);
233 void FmXDisposeListener::setAdapter(FmXDisposeMultiplexer* pAdapter)
235 std::scoped_lock aGuard(m_aMutex);
236 m_pAdapter = pAdapter;
239 FmXDisposeMultiplexer::FmXDisposeMultiplexer(FmXDisposeListener* _pListener, const Reference< css::lang::XComponent>& _rxObject)
240 :m_xObject(_rxObject)
241 ,m_pListener(_pListener)
243 m_pListener->setAdapter(this);
245 if (m_xObject.is())
246 m_xObject->addEventListener(this);
249 FmXDisposeMultiplexer::~FmXDisposeMultiplexer()
253 // css::lang::XEventListener
255 void FmXDisposeMultiplexer::disposing(const css::lang::EventObject& /*Source*/)
257 Reference< css::lang::XEventListener> xPreventDelete(this);
259 if (m_pListener)
261 m_pListener->disposing(0);
262 m_pListener->setAdapter(nullptr);
263 m_pListener = nullptr;
265 m_xObject = nullptr;
269 void FmXDisposeMultiplexer::dispose()
271 if (m_xObject.is())
273 Reference< css::lang::XEventListener> xPreventDelete(this);
275 m_xObject->removeEventListener(this);
276 m_xObject = nullptr;
278 m_pListener->setAdapter(nullptr);
279 m_pListener = nullptr;
284 SdrObjKind getControlTypeByObject(const Reference< css::lang::XServiceInfo>& _rxObject)
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())
290 return SdrObjKind::FormControl;
292 OUString sPersistentServiceName = xPersistence->getServiceName();
293 if (sPersistentServiceName == FM_COMPONENT_EDIT) // 5.0-Name
295 // may be a simple edit field or a formatted field, dependent of the supported services
296 if (_rxObject->supportsService(FM_SUN_COMPONENT_FORMATTEDFIELD))
297 return SdrObjKind::FormFormattedField;
298 return SdrObjKind::FormEdit;
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)
305 return SdrObjKind::FormFixedText;
306 if (sPersistentServiceName == FM_COMPONENT_LISTBOX)
307 return SdrObjKind::FormListbox;
308 if (sPersistentServiceName == FM_COMPONENT_CHECKBOX)
309 return SdrObjKind::FormCheckbox;
310 if (sPersistentServiceName == FM_COMPONENT_RADIOBUTTON)
311 return SdrObjKind::FormRadioButton;
312 if (sPersistentServiceName == FM_COMPONENT_GROUPBOX)
313 return SdrObjKind::FormGroupBox;
314 if (sPersistentServiceName == FM_COMPONENT_COMBOBOX)
315 return SdrObjKind::FormCombobox;
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)
321 return SdrObjKind::FormImageButton;
322 if (sPersistentServiceName == FM_COMPONENT_FILECONTROL)
323 return SdrObjKind::FormFileControl;
324 if (sPersistentServiceName == FM_COMPONENT_DATEFIELD)
325 return SdrObjKind::FormDateField;
326 if (sPersistentServiceName == FM_COMPONENT_TIMEFIELD)
327 return SdrObjKind::FormTimeField;
328 if (sPersistentServiceName == FM_COMPONENT_NUMERICFIELD)
329 return SdrObjKind::FormNumericField;
330 if (sPersistentServiceName == FM_COMPONENT_CURRENCYFIELD)
331 return SdrObjKind::FormCurrencyField;
332 if (sPersistentServiceName == FM_COMPONENT_PATTERNFIELD)
333 return SdrObjKind::FormPatternField;
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)
339 return SdrObjKind::FormImageControl;
340 if (sPersistentServiceName == FM_COMPONENT_FORMATTEDFIELD)
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
344 return SdrObjKind::FormFormattedField;
346 if ( sPersistentServiceName == FM_SUN_COMPONENT_SCROLLBAR )
347 return SdrObjKind::FormScrollbar;
348 if ( sPersistentServiceName == FM_SUN_COMPONENT_SPINBUTTON )
349 return SdrObjKind::FormSpinButton;
350 if ( sPersistentServiceName == FM_SUN_COMPONENT_NAVIGATIONBAR )
351 return SdrObjKind::FormNavigationBar;
353 OSL_FAIL("::getControlTypeByObject : unknown object type !");
354 return SdrObjKind::FormControl;
358 bool isRowSetAlive(const Reference< XInterface >& _rxRowSet)
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;
368 return bIsAlive;
371 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */