1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
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
;
66 bool lcl_shouldDisplayError( const Any
& _rError
)
69 if ( !( _rError
>>= aError
) )
72 if ( ! aError
.Message
.startsWith( "[OOoBase]" ) )
73 // it is an exception *not* thrown by an OOo Base core component
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 )
81 // everything else is to be displayed
86 void displayException(const Any
& _rExcept
, const css::uno::Reference
<css::awt::XWindow
>& rParent
)
88 // check whether we need to display it
89 if ( !lcl_shouldDisplayError( _rExcept
) )
94 Reference
< XExecutableDialog
> xErrorDialog
= ErrorMessageDialog::create(::comphelper::getProcessComponentContext(), "", rParent
, _rExcept
);
95 xErrorDialog
->execute();
97 catch(const Exception
&)
99 TOOLS_WARN_EXCEPTION("svx.form", "could not display the error message!");
103 void displayException(const css::sdbc::SQLException
& _rExcept
, const css::uno::Reference
<css::awt::XWindow
>& rParent
)
105 displayException(makeAny(_rExcept
), rParent
);
108 void displayException(const css::sdb::SQLContext
& _rExcept
, const css::uno::Reference
<css::awt::XWindow
>& rParent
)
110 displayException(makeAny(_rExcept
), rParent
);
113 void displayException(const css::sdb::SQLErrorEvent
& _rEvent
, const css::uno::Reference
<css::awt::XWindow
>& rParent
)
115 displayException(_rEvent
.Reason
, rParent
);
118 sal_Int32
getElementPos(const Reference
< css::container::XIndexAccess
>& xCont
, const Reference
< XInterface
>& xElement
)
120 sal_Int32 nIndex
= -1;
125 Reference
< XInterface
> xNormalized( xElement
, UNO_QUERY
);
126 DBG_ASSERT( xNormalized
.is(), "getElementPos: invalid element!" );
127 if ( xNormalized
.is() )
129 // find child position
130 nIndex
= xCont
->getCount();
135 Reference
< XInterface
> xCurrent(xCont
->getByIndex( nIndex
),UNO_QUERY
);
136 DBG_ASSERT( xCurrent
.get() == Reference
< XInterface
>( xCurrent
, UNO_QUERY
).get(),
137 "getElementPos: container element not normalized!" );
138 if ( xNormalized
.get() == xCurrent
.get() )
143 TOOLS_WARN_EXCEPTION( "svx", "getElementPos" );
152 OUString
getLabelName(const Reference
< css::beans::XPropertySet
>& xControlModel
)
154 if (!xControlModel
.is())
157 if (::comphelper::hasProperty(FM_PROP_CONTROLLABEL
, xControlModel
))
159 Reference
< css::beans::XPropertySet
> xLabelSet
;
160 xControlModel
->getPropertyValue(FM_PROP_CONTROLLABEL
) >>= xLabelSet
;
161 if (xLabelSet
.is() && ::comphelper::hasProperty(FM_PROP_LABEL
, xLabelSet
))
163 Any
aLabel( xLabelSet
->getPropertyValue(FM_PROP_LABEL
) );
164 if ((aLabel
.getValueTypeClass() == TypeClass_STRING
) && !::comphelper::getString(aLabel
).isEmpty())
165 return ::comphelper::getString(aLabel
);
169 return ::comphelper::getString(xControlModel
->getPropertyValue(FM_PROP_CONTROLSOURCE
));
175 CursorWrapper::CursorWrapper(const Reference
< css::sdbc::XRowSet
>& _rxCursor
, bool bUseCloned
)
177 ImplConstruct(Reference
< css::sdbc::XResultSet
>(_rxCursor
), bUseCloned
);
181 CursorWrapper::CursorWrapper(const Reference
< css::sdbc::XResultSet
>& _rxCursor
, bool bUseCloned
)
183 ImplConstruct(_rxCursor
, bUseCloned
);
187 void CursorWrapper::ImplConstruct(const Reference
< css::sdbc::XResultSet
>& _rxCursor
, bool bUseCloned
)
191 Reference
< css::sdb::XResultSetAccess
> xAccess(_rxCursor
, UNO_QUERY
);
194 m_xMoveOperations
= xAccess
.is() ? xAccess
->createResultSet() : Reference
< css::sdbc::XResultSet
>();
201 m_xMoveOperations
= _rxCursor
;
203 m_xBookmarkOperations
.set(m_xMoveOperations
, css::uno::UNO_QUERY
);
204 m_xColumnsSupplier
.set(m_xMoveOperations
, css::uno::UNO_QUERY
);
205 m_xPropertyAccess
.set(m_xMoveOperations
, css::uno::UNO_QUERY
);
207 if ( !m_xMoveOperations
.is() || !m_xBookmarkOperations
.is() || !m_xColumnsSupplier
.is() || !m_xPropertyAccess
.is() )
208 { // all or nothing !!
209 m_xMoveOperations
= nullptr;
210 m_xBookmarkOperations
= nullptr;
211 m_xColumnsSupplier
= nullptr;
214 m_xGeneric
= m_xMoveOperations
.get();
217 CursorWrapper
& CursorWrapper::operator=(const Reference
< css::sdbc::XRowSet
>& _rxCursor
)
219 m_xMoveOperations
.set(_rxCursor
);
220 m_xBookmarkOperations
.set(_rxCursor
, UNO_QUERY
);
221 m_xColumnsSupplier
.set(_rxCursor
, UNO_QUERY
);
222 if (!m_xMoveOperations
.is() || !m_xBookmarkOperations
.is() || !m_xColumnsSupplier
.is())
223 { // all or nothing !!
224 m_xMoveOperations
= nullptr;
225 m_xBookmarkOperations
= nullptr;
226 m_xColumnsSupplier
= nullptr;
231 FmXDisposeListener::~FmXDisposeListener()
236 void FmXDisposeListener::setAdapter(FmXDisposeMultiplexer
* pAdapter
)
238 ::osl::MutexGuard
aGuard(m_aMutex
);
239 m_pAdapter
= pAdapter
;
242 FmXDisposeMultiplexer::FmXDisposeMultiplexer(FmXDisposeListener
* _pListener
, const Reference
< css::lang::XComponent
>& _rxObject
)
243 :m_xObject(_rxObject
)
244 ,m_pListener(_pListener
)
246 m_pListener
->setAdapter(this);
249 m_xObject
->addEventListener(this);
252 FmXDisposeMultiplexer::~FmXDisposeMultiplexer()
256 // css::lang::XEventListener
258 void FmXDisposeMultiplexer::disposing(const css::lang::EventObject
& /*Source*/)
260 Reference
< css::lang::XEventListener
> xPreventDelete(this);
264 m_pListener
->disposing(0);
265 m_pListener
->setAdapter(nullptr);
266 m_pListener
= nullptr;
272 void FmXDisposeMultiplexer::dispose()
276 Reference
< css::lang::XEventListener
> xPreventDelete(this);
278 m_xObject
->removeEventListener(this);
281 m_pListener
->setAdapter(nullptr);
282 m_pListener
= nullptr;
287 sal_Int16
getControlTypeByObject(const Reference
< css::lang::XServiceInfo
>& _rxObject
)
289 // ask for the persistent service name
290 Reference
< css::io::XPersistObject
> xPersistence(_rxObject
, UNO_QUERY
);
291 DBG_ASSERT(xPersistence
.is(), "::getControlTypeByObject : argument should be a css::io::XPersistObject !");
292 if (!xPersistence
.is())
293 return OBJ_FM_CONTROL
;
295 OUString sPersistentServiceName
= xPersistence
->getServiceName();
296 if (sPersistentServiceName
== FM_COMPONENT_EDIT
) // 5.0-Name
298 // may be a simple edit field or a formatted field, dependent of the supported services
299 if (_rxObject
->supportsService(FM_SUN_COMPONENT_FORMATTEDFIELD
))
300 return OBJ_FM_FORMATTEDFIELD
;
303 if (sPersistentServiceName
== FM_COMPONENT_TEXTFIELD
)
305 if (sPersistentServiceName
== FM_COMPONENT_COMMANDBUTTON
)
306 return OBJ_FM_BUTTON
;
307 if (sPersistentServiceName
== FM_COMPONENT_FIXEDTEXT
)
308 return OBJ_FM_FIXEDTEXT
;
309 if (sPersistentServiceName
== FM_COMPONENT_LISTBOX
)
310 return OBJ_FM_LISTBOX
;
311 if (sPersistentServiceName
== FM_COMPONENT_CHECKBOX
)
312 return OBJ_FM_CHECKBOX
;
313 if (sPersistentServiceName
== FM_COMPONENT_RADIOBUTTON
)
314 return OBJ_FM_RADIOBUTTON
;
315 if (sPersistentServiceName
== FM_COMPONENT_GROUPBOX
)
316 return OBJ_FM_GROUPBOX
;
317 if (sPersistentServiceName
== FM_COMPONENT_COMBOBOX
)
318 return OBJ_FM_COMBOBOX
;
319 if (sPersistentServiceName
== FM_COMPONENT_GRID
) // 5.0-Name
321 if (sPersistentServiceName
== FM_COMPONENT_GRIDCONTROL
)
323 if (sPersistentServiceName
== FM_COMPONENT_IMAGEBUTTON
)
324 return OBJ_FM_IMAGEBUTTON
;
325 if (sPersistentServiceName
== FM_COMPONENT_FILECONTROL
)
326 return OBJ_FM_FILECONTROL
;
327 if (sPersistentServiceName
== FM_COMPONENT_DATEFIELD
)
328 return OBJ_FM_DATEFIELD
;
329 if (sPersistentServiceName
== FM_COMPONENT_TIMEFIELD
)
330 return OBJ_FM_TIMEFIELD
;
331 if (sPersistentServiceName
== FM_COMPONENT_NUMERICFIELD
)
332 return OBJ_FM_NUMERICFIELD
;
333 if (sPersistentServiceName
== FM_COMPONENT_CURRENCYFIELD
)
334 return OBJ_FM_CURRENCYFIELD
;
335 if (sPersistentServiceName
== FM_COMPONENT_PATTERNFIELD
)
336 return OBJ_FM_PATTERNFIELD
;
337 if (sPersistentServiceName
== FM_COMPONENT_HIDDEN
) // 5.0-Name
338 return OBJ_FM_HIDDEN
;
339 if (sPersistentServiceName
== FM_COMPONENT_HIDDENCONTROL
)
340 return OBJ_FM_HIDDEN
;
341 if (sPersistentServiceName
== FM_COMPONENT_IMAGECONTROL
)
342 return OBJ_FM_IMAGECONTROL
;
343 if (sPersistentServiceName
== FM_COMPONENT_FORMATTEDFIELD
)
345 OSL_FAIL("::getControlTypeByObject : suspicious persistent service name (formatted field) !");
346 // objects with that service name should exist as they aren't compatible with older versions
347 return OBJ_FM_FORMATTEDFIELD
;
349 if ( sPersistentServiceName
== FM_SUN_COMPONENT_SCROLLBAR
)
350 return OBJ_FM_SCROLLBAR
;
351 if ( sPersistentServiceName
== FM_SUN_COMPONENT_SPINBUTTON
)
352 return OBJ_FM_SPINBUTTON
;
353 if ( sPersistentServiceName
== FM_SUN_COMPONENT_NAVIGATIONBAR
)
354 return OBJ_FM_NAVIGATIONBAR
;
356 OSL_FAIL("::getControlTypeByObject : unknown object type !");
357 return OBJ_FM_CONTROL
;
361 bool isRowSetAlive(const Reference
< XInterface
>& _rxRowSet
)
363 bool bIsAlive
= false;
364 Reference
< css::sdbcx::XColumnsSupplier
> xSupplyCols(_rxRowSet
, UNO_QUERY
);
365 Reference
< css::container::XIndexAccess
> xCols
;
366 if (xSupplyCols
.is())
367 xCols
.set(xSupplyCols
->getColumns(), UNO_QUERY
);
368 if (xCols
.is() && (xCols
->getCount() > 0))
374 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */