1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_dbaccess.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "dbexchange.hxx"
32*cdf0e10cSrcweir #include <sot/formats.hxx>
33*cdf0e10cSrcweir #include <sot/storage.hxx>
34*cdf0e10cSrcweir #include <osl/diagnose.h>
35*cdf0e10cSrcweir #include <com/sun/star/sdb/CommandType.hpp>
36*cdf0e10cSrcweir #include <com/sun/star/sdb/XResultSetAccess.hpp>
37*cdf0e10cSrcweir #include "TokenWriter.hxx"
38*cdf0e10cSrcweir #include "dbustrings.hrc"
39*cdf0e10cSrcweir #include <comphelper/uno3.hxx>
40*cdf0e10cSrcweir #include <svx/dataaccessdescriptor.hxx>
41*cdf0e10cSrcweir #include "UITools.hxx"
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir namespace dbaui
45*cdf0e10cSrcweir {
46*cdf0e10cSrcweir 	using namespace ::com::sun::star::uno;
47*cdf0e10cSrcweir 	using namespace ::com::sun::star::beans;
48*cdf0e10cSrcweir 	using namespace ::com::sun::star::sdb;
49*cdf0e10cSrcweir 	using namespace ::com::sun::star::beans;
50*cdf0e10cSrcweir 	using namespace ::com::sun::star::lang;
51*cdf0e10cSrcweir 	using namespace ::com::sun::star::util;
52*cdf0e10cSrcweir 	using namespace ::com::sun::star::sdbc;
53*cdf0e10cSrcweir 	using namespace ::com::sun::star::datatransfer;
54*cdf0e10cSrcweir 	using namespace ::svx;
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir 	namespace
57*cdf0e10cSrcweir 	{
58*cdf0e10cSrcweir 		template<class T > void lcl_setListener(const Reference<T>& _xComponent, const Reference< XEventListener >& i_rListener, const bool i_bAdd )
59*cdf0e10cSrcweir 		{
60*cdf0e10cSrcweir 			if ( !_xComponent.is() )
61*cdf0e10cSrcweir                 return;
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir             Reference< XComponent> xCom( _xComponent, UNO_QUERY );
64*cdf0e10cSrcweir             OSL_ENSURE( xCom.is(), "lcl_setListener: no component!" );
65*cdf0e10cSrcweir 			if ( !xCom.is() )
66*cdf0e10cSrcweir                 return;
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir             i_bAdd ? xCom->addEventListener( i_rListener ) : xCom->removeEventListener( i_rListener );
69*cdf0e10cSrcweir 		}
70*cdf0e10cSrcweir 	}
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
73*cdf0e10cSrcweir 	ODataClipboard::ODataClipboard(
74*cdf0e10cSrcweir 					const ::rtl::OUString&	_rDatasource,
75*cdf0e10cSrcweir 					const sal_Int32			_nCommandType,
76*cdf0e10cSrcweir 					const ::rtl::OUString&	_rCommand,
77*cdf0e10cSrcweir 					const Reference< XConnection >& _rxConnection,
78*cdf0e10cSrcweir 					const Reference< XNumberFormatter >& _rxFormatter,
79*cdf0e10cSrcweir 					const Reference< XMultiServiceFactory >& _rxORB)
80*cdf0e10cSrcweir 					:ODataAccessObjectTransferable( _rDatasource,::rtl::OUString(), _nCommandType, _rCommand, _rxConnection )
81*cdf0e10cSrcweir 		,m_pHtml(NULL)
82*cdf0e10cSrcweir 		,m_pRtf(NULL)
83*cdf0e10cSrcweir 	{
84*cdf0e10cSrcweir 		osl_incrementInterlockedCount( &m_refCount );
85*cdf0e10cSrcweir 		lcl_setListener( _rxConnection, this, true );
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir 		m_pHtml.set( new OHTMLImportExport( getDescriptor(), _rxORB, _rxFormatter ) );
88*cdf0e10cSrcweir 		m_pRtf.set( new ORTFImportExport( getDescriptor(), _rxORB, _rxFormatter ) );
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir         osl_decrementInterlockedCount( &m_refCount );
91*cdf0e10cSrcweir 	}
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
94*cdf0e10cSrcweir 	ODataClipboard::ODataClipboard(
95*cdf0e10cSrcweir 					const ::rtl::OUString&	_rDatasource,
96*cdf0e10cSrcweir 					const sal_Int32			_nCommandType,
97*cdf0e10cSrcweir 					const ::rtl::OUString&	_rCommand,
98*cdf0e10cSrcweir 					const Reference< XNumberFormatter >& _rxFormatter,
99*cdf0e10cSrcweir 					const Reference< XMultiServiceFactory >& _rxORB)
100*cdf0e10cSrcweir 		:ODataAccessObjectTransferable( _rDatasource, ::rtl::OUString(),_nCommandType, _rCommand)
101*cdf0e10cSrcweir 		,m_pHtml(NULL)
102*cdf0e10cSrcweir 		,m_pRtf(NULL)
103*cdf0e10cSrcweir 	{
104*cdf0e10cSrcweir 		m_pHtml.set( new OHTMLImportExport( getDescriptor(),_rxORB, _rxFormatter ) );
105*cdf0e10cSrcweir 		m_pRtf.set( new ORTFImportExport( getDescriptor(),_rxORB, _rxFormatter ) );
106*cdf0e10cSrcweir 	}
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
109*cdf0e10cSrcweir     ODataClipboard::ODataClipboard(	const Reference< XPropertySet >& i_rAliveForm,
110*cdf0e10cSrcweir                                     const Sequence< Any >& i_rSelectedRows,
111*cdf0e10cSrcweir                                     const sal_Bool i_bBookmarkSelection,
112*cdf0e10cSrcweir                                     const Reference< XMultiServiceFactory >& i_rORB )
113*cdf0e10cSrcweir         :ODataAccessObjectTransferable( i_rAliveForm )
114*cdf0e10cSrcweir         ,m_pHtml(NULL)
115*cdf0e10cSrcweir         ,m_pRtf(NULL)
116*cdf0e10cSrcweir     {
117*cdf0e10cSrcweir         OSL_PRECOND( i_rORB.is(), "ODataClipboard::ODataClipboard: having no factory is not good ..." );
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir         osl_incrementInterlockedCount( &m_refCount );
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir         Reference<XConnection> xConnection;
122*cdf0e10cSrcweir         getDescriptor()[ daConnection ] >>= xConnection;
123*cdf0e10cSrcweir         lcl_setListener( xConnection, this, true );
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir         // do not pass the form itself as source result set, since the client might operate on the form, which
126*cdf0e10cSrcweir         // might lead to undesired effects. Instead, use a clone.
127*cdf0e10cSrcweir         Reference< XResultSet > xResultSetClone;
128*cdf0e10cSrcweir 		Reference< XResultSetAccess > xResultSetAccess( i_rAliveForm, UNO_QUERY );
129*cdf0e10cSrcweir 		if ( xResultSetAccess.is() )
130*cdf0e10cSrcweir 			xResultSetClone = xResultSetAccess->createResultSet();
131*cdf0e10cSrcweir         OSL_ENSURE( xResultSetClone.is(), "ODataClipboard::ODataClipboard: could not clone the form's result set" );
132*cdf0e10cSrcweir         lcl_setListener( xResultSetClone, this, true );
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir         getDescriptor()[daCursor]			<<= xResultSetClone;
135*cdf0e10cSrcweir         getDescriptor()[daSelection]		<<= i_rSelectedRows;
136*cdf0e10cSrcweir         getDescriptor()[daBookmarkSelection]<<= i_bBookmarkSelection;
137*cdf0e10cSrcweir         addCompatibleSelectionDescription( i_rSelectedRows );
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir         if ( xConnection.is() && i_rORB.is() )
140*cdf0e10cSrcweir         {
141*cdf0e10cSrcweir             Reference< XNumberFormatter > xFormatter( getNumberFormatter( xConnection, i_rORB ) );
142*cdf0e10cSrcweir             if ( xFormatter.is() )
143*cdf0e10cSrcweir             {
144*cdf0e10cSrcweir                 m_pHtml.set( new OHTMLImportExport( getDescriptor(), i_rORB, xFormatter ) );
145*cdf0e10cSrcweir                 m_pRtf.set( new ORTFImportExport( getDescriptor(), i_rORB, xFormatter ) );
146*cdf0e10cSrcweir             }
147*cdf0e10cSrcweir         }
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir         osl_decrementInterlockedCount( &m_refCount );
150*cdf0e10cSrcweir     }
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
153*cdf0e10cSrcweir 	sal_Bool ODataClipboard::WriteObject( SotStorageStreamRef& rxOStm, void* pUserObject, sal_uInt32 nUserObjectId, const ::com::sun::star::datatransfer::DataFlavor& /*rFlavor*/ )
154*cdf0e10cSrcweir 	{
155*cdf0e10cSrcweir 		if (nUserObjectId == SOT_FORMAT_RTF || nUserObjectId == SOT_FORMATSTR_ID_HTML )
156*cdf0e10cSrcweir 		{
157*cdf0e10cSrcweir 			ODatabaseImportExport* pExport = reinterpret_cast<ODatabaseImportExport*>(pUserObject);
158*cdf0e10cSrcweir 			if ( pExport && rxOStm.Is() )
159*cdf0e10cSrcweir 			{
160*cdf0e10cSrcweir 				pExport->setStream(&rxOStm);
161*cdf0e10cSrcweir 				return pExport->Write();
162*cdf0e10cSrcweir 			}
163*cdf0e10cSrcweir 		}
164*cdf0e10cSrcweir 		return sal_False;
165*cdf0e10cSrcweir 	}
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
168*cdf0e10cSrcweir 	void ODataClipboard::AddSupportedFormats()
169*cdf0e10cSrcweir 	{
170*cdf0e10cSrcweir 		if ( m_pRtf.is() )
171*cdf0e10cSrcweir 			AddFormat( SOT_FORMAT_RTF );
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir 		if ( m_pHtml.is() )
174*cdf0e10cSrcweir 			AddFormat( SOT_FORMATSTR_ID_HTML );
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir 		ODataAccessObjectTransferable::AddSupportedFormats();
177*cdf0e10cSrcweir 	}
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
180*cdf0e10cSrcweir 	sal_Bool ODataClipboard::GetData( const DataFlavor& rFlavor )
181*cdf0e10cSrcweir 	{
182*cdf0e10cSrcweir 		const sal_uLong nFormat = SotExchange::GetFormat(rFlavor);
183*cdf0e10cSrcweir 		switch (nFormat)
184*cdf0e10cSrcweir 		{
185*cdf0e10cSrcweir 			case SOT_FORMAT_RTF:
186*cdf0e10cSrcweir                 if ( m_pRtf.is() )
187*cdf0e10cSrcweir 				    m_pRtf->initialize(getDescriptor());
188*cdf0e10cSrcweir 			    return m_pRtf.is() && SetObject( m_pRtf.get(), SOT_FORMAT_RTF, rFlavor );
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir             case SOT_FORMATSTR_ID_HTML:
191*cdf0e10cSrcweir                 if ( m_pHtml.is() )
192*cdf0e10cSrcweir 				    m_pHtml->initialize(getDescriptor());
193*cdf0e10cSrcweir 			    return m_pHtml.is() && SetObject( m_pHtml.get(), SOT_FORMATSTR_ID_HTML, rFlavor );
194*cdf0e10cSrcweir 		}
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir 		return ODataAccessObjectTransferable::GetData( rFlavor );
197*cdf0e10cSrcweir 	}
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
200*cdf0e10cSrcweir 	void ODataClipboard::ObjectReleased()
201*cdf0e10cSrcweir 	{
202*cdf0e10cSrcweir         if ( m_pHtml.is() )
203*cdf0e10cSrcweir         {
204*cdf0e10cSrcweir             m_pHtml->dispose();
205*cdf0e10cSrcweir 		    m_pHtml.clear();
206*cdf0e10cSrcweir         }
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir         if ( m_pRtf.is() )
209*cdf0e10cSrcweir         {
210*cdf0e10cSrcweir             m_pRtf->dispose();
211*cdf0e10cSrcweir 		    m_pRtf.clear();
212*cdf0e10cSrcweir         }
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir         if ( getDescriptor().has( daConnection ) )
215*cdf0e10cSrcweir         {
216*cdf0e10cSrcweir             Reference<XConnection> xConnection( getDescriptor()[daConnection], UNO_QUERY );
217*cdf0e10cSrcweir 			lcl_setListener( xConnection, this, false );
218*cdf0e10cSrcweir         }
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir 		if ( getDescriptor().has( daCursor ) )
221*cdf0e10cSrcweir         {
222*cdf0e10cSrcweir             Reference< XResultSet > xResultSet( getDescriptor()[ daCursor ], UNO_QUERY );
223*cdf0e10cSrcweir 			lcl_setListener( xResultSet, this, false );
224*cdf0e10cSrcweir         }
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir 		ODataAccessObjectTransferable::ObjectReleased( );
227*cdf0e10cSrcweir 	}
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
230*cdf0e10cSrcweir 	void SAL_CALL ODataClipboard::disposing( const ::com::sun::star::lang::EventObject& i_rSource ) throw (::com::sun::star::uno::RuntimeException)
231*cdf0e10cSrcweir 	{
232*cdf0e10cSrcweir         ODataAccessDescriptor& rDescriptor( getDescriptor() );
233*cdf0e10cSrcweir 
234*cdf0e10cSrcweir 		if ( rDescriptor.has( daConnection ) )
235*cdf0e10cSrcweir         {
236*cdf0e10cSrcweir             Reference< XConnection > xConnection( rDescriptor[daConnection], UNO_QUERY );
237*cdf0e10cSrcweir             if ( xConnection == i_rSource.Source )
238*cdf0e10cSrcweir             {
239*cdf0e10cSrcweir                 rDescriptor.erase( daConnection );
240*cdf0e10cSrcweir             }
241*cdf0e10cSrcweir         }
242*cdf0e10cSrcweir 
243*cdf0e10cSrcweir 		if ( rDescriptor.has( daCursor ) )
244*cdf0e10cSrcweir         {
245*cdf0e10cSrcweir             Reference< XResultSet > xResultSet( rDescriptor[ daCursor ], UNO_QUERY );
246*cdf0e10cSrcweir             if ( xResultSet == i_rSource.Source )
247*cdf0e10cSrcweir             {
248*cdf0e10cSrcweir                 rDescriptor.erase( daCursor );
249*cdf0e10cSrcweir                 // Selection and BookmarkSelection are meaningless without a result set
250*cdf0e10cSrcweir                 if ( rDescriptor.has( daSelection ) )
251*cdf0e10cSrcweir                     rDescriptor.erase( daSelection );
252*cdf0e10cSrcweir                 if ( rDescriptor.has( daBookmarkSelection ) )
253*cdf0e10cSrcweir                     rDescriptor.erase( daBookmarkSelection );
254*cdf0e10cSrcweir             }
255*cdf0e10cSrcweir         }
256*cdf0e10cSrcweir 
257*cdf0e10cSrcweir         // no matter whether it was the source connection or the source result set which died,
258*cdf0e10cSrcweir         // we cannot provide the data anymore.
259*cdf0e10cSrcweir         ClearFormats();
260*cdf0e10cSrcweir 	}
261*cdf0e10cSrcweir }
262*cdf0e10cSrcweir 
263*cdf0e10cSrcweir 
264