Bump version to 6.4-15
[LibreOffice.git] / svx / source / form / fmtools.cxx
blob6297a3d0e69f2225331897dbc70b916126998628
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/fmglob.hxx>
26 #include <com/sun/star/awt/LineEndFormat.hpp>
27 #include <com/sun/star/beans/PropertyAttribute.hpp>
28 #include <com/sun/star/beans/XIntrospection.hpp>
29 #include <com/sun/star/container/XChild.hpp>
30 #include <com/sun/star/container/XIndexAccess.hpp>
31 #include <com/sun/star/form/XForm.hpp>
32 #include <com/sun/star/form/XFormComponent.hpp>
33 #include <com/sun/star/form/XGridColumnFactory.hpp>
34 #include <com/sun/star/io/XActiveDataSink.hpp>
35 #include <com/sun/star/io/XActiveDataSource.hpp>
36 #include <com/sun/star/io/XObjectInputStream.hpp>
37 #include <com/sun/star/io/XObjectOutputStream.hpp>
38 #include <com/sun/star/io/XPersistObject.hpp>
39 #include <com/sun/star/lang/Locale.hpp>
40 #include <com/sun/star/lang/XServiceInfo.hpp>
41 #include <com/sun/star/sdb/CommandType.hpp>
42 #include <com/sun/star/sdb/ErrorCondition.hpp>
43 #include <com/sun/star/sdb/ErrorMessageDialog.hpp>
44 #include <com/sun/star/sdb/SQLContext.hpp>
45 #include <com/sun/star/sdb/SQLErrorEvent.hpp>
46 #include <com/sun/star/sdb/XCompletedConnection.hpp>
47 #include <com/sun/star/sdb/XQueriesSupplier.hpp>
48 #include <com/sun/star/sdb/XResultSetAccess.hpp>
49 #include <com/sun/star/sdbc/DataType.hpp>
50 #include <com/sun/star/sdbc/XDataSource.hpp>
51 #include <com/sun/star/sdbcx/Privilege.hpp>
52 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
53 #include <com/sun/star/task/XInteractionHandler.hpp>
54 #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
55 #include <com/sun/star/uno/XNamingService.hpp>
56 #include <com/sun/star/util/Language.hpp>
57 #include <com/sun/star/util/NumberFormat.hpp>
58 #include <com/sun/star/util/XCloneable.hpp>
59 #include <com/sun/star/util/XNumberFormatTypes.hpp>
60 #include <com/sun/star/util/XNumberFormats.hpp>
61 #include <com/sun/star/util/XNumberFormatsSupplier.hpp>
62 #include <com/sun/star/util/XNumberFormatter.hpp>
64 #include <basic/sbxvar.hxx>
65 #include <svl/eitem.hxx>
66 #include <svl/stritem.hxx>
67 #include <comphelper/processfactory.hxx>
68 #include <comphelper/property.hxx>
69 #include <comphelper/types.hxx>
70 #include <connectivity/dbexception.hxx>
71 #include <connectivity/dbtools.hxx>
72 #include <cppuhelper/typeprovider.hxx>
73 #include <osl/diagnose.h>
74 #include <rtl/math.hxx>
75 #include <sfx2/bindings.hxx>
76 #include <toolkit/helper/vclunohelper.hxx>
77 #include <tools/debug.hxx>
78 #include <vcl/stdtext.hxx>
79 #include <vcl/svapp.hxx>
81 #include <algorithm>
83 using namespace ::com::sun::star::uno;
84 using namespace ::com::sun::star::util;
85 using namespace ::com::sun::star::lang;
86 using namespace ::com::sun::star::awt;
87 using namespace ::com::sun::star::beans;
88 using namespace ::com::sun::star::container;
89 using namespace ::com::sun::star::ui::dialogs;
90 using namespace ::com::sun::star::sdbc;
91 using namespace ::com::sun::star::sdbcx;
92 using namespace ::com::sun::star::sdb;
93 using namespace ::com::sun::star::task;
94 using namespace ::com::sun::star::form;
95 using namespace ::svxform;
98 namespace
100 bool lcl_shouldDisplayError( const Any& _rError )
102 SQLException aError;
103 if ( !( _rError >>= aError ) )
104 return true;
106 if ( ! aError.Message.startsWith( "[OOoBase]" ) )
107 // it is an exception *not* thrown by an OOo Base core component
108 return true;
110 // the only exception we do not display ATM is a RowSetVetoException, which
111 // has been raised because an XRowSetApprovalListener vetoed a change
112 if ( aError.ErrorCode + ErrorCondition::ROW_SET_OPERATION_VETOED == 0 )
113 return false;
115 // everything else is to be displayed
116 return true;
121 void displayException(const Any& _rExcept, vcl::Window* _pParent)
123 // check whether we need to display it
124 if ( !lcl_shouldDisplayError( _rExcept ) )
125 return;
129 // the parent window
130 vcl::Window* pParentWindow = _pParent ? _pParent : Application::GetDefDialogParent();
131 Reference< XWindow > xParentWindow = VCLUnoHelper::GetInterface(pParentWindow);
133 Reference< XExecutableDialog > xErrorDialog = ErrorMessageDialog::create(::comphelper::getProcessComponentContext(), "", xParentWindow, _rExcept);
134 xErrorDialog->execute();
136 catch(const Exception&)
138 OSL_FAIL("displayException: could not display the error message!");
143 void displayException(const css::sdbc::SQLException& _rExcept, vcl::Window* _pParent)
145 displayException(makeAny(_rExcept), _pParent);
149 void displayException(const css::sdb::SQLContext& _rExcept, vcl::Window* _pParent)
151 displayException(makeAny(_rExcept), _pParent);
155 void displayException(const css::sdb::SQLErrorEvent& _rEvent, vcl::Window* _pParent)
157 displayException(_rEvent.Reason, _pParent);
161 sal_Int32 getElementPos(const Reference< css::container::XIndexAccess>& xCont, const Reference< XInterface >& xElement)
163 sal_Int32 nIndex = -1;
164 if (!xCont.is())
165 return nIndex;
168 Reference< XInterface > xNormalized( xElement, UNO_QUERY );
169 DBG_ASSERT( xNormalized.is(), "getElementPos: invalid element!" );
170 if ( xNormalized.is() )
172 // find child position
173 nIndex = xCont->getCount();
174 while (nIndex--)
178 Reference< XInterface > xCurrent(xCont->getByIndex( nIndex ),UNO_QUERY);
179 DBG_ASSERT( xCurrent.get() == Reference< XInterface >( xCurrent, UNO_QUERY ).get(),
180 "getElementPos: container element not normalized!" );
181 if ( xNormalized.get() == xCurrent.get() )
182 break;
184 catch(Exception&)
186 OSL_FAIL( "getElementPos: caught an exception!" );
191 return nIndex;
195 OUString getLabelName(const Reference< css::beans::XPropertySet>& xControlModel)
197 if (!xControlModel.is())
198 return OUString();
200 if (::comphelper::hasProperty(FM_PROP_CONTROLLABEL, xControlModel))
202 Reference< css::beans::XPropertySet> xLabelSet;
203 xControlModel->getPropertyValue(FM_PROP_CONTROLLABEL) >>= xLabelSet;
204 if (xLabelSet.is() && ::comphelper::hasProperty(FM_PROP_LABEL, xLabelSet))
206 Any aLabel( xLabelSet->getPropertyValue(FM_PROP_LABEL) );
207 if ((aLabel.getValueTypeClass() == TypeClass_STRING) && !::comphelper::getString(aLabel).isEmpty())
208 return ::comphelper::getString(aLabel);
212 return ::comphelper::getString(xControlModel->getPropertyValue(FM_PROP_CONTROLSOURCE));
216 // = CursorWrapper
218 CursorWrapper::CursorWrapper(const Reference< css::sdbc::XRowSet>& _rxCursor, bool bUseCloned)
220 ImplConstruct(Reference< css::sdbc::XResultSet>(_rxCursor, UNO_QUERY), bUseCloned);
224 CursorWrapper::CursorWrapper(const Reference< css::sdbc::XResultSet>& _rxCursor, bool bUseCloned)
226 ImplConstruct(_rxCursor, bUseCloned);
230 void CursorWrapper::ImplConstruct(const Reference< css::sdbc::XResultSet>& _rxCursor, bool bUseCloned)
232 if (bUseCloned)
234 Reference< css::sdb::XResultSetAccess> xAccess(_rxCursor, UNO_QUERY);
237 m_xMoveOperations = xAccess.is() ? xAccess->createResultSet() : Reference< css::sdbc::XResultSet>();
239 catch(Exception&)
243 else
244 m_xMoveOperations = _rxCursor;
246 m_xBookmarkOperations.set(m_xMoveOperations, css::uno::UNO_QUERY);
247 m_xColumnsSupplier.set(m_xMoveOperations, css::uno::UNO_QUERY);
248 m_xPropertyAccess.set(m_xMoveOperations, css::uno::UNO_QUERY);
250 if ( !m_xMoveOperations.is() || !m_xBookmarkOperations.is() || !m_xColumnsSupplier.is() || !m_xPropertyAccess.is() )
251 { // all or nothing !!
252 m_xMoveOperations = nullptr;
253 m_xBookmarkOperations = nullptr;
254 m_xColumnsSupplier = nullptr;
256 else
257 m_xGeneric = m_xMoveOperations.get();
260 CursorWrapper& CursorWrapper::operator=(const Reference< css::sdbc::XRowSet>& _rxCursor)
262 m_xMoveOperations.set(_rxCursor, UNO_QUERY);
263 m_xBookmarkOperations.set(_rxCursor, UNO_QUERY);
264 m_xColumnsSupplier.set(_rxCursor, UNO_QUERY);
265 if (!m_xMoveOperations.is() || !m_xBookmarkOperations.is() || !m_xColumnsSupplier.is())
266 { // all or nothing !!
267 m_xMoveOperations = nullptr;
268 m_xBookmarkOperations = nullptr;
269 m_xColumnsSupplier = nullptr;
271 return *this;
274 FmXDisposeListener::~FmXDisposeListener()
276 setAdapter(nullptr);
279 void FmXDisposeListener::setAdapter(FmXDisposeMultiplexer* pAdapter)
281 ::osl::MutexGuard aGuard(m_aMutex);
282 m_pAdapter = pAdapter;
285 FmXDisposeMultiplexer::FmXDisposeMultiplexer(FmXDisposeListener* _pListener, const Reference< css::lang::XComponent>& _rxObject)
286 :m_xObject(_rxObject)
287 ,m_pListener(_pListener)
289 m_pListener->setAdapter(this);
291 if (m_xObject.is())
292 m_xObject->addEventListener(this);
295 FmXDisposeMultiplexer::~FmXDisposeMultiplexer()
299 // css::lang::XEventListener
301 void FmXDisposeMultiplexer::disposing(const css::lang::EventObject& /*Source*/)
303 Reference< css::lang::XEventListener> xPreventDelete(this);
305 if (m_pListener)
307 m_pListener->disposing(0);
308 m_pListener->setAdapter(nullptr);
309 m_pListener = nullptr;
311 m_xObject = nullptr;
315 void FmXDisposeMultiplexer::dispose()
317 if (m_xObject.is())
319 Reference< css::lang::XEventListener> xPreventDelete(this);
321 m_xObject->removeEventListener(this);
322 m_xObject = nullptr;
324 m_pListener->setAdapter(nullptr);
325 m_pListener = nullptr;
330 sal_Int16 getControlTypeByObject(const Reference< css::lang::XServiceInfo>& _rxObject)
332 // ask for the persistent service name
333 Reference< css::io::XPersistObject> xPersistence(_rxObject, UNO_QUERY);
334 DBG_ASSERT(xPersistence.is(), "::getControlTypeByObject : argument should be a css::io::XPersistObject !");
335 if (!xPersistence.is())
336 return OBJ_FM_CONTROL;
338 OUString sPersistentServiceName = xPersistence->getServiceName();
339 if (sPersistentServiceName == FM_COMPONENT_EDIT) // 5.0-Name
341 // may be a simple edit field or a formatted field, dependent of the supported services
342 if (_rxObject->supportsService(FM_SUN_COMPONENT_FORMATTEDFIELD))
343 return OBJ_FM_FORMATTEDFIELD;
344 return OBJ_FM_EDIT;
346 if (sPersistentServiceName == FM_COMPONENT_TEXTFIELD)
347 return OBJ_FM_EDIT;
348 if (sPersistentServiceName == FM_COMPONENT_COMMANDBUTTON)
349 return OBJ_FM_BUTTON;
350 if (sPersistentServiceName == FM_COMPONENT_FIXEDTEXT)
351 return OBJ_FM_FIXEDTEXT;
352 if (sPersistentServiceName == FM_COMPONENT_LISTBOX)
353 return OBJ_FM_LISTBOX;
354 if (sPersistentServiceName == FM_COMPONENT_CHECKBOX)
355 return OBJ_FM_CHECKBOX;
356 if (sPersistentServiceName == FM_COMPONENT_RADIOBUTTON)
357 return OBJ_FM_RADIOBUTTON;
358 if (sPersistentServiceName == FM_COMPONENT_GROUPBOX)
359 return OBJ_FM_GROUPBOX;
360 if (sPersistentServiceName == FM_COMPONENT_COMBOBOX)
361 return OBJ_FM_COMBOBOX;
362 if (sPersistentServiceName == FM_COMPONENT_GRID) // 5.0-Name
363 return OBJ_FM_GRID;
364 if (sPersistentServiceName == FM_COMPONENT_GRIDCONTROL)
365 return OBJ_FM_GRID;
366 if (sPersistentServiceName == FM_COMPONENT_IMAGEBUTTON)
367 return OBJ_FM_IMAGEBUTTON;
368 if (sPersistentServiceName == FM_COMPONENT_FILECONTROL)
369 return OBJ_FM_FILECONTROL;
370 if (sPersistentServiceName == FM_COMPONENT_DATEFIELD)
371 return OBJ_FM_DATEFIELD;
372 if (sPersistentServiceName == FM_COMPONENT_TIMEFIELD)
373 return OBJ_FM_TIMEFIELD;
374 if (sPersistentServiceName == FM_COMPONENT_NUMERICFIELD)
375 return OBJ_FM_NUMERICFIELD;
376 if (sPersistentServiceName == FM_COMPONENT_CURRENCYFIELD)
377 return OBJ_FM_CURRENCYFIELD;
378 if (sPersistentServiceName == FM_COMPONENT_PATTERNFIELD)
379 return OBJ_FM_PATTERNFIELD;
380 if (sPersistentServiceName == FM_COMPONENT_HIDDEN) // 5.0-Name
381 return OBJ_FM_HIDDEN;
382 if (sPersistentServiceName == FM_COMPONENT_HIDDENCONTROL)
383 return OBJ_FM_HIDDEN;
384 if (sPersistentServiceName == FM_COMPONENT_IMAGECONTROL)
385 return OBJ_FM_IMAGECONTROL;
386 if (sPersistentServiceName == FM_COMPONENT_FORMATTEDFIELD)
388 OSL_FAIL("::getControlTypeByObject : suspicious persistent service name (formatted field) !");
389 // objects with that service name should exist as they aren't compatible with older versions
390 return OBJ_FM_FORMATTEDFIELD;
392 if ( sPersistentServiceName == FM_SUN_COMPONENT_SCROLLBAR )
393 return OBJ_FM_SCROLLBAR;
394 if ( sPersistentServiceName == FM_SUN_COMPONENT_SPINBUTTON )
395 return OBJ_FM_SPINBUTTON;
396 if ( sPersistentServiceName == FM_SUN_COMPONENT_NAVIGATIONBAR )
397 return OBJ_FM_NAVIGATIONBAR;
399 OSL_FAIL("::getControlTypeByObject : unknown object type !");
400 return OBJ_FM_CONTROL;
404 bool isRowSetAlive(const Reference< XInterface >& _rxRowSet)
406 bool bIsAlive = false;
407 Reference< css::sdbcx::XColumnsSupplier> xSupplyCols(_rxRowSet, UNO_QUERY);
408 Reference< css::container::XIndexAccess> xCols;
409 if (xSupplyCols.is())
410 xCols.set(xSupplyCols->getColumns(), UNO_QUERY);
411 if (xCols.is() && (xCols->getCount() > 0))
412 bIsAlive = true;
414 return bIsAlive;
417 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */