Version 7.1.7.1, tag libreoffice-7.1.7.1
[LibreOffice.git] / svx / source / form / fmtools.cxx
blob99e82fca3a3341dfceaa1b9967d28bee15efbad0
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/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>
41 #include <comphelper/processfactory.hxx>
42 #include <comphelper/property.hxx>
43 #include <comphelper/types.hxx>
44 #include <toolkit/helper/vclunohelper.hxx>
45 #include <tools/debug.hxx>
46 #include <tools/diagnose_ex.h>
47 #include <vcl/svapp.hxx>
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;
64 namespace
66 bool lcl_shouldDisplayError( const Any& _rError )
68 SQLException aError;
69 if ( !( _rError >>= aError ) )
70 return true;
72 if ( ! aError.Message.startsWith( "[OOoBase]" ) )
73 // it is an exception *not* thrown by an OOo Base core component
74 return true;
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;
81 // everything else is to be displayed
82 return true;
87 void displayException(const Any& _rExcept, vcl::Window* _pParent)
89 // check whether we need to display it
90 if ( !lcl_shouldDisplayError( _rExcept ) )
91 return;
93 try
95 // the parent window
96 vcl::Window* pParentWindow = _pParent ? _pParent : Application::GetDefDialogParent();
97 Reference< XWindow > xParentWindow = VCLUnoHelper::GetInterface(pParentWindow);
99 Reference< XExecutableDialog > xErrorDialog = ErrorMessageDialog::create(::comphelper::getProcessComponentContext(), "", xParentWindow, _rExcept);
100 xErrorDialog->execute();
102 catch(const Exception&)
104 TOOLS_WARN_EXCEPTION("svx.form", "could not display the error message!");
109 void displayException(const css::sdbc::SQLException& _rExcept, vcl::Window* _pParent)
111 displayException(makeAny(_rExcept), _pParent);
115 void displayException(const css::sdb::SQLContext& _rExcept, vcl::Window* _pParent)
117 displayException(makeAny(_rExcept), _pParent);
121 void displayException(const css::sdb::SQLErrorEvent& _rEvent, vcl::Window* _pParent)
123 displayException(_rEvent.Reason, _pParent);
127 sal_Int32 getElementPos(const Reference< css::container::XIndexAccess>& xCont, const Reference< XInterface >& xElement)
129 sal_Int32 nIndex = -1;
130 if (!xCont.is())
131 return nIndex;
134 Reference< XInterface > xNormalized( xElement, UNO_QUERY );
135 DBG_ASSERT( xNormalized.is(), "getElementPos: invalid element!" );
136 if ( xNormalized.is() )
138 // find child position
139 nIndex = xCont->getCount();
140 while (nIndex--)
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;
150 catch(Exception&)
152 TOOLS_WARN_EXCEPTION( "svx", "getElementPos" );
157 return nIndex;
161 OUString getLabelName(const Reference< css::beans::XPropertySet>& xControlModel)
163 if (!xControlModel.is())
164 return OUString();
166 if (::comphelper::hasProperty(FM_PROP_CONTROLLABEL, xControlModel))
168 Reference< css::beans::XPropertySet> xLabelSet;
169 xControlModel->getPropertyValue(FM_PROP_CONTROLLABEL) >>= xLabelSet;
170 if (xLabelSet.is() && ::comphelper::hasProperty(FM_PROP_LABEL, xLabelSet))
172 Any aLabel( xLabelSet->getPropertyValue(FM_PROP_LABEL) );
173 if ((aLabel.getValueTypeClass() == TypeClass_STRING) && !::comphelper::getString(aLabel).isEmpty())
174 return ::comphelper::getString(aLabel);
178 return ::comphelper::getString(xControlModel->getPropertyValue(FM_PROP_CONTROLSOURCE));
182 // = CursorWrapper
184 CursorWrapper::CursorWrapper(const Reference< css::sdbc::XRowSet>& _rxCursor, bool bUseCloned)
186 ImplConstruct(Reference< css::sdbc::XResultSet>(_rxCursor), bUseCloned);
190 CursorWrapper::CursorWrapper(const Reference< css::sdbc::XResultSet>& _rxCursor, bool bUseCloned)
192 ImplConstruct(_rxCursor, bUseCloned);
196 void CursorWrapper::ImplConstruct(const Reference< css::sdbc::XResultSet>& _rxCursor, bool bUseCloned)
198 if (bUseCloned)
200 Reference< css::sdb::XResultSetAccess> xAccess(_rxCursor, UNO_QUERY);
203 m_xMoveOperations = xAccess.is() ? xAccess->createResultSet() : Reference< css::sdbc::XResultSet>();
205 catch(Exception&)
209 else
210 m_xMoveOperations = _rxCursor;
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);
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;
222 else
223 m_xGeneric = m_xMoveOperations.get();
226 CursorWrapper& CursorWrapper::operator=(const Reference< css::sdbc::XRowSet>& _rxCursor)
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;
237 return *this;
240 FmXDisposeListener::~FmXDisposeListener()
242 setAdapter(nullptr);
245 void FmXDisposeListener::setAdapter(FmXDisposeMultiplexer* pAdapter)
247 ::osl::MutexGuard aGuard(m_aMutex);
248 m_pAdapter = pAdapter;
251 FmXDisposeMultiplexer::FmXDisposeMultiplexer(FmXDisposeListener* _pListener, const Reference< css::lang::XComponent>& _rxObject)
252 :m_xObject(_rxObject)
253 ,m_pListener(_pListener)
255 m_pListener->setAdapter(this);
257 if (m_xObject.is())
258 m_xObject->addEventListener(this);
261 FmXDisposeMultiplexer::~FmXDisposeMultiplexer()
265 // css::lang::XEventListener
267 void FmXDisposeMultiplexer::disposing(const css::lang::EventObject& /*Source*/)
269 Reference< css::lang::XEventListener> xPreventDelete(this);
271 if (m_pListener)
273 m_pListener->disposing(0);
274 m_pListener->setAdapter(nullptr);
275 m_pListener = nullptr;
277 m_xObject = nullptr;
281 void FmXDisposeMultiplexer::dispose()
283 if (m_xObject.is())
285 Reference< css::lang::XEventListener> xPreventDelete(this);
287 m_xObject->removeEventListener(this);
288 m_xObject = nullptr;
290 m_pListener->setAdapter(nullptr);
291 m_pListener = nullptr;
296 sal_Int16 getControlTypeByObject(const Reference< css::lang::XServiceInfo>& _rxObject)
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;
304 OUString sPersistentServiceName = xPersistence->getServiceName();
305 if (sPersistentServiceName == FM_COMPONENT_EDIT) // 5.0-Name
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;
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)
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;
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;
365 OSL_FAIL("::getControlTypeByObject : unknown object type !");
366 return OBJ_FM_CONTROL;
370 bool isRowSetAlive(const Reference< XInterface >& _rxRowSet)
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;
380 return bIsAlive;
383 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */