LibreOffice Module dbaccess (master) 1
dsbrowserDnD.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 <dbtreelistbox.hxx>
22#include "dbtreemodel.hxx"
23#include <UITools.hxx>
24#include <unodatbr.hxx>
25
26#include <com/sun/star/frame/XStorable.hpp>
27#include <com/sun/star/sdb/CommandType.hpp>
28#include <com/sun/star/sdbc/SQLException.hpp>
29
35#include <osl/diagnose.h>
36#include <vcl/svapp.hxx>
37
38#include <algorithm>
39
40namespace dbaui
41{
42
43 using namespace ::com::sun::star::uno;
44 using namespace ::com::sun::star::sdb;
45 using namespace ::com::sun::star::sdbc;
46 using namespace ::com::sun::star::sdbcx;
47 using namespace ::com::sun::star::beans;
48 using namespace ::com::sun::star::util;
49 using namespace ::com::sun::star::frame;
50 using namespace ::com::sun::star::container;
51 using namespace ::com::sun::star::lang;
52 using namespace ::com::sun::star::form;
53 using namespace ::com::sun::star::io;
54 using namespace ::com::sun::star::i18n;
55 using namespace ::com::sun::star::task;
56 using namespace ::com::sun::star::datatransfer;
57 using namespace ::dbtools;
58 using namespace ::svx;
59
60 bool SbaTableQueryBrowser::implCopyObject(ODataClipboard& rExchange, const weld::TreeIter& rApplyTo, sal_Int32 nCommandType)
61 {
62 try
63 {
64 OUString aName = GetEntryText(rApplyTo);
65 std::unique_ptr<weld::TreeIter> xRootEntry(m_pTreeView->GetRootLevelParent(&rApplyTo));
66 OUString aDSName = getDataSourceAccessor(*xRootEntry);
67
68 SharedConnection xConnection;
69 if ( CommandType::QUERY != nCommandType )
70 {
71 if (!ensureConnection(&rApplyTo, xConnection))
72 return false;
73 rExchange.Update(aDSName, nCommandType, aName, xConnection, getNumberFormatter(), getORB());
74 }
75 else
76 rExchange.Update(aDSName, nCommandType, aName, getNumberFormatter(), getORB());
77
78 // the ownership goes to ODataClipboards
79 return true;
80 }
81 catch(const SQLException& )
82 {
83 showError( SQLExceptionInfo( ::cppu::getCaughtException() ) );
84 }
85 catch( const Exception& )
86 {
87 DBG_UNHANDLED_EXCEPTION("dbaccess");
88 }
89 return false;
90 }
91
93 {
94 // check if we're a table or query container
95 weld::TreeView& rTreeView = m_pTreeView->GetWidget();
96 std::unique_ptr<weld::TreeIter> xHitEntry(rTreeView.make_iterator());
97 // get_dest_row_at_pos with false cause no drop if no entry was hit exactly
98 if (rTreeView.get_dest_row_at_pos(_rEvt.maPosPixel, xHitEntry.get(), false))
99 {
100 // it must be a container
101 EntryType eEntryType = getEntryType(*xHitEntry);
102 SharedConnection xConnection;
103 if ( eEntryType == etTableContainer && ensureConnection(xHitEntry.get(), xConnection ) && xConnection.is())
104 {
105 Reference<XChild> xChild(xConnection,UNO_QUERY);
107 if ( xChild.is() )
108 xStore.set( getDataSourceOrModel(xChild->getParent()), UNO_QUERY );
109 // check for the concrete type
110 if ( xStore.is() && !xStore->isReadonly() && std::any_of(_rFlavors.begin(),_rFlavors.end(),TAppSupportedSotFunctor(E_TABLE)) )
111 return DND_ACTION_COPY;
112 }
113 }
114
115 return DND_ACTION_NONE;
116 }
118 {
119 weld::TreeView& rTreeView = m_pTreeView->GetWidget();
120 std::unique_ptr<weld::TreeIter> xHitEntry(rTreeView.make_iterator());
121 // get_dest_row_at_pos with false cause no drop if no entry was hit exactly
122 if (!rTreeView.get_dest_row_at_pos(_rEvt.maPosPixel, xHitEntry.get(), false))
123 return DND_ACTION_NONE;
124 EntryType eEntryType = getEntryType(*xHitEntry);
125 if (!isContainer(eEntryType))
126 {
127 OSL_FAIL("SbaTableQueryBrowser::executeDrop: what the hell did queryDrop do?");
128 // queryDrop should not have allowed us to reach this situation...
129 return DND_ACTION_NONE;
130 }
131 // a TransferableDataHelper for accessing the dropped data
132 TransferableDataHelper aDroppedData(_rEvt.maDropEvent.Transferable);
133
134 // reset the data of the previous async drop (if any)
135 if ( m_nAsyncDrop )
137
138 m_nAsyncDrop = nullptr;
142 m_aAsyncDrop.bError = false;
143 m_aAsyncDrop.bHtml = false;
144 m_aAsyncDrop.xDroppedAt.reset();
145 m_aAsyncDrop.aUrl.clear();
146
147 // loop through the available formats and see what we can do ...
148 // first we have to check if it is our own format, if not we have to copy the stream :-(
149 if ( ODataAccessObjectTransferable::canExtractObjectDescriptor(aDroppedData.GetDataFlavorExVector()) )
150 {
151 m_aAsyncDrop.aDroppedData = ODataAccessObjectTransferable::extractObjectDescriptor(aDroppedData);
152 m_aAsyncDrop.xDroppedAt = std::move(xHitEntry);
153
154 // asynchron because we some dialogs and we aren't allowed to show them while in D&D
156 return DND_ACTION_COPY;
157 }
158 else
159 {
160 SharedConnection xDestConnection;
161 if ( ensureConnection( xHitEntry.get(), xDestConnection )
162 && xDestConnection.is()
163 && m_aTableCopyHelper.copyTagTable( aDroppedData, m_aAsyncDrop, xDestConnection )
164 )
165 {
166 m_aAsyncDrop.xDroppedAt = std::move(xHitEntry);
167
168 // asynchron because we some dialogs and we aren't allowed to show them while in D&D
170 return DND_ACTION_COPY;
171 }
172 }
173
174 return DND_ACTION_NONE;
175 }
176
178 {
179 // it must be a query/table
180 EntryType eEntryType = getEntryType(rEntry);
181 if (!isObject(eEntryType))
182 return false;
183
184 ODataClipboard& rExchange = static_cast<ODataClipboard&>(m_pTreeView->GetDataTransfer());
185 return implCopyObject(rExchange, rEntry, (etTableOrView == eEntryType) ? CommandType::TABLE : CommandType::QUERY);
186 }
187
189 {
190 weld::TreeView& rTreeView = m_pTreeView->GetWidget();
191 std::unique_ptr<weld::TreeIter> xSelected = rTreeView.make_iterator();
192 if (rTreeView.get_selected(xSelected.get()) && isEntryCopyAllowed(*xSelected))
193 copyEntry(*xSelected);
194 }
195
197 {
198 EntryType eType = getEntryType(rEntry);
199 return ( eType == etTableOrView || eType == etQuery );
200 }
201
203 {
204 EntryType eType = getEntryType(rEntry);
206 if (implCopyObject(*xTransfer, rEntry, eType == etQuery ? CommandType::QUERY : CommandType::TABLE))
207 xTransfer->CopyToClipboard(getView());
208 }
209
210 IMPL_LINK_NOARG( SbaTableQueryBrowser, OnAsyncDrop, void*, void )
211 {
212 m_nAsyncDrop = nullptr;
213 SolarMutexGuard aSolarGuard;
214 ::osl::MutexGuard aGuard( getMutex() );
215
216 if ( m_aAsyncDrop.nType == E_TABLE )
217 {
218 SharedConnection xDestConnection;
219 if ( ensureConnection(m_aAsyncDrop.xDroppedAt.get(), xDestConnection) && xDestConnection.is())
220 {
221 std::unique_ptr<weld::TreeIter> xDataSourceEntry =
222 m_pTreeView->GetRootLevelParent(m_aAsyncDrop.xDroppedAt.get());
223 m_aTableCopyHelper.asyncCopyTagTable(m_aAsyncDrop, getDataSourceAccessor(*xDataSourceEntry), xDestConnection);
224 }
225 }
226
227 m_aAsyncDrop.aDroppedData.clear();
228 }
229
231 {
232 weld::TreeView& rTreeView = m_pTreeView->GetWidget();
233 rTreeView.all_foreach([this, &rTreeView](weld::TreeIter& rEntryLoop){
234 // clear the user data of the tree model
235 DBTreeListUserData* pData = weld::fromId<DBTreeListUserData*>(rTreeView.get_id(rEntryLoop));
236 if (pData)
237 {
238 rTreeView.set_id(rEntryLoop, OUString());
239 Reference<XContainer> xContainer(pData->xContainer, UNO_QUERY);
240 if (xContainer.is())
241 xContainer->removeContainerListener(this);
242
243 if (pData->xConnection.is())
244 {
245 // connections are to be stored *only* at the data source entries
246 impl_releaseConnection(pData->xConnection);
247 }
248
249 delete pData;
250 }
251 return false;
252 });
253
254 m_xCurrentlyDisplayed.reset();
255 }
256} // namespace dbaui
257
258/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static ImplSVEvent * PostUserEvent(const Link< void *, void > &rLink, void *pCaller=nullptr, bool bReferenceLink=false)
static void RemoveUserEvent(ImplSVEvent *nUserEvent)
const DataFlavorExVector & GetDataFlavorExVector() const
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)
bool copyTagTable(DropDescriptor const &_rDesc, bool _bCheck, const SharedConnection &_xConnection)
copies a table which was constructed by tags like HTML or RTF
void clearTreeModel()
clears the tree list box
virtual sal_Int8 executeDrop(const ExecuteDropEvent &_rEvt) override
execute a drop request
bool ensureConnection(const weld::TreeIter *pDSEntry, void *pDSData, SharedConnection &rConnection)
Definition: unodatbr.cxx:3346
static bool isObject(EntryType _eType)
Definition: unodatbr.hxx:320
VclPtr< InterimDBTreeListBox > m_pTreeView
Definition: unodatbr.hxx:96
bool isEntryCopyAllowed(const weld::TreeIter &rEntry) const
void impl_releaseConnection(SharedConnection &_rxConnection)
flushes and disposes the given connection, and de-registers as listener
Definition: unodatbr.cxx:2936
OTableCopyHelper::DropDescriptor m_aAsyncDrop
Definition: unodatbr.hxx:90
std::unique_ptr< weld::TreeIter > m_xCurrentlyDisplayed
Definition: unodatbr.hxx:98
bool implCopyObject(ODataClipboard &rExchange, const weld::TreeIter &rApplyTo, sal_Int32 nCommandType)
OUString getDataSourceAccessor(const weld::TreeIter &rDataSourceEntry) const
retrieves the data source URL/name for the given entry representing a data source
Definition: unodatbr.cxx:1055
virtual sal_Int8 queryDrop(const AcceptDropEvent &_rEvt, const DataFlavorExVector &_rFlavors) override
check whether or not a drop request should be accepted
virtual bool requestDrag(const weld::TreeIter &rEntry) override
handler for StartDrag requests
OUString GetEntryText(const weld::TreeIter &rEntry) const
ImplSVEvent * m_nAsyncDrop
Definition: unodatbr.hxx:99
static bool isContainer(EntryType _eType)
Definition: unodatbr.hxx:321
void copyEntry(const weld::TreeIter &rEntry)
EntryType getEntryType(const weld::TreeIter &rEntry) const
OTableCopyHelper m_aTableCopyHelper
Definition: unodatbr.hxx:91
const css::uno::Reference< css::util::XNumberFormatter > & getNumberFormatter() const
Definition: brwctrlr.hxx:126
virtual std::unique_ptr< TreeIter > make_iterator(const TreeIter *pOrig=nullptr) const=0
virtual bool get_selected(TreeIter *pIter) const=0
virtual bool get_dest_row_at_pos(const Point &rPos, weld::TreeIter *pResult, bool bDnDMode, bool bAutoScroll=true)=0
virtual void set_id(int row, const OUString &rId)=0
virtual void all_foreach(const std::function< bool(TreeIter &)> &func)=0
virtual OUString get_id(int pos) const=0
#define DBG_UNHANDLED_EXCEPTION(...)
::std::vector< DataFlavorEx > DataFlavorExVector
OUString eType
Definition: generalpage.cxx:78
OUString aName
std::unique_ptr< sal_Int32[]> pData
@ Exception
IMPL_LINK_NOARG(OApplicationController, OnClipboardChanged, TransferableDataHelper *, void)
css::uno::Reference< css::uno::XInterface > getDataSourceOrModel(const css::uno::Reference< css::uno::XInterface > &_xObject)
returns either the model when data source is given as parameter, or returns a data source when a mode...
void showError(const SQLExceptionInfo &_rInfo, const Reference< XWindow > &_xParent, const Reference< XComponentContext > &_rxContext)
::osl::Mutex & getMutex()
SwView * getView(const uno::Reference< frame::XModel > &xModel)
const css::datatransfer::dnd::DropTargetDropEvent maDropEvent
svx::ODataAccessDescriptor aDroppedData
std::unique_ptr< weld::TreeIter > xDroppedAt
Functor object for class DataFlavorExVector::value_type returntype is bool.
#define DND_ACTION_COPY
#define DND_ACTION_NONE
signed char sal_Int8