Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
21 : #include "fmprop.hrc"
22 : #include "fmservs.hxx"
23 : #include "svx/fmtools.hxx"
24 : #include "svx/fmglob.hxx"
25 :
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/form/XForm.hpp>
31 : #include <com/sun/star/form/XFormComponent.hpp>
32 : #include <com/sun/star/form/XGridColumnFactory.hpp>
33 : #include <com/sun/star/io/XActiveDataSink.hpp>
34 : #include <com/sun/star/io/XActiveDataSource.hpp>
35 : #include <com/sun/star/io/XObjectInputStream.hpp>
36 : #include <com/sun/star/io/XObjectOutputStream.hpp>
37 : #include <com/sun/star/io/XPersistObject.hpp>
38 : #include <com/sun/star/lang/Locale.hpp>
39 : #include <com/sun/star/lang/XServiceInfo.hpp>
40 : #include <com/sun/star/sdb/CommandType.hpp>
41 : #include <com/sun/star/sdb/ErrorCondition.hpp>
42 : #include <com/sun/star/sdb/ErrorMessageDialog.hpp>
43 : #include <com/sun/star/sdb/SQLContext.hpp>
44 : #include <com/sun/star/sdb/XCompletedConnection.hpp>
45 : #include <com/sun/star/sdb/XQueriesSupplier.hpp>
46 : #include <com/sun/star/sdb/XResultSetAccess.hpp>
47 : #include <com/sun/star/sdbc/DataType.hpp>
48 : #include <com/sun/star/sdbc/XDataSource.hpp>
49 : #include <com/sun/star/sdbcx/Privilege.hpp>
50 : #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
51 : #include <com/sun/star/task/XInteractionHandler.hpp>
52 : #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
53 : #include <com/sun/star/uno/XNamingService.hpp>
54 : #include <com/sun/star/util/Language.hpp>
55 : #include <com/sun/star/util/NumberFormat.hpp>
56 : #include <com/sun/star/util/XCloneable.hpp>
57 : #include <com/sun/star/util/XNumberFormatTypes.hpp>
58 : #include <com/sun/star/util/XNumberFormats.hpp>
59 : #include <com/sun/star/util/XNumberFormatsSupplier.hpp>
60 : #include <com/sun/star/util/XNumberFormatter.hpp>
61 :
62 : #include <basic/sbxvar.hxx>
63 : #include <svl/eitem.hxx>
64 : #include <svl/stritem.hxx>
65 : #include <comphelper/container.hxx>
66 : #include <comphelper/extract.hxx>
67 : #include <comphelper/processfactory.hxx>
68 : #include <comphelper/property.hxx>
69 : #include <comphelper/sequence.hxx>
70 : #include <comphelper/types.hxx>
71 : #include <comphelper/uno3.hxx>
72 : #include <connectivity/dbexception.hxx>
73 : #include <connectivity/dbtools.hxx>
74 : #include <cppuhelper/typeprovider.hxx>
75 : #include <rtl/math.hxx>
76 : #include <sfx2/bindings.hxx>
77 : #include <toolkit/helper/vclunohelper.hxx>
78 : #include <tools/debug.hxx>
79 : #include <vcl/stdtext.hxx>
80 : #include <vcl/svapp.hxx>
81 :
82 : #include <algorithm>
83 :
84 : using namespace ::com::sun::star::uno;
85 : using namespace ::com::sun::star::util;
86 : using namespace ::com::sun::star::lang;
87 : using namespace ::com::sun::star::frame;
88 : using namespace ::com::sun::star::awt;
89 : using namespace ::com::sun::star::beans;
90 : using namespace ::com::sun::star::container;
91 : using namespace ::com::sun::star::ui::dialogs;
92 : using namespace ::com::sun::star::sdbc;
93 : using namespace ::com::sun::star::sdbcx;
94 : using namespace ::com::sun::star::sdb;
95 : using namespace ::com::sun::star::task;
96 : using namespace ::com::sun::star::form;
97 : using namespace ::svxform;
98 :
99 :
100 : namespace
101 : {
102 0 : static bool lcl_shouldDisplayError( const Any& _rError )
103 : {
104 0 : SQLException aError;
105 0 : if ( !( _rError >>= aError ) )
106 0 : return true;
107 :
108 0 : if ( ! aError.Message.startsWith( "[OOoBase]" ) )
109 : // it is an exception *not* thrown by an OOo Base core component
110 0 : return true;
111 :
112 : // the only exception we do not display ATM is a RowSetVetoException, which
113 : // has been raised because an XRowSetApprovalListener vetoed a change
114 0 : if ( aError.ErrorCode + ErrorCondition::ROW_SET_OPERATION_VETOED == 0 )
115 0 : return false;
116 :
117 : // everything else is to be displayed
118 0 : return true;
119 : }
120 : }
121 :
122 :
123 0 : void displayException(const Any& _rExcept, vcl::Window* _pParent)
124 : {
125 : // check whether we need to display it
126 0 : if ( !lcl_shouldDisplayError( _rExcept ) )
127 0 : return;
128 :
129 : try
130 : {
131 : // the parent window
132 0 : vcl::Window* pParentWindow = _pParent ? _pParent : Application::GetDefDialogParent();
133 0 : Reference< XWindow > xParentWindow = VCLUnoHelper::GetInterface(pParentWindow);
134 :
135 0 : Reference< XExecutableDialog > xErrorDialog = ErrorMessageDialog::create(::comphelper::getProcessComponentContext(), "", xParentWindow, _rExcept);
136 0 : xErrorDialog->execute();
137 : }
138 0 : catch(const Exception&)
139 : {
140 : OSL_FAIL("displayException: could not display the error message!");
141 : }
142 : }
143 :
144 :
145 0 : void displayException(const ::com::sun::star::sdbc::SQLException& _rExcept, vcl::Window* _pParent)
146 : {
147 0 : displayException(makeAny(_rExcept), _pParent);
148 0 : }
149 :
150 :
151 0 : void displayException(const ::com::sun::star::sdbc::SQLWarning& _rExcept, vcl::Window* _pParent)
152 : {
153 0 : displayException(makeAny(_rExcept), _pParent);
154 0 : }
155 :
156 :
157 0 : void displayException(const ::com::sun::star::sdb::SQLContext& _rExcept, vcl::Window* _pParent)
158 : {
159 0 : displayException(makeAny(_rExcept), _pParent);
160 0 : }
161 :
162 :
163 0 : void displayException(const ::com::sun::star::sdb::SQLErrorEvent& _rEvent, vcl::Window* _pParent)
164 : {
165 0 : displayException(_rEvent.Reason, _pParent);
166 0 : }
167 :
168 :
169 317 : sal_Int32 getElementPos(const Reference< ::com::sun::star::container::XIndexAccess>& xCont, const Reference< XInterface >& xElement)
170 : {
171 317 : sal_Int32 nIndex = -1;
172 317 : if (!xCont.is())
173 0 : return nIndex;
174 :
175 :
176 317 : Reference< XInterface > xNormalized( xElement, UNO_QUERY );
177 : DBG_ASSERT( xNormalized.is(), "getElementPos: invalid element!" );
178 317 : if ( xNormalized.is() )
179 : {
180 : // find child position
181 317 : nIndex = xCont->getCount();
182 1718 : while (nIndex--)
183 : {
184 : try
185 : {
186 1401 : Reference< XInterface > xCurrent(xCont->getByIndex( nIndex ),UNO_QUERY);
187 : DBG_ASSERT( xCurrent.get() == Reference< XInterface >( xCurrent, UNO_QUERY ).get(),
188 : "getElementPos: container element not normalized!" );
189 1401 : if ( xNormalized.get() == xCurrent.get() )
190 317 : break;
191 : }
192 0 : catch(Exception&)
193 : {
194 : OSL_FAIL( "getElementPos: caught an exception!" );
195 : }
196 :
197 : }
198 : }
199 317 : return nIndex;
200 : }
201 :
202 :
203 0 : OUString getLabelName(const Reference< ::com::sun::star::beans::XPropertySet>& xControlModel)
204 : {
205 0 : if (!xControlModel.is())
206 0 : return OUString();
207 :
208 0 : if (::comphelper::hasProperty(FM_PROP_CONTROLLABEL, xControlModel))
209 : {
210 0 : Reference< ::com::sun::star::beans::XPropertySet> xLabelSet;
211 0 : xControlModel->getPropertyValue(FM_PROP_CONTROLLABEL) >>= xLabelSet;
212 0 : if (xLabelSet.is() && ::comphelper::hasProperty(FM_PROP_LABEL, xLabelSet))
213 : {
214 0 : Any aLabel( xLabelSet->getPropertyValue(FM_PROP_LABEL) );
215 0 : if ((aLabel.getValueTypeClass() == TypeClass_STRING) && !::comphelper::getString(aLabel).isEmpty())
216 0 : return ::comphelper::getString(aLabel);
217 0 : }
218 : }
219 :
220 0 : return ::comphelper::getString(xControlModel->getPropertyValue(FM_PROP_CONTROLSOURCE));
221 : }
222 :
223 :
224 : // = CursorWrapper
225 :
226 1 : CursorWrapper::CursorWrapper(const Reference< ::com::sun::star::sdbc::XRowSet>& _rxCursor, bool bUseCloned)
227 : {
228 1 : ImplConstruct(Reference< ::com::sun::star::sdbc::XResultSet>(_rxCursor, UNO_QUERY), bUseCloned);
229 1 : }
230 :
231 :
232 1 : CursorWrapper::CursorWrapper(const Reference< ::com::sun::star::sdbc::XResultSet>& _rxCursor, bool bUseCloned)
233 : {
234 1 : ImplConstruct(_rxCursor, bUseCloned);
235 1 : }
236 :
237 :
238 2 : void CursorWrapper::ImplConstruct(const Reference< ::com::sun::star::sdbc::XResultSet>& _rxCursor, bool bUseCloned)
239 : {
240 2 : if (bUseCloned)
241 : {
242 0 : Reference< ::com::sun::star::sdb::XResultSetAccess> xAccess(_rxCursor, UNO_QUERY);
243 : try
244 : {
245 0 : m_xMoveOperations = xAccess.is() ? xAccess->createResultSet() : Reference< ::com::sun::star::sdbc::XResultSet>();
246 : }
247 0 : catch(Exception&)
248 : {
249 0 : }
250 : }
251 : else
252 2 : m_xMoveOperations = _rxCursor;
253 :
254 2 : m_xBookmarkOperations.set(m_xMoveOperations, css::uno::UNO_QUERY);
255 2 : m_xColumnsSupplier.set(m_xMoveOperations, css::uno::UNO_QUERY);
256 2 : m_xPropertyAccess.set(m_xMoveOperations, css::uno::UNO_QUERY);
257 :
258 2 : if ( !m_xMoveOperations.is() || !m_xBookmarkOperations.is() || !m_xColumnsSupplier.is() || !m_xPropertyAccess.is() )
259 : { // all or nothing !!
260 0 : m_xMoveOperations = NULL;
261 0 : m_xBookmarkOperations = NULL;
262 0 : m_xColumnsSupplier = NULL;
263 : }
264 : else
265 2 : m_xGeneric = m_xMoveOperations.get();
266 2 : }
267 :
268 :
269 0 : const CursorWrapper& CursorWrapper::operator=(const Reference< ::com::sun::star::sdbc::XRowSet>& _rxCursor)
270 : {
271 0 : m_xMoveOperations = Reference< ::com::sun::star::sdbc::XResultSet>(_rxCursor, UNO_QUERY);
272 0 : m_xBookmarkOperations = Reference< ::com::sun::star::sdbcx::XRowLocate>(_rxCursor, UNO_QUERY);
273 0 : m_xColumnsSupplier = Reference< ::com::sun::star::sdbcx::XColumnsSupplier>(_rxCursor, UNO_QUERY);
274 0 : if (!m_xMoveOperations.is() || !m_xBookmarkOperations.is() || !m_xColumnsSupplier.is())
275 : { // all or nothing !!
276 0 : m_xMoveOperations = NULL;
277 0 : m_xBookmarkOperations = NULL;
278 0 : m_xColumnsSupplier = NULL;
279 : }
280 0 : return *this;
281 : }
282 :
283 :
284 1 : FmXDisposeListener::~FmXDisposeListener()
285 : {
286 1 : setAdapter(NULL);
287 1 : }
288 :
289 :
290 3 : void FmXDisposeListener::setAdapter(FmXDisposeMultiplexer* pAdapter)
291 : {
292 3 : if (m_pAdapter)
293 : {
294 1 : ::osl::MutexGuard aGuard(m_rMutex);
295 1 : m_pAdapter->release();
296 1 : m_pAdapter = NULL;
297 : }
298 :
299 3 : if (pAdapter)
300 : {
301 1 : ::osl::MutexGuard aGuard(m_rMutex);
302 1 : m_pAdapter = pAdapter;
303 1 : m_pAdapter->acquire();
304 : }
305 3 : }
306 :
307 :
308 :
309 1 : FmXDisposeMultiplexer::FmXDisposeMultiplexer(FmXDisposeListener* _pListener, const Reference< ::com::sun::star::lang::XComponent>& _rxObject, sal_Int16 _nId)
310 : :m_xObject(_rxObject)
311 : ,m_pListener(_pListener)
312 1 : ,m_nId(_nId)
313 : {
314 1 : m_pListener->setAdapter(this);
315 :
316 1 : if (m_xObject.is())
317 1 : m_xObject->addEventListener(this);
318 1 : }
319 :
320 :
321 2 : FmXDisposeMultiplexer::~FmXDisposeMultiplexer()
322 : {
323 2 : }
324 :
325 : // ::com::sun::star::lang::XEventListener
326 :
327 1 : void FmXDisposeMultiplexer::disposing(const ::com::sun::star::lang::EventObject& _Source) throw( RuntimeException, std::exception )
328 : {
329 1 : Reference< ::com::sun::star::lang::XEventListener> xPreventDelete(this);
330 :
331 1 : if (m_pListener)
332 : {
333 1 : m_pListener->disposing(_Source, m_nId);
334 1 : m_pListener->setAdapter(NULL);
335 1 : m_pListener = NULL;
336 : }
337 1 : m_xObject = NULL;
338 1 : }
339 :
340 :
341 1 : void FmXDisposeMultiplexer::dispose()
342 : {
343 1 : if (m_xObject.is())
344 : {
345 0 : Reference< ::com::sun::star::lang::XEventListener> xPreventDelete(this);
346 :
347 0 : m_xObject->removeEventListener(this);
348 0 : m_xObject = NULL;
349 :
350 0 : m_pListener->setAdapter(NULL);
351 0 : m_pListener = NULL;
352 : }
353 1 : }
354 :
355 :
356 :
357 0 : sal_Int16 getControlTypeByObject(const Reference< ::com::sun::star::lang::XServiceInfo>& _rxObject)
358 : {
359 : // ask for the persistent service name
360 0 : Reference< ::com::sun::star::io::XPersistObject> xPersistence(_rxObject, UNO_QUERY);
361 : DBG_ASSERT(xPersistence.is(), "::getControlTypeByObject : argument shold be an ::com::sun::star::io::XPersistObject !");
362 0 : if (!xPersistence.is())
363 0 : return OBJ_FM_CONTROL;
364 :
365 0 : OUString sPersistentServiceName = xPersistence->getServiceName();
366 0 : if (sPersistentServiceName == FM_COMPONENT_EDIT) // 5.0-Name
367 : {
368 : // may be a simple edit field or a formatted field, dependent of the supported services
369 0 : if (_rxObject->supportsService(FM_SUN_COMPONENT_FORMATTEDFIELD))
370 0 : return OBJ_FM_FORMATTEDFIELD;
371 0 : return OBJ_FM_EDIT;
372 : }
373 0 : if (sPersistentServiceName == FM_COMPONENT_TEXTFIELD)
374 0 : return OBJ_FM_EDIT;
375 0 : if (sPersistentServiceName == FM_COMPONENT_COMMANDBUTTON)
376 0 : return OBJ_FM_BUTTON;
377 0 : if (sPersistentServiceName == FM_COMPONENT_FIXEDTEXT)
378 0 : return OBJ_FM_FIXEDTEXT;
379 0 : if (sPersistentServiceName == FM_COMPONENT_LISTBOX)
380 0 : return OBJ_FM_LISTBOX;
381 0 : if (sPersistentServiceName == FM_COMPONENT_CHECKBOX)
382 0 : return OBJ_FM_CHECKBOX;
383 0 : if (sPersistentServiceName == FM_COMPONENT_RADIOBUTTON)
384 0 : return OBJ_FM_RADIOBUTTON;
385 0 : if (sPersistentServiceName == FM_COMPONENT_GROUPBOX)
386 0 : return OBJ_FM_GROUPBOX;
387 0 : if (sPersistentServiceName == FM_COMPONENT_COMBOBOX)
388 0 : return OBJ_FM_COMBOBOX;
389 0 : if (sPersistentServiceName == FM_COMPONENT_GRID) // 5.0-Name
390 0 : return OBJ_FM_GRID;
391 0 : if (sPersistentServiceName == FM_COMPONENT_GRIDCONTROL)
392 0 : return OBJ_FM_GRID;
393 0 : if (sPersistentServiceName == FM_COMPONENT_IMAGEBUTTON)
394 0 : return OBJ_FM_IMAGEBUTTON;
395 0 : if (sPersistentServiceName == FM_COMPONENT_FILECONTROL)
396 0 : return OBJ_FM_FILECONTROL;
397 0 : if (sPersistentServiceName == FM_COMPONENT_DATEFIELD)
398 0 : return OBJ_FM_DATEFIELD;
399 0 : if (sPersistentServiceName == FM_COMPONENT_TIMEFIELD)
400 0 : return OBJ_FM_TIMEFIELD;
401 0 : if (sPersistentServiceName == FM_COMPONENT_NUMERICFIELD)
402 0 : return OBJ_FM_NUMERICFIELD;
403 0 : if (sPersistentServiceName == FM_COMPONENT_CURRENCYFIELD)
404 0 : return OBJ_FM_CURRENCYFIELD;
405 0 : if (sPersistentServiceName == FM_COMPONENT_PATTERNFIELD)
406 0 : return OBJ_FM_PATTERNFIELD;
407 0 : if (sPersistentServiceName == FM_COMPONENT_HIDDEN) // 5.0-Name
408 0 : return OBJ_FM_HIDDEN;
409 0 : if (sPersistentServiceName == FM_COMPONENT_HIDDENCONTROL)
410 0 : return OBJ_FM_HIDDEN;
411 0 : if (sPersistentServiceName == FM_COMPONENT_IMAGECONTROL)
412 0 : return OBJ_FM_IMAGECONTROL;
413 0 : if (sPersistentServiceName == FM_COMPONENT_FORMATTEDFIELD)
414 : {
415 : OSL_FAIL("::getControlTypeByObject : suspicious persistent service name (formatted field) !");
416 : // objects with that service name should exist as they aren't compatible with older versions
417 0 : return OBJ_FM_FORMATTEDFIELD;
418 : }
419 0 : if ( sPersistentServiceName == FM_SUN_COMPONENT_SCROLLBAR )
420 0 : return OBJ_FM_SCROLLBAR;
421 0 : if ( sPersistentServiceName == FM_SUN_COMPONENT_SPINBUTTON )
422 0 : return OBJ_FM_SPINBUTTON;
423 0 : if ( sPersistentServiceName == FM_SUN_COMPONENT_NAVIGATIONBAR )
424 0 : return OBJ_FM_NAVIGATIONBAR;
425 :
426 : OSL_FAIL("::getControlTypeByObject : unknown object type !");
427 0 : return OBJ_FM_CONTROL;
428 : }
429 :
430 :
431 0 : bool isRowSetAlive(const Reference< XInterface >& _rxRowSet)
432 : {
433 0 : bool bIsAlive = false;
434 0 : Reference< ::com::sun::star::sdbcx::XColumnsSupplier> xSupplyCols(_rxRowSet, UNO_QUERY);
435 0 : Reference< ::com::sun::star::container::XIndexAccess> xCols;
436 0 : if (xSupplyCols.is())
437 0 : xCols = Reference< ::com::sun::star::container::XIndexAccess>(xSupplyCols->getColumns(), UNO_QUERY);
438 0 : if (xCols.is() && (xCols->getCount() > 0))
439 0 : bIsAlive = true;
440 :
441 0 : return bIsAlive;
442 : }
443 :
444 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|