Bump version to 5.0-14
[LibreOffice.git] / dbaccess / source / ui / misc / dbsubcomponentcontroller.cxx
blob4665a9a882af23687d18f64cfd6e751f3c3668c7
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 .
20 #include "browserids.hxx"
21 #include "commontypes.hxx"
22 #include <dbaccess/dataview.hxx>
23 #include "dbu_misc.hrc"
24 #include "dbustrings.hrc"
25 #include "moduledbu.hxx"
26 #include <dbaccess/dbsubcomponentcontroller.hxx>
28 #include <com/sun/star/frame/XUntitledNumbers.hpp>
29 #include <com/sun/star/beans/PropertyAttribute.hpp>
30 #include <com/sun/star/container/XChild.hpp>
31 #include <com/sun/star/container/XNameAccess.hpp>
32 #include <com/sun/star/sdb/XDocumentDataSource.hpp>
33 #include <com/sun/star/sdb/XOfficeDatabaseDocument.hpp>
34 #include <com/sun/star/sdbc/XDataSource.hpp>
35 #include <com/sun/star/util/NumberFormatter.hpp>
36 #include <com/sun/star/lang/IllegalArgumentException.hpp>
38 #include <comphelper/processfactory.hxx>
39 #include <comphelper/sequence.hxx>
40 #include <comphelper/types.hxx>
41 #include <connectivity/dbexception.hxx>
42 #include <connectivity/dbtools.hxx>
43 #include <cppuhelper/typeprovider.hxx>
44 #include <rtl/ustrbuf.hxx>
45 #include <toolkit/helper/vclunohelper.hxx>
46 #include <tools/debug.hxx>
47 #include <tools/diagnose_ex.h>
48 #include <vcl/layout.hxx>
50 namespace dbaui
53 using ::com::sun::star::uno::Any;
54 using ::com::sun::star::uno::Reference;
55 using ::com::sun::star::beans::XPropertySet;
56 using ::com::sun::star::lang::XMultiServiceFactory;
57 using ::com::sun::star::uno::RuntimeException;
58 using ::com::sun::star::uno::Sequence;
59 using ::com::sun::star::uno::Type;
60 using ::com::sun::star::uno::XComponentContext;
61 using ::com::sun::star::sdbc::XConnection;
62 using ::com::sun::star::uno::UNO_QUERY;
63 using ::com::sun::star::container::XChild;
64 using ::com::sun::star::sdbc::XDataSource;
65 using ::com::sun::star::util::NumberFormatter;
66 using ::com::sun::star::util::XNumberFormatter;
67 using ::com::sun::star::util::XNumberFormatsSupplier;
68 using ::com::sun::star::frame::XFrame;
69 using ::com::sun::star::uno::Exception;
70 using ::com::sun::star::sdbc::SQLException;
71 using ::com::sun::star::lang::EventObject;
72 using ::com::sun::star::beans::PropertyValue;
73 using ::com::sun::star::frame::XModel;
74 using ::com::sun::star::sdb::XOfficeDatabaseDocument;
75 using ::com::sun::star::awt::XWindow;
76 using ::com::sun::star::sdbc::XDatabaseMetaData;
77 using ::com::sun::star::sdb::XDocumentDataSource;
78 using ::com::sun::star::document::XEmbeddedScripts;
79 using ::com::sun::star::lang::IllegalArgumentException;
80 using ::com::sun::star::uno::UNO_SET_THROW;
81 using ::com::sun::star::uno::UNO_QUERY_THROW;
82 using ::com::sun::star::frame::XUntitledNumbers;
83 using ::com::sun::star::beans::PropertyVetoException;
85 class DataSourceHolder
87 public:
88 DataSourceHolder()
92 DataSourceHolder( const Reference< XDataSource >& _rxDataSource )
94 m_xDataSource = _rxDataSource;
95 Reference< XDocumentDataSource > xDocDS( m_xDataSource, UNO_QUERY );
96 if ( xDocDS.is() )
97 m_xDocument = xDocDS->getDatabaseDocument();
99 m_xDataSourceProps.set( m_xDataSource, UNO_QUERY );
102 const Reference< XDataSource >& getDataSource() const { return m_xDataSource; }
103 const Reference< XPropertySet >& getDataSourceProps() const { return m_xDataSourceProps; }
104 const Reference< XOfficeDatabaseDocument > getDatabaseDocument() const { return m_xDocument; }
106 bool is() const { return m_xDataSource.is(); }
108 void clear()
110 m_xDataSource.clear();
111 m_xDocument.clear();
114 private:
115 Reference< XDataSource > m_xDataSource;
116 Reference< XPropertySet > m_xDataSourceProps;
117 Reference< XOfficeDatabaseDocument > m_xDocument;
120 struct DBSubComponentController_Impl
122 private:
123 ::boost::optional< bool > m_aDocScriptSupport;
125 public:
126 OModuleClient m_aModuleClient;
127 ::dbtools::SQLExceptionInfo m_aCurrentError;
129 ::cppu::OInterfaceContainerHelper
130 m_aModifyListeners;
132 // <properties>
133 SharedConnection m_xConnection;
134 ::dbtools::DatabaseMetaData m_aSdbMetaData;
135 // </properties>
136 OUString m_sDataSourceName; // the data source we're working for
137 DataSourceHolder m_aDataSource;
138 Reference< XModel > m_xDocument;
139 Reference< XNumberFormatter > m_xFormatter; // a number formatter working with the connection's NumberFormatsSupplier
140 sal_Int32 m_nDocStartNumber;
141 bool m_bSuspended; // is true when the controller was already suspended
142 bool m_bEditable; // is the control readonly or not
143 bool m_bModified; // is the data modified
144 bool m_bNotAttached;
146 DBSubComponentController_Impl( ::osl::Mutex& i_rMutex )
147 :m_aDocScriptSupport()
148 ,m_aModifyListeners( i_rMutex )
149 ,m_nDocStartNumber(0)
150 ,m_bSuspended( false )
151 ,m_bEditable(true)
152 ,m_bModified(false)
153 ,m_bNotAttached(true)
157 bool documentHasScriptSupport() const
159 OSL_PRECOND( !!m_aDocScriptSupport,
160 "DBSubComponentController_Impl::documentHasScriptSupport: not completely initialized, yet - don't know!?" );
161 return !!m_aDocScriptSupport && *m_aDocScriptSupport;
164 void setDocumentScriptSupport( const bool _bSupport )
166 OSL_PRECOND( !m_aDocScriptSupport,
167 "DBSubComponentController_Impl::setDocumentScriptSupport: already initialized!" );
168 m_aDocScriptSupport = ::boost::optional< bool >( _bSupport );
172 // DBSubComponentController
173 DBSubComponentController::DBSubComponentController(const Reference< XComponentContext >& _rxORB)
174 :DBSubComponentController_Base( _rxORB )
175 ,m_pImpl( new DBSubComponentController_Impl( getMutex() ) )
179 DBSubComponentController::~DBSubComponentController()
183 void DBSubComponentController::impl_initialize()
185 OGenericUnoController::impl_initialize();
187 const ::comphelper::NamedValueCollection& rArguments( getInitParams() );
189 Reference< XConnection > xConnection;
190 xConnection = rArguments.getOrDefault( OUString(PROPERTY_ACTIVE_CONNECTION), xConnection );
192 if ( !xConnection.is() )
193 ::dbtools::isEmbeddedInDatabase( getModel(), xConnection );
195 if ( xConnection.is() )
196 initializeConnection( xConnection );
198 bool bShowError = true;
199 if ( !isConnected() )
201 reconnect( false );
202 bShowError = false;
204 if ( !isConnected() )
206 if ( bShowError )
207 connectionLostMessage();
208 throw IllegalArgumentException();
212 Any SAL_CALL DBSubComponentController::queryInterface(const Type& _rType) throw (RuntimeException, std::exception)
214 if ( _rType.equals( cppu::UnoType<XScriptInvocationContext>::get() ) )
216 if ( m_pImpl->documentHasScriptSupport() )
217 return makeAny( Reference< XScriptInvocationContext >( this ) );
218 return Any();
221 return DBSubComponentController_Base::queryInterface( _rType );
224 Sequence< Type > SAL_CALL DBSubComponentController::getTypes( ) throw (RuntimeException, std::exception)
226 Sequence< Type > aTypes( DBSubComponentController_Base::getTypes() );
227 if ( !m_pImpl->documentHasScriptSupport() )
229 Sequence< Type > aStrippedTypes( aTypes.getLength() - 1 );
230 ::std::remove_copy_if(
231 aTypes.getConstArray(),
232 aTypes.getConstArray() + aTypes.getLength(),
233 aStrippedTypes.getArray(),
234 ::std::bind2nd( ::std::equal_to< Type >(), cppu::UnoType<XScriptInvocationContext>::get() )
236 aTypes = aStrippedTypes;
238 return aTypes;
241 void DBSubComponentController::initializeConnection( const Reference< XConnection >& _rxForeignConn )
243 DBG_ASSERT( !isConnected(), "DBSubComponentController::initializeConnection: not to be called when already connected!" );
244 // usually this gets called from within initialize of derived classes ...
245 if ( isConnected() )
246 disconnect();
248 m_pImpl->m_xConnection.reset( _rxForeignConn, SharedConnection::NoTakeOwnership );
249 m_pImpl->m_aSdbMetaData.reset( m_pImpl->m_xConnection );
250 startConnectionListening( m_pImpl->m_xConnection );
252 // get the data source the connection belongs to
255 // determine our data source
256 OSL_PRECOND( !m_pImpl->m_aDataSource.is(), "DBSubComponentController::initializeConnection: already a data source in this phase?" );
258 Reference< XChild > xConnAsChild( m_pImpl->m_xConnection, UNO_QUERY );
259 Reference< XDataSource > xDS;
260 if ( xConnAsChild.is() )
261 xDS = Reference< XDataSource >( xConnAsChild->getParent(), UNO_QUERY );
263 // (take the indirection through XDataSource to ensure we have a correct object ....)
264 m_pImpl->m_aDataSource = xDS;
266 SAL_WARN_IF( !m_pImpl->m_aDataSource.is(), "dbaccess.ui", "DBSubComponentController::initializeConnection: unable to obtain the data source object!" );
268 if ( m_pImpl->m_bNotAttached )
270 Reference< XUntitledNumbers > xUntitledProvider( getDatabaseDocument(), UNO_QUERY );
271 m_pImpl->m_nDocStartNumber = 1;
272 if ( xUntitledProvider.is() )
273 m_pImpl->m_nDocStartNumber = xUntitledProvider->leaseNumber( static_cast< XWeak* >( this ) );
276 // determine the availability of script support in our document. Our own XScriptInvocationContext
277 // interface depends on this
278 m_pImpl->setDocumentScriptSupport( Reference< XEmbeddedScripts >( getDatabaseDocument(), UNO_QUERY ).is() );
280 // get a number formatter
281 Reference< XPropertySet > xDataSourceProps( m_pImpl->m_aDataSource.getDataSourceProps(), UNO_SET_THROW );
282 xDataSourceProps->getPropertyValue( PROPERTY_NAME ) >>= m_pImpl->m_sDataSourceName;
283 DBG_ASSERT( !m_pImpl->m_sDataSourceName.isEmpty(), "DBSubComponentController::initializeConnection: invalid data source name!" );
284 Reference< XNumberFormatsSupplier> xSupplier = ::dbtools::getNumberFormats(m_pImpl->m_xConnection);
285 if(xSupplier.is())
287 m_pImpl->m_xFormatter = Reference< XNumberFormatter >(
288 NumberFormatter::create(getORB()), UNO_QUERY_THROW);
289 m_pImpl->m_xFormatter->attachNumberFormatsSupplier(xSupplier);
291 OSL_ENSURE(m_pImpl->m_xFormatter.is(),"No NumberFormatter!");
293 catch( const Exception& )
295 DBG_UNHANDLED_EXCEPTION();
299 void DBSubComponentController::reconnect( bool _bUI )
301 OSL_ENSURE(!m_pImpl->m_bSuspended, "Cannot reconnect while suspended!");
303 stopConnectionListening( m_pImpl->m_xConnection );
304 m_pImpl->m_aSdbMetaData.reset( NULL );
305 m_pImpl->m_xConnection.clear();
307 // reconnect
308 bool bReConnect = true;
309 if ( _bUI )
311 ScopedVclPtrInstance< MessageDialog > aQuery(getView(), ModuleRes(STR_QUERY_CONNECTION_LOST), VCL_MESSAGE_QUESTION, VCL_BUTTONS_YES_NO);
312 bReConnect = ( RET_YES == aQuery->Execute() );
315 // now really reconnect ...
316 if ( bReConnect )
318 m_pImpl->m_xConnection.reset( connect( m_pImpl->m_aDataSource.getDataSource(), NULL ), SharedConnection::TakeOwnership );
319 m_pImpl->m_aSdbMetaData.reset( m_pImpl->m_xConnection );
322 // invalidate all slots
323 InvalidateAll();
326 void DBSubComponentController::disconnect()
328 stopConnectionListening(m_pImpl->m_xConnection);
329 m_pImpl->m_aSdbMetaData.reset( NULL );
330 m_pImpl->m_xConnection.clear();
332 InvalidateAll();
335 void DBSubComponentController::losingConnection()
337 // our connection was disposed so we need a new one
338 reconnect( true );
339 InvalidateAll();
342 void SAL_CALL DBSubComponentController::disposing()
344 DBSubComponentController_Base::disposing();
346 disconnect();
348 attachFrame( Reference < XFrame >() );
350 m_pImpl->m_aDataSource.clear();
353 void SAL_CALL DBSubComponentController::disposing(const EventObject& _rSource) throw( RuntimeException, std::exception )
355 if ( _rSource.Source == getConnection() )
357 if ( !m_pImpl->m_bSuspended // when already suspended then we don't have to reconnect
358 && !getBroadcastHelper().bInDispose
359 && !getBroadcastHelper().bDisposed
360 && isConnected()
363 losingConnection();
365 else
367 m_pImpl->m_xConnection.reset( m_pImpl->m_xConnection, SharedConnection::NoTakeOwnership );
368 // this prevents the "disposeComponent" call in disconnect
369 disconnect();
372 else
373 DBSubComponentController_Base::disposing( _rSource );
376 void DBSubComponentController::appendError( const OUString& _rErrorMessage, const ::dbtools::StandardSQLState _eSQLState,
377 const sal_Int32 _nErrorCode )
379 m_pImpl->m_aCurrentError.append( ::dbtools::SQLExceptionInfo::SQL_EXCEPTION, _rErrorMessage, getStandardSQLState( _eSQLState ),
380 _nErrorCode );
382 void DBSubComponentController::clearError()
384 m_pImpl->m_aCurrentError = ::dbtools::SQLExceptionInfo();
387 bool DBSubComponentController::hasError() const
389 return m_pImpl->m_aCurrentError.isValid();
392 const ::dbtools::SQLExceptionInfo& DBSubComponentController::getError() const
394 return m_pImpl->m_aCurrentError;
397 void DBSubComponentController::displayError()
399 showError( m_pImpl->m_aCurrentError );
402 sal_Bool SAL_CALL DBSubComponentController::suspend(sal_Bool bSuspend) throw( RuntimeException, std::exception )
404 m_pImpl->m_bSuspended = bSuspend;
405 if ( !bSuspend && !isConnected() )
406 reconnect(true);
408 return sal_True;
411 sal_Bool SAL_CALL DBSubComponentController::attachModel( const Reference< XModel > & _rxModel) throw( RuntimeException, std::exception )
413 if ( !_rxModel.is() )
414 return sal_False;
415 if ( !DBSubComponentController_Base::attachModel( _rxModel ) )
416 return sal_False;
418 m_pImpl->m_bNotAttached = false;
419 if ( m_pImpl->m_nDocStartNumber == 1 )
420 releaseNumberForComponent();
422 Reference< XUntitledNumbers > xUntitledProvider( _rxModel, UNO_QUERY );
423 m_pImpl->m_nDocStartNumber = 1;
424 if ( xUntitledProvider.is() )
425 m_pImpl->m_nDocStartNumber = xUntitledProvider->leaseNumber( static_cast< XWeak* >( this ) );
427 return sal_True;
430 void DBSubComponentController::Execute(sal_uInt16 _nId, const Sequence< PropertyValue >& _rArgs)
432 if ( _nId == ID_BROWSER_CLOSE )
434 closeTask();
435 return;
438 DBSubComponentController_Base::Execute( _nId, _rArgs );
439 InvalidateFeature( _nId );
442 OUString DBSubComponentController::getDataSourceName() const
444 OUString sName;
445 Reference< XPropertySet > xDataSourceProps( m_pImpl->m_aDataSource.getDataSourceProps() );
446 if ( xDataSourceProps.is() )
447 xDataSourceProps->getPropertyValue(PROPERTY_NAME) >>= sName;
448 return sName;
450 void DBSubComponentController::connectionLostMessage() const
452 OUString aMessage(ModuleRes(RID_STR_CONNECTION_LOST));
453 Reference< XWindow > xWindow = getTopMostContainerWindow();
454 vcl::Window* pWin = NULL;
455 if ( xWindow.is() )
456 pWin = VCLUnoHelper::GetWindow(xWindow);
457 if ( !pWin )
458 pWin = getView()->Window::GetParent();
460 ScopedVclPtrInstance<MessageDialog>::Create(pWin, aMessage, VCL_MESSAGE_INFO)->Execute();
462 const Reference< XConnection >& DBSubComponentController::getConnection() const
464 return m_pImpl->m_xConnection;
467 bool DBSubComponentController::isReadOnly() const
469 return !m_pImpl->m_bEditable;
472 bool DBSubComponentController::isEditable() const
474 return m_pImpl->m_bEditable;
477 void DBSubComponentController::setEditable(bool _bEditable)
479 m_pImpl->m_bEditable = _bEditable;
482 const ::dbtools::DatabaseMetaData& DBSubComponentController::getSdbMetaData() const
484 return m_pImpl->m_aSdbMetaData;
487 bool DBSubComponentController::isConnected() const
489 return m_pImpl->m_xConnection.is();
492 Reference< XDatabaseMetaData > DBSubComponentController::getMetaData( ) const
494 Reference< XDatabaseMetaData > xMeta;
497 if ( isConnected() )
498 xMeta.set( m_pImpl->m_xConnection->getMetaData(), UNO_SET_THROW );
500 catch( const Exception& )
502 DBG_UNHANDLED_EXCEPTION();
504 return xMeta;
507 const Reference< XPropertySet >& DBSubComponentController::getDataSource() const
509 return m_pImpl->m_aDataSource.getDataSourceProps();
512 bool DBSubComponentController::haveDataSource() const
514 return m_pImpl->m_aDataSource.is();
517 Reference< XModel > DBSubComponentController::getDatabaseDocument() const
519 return Reference< XModel >( m_pImpl->m_aDataSource.getDatabaseDocument(), UNO_QUERY );
522 Reference< XNumberFormatter > DBSubComponentController::getNumberFormatter() const
524 return m_pImpl->m_xFormatter;
527 Reference< XModel > DBSubComponentController::getPrivateModel() const
529 return getDatabaseDocument();
531 // XTitle
532 OUString SAL_CALL DBSubComponentController::getTitle()
533 throw (RuntimeException, std::exception)
535 ::osl::MutexGuard aGuard( getMutex() );
536 if ( m_bExternalTitle )
537 return impl_getTitleHelper_throw()->getTitle ();
539 OUStringBuffer sTitle;
540 Reference< XTitle > xTitle(getPrivateModel(),UNO_QUERY);
541 if ( xTitle.is() )
543 sTitle.append( xTitle->getTitle() );
544 sTitle.appendAscii(" : ");
546 sTitle.append( getPrivateTitle() );
547 return sTitle.makeStringAndClear();
550 sal_Int32 DBSubComponentController::getCurrentStartNumber() const
552 return m_pImpl->m_nDocStartNumber;
555 Reference< XEmbeddedScripts > SAL_CALL DBSubComponentController::getScriptContainer() throw (RuntimeException, std::exception)
557 ::osl::MutexGuard aGuard( getMutex() );
558 if ( !m_pImpl->documentHasScriptSupport() )
559 return NULL;
561 return Reference< XEmbeddedScripts >( getDatabaseDocument(), UNO_QUERY_THROW );
564 void SAL_CALL DBSubComponentController::addModifyListener( const Reference< XModifyListener >& i_Listener ) throw (RuntimeException, std::exception)
566 ::osl::MutexGuard aGuard( getMutex() );
567 m_pImpl->m_aModifyListeners.addInterface( i_Listener );
570 void SAL_CALL DBSubComponentController::removeModifyListener( const Reference< XModifyListener >& i_Listener ) throw (RuntimeException, std::exception)
572 ::osl::MutexGuard aGuard( getMutex() );
573 m_pImpl->m_aModifyListeners.removeInterface( i_Listener );
576 sal_Bool SAL_CALL DBSubComponentController::isModified( ) throw (RuntimeException, std::exception)
578 ::osl::MutexGuard aGuard( getMutex() );
579 return impl_isModified();
582 void SAL_CALL DBSubComponentController::setModified( sal_Bool i_bModified ) throw (PropertyVetoException, RuntimeException, std::exception)
584 ::osl::ClearableMutexGuard aGuard( getMutex() );
586 if ( m_pImpl->m_bModified == bool(i_bModified) )
587 return;
589 m_pImpl->m_bModified = i_bModified;
590 impl_onModifyChanged();
592 EventObject aEvent( *this );
593 aGuard.clear();
594 m_pImpl->m_aModifyListeners.notifyEach( &XModifyListener::modified, aEvent );
597 bool DBSubComponentController::impl_isModified() const
599 return m_pImpl->m_bModified;
602 void DBSubComponentController::impl_onModifyChanged()
604 InvalidateFeature( ID_BROWSER_SAVEDOC );
605 if ( isFeatureSupported( ID_BROWSER_SAVEASDOC ) )
606 InvalidateFeature( ID_BROWSER_SAVEASDOC );
609 } // namespace dbaui
611 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */