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 
27cdf0e10cSrcweir #include "dbexchange.hxx"
28cdf0e10cSrcweir #include <sot/formats.hxx>
29cdf0e10cSrcweir #include <sot/storage.hxx>
30cdf0e10cSrcweir #include <osl/diagnose.h>
31cdf0e10cSrcweir #include <com/sun/star/sdb/CommandType.hpp>
32cdf0e10cSrcweir #include <com/sun/star/sdb/XResultSetAccess.hpp>
33cdf0e10cSrcweir #include "TokenWriter.hxx"
34cdf0e10cSrcweir #include "dbustrings.hrc"
35cdf0e10cSrcweir #include <comphelper/uno3.hxx>
36cdf0e10cSrcweir #include <svx/dataaccessdescriptor.hxx>
37cdf0e10cSrcweir #include "UITools.hxx"
38cdf0e10cSrcweir 
39cdf0e10cSrcweir 
40cdf0e10cSrcweir namespace dbaui
41cdf0e10cSrcweir {
42cdf0e10cSrcweir 	using namespace ::com::sun::star::uno;
43cdf0e10cSrcweir 	using namespace ::com::sun::star::beans;
44cdf0e10cSrcweir 	using namespace ::com::sun::star::sdb;
45cdf0e10cSrcweir 	using namespace ::com::sun::star::beans;
46cdf0e10cSrcweir 	using namespace ::com::sun::star::lang;
47cdf0e10cSrcweir 	using namespace ::com::sun::star::util;
48cdf0e10cSrcweir 	using namespace ::com::sun::star::sdbc;
49cdf0e10cSrcweir 	using namespace ::com::sun::star::datatransfer;
50cdf0e10cSrcweir 	using namespace ::svx;
51cdf0e10cSrcweir 
52cdf0e10cSrcweir 	namespace
53cdf0e10cSrcweir 	{
lcl_setListener(const Reference<T> & _xComponent,const Reference<XEventListener> & i_rListener,const bool i_bAdd)54cdf0e10cSrcweir 		template<class T > void lcl_setListener(const Reference<T>& _xComponent, const Reference< XEventListener >& i_rListener, const bool i_bAdd )
55cdf0e10cSrcweir 		{
56cdf0e10cSrcweir 			if ( !_xComponent.is() )
57cdf0e10cSrcweir                 return;
58cdf0e10cSrcweir 
59cdf0e10cSrcweir             Reference< XComponent> xCom( _xComponent, UNO_QUERY );
60cdf0e10cSrcweir             OSL_ENSURE( xCom.is(), "lcl_setListener: no component!" );
61cdf0e10cSrcweir 			if ( !xCom.is() )
62cdf0e10cSrcweir                 return;
63cdf0e10cSrcweir 
64cdf0e10cSrcweir             i_bAdd ? xCom->addEventListener( i_rListener ) : xCom->removeEventListener( i_rListener );
65cdf0e10cSrcweir 		}
66cdf0e10cSrcweir 	}
67cdf0e10cSrcweir 
68cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
ODataClipboard(const::rtl::OUString & _rDatasource,const sal_Int32 _nCommandType,const::rtl::OUString & _rCommand,const Reference<XConnection> & _rxConnection,const Reference<XNumberFormatter> & _rxFormatter,const Reference<XMultiServiceFactory> & _rxORB)69cdf0e10cSrcweir 	ODataClipboard::ODataClipboard(
70cdf0e10cSrcweir 					const ::rtl::OUString&	_rDatasource,
71cdf0e10cSrcweir 					const sal_Int32			_nCommandType,
72cdf0e10cSrcweir 					const ::rtl::OUString&	_rCommand,
73cdf0e10cSrcweir 					const Reference< XConnection >& _rxConnection,
74cdf0e10cSrcweir 					const Reference< XNumberFormatter >& _rxFormatter,
75cdf0e10cSrcweir 					const Reference< XMultiServiceFactory >& _rxORB)
76cdf0e10cSrcweir 					:ODataAccessObjectTransferable( _rDatasource,::rtl::OUString(), _nCommandType, _rCommand, _rxConnection )
77cdf0e10cSrcweir 		,m_pHtml(NULL)
78cdf0e10cSrcweir 		,m_pRtf(NULL)
79cdf0e10cSrcweir 	{
80cdf0e10cSrcweir 		osl_incrementInterlockedCount( &m_refCount );
81cdf0e10cSrcweir 		lcl_setListener( _rxConnection, this, true );
82cdf0e10cSrcweir 
83cdf0e10cSrcweir 		m_pHtml.set( new OHTMLImportExport( getDescriptor(), _rxORB, _rxFormatter ) );
84cdf0e10cSrcweir 		m_pRtf.set( new ORTFImportExport( getDescriptor(), _rxORB, _rxFormatter ) );
85cdf0e10cSrcweir 
86cdf0e10cSrcweir         osl_decrementInterlockedCount( &m_refCount );
87cdf0e10cSrcweir 	}
88cdf0e10cSrcweir 
89cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
ODataClipboard(const::rtl::OUString & _rDatasource,const sal_Int32 _nCommandType,const::rtl::OUString & _rCommand,const Reference<XNumberFormatter> & _rxFormatter,const Reference<XMultiServiceFactory> & _rxORB)90cdf0e10cSrcweir 	ODataClipboard::ODataClipboard(
91cdf0e10cSrcweir 					const ::rtl::OUString&	_rDatasource,
92cdf0e10cSrcweir 					const sal_Int32			_nCommandType,
93cdf0e10cSrcweir 					const ::rtl::OUString&	_rCommand,
94cdf0e10cSrcweir 					const Reference< XNumberFormatter >& _rxFormatter,
95cdf0e10cSrcweir 					const Reference< XMultiServiceFactory >& _rxORB)
96cdf0e10cSrcweir 		:ODataAccessObjectTransferable( _rDatasource, ::rtl::OUString(),_nCommandType, _rCommand)
97cdf0e10cSrcweir 		,m_pHtml(NULL)
98cdf0e10cSrcweir 		,m_pRtf(NULL)
99cdf0e10cSrcweir 	{
100cdf0e10cSrcweir 		m_pHtml.set( new OHTMLImportExport( getDescriptor(),_rxORB, _rxFormatter ) );
101cdf0e10cSrcweir 		m_pRtf.set( new ORTFImportExport( getDescriptor(),_rxORB, _rxFormatter ) );
102cdf0e10cSrcweir 	}
103cdf0e10cSrcweir 
104cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
ODataClipboard(const Reference<XPropertySet> & i_rAliveForm,const Sequence<Any> & i_rSelectedRows,const sal_Bool i_bBookmarkSelection,const Reference<XMultiServiceFactory> & i_rORB)105cdf0e10cSrcweir     ODataClipboard::ODataClipboard(	const Reference< XPropertySet >& i_rAliveForm,
106cdf0e10cSrcweir                                     const Sequence< Any >& i_rSelectedRows,
107cdf0e10cSrcweir                                     const sal_Bool i_bBookmarkSelection,
108cdf0e10cSrcweir                                     const Reference< XMultiServiceFactory >& i_rORB )
109cdf0e10cSrcweir         :ODataAccessObjectTransferable( i_rAliveForm )
110cdf0e10cSrcweir         ,m_pHtml(NULL)
111cdf0e10cSrcweir         ,m_pRtf(NULL)
112cdf0e10cSrcweir     {
113cdf0e10cSrcweir         OSL_PRECOND( i_rORB.is(), "ODataClipboard::ODataClipboard: having no factory is not good ..." );
114cdf0e10cSrcweir 
115cdf0e10cSrcweir         osl_incrementInterlockedCount( &m_refCount );
116cdf0e10cSrcweir 
117cdf0e10cSrcweir         Reference<XConnection> xConnection;
118cdf0e10cSrcweir         getDescriptor()[ daConnection ] >>= xConnection;
119cdf0e10cSrcweir         lcl_setListener( xConnection, this, true );
120cdf0e10cSrcweir 
121cdf0e10cSrcweir         // do not pass the form itself as source result set, since the client might operate on the form, which
122cdf0e10cSrcweir         // might lead to undesired effects. Instead, use a clone.
123cdf0e10cSrcweir         Reference< XResultSet > xResultSetClone;
124cdf0e10cSrcweir 		Reference< XResultSetAccess > xResultSetAccess( i_rAliveForm, UNO_QUERY );
125cdf0e10cSrcweir 		if ( xResultSetAccess.is() )
126cdf0e10cSrcweir 			xResultSetClone = xResultSetAccess->createResultSet();
127cdf0e10cSrcweir         OSL_ENSURE( xResultSetClone.is(), "ODataClipboard::ODataClipboard: could not clone the form's result set" );
128cdf0e10cSrcweir         lcl_setListener( xResultSetClone, this, true );
129cdf0e10cSrcweir 
130cdf0e10cSrcweir         getDescriptor()[daCursor]			<<= xResultSetClone;
131cdf0e10cSrcweir         getDescriptor()[daSelection]		<<= i_rSelectedRows;
132cdf0e10cSrcweir         getDescriptor()[daBookmarkSelection]<<= i_bBookmarkSelection;
133cdf0e10cSrcweir         addCompatibleSelectionDescription( i_rSelectedRows );
134cdf0e10cSrcweir 
135cdf0e10cSrcweir         if ( xConnection.is() && i_rORB.is() )
136cdf0e10cSrcweir         {
137cdf0e10cSrcweir             Reference< XNumberFormatter > xFormatter( getNumberFormatter( xConnection, i_rORB ) );
138cdf0e10cSrcweir             if ( xFormatter.is() )
139cdf0e10cSrcweir             {
140cdf0e10cSrcweir                 m_pHtml.set( new OHTMLImportExport( getDescriptor(), i_rORB, xFormatter ) );
141cdf0e10cSrcweir                 m_pRtf.set( new ORTFImportExport( getDescriptor(), i_rORB, xFormatter ) );
142cdf0e10cSrcweir             }
143cdf0e10cSrcweir         }
144cdf0e10cSrcweir 
145cdf0e10cSrcweir         osl_decrementInterlockedCount( &m_refCount );
146cdf0e10cSrcweir     }
147cdf0e10cSrcweir 
148cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
WriteObject(SotStorageStreamRef & rxOStm,void * pUserObject,sal_uInt32 nUserObjectId,const::com::sun::star::datatransfer::DataFlavor &)149cdf0e10cSrcweir 	sal_Bool ODataClipboard::WriteObject( SotStorageStreamRef& rxOStm, void* pUserObject, sal_uInt32 nUserObjectId, const ::com::sun::star::datatransfer::DataFlavor& /*rFlavor*/ )
150cdf0e10cSrcweir 	{
151cdf0e10cSrcweir 		if (nUserObjectId == SOT_FORMAT_RTF || nUserObjectId == SOT_FORMATSTR_ID_HTML )
152cdf0e10cSrcweir 		{
153cdf0e10cSrcweir 			ODatabaseImportExport* pExport = reinterpret_cast<ODatabaseImportExport*>(pUserObject);
154cdf0e10cSrcweir 			if ( pExport && rxOStm.Is() )
155cdf0e10cSrcweir 			{
156cdf0e10cSrcweir 				pExport->setStream(&rxOStm);
157cdf0e10cSrcweir 				return pExport->Write();
158cdf0e10cSrcweir 			}
159cdf0e10cSrcweir 		}
160cdf0e10cSrcweir 		return sal_False;
161cdf0e10cSrcweir 	}
162cdf0e10cSrcweir 
163cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
AddSupportedFormats()164cdf0e10cSrcweir 	void ODataClipboard::AddSupportedFormats()
165cdf0e10cSrcweir 	{
166cdf0e10cSrcweir 		if ( m_pRtf.is() )
167cdf0e10cSrcweir 			AddFormat( SOT_FORMAT_RTF );
168cdf0e10cSrcweir 
169cdf0e10cSrcweir 		if ( m_pHtml.is() )
170cdf0e10cSrcweir 			AddFormat( SOT_FORMATSTR_ID_HTML );
171cdf0e10cSrcweir 
172cdf0e10cSrcweir 		ODataAccessObjectTransferable::AddSupportedFormats();
173cdf0e10cSrcweir 	}
174cdf0e10cSrcweir 
175cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
GetData(const DataFlavor & rFlavor)176cdf0e10cSrcweir 	sal_Bool ODataClipboard::GetData( const DataFlavor& rFlavor )
177cdf0e10cSrcweir 	{
178cdf0e10cSrcweir 		const sal_uLong nFormat = SotExchange::GetFormat(rFlavor);
179cdf0e10cSrcweir 		switch (nFormat)
180cdf0e10cSrcweir 		{
181cdf0e10cSrcweir 			case SOT_FORMAT_RTF:
182cdf0e10cSrcweir                 if ( m_pRtf.is() )
183cdf0e10cSrcweir 				    m_pRtf->initialize(getDescriptor());
184cdf0e10cSrcweir 			    return m_pRtf.is() && SetObject( m_pRtf.get(), SOT_FORMAT_RTF, rFlavor );
185cdf0e10cSrcweir 
186cdf0e10cSrcweir             case SOT_FORMATSTR_ID_HTML:
187cdf0e10cSrcweir                 if ( m_pHtml.is() )
188cdf0e10cSrcweir 				    m_pHtml->initialize(getDescriptor());
189cdf0e10cSrcweir 			    return m_pHtml.is() && SetObject( m_pHtml.get(), SOT_FORMATSTR_ID_HTML, rFlavor );
190cdf0e10cSrcweir 		}
191cdf0e10cSrcweir 
192cdf0e10cSrcweir 		return ODataAccessObjectTransferable::GetData( rFlavor );
193cdf0e10cSrcweir 	}
194cdf0e10cSrcweir 
195cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
ObjectReleased()196cdf0e10cSrcweir 	void ODataClipboard::ObjectReleased()
197cdf0e10cSrcweir 	{
198cdf0e10cSrcweir         if ( m_pHtml.is() )
199cdf0e10cSrcweir         {
200cdf0e10cSrcweir             m_pHtml->dispose();
201cdf0e10cSrcweir 		    m_pHtml.clear();
202cdf0e10cSrcweir         }
203cdf0e10cSrcweir 
204cdf0e10cSrcweir         if ( m_pRtf.is() )
205cdf0e10cSrcweir         {
206cdf0e10cSrcweir             m_pRtf->dispose();
207cdf0e10cSrcweir 		    m_pRtf.clear();
208cdf0e10cSrcweir         }
209cdf0e10cSrcweir 
210cdf0e10cSrcweir         if ( getDescriptor().has( daConnection ) )
211cdf0e10cSrcweir         {
212cdf0e10cSrcweir             Reference<XConnection> xConnection( getDescriptor()[daConnection], UNO_QUERY );
213cdf0e10cSrcweir 			lcl_setListener( xConnection, this, false );
214cdf0e10cSrcweir         }
215cdf0e10cSrcweir 
216cdf0e10cSrcweir 		if ( getDescriptor().has( daCursor ) )
217cdf0e10cSrcweir         {
218cdf0e10cSrcweir             Reference< XResultSet > xResultSet( getDescriptor()[ daCursor ], UNO_QUERY );
219cdf0e10cSrcweir 			lcl_setListener( xResultSet, this, false );
220cdf0e10cSrcweir         }
221cdf0e10cSrcweir 
222cdf0e10cSrcweir 		ODataAccessObjectTransferable::ObjectReleased( );
223cdf0e10cSrcweir 	}
224cdf0e10cSrcweir 
225cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
disposing(const::com::sun::star::lang::EventObject & i_rSource)226cdf0e10cSrcweir 	void SAL_CALL ODataClipboard::disposing( const ::com::sun::star::lang::EventObject& i_rSource ) throw (::com::sun::star::uno::RuntimeException)
227cdf0e10cSrcweir 	{
228cdf0e10cSrcweir         ODataAccessDescriptor& rDescriptor( getDescriptor() );
229cdf0e10cSrcweir 
230cdf0e10cSrcweir 		if ( rDescriptor.has( daConnection ) )
231cdf0e10cSrcweir         {
232cdf0e10cSrcweir             Reference< XConnection > xConnection( rDescriptor[daConnection], UNO_QUERY );
233cdf0e10cSrcweir             if ( xConnection == i_rSource.Source )
234cdf0e10cSrcweir             {
235cdf0e10cSrcweir                 rDescriptor.erase( daConnection );
236cdf0e10cSrcweir             }
237cdf0e10cSrcweir         }
238cdf0e10cSrcweir 
239cdf0e10cSrcweir 		if ( rDescriptor.has( daCursor ) )
240cdf0e10cSrcweir         {
241cdf0e10cSrcweir             Reference< XResultSet > xResultSet( rDescriptor[ daCursor ], UNO_QUERY );
242cdf0e10cSrcweir             if ( xResultSet == i_rSource.Source )
243cdf0e10cSrcweir             {
244cdf0e10cSrcweir                 rDescriptor.erase( daCursor );
245cdf0e10cSrcweir                 // Selection and BookmarkSelection are meaningless without a result set
246cdf0e10cSrcweir                 if ( rDescriptor.has( daSelection ) )
247cdf0e10cSrcweir                     rDescriptor.erase( daSelection );
248cdf0e10cSrcweir                 if ( rDescriptor.has( daBookmarkSelection ) )
249cdf0e10cSrcweir                     rDescriptor.erase( daBookmarkSelection );
250cdf0e10cSrcweir             }
251cdf0e10cSrcweir         }
252cdf0e10cSrcweir 
253cdf0e10cSrcweir         // no matter whether it was the source connection or the source result set which died,
254cdf0e10cSrcweir         // we cannot provide the data anymore.
255cdf0e10cSrcweir         ClearFormats();
256cdf0e10cSrcweir 	}
257cdf0e10cSrcweir }
258cdf0e10cSrcweir 
259cdf0e10cSrcweir 
260