1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <TableCopyHelper.hxx>
21 #include <core_resource.hxx>
22 #include <strings.hrc>
23 #include <strings.hxx>
24 #include <dbaccess/genericcontroller.hxx>
25 #include <com/sun/star/task/InteractionHandler.hpp>
26 #include <com/sun/star/task/XInteractionHandler.hpp>
27 #include <com/sun/star/sdb/application/CopyTableOperation.hpp>
28 #include <com/sun/star/sdb/application/CopyTableWizard.hpp>
29 #include <com/sun/star/sdb/DataAccessDescriptorFactory.hpp>
30 #include <com/sun/star/sdb/CommandType.hpp>
32 #include <TokenWriter.hxx>
33 #include <UITools.hxx>
34 #include <dbaccess/dataview.hxx>
35 #include <svx/dbaexchange.hxx>
36 #include <unotools/ucbhelper.hxx>
37 #include <tools/urlobj.hxx>
38 #include <tools/diagnose_ex.h>
39 #include <sal/log.hxx>
40 #include <toolkit/helper/vclunohelper.hxx>
41 #include <unotools/tempfile.hxx>
42 #include <cppuhelper/exc_hlp.hxx>
46 using namespace ::dbtools
;
47 using namespace ::svx
;
48 using namespace ::com::sun::star::uno
;
49 using namespace ::com::sun::star::task
;
50 using namespace ::com::sun::star::beans
;
51 using namespace ::com::sun::star::lang
;
52 using namespace ::com::sun::star::container
;
53 using namespace ::com::sun::star::sdb
;
54 using namespace ::com::sun::star::sdb::application
;
55 using namespace ::com::sun::star::sdbc
;
56 using namespace ::com::sun::star::sdbcx
;
57 using namespace ::com::sun::star::ucb
;
59 OTableCopyHelper::OTableCopyHelper(OGenericUnoController
* _pController
)
60 :m_pController(_pController
)
64 void OTableCopyHelper::insertTable( const OUString
& i_rSourceDataSource
, const Reference
<XConnection
>& i_rSourceConnection
,
65 const OUString
& i_rCommand
, const sal_Int32 i_nCommandType
,
66 const Reference
< XResultSet
>& i_rSourceRows
, const Sequence
< Any
>& i_rSelection
, const bool i_bBookmarkSelection
,
67 const OUString
& i_rDestDataSource
, const Reference
<XConnection
>& i_rDestConnection
)
69 if ( CommandType::QUERY
!= i_nCommandType
&& CommandType::TABLE
!= i_nCommandType
)
71 SAL_WARN("dbaccess.ui", "OTableCopyHelper::insertTable: invalid call (no supported format found)!" );
77 Reference
<XConnection
> xSrcConnection( i_rSourceConnection
);
78 if ( i_rSourceDataSource
== i_rDestDataSource
)
79 xSrcConnection
= i_rDestConnection
;
81 if ( !xSrcConnection
.is() || !i_rDestConnection
.is() )
83 SAL_WARN("dbaccess.ui", "OTableCopyHelper::insertTable: no connection/s!" );
87 Reference
<XComponentContext
> aContext( m_pController
->getORB() );
89 Reference
< XDataAccessDescriptorFactory
> xFactory( DataAccessDescriptorFactory::get( aContext
) );
91 Reference
< XPropertySet
> xSource( xFactory
->createDataAccessDescriptor(), UNO_SET_THROW
);
92 xSource
->setPropertyValue( PROPERTY_COMMAND_TYPE
, makeAny( i_nCommandType
) );
93 xSource
->setPropertyValue( PROPERTY_COMMAND
, makeAny( i_rCommand
) );
94 xSource
->setPropertyValue( PROPERTY_ACTIVE_CONNECTION
, makeAny( xSrcConnection
) );
95 xSource
->setPropertyValue( PROPERTY_RESULT_SET
, makeAny( i_rSourceRows
) );
96 xSource
->setPropertyValue( PROPERTY_SELECTION
, makeAny( i_rSelection
) );
97 xSource
->setPropertyValue( PROPERTY_BOOKMARK_SELECTION
, makeAny( i_bBookmarkSelection
) );
99 Reference
< XPropertySet
> xDest( xFactory
->createDataAccessDescriptor(), UNO_SET_THROW
);
100 xDest
->setPropertyValue( PROPERTY_ACTIVE_CONNECTION
, makeAny( i_rDestConnection
) );
102 auto xInteractionHandler
= InteractionHandler::createWithParent(aContext
, VCLUnoHelper::GetInterface(m_pController
->getView()));
104 Reference
<XCopyTableWizard
> xWizard(CopyTableWizard::createWithInteractionHandler(aContext
, xSource
, xDest
, xInteractionHandler
), UNO_SET_THROW
);
106 OUString
sTableNameForAppend( GetTableNameForAppend() );
107 xWizard
->setDestinationTableName( GetTableNameForAppend() );
109 bool bAppendToExisting
= !sTableNameForAppend
.isEmpty();
110 xWizard
->setOperation( bAppendToExisting
? CopyTableOperation::AppendData
: CopyTableOperation::CopyDefinitionAndData
);
114 catch( const SQLException
& )
116 m_pController
->showError( SQLExceptionInfo( ::cppu::getCaughtException() ) );
118 catch( const Exception
& )
120 DBG_UNHANDLED_EXCEPTION("dbaccess");
124 void OTableCopyHelper::pasteTable( const svx::ODataAccessDescriptor
& _rPasteData
, const OUString
& i_rDestDataSourceName
,
125 const SharedConnection
& i_rDestConnection
)
127 OUString sSrcDataSourceName
= _rPasteData
.getDataSource();
130 _rPasteData
[ DataAccessDescriptorProperty::Command
] >>= sCommand
;
132 Reference
<XConnection
> xSrcConnection
;
133 if ( _rPasteData
.has(DataAccessDescriptorProperty::Connection
) )
135 OSL_VERIFY( _rPasteData
[DataAccessDescriptorProperty::Connection
] >>= xSrcConnection
);
138 Reference
< XResultSet
> xResultSet
;
139 if ( _rPasteData
.has(DataAccessDescriptorProperty::Cursor
) )
141 OSL_VERIFY( _rPasteData
[ DataAccessDescriptorProperty::Cursor
] >>= xResultSet
);
144 Sequence
< Any
> aSelection
;
145 if ( _rPasteData
.has( DataAccessDescriptorProperty::Selection
) )
147 OSL_VERIFY( _rPasteData
[ DataAccessDescriptorProperty::Selection
] >>= aSelection
);
148 OSL_ENSURE( _rPasteData
.has( DataAccessDescriptorProperty::BookmarkSelection
), "OTableCopyHelper::pasteTable: you should specify BookmarkSelection, too, to be on the safe side!" );
151 bool bBookmarkSelection( true );
152 if ( _rPasteData
.has( DataAccessDescriptorProperty::BookmarkSelection
) )
154 OSL_VERIFY( _rPasteData
[ DataAccessDescriptorProperty::BookmarkSelection
] >>= bBookmarkSelection
);
156 OSL_ENSURE( bBookmarkSelection
, "OTableCopyHelper::pasteTable: working with selection-indices (instead of bookmarks) is error-prone, and thus deprecated!" );
158 sal_Int32 nCommandType
= CommandType::COMMAND
;
159 if ( _rPasteData
.has(DataAccessDescriptorProperty::CommandType
) )
160 _rPasteData
[DataAccessDescriptorProperty::CommandType
] >>= nCommandType
;
162 insertTable( sSrcDataSourceName
, xSrcConnection
, sCommand
, nCommandType
,
163 xResultSet
, aSelection
, bBookmarkSelection
,
164 i_rDestDataSourceName
, i_rDestConnection
);
167 void OTableCopyHelper::pasteTable( SotClipboardFormatId _nFormatId
168 ,const TransferableDataHelper
& _rTransData
169 ,const OUString
& i_rDestDataSource
170 ,const SharedConnection
& _xConnection
)
172 if ( _nFormatId
== SotClipboardFormatId::DBACCESS_TABLE
|| _nFormatId
== SotClipboardFormatId::DBACCESS_QUERY
)
174 if ( ODataAccessObjectTransferable::canExtractObjectDescriptor(_rTransData
.GetDataFlavorExVector()) )
176 svx::ODataAccessDescriptor aPasteData
= ODataAccessObjectTransferable::extractObjectDescriptor(_rTransData
);
177 pasteTable( aPasteData
,i_rDestDataSource
,_xConnection
);
180 else if ( _rTransData
.HasFormat(_nFormatId
) )
184 DropDescriptor aTrans
;
186 if ( _nFormatId
!= SotClipboardFormatId::RTF
)
187 bOk
= const_cast<TransferableDataHelper
&>(_rTransData
).GetSotStorageStream(SotClipboardFormatId::HTML
,aTrans
.aHtmlRtfStorage
);
189 bOk
= const_cast<TransferableDataHelper
&>(_rTransData
).GetSotStorageStream(SotClipboardFormatId::RTF
,aTrans
.aHtmlRtfStorage
);
191 aTrans
.nType
= E_TABLE
;
192 aTrans
.bHtml
= SotClipboardFormatId::HTML
== _nFormatId
;
193 aTrans
.sDefaultTableName
= GetTableNameForAppend();
194 if ( !bOk
|| !copyTagTable(aTrans
,false,_xConnection
) )
195 m_pController
->showError(SQLException(DBA_RES(STR_NO_TABLE_FORMAT_INSIDE
), *m_pController
, "S1000", 0, Any()));
197 catch(const SQLException
&)
199 m_pController
->showError( SQLExceptionInfo( ::cppu::getCaughtException() ) );
201 catch( const Exception
& )
203 DBG_UNHANDLED_EXCEPTION("dbaccess");
207 m_pController
->showError(SQLException(DBA_RES(STR_NO_TABLE_FORMAT_INSIDE
), *m_pController
, "S1000", 0, Any()));
210 void OTableCopyHelper::pasteTable( const TransferableDataHelper
& _rTransData
211 ,const OUString
& i_rDestDataSource
212 ,const SharedConnection
& _xConnection
)
214 if ( _rTransData
.HasFormat(SotClipboardFormatId::DBACCESS_TABLE
) || _rTransData
.HasFormat(SotClipboardFormatId::DBACCESS_QUERY
) )
215 pasteTable( SotClipboardFormatId::DBACCESS_TABLE
,_rTransData
,i_rDestDataSource
,_xConnection
);
216 else if ( _rTransData
.HasFormat(SotClipboardFormatId::HTML
) )
217 pasteTable( SotClipboardFormatId::HTML
,_rTransData
,i_rDestDataSource
,_xConnection
);
218 else if ( _rTransData
.HasFormat(SotClipboardFormatId::RTF
) )
219 pasteTable( SotClipboardFormatId::RTF
,_rTransData
,i_rDestDataSource
,_xConnection
);
222 bool OTableCopyHelper::copyTagTable(OTableCopyHelper::DropDescriptor
const & _rDesc
, bool _bCheck
, const SharedConnection
& _xConnection
)
224 Reference
<XEventListener
> xEvt
;
225 ODatabaseImportExport
* pImport
= nullptr;
227 pImport
= new OHTMLImportExport(_xConnection
,getNumberFormatter(_xConnection
, m_pController
->getORB()),m_pController
->getORB());
229 pImport
= new ORTFImportExport(_xConnection
,getNumberFormatter(_xConnection
, m_pController
->getORB()),m_pController
->getORB());
232 SvStream
* pStream
= _rDesc
.aHtmlRtfStorage
.get();
234 pImport
->enableCheckOnly();
236 //set the selected tablename
237 pImport
->setSTableName(_rDesc
.sDefaultTableName
);
239 pImport
->setStream(pStream
);
240 return pImport
->Read();
243 bool OTableCopyHelper::isTableFormat(const TransferableDataHelper
& _rClipboard
)
245 bool bTableFormat
= _rClipboard
.HasFormat(SotClipboardFormatId::DBACCESS_TABLE
)
246 || _rClipboard
.HasFormat(SotClipboardFormatId::DBACCESS_QUERY
)
247 || _rClipboard
.HasFormat(SotClipboardFormatId::RTF
)
248 || _rClipboard
.HasFormat(SotClipboardFormatId::HTML
);
253 bool OTableCopyHelper::copyTagTable(const TransferableDataHelper
& _aDroppedData
254 ,DropDescriptor
& _rAsyncDrop
255 ,const SharedConnection
& _xConnection
)
258 bool bHtml
= _aDroppedData
.HasFormat(SotClipboardFormatId::HTML
);
259 if ( bHtml
|| _aDroppedData
.HasFormat(SotClipboardFormatId::RTF
) )
263 bOk
= const_cast<TransferableDataHelper
&>(_aDroppedData
).GetSotStorageStream(SotClipboardFormatId::HTML
,_rAsyncDrop
.aHtmlRtfStorage
);
265 bOk
= const_cast<TransferableDataHelper
&>(_aDroppedData
).GetSotStorageStream(SotClipboardFormatId::RTF
,_rAsyncDrop
.aHtmlRtfStorage
);
267 _rAsyncDrop
.bHtml
= bHtml
;
268 _rAsyncDrop
.bError
= !copyTagTable(_rAsyncDrop
,true,_xConnection
);
270 bRet
= ( !_rAsyncDrop
.bError
&& bOk
&& _rAsyncDrop
.aHtmlRtfStorage
.is() );
273 // now we need to copy the stream
274 ::utl::TempFile aTmp
;
275 _rAsyncDrop
.aUrl
= aTmp
.GetURL();
276 ::tools::SvRef
<SotStorageStream
> aNew
= new SotStorageStream( aTmp
.GetFileName() );
277 _rAsyncDrop
.aHtmlRtfStorage
->Seek(STREAM_SEEK_TO_BEGIN
);
278 _rAsyncDrop
.aHtmlRtfStorage
->CopyTo( aNew
.get() );
280 _rAsyncDrop
.aHtmlRtfStorage
= aNew
;
283 _rAsyncDrop
.aHtmlRtfStorage
= nullptr;
288 void OTableCopyHelper::asyncCopyTagTable( DropDescriptor
& _rDesc
289 ,const OUString
& i_rDestDataSource
290 ,const SharedConnection
& _xConnection
)
292 if ( _rDesc
.aHtmlRtfStorage
.is() )
294 copyTagTable(_rDesc
,false,_xConnection
);
295 _rDesc
.aHtmlRtfStorage
= nullptr;
296 // we now have to delete the temp file created in executeDrop
298 aURL
.SetURL(_rDesc
.aUrl
);
299 ::utl::UCBContentHelper::Kill(aURL
.GetMainURL(INetURLObject::DecodeMechanism::NONE
));
301 else if ( !_rDesc
.bError
)
302 pasteTable(_rDesc
.aDroppedData
,i_rDestDataSource
,_xConnection
);
304 m_pController
->showError(SQLException(DBA_RES(STR_NO_TABLE_FORMAT_INSIDE
), *m_pController
, "S1000", 0, Any()));
309 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */