update credits
[LibreOffice.git] / svx / source / form / fmtools.cxx
blobddeb875695acc426e3d9fc7de8cd856a08f68776
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.hrc"
22 #include "fmservs.hxx"
23 #include "svx/fmtools.hxx"
24 #include "svx/dbtoolsclient.hxx"
25 #include "svx/fmglob.hxx"
27 #include <com/sun/star/awt/LineEndFormat.hpp>
28 #include <com/sun/star/beans/PropertyAttribute.hpp>
29 #include <com/sun/star/beans/XIntrospection.hpp>
30 #include <com/sun/star/container/XChild.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/XCompletedConnection.hpp>
46 #include <com/sun/star/sdb/XQueriesSupplier.hpp>
47 #include <com/sun/star/sdb/XResultSetAccess.hpp>
48 #include <com/sun/star/sdbc/DataType.hpp>
49 #include <com/sun/star/sdbc/XDataSource.hpp>
50 #include <com/sun/star/sdbcx/Privilege.hpp>
51 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
52 #include <com/sun/star/task/XInteractionHandler.hpp>
53 #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
54 #include <com/sun/star/uno/XNamingService.hpp>
55 #include <com/sun/star/util/Language.hpp>
56 #include <com/sun/star/util/NumberFormat.hpp>
57 #include <com/sun/star/util/XCloneable.hpp>
58 #include <com/sun/star/util/XNumberFormatTypes.hpp>
59 #include <com/sun/star/util/XNumberFormats.hpp>
60 #include <com/sun/star/util/XNumberFormatsSupplier.hpp>
61 #include <com/sun/star/util/XNumberFormatter.hpp>
63 #include <basic/sbxvar.hxx>
64 #include <svl/eitem.hxx>
65 #include <svl/stritem.hxx>
66 #include <comphelper/container.hxx>
67 #include <comphelper/extract.hxx>
68 #include <comphelper/processfactory.hxx>
69 #include <comphelper/property.hxx>
70 #include <comphelper/sequence.hxx>
71 #include <comphelper/types.hxx>
72 #include <comphelper/uno3.hxx>
73 #include <connectivity/dbexception.hxx>
74 #include <connectivity/dbtools.hxx>
75 #include <cppuhelper/typeprovider.hxx>
76 #include <rtl/logfile.hxx>
77 #include <rtl/math.hxx>
78 #include <sfx2/bindings.hxx>
79 #include <toolkit/unohlp.hxx>
80 #include <tools/debug.hxx>
81 #include <vcl/stdtext.hxx>
82 #include <vcl/svapp.hxx>
84 #include <algorithm>
86 using namespace ::com::sun::star::uno;
87 using namespace ::com::sun::star::util;
88 using namespace ::com::sun::star::lang;
89 using namespace ::com::sun::star::frame;
90 using namespace ::com::sun::star::awt;
91 using namespace ::com::sun::star::beans;
92 using namespace ::com::sun::star::container;
93 using namespace ::com::sun::star::ui::dialogs;
94 using namespace ::com::sun::star::sdbc;
95 using namespace ::com::sun::star::sdbcx;
96 using namespace ::com::sun::star::sdb;
97 using namespace ::com::sun::star::task;
98 using namespace ::com::sun::star::form;
99 using namespace ::svxform;
100 using namespace ::connectivity::simple;
102 // ------------------------------------------------------------------------------
103 namespace
105 static bool lcl_shouldDisplayError( const Any& _rError )
107 SQLException aError;
108 if ( !( _rError >>= aError ) )
109 return true;
111 if ( aError.Message.indexOfAsciiL( RTL_CONSTASCII_STRINGPARAM( "[OOoBase]" ) ) != 0 )
112 // it is an exception *not* thrown by an OOo Base core component
113 return true;
115 // the only exception we do not display ATM is a RowSetVetoException, which
116 // has been raised because an XRowSetApprovalListener vetoed a change
117 if ( aError.ErrorCode + ErrorCondition::ROW_SET_OPERATION_VETOED == 0 )
118 return false;
120 // everything else is to be displayed
121 return true;
125 // ------------------------------------------------------------------------------
126 void displayException(const Any& _rExcept, Window* _pParent)
128 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "svx", "Ocke.Janssen@sun.com", "fmtools::displayException" );
129 // check whether we need to display it
130 if ( !lcl_shouldDisplayError( _rExcept ) )
131 return;
135 // the parent window
136 Window* pParentWindow = _pParent ? _pParent : GetpApp()->GetDefDialogParent();
137 Reference< XWindow > xParentWindow = VCLUnoHelper::GetInterface(pParentWindow);
139 Reference< XExecutableDialog > xErrorDialog = ErrorMessageDialog::create(::comphelper::getProcessComponentContext(), "", xParentWindow, _rExcept);
140 xErrorDialog->execute();
142 catch(const Exception&)
144 OSL_FAIL("displayException: could not display the error message!");
148 // ------------------------------------------------------------------------------
149 void displayException(const ::com::sun::star::sdbc::SQLException& _rExcept, Window* _pParent)
151 displayException(makeAny(_rExcept), _pParent);
154 // ------------------------------------------------------------------------------
155 void displayException(const ::com::sun::star::sdbc::SQLWarning& _rExcept, Window* _pParent)
157 displayException(makeAny(_rExcept), _pParent);
160 // ------------------------------------------------------------------------------
161 void displayException(const ::com::sun::star::sdb::SQLContext& _rExcept, Window* _pParent)
163 displayException(makeAny(_rExcept), _pParent);
166 // ------------------------------------------------------------------------------
167 void displayException(const ::com::sun::star::sdb::SQLErrorEvent& _rEvent, Window* _pParent)
169 displayException(_rEvent.Reason, _pParent);
172 //------------------------------------------------------------------------------
173 sal_Int32 getElementPos(const Reference< ::com::sun::star::container::XIndexAccess>& xCont, const Reference< XInterface >& xElement)
175 sal_Int32 nIndex = -1;
176 if (!xCont.is())
177 return nIndex;
180 Reference< XInterface > xNormalized( xElement, UNO_QUERY );
181 DBG_ASSERT( xNormalized.is(), "getElementPos: invalid element!" );
182 if ( xNormalized.is() )
184 // Feststellen an welcher Position sich das Kind befindet
185 nIndex = xCont->getCount();
186 while (nIndex--)
190 Reference< XInterface > xCurrent(xCont->getByIndex( nIndex ),UNO_QUERY);
191 DBG_ASSERT( xCurrent.get() == Reference< XInterface >( xCurrent, UNO_QUERY ).get(),
192 "getElementPos: container element not normalized!" );
193 if ( xNormalized.get() == xCurrent.get() )
194 break;
196 catch(Exception&)
198 OSL_FAIL( "getElementPos: caught an exception!" );
203 return nIndex;
206 //------------------------------------------------------------------
207 OUString getLabelName(const Reference< ::com::sun::star::beans::XPropertySet>& xControlModel)
209 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "svx", "Ocke.Janssen@sun.com", "fmtools::getLabelName" );
210 if (!xControlModel.is())
211 return OUString();
213 if (::comphelper::hasProperty(FM_PROP_CONTROLLABEL, xControlModel))
215 Reference< ::com::sun::star::beans::XPropertySet> xLabelSet;
216 xControlModel->getPropertyValue(FM_PROP_CONTROLLABEL) >>= xLabelSet;
217 if (xLabelSet.is() && ::comphelper::hasProperty(FM_PROP_LABEL, xLabelSet))
219 Any aLabel( xLabelSet->getPropertyValue(FM_PROP_LABEL) );
220 if ((aLabel.getValueTypeClass() == TypeClass_STRING) && !::comphelper::getString(aLabel).isEmpty())
221 return ::comphelper::getString(aLabel);
225 return ::comphelper::getString(xControlModel->getPropertyValue(FM_PROP_CONTROLSOURCE));
228 //========================================================================
229 // = CursorWrapper
230 //------------------------------------------------------------------------
231 CursorWrapper::CursorWrapper(const Reference< ::com::sun::star::sdbc::XRowSet>& _rxCursor, sal_Bool bUseCloned)
233 ImplConstruct(Reference< ::com::sun::star::sdbc::XResultSet>(_rxCursor, UNO_QUERY), bUseCloned);
236 //------------------------------------------------------------------------
237 CursorWrapper::CursorWrapper(const Reference< ::com::sun::star::sdbc::XResultSet>& _rxCursor, sal_Bool bUseCloned)
239 ImplConstruct(_rxCursor, bUseCloned);
242 //------------------------------------------------------------------------
243 void CursorWrapper::ImplConstruct(const Reference< ::com::sun::star::sdbc::XResultSet>& _rxCursor, sal_Bool bUseCloned)
245 if (bUseCloned)
247 Reference< ::com::sun::star::sdb::XResultSetAccess> xAccess(_rxCursor, UNO_QUERY);
250 m_xMoveOperations = xAccess.is() ? xAccess->createResultSet() : Reference< ::com::sun::star::sdbc::XResultSet>();
252 catch(Exception&)
256 else
257 m_xMoveOperations = _rxCursor;
259 m_xBookmarkOperations = m_xBookmarkOperations.query( m_xMoveOperations );
260 m_xColumnsSupplier = m_xColumnsSupplier.query( m_xMoveOperations );
261 m_xPropertyAccess = m_xPropertyAccess.query( m_xMoveOperations );
263 if ( !m_xMoveOperations.is() || !m_xBookmarkOperations.is() || !m_xColumnsSupplier.is() || !m_xPropertyAccess.is() )
264 { // all or nothing !!
265 m_xMoveOperations = NULL;
266 m_xBookmarkOperations = NULL;
267 m_xColumnsSupplier = NULL;
269 else
270 m_xGeneric = m_xMoveOperations.get();
273 //------------------------------------------------------------------------
274 const CursorWrapper& CursorWrapper::operator=(const Reference< ::com::sun::star::sdbc::XRowSet>& _rxCursor)
276 m_xMoveOperations = Reference< ::com::sun::star::sdbc::XResultSet>(_rxCursor, UNO_QUERY);
277 m_xBookmarkOperations = Reference< ::com::sun::star::sdbcx::XRowLocate>(_rxCursor, UNO_QUERY);
278 m_xColumnsSupplier = Reference< ::com::sun::star::sdbcx::XColumnsSupplier>(_rxCursor, UNO_QUERY);
279 if (!m_xMoveOperations.is() || !m_xBookmarkOperations.is() || !m_xColumnsSupplier.is())
280 { // all or nothing !!
281 m_xMoveOperations = NULL;
282 m_xBookmarkOperations = NULL;
283 m_xColumnsSupplier = NULL;
285 return *this;
288 //------------------------------------------------------------------------------
289 FmXDisposeListener::~FmXDisposeListener()
291 setAdapter(NULL);
294 //------------------------------------------------------------------------------
295 void FmXDisposeListener::setAdapter(FmXDisposeMultiplexer* pAdapter)
297 if (m_pAdapter)
299 ::osl::MutexGuard aGuard(m_rMutex);
300 m_pAdapter->release();
301 m_pAdapter = NULL;
304 if (pAdapter)
306 ::osl::MutexGuard aGuard(m_rMutex);
307 m_pAdapter = pAdapter;
308 m_pAdapter->acquire();
312 //==============================================================================
313 DBG_NAME(FmXDisposeMultiplexer);
314 //------------------------------------------------------------------------------
315 FmXDisposeMultiplexer::FmXDisposeMultiplexer(FmXDisposeListener* _pListener, const Reference< ::com::sun::star::lang::XComponent>& _rxObject, sal_Int16 _nId)
316 :m_xObject(_rxObject)
317 ,m_pListener(_pListener)
318 ,m_nId(_nId)
320 DBG_CTOR(FmXDisposeMultiplexer, NULL);
321 m_pListener->setAdapter(this);
323 if (m_xObject.is())
324 m_xObject->addEventListener(this);
327 //------------------------------------------------------------------------------
328 FmXDisposeMultiplexer::~FmXDisposeMultiplexer()
330 DBG_DTOR(FmXDisposeMultiplexer, NULL);
333 // ::com::sun::star::lang::XEventListener
334 //------------------------------------------------------------------
335 void FmXDisposeMultiplexer::disposing(const ::com::sun::star::lang::EventObject& _Source) throw( RuntimeException )
337 Reference< ::com::sun::star::lang::XEventListener> xPreventDelete(this);
339 if (m_pListener)
341 m_pListener->disposing(_Source, m_nId);
342 m_pListener->setAdapter(NULL);
343 m_pListener = NULL;
345 m_xObject = NULL;
348 //------------------------------------------------------------------
349 void FmXDisposeMultiplexer::dispose()
351 if (m_xObject.is())
353 Reference< ::com::sun::star::lang::XEventListener> xPreventDelete(this);
355 m_xObject->removeEventListener(this);
356 m_xObject = NULL;
358 m_pListener->setAdapter(NULL);
359 m_pListener = NULL;
363 //==============================================================================
364 //------------------------------------------------------------------------------
365 sal_Int16 getControlTypeByObject(const Reference< ::com::sun::star::lang::XServiceInfo>& _rxObject)
367 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "svx", "Ocke.Janssen@sun.com", "fmtools::getControlTypeByObject" );
368 // ask for the persistent service name
369 Reference< ::com::sun::star::io::XPersistObject> xPersistence(_rxObject, UNO_QUERY);
370 DBG_ASSERT(xPersistence.is(), "::getControlTypeByObject : argument shold be an ::com::sun::star::io::XPersistObject !");
371 if (!xPersistence.is())
372 return OBJ_FM_CONTROL;
374 OUString sPersistentServiceName = xPersistence->getServiceName();
375 if (sPersistentServiceName.equals(FM_COMPONENT_EDIT)) // 5.0-Name
377 // may be a simple edit field or a formatted field, dependent of the supported services
378 if (_rxObject->supportsService(FM_SUN_COMPONENT_FORMATTEDFIELD))
379 return OBJ_FM_FORMATTEDFIELD;
380 return OBJ_FM_EDIT;
382 if (sPersistentServiceName.equals(FM_COMPONENT_TEXTFIELD))
383 return OBJ_FM_EDIT;
384 if (sPersistentServiceName.equals(FM_COMPONENT_COMMANDBUTTON))
385 return OBJ_FM_BUTTON;
386 if (sPersistentServiceName.equals(FM_COMPONENT_FIXEDTEXT))
387 return OBJ_FM_FIXEDTEXT;
388 if (sPersistentServiceName.equals(FM_COMPONENT_LISTBOX))
389 return OBJ_FM_LISTBOX;
390 if (sPersistentServiceName.equals(FM_COMPONENT_CHECKBOX))
391 return OBJ_FM_CHECKBOX;
392 if (sPersistentServiceName.equals(FM_COMPONENT_RADIOBUTTON))
393 return OBJ_FM_RADIOBUTTON;
394 if (sPersistentServiceName.equals(FM_COMPONENT_GROUPBOX))
395 return OBJ_FM_GROUPBOX;
396 if (sPersistentServiceName.equals(FM_COMPONENT_COMBOBOX))
397 return OBJ_FM_COMBOBOX;
398 if (sPersistentServiceName.equals(FM_COMPONENT_GRID)) // 5.0-Name
399 return OBJ_FM_GRID;
400 if (sPersistentServiceName.equals(FM_COMPONENT_GRIDCONTROL))
401 return OBJ_FM_GRID;
402 if (sPersistentServiceName.equals(FM_COMPONENT_IMAGEBUTTON))
403 return OBJ_FM_IMAGEBUTTON;
404 if (sPersistentServiceName.equals(FM_COMPONENT_FILECONTROL))
405 return OBJ_FM_FILECONTROL;
406 if (sPersistentServiceName.equals(FM_COMPONENT_DATEFIELD))
407 return OBJ_FM_DATEFIELD;
408 if (sPersistentServiceName.equals(FM_COMPONENT_TIMEFIELD))
409 return OBJ_FM_TIMEFIELD;
410 if (sPersistentServiceName.equals(FM_COMPONENT_NUMERICFIELD))
411 return OBJ_FM_NUMERICFIELD;
412 if (sPersistentServiceName.equals(FM_COMPONENT_CURRENCYFIELD))
413 return OBJ_FM_CURRENCYFIELD;
414 if (sPersistentServiceName.equals(FM_COMPONENT_PATTERNFIELD))
415 return OBJ_FM_PATTERNFIELD;
416 if (sPersistentServiceName.equals(FM_COMPONENT_HIDDEN)) // 5.0-Name
417 return OBJ_FM_HIDDEN;
418 if (sPersistentServiceName.equals(FM_COMPONENT_HIDDENCONTROL))
419 return OBJ_FM_HIDDEN;
420 if (sPersistentServiceName.equals(FM_COMPONENT_IMAGECONTROL))
421 return OBJ_FM_IMAGECONTROL;
422 if (sPersistentServiceName.equals(FM_COMPONENT_FORMATTEDFIELD))
424 OSL_FAIL("::getControlTypeByObject : suspicious persistent service name (formatted field) !");
425 // objects with that service name should exist as they aren't compatible with older versions
426 return OBJ_FM_FORMATTEDFIELD;
428 if ( sPersistentServiceName.equals( FM_SUN_COMPONENT_SCROLLBAR ) )
429 return OBJ_FM_SCROLLBAR;
430 if ( sPersistentServiceName.equals( FM_SUN_COMPONENT_SPINBUTTON) )
431 return OBJ_FM_SPINBUTTON;
432 if (sPersistentServiceName.equals(FM_SUN_COMPONENT_NAVIGATIONBAR))
433 return OBJ_FM_NAVIGATIONBAR;
435 OSL_FAIL("::getControlTypeByObject : unknown object type !");
436 return OBJ_FM_CONTROL;
439 //------------------------------------------------------------------------------
440 sal_Bool isRowSetAlive(const Reference< XInterface >& _rxRowSet)
442 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "svx", "Ocke.Janssen@sun.com", "fmtools::isRowSetAlive" );
443 sal_Bool bIsAlive = sal_False;
444 Reference< ::com::sun::star::sdbcx::XColumnsSupplier> xSupplyCols(_rxRowSet, UNO_QUERY);
445 Reference< ::com::sun::star::container::XIndexAccess> xCols;
446 if (xSupplyCols.is())
447 xCols = Reference< ::com::sun::star::container::XIndexAccess>(xSupplyCols->getColumns(), UNO_QUERY);
448 if (xCols.is() && (xCols->getCount() > 0))
449 bIsAlive = sal_True;
451 return bIsAlive;
454 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */