Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / svx / source / form / fmtools.cxx
blob73459db395a7dae7080059500578b3644082b299
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/math.hxx>
77 #include <sfx2/bindings.hxx>
78 #include <toolkit/helper/vclunohelper.hxx>
79 #include <tools/debug.hxx>
80 #include <vcl/stdtext.hxx>
81 #include <vcl/svapp.hxx>
83 #include <algorithm>
85 using namespace ::com::sun::star::uno;
86 using namespace ::com::sun::star::util;
87 using namespace ::com::sun::star::lang;
88 using namespace ::com::sun::star::frame;
89 using namespace ::com::sun::star::awt;
90 using namespace ::com::sun::star::beans;
91 using namespace ::com::sun::star::container;
92 using namespace ::com::sun::star::ui::dialogs;
93 using namespace ::com::sun::star::sdbc;
94 using namespace ::com::sun::star::sdbcx;
95 using namespace ::com::sun::star::sdb;
96 using namespace ::com::sun::star::task;
97 using namespace ::com::sun::star::form;
98 using namespace ::svxform;
99 using namespace ::connectivity::simple;
102 namespace
104 static bool lcl_shouldDisplayError( const Any& _rError )
106 SQLException aError;
107 if ( !( _rError >>= aError ) )
108 return true;
110 if ( ! aError.Message.startsWith( "[OOoBase]" ) )
111 // it is an exception *not* thrown by an OOo Base core component
112 return true;
114 // the only exception we do not display ATM is a RowSetVetoException, which
115 // has been raised because an XRowSetApprovalListener vetoed a change
116 if ( aError.ErrorCode + ErrorCondition::ROW_SET_OPERATION_VETOED == 0 )
117 return false;
119 // everything else is to be displayed
120 return true;
125 void displayException(const Any& _rExcept, Window* _pParent)
127 // check whether we need to display it
128 if ( !lcl_shouldDisplayError( _rExcept ) )
129 return;
133 // the parent window
134 Window* pParentWindow = _pParent ? _pParent : GetpApp()->GetDefDialogParent();
135 Reference< XWindow > xParentWindow = VCLUnoHelper::GetInterface(pParentWindow);
137 Reference< XExecutableDialog > xErrorDialog = ErrorMessageDialog::create(::comphelper::getProcessComponentContext(), "", xParentWindow, _rExcept);
138 xErrorDialog->execute();
140 catch(const Exception&)
142 OSL_FAIL("displayException: could not display the error message!");
147 void displayException(const ::com::sun::star::sdbc::SQLException& _rExcept, Window* _pParent)
149 displayException(makeAny(_rExcept), _pParent);
153 void displayException(const ::com::sun::star::sdbc::SQLWarning& _rExcept, Window* _pParent)
155 displayException(makeAny(_rExcept), _pParent);
159 void displayException(const ::com::sun::star::sdb::SQLContext& _rExcept, Window* _pParent)
161 displayException(makeAny(_rExcept), _pParent);
165 void displayException(const ::com::sun::star::sdb::SQLErrorEvent& _rEvent, Window* _pParent)
167 displayException(_rEvent.Reason, _pParent);
171 sal_Int32 getElementPos(const Reference< ::com::sun::star::container::XIndexAccess>& xCont, const Reference< XInterface >& xElement)
173 sal_Int32 nIndex = -1;
174 if (!xCont.is())
175 return nIndex;
178 Reference< XInterface > xNormalized( xElement, UNO_QUERY );
179 DBG_ASSERT( xElement.is(), "getElementPos: invalid element!" );
180 if ( xNormalized.is() )
182 // Feststellen an welcher Position sich das Kind befindet
183 nIndex = xCont->getCount();
184 while (nIndex--)
188 Reference< XInterface > xCurrent(xCont->getByIndex( nIndex ),UNO_QUERY);
189 DBG_ASSERT( xCurrent.get() == Reference< XInterface >( xCurrent, UNO_QUERY ).get(),
190 "getElementPos: container element not normalized!" );
191 if ( xNormalized.get() == xCurrent.get() )
192 break;
194 catch(Exception&)
196 OSL_FAIL( "getElementPos: caught an exception!" );
201 return nIndex;
205 OUString getLabelName(const Reference< ::com::sun::star::beans::XPropertySet>& xControlModel)
207 if (!xControlModel.is())
208 return OUString();
210 if (::comphelper::hasProperty(FM_PROP_CONTROLLABEL, xControlModel))
212 Reference< ::com::sun::star::beans::XPropertySet> xLabelSet;
213 xControlModel->getPropertyValue(FM_PROP_CONTROLLABEL) >>= xLabelSet;
214 if (xLabelSet.is() && ::comphelper::hasProperty(FM_PROP_LABEL, xLabelSet))
216 Any aLabel( xLabelSet->getPropertyValue(FM_PROP_LABEL) );
217 if ((aLabel.getValueTypeClass() == TypeClass_STRING) && !::comphelper::getString(aLabel).isEmpty())
218 return ::comphelper::getString(aLabel);
222 return ::comphelper::getString(xControlModel->getPropertyValue(FM_PROP_CONTROLSOURCE));
226 // = CursorWrapper
228 CursorWrapper::CursorWrapper(const Reference< ::com::sun::star::sdbc::XRowSet>& _rxCursor, bool bUseCloned)
230 ImplConstruct(Reference< ::com::sun::star::sdbc::XResultSet>(_rxCursor, UNO_QUERY), bUseCloned);
234 CursorWrapper::CursorWrapper(const Reference< ::com::sun::star::sdbc::XResultSet>& _rxCursor, bool bUseCloned)
236 ImplConstruct(_rxCursor, bUseCloned);
240 void CursorWrapper::ImplConstruct(const Reference< ::com::sun::star::sdbc::XResultSet>& _rxCursor, bool bUseCloned)
242 if (bUseCloned)
244 Reference< ::com::sun::star::sdb::XResultSetAccess> xAccess(_rxCursor, UNO_QUERY);
247 m_xMoveOperations = xAccess.is() ? xAccess->createResultSet() : Reference< ::com::sun::star::sdbc::XResultSet>();
249 catch(Exception&)
253 else
254 m_xMoveOperations = _rxCursor;
256 m_xBookmarkOperations = m_xBookmarkOperations.query( m_xMoveOperations );
257 m_xColumnsSupplier = m_xColumnsSupplier.query( m_xMoveOperations );
258 m_xPropertyAccess = m_xPropertyAccess.query( m_xMoveOperations );
260 if ( !m_xMoveOperations.is() || !m_xBookmarkOperations.is() || !m_xColumnsSupplier.is() || !m_xPropertyAccess.is() )
261 { // all or nothing !!
262 m_xMoveOperations = NULL;
263 m_xBookmarkOperations = NULL;
264 m_xColumnsSupplier = NULL;
266 else
267 m_xGeneric = m_xMoveOperations.get();
271 const CursorWrapper& CursorWrapper::operator=(const Reference< ::com::sun::star::sdbc::XRowSet>& _rxCursor)
273 m_xMoveOperations = Reference< ::com::sun::star::sdbc::XResultSet>(_rxCursor, UNO_QUERY);
274 m_xBookmarkOperations = Reference< ::com::sun::star::sdbcx::XRowLocate>(_rxCursor, UNO_QUERY);
275 m_xColumnsSupplier = Reference< ::com::sun::star::sdbcx::XColumnsSupplier>(_rxCursor, UNO_QUERY);
276 if (!m_xMoveOperations.is() || !m_xBookmarkOperations.is() || !m_xColumnsSupplier.is())
277 { // all or nothing !!
278 m_xMoveOperations = NULL;
279 m_xBookmarkOperations = NULL;
280 m_xColumnsSupplier = NULL;
282 return *this;
286 FmXDisposeListener::~FmXDisposeListener()
288 setAdapter(NULL);
292 void FmXDisposeListener::setAdapter(FmXDisposeMultiplexer* pAdapter)
294 if (m_pAdapter)
296 ::osl::MutexGuard aGuard(m_rMutex);
297 m_pAdapter->release();
298 m_pAdapter = NULL;
301 if (pAdapter)
303 ::osl::MutexGuard aGuard(m_rMutex);
304 m_pAdapter = pAdapter;
305 m_pAdapter->acquire();
311 FmXDisposeMultiplexer::FmXDisposeMultiplexer(FmXDisposeListener* _pListener, const Reference< ::com::sun::star::lang::XComponent>& _rxObject, sal_Int16 _nId)
312 :m_xObject(_rxObject)
313 ,m_pListener(_pListener)
314 ,m_nId(_nId)
316 m_pListener->setAdapter(this);
318 if (m_xObject.is())
319 m_xObject->addEventListener(this);
323 FmXDisposeMultiplexer::~FmXDisposeMultiplexer()
327 // ::com::sun::star::lang::XEventListener
329 void FmXDisposeMultiplexer::disposing(const ::com::sun::star::lang::EventObject& _Source) throw( RuntimeException, std::exception )
331 Reference< ::com::sun::star::lang::XEventListener> xPreventDelete(this);
333 if (m_pListener)
335 m_pListener->disposing(_Source, m_nId);
336 m_pListener->setAdapter(NULL);
337 m_pListener = NULL;
339 m_xObject = NULL;
343 void FmXDisposeMultiplexer::dispose()
345 if (m_xObject.is())
347 Reference< ::com::sun::star::lang::XEventListener> xPreventDelete(this);
349 m_xObject->removeEventListener(this);
350 m_xObject = NULL;
352 m_pListener->setAdapter(NULL);
353 m_pListener = NULL;
359 sal_Int16 getControlTypeByObject(const Reference< ::com::sun::star::lang::XServiceInfo>& _rxObject)
361 // ask for the persistent service name
362 Reference< ::com::sun::star::io::XPersistObject> xPersistence(_rxObject, UNO_QUERY);
363 DBG_ASSERT(xPersistence.is(), "::getControlTypeByObject : argument shold be an ::com::sun::star::io::XPersistObject !");
364 if (!xPersistence.is())
365 return OBJ_FM_CONTROL;
367 OUString sPersistentServiceName = xPersistence->getServiceName();
368 if (sPersistentServiceName.equals(FM_COMPONENT_EDIT)) // 5.0-Name
370 // may be a simple edit field or a formatted field, dependent of the supported services
371 if (_rxObject->supportsService(FM_SUN_COMPONENT_FORMATTEDFIELD))
372 return OBJ_FM_FORMATTEDFIELD;
373 return OBJ_FM_EDIT;
375 if (sPersistentServiceName.equals(FM_COMPONENT_TEXTFIELD))
376 return OBJ_FM_EDIT;
377 if (sPersistentServiceName.equals(FM_COMPONENT_COMMANDBUTTON))
378 return OBJ_FM_BUTTON;
379 if (sPersistentServiceName.equals(FM_COMPONENT_FIXEDTEXT))
380 return OBJ_FM_FIXEDTEXT;
381 if (sPersistentServiceName.equals(FM_COMPONENT_LISTBOX))
382 return OBJ_FM_LISTBOX;
383 if (sPersistentServiceName.equals(FM_COMPONENT_CHECKBOX))
384 return OBJ_FM_CHECKBOX;
385 if (sPersistentServiceName.equals(FM_COMPONENT_RADIOBUTTON))
386 return OBJ_FM_RADIOBUTTON;
387 if (sPersistentServiceName.equals(FM_COMPONENT_GROUPBOX))
388 return OBJ_FM_GROUPBOX;
389 if (sPersistentServiceName.equals(FM_COMPONENT_COMBOBOX))
390 return OBJ_FM_COMBOBOX;
391 if (sPersistentServiceName.equals(FM_COMPONENT_GRID)) // 5.0-Name
392 return OBJ_FM_GRID;
393 if (sPersistentServiceName.equals(FM_COMPONENT_GRIDCONTROL))
394 return OBJ_FM_GRID;
395 if (sPersistentServiceName.equals(FM_COMPONENT_IMAGEBUTTON))
396 return OBJ_FM_IMAGEBUTTON;
397 if (sPersistentServiceName.equals(FM_COMPONENT_FILECONTROL))
398 return OBJ_FM_FILECONTROL;
399 if (sPersistentServiceName.equals(FM_COMPONENT_DATEFIELD))
400 return OBJ_FM_DATEFIELD;
401 if (sPersistentServiceName.equals(FM_COMPONENT_TIMEFIELD))
402 return OBJ_FM_TIMEFIELD;
403 if (sPersistentServiceName.equals(FM_COMPONENT_NUMERICFIELD))
404 return OBJ_FM_NUMERICFIELD;
405 if (sPersistentServiceName.equals(FM_COMPONENT_CURRENCYFIELD))
406 return OBJ_FM_CURRENCYFIELD;
407 if (sPersistentServiceName.equals(FM_COMPONENT_PATTERNFIELD))
408 return OBJ_FM_PATTERNFIELD;
409 if (sPersistentServiceName.equals(FM_COMPONENT_HIDDEN)) // 5.0-Name
410 return OBJ_FM_HIDDEN;
411 if (sPersistentServiceName.equals(FM_COMPONENT_HIDDENCONTROL))
412 return OBJ_FM_HIDDEN;
413 if (sPersistentServiceName.equals(FM_COMPONENT_IMAGECONTROL))
414 return OBJ_FM_IMAGECONTROL;
415 if (sPersistentServiceName.equals(FM_COMPONENT_FORMATTEDFIELD))
417 OSL_FAIL("::getControlTypeByObject : suspicious persistent service name (formatted field) !");
418 // objects with that service name should exist as they aren't compatible with older versions
419 return OBJ_FM_FORMATTEDFIELD;
421 if ( sPersistentServiceName.equals( FM_SUN_COMPONENT_SCROLLBAR ) )
422 return OBJ_FM_SCROLLBAR;
423 if ( sPersistentServiceName.equals( FM_SUN_COMPONENT_SPINBUTTON) )
424 return OBJ_FM_SPINBUTTON;
425 if (sPersistentServiceName.equals(FM_SUN_COMPONENT_NAVIGATIONBAR))
426 return OBJ_FM_NAVIGATIONBAR;
428 OSL_FAIL("::getControlTypeByObject : unknown object type !");
429 return OBJ_FM_CONTROL;
433 bool isRowSetAlive(const Reference< XInterface >& _rxRowSet)
435 bool bIsAlive = false;
436 Reference< ::com::sun::star::sdbcx::XColumnsSupplier> xSupplyCols(_rxRowSet, UNO_QUERY);
437 Reference< ::com::sun::star::container::XIndexAccess> xCols;
438 if (xSupplyCols.is())
439 xCols = Reference< ::com::sun::star::container::XIndexAccess>(xSupplyCols->getColumns(), UNO_QUERY);
440 if (xCols.is() && (xCols->getCount() > 0))
441 bIsAlive = true;
443 return bIsAlive;
446 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */