android: Update app icon to new startcenter icon
[LibreOffice.git] / dbaccess / source / ui / misc / dbsubcomponentcontroller.cxx
blob12a73c78877ffc26676626d99b4554eff1f208d1
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,
156 "DBSubComponentController_Impl::documentHasScriptSupport: not completely initialized, yet - don't know!?" );
157 return !!m_aDocScriptSupport && *m_aDocScriptSupport;
160 void setDocumentScriptSupport( const bool _bSupport )
162 OSL_PRECOND( !m_aDocScriptSupport,
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()
181 OGenericUnoController::impl_initialize();
183 const ::comphelper::NamedValueCollection& rArguments( getInitParams() );
185 Reference< XConnection > xConnection;
186 xConnection = rArguments.getOrDefault( PROPERTY_ACTIVE_CONNECTION, xConnection );
188 if ( !xConnection.is() )
189 ::dbtools::isEmbeddedInDatabase( getModel(), xConnection );
191 if ( xConnection.is() )
192 initializeConnection( xConnection );
194 bool bShowError = true;
195 if ( !isConnected() )
197 reconnect( false );
198 bShowError = false;
200 if ( !isConnected() )
202 if ( bShowError )
203 connectionLostMessage();
204 throw IllegalArgumentException();
208 Any SAL_CALL DBSubComponentController::queryInterface(const Type& _rType)
210 if ( _rType.equals( cppu::UnoType<XScriptInvocationContext>::get() ) )
212 if ( m_pImpl->documentHasScriptSupport() )
213 return Any( Reference< XScriptInvocationContext >( this ) );
214 return Any();
217 return DBSubComponentController_Base::queryInterface( _rType );
220 Sequence< Type > SAL_CALL DBSubComponentController::getTypes( )
222 Sequence< Type > aTypes( DBSubComponentController_Base::getTypes() );
223 if ( !m_pImpl->documentHasScriptSupport() )
225 auto [begin, end] = asNonConstRange(aTypes);
226 auto newEnd = std::remove_if( begin, end,
227 [](const Type& type)
228 { return type == cppu::UnoType<XScriptInvocationContext>::get(); } );
229 aTypes.realloc( std::distance(begin, newEnd) );
231 return aTypes;
234 void DBSubComponentController::initializeConnection( const Reference< XConnection >& _rxForeignConn )
236 DBG_ASSERT( !isConnected(), "DBSubComponentController::initializeConnection: not to be called when already connected!" );
237 // usually this gets called from within initialize of derived classes ...
238 if ( isConnected() )
239 disconnect();
241 m_pImpl->m_xConnection.reset( _rxForeignConn, SharedConnection::NoTakeOwnership );
242 m_pImpl->m_aSdbMetaData.reset( m_pImpl->m_xConnection );
243 startConnectionListening( m_pImpl->m_xConnection );
245 // get the data source the connection belongs to
248 // determine our data source
249 OSL_PRECOND( !m_pImpl->m_aDataSource.is(), "DBSubComponentController::initializeConnection: already a data source in this phase?" );
251 Reference< XChild > xConnAsChild( m_pImpl->m_xConnection, UNO_QUERY );
252 Reference< XDataSource > xDS;
253 if ( xConnAsChild.is() )
254 xDS.set( xConnAsChild->getParent(), UNO_QUERY );
256 // (take the indirection through XDataSource to ensure we have a correct object...)
257 m_pImpl->m_aDataSource = DataSourceHolder(xDS);
259 SAL_WARN_IF( !m_pImpl->m_aDataSource.is(), "dbaccess.ui", "DBSubComponentController::initializeConnection: unable to obtain the data source object!" );
261 if ( m_pImpl->m_bNotAttached )
263 Reference< XUntitledNumbers > xUntitledProvider( getDatabaseDocument(), UNO_QUERY );
264 m_pImpl->m_nDocStartNumber = 1;
265 if ( xUntitledProvider.is() )
266 m_pImpl->m_nDocStartNumber = xUntitledProvider->leaseNumber( static_cast< XWeak* >( this ) );
269 // determine the availability of script support in our document. Our own XScriptInvocationContext
270 // interface depends on this
271 m_pImpl->setDocumentScriptSupport( Reference< XEmbeddedScripts >( getDatabaseDocument(), UNO_QUERY ).is() );
273 // get a number formatter
274 Reference< XPropertySet > xDataSourceProps( m_pImpl->m_aDataSource.getDataSourceProps(), UNO_SET_THROW );
275 xDataSourceProps->getPropertyValue( PROPERTY_NAME ) >>= m_pImpl->m_sDataSourceName;
276 DBG_ASSERT( !m_pImpl->m_sDataSourceName.isEmpty(), "DBSubComponentController::initializeConnection: invalid data source name!" );
277 Reference< XNumberFormatsSupplier> xSupplier = ::dbtools::getNumberFormats(m_pImpl->m_xConnection);
278 if(xSupplier.is())
280 m_pImpl->m_xFormatter.set(NumberFormatter::create(getORB()), UNO_QUERY_THROW);
281 m_pImpl->m_xFormatter->attachNumberFormatsSupplier(xSupplier);
283 OSL_ENSURE(m_pImpl->m_xFormatter.is(),"No NumberFormatter!");
285 catch( const Exception& )
287 DBG_UNHANDLED_EXCEPTION("dbaccess");
291 void DBSubComponentController::reconnect( bool _bUI )
293 OSL_ENSURE(!m_pImpl->m_bSuspended, "Cannot reconnect while suspended!");
295 stopConnectionListening( m_pImpl->m_xConnection );
296 m_pImpl->m_aSdbMetaData.reset( nullptr );
297 m_pImpl->m_xConnection.clear();
299 // reconnect
300 bool bReConnect = true;
301 if ( _bUI )
303 std::unique_ptr<weld::MessageDialog> xQuery(Application::CreateMessageDialog(getFrameWeld(),
304 VclMessageType::Question, VclButtonsType::YesNo,
305 DBA_RES(STR_QUERY_CONNECTION_LOST)));
306 bReConnect = (RET_YES == xQuery->run());
309 // now really reconnect ...
310 if ( bReConnect )
312 m_pImpl->m_xConnection.reset( connect( m_pImpl->m_aDataSource.getDataSource() ), SharedConnection::TakeOwnership );
313 m_pImpl->m_aSdbMetaData.reset( m_pImpl->m_xConnection );
316 // invalidate all slots
317 InvalidateAll();
320 void DBSubComponentController::disconnect()
322 stopConnectionListening(m_pImpl->m_xConnection);
323 m_pImpl->m_aSdbMetaData.reset( nullptr );
324 m_pImpl->m_xConnection.clear();
326 InvalidateAll();
329 void DBSubComponentController::losingConnection()
331 // our connection was disposed so we need a new one
332 reconnect( true );
333 InvalidateAll();
336 void SAL_CALL DBSubComponentController::disposing()
338 DBSubComponentController_Base::disposing();
340 disconnect();
342 attachFrame( Reference < XFrame >() );
344 m_pImpl->m_aDataSource.clear();
347 void SAL_CALL DBSubComponentController::disposing(const EventObject& _rSource)
349 if ( _rSource.Source == getConnection() )
351 if ( !m_pImpl->m_bSuspended // when already suspended then we don't have to reconnect
352 && !getBroadcastHelper().bInDispose
353 && !getBroadcastHelper().bDisposed
354 && isConnected()
357 losingConnection();
359 else
361 m_pImpl->m_xConnection.reset( m_pImpl->m_xConnection, SharedConnection::NoTakeOwnership );
362 // this prevents the "disposeComponent" call in disconnect
363 disconnect();
366 else
367 DBSubComponentController_Base::disposing( _rSource );
370 void DBSubComponentController::appendError( const OUString& _rErrorMessage )
372 m_pImpl->m_aCurrentError.append( ::dbtools::SQLExceptionInfo::TYPE::SQLException, _rErrorMessage,
373 getStandardSQLState( ::dbtools::StandardSQLState::GENERAL_ERROR ),
374 1000 );
376 void DBSubComponentController::clearError()
378 m_pImpl->m_aCurrentError = ::dbtools::SQLExceptionInfo();
381 bool DBSubComponentController::hasError() const
383 return m_pImpl->m_aCurrentError.isValid();
386 const ::dbtools::SQLExceptionInfo& DBSubComponentController::getError() const
388 return m_pImpl->m_aCurrentError;
391 void DBSubComponentController::displayError()
393 showError( m_pImpl->m_aCurrentError );
396 sal_Bool SAL_CALL DBSubComponentController::suspend(sal_Bool bSuspend)
398 m_pImpl->m_bSuspended = bSuspend;
399 if ( !bSuspend && !isConnected() )
400 reconnect(true);
402 return true;
405 sal_Bool SAL_CALL DBSubComponentController::attachModel( const Reference< XModel > & _rxModel)
407 if ( !_rxModel.is() )
408 return false;
409 if ( !DBSubComponentController_Base::attachModel( _rxModel ) )
410 return false;
412 m_pImpl->m_bNotAttached = false;
413 if ( m_pImpl->m_nDocStartNumber == 1 )
414 releaseNumberForComponent();
416 Reference< XUntitledNumbers > xUntitledProvider( _rxModel, UNO_QUERY );
417 m_pImpl->m_nDocStartNumber = 1;
418 if ( xUntitledProvider.is() )
419 m_pImpl->m_nDocStartNumber = xUntitledProvider->leaseNumber( static_cast< XWeak* >( this ) );
421 return true;
424 void DBSubComponentController::Execute(sal_uInt16 _nId, const Sequence< PropertyValue >& _rArgs)
426 if ( _nId == ID_BROWSER_CLOSE )
428 closeTask();
429 return;
432 DBSubComponentController_Base::Execute( _nId, _rArgs );
433 InvalidateFeature( _nId );
436 OUString DBSubComponentController::getDataSourceName() const
438 OUString sName;
439 Reference< XPropertySet > xDataSourceProps( m_pImpl->m_aDataSource.getDataSourceProps() );
440 if ( xDataSourceProps.is() )
441 xDataSourceProps->getPropertyValue(PROPERTY_NAME) >>= sName;
442 return sName;
444 void DBSubComponentController::connectionLostMessage() const
446 OUString aMessage(DBA_RES(RID_STR_CONNECTION_LOST));
447 Reference< XWindow > xWindow = getTopMostContainerWindow();
448 vcl::Window* pWin = nullptr;
449 if ( xWindow.is() )
450 pWin = VCLUnoHelper::GetWindow(xWindow);
451 if ( !pWin )
452 pWin = getView()->Window::GetParent();
454 std::unique_ptr<weld::MessageDialog> xInfo(Application::CreateMessageDialog(pWin ? pWin->GetFrameWeld() : nullptr,
455 VclMessageType::Info, VclButtonsType::Ok, aMessage));
456 xInfo->run();
458 const Reference< XConnection >& DBSubComponentController::getConnection() const
460 return m_pImpl->m_xConnection;
463 bool DBSubComponentController::isReadOnly() const
465 return !m_pImpl->m_bEditable;
468 bool DBSubComponentController::isEditable() const
470 return m_pImpl->m_bEditable;
473 void DBSubComponentController::setEditable(bool _bEditable)
475 m_pImpl->m_bEditable = _bEditable;
478 const ::dbtools::DatabaseMetaData& DBSubComponentController::getSdbMetaData() const
480 return m_pImpl->m_aSdbMetaData;
483 bool DBSubComponentController::isConnected() const
485 return m_pImpl->m_xConnection.is();
488 Reference< XDatabaseMetaData > DBSubComponentController::getMetaData( ) const
490 Reference< XDatabaseMetaData > xMeta;
493 if ( isConnected() )
494 xMeta.set( m_pImpl->m_xConnection->getMetaData(), UNO_SET_THROW );
496 catch( const Exception& )
498 DBG_UNHANDLED_EXCEPTION("dbaccess");
500 return xMeta;
503 const Reference< XPropertySet >& DBSubComponentController::getDataSource() const
505 return m_pImpl->m_aDataSource.getDataSourceProps();
508 bool DBSubComponentController::haveDataSource() const
510 return m_pImpl->m_aDataSource.is();
513 Reference< XModel > DBSubComponentController::getDatabaseDocument() const
515 return Reference< XModel >( m_pImpl->m_aDataSource.getDatabaseDocument(), UNO_QUERY );
518 Reference< XNumberFormatter > const & DBSubComponentController::getNumberFormatter() const
520 return m_pImpl->m_xFormatter;
523 Reference< XModel > DBSubComponentController::getPrivateModel() const
525 return getDatabaseDocument();
527 // XTitle
528 OUString SAL_CALL DBSubComponentController::getTitle()
530 ::osl::MutexGuard aGuard( getMutex() );
531 if ( m_bExternalTitle )
532 return impl_getTitleHelper_throw()->getTitle ();
534 OUStringBuffer sTitle;
535 Reference< XTitle > xTitle(getPrivateModel(),UNO_QUERY);
536 if ( xTitle.is() )
538 sTitle.append( xTitle->getTitle() + " : ");
540 sTitle.append( getPrivateTitle() );
541 return sTitle.makeStringAndClear();
544 sal_Int32 DBSubComponentController::getCurrentStartNumber() const
546 return m_pImpl->m_nDocStartNumber;
549 Reference< XEmbeddedScripts > SAL_CALL DBSubComponentController::getScriptContainer()
551 ::osl::MutexGuard aGuard( getMutex() );
552 if ( !m_pImpl->documentHasScriptSupport() )
553 return nullptr;
555 return Reference< XEmbeddedScripts >( getDatabaseDocument(), UNO_QUERY_THROW );
558 void SAL_CALL DBSubComponentController::addModifyListener( const Reference< XModifyListener >& i_Listener )
560 ::osl::MutexGuard aGuard( getMutex() );
561 m_pImpl->m_aModifyListeners.addInterface( i_Listener );
564 void SAL_CALL DBSubComponentController::removeModifyListener( const Reference< XModifyListener >& i_Listener )
566 ::osl::MutexGuard aGuard( getMutex() );
567 m_pImpl->m_aModifyListeners.removeInterface( i_Listener );
570 sal_Bool SAL_CALL DBSubComponentController::isModified( )
572 ::osl::MutexGuard aGuard( getMutex() );
573 return impl_isModified();
576 void SAL_CALL DBSubComponentController::setModified( sal_Bool i_bModified )
578 ::osl::ClearableMutexGuard aGuard( getMutex() );
580 if ( m_pImpl->m_bModified == bool(i_bModified) )
581 return;
583 m_pImpl->m_bModified = i_bModified;
584 impl_onModifyChanged();
586 EventObject aEvent( *this );
587 aGuard.clear();
588 m_pImpl->m_aModifyListeners.notifyEach( &XModifyListener::modified, aEvent );
591 bool DBSubComponentController::impl_isModified() const
593 return m_pImpl->m_bModified;
596 void DBSubComponentController::impl_onModifyChanged()
598 InvalidateFeature( ID_BROWSER_SAVEDOC );
599 if ( isFeatureSupported( ID_BROWSER_SAVEASDOC ) )
600 InvalidateFeature( ID_BROWSER_SAVEASDOC );
603 } // namespace dbaui
605 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */