Version 3.6.0.2, tag libreoffice-3.6.0.2
[LibreOffice.git] / dbaccess / source / ui / browser / dbexchange.cxx
blob133ceb71ba1090c1f5245c4e220214b4a18daa39
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include "dbexchange.hxx"
31 #include <sot/formats.hxx>
32 #include <sot/storage.hxx>
33 #include <osl/diagnose.h>
34 #include <com/sun/star/sdb/CommandType.hpp>
35 #include <com/sun/star/sdb/XResultSetAccess.hpp>
36 #include "TokenWriter.hxx"
37 #include "dbustrings.hrc"
38 #include <comphelper/uno3.hxx>
39 #include <svx/dataaccessdescriptor.hxx>
40 #include "UITools.hxx"
43 namespace dbaui
45 using namespace ::com::sun::star::uno;
46 using namespace ::com::sun::star::beans;
47 using namespace ::com::sun::star::sdb;
48 using namespace ::com::sun::star::beans;
49 using namespace ::com::sun::star::lang;
50 using namespace ::com::sun::star::util;
51 using namespace ::com::sun::star::sdbc;
52 using namespace ::com::sun::star::datatransfer;
53 using namespace ::svx;
55 namespace
57 template<class T > void lcl_setListener(const Reference<T>& _xComponent, const Reference< XEventListener >& i_rListener, const bool i_bAdd )
59 if ( !_xComponent.is() )
60 return;
62 Reference< XComponent> xCom( _xComponent, UNO_QUERY );
63 OSL_ENSURE( xCom.is(), "lcl_setListener: no component!" );
64 if ( !xCom.is() )
65 return;
67 i_bAdd ? xCom->addEventListener( i_rListener ) : xCom->removeEventListener( i_rListener );
71 // -----------------------------------------------------------------------------
72 ODataClipboard::ODataClipboard(
73 const ::rtl::OUString& _rDatasource,
74 const sal_Int32 _nCommandType,
75 const ::rtl::OUString& _rCommand,
76 const Reference< XConnection >& _rxConnection,
77 const Reference< XNumberFormatter >& _rxFormatter,
78 const Reference< XMultiServiceFactory >& _rxORB)
79 :ODataAccessObjectTransferable( _rDatasource,::rtl::OUString(), _nCommandType, _rCommand, _rxConnection )
80 ,m_pHtml(NULL)
81 ,m_pRtf(NULL)
83 osl_incrementInterlockedCount( &m_refCount );
84 lcl_setListener( _rxConnection, this, true );
86 m_pHtml.set( new OHTMLImportExport( getDescriptor(), _rxORB, _rxFormatter ) );
87 m_pRtf.set( new ORTFImportExport( getDescriptor(), _rxORB, _rxFormatter ) );
89 osl_decrementInterlockedCount( &m_refCount );
92 // -----------------------------------------------------------------------------
93 ODataClipboard::ODataClipboard(
94 const ::rtl::OUString& _rDatasource,
95 const sal_Int32 _nCommandType,
96 const ::rtl::OUString& _rCommand,
97 const Reference< XNumberFormatter >& _rxFormatter,
98 const Reference< XMultiServiceFactory >& _rxORB)
99 :ODataAccessObjectTransferable( _rDatasource, ::rtl::OUString(),_nCommandType, _rCommand)
100 ,m_pHtml(NULL)
101 ,m_pRtf(NULL)
103 m_pHtml.set( new OHTMLImportExport( getDescriptor(),_rxORB, _rxFormatter ) );
104 m_pRtf.set( new ORTFImportExport( getDescriptor(),_rxORB, _rxFormatter ) );
107 // -----------------------------------------------------------------------------
108 ODataClipboard::ODataClipboard( const Reference< XPropertySet >& i_rAliveForm,
109 const Sequence< Any >& i_rSelectedRows,
110 const sal_Bool i_bBookmarkSelection,
111 const Reference< XMultiServiceFactory >& i_rORB )
112 :ODataAccessObjectTransferable( i_rAliveForm )
113 ,m_pHtml(NULL)
114 ,m_pRtf(NULL)
116 OSL_PRECOND( i_rORB.is(), "ODataClipboard::ODataClipboard: having no factory is not good ..." );
118 osl_incrementInterlockedCount( &m_refCount );
120 Reference<XConnection> xConnection;
121 getDescriptor()[ daConnection ] >>= xConnection;
122 lcl_setListener( xConnection, this, true );
124 // do not pass the form itself as source result set, since the client might operate on the form, which
125 // might lead to undesired effects. Instead, use a clone.
126 Reference< XResultSet > xResultSetClone;
127 Reference< XResultSetAccess > xResultSetAccess( i_rAliveForm, UNO_QUERY );
128 if ( xResultSetAccess.is() )
129 xResultSetClone = xResultSetAccess->createResultSet();
130 OSL_ENSURE( xResultSetClone.is(), "ODataClipboard::ODataClipboard: could not clone the form's result set" );
131 lcl_setListener( xResultSetClone, this, true );
133 getDescriptor()[daCursor] <<= xResultSetClone;
134 getDescriptor()[daSelection] <<= i_rSelectedRows;
135 getDescriptor()[daBookmarkSelection]<<= i_bBookmarkSelection;
136 addCompatibleSelectionDescription( i_rSelectedRows );
138 if ( xConnection.is() && i_rORB.is() )
140 Reference< XNumberFormatter > xFormatter( getNumberFormatter( xConnection, i_rORB ) );
141 if ( xFormatter.is() )
143 m_pHtml.set( new OHTMLImportExport( getDescriptor(), i_rORB, xFormatter ) );
144 m_pRtf.set( new ORTFImportExport( getDescriptor(), i_rORB, xFormatter ) );
148 osl_decrementInterlockedCount( &m_refCount );
151 // -----------------------------------------------------------------------------
152 sal_Bool ODataClipboard::WriteObject( SotStorageStreamRef& rxOStm, void* pUserObject, sal_uInt32 nUserObjectId, const ::com::sun::star::datatransfer::DataFlavor& /*rFlavor*/ )
154 if (nUserObjectId == SOT_FORMAT_RTF || nUserObjectId == SOT_FORMATSTR_ID_HTML )
156 ODatabaseImportExport* pExport = reinterpret_cast<ODatabaseImportExport*>(pUserObject);
157 if ( pExport && rxOStm.Is() )
159 pExport->setStream(&rxOStm);
160 return pExport->Write();
163 return sal_False;
166 // -----------------------------------------------------------------------------
167 void ODataClipboard::AddSupportedFormats()
169 if ( m_pRtf.is() )
170 AddFormat( SOT_FORMAT_RTF );
172 if ( m_pHtml.is() )
173 AddFormat( SOT_FORMATSTR_ID_HTML );
175 ODataAccessObjectTransferable::AddSupportedFormats();
178 // -----------------------------------------------------------------------------
179 sal_Bool ODataClipboard::GetData( const DataFlavor& rFlavor )
181 const sal_uLong nFormat = SotExchange::GetFormat(rFlavor);
182 switch (nFormat)
184 case SOT_FORMAT_RTF:
185 if ( m_pRtf.is() )
186 m_pRtf->initialize(getDescriptor());
187 return m_pRtf.is() && SetObject( m_pRtf.get(), SOT_FORMAT_RTF, rFlavor );
189 case SOT_FORMATSTR_ID_HTML:
190 if ( m_pHtml.is() )
191 m_pHtml->initialize(getDescriptor());
192 return m_pHtml.is() && SetObject( m_pHtml.get(), SOT_FORMATSTR_ID_HTML, rFlavor );
195 return ODataAccessObjectTransferable::GetData( rFlavor );
198 // -----------------------------------------------------------------------------
199 void ODataClipboard::ObjectReleased()
201 if ( m_pHtml.is() )
203 m_pHtml->dispose();
204 m_pHtml.clear();
207 if ( m_pRtf.is() )
209 m_pRtf->dispose();
210 m_pRtf.clear();
213 if ( getDescriptor().has( daConnection ) )
215 Reference<XConnection> xConnection( getDescriptor()[daConnection], UNO_QUERY );
216 lcl_setListener( xConnection, this, false );
219 if ( getDescriptor().has( daCursor ) )
221 Reference< XResultSet > xResultSet( getDescriptor()[ daCursor ], UNO_QUERY );
222 lcl_setListener( xResultSet, this, false );
225 ODataAccessObjectTransferable::ObjectReleased( );
228 // -----------------------------------------------------------------------------
229 void SAL_CALL ODataClipboard::disposing( const ::com::sun::star::lang::EventObject& i_rSource ) throw (::com::sun::star::uno::RuntimeException)
231 ODataAccessDescriptor& rDescriptor( getDescriptor() );
233 if ( rDescriptor.has( daConnection ) )
235 Reference< XConnection > xConnection( rDescriptor[daConnection], UNO_QUERY );
236 if ( xConnection == i_rSource.Source )
238 rDescriptor.erase( daConnection );
242 if ( rDescriptor.has( daCursor ) )
244 Reference< XResultSet > xResultSet( rDescriptor[ daCursor ], UNO_QUERY );
245 if ( xResultSet == i_rSource.Source )
247 rDescriptor.erase( daCursor );
248 // Selection and BookmarkSelection are meaningless without a result set
249 if ( rDescriptor.has( daSelection ) )
250 rDescriptor.erase( daSelection );
251 if ( rDescriptor.has( daBookmarkSelection ) )
252 rDescriptor.erase( daBookmarkSelection );
256 // no matter whether it was the source connection or the source result set which died,
257 // we cannot provide the data anymore.
258 ClearFormats();
263 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */