Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "dbexchange.hxx"
21 : #include <sot/formats.hxx>
22 : #include <sot/storage.hxx>
23 : #include <osl/diagnose.h>
24 : #include <com/sun/star/sdb/CommandType.hpp>
25 : #include <com/sun/star/sdb/XResultSetAccess.hpp>
26 : #include "TokenWriter.hxx"
27 : #include "dbustrings.hrc"
28 : #include <comphelper/uno3.hxx>
29 : #include <svx/dataaccessdescriptor.hxx>
30 : #include "UITools.hxx"
31 : #include <comphelper/processfactory.hxx>
32 :
33 : namespace dbaui
34 : {
35 : using namespace ::com::sun::star::uno;
36 : using namespace ::com::sun::star::beans;
37 : using namespace ::com::sun::star::sdb;
38 : using namespace ::com::sun::star::lang;
39 : using namespace ::com::sun::star::util;
40 : using namespace ::com::sun::star::sdbc;
41 : using namespace ::com::sun::star::datatransfer;
42 : using namespace ::svx;
43 :
44 : namespace
45 : {
46 0 : template<class T > void lcl_setListener(const Reference<T>& _xComponent, const Reference< XEventListener >& i_rListener, const bool i_bAdd )
47 : {
48 0 : if ( !_xComponent.is() )
49 0 : return;
50 :
51 0 : Reference< XComponent> xCom( _xComponent, UNO_QUERY );
52 : OSL_ENSURE( xCom.is(), "lcl_setListener: no component!" );
53 0 : if ( !xCom.is() )
54 0 : return;
55 :
56 0 : i_bAdd ? xCom->addEventListener( i_rListener ) : xCom->removeEventListener( i_rListener );
57 : }
58 : }
59 :
60 0 : ODataClipboard::ODataClipboard(
61 : const OUString& _rDatasource,
62 : const sal_Int32 _nCommandType,
63 : const OUString& _rCommand,
64 : const Reference< XConnection >& _rxConnection,
65 : const Reference< XNumberFormatter >& _rxFormatter,
66 : const Reference< XComponentContext >& _rxORB)
67 : :ODataAccessObjectTransferable( _rDatasource,OUString(), _nCommandType, _rCommand, _rxConnection )
68 : ,m_pHtml(NULL)
69 0 : ,m_pRtf(NULL)
70 : {
71 0 : osl_atomic_increment( &m_refCount );
72 0 : lcl_setListener( _rxConnection, this, true );
73 :
74 0 : m_pHtml.set( new OHTMLImportExport( getDescriptor(), _rxORB, _rxFormatter ) );
75 0 : m_pRtf.set( new ORTFImportExport( getDescriptor(), _rxORB, _rxFormatter ) );
76 :
77 0 : osl_atomic_decrement( &m_refCount );
78 0 : }
79 :
80 0 : ODataClipboard::ODataClipboard(
81 : const OUString& _rDatasource,
82 : const sal_Int32 _nCommandType,
83 : const OUString& _rCommand,
84 : const Reference< XNumberFormatter >& _rxFormatter,
85 : const Reference< XComponentContext >& _rxORB)
86 : :ODataAccessObjectTransferable( _rDatasource, OUString(),_nCommandType, _rCommand)
87 : ,m_pHtml(NULL)
88 0 : ,m_pRtf(NULL)
89 : {
90 0 : m_pHtml.set( new OHTMLImportExport( getDescriptor(),_rxORB, _rxFormatter ) );
91 0 : m_pRtf.set( new ORTFImportExport( getDescriptor(),_rxORB, _rxFormatter ) );
92 0 : }
93 :
94 0 : ODataClipboard::ODataClipboard( const Reference< XPropertySet >& i_rAliveForm,
95 : const Sequence< Any >& i_rSelectedRows,
96 : const bool i_bBookmarkSelection,
97 : const Reference< XComponentContext >& i_rORB )
98 : :ODataAccessObjectTransferable( i_rAliveForm )
99 : ,m_pHtml(NULL)
100 0 : ,m_pRtf(NULL)
101 : {
102 : OSL_PRECOND( i_rORB.is(), "ODataClipboard::ODataClipboard: having no factory is not good ..." );
103 :
104 0 : osl_atomic_increment( &m_refCount );
105 :
106 0 : Reference<XConnection> xConnection;
107 0 : getDescriptor()[ daConnection ] >>= xConnection;
108 0 : lcl_setListener( xConnection, this, true );
109 :
110 : // do not pass the form itself as source result set, since the client might operate on the form, which
111 : // might lead to undesired effects. Instead, use a clone.
112 0 : Reference< XResultSet > xResultSetClone;
113 0 : Reference< XResultSetAccess > xResultSetAccess( i_rAliveForm, UNO_QUERY );
114 0 : if ( xResultSetAccess.is() )
115 0 : xResultSetClone = xResultSetAccess->createResultSet();
116 : OSL_ENSURE( xResultSetClone.is(), "ODataClipboard::ODataClipboard: could not clone the form's result set" );
117 0 : lcl_setListener( xResultSetClone, this, true );
118 :
119 0 : getDescriptor()[daCursor] <<= xResultSetClone;
120 0 : getDescriptor()[daSelection] <<= i_rSelectedRows;
121 0 : getDescriptor()[daBookmarkSelection]<<= i_bBookmarkSelection;
122 0 : addCompatibleSelectionDescription( i_rSelectedRows );
123 :
124 0 : if ( xConnection.is() && i_rORB.is() )
125 : {
126 0 : Reference< XNumberFormatter > xFormatter( getNumberFormatter( xConnection, i_rORB ) );
127 0 : if ( xFormatter.is() )
128 : {
129 0 : m_pHtml.set( new OHTMLImportExport( getDescriptor(), i_rORB, xFormatter ) );
130 0 : m_pRtf.set( new ORTFImportExport( getDescriptor(), i_rORB, xFormatter ) );
131 0 : }
132 : }
133 :
134 0 : osl_atomic_decrement( &m_refCount );
135 0 : }
136 :
137 0 : bool ODataClipboard::WriteObject( ::tools::SvRef<SotStorageStream>& rxOStm, void* pUserObject, SotClipboardFormatId nUserObjectId, const ::com::sun::star::datatransfer::DataFlavor& /*rFlavor*/ )
138 : {
139 0 : if (nUserObjectId == SotClipboardFormatId::RTF || nUserObjectId == SotClipboardFormatId::HTML )
140 : {
141 0 : ODatabaseImportExport* pExport = static_cast<ODatabaseImportExport*>(pUserObject);
142 0 : if ( pExport && rxOStm.Is() )
143 : {
144 0 : pExport->setStream(&rxOStm);
145 0 : return pExport->Write();
146 : }
147 : }
148 0 : return false;
149 : }
150 :
151 0 : void ODataClipboard::AddSupportedFormats()
152 : {
153 0 : if ( m_pRtf.is() )
154 0 : AddFormat( SotClipboardFormatId::RTF );
155 :
156 0 : if ( m_pHtml.is() )
157 0 : AddFormat( SotClipboardFormatId::HTML );
158 :
159 0 : ODataAccessObjectTransferable::AddSupportedFormats();
160 0 : }
161 :
162 0 : bool ODataClipboard::GetData( const DataFlavor& rFlavor, const OUString& rDestDoc )
163 : {
164 0 : const SotClipboardFormatId nFormat = SotExchange::GetFormat(rFlavor);
165 0 : switch (nFormat)
166 : {
167 : case SotClipboardFormatId::RTF:
168 0 : if ( m_pRtf.is() )
169 0 : m_pRtf->initialize(getDescriptor());
170 0 : return m_pRtf.is() && SetObject( m_pRtf.get(), SotClipboardFormatId::RTF, rFlavor );
171 :
172 : case SotClipboardFormatId::HTML:
173 0 : if ( m_pHtml.is() )
174 0 : m_pHtml->initialize(getDescriptor());
175 0 : return m_pHtml.is() && SetObject( m_pHtml.get(), SotClipboardFormatId::HTML, rFlavor );
176 :
177 0 : default: break;
178 : }
179 :
180 0 : return ODataAccessObjectTransferable::GetData(rFlavor, rDestDoc);
181 : }
182 :
183 0 : void ODataClipboard::ObjectReleased()
184 : {
185 0 : if ( m_pHtml.is() )
186 : {
187 0 : m_pHtml->dispose();
188 0 : m_pHtml.clear();
189 : }
190 :
191 0 : if ( m_pRtf.is() )
192 : {
193 0 : m_pRtf->dispose();
194 0 : m_pRtf.clear();
195 : }
196 :
197 0 : if ( getDescriptor().has( daConnection ) )
198 : {
199 0 : Reference<XConnection> xConnection( getDescriptor()[daConnection], UNO_QUERY );
200 0 : lcl_setListener( xConnection, this, false );
201 : }
202 :
203 0 : if ( getDescriptor().has( daCursor ) )
204 : {
205 0 : Reference< XResultSet > xResultSet( getDescriptor()[ daCursor ], UNO_QUERY );
206 0 : lcl_setListener( xResultSet, this, false );
207 : }
208 :
209 0 : ODataAccessObjectTransferable::ObjectReleased( );
210 0 : }
211 :
212 0 : void SAL_CALL ODataClipboard::disposing( const ::com::sun::star::lang::EventObject& i_rSource ) throw (::com::sun::star::uno::RuntimeException, std::exception)
213 : {
214 0 : ODataAccessDescriptor& rDescriptor( getDescriptor() );
215 :
216 0 : if ( rDescriptor.has( daConnection ) )
217 : {
218 0 : Reference< XConnection > xConnection( rDescriptor[daConnection], UNO_QUERY );
219 0 : if ( xConnection == i_rSource.Source )
220 : {
221 0 : rDescriptor.erase( daConnection );
222 0 : }
223 : }
224 :
225 0 : if ( rDescriptor.has( daCursor ) )
226 : {
227 0 : Reference< XResultSet > xResultSet( rDescriptor[ daCursor ], UNO_QUERY );
228 0 : if ( xResultSet == i_rSource.Source )
229 : {
230 0 : rDescriptor.erase( daCursor );
231 : // Selection and BookmarkSelection are meaningless without a result set
232 0 : if ( rDescriptor.has( daSelection ) )
233 0 : rDescriptor.erase( daSelection );
234 0 : if ( rDescriptor.has( daBookmarkSelection ) )
235 0 : rDescriptor.erase( daBookmarkSelection );
236 0 : }
237 : }
238 :
239 : // no matter whether it was the source connection or the source result set which died,
240 : // we cannot provide the data anymore.
241 0 : ClearFormats();
242 0 : }
243 36 : }
244 :
245 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|