196de5490SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
396de5490SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
496de5490SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
596de5490SAndrew Rist  * distributed with this work for additional information
696de5490SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
796de5490SAndrew Rist  * to you under the Apache License, Version 2.0 (the
896de5490SAndrew Rist  * "License"); you may not use this file except in compliance
996de5490SAndrew Rist  * with the License.  You may obtain a copy of the License at
1096de5490SAndrew Rist  *
1196de5490SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
1296de5490SAndrew Rist  *
1396de5490SAndrew Rist  * Unless required by applicable law or agreed to in writing,
1496de5490SAndrew Rist  * software distributed under the License is distributed on an
1596de5490SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1696de5490SAndrew Rist  * KIND, either express or implied.  See the License for the
1796de5490SAndrew Rist  * specific language governing permissions and limitations
1896de5490SAndrew Rist  * under the License.
1996de5490SAndrew Rist  *
2096de5490SAndrew Rist  *************************************************************/
2196de5490SAndrew Rist 
2296de5490SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25*b63233d8Sdamjan #include "precompiled_dbui.hxx"
26cdf0e10cSrcweir #ifndef DBAUI_TABLEWINDOWDATA_HXX
27cdf0e10cSrcweir #include "TableWindowData.hxx"
28cdf0e10cSrcweir #endif
29cdf0e10cSrcweir #ifndef _TOOLS_DEBUG_HXX
30cdf0e10cSrcweir #include <tools/debug.hxx>
31cdf0e10cSrcweir #endif
32cdf0e10cSrcweir #include <com/sun/star/sdb/XQueriesSupplier.hpp>
33cdf0e10cSrcweir #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
34cdf0e10cSrcweir #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
35cdf0e10cSrcweir #include <com/sun/star/sdbcx/XKeysSupplier.hpp>
36cdf0e10cSrcweir #include <com/sun/star/container/XNameAccess.hpp>
37cdf0e10cSrcweir #include <com/sun/star/container/XIndexAccess.hpp>
38cdf0e10cSrcweir 
39cdf0e10cSrcweir using namespace dbaui;
40cdf0e10cSrcweir using namespace ::com::sun::star::lang;
41cdf0e10cSrcweir using namespace ::com::sun::star::uno;
42cdf0e10cSrcweir using namespace ::com::sun::star::sdb;
43cdf0e10cSrcweir using namespace ::com::sun::star::sdbc;
44cdf0e10cSrcweir using namespace ::com::sun::star::sdbcx;
45cdf0e10cSrcweir using namespace ::com::sun::star::beans;
46cdf0e10cSrcweir using namespace ::com::sun::star::container;
47cdf0e10cSrcweir 
48cdf0e10cSrcweir //==================================================================
49cdf0e10cSrcweir // class OTableWindowData
50cdf0e10cSrcweir //==================================================================
DBG_NAME(OTableWindowData)51cdf0e10cSrcweir DBG_NAME(OTableWindowData)
52cdf0e10cSrcweir //------------------------------------------------------------------------------
53cdf0e10cSrcweir OTableWindowData::OTableWindowData( const Reference< XPropertySet>& _xTable
54cdf0e10cSrcweir                                    ,const ::rtl::OUString& _rComposedName
55cdf0e10cSrcweir                                    ,const ::rtl::OUString& rTableName
56cdf0e10cSrcweir                                    ,const ::rtl::OUString& rWinName )
57cdf0e10cSrcweir     :m_xTable(_xTable)
58cdf0e10cSrcweir     ,m_aTableName( rTableName )
59cdf0e10cSrcweir 	,m_aWinName( rWinName )
60cdf0e10cSrcweir 	,m_sComposedName(_rComposedName)
61cdf0e10cSrcweir 	,m_aPosition( Point(-1,-1) )
62cdf0e10cSrcweir 	,m_aSize( Size(-1,-1) )
63cdf0e10cSrcweir 	,m_bShowAll( sal_True )
64cdf0e10cSrcweir     ,m_bIsQuery(false)
65cdf0e10cSrcweir     ,m_bIsValid(true)
66cdf0e10cSrcweir {
67cdf0e10cSrcweir 	DBG_CTOR(OTableWindowData,NULL);
68cdf0e10cSrcweir 	if( !m_aWinName.getLength() )
69cdf0e10cSrcweir 		m_aWinName = m_aTableName;
70cdf0e10cSrcweir 
71cdf0e10cSrcweir     listen();
72cdf0e10cSrcweir }
73cdf0e10cSrcweir 
74cdf0e10cSrcweir //------------------------------------------------------------------------------
~OTableWindowData()75cdf0e10cSrcweir OTableWindowData::~OTableWindowData()
76cdf0e10cSrcweir {
77cdf0e10cSrcweir 	DBG_DTOR(OTableWindowData,NULL);
78cdf0e10cSrcweir     Reference<XComponent> xComponent( m_xTable, UNO_QUERY );
79cdf0e10cSrcweir 	if ( xComponent.is() )
80cdf0e10cSrcweir 		stopComponentListening( xComponent );
81cdf0e10cSrcweir }
82cdf0e10cSrcweir 
83cdf0e10cSrcweir //------------------------------------------------------------------------------
HasPosition() const84cdf0e10cSrcweir sal_Bool OTableWindowData::HasPosition() const
85cdf0e10cSrcweir {
86cdf0e10cSrcweir 	return ( (m_aPosition.X() != -1) && (m_aPosition.Y() != -1) );
87cdf0e10cSrcweir }
88cdf0e10cSrcweir 
89cdf0e10cSrcweir //------------------------------------------------------------------------------
HasSize() const90cdf0e10cSrcweir sal_Bool OTableWindowData::HasSize() const
91cdf0e10cSrcweir {
92cdf0e10cSrcweir 	return ( (m_aSize.Width() != -1) && (m_aSize.Height() !=-1) );
93cdf0e10cSrcweir }
94cdf0e10cSrcweir // -----------------------------------------------------------------------------
_disposing(const::com::sun::star::lang::EventObject &)95cdf0e10cSrcweir void OTableWindowData::_disposing( const ::com::sun::star::lang::EventObject& /*_rSource*/ )
96cdf0e10cSrcweir {
97cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
98cdf0e10cSrcweir 	// it doesn't matter which one was disposed
99cdf0e10cSrcweir     m_xColumns.clear();
100cdf0e10cSrcweir     m_xKeys.clear();
101cdf0e10cSrcweir 	m_xTable.clear();
102cdf0e10cSrcweir }
103cdf0e10cSrcweir // -----------------------------------------------------------------------------
init(const Reference<XConnection> & _xConnection,bool _bAllowQueries)104cdf0e10cSrcweir bool OTableWindowData::init(const Reference< XConnection  >& _xConnection,bool _bAllowQueries)
105cdf0e10cSrcweir {
106cdf0e10cSrcweir     OSL_ENSURE(!m_xTable.is(),"We are already connected to a table!");
107cdf0e10cSrcweir 
108cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
109cdf0e10cSrcweir 
110cdf0e10cSrcweir 	Reference< XQueriesSupplier > xSupQueries( _xConnection, UNO_QUERY_THROW );
111cdf0e10cSrcweir     Reference< XNameAccess > xQueries( xSupQueries->getQueries(), UNO_QUERY_THROW );
112cdf0e10cSrcweir     bool bIsKnownQuery = _bAllowQueries && xQueries->hasByName( m_sComposedName );
113cdf0e10cSrcweir 
114cdf0e10cSrcweir     Reference< XTablesSupplier > xSupTables( _xConnection, UNO_QUERY_THROW );
115cdf0e10cSrcweir     Reference< XNameAccess > xTables( xSupTables->getTables(), UNO_QUERY_THROW );
116cdf0e10cSrcweir     bool bIsKnownTable = xTables->hasByName( m_sComposedName );
117cdf0e10cSrcweir 
118cdf0e10cSrcweir     if ( bIsKnownQuery )
119cdf0e10cSrcweir         m_xTable.set( xQueries->getByName( m_sComposedName ), UNO_QUERY );
120cdf0e10cSrcweir     else if ( bIsKnownTable )
121cdf0e10cSrcweir         m_xTable.set( xTables->getByName( m_sComposedName ), UNO_QUERY );
122cdf0e10cSrcweir     else
123cdf0e10cSrcweir         m_bIsValid = false;
124cdf0e10cSrcweir 
125cdf0e10cSrcweir     // if we survived so far, we know whether it's a query
126cdf0e10cSrcweir     m_bIsQuery = bIsKnownQuery;
127cdf0e10cSrcweir 
128cdf0e10cSrcweir     listen();
129cdf0e10cSrcweir 
130cdf0e10cSrcweir     Reference< XIndexAccess > xColumnsAsIndex( m_xColumns,UNO_QUERY );
131cdf0e10cSrcweir     return xColumnsAsIndex.is() && ( xColumnsAsIndex->getCount() > 0 );
132cdf0e10cSrcweir }
133cdf0e10cSrcweir // -----------------------------------------------------------------------------
listen()134cdf0e10cSrcweir void OTableWindowData::listen()
135cdf0e10cSrcweir {
136cdf0e10cSrcweir     if ( m_xTable.is() )
137cdf0e10cSrcweir     {
138cdf0e10cSrcweir         // listen for the object being disposed
139cdf0e10cSrcweir 	    Reference<XComponent> xComponent( m_xTable, UNO_QUERY );
140cdf0e10cSrcweir 	    if ( xComponent.is() )
141cdf0e10cSrcweir 		    startComponentListening( xComponent );
142cdf0e10cSrcweir 
143cdf0e10cSrcweir         // obtain the columns
144cdf0e10cSrcweir         Reference< XColumnsSupplier > xColumnsSups( m_xTable, UNO_QUERY);
145cdf0e10cSrcweir         if ( xColumnsSups.is() )
146cdf0e10cSrcweir 	        m_xColumns = xColumnsSups->getColumns();
147cdf0e10cSrcweir 
148cdf0e10cSrcweir         Reference<XKeysSupplier> xKeySup(m_xTable,UNO_QUERY);
149cdf0e10cSrcweir 	    if ( xKeySup.is() )
150cdf0e10cSrcweir 		    m_xKeys = xKeySup->getKeys();
151cdf0e10cSrcweir     }
152cdf0e10cSrcweir }
153cdf0e10cSrcweir // -----------------------------------------------------------------------------
154