LibreOffice Module dbaccess (master) 1
dbexchange.cxx
Go to the documentation of this file.
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 <com/sun/star/lang/XComponent.hpp>
27#include <TokenWriter.hxx>
29#include <UITools.hxx>
30
31namespace dbaui
32{
33 constexpr sal_uInt32 FORMAT_OBJECT_ID_RTF = 1;
34 constexpr sal_uInt32 FORMAT_OBJECT_ID_HTML = 2;
35
36 using namespace ::com::sun::star::uno;
37 using namespace ::com::sun::star::beans;
38 using namespace ::com::sun::star::sdb;
39 using namespace ::com::sun::star::lang;
40 using namespace ::com::sun::star::util;
41 using namespace ::com::sun::star::sdbc;
42 using namespace ::com::sun::star::datatransfer;
43 using namespace ::svx;
44
45 namespace
46 {
47 template<class T > void lcl_setListener(const Reference<T>& _xComponent, const Reference< XEventListener >& i_rListener, const bool i_bAdd )
48 {
49 if ( !_xComponent.is() )
50 return;
51
52 Reference< XComponent> xCom( _xComponent, UNO_QUERY );
53 OSL_ENSURE( xCom.is(), "lcl_setListener: no component!" );
54 if ( !xCom.is() )
55 return;
56
57 i_bAdd ? xCom->addEventListener( i_rListener ) : xCom->removeEventListener( i_rListener );
58 }
59 }
60
62 {
63 }
64
66 const OUString& rDatasource,
67 const sal_Int32 nCommandType,
68 const OUString& rCommand,
69 const Reference< XConnection >& rxConnection,
70 const Reference< XNumberFormatter >& rxFormatter,
72 {
73 ClearFormats();
74
75 ODataAccessObjectTransferable::Update(rDatasource, nCommandType, rCommand, rxConnection);
76
77 lcl_setListener(rxConnection, this, true);
78
79 m_pHtml.set(new OHTMLImportExport(getDescriptor(), rxORB, rxFormatter));
80 m_pRtf.set(new ORTFImportExport(getDescriptor(), rxORB, rxFormatter));
81
83 }
84
86 const OUString& rDatasource,
87 const sal_Int32 nCommandType,
88 const OUString& rCommand,
89 const Reference< XNumberFormatter >& rxFormatter,
90 const Reference< XComponentContext >& rxORB)
91 {
92 ClearFormats();
93
94 ODataAccessObjectTransferable::Update(rDatasource, nCommandType, rCommand);
95
96 m_pHtml.set(new OHTMLImportExport(getDescriptor(), rxORB, rxFormatter));
97 m_pRtf.set(new ORTFImportExport(getDescriptor(), rxORB, rxFormatter));
98
100 }
101
102 ODataClipboard::ODataClipboard( const Reference< XPropertySet >& i_rAliveForm,
103 const Sequence< Any >& i_rSelectedRows,
104 const bool i_bBookmarkSelection,
105 const Reference< XComponentContext >& i_rORB )
106 :ODataAccessObjectTransferable( i_rAliveForm )
107 {
108 OSL_PRECOND( i_rORB.is(), "ODataClipboard::ODataClipboard: having no factory is not good ..." );
109
110 osl_atomic_increment( &m_refCount );
111
112 Reference<XConnection> xConnection;
113 getDescriptor()[ DataAccessDescriptorProperty::Connection ] >>= xConnection;
114 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 Reference< XResultSet > xResultSetClone;
119 Reference< XResultSetAccess > xResultSetAccess( i_rAliveForm, UNO_QUERY );
120 if ( xResultSetAccess.is() )
121 xResultSetClone = xResultSetAccess->createResultSet();
122 OSL_ENSURE( xResultSetClone.is(), "ODataClipboard::ODataClipboard: could not clone the form's result set" );
123 lcl_setListener( xResultSetClone, this, true );
124
125 getDescriptor()[DataAccessDescriptorProperty::Cursor] <<= xResultSetClone;
126 getDescriptor()[DataAccessDescriptorProperty::Selection] <<= i_rSelectedRows;
127 getDescriptor()[DataAccessDescriptorProperty::BookmarkSelection]<<= i_bBookmarkSelection;
128 addCompatibleSelectionDescription( i_rSelectedRows );
129
130 if ( xConnection.is() && i_rORB.is() )
131 {
132 Reference< XNumberFormatter > xFormatter( getNumberFormatter( xConnection, i_rORB ) );
133 if ( xFormatter.is() )
134 {
135 m_pHtml.set( new OHTMLImportExport( getDescriptor(), i_rORB, xFormatter ) );
136 m_pRtf.set( new ORTFImportExport( getDescriptor(), i_rORB, xFormatter ) );
137 }
138 }
139
140 osl_atomic_decrement( &m_refCount );
141 }
142
143 bool ODataClipboard::WriteObject( ::tools::SvRef<SotTempStream>& rxOStm, void* pUserObject, sal_uInt32 nUserObjectId, const css::datatransfer::DataFlavor& /*rFlavor*/ )
144 {
145 if (nUserObjectId == FORMAT_OBJECT_ID_RTF || nUserObjectId == FORMAT_OBJECT_ID_HTML )
146 {
147 ODatabaseImportExport* pExport = static_cast<ODatabaseImportExport*>(pUserObject);
148 if ( pExport && rxOStm.is() )
149 {
150 pExport->setStream(rxOStm.get());
151 return pExport->Write();
152 }
153 }
154 return false;
155 }
156
158 {
159 if ( m_pRtf.is() )
160 AddFormat( SotClipboardFormatId::RTF );
161
162 if ( m_pHtml.is() )
163 AddFormat( SotClipboardFormatId::HTML );
164
165 ODataAccessObjectTransferable::AddSupportedFormats();
166 }
167
168 bool ODataClipboard::GetData( const DataFlavor& rFlavor, const OUString& rDestDoc )
169 {
170 const SotClipboardFormatId nFormat = SotExchange::GetFormat(rFlavor);
171 switch (nFormat)
172 {
173 case SotClipboardFormatId::RTF:
174 if ( m_pRtf.is() )
175 m_pRtf->initialize(getDescriptor());
176 return m_pRtf.is() && SetObject( m_pRtf.get(), FORMAT_OBJECT_ID_RTF, rFlavor );
177
178 case SotClipboardFormatId::HTML:
179 if ( m_pHtml.is() )
180 m_pHtml->initialize(getDescriptor());
181 return m_pHtml.is() && SetObject( m_pHtml.get(), FORMAT_OBJECT_ID_HTML, rFlavor );
182
183 default: break;
184 }
185
186 return ODataAccessObjectTransferable::GetData(rFlavor, rDestDoc);
187 }
188
190 {
191 if ( m_pHtml.is() )
192 {
193 m_pHtml->dispose();
194 m_pHtml.clear();
195 }
196
197 if ( m_pRtf.is() )
198 {
199 m_pRtf->dispose();
200 m_pRtf.clear();
201 }
202
203 if ( getDescriptor().has( DataAccessDescriptorProperty::Connection ) )
204 {
205 Reference<XConnection> xConnection( getDescriptor()[DataAccessDescriptorProperty::Connection], UNO_QUERY );
206 lcl_setListener( xConnection, this, false );
207 }
208
209 if ( getDescriptor().has( DataAccessDescriptorProperty::Cursor ) )
210 {
211 Reference< XResultSet > xResultSet( getDescriptor()[ DataAccessDescriptorProperty::Cursor ], UNO_QUERY );
212 lcl_setListener( xResultSet, this, false );
213 }
214
215 ODataAccessObjectTransferable::ObjectReleased( );
216 }
217
218 void SAL_CALL ODataClipboard::disposing( const css::lang::EventObject& i_rSource )
219 {
220 ODataAccessDescriptor& rDescriptor( getDescriptor() );
221
222 if ( rDescriptor.has( DataAccessDescriptorProperty::Connection ) )
223 {
224 Reference< XConnection > xConnection( rDescriptor[DataAccessDescriptorProperty::Connection], UNO_QUERY );
225 if ( xConnection == i_rSource.Source )
226 {
227 rDescriptor.erase( DataAccessDescriptorProperty::Connection );
228 }
229 }
230
231 if ( rDescriptor.has( DataAccessDescriptorProperty::Cursor ) )
232 {
233 Reference< XResultSet > xResultSet( rDescriptor[ DataAccessDescriptorProperty::Cursor ], UNO_QUERY );
234 if ( xResultSet == i_rSource.Source )
235 {
236 rDescriptor.erase( DataAccessDescriptorProperty::Cursor );
237 // Selection and BookmarkSelection are meaningless without a result set
238 if ( rDescriptor.has( DataAccessDescriptorProperty::Selection ) )
239 rDescriptor.erase( DataAccessDescriptorProperty::Selection );
240 if ( rDescriptor.has( DataAccessDescriptorProperty::BookmarkSelection ) )
241 rDescriptor.erase( DataAccessDescriptorProperty::BookmarkSelection );
242 }
243 }
244
245 // no matter whether it was the source connection or the source result set which died,
246 // we cannot provide the data anymore.
247 ClearFormats();
248 }
249}
250
251/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static SotClipboardFormatId GetFormat(const css::datatransfer::DataFlavor &rFlavor)
::rtl::Reference< OHTMLImportExport > m_pHtml
Definition: dbexchange.hxx:41
virtual bool WriteObject(tools::SvRef< SotTempStream > &rxOStm, void *pUserObject, sal_uInt32 nUserObjectId, const css::datatransfer::DataFlavor &rFlavor) override
Definition: dbexchange.cxx:143
void Update(const OUString &_rDatasource, const sal_Int32 _nCommandType, const OUString &_rCommand, const css::uno::Reference< css::sdbc::XConnection > &_rxConnection, const css::uno::Reference< css::util::XNumberFormatter > &_rxFormatter, const css::uno::Reference< css::uno::XComponentContext > &_rxORB)
virtual void SAL_CALL disposing(const css::lang::EventObject &Source) override
Definition: dbexchange.cxx:218
::rtl::Reference< ORTFImportExport > m_pRtf
Definition: dbexchange.hxx:42
virtual void ObjectReleased() override
Definition: dbexchange.cxx:189
virtual bool GetData(const css::datatransfer::DataFlavor &rFlavor, const OUString &rDestDoc) override
Definition: dbexchange.cxx:168
virtual void AddSupportedFormats() override
Definition: dbexchange.cxx:157
void setStream(SvStream *_pStream)
Definition: TokenWriter.hxx:94
const ODataAccessDescriptor & getDescriptor() const
void addCompatibleSelectionDescription(const css::uno::Sequence< css::uno::Any > &_rSelRows)
T * get() const
bool is() const
SotClipboardFormatId
constexpr sal_uInt32 FORMAT_OBJECT_ID_RTF
Definition: dbexchange.cxx:33
css::uno::Reference< css::util::XNumberFormatter > getNumberFormatter(const css::uno::Reference< css::sdbc::XConnection > &_rxConnection, const css::uno::Reference< css::uno::XComponentContext > &_rxContext)
creates a number formatter
constexpr sal_uInt32 FORMAT_OBJECT_ID_HTML
Definition: dbexchange.cxx:34