Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / dbaccess / source / ui / misc / dbsubcomponentcontroller.cxx
blob0614726f75bf7790d6049f331e61feba0c5a3ce9
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 <comphelper/interfacecontainer2.hxx>
45 #include <rtl/ustrbuf.hxx>
46 #include <toolkit/helper/vclunohelper.hxx>
47 #include <tools/debug.hxx>
48 #include <tools/diagnose_ex.h>
49 #include <vcl/layout.hxx>
51 namespace dbaui
54 using ::com::sun::star::uno::Any;
55 using ::com::sun::star::uno::Reference;
56 using ::com::sun::star::beans::XPropertySet;
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 explicit 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 ::comphelper::OInterfaceContainerHelper2
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< XNumberFormatter > m_xFormatter; // a number formatter working with the connection's NumberFormatsSupplier
139 sal_Int32 m_nDocStartNumber;
140 bool m_bSuspended; // is true when the controller was already suspended
141 bool m_bEditable; // is the control readonly or not
142 bool m_bModified; // is the data modified
143 bool m_bNotAttached;
145 explicit DBSubComponentController_Impl(osl::Mutex& i_rMutex)
146 :m_aDocScriptSupport()
147 ,m_aModifyListeners( i_rMutex )
148 ,m_nDocStartNumber(0)
149 ,m_bSuspended( false )
150 ,m_bEditable(true)
151 ,m_bModified(false)
152 ,m_bNotAttached(true)
156 bool documentHasScriptSupport() const
158 OSL_PRECOND( !!m_aDocScriptSupport,
159 "DBSubComponentController_Impl::documentHasScriptSupport: not completely initialized, yet - don't know!?" );
160 return !!m_aDocScriptSupport && *m_aDocScriptSupport;
163 void setDocumentScriptSupport( const bool _bSupport )
165 OSL_PRECOND( !m_aDocScriptSupport,
166 "DBSubComponentController_Impl::setDocumentScriptSupport: already initialized!" );
167 m_aDocScriptSupport = ::boost::optional< bool >( _bSupport );
171 // DBSubComponentController
172 DBSubComponentController::DBSubComponentController(const Reference< XComponentContext >& _rxORB)
173 :DBSubComponentController_Base( _rxORB )
174 ,m_pImpl( new DBSubComponentController_Impl( getMutex() ) )
178 DBSubComponentController::~DBSubComponentController()
182 void DBSubComponentController::impl_initialize()
184 OGenericUnoController::impl_initialize();
186 const ::comphelper::NamedValueCollection& rArguments( getInitParams() );
188 Reference< XConnection > xConnection;
189 xConnection = rArguments.getOrDefault( PROPERTY_ACTIVE_CONNECTION, xConnection );
191 if ( !xConnection.is() )
192 ::dbtools::isEmbeddedInDatabase( getModel(), xConnection );
194 if ( xConnection.is() )
195 initializeConnection( xConnection );
197 bool bShowError = true;
198 if ( !isConnected() )
200 reconnect( false );
201 bShowError = false;
203 if ( !isConnected() )
205 if ( bShowError )
206 connectionLostMessage();
207 throw IllegalArgumentException();
211 Any SAL_CALL DBSubComponentController::queryInterface(const Type& _rType) throw (RuntimeException, std::exception)
213 if ( _rType.equals( cppu::UnoType<XScriptInvocationContext>::get() ) )
215 if ( m_pImpl->documentHasScriptSupport() )
216 return makeAny( Reference< XScriptInvocationContext >( this ) );
217 return Any();
220 return DBSubComponentController_Base::queryInterface( _rType );
223 Sequence< Type > SAL_CALL DBSubComponentController::getTypes( ) throw (RuntimeException, std::exception)
225 Sequence< Type > aTypes( DBSubComponentController_Base::getTypes() );
226 if ( !m_pImpl->documentHasScriptSupport() )
228 Sequence< Type > aStrippedTypes( aTypes.getLength() - 1 );
229 ::std::remove_copy_if(
230 aTypes.getConstArray(),
231 aTypes.getConstArray() + aTypes.getLength(),
232 aStrippedTypes.getArray(),
233 ::std::bind2nd( ::std::equal_to< Type >(), cppu::UnoType<XScriptInvocationContext>::get() )
235 aTypes = aStrippedTypes;
237 return aTypes;
240 void DBSubComponentController::initializeConnection( const Reference< XConnection >& _rxForeignConn )
242 DBG_ASSERT( !isConnected(), "DBSubComponentController::initializeConnection: not to be called when already connected!" );
243 // usually this gets called from within initialize of derived classes ...
244 if ( isConnected() )
245 disconnect();
247 m_pImpl->m_xConnection.reset( _rxForeignConn, SharedConnection::NoTakeOwnership );
248 m_pImpl->m_aSdbMetaData.reset( m_pImpl->m_xConnection );
249 startConnectionListening( m_pImpl->m_xConnection );
251 // get the data source the connection belongs to
254 // determine our data source
255 OSL_PRECOND( !m_pImpl->m_aDataSource.is(), "DBSubComponentController::initializeConnection: already a data source in this phase?" );
257 Reference< XChild > xConnAsChild( m_pImpl->m_xConnection, UNO_QUERY );
258 Reference< XDataSource > xDS;
259 if ( xConnAsChild.is() )
260 xDS.set( xConnAsChild->getParent(), UNO_QUERY );
262 // (take the indirection through XDataSource to ensure we have a correct object ....)
263 m_pImpl->m_aDataSource = DataSourceHolder(xDS);
265 SAL_WARN_IF( !m_pImpl->m_aDataSource.is(), "dbaccess.ui", "DBSubComponentController::initializeConnection: unable to obtain the data source object!" );
267 if ( m_pImpl->m_bNotAttached )
269 Reference< XUntitledNumbers > xUntitledProvider( getDatabaseDocument(), UNO_QUERY );
270 m_pImpl->m_nDocStartNumber = 1;
271 if ( xUntitledProvider.is() )
272 m_pImpl->m_nDocStartNumber = xUntitledProvider->leaseNumber( static_cast< XWeak* >( this ) );
275 // determine the availability of script support in our document. Our own XScriptInvocationContext
276 // interface depends on this
277 m_pImpl->setDocumentScriptSupport( Reference< XEmbeddedScripts >( getDatabaseDocument(), UNO_QUERY ).is() );
279 // get a number formatter
280 Reference< XPropertySet > xDataSourceProps( m_pImpl->m_aDataSource.getDataSourceProps(), UNO_SET_THROW );
281 xDataSourceProps->getPropertyValue( PROPERTY_NAME ) >>= m_pImpl->m_sDataSourceName;
282 DBG_ASSERT( !m_pImpl->m_sDataSourceName.isEmpty(), "DBSubComponentController::initializeConnection: invalid data source name!" );
283 Reference< XNumberFormatsSupplier> xSupplier = ::dbtools::getNumberFormats(m_pImpl->m_xConnection);
284 if(xSupplier.is())
286 m_pImpl->m_xFormatter.set(NumberFormatter::create(getORB()), UNO_QUERY_THROW);
287 m_pImpl->m_xFormatter->attachNumberFormatsSupplier(xSupplier);
289 OSL_ENSURE(m_pImpl->m_xFormatter.is(),"No NumberFormatter!");
291 catch( const Exception& )
293 DBG_UNHANDLED_EXCEPTION();
297 void DBSubComponentController::reconnect( bool _bUI )
299 OSL_ENSURE(!m_pImpl->m_bSuspended, "Cannot reconnect while suspended!");
301 stopConnectionListening( m_pImpl->m_xConnection );
302 m_pImpl->m_aSdbMetaData.reset( nullptr );
303 m_pImpl->m_xConnection.clear();
305 // reconnect
306 bool bReConnect = true;
307 if ( _bUI )
309 ScopedVclPtrInstance< MessageDialog > aQuery(getView(), ModuleRes(STR_QUERY_CONNECTION_LOST), VCL_MESSAGE_QUESTION, VCL_BUTTONS_YES_NO);
310 bReConnect = ( RET_YES == aQuery->Execute() );
313 // now really reconnect ...
314 if ( bReConnect )
316 m_pImpl->m_xConnection.reset( connect( m_pImpl->m_aDataSource.getDataSource(), nullptr ), SharedConnection::TakeOwnership );
317 m_pImpl->m_aSdbMetaData.reset( m_pImpl->m_xConnection );
320 // invalidate all slots
321 InvalidateAll();
324 void DBSubComponentController::disconnect()
326 stopConnectionListening(m_pImpl->m_xConnection);
327 m_pImpl->m_aSdbMetaData.reset( nullptr );
328 m_pImpl->m_xConnection.clear();
330 InvalidateAll();
333 void DBSubComponentController::losingConnection()
335 // our connection was disposed so we need a new one
336 reconnect( true );
337 InvalidateAll();
340 void SAL_CALL DBSubComponentController::disposing()
342 DBSubComponentController_Base::disposing();
344 disconnect();
346 attachFrame( Reference < XFrame >() );
348 m_pImpl->m_aDataSource.clear();
351 void SAL_CALL DBSubComponentController::disposing(const EventObject& _rSource) throw( RuntimeException, std::exception )
353 if ( _rSource.Source == getConnection() )
355 if ( !m_pImpl->m_bSuspended // when already suspended then we don't have to reconnect
356 && !getBroadcastHelper().bInDispose
357 && !getBroadcastHelper().bDisposed
358 && isConnected()
361 losingConnection();
363 else
365 m_pImpl->m_xConnection.reset( m_pImpl->m_xConnection, SharedConnection::NoTakeOwnership );
366 // this prevents the "disposeComponent" call in disconnect
367 disconnect();
370 else
371 DBSubComponentController_Base::disposing( _rSource );
374 void DBSubComponentController::appendError( const OUString& _rErrorMessage )
376 m_pImpl->m_aCurrentError.append( ::dbtools::SQLExceptionInfo::TYPE::SQLException, _rErrorMessage,
377 getStandardSQLState( ::dbtools::StandardSQLState::GENERAL_ERROR ),
378 1000 );
380 void DBSubComponentController::clearError()
382 m_pImpl->m_aCurrentError = ::dbtools::SQLExceptionInfo();
385 bool DBSubComponentController::hasError() const
387 return m_pImpl->m_aCurrentError.isValid();
390 const ::dbtools::SQLExceptionInfo& DBSubComponentController::getError() const
392 return m_pImpl->m_aCurrentError;
395 void DBSubComponentController::displayError()
397 showError( m_pImpl->m_aCurrentError );
400 sal_Bool SAL_CALL DBSubComponentController::suspend(sal_Bool bSuspend) throw( RuntimeException, std::exception )
402 m_pImpl->m_bSuspended = bSuspend;
403 if ( !bSuspend && !isConnected() )
404 reconnect(true);
406 return true;
409 sal_Bool SAL_CALL DBSubComponentController::attachModel( const Reference< XModel > & _rxModel) throw( RuntimeException, std::exception )
411 if ( !_rxModel.is() )
412 return false;
413 if ( !DBSubComponentController_Base::attachModel( _rxModel ) )
414 return false;
416 m_pImpl->m_bNotAttached = false;
417 if ( m_pImpl->m_nDocStartNumber == 1 )
418 releaseNumberForComponent();
420 Reference< XUntitledNumbers > xUntitledProvider( _rxModel, UNO_QUERY );
421 m_pImpl->m_nDocStartNumber = 1;
422 if ( xUntitledProvider.is() )
423 m_pImpl->m_nDocStartNumber = xUntitledProvider->leaseNumber( static_cast< XWeak* >( this ) );
425 return true;
428 void DBSubComponentController::Execute(sal_uInt16 _nId, const Sequence< PropertyValue >& _rArgs)
430 if ( _nId == ID_BROWSER_CLOSE )
432 closeTask();
433 return;
436 DBSubComponentController_Base::Execute( _nId, _rArgs );
437 InvalidateFeature( _nId );
440 OUString DBSubComponentController::getDataSourceName() const
442 OUString sName;
443 Reference< XPropertySet > xDataSourceProps( m_pImpl->m_aDataSource.getDataSourceProps() );
444 if ( xDataSourceProps.is() )
445 xDataSourceProps->getPropertyValue(PROPERTY_NAME) >>= sName;
446 return sName;
448 void DBSubComponentController::connectionLostMessage() const
450 OUString aMessage(ModuleRes(RID_STR_CONNECTION_LOST));
451 Reference< XWindow > xWindow = getTopMostContainerWindow();
452 vcl::Window* pWin = nullptr;
453 if ( xWindow.is() )
454 pWin = VCLUnoHelper::GetWindow(xWindow);
455 if ( !pWin )
456 pWin = getView()->Window::GetParent();
458 ScopedVclPtrInstance<MessageDialog>(pWin, aMessage, VCL_MESSAGE_INFO)->Execute();
460 const Reference< XConnection >& DBSubComponentController::getConnection() const
462 return m_pImpl->m_xConnection;
465 bool DBSubComponentController::isReadOnly() const
467 return !m_pImpl->m_bEditable;
470 bool DBSubComponentController::isEditable() const
472 return m_pImpl->m_bEditable;
475 void DBSubComponentController::setEditable(bool _bEditable)
477 m_pImpl->m_bEditable = _bEditable;
480 const ::dbtools::DatabaseMetaData& DBSubComponentController::getSdbMetaData() const
482 return m_pImpl->m_aSdbMetaData;
485 bool DBSubComponentController::isConnected() const
487 return m_pImpl->m_xConnection.is();
490 Reference< XDatabaseMetaData > DBSubComponentController::getMetaData( ) const
492 Reference< XDatabaseMetaData > xMeta;
495 if ( isConnected() )
496 xMeta.set( m_pImpl->m_xConnection->getMetaData(), UNO_SET_THROW );
498 catch( const Exception& )
500 DBG_UNHANDLED_EXCEPTION();
502 return xMeta;
505 const Reference< XPropertySet >& DBSubComponentController::getDataSource() const
507 return m_pImpl->m_aDataSource.getDataSourceProps();
510 bool DBSubComponentController::haveDataSource() const
512 return m_pImpl->m_aDataSource.is();
515 Reference< XModel > DBSubComponentController::getDatabaseDocument() const
517 return Reference< XModel >( m_pImpl->m_aDataSource.getDatabaseDocument(), UNO_QUERY );
520 Reference< XNumberFormatter > DBSubComponentController::getNumberFormatter() const
522 return m_pImpl->m_xFormatter;
525 Reference< XModel > DBSubComponentController::getPrivateModel() const
527 return getDatabaseDocument();
529 // XTitle
530 OUString SAL_CALL DBSubComponentController::getTitle()
531 throw (RuntimeException, std::exception)
533 ::osl::MutexGuard aGuard( getMutex() );
534 if ( m_bExternalTitle )
535 return impl_getTitleHelper_throw()->getTitle ();
537 OUStringBuffer sTitle;
538 Reference< XTitle > xTitle(getPrivateModel(),UNO_QUERY);
539 if ( xTitle.is() )
541 sTitle.append( xTitle->getTitle() );
542 sTitle.append(" : ");
544 sTitle.append( getPrivateTitle() );
545 return sTitle.makeStringAndClear();
548 sal_Int32 DBSubComponentController::getCurrentStartNumber() const
550 return m_pImpl->m_nDocStartNumber;
553 Reference< XEmbeddedScripts > SAL_CALL DBSubComponentController::getScriptContainer() throw (RuntimeException, std::exception)
555 ::osl::MutexGuard aGuard( getMutex() );
556 if ( !m_pImpl->documentHasScriptSupport() )
557 return nullptr;
559 return Reference< XEmbeddedScripts >( getDatabaseDocument(), UNO_QUERY_THROW );
562 void SAL_CALL DBSubComponentController::addModifyListener( const Reference< XModifyListener >& i_Listener ) throw (RuntimeException, std::exception)
564 ::osl::MutexGuard aGuard( getMutex() );
565 m_pImpl->m_aModifyListeners.addInterface( i_Listener );
568 void SAL_CALL DBSubComponentController::removeModifyListener( const Reference< XModifyListener >& i_Listener ) throw (RuntimeException, std::exception)
570 ::osl::MutexGuard aGuard( getMutex() );
571 m_pImpl->m_aModifyListeners.removeInterface( i_Listener );
574 sal_Bool SAL_CALL DBSubComponentController::isModified( ) throw (RuntimeException, std::exception)
576 ::osl::MutexGuard aGuard( getMutex() );
577 return impl_isModified();
580 void SAL_CALL DBSubComponentController::setModified( sal_Bool i_bModified ) throw (PropertyVetoException, RuntimeException, std::exception)
582 ::osl::ClearableMutexGuard aGuard( getMutex() );
584 if ( m_pImpl->m_bModified == bool(i_bModified) )
585 return;
587 m_pImpl->m_bModified = i_bModified;
588 impl_onModifyChanged();
590 EventObject aEvent( *this );
591 aGuard.clear();
592 m_pImpl->m_aModifyListeners.notifyEach( &XModifyListener::modified, aEvent );
595 bool DBSubComponentController::impl_isModified() const
597 return m_pImpl->m_bModified;
600 void DBSubComponentController::impl_onModifyChanged()
602 InvalidateFeature( ID_BROWSER_SAVEDOC );
603 if ( isFeatureSupported( ID_BROWSER_SAVEASDOC ) )
604 InvalidateFeature( ID_BROWSER_SAVEASDOC );
607 } // namespace dbaui
609 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */