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