cid#1640468 Dereference after null check
[LibreOffice.git] / dbaccess / source / ui / misc / dbsubcomponentcontroller.cxx
blob0d3676db5d80b247082d798ce012f22023a12204
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 <core_resource.hxx>
23 #include <dbaccess/dataview.hxx>
24 #include <strings.hrc>
25 #include <strings.hxx>
26 #include <dbaccess/dbsubcomponentcontroller.hxx>
28 #include <com/sun/star/frame/XUntitledNumbers.hpp>
29 #include <com/sun/star/container/XChild.hpp>
30 #include <com/sun/star/sdb/XDocumentDataSource.hpp>
31 #include <com/sun/star/sdb/XOfficeDatabaseDocument.hpp>
32 #include <com/sun/star/sdbc/XDataSource.hpp>
33 #include <com/sun/star/util/NumberFormatter.hpp>
34 #include <com/sun/star/lang/IllegalArgumentException.hpp>
36 #include <comphelper/types.hxx>
37 #include <connectivity/dbexception.hxx>
38 #include <connectivity/dbmetadata.hxx>
39 #include <connectivity/dbtools.hxx>
40 #include <comphelper/interfacecontainer3.hxx>
41 #include <rtl/ustrbuf.hxx>
42 #include <sal/log.hxx>
43 #include <toolkit/helper/vclunohelper.hxx>
44 #include <tools/debug.hxx>
45 #include <comphelper/diagnose_ex.hxx>
46 #include <vcl/svapp.hxx>
47 #include <vcl/weld.hxx>
49 namespace dbaui
52 using ::com::sun::star::uno::Any;
53 using ::com::sun::star::uno::Reference;
54 using ::com::sun::star::beans::XPropertySet;
55 using ::com::sun::star::uno::Sequence;
56 using ::com::sun::star::uno::Type;
57 using ::com::sun::star::uno::XComponentContext;
58 using ::com::sun::star::sdbc::XConnection;
59 using ::com::sun::star::uno::UNO_QUERY;
60 using ::com::sun::star::container::XChild;
61 using ::com::sun::star::sdbc::XDataSource;
62 using ::com::sun::star::util::NumberFormatter;
63 using ::com::sun::star::util::XNumberFormatter;
64 using ::com::sun::star::util::XNumberFormatsSupplier;
65 using ::com::sun::star::frame::XFrame;
66 using ::com::sun::star::uno::Exception;
67 using ::com::sun::star::lang::EventObject;
68 using ::com::sun::star::beans::PropertyValue;
69 using ::com::sun::star::frame::XModel;
70 using ::com::sun::star::sdb::XOfficeDatabaseDocument;
71 using ::com::sun::star::awt::XWindow;
72 using ::com::sun::star::sdbc::XDatabaseMetaData;
73 using ::com::sun::star::sdb::XDocumentDataSource;
74 using ::com::sun::star::document::XEmbeddedScripts;
75 using ::com::sun::star::lang::IllegalArgumentException;
76 using ::com::sun::star::uno::UNO_SET_THROW;
77 using ::com::sun::star::uno::UNO_QUERY_THROW;
78 using ::com::sun::star::frame::XUntitledNumbers;
80 namespace {
82 class DataSourceHolder
84 public:
85 DataSourceHolder()
89 explicit DataSourceHolder(const Reference< XDataSource >& _rxDataSource)
90 : m_xDataSource(_rxDataSource)
92 Reference< XDocumentDataSource > xDocDS( m_xDataSource, UNO_QUERY );
93 if ( xDocDS.is() )
94 m_xDocument = xDocDS->getDatabaseDocument();
96 m_xDataSourceProps.set( m_xDataSource, UNO_QUERY );
99 const Reference< XDataSource >& getDataSource() const { return m_xDataSource; }
100 const Reference< XPropertySet >& getDataSourceProps() const { return m_xDataSourceProps; }
101 const Reference< XOfficeDatabaseDocument >& getDatabaseDocument() const { return m_xDocument; }
103 bool is() const { return m_xDataSource.is(); }
105 void clear()
107 m_xDataSource.clear();
108 m_xDocument.clear();
111 private:
112 Reference< XDataSource > m_xDataSource;
113 Reference< XPropertySet > m_xDataSourceProps;
114 Reference< XOfficeDatabaseDocument > m_xDocument;
119 struct DBSubComponentController_Impl
121 private:
122 ::std::optional< bool > m_aDocScriptSupport;
124 public:
125 ::dbtools::SQLExceptionInfo m_aCurrentError;
127 ::comphelper::OInterfaceContainerHelper3<css::util::XModifyListener>
128 m_aModifyListeners;
130 // <properties>
131 SharedConnection m_xConnection;
132 ::dbtools::DatabaseMetaData m_aSdbMetaData;
133 // </properties>
134 OUString m_sDataSourceName; // the data source we're working for
135 DataSourceHolder m_aDataSource;
136 Reference< XNumberFormatter > m_xFormatter; // a number formatter working with the connection's NumberFormatsSupplier
137 sal_Int32 m_nDocStartNumber;
138 bool m_bSuspended; // is true when the controller was already suspended
139 bool m_bEditable; // is the control readonly or not
140 bool m_bModified; // is the data modified
141 bool m_bNotAttached;
143 explicit DBSubComponentController_Impl(osl::Mutex& i_rMutex)
144 :m_aModifyListeners( i_rMutex )
145 ,m_nDocStartNumber(0)
146 ,m_bSuspended( false )
147 ,m_bEditable(true)
148 ,m_bModified(false)
149 ,m_bNotAttached(true)
153 bool documentHasScriptSupport() const
155 OSL_PRECOND( m_aDocScriptSupport.has_value(),
156 "DBSubComponentController_Impl::documentHasScriptSupport: not completely initialized, yet - don't know!?" );
157 return m_aDocScriptSupport.has_value() && *m_aDocScriptSupport;
160 void setDocumentScriptSupport( const bool _bSupport )
162 OSL_PRECOND( !m_aDocScriptSupport.has_value(),
163 "DBSubComponentController_Impl::setDocumentScriptSupport: already initialized!" );
164 m_aDocScriptSupport = ::std::optional< bool >( _bSupport );
168 // DBSubComponentController
169 DBSubComponentController::DBSubComponentController(const Reference< XComponentContext >& _rxORB)
170 :DBSubComponentController_Base( _rxORB )
171 ,m_pImpl( new DBSubComponentController_Impl( getMutex() ) )
175 DBSubComponentController::~DBSubComponentController()
179 void DBSubComponentController::impl_initialize(const ::comphelper::NamedValueCollection& rArguments)
181 OGenericUnoController::impl_initialize(rArguments);
183 Reference< XConnection > xConnection;
184 xConnection = rArguments.getOrDefault( PROPERTY_ACTIVE_CONNECTION, xConnection );
186 if ( !xConnection.is() )
187 ::dbtools::isEmbeddedInDatabase( getModel(), xConnection );
189 if ( xConnection.is() )
190 initializeConnection( xConnection );
192 bool bShowError = true;
193 if ( !isConnected() )
195 reconnect( false );
196 bShowError = false;
198 if ( !isConnected() )
200 if ( bShowError )
201 connectionLostMessage();
202 throw IllegalArgumentException();
206 Any SAL_CALL DBSubComponentController::queryInterface(const Type& _rType)
208 if ( _rType.equals( cppu::UnoType<XScriptInvocationContext>::get() ) )
210 if ( m_pImpl->documentHasScriptSupport() )
211 return Any( Reference< XScriptInvocationContext >( this ) );
212 return Any();
215 return DBSubComponentController_Base::queryInterface( _rType );
218 Sequence< Type > SAL_CALL DBSubComponentController::getTypes( )
220 Sequence< Type > aTypes( DBSubComponentController_Base::getTypes() );
221 if ( !m_pImpl->documentHasScriptSupport() )
223 auto [begin, end] = asNonConstRange(aTypes);
224 auto newEnd = std::remove_if( begin, end,
225 [](const Type& type)
226 { return type == cppu::UnoType<XScriptInvocationContext>::get(); } );
227 aTypes.realloc( std::distance(begin, newEnd) );
229 return aTypes;
232 void DBSubComponentController::initializeConnection( const Reference< XConnection >& _rxForeignConn )
234 DBG_ASSERT( !isConnected(), "DBSubComponentController::initializeConnection: not to be called when already connected!" );
235 // usually this gets called from within initialize of derived classes ...
236 if ( isConnected() )
237 disconnect();
239 m_pImpl->m_xConnection.reset( _rxForeignConn, SharedConnection::NoTakeOwnership );
240 m_pImpl->m_aSdbMetaData.reset( m_pImpl->m_xConnection );
241 startConnectionListening( m_pImpl->m_xConnection );
243 // get the data source the connection belongs to
246 // determine our data source
247 OSL_PRECOND( !m_pImpl->m_aDataSource.is(), "DBSubComponentController::initializeConnection: already a data source in this phase?" );
249 Reference< XChild > xConnAsChild( m_pImpl->m_xConnection, UNO_QUERY );
250 Reference< XDataSource > xDS;
251 if ( xConnAsChild.is() )
252 xDS.set( xConnAsChild->getParent(), UNO_QUERY );
254 // (take the indirection through XDataSource to ensure we have a correct object...)
255 m_pImpl->m_aDataSource = DataSourceHolder(xDS);
257 SAL_WARN_IF( !m_pImpl->m_aDataSource.is(), "dbaccess.ui", "DBSubComponentController::initializeConnection: unable to obtain the data source object!" );
259 if ( m_pImpl->m_bNotAttached )
261 Reference< XUntitledNumbers > xUntitledProvider( getDatabaseDocument(), UNO_QUERY );
262 m_pImpl->m_nDocStartNumber = 1;
263 if ( xUntitledProvider.is() )
264 m_pImpl->m_nDocStartNumber = xUntitledProvider->leaseNumber( static_cast< XWeak* >( this ) );
267 // determine the availability of script support in our document. Our own XScriptInvocationContext
268 // interface depends on this
269 m_pImpl->setDocumentScriptSupport( Reference< XEmbeddedScripts >( getDatabaseDocument(), UNO_QUERY ).is() );
271 // get a number formatter
272 Reference< XPropertySet > xDataSourceProps( m_pImpl->m_aDataSource.getDataSourceProps(), UNO_SET_THROW );
273 xDataSourceProps->getPropertyValue( PROPERTY_NAME ) >>= m_pImpl->m_sDataSourceName;
274 DBG_ASSERT( !m_pImpl->m_sDataSourceName.isEmpty(), "DBSubComponentController::initializeConnection: invalid data source name!" );
275 Reference< XNumberFormatsSupplier> xSupplier = ::dbtools::getNumberFormats(m_pImpl->m_xConnection);
276 if(xSupplier.is())
278 m_pImpl->m_xFormatter.set(NumberFormatter::create(getORB()), UNO_QUERY_THROW);
279 m_pImpl->m_xFormatter->attachNumberFormatsSupplier(xSupplier);
281 OSL_ENSURE(m_pImpl->m_xFormatter.is(),"No NumberFormatter!");
283 catch( const Exception& )
285 DBG_UNHANDLED_EXCEPTION("dbaccess");
289 void DBSubComponentController::reconnect( bool _bUI )
291 OSL_ENSURE(!m_pImpl->m_bSuspended, "Cannot reconnect while suspended!");
293 stopConnectionListening( m_pImpl->m_xConnection );
294 m_pImpl->m_aSdbMetaData.reset( nullptr );
295 m_pImpl->m_xConnection.clear();
297 // reconnect
298 bool bReConnect = true;
299 if ( _bUI )
301 std::unique_ptr<weld::MessageDialog> xQuery(Application::CreateMessageDialog(getFrameWeld(),
302 VclMessageType::Question, VclButtonsType::YesNo,
303 DBA_RES(STR_QUERY_CONNECTION_LOST)));
304 bReConnect = (RET_YES == xQuery->run());
307 // now really reconnect ...
308 if ( bReConnect )
310 m_pImpl->m_xConnection.reset( connect( m_pImpl->m_aDataSource.getDataSource() ), SharedConnection::TakeOwnership );
311 m_pImpl->m_aSdbMetaData.reset( m_pImpl->m_xConnection );
314 // invalidate all slots
315 InvalidateAll();
318 void DBSubComponentController::disconnect()
320 stopConnectionListening(m_pImpl->m_xConnection);
321 m_pImpl->m_aSdbMetaData.reset( nullptr );
322 m_pImpl->m_xConnection.clear();
324 InvalidateAll();
327 void DBSubComponentController::losingConnection()
329 // our connection was disposed so we need a new one
330 reconnect( true );
331 InvalidateAll();
334 void SAL_CALL DBSubComponentController::disposing()
336 DBSubComponentController_Base::disposing();
338 disconnect();
340 attachFrame( Reference < XFrame >() );
342 m_pImpl->m_aDataSource.clear();
345 void SAL_CALL DBSubComponentController::disposing(const EventObject& _rSource)
347 if ( _rSource.Source == getConnection() )
349 if ( !m_pImpl->m_bSuspended // when already suspended then we don't have to reconnect
350 && !getBroadcastHelper().bInDispose
351 && !getBroadcastHelper().bDisposed
352 && isConnected()
355 losingConnection();
357 else
359 m_pImpl->m_xConnection.reset( m_pImpl->m_xConnection, SharedConnection::NoTakeOwnership );
360 // this prevents the "disposeComponent" call in disconnect
361 disconnect();
364 else
365 DBSubComponentController_Base::disposing( _rSource );
368 void DBSubComponentController::appendError( const OUString& _rErrorMessage )
370 m_pImpl->m_aCurrentError.append( ::dbtools::SQLExceptionInfo::TYPE::SQLException, _rErrorMessage,
371 getStandardSQLState( ::dbtools::StandardSQLState::GENERAL_ERROR ),
372 1000 );
374 void DBSubComponentController::clearError()
376 m_pImpl->m_aCurrentError = ::dbtools::SQLExceptionInfo();
379 bool DBSubComponentController::hasError() const
381 return m_pImpl->m_aCurrentError.isValid();
384 const ::dbtools::SQLExceptionInfo& DBSubComponentController::getError() const
386 return m_pImpl->m_aCurrentError;
389 void DBSubComponentController::displayError()
391 showError( m_pImpl->m_aCurrentError );
394 sal_Bool SAL_CALL DBSubComponentController::suspend(sal_Bool bSuspend)
396 m_pImpl->m_bSuspended = bSuspend;
397 if ( !bSuspend && !isConnected() )
398 reconnect(true);
400 return true;
403 sal_Bool SAL_CALL DBSubComponentController::attachModel( const Reference< XModel > & _rxModel)
405 if ( !_rxModel.is() )
406 return false;
407 if ( !DBSubComponentController_Base::attachModel( _rxModel ) )
408 return false;
410 m_pImpl->m_bNotAttached = false;
411 if ( m_pImpl->m_nDocStartNumber == 1 )
412 releaseNumberForComponent();
414 Reference< XUntitledNumbers > xUntitledProvider( _rxModel, UNO_QUERY );
415 m_pImpl->m_nDocStartNumber = 1;
416 if ( xUntitledProvider.is() )
417 m_pImpl->m_nDocStartNumber = xUntitledProvider->leaseNumber( static_cast< XWeak* >( this ) );
419 return true;
422 void DBSubComponentController::Execute(sal_uInt16 _nId, const Sequence< PropertyValue >& _rArgs)
424 if ( _nId == ID_BROWSER_CLOSE )
426 closeTask();
427 return;
430 DBSubComponentController_Base::Execute( _nId, _rArgs );
431 InvalidateFeature( _nId );
434 OUString DBSubComponentController::getDataSourceName() const
436 OUString sName;
437 Reference< XPropertySet > xDataSourceProps( m_pImpl->m_aDataSource.getDataSourceProps() );
438 if ( xDataSourceProps.is() )
439 xDataSourceProps->getPropertyValue(PROPERTY_NAME) >>= sName;
440 return sName;
442 void DBSubComponentController::connectionLostMessage() const
444 OUString aMessage(DBA_RES(RID_STR_CONNECTION_LOST));
445 Reference< XWindow > xWindow = getTopMostContainerWindow();
446 vcl::Window* pWin = nullptr;
447 if ( xWindow.is() )
448 pWin = VCLUnoHelper::GetWindow(xWindow);
449 if ( !pWin )
450 pWin = getView()->Window::GetParent();
452 std::unique_ptr<weld::MessageDialog> xInfo(Application::CreateMessageDialog(pWin ? pWin->GetFrameWeld() : nullptr,
453 VclMessageType::Info, VclButtonsType::Ok, aMessage));
454 xInfo->run();
456 const Reference< XConnection >& DBSubComponentController::getConnection() const
458 return m_pImpl->m_xConnection;
461 bool DBSubComponentController::isReadOnly() const
463 return !m_pImpl->m_bEditable;
466 bool DBSubComponentController::isEditable() const
468 return m_pImpl->m_bEditable;
471 void DBSubComponentController::setEditable(bool _bEditable)
473 m_pImpl->m_bEditable = _bEditable;
476 const ::dbtools::DatabaseMetaData& DBSubComponentController::getSdbMetaData() const
478 return m_pImpl->m_aSdbMetaData;
481 bool DBSubComponentController::isConnected() const
483 return m_pImpl->m_xConnection.is();
486 Reference< XDatabaseMetaData > DBSubComponentController::getMetaData( ) const
488 Reference< XDatabaseMetaData > xMeta;
491 if ( isConnected() )
492 xMeta.set( m_pImpl->m_xConnection->getMetaData(), UNO_SET_THROW );
494 catch( const Exception& )
496 DBG_UNHANDLED_EXCEPTION("dbaccess");
498 return xMeta;
501 const Reference< XPropertySet >& DBSubComponentController::getDataSource() const
503 return m_pImpl->m_aDataSource.getDataSourceProps();
506 bool DBSubComponentController::haveDataSource() const
508 return m_pImpl->m_aDataSource.is();
511 Reference< XModel > DBSubComponentController::getDatabaseDocument() const
513 return Reference< XModel >( m_pImpl->m_aDataSource.getDatabaseDocument(), UNO_QUERY );
516 Reference< XNumberFormatter > const & DBSubComponentController::getNumberFormatter() const
518 return m_pImpl->m_xFormatter;
521 Reference< XModel > DBSubComponentController::getPrivateModel() const
523 return getDatabaseDocument();
525 // XTitle
526 OUString SAL_CALL DBSubComponentController::getTitle()
528 ::osl::MutexGuard aGuard( getMutex() );
529 if ( m_bExternalTitle )
530 return impl_getTitleHelper_throw()->getTitle ();
532 OUStringBuffer sTitle;
533 Reference< XTitle > xTitle(getPrivateModel(),UNO_QUERY);
534 if ( xTitle.is() )
536 sTitle.append( xTitle->getTitle() + " : ");
538 sTitle.append( getPrivateTitle() );
539 return sTitle.makeStringAndClear();
542 sal_Int32 DBSubComponentController::getCurrentStartNumber() const
544 return m_pImpl->m_nDocStartNumber;
547 Reference< XEmbeddedScripts > SAL_CALL DBSubComponentController::getScriptContainer()
549 ::osl::MutexGuard aGuard( getMutex() );
550 if ( !m_pImpl->documentHasScriptSupport() )
551 return nullptr;
553 return Reference< XEmbeddedScripts >( getDatabaseDocument(), UNO_QUERY_THROW );
556 void SAL_CALL DBSubComponentController::addModifyListener( const Reference< XModifyListener >& i_Listener )
558 ::osl::MutexGuard aGuard( getMutex() );
559 m_pImpl->m_aModifyListeners.addInterface( i_Listener );
562 void SAL_CALL DBSubComponentController::removeModifyListener( const Reference< XModifyListener >& i_Listener )
564 ::osl::MutexGuard aGuard( getMutex() );
565 m_pImpl->m_aModifyListeners.removeInterface( i_Listener );
568 sal_Bool SAL_CALL DBSubComponentController::isModified( )
570 ::osl::MutexGuard aGuard( getMutex() );
571 return impl_isModified();
574 void SAL_CALL DBSubComponentController::setModified( sal_Bool i_bModified )
576 ::osl::ClearableMutexGuard aGuard( getMutex() );
578 if ( m_pImpl->m_bModified == bool(i_bModified) )
579 return;
581 m_pImpl->m_bModified = i_bModified;
582 impl_onModifyChanged();
584 EventObject aEvent( *this );
585 aGuard.clear();
586 m_pImpl->m_aModifyListeners.notifyEach( &XModifyListener::modified, aEvent );
589 bool DBSubComponentController::impl_isModified() const
591 return m_pImpl->m_bModified;
594 void DBSubComponentController::impl_onModifyChanged()
596 InvalidateFeature( ID_BROWSER_SAVEDOC );
597 if ( isFeatureSupported( ID_BROWSER_SAVEASDOC ) )
598 InvalidateFeature( ID_BROWSER_SAVEASDOC );
601 } // namespace dbaui
603 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */