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