1*96de5490SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*96de5490SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*96de5490SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*96de5490SAndrew Rist  * distributed with this work for additional information
6*96de5490SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*96de5490SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*96de5490SAndrew Rist  * "License"); you may not use this file except in compliance
9*96de5490SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*96de5490SAndrew Rist  *
11*96de5490SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*96de5490SAndrew Rist  *
13*96de5490SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*96de5490SAndrew Rist  * software distributed under the License is distributed on an
15*96de5490SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*96de5490SAndrew Rist  * KIND, either express or implied.  See the License for the
17*96de5490SAndrew Rist  * specific language governing permissions and limitations
18*96de5490SAndrew Rist  * under the License.
19*96de5490SAndrew Rist  *
20*96de5490SAndrew Rist  *************************************************************/
21*96de5490SAndrew Rist 
22*96de5490SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_dbaccess.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "browserids.hxx"
28cdf0e10cSrcweir #include "commontypes.hxx"
29cdf0e10cSrcweir #include "dataview.hxx"
30cdf0e10cSrcweir #include "dbu_misc.hrc"
31cdf0e10cSrcweir #include "dbustrings.hrc"
32cdf0e10cSrcweir #include "moduledbu.hxx"
33cdf0e10cSrcweir #include "dbsubcomponentcontroller.hxx"
34cdf0e10cSrcweir 
35cdf0e10cSrcweir /** === begin UNO includes === **/
36cdf0e10cSrcweir #include <com/sun/star/frame/XUntitledNumbers.hpp>
37cdf0e10cSrcweir #include <com/sun/star/beans/PropertyAttribute.hpp>
38cdf0e10cSrcweir #include <com/sun/star/container/XChild.hpp>
39cdf0e10cSrcweir #include <com/sun/star/container/XNameAccess.hpp>
40cdf0e10cSrcweir #include <com/sun/star/sdb/XDocumentDataSource.hpp>
41cdf0e10cSrcweir #include <com/sun/star/sdb/XOfficeDatabaseDocument.hpp>
42cdf0e10cSrcweir #include <com/sun/star/sdbc/XDataSource.hpp>
43cdf0e10cSrcweir #include <com/sun/star/lang/IllegalArgumentException.hpp>
44cdf0e10cSrcweir #include <com/sun/star/frame/XUntitledNumbers.hpp>
45cdf0e10cSrcweir /** === end UNO includes === **/
46cdf0e10cSrcweir 
47cdf0e10cSrcweir #include <comphelper/sequence.hxx>
48cdf0e10cSrcweir #include <comphelper/types.hxx>
49cdf0e10cSrcweir #include <connectivity/dbexception.hxx>
50cdf0e10cSrcweir #include <connectivity/dbtools.hxx>
51cdf0e10cSrcweir #include <cppuhelper/typeprovider.hxx>
52cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
53cdf0e10cSrcweir #include <toolkit/unohlp.hxx>
54cdf0e10cSrcweir #include <tools/debug.hxx>
55cdf0e10cSrcweir #include <tools/diagnose_ex.h>
56cdf0e10cSrcweir #include <vcl/msgbox.hxx>
57cdf0e10cSrcweir 
58cdf0e10cSrcweir //........................................................................
59cdf0e10cSrcweir namespace dbaui
60cdf0e10cSrcweir {
61cdf0e10cSrcweir //........................................................................
62cdf0e10cSrcweir 
63cdf0e10cSrcweir     /** === begin UNO using === **/
64cdf0e10cSrcweir     using ::com::sun::star::uno::Any;
65cdf0e10cSrcweir     using ::com::sun::star::uno::Reference;
66cdf0e10cSrcweir     using ::com::sun::star::beans::XPropertySet;
67cdf0e10cSrcweir     using ::com::sun::star::util::XNumberFormatter;
68cdf0e10cSrcweir     using ::com::sun::star::lang::XMultiServiceFactory;
69cdf0e10cSrcweir     using ::com::sun::star::uno::RuntimeException;
70cdf0e10cSrcweir     using ::com::sun::star::uno::Sequence;
71cdf0e10cSrcweir     using ::com::sun::star::uno::Type;
72cdf0e10cSrcweir     using ::com::sun::star::sdbc::XConnection;
73cdf0e10cSrcweir     using ::com::sun::star::uno::UNO_QUERY;
74cdf0e10cSrcweir     using ::com::sun::star::container::XChild;
75cdf0e10cSrcweir     using ::com::sun::star::sdbc::XDataSource;
76cdf0e10cSrcweir     using ::com::sun::star::util::XNumberFormatter;
77cdf0e10cSrcweir     using ::com::sun::star::util::XNumberFormatsSupplier;
78cdf0e10cSrcweir     using ::com::sun::star::frame::XFrame;
79cdf0e10cSrcweir     using ::com::sun::star::uno::Exception;
80cdf0e10cSrcweir     using ::com::sun::star::sdbc::SQLException;
81cdf0e10cSrcweir     using ::com::sun::star::lang::EventObject;
82cdf0e10cSrcweir     using ::com::sun::star::beans::PropertyValue;
83cdf0e10cSrcweir     using ::com::sun::star::frame::XModel;
84cdf0e10cSrcweir     using ::com::sun::star::sdb::XOfficeDatabaseDocument;
85cdf0e10cSrcweir     using ::com::sun::star::awt::XWindow;
86cdf0e10cSrcweir     using ::com::sun::star::sdbc::XDatabaseMetaData;
87cdf0e10cSrcweir     using ::com::sun::star::sdb::XDocumentDataSource;
88cdf0e10cSrcweir     using ::com::sun::star::document::XEmbeddedScripts;
89cdf0e10cSrcweir     using ::com::sun::star::lang::IllegalArgumentException;
90cdf0e10cSrcweir     using ::com::sun::star::uno::UNO_SET_THROW;
91cdf0e10cSrcweir     using ::com::sun::star::uno::UNO_QUERY_THROW;
92cdf0e10cSrcweir     using ::com::sun::star::frame::XUntitledNumbers;
93cdf0e10cSrcweir     using ::com::sun::star::beans::PropertyVetoException;
94cdf0e10cSrcweir     /** === end UNO using === **/
95cdf0e10cSrcweir 
96cdf0e10cSrcweir     class DataSourceHolder
97cdf0e10cSrcweir     {
98cdf0e10cSrcweir     public:
DataSourceHolder()99cdf0e10cSrcweir         DataSourceHolder()
100cdf0e10cSrcweir         {
101cdf0e10cSrcweir         }
102cdf0e10cSrcweir 
DataSourceHolder(const Reference<XDataSource> & _rxDataSource)103cdf0e10cSrcweir         DataSourceHolder( const Reference< XDataSource >& _rxDataSource )
104cdf0e10cSrcweir         {
105cdf0e10cSrcweir             m_xDataSource = _rxDataSource;
106cdf0e10cSrcweir             Reference< XDocumentDataSource > xDocDS( m_xDataSource, UNO_QUERY );
107cdf0e10cSrcweir             if ( xDocDS.is() )
108cdf0e10cSrcweir                 m_xDocument = xDocDS->getDatabaseDocument();
109cdf0e10cSrcweir 
110cdf0e10cSrcweir             m_xDataSourceProps.set( m_xDataSource, UNO_QUERY );
111cdf0e10cSrcweir         }
112cdf0e10cSrcweir 
getDataSource() const113cdf0e10cSrcweir         const Reference< XDataSource >&             getDataSource() const { return m_xDataSource; }
getDataSourceProps() const114cdf0e10cSrcweir         const Reference< XPropertySet >&            getDataSourceProps() const { return m_xDataSourceProps; }
getDatabaseDocument() const115cdf0e10cSrcweir         const Reference< XOfficeDatabaseDocument >  getDatabaseDocument() const { return m_xDocument; }
116cdf0e10cSrcweir 
is() const117cdf0e10cSrcweir         bool is() const { return m_xDataSource.is(); }
118cdf0e10cSrcweir 
clear()119cdf0e10cSrcweir         void clear()
120cdf0e10cSrcweir         {
121cdf0e10cSrcweir             m_xDataSource.clear();
122cdf0e10cSrcweir             m_xDocument.clear();
123cdf0e10cSrcweir         }
124cdf0e10cSrcweir 
125cdf0e10cSrcweir     private:
126cdf0e10cSrcweir         Reference< XDataSource >                m_xDataSource;
127cdf0e10cSrcweir         Reference< XPropertySet >               m_xDataSourceProps;
128cdf0e10cSrcweir         Reference< XOfficeDatabaseDocument >    m_xDocument;
129cdf0e10cSrcweir     };
130cdf0e10cSrcweir 
131cdf0e10cSrcweir     struct DBSubComponentController_Impl
132cdf0e10cSrcweir     {
133cdf0e10cSrcweir     private:
134cdf0e10cSrcweir         ::boost::optional< bool >       m_aDocScriptSupport;
135cdf0e10cSrcweir 
136cdf0e10cSrcweir     public:
137cdf0e10cSrcweir         OModuleClient                   m_aModuleClient;
138cdf0e10cSrcweir         ::dbtools::SQLExceptionInfo     m_aCurrentError;
139cdf0e10cSrcweir 
140cdf0e10cSrcweir         ::cppu::OInterfaceContainerHelper
141cdf0e10cSrcweir                                         m_aModifyListeners;
142cdf0e10cSrcweir 
143cdf0e10cSrcweir 		// <properties>
144cdf0e10cSrcweir         SharedConnection                m_xConnection;
145cdf0e10cSrcweir         ::dbtools::DatabaseMetaData     m_aSdbMetaData;
146cdf0e10cSrcweir 		// </properties>
147cdf0e10cSrcweir 		::rtl::OUString	                m_sDataSourceName;		// the data source we're working for
148cdf0e10cSrcweir 		DataSourceHolder                m_aDataSource;
149cdf0e10cSrcweir         Reference< XModel >             m_xDocument;
150cdf0e10cSrcweir 		Reference< XNumberFormatter > 	m_xFormatter;	// a number formatter working with the connection's NumberFormatsSupplier
151cdf0e10cSrcweir         sal_Int32                       m_nDocStartNumber;
152cdf0e10cSrcweir 		sal_Bool		                m_bSuspended;	// is true when the controller was already suspended
153cdf0e10cSrcweir 		sal_Bool		                m_bEditable;	// is the control readonly or not
154cdf0e10cSrcweir 		sal_Bool		                m_bModified;	// is the data modified
155cdf0e10cSrcweir         bool                            m_bNotAttached;
156cdf0e10cSrcweir 
DBSubComponentController_Impldbaui::DBSubComponentController_Impl157cdf0e10cSrcweir         DBSubComponentController_Impl( ::osl::Mutex& i_rMutex )
158cdf0e10cSrcweir             :m_aDocScriptSupport()
159cdf0e10cSrcweir             ,m_aModifyListeners( i_rMutex )
160cdf0e10cSrcweir             ,m_nDocStartNumber(0)
161cdf0e10cSrcweir             ,m_bSuspended( sal_False )
162cdf0e10cSrcweir 		    ,m_bEditable(sal_True)
163cdf0e10cSrcweir 		    ,m_bModified(sal_False)
164cdf0e10cSrcweir             ,m_bNotAttached(true)
165cdf0e10cSrcweir         {
166cdf0e10cSrcweir         }
167cdf0e10cSrcweir 
documentHasScriptSupportdbaui::DBSubComponentController_Impl168cdf0e10cSrcweir         bool    documentHasScriptSupport() const
169cdf0e10cSrcweir         {
170cdf0e10cSrcweir             OSL_PRECOND( !!m_aDocScriptSupport,
171cdf0e10cSrcweir                 "DBSubComponentController_Impl::documentHasScriptSupport: not completely initialized, yet - don't know!?" );
172cdf0e10cSrcweir             return !!m_aDocScriptSupport && *m_aDocScriptSupport;
173cdf0e10cSrcweir         }
174cdf0e10cSrcweir 
setDocumentScriptSupportdbaui::DBSubComponentController_Impl175cdf0e10cSrcweir         void    setDocumentScriptSupport( const bool _bSupport )
176cdf0e10cSrcweir         {
177cdf0e10cSrcweir             OSL_PRECOND( !m_aDocScriptSupport,
178cdf0e10cSrcweir                 "DBSubComponentController_Impl::setDocumentScriptSupport: already initialized!" );
179cdf0e10cSrcweir             m_aDocScriptSupport = ::boost::optional< bool >( _bSupport );
180cdf0e10cSrcweir         }
181cdf0e10cSrcweir     };
182cdf0e10cSrcweir 
183cdf0e10cSrcweir 	//====================================================================
184cdf0e10cSrcweir 	//= DBSubComponentController
185cdf0e10cSrcweir 	//====================================================================
186cdf0e10cSrcweir 	//--------------------------------------------------------------------
DBSubComponentController(const Reference<XMultiServiceFactory> & _rxORB)187cdf0e10cSrcweir 	DBSubComponentController::DBSubComponentController(const Reference< XMultiServiceFactory >& _rxORB)
188cdf0e10cSrcweir         :DBSubComponentController_Base( _rxORB )
189cdf0e10cSrcweir         ,m_pImpl( new DBSubComponentController_Impl( getMutex() ) )
190cdf0e10cSrcweir 	{
191cdf0e10cSrcweir 	}
192cdf0e10cSrcweir 
193cdf0e10cSrcweir     //--------------------------------------------------------------------
~DBSubComponentController()194cdf0e10cSrcweir     DBSubComponentController::~DBSubComponentController()
195cdf0e10cSrcweir     {
196cdf0e10cSrcweir     }
197cdf0e10cSrcweir 
198cdf0e10cSrcweir 	//--------------------------------------------------------------------
impl_initialize()199cdf0e10cSrcweir     void DBSubComponentController::impl_initialize()
200cdf0e10cSrcweir     {
201cdf0e10cSrcweir         OGenericUnoController::impl_initialize();
202cdf0e10cSrcweir 
203cdf0e10cSrcweir         const ::comphelper::NamedValueCollection& rArguments( getInitParams() );
204cdf0e10cSrcweir 
205cdf0e10cSrcweir 		Reference< XConnection > xConnection;
206cdf0e10cSrcweir         xConnection = rArguments.getOrDefault( (::rtl::OUString)PROPERTY_ACTIVE_CONNECTION, xConnection );
207cdf0e10cSrcweir 
208cdf0e10cSrcweir         if ( !xConnection.is() )
209cdf0e10cSrcweir             ::dbtools::isEmbeddedInDatabase( getModel(), xConnection );
210cdf0e10cSrcweir 
211cdf0e10cSrcweir 		if ( xConnection.is() )
212cdf0e10cSrcweir             initializeConnection( xConnection );
213cdf0e10cSrcweir 
214cdf0e10cSrcweir 		bool bShowError = true;
215cdf0e10cSrcweir 		if ( !isConnected() )
216cdf0e10cSrcweir         {
217cdf0e10cSrcweir 			reconnect( sal_False );
218cdf0e10cSrcweir 			bShowError = false;
219cdf0e10cSrcweir 		}
220cdf0e10cSrcweir 		if ( !isConnected() )
221cdf0e10cSrcweir 		{
222cdf0e10cSrcweir 			if ( bShowError )
223cdf0e10cSrcweir 				connectionLostMessage();
224cdf0e10cSrcweir 			throw IllegalArgumentException();
225cdf0e10cSrcweir 		}
226cdf0e10cSrcweir     }
227cdf0e10cSrcweir 
228cdf0e10cSrcweir     //--------------------------------------------------------------------
queryInterface(const Type & _rType)229cdf0e10cSrcweir     Any SAL_CALL DBSubComponentController::queryInterface(const Type& _rType) throw (RuntimeException)
230cdf0e10cSrcweir     {
231cdf0e10cSrcweir         if ( _rType.equals( XScriptInvocationContext::static_type() ) )
232cdf0e10cSrcweir         {
233cdf0e10cSrcweir             if ( m_pImpl->documentHasScriptSupport() )
234cdf0e10cSrcweir                 return makeAny( Reference< XScriptInvocationContext >( this ) );
235cdf0e10cSrcweir             return Any();
236cdf0e10cSrcweir         }
237cdf0e10cSrcweir 
238cdf0e10cSrcweir         return DBSubComponentController_Base::queryInterface( _rType );
239cdf0e10cSrcweir     }
240cdf0e10cSrcweir 
241cdf0e10cSrcweir     //--------------------------------------------------------------------
getTypes()242cdf0e10cSrcweir     Sequence< Type > SAL_CALL DBSubComponentController::getTypes(  ) throw (RuntimeException)
243cdf0e10cSrcweir     {
244cdf0e10cSrcweir         Sequence< Type > aTypes( DBSubComponentController_Base::getTypes() );
245cdf0e10cSrcweir         if ( !m_pImpl->documentHasScriptSupport() )
246cdf0e10cSrcweir         {
247cdf0e10cSrcweir             Sequence< Type > aStrippedTypes( aTypes.getLength() - 1 );
248cdf0e10cSrcweir             ::std::remove_copy_if(
249cdf0e10cSrcweir                 aTypes.getConstArray(),
250cdf0e10cSrcweir                 aTypes.getConstArray() + aTypes.getLength(),
251cdf0e10cSrcweir                 aStrippedTypes.getArray(),
252cdf0e10cSrcweir                 ::std::bind2nd( ::std::equal_to< Type >(), XScriptInvocationContext::static_type() )
253cdf0e10cSrcweir             );
254cdf0e10cSrcweir             aTypes = aStrippedTypes;
255cdf0e10cSrcweir         }
256cdf0e10cSrcweir         return aTypes;
257cdf0e10cSrcweir     }
258cdf0e10cSrcweir 
259cdf0e10cSrcweir 	//--------------------------------------------------------------------
initializeConnection(const Reference<XConnection> & _rxForeignConn)260cdf0e10cSrcweir 	void DBSubComponentController::initializeConnection( const Reference< XConnection >& _rxForeignConn )
261cdf0e10cSrcweir 	{
262cdf0e10cSrcweir 		DBG_ASSERT( !isConnected(), "DBSubComponentController::initializeConnection: not to be called when already connected!" );
263cdf0e10cSrcweir 			// usually this gets called from within initialize of derived classes ...
264cdf0e10cSrcweir 		if ( isConnected() )
265cdf0e10cSrcweir 			disconnect();
266cdf0e10cSrcweir 
267cdf0e10cSrcweir         m_pImpl->m_xConnection.reset( _rxForeignConn, SharedConnection::NoTakeOwnership );
268cdf0e10cSrcweir         m_pImpl->m_aSdbMetaData.reset( m_pImpl->m_xConnection );
269cdf0e10cSrcweir 		startConnectionListening( m_pImpl->m_xConnection );
270cdf0e10cSrcweir 
271cdf0e10cSrcweir 		// get the data source the connection belongs to
272cdf0e10cSrcweir 		try
273cdf0e10cSrcweir 		{
274cdf0e10cSrcweir             // determine our data source
275cdf0e10cSrcweir             OSL_PRECOND( !m_pImpl->m_aDataSource.is(), "DBSubComponentController::initializeConnection: already a data source in this phase?" );
276cdf0e10cSrcweir             {
277cdf0e10cSrcweir 			    Reference< XChild > xConnAsChild( m_pImpl->m_xConnection, UNO_QUERY );
278cdf0e10cSrcweir 			    Reference< XDataSource > xDS;
279cdf0e10cSrcweir 			    if ( xConnAsChild.is() )
280cdf0e10cSrcweir 				    xDS = Reference< XDataSource >( xConnAsChild->getParent(), UNO_QUERY );
281cdf0e10cSrcweir 
282cdf0e10cSrcweir 			    // (take the indirection through XDataSource to ensure we have a correct object ....)
283cdf0e10cSrcweir 			    m_pImpl->m_aDataSource = xDS;
284cdf0e10cSrcweir             }
285cdf0e10cSrcweir             OSL_POSTCOND( m_pImpl->m_aDataSource.is(), "DBSubComponentController::initializeConnection: unable to obtain the data source object!" );
286cdf0e10cSrcweir 
287cdf0e10cSrcweir             if ( m_pImpl->m_bNotAttached )
288cdf0e10cSrcweir             {
289cdf0e10cSrcweir                 Reference< XUntitledNumbers > xUntitledProvider( getDatabaseDocument(), UNO_QUERY );
290cdf0e10cSrcweir                 m_pImpl->m_nDocStartNumber = 1;
291cdf0e10cSrcweir                 if ( xUntitledProvider.is() )
292cdf0e10cSrcweir                     m_pImpl->m_nDocStartNumber = xUntitledProvider->leaseNumber( static_cast< XWeak* >( this ) );
293cdf0e10cSrcweir             }
294cdf0e10cSrcweir 
295cdf0e10cSrcweir             // determine the availability of script support in our document. Our own XScriptInvocationContext
296cdf0e10cSrcweir             // interface depends on this
297cdf0e10cSrcweir             m_pImpl->setDocumentScriptSupport( Reference< XEmbeddedScripts >( getDatabaseDocument(), UNO_QUERY ).is() );
298cdf0e10cSrcweir 
299cdf0e10cSrcweir             // get a number formatter
300cdf0e10cSrcweir             Reference< XPropertySet > xDataSourceProps( m_pImpl->m_aDataSource.getDataSourceProps(), UNO_SET_THROW );
301cdf0e10cSrcweir 			xDataSourceProps->getPropertyValue( PROPERTY_NAME ) >>= m_pImpl->m_sDataSourceName;
302cdf0e10cSrcweir 			DBG_ASSERT( m_pImpl->m_sDataSourceName.getLength(), "DBSubComponentController::initializeConnection: invalid data source name!" );
303cdf0e10cSrcweir 			Reference< XNumberFormatsSupplier> xSupplier = ::dbtools::getNumberFormats(m_pImpl->m_xConnection);
304cdf0e10cSrcweir 			if(xSupplier.is())
305cdf0e10cSrcweir 			{
306cdf0e10cSrcweir 				m_pImpl->m_xFormatter = Reference< XNumberFormatter >(getORB()
307cdf0e10cSrcweir 					->createInstance(::rtl::OUString::createFromAscii("com.sun.star.util.NumberFormatter")), UNO_QUERY);
308cdf0e10cSrcweir 				m_pImpl->m_xFormatter->attachNumberFormatsSupplier(xSupplier);
309cdf0e10cSrcweir 			}
310cdf0e10cSrcweir 			OSL_ENSURE(m_pImpl->m_xFormatter.is(),"No NumberFormatter!");
311cdf0e10cSrcweir 		}
312cdf0e10cSrcweir         catch( const Exception& )
313cdf0e10cSrcweir         {
314cdf0e10cSrcweir             DBG_UNHANDLED_EXCEPTION();
315cdf0e10cSrcweir         }
316cdf0e10cSrcweir 	}
317cdf0e10cSrcweir 
318cdf0e10cSrcweir 	//--------------------------------------------------------------------
reconnect(sal_Bool _bUI)319cdf0e10cSrcweir 	void DBSubComponentController::reconnect( sal_Bool _bUI )
320cdf0e10cSrcweir 	{
321cdf0e10cSrcweir 		OSL_ENSURE(!m_pImpl->m_bSuspended, "Cannot reconnect while suspended!");
322cdf0e10cSrcweir 
323cdf0e10cSrcweir 		stopConnectionListening( m_pImpl->m_xConnection );
324cdf0e10cSrcweir         m_pImpl->m_aSdbMetaData.reset( NULL );
325cdf0e10cSrcweir         m_pImpl->m_xConnection.clear();
326cdf0e10cSrcweir 
327cdf0e10cSrcweir 		// reconnect
328cdf0e10cSrcweir 		sal_Bool bReConnect = sal_True;
329cdf0e10cSrcweir 		if ( _bUI )
330cdf0e10cSrcweir 		{
331cdf0e10cSrcweir 			QueryBox aQuery( getView(), ModuleRes(QUERY_CONNECTION_LOST) );
332cdf0e10cSrcweir 			bReConnect = ( RET_YES == aQuery.Execute() );
333cdf0e10cSrcweir 		}
334cdf0e10cSrcweir 
335cdf0e10cSrcweir 		// now really reconnect ...
336cdf0e10cSrcweir 		if ( bReConnect )
337cdf0e10cSrcweir         {
338cdf0e10cSrcweir             m_pImpl->m_xConnection.reset( connect( m_pImpl->m_aDataSource.getDataSource(), NULL ), SharedConnection::TakeOwnership );
339cdf0e10cSrcweir             m_pImpl->m_aSdbMetaData.reset( m_pImpl->m_xConnection );
340cdf0e10cSrcweir         }
341cdf0e10cSrcweir 
342cdf0e10cSrcweir 		// invalidate all slots
343cdf0e10cSrcweir 		InvalidateAll();
344cdf0e10cSrcweir 	}
345cdf0e10cSrcweir 
346cdf0e10cSrcweir 	//--------------------------------------------------------------------
disconnect()347cdf0e10cSrcweir 	void DBSubComponentController::disconnect()
348cdf0e10cSrcweir 	{
349cdf0e10cSrcweir 		stopConnectionListening(m_pImpl->m_xConnection);
350cdf0e10cSrcweir         m_pImpl->m_aSdbMetaData.reset( NULL );
351cdf0e10cSrcweir         m_pImpl->m_xConnection.clear();
352cdf0e10cSrcweir 
353cdf0e10cSrcweir 		InvalidateAll();
354cdf0e10cSrcweir 	}
355cdf0e10cSrcweir 
356cdf0e10cSrcweir 	//--------------------------------------------------------------------
losingConnection()357cdf0e10cSrcweir 	void DBSubComponentController::losingConnection()
358cdf0e10cSrcweir 	{
359cdf0e10cSrcweir 		// our connection was disposed so we need a new one
360cdf0e10cSrcweir 		reconnect( sal_True );
361cdf0e10cSrcweir 	    InvalidateAll();
362cdf0e10cSrcweir 	}
363cdf0e10cSrcweir 
364cdf0e10cSrcweir 	//--------------------------------------------------------------------
disposing()365cdf0e10cSrcweir 	void SAL_CALL DBSubComponentController::disposing()
366cdf0e10cSrcweir 	{
367cdf0e10cSrcweir 		DBSubComponentController_Base::disposing();
368cdf0e10cSrcweir 
369cdf0e10cSrcweir         disconnect();
370cdf0e10cSrcweir 
371cdf0e10cSrcweir 		attachFrame( Reference < XFrame >() );
372cdf0e10cSrcweir 
373cdf0e10cSrcweir 		m_pImpl->m_aDataSource.clear();
374cdf0e10cSrcweir 	}
375cdf0e10cSrcweir 
376cdf0e10cSrcweir 	//--------------------------------------------------------------------
disposing(const EventObject & _rSource)377cdf0e10cSrcweir 	void SAL_CALL DBSubComponentController::disposing(const EventObject& _rSource) throw( RuntimeException )
378cdf0e10cSrcweir 	{
379cdf0e10cSrcweir         if ( _rSource.Source == getConnection() )
380cdf0e10cSrcweir         {
381cdf0e10cSrcweir 		    if (    !m_pImpl->m_bSuspended // when already suspended then we don't have to reconnect
382cdf0e10cSrcweir 			    &&	!getBroadcastHelper().bInDispose
383cdf0e10cSrcweir 			    &&	!getBroadcastHelper().bDisposed
384cdf0e10cSrcweir 			    &&	isConnected()
385cdf0e10cSrcweir 			    )
386cdf0e10cSrcweir 		    {
387cdf0e10cSrcweir 			    losingConnection();
388cdf0e10cSrcweir 		    }
389cdf0e10cSrcweir             else
390cdf0e10cSrcweir             {
391cdf0e10cSrcweir                 m_pImpl->m_xConnection.reset( m_pImpl->m_xConnection, SharedConnection::NoTakeOwnership );
392cdf0e10cSrcweir                     // this prevents the "disposeComponent" call in disconnect
393cdf0e10cSrcweir                 disconnect();
394cdf0e10cSrcweir             }
395cdf0e10cSrcweir         }
396cdf0e10cSrcweir 		else
397cdf0e10cSrcweir             DBSubComponentController_Base::disposing( _rSource );
398cdf0e10cSrcweir 	}
399cdf0e10cSrcweir 
400cdf0e10cSrcweir     //--------------------------------------------------------------------
appendError(const::rtl::OUString & _rErrorMessage,const::dbtools::StandardSQLState _eSQLState,const sal_Int32 _nErrorCode)401cdf0e10cSrcweir     void DBSubComponentController::appendError( const ::rtl::OUString& _rErrorMessage, const ::dbtools::StandardSQLState _eSQLState,
402cdf0e10cSrcweir             const sal_Int32 _nErrorCode )
403cdf0e10cSrcweir 	{
404cdf0e10cSrcweir         m_pImpl->m_aCurrentError.append( ::dbtools::SQLExceptionInfo::SQL_EXCEPTION, _rErrorMessage, getStandardSQLStateAscii( _eSQLState ),
405cdf0e10cSrcweir             _nErrorCode );
406cdf0e10cSrcweir 	}
407cdf0e10cSrcweir 	//--------------------------------------------------------------------
clearError()408cdf0e10cSrcweir 	void DBSubComponentController::clearError()
409cdf0e10cSrcweir 	{
410cdf0e10cSrcweir         m_pImpl->m_aCurrentError = ::dbtools::SQLExceptionInfo();
411cdf0e10cSrcweir 	}
412cdf0e10cSrcweir 
413cdf0e10cSrcweir 	//--------------------------------------------------------------------
hasError() const414cdf0e10cSrcweir 	sal_Bool DBSubComponentController::hasError() const
415cdf0e10cSrcweir 	{
416cdf0e10cSrcweir 		return m_pImpl->m_aCurrentError.isValid();
417cdf0e10cSrcweir 	}
418cdf0e10cSrcweir 
419cdf0e10cSrcweir 	//--------------------------------------------------------------------
getError() const420cdf0e10cSrcweir     const ::dbtools::SQLExceptionInfo& DBSubComponentController::getError() const
421cdf0e10cSrcweir     {
422cdf0e10cSrcweir         return m_pImpl->m_aCurrentError;
423cdf0e10cSrcweir     }
424cdf0e10cSrcweir 
425cdf0e10cSrcweir 	//--------------------------------------------------------------------
displayError()426cdf0e10cSrcweir     void DBSubComponentController::displayError()
427cdf0e10cSrcweir     {
428cdf0e10cSrcweir         showError( m_pImpl->m_aCurrentError );
429cdf0e10cSrcweir     }
430cdf0e10cSrcweir 
431cdf0e10cSrcweir 	//--------------------------------------------------------------------
suspend(sal_Bool bSuspend)432cdf0e10cSrcweir 	sal_Bool SAL_CALL DBSubComponentController::suspend(sal_Bool bSuspend) throw( RuntimeException )
433cdf0e10cSrcweir 	{
434cdf0e10cSrcweir 		m_pImpl->m_bSuspended = bSuspend;
435cdf0e10cSrcweir 		if ( !bSuspend && !isConnected() )
436cdf0e10cSrcweir 			reconnect(sal_True);
437cdf0e10cSrcweir 
438cdf0e10cSrcweir 
439cdf0e10cSrcweir 		return sal_True;
440cdf0e10cSrcweir 	}
441cdf0e10cSrcweir 
442cdf0e10cSrcweir     // -----------------------------------------------------------------------------
attachModel(const Reference<XModel> & _rxModel)443cdf0e10cSrcweir     sal_Bool SAL_CALL DBSubComponentController::attachModel( const Reference< XModel > & _rxModel) throw( RuntimeException )
444cdf0e10cSrcweir     {
445cdf0e10cSrcweir         if ( !_rxModel.is() )
446cdf0e10cSrcweir             return sal_False;
447cdf0e10cSrcweir         if ( !DBSubComponentController_Base::attachModel( _rxModel ) )
448cdf0e10cSrcweir             return sal_False;
449cdf0e10cSrcweir 
450cdf0e10cSrcweir         m_pImpl->m_bNotAttached = false;
451cdf0e10cSrcweir         if ( m_pImpl->m_nDocStartNumber == 1 )
452cdf0e10cSrcweir             releaseNumberForComponent();
453cdf0e10cSrcweir 
454cdf0e10cSrcweir         Reference< XUntitledNumbers > xUntitledProvider( _rxModel, UNO_QUERY );
455cdf0e10cSrcweir         m_pImpl->m_nDocStartNumber = 1;
456cdf0e10cSrcweir         if ( xUntitledProvider.is() )
457cdf0e10cSrcweir             m_pImpl->m_nDocStartNumber = xUntitledProvider->leaseNumber( static_cast< XWeak* >( this ) );
458cdf0e10cSrcweir 
459cdf0e10cSrcweir 		return sal_True;
460cdf0e10cSrcweir     }
461cdf0e10cSrcweir 
462cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
Execute(sal_uInt16 _nId,const Sequence<PropertyValue> & _rArgs)463cdf0e10cSrcweir 	void DBSubComponentController::Execute(sal_uInt16 _nId, const Sequence< PropertyValue >& _rArgs)
464cdf0e10cSrcweir 	{
465cdf0e10cSrcweir         if ( _nId == ID_BROWSER_CLOSE )
466cdf0e10cSrcweir         {
467cdf0e10cSrcweir             closeTask();
468cdf0e10cSrcweir             return;
469cdf0e10cSrcweir         }
470cdf0e10cSrcweir 
471cdf0e10cSrcweir         DBSubComponentController_Base::Execute( _nId, _rArgs );
472cdf0e10cSrcweir 		InvalidateFeature( _nId );
473cdf0e10cSrcweir 	}
474cdf0e10cSrcweir 
475cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
getDataSourceName() const476cdf0e10cSrcweir 	::rtl::OUString DBSubComponentController::getDataSourceName() const
477cdf0e10cSrcweir 	{
478cdf0e10cSrcweir 		::rtl::OUString sName;
479cdf0e10cSrcweir         Reference< XPropertySet > xDataSourceProps( m_pImpl->m_aDataSource.getDataSourceProps() );
480cdf0e10cSrcweir 		if ( xDataSourceProps.is() )
481cdf0e10cSrcweir 			xDataSourceProps->getPropertyValue(PROPERTY_NAME) >>= sName;
482cdf0e10cSrcweir 		return sName;
483cdf0e10cSrcweir 	}
484cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
connectionLostMessage() const485cdf0e10cSrcweir 	void DBSubComponentController::connectionLostMessage() const
486cdf0e10cSrcweir 	{
487cdf0e10cSrcweir 		String aMessage(ModuleRes(RID_STR_CONNECTION_LOST));
488cdf0e10cSrcweir 		Reference< XWindow > xWindow = getTopMostContainerWindow();
489cdf0e10cSrcweir 		Window* pWin = NULL;
490cdf0e10cSrcweir 		if ( xWindow.is() )
491cdf0e10cSrcweir 			pWin = VCLUnoHelper::GetWindow(xWindow);
492cdf0e10cSrcweir 		if ( !pWin )
493cdf0e10cSrcweir 			pWin = getView()->Window::GetParent();
494cdf0e10cSrcweir 
495cdf0e10cSrcweir 		InfoBox(pWin, aMessage).Execute();
496cdf0e10cSrcweir 	}
497cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
getConnection() const498cdf0e10cSrcweir 	const Reference< XConnection >& DBSubComponentController::getConnection() const
499cdf0e10cSrcweir 	{
500cdf0e10cSrcweir 		return m_pImpl->m_xConnection;
501cdf0e10cSrcweir 	}
502cdf0e10cSrcweir 
503cdf0e10cSrcweir     // -----------------------------------------------------------------------------
isReadOnly() const504cdf0e10cSrcweir     sal_Bool DBSubComponentController::isReadOnly() const
505cdf0e10cSrcweir     {
506cdf0e10cSrcweir         return !m_pImpl->m_bEditable;
507cdf0e10cSrcweir     }
508cdf0e10cSrcweir 
509cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
isEditable() const510cdf0e10cSrcweir 	sal_Bool DBSubComponentController::isEditable() const
511cdf0e10cSrcweir     {
512cdf0e10cSrcweir         return m_pImpl->m_bEditable;
513cdf0e10cSrcweir     }
514cdf0e10cSrcweir 
515cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
setEditable(sal_Bool _bEditable)516cdf0e10cSrcweir     void DBSubComponentController::setEditable(sal_Bool _bEditable)
517cdf0e10cSrcweir     {
518cdf0e10cSrcweir         m_pImpl->m_bEditable = _bEditable;
519cdf0e10cSrcweir     }
520cdf0e10cSrcweir 
521cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
getSdbMetaData() const522cdf0e10cSrcweir     const ::dbtools::DatabaseMetaData& DBSubComponentController::getSdbMetaData() const
523cdf0e10cSrcweir     {
524cdf0e10cSrcweir         return m_pImpl->m_aSdbMetaData;
525cdf0e10cSrcweir     }
526cdf0e10cSrcweir 
527cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
isConnected() const528cdf0e10cSrcweir     sal_Bool DBSubComponentController::isConnected() const
529cdf0e10cSrcweir     {
530cdf0e10cSrcweir         return m_pImpl->m_xConnection.is();
531cdf0e10cSrcweir     }
532cdf0e10cSrcweir 
533cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
getMetaData() const534cdf0e10cSrcweir     Reference< XDatabaseMetaData > DBSubComponentController::getMetaData( ) const
535cdf0e10cSrcweir     {
536cdf0e10cSrcweir         Reference< XDatabaseMetaData > xMeta;
537cdf0e10cSrcweir         try
538cdf0e10cSrcweir         {
539cdf0e10cSrcweir             if ( isConnected() )
540cdf0e10cSrcweir                 xMeta.set( m_pImpl->m_xConnection->getMetaData(), UNO_SET_THROW );
541cdf0e10cSrcweir         }
542cdf0e10cSrcweir         catch( const Exception& )
543cdf0e10cSrcweir         {
544cdf0e10cSrcweir         	DBG_UNHANDLED_EXCEPTION();
545cdf0e10cSrcweir         }
546cdf0e10cSrcweir         return xMeta;
547cdf0e10cSrcweir     }
548cdf0e10cSrcweir 
549cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
getDataSource() const550cdf0e10cSrcweir     const Reference< XPropertySet >& DBSubComponentController::getDataSource() const
551cdf0e10cSrcweir     {
552cdf0e10cSrcweir         return m_pImpl->m_aDataSource.getDataSourceProps();
553cdf0e10cSrcweir     }
554cdf0e10cSrcweir 
555cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
haveDataSource() const556cdf0e10cSrcweir     sal_Bool DBSubComponentController::haveDataSource() const
557cdf0e10cSrcweir     {
558cdf0e10cSrcweir         return m_pImpl->m_aDataSource.is();
559cdf0e10cSrcweir     }
560cdf0e10cSrcweir 
561cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
getDatabaseDocument() const562cdf0e10cSrcweir     Reference< XModel > DBSubComponentController::getDatabaseDocument() const
563cdf0e10cSrcweir     {
564cdf0e10cSrcweir         return Reference< XModel >( m_pImpl->m_aDataSource.getDatabaseDocument(), UNO_QUERY );
565cdf0e10cSrcweir     }
566cdf0e10cSrcweir 
567cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
getNumberFormatter() const568cdf0e10cSrcweir     Reference< XNumberFormatter > DBSubComponentController::getNumberFormatter() const
569cdf0e10cSrcweir     {
570cdf0e10cSrcweir         return m_pImpl->m_xFormatter;
571cdf0e10cSrcweir     }
572cdf0e10cSrcweir 
573cdf0e10cSrcweir     // -----------------------------------------------------------------------------
getPrivateModel() const574cdf0e10cSrcweir     Reference< XModel > DBSubComponentController::getPrivateModel() const
575cdf0e10cSrcweir     {
576cdf0e10cSrcweir 		return getDatabaseDocument();
577cdf0e10cSrcweir     }
578cdf0e10cSrcweir     // -----------------------------------------------------------------------------
579cdf0e10cSrcweir     // XTitle
getTitle()580cdf0e10cSrcweir     ::rtl::OUString SAL_CALL DBSubComponentController::getTitle()
581cdf0e10cSrcweir         throw (RuntimeException)
582cdf0e10cSrcweir     {
583cdf0e10cSrcweir         ::osl::MutexGuard aGuard( getMutex() );
584cdf0e10cSrcweir         if ( m_bExternalTitle )
585cdf0e10cSrcweir             return impl_getTitleHelper_throw()->getTitle ();
586cdf0e10cSrcweir 
587cdf0e10cSrcweir         ::rtl::OUStringBuffer sTitle;
588cdf0e10cSrcweir         Reference< XTitle > xTitle(getPrivateModel(),UNO_QUERY);
589cdf0e10cSrcweir         if ( xTitle.is() )
590cdf0e10cSrcweir         {
591cdf0e10cSrcweir             sTitle.append( xTitle->getTitle() );
592cdf0e10cSrcweir             sTitle.appendAscii(" : ");
593cdf0e10cSrcweir         }
594cdf0e10cSrcweir         sTitle.append( getPrivateTitle() );
595cdf0e10cSrcweir         return sTitle.makeStringAndClear();
596cdf0e10cSrcweir     }
597cdf0e10cSrcweir 
598cdf0e10cSrcweir     // -----------------------------------------------------------------------------
getCurrentStartNumber() const599cdf0e10cSrcweir     sal_Int32 DBSubComponentController::getCurrentStartNumber() const
600cdf0e10cSrcweir     {
601cdf0e10cSrcweir         return m_pImpl->m_nDocStartNumber;
602cdf0e10cSrcweir     }
603cdf0e10cSrcweir 
604cdf0e10cSrcweir     // -----------------------------------------------------------------------------
getScriptContainer()605cdf0e10cSrcweir     Reference< XEmbeddedScripts > SAL_CALL DBSubComponentController::getScriptContainer() throw (RuntimeException)
606cdf0e10cSrcweir     {
607cdf0e10cSrcweir         ::osl::MutexGuard aGuard( getMutex() );
608cdf0e10cSrcweir         if ( !m_pImpl->documentHasScriptSupport() )
609cdf0e10cSrcweir             return NULL;
610cdf0e10cSrcweir 
611cdf0e10cSrcweir         return Reference< XEmbeddedScripts >( getDatabaseDocument(), UNO_QUERY_THROW );
612cdf0e10cSrcweir     }
613cdf0e10cSrcweir 
614cdf0e10cSrcweir     // -----------------------------------------------------------------------------
addModifyListener(const Reference<XModifyListener> & i_Listener)615cdf0e10cSrcweir     void SAL_CALL DBSubComponentController::addModifyListener( const Reference< XModifyListener >& i_Listener ) throw (RuntimeException)
616cdf0e10cSrcweir     {
617cdf0e10cSrcweir         ::osl::MutexGuard aGuard( getMutex() );
618cdf0e10cSrcweir         m_pImpl->m_aModifyListeners.addInterface( i_Listener );
619cdf0e10cSrcweir     }
620cdf0e10cSrcweir 
621cdf0e10cSrcweir     // -----------------------------------------------------------------------------
removeModifyListener(const Reference<XModifyListener> & i_Listener)622cdf0e10cSrcweir     void SAL_CALL DBSubComponentController::removeModifyListener( const Reference< XModifyListener >& i_Listener ) throw (RuntimeException)
623cdf0e10cSrcweir     {
624cdf0e10cSrcweir         ::osl::MutexGuard aGuard( getMutex() );
625cdf0e10cSrcweir         m_pImpl->m_aModifyListeners.removeInterface( i_Listener );
626cdf0e10cSrcweir     }
627cdf0e10cSrcweir 
628cdf0e10cSrcweir     // -----------------------------------------------------------------------------
isModified()629cdf0e10cSrcweir     ::sal_Bool SAL_CALL DBSubComponentController::isModified(  ) throw (RuntimeException)
630cdf0e10cSrcweir     {
631cdf0e10cSrcweir         ::osl::MutexGuard aGuard( getMutex() );
632cdf0e10cSrcweir         return impl_isModified();
633cdf0e10cSrcweir     }
634cdf0e10cSrcweir 
635cdf0e10cSrcweir     // -----------------------------------------------------------------------------
setModified(::sal_Bool i_bModified)636cdf0e10cSrcweir     void SAL_CALL DBSubComponentController::setModified( ::sal_Bool i_bModified ) throw (PropertyVetoException, RuntimeException)
637cdf0e10cSrcweir     {
638cdf0e10cSrcweir         ::osl::ClearableMutexGuard aGuard( getMutex() );
639cdf0e10cSrcweir 
640cdf0e10cSrcweir         if ( m_pImpl->m_bModified == i_bModified )
641cdf0e10cSrcweir             return;
642cdf0e10cSrcweir 
643cdf0e10cSrcweir 		m_pImpl->m_bModified = i_bModified;
644cdf0e10cSrcweir         impl_onModifyChanged();
645cdf0e10cSrcweir 
646cdf0e10cSrcweir         EventObject aEvent( *this );
647cdf0e10cSrcweir         aGuard.clear();
648cdf0e10cSrcweir         m_pImpl->m_aModifyListeners.notifyEach( &XModifyListener::modified, aEvent );
649cdf0e10cSrcweir     }
650cdf0e10cSrcweir 
651cdf0e10cSrcweir     // -----------------------------------------------------------------------------
impl_isModified() const652cdf0e10cSrcweir     sal_Bool DBSubComponentController::impl_isModified() const
653cdf0e10cSrcweir     {
654cdf0e10cSrcweir         return m_pImpl->m_bModified;
655cdf0e10cSrcweir     }
656cdf0e10cSrcweir 
657cdf0e10cSrcweir     // -----------------------------------------------------------------------------
impl_onModifyChanged()658cdf0e10cSrcweir     void DBSubComponentController::impl_onModifyChanged()
659cdf0e10cSrcweir     {
660cdf0e10cSrcweir         InvalidateFeature( ID_BROWSER_SAVEDOC );
661cdf0e10cSrcweir         if ( isFeatureSupported( ID_BROWSER_SAVEASDOC ) )
662cdf0e10cSrcweir             InvalidateFeature( ID_BROWSER_SAVEASDOC );
663cdf0e10cSrcweir     }
664cdf0e10cSrcweir 
665cdf0e10cSrcweir //........................................................................
666cdf0e10cSrcweir }	// namespace dbaui
667cdf0e10cSrcweir //........................................................................
668cdf0e10cSrcweir 
669