nss: upgrade to release 3.73
[LibreOffice.git] / dbaccess / source / ui / browser / dsbrowserDnD.cxx
blob7d51a5abf8fb45ba0de9f61bab99d65e79d819e8
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <dbexchange.hxx>
21 #include <dbtreelistbox.hxx>
22 #include "dbtreemodel.hxx"
23 #include <UITools.hxx>
24 #include <unodatbr.hxx>
26 #include <com/sun/star/frame/XStorable.hpp>
27 #include <com/sun/star/sdb/CommandType.hpp>
28 #include <com/sun/star/sdbc/SQLException.hpp>
30 #include <connectivity/dbexception.hxx>
31 #include <connectivity/dbtools.hxx>
32 #include <cppuhelper/exc_hlp.hxx>
33 #include <svx/dataaccessdescriptor.hxx>
34 #include <tools/diagnose_ex.h>
35 #include <osl/diagnose.h>
36 #include <vcl/svapp.hxx>
38 #include <algorithm>
40 namespace dbaui
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;
60 bool SbaTableQueryBrowser::implCopyObject(ODataClipboard& rExchange, const weld::TreeIter& rApplyTo, sal_Int32 nCommandType)
62 try
64 OUString aName = GetEntryText(rApplyTo);
65 std::unique_ptr<weld::TreeIter> xRootEntry(m_pTreeView->GetRootLevelParent(&rApplyTo));
66 OUString aDSName = getDataSourceAccessor(*xRootEntry);
68 SharedConnection xConnection;
69 if ( CommandType::QUERY != nCommandType )
71 if (!ensureConnection(&rApplyTo, xConnection))
72 return false;
73 rExchange.Update(aDSName, nCommandType, aName, xConnection, getNumberFormatter(), getORB());
75 else
76 rExchange.Update(aDSName, nCommandType, aName, getNumberFormatter(), getORB());
78 // the ownership goes to ODataClipboards
79 return true;
81 catch(const SQLException& )
83 showError( SQLExceptionInfo( ::cppu::getCaughtException() ) );
85 catch( const Exception& )
87 DBG_UNHANDLED_EXCEPTION("dbaccess");
89 return false;
92 sal_Int8 SbaTableQueryBrowser::queryDrop( const AcceptDropEvent& _rEvt, const DataFlavorExVector& _rFlavors )
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))
100 // it must be a container
101 EntryType eEntryType = getEntryType(*xHitEntry);
102 SharedConnection xConnection;
103 if ( eEntryType == etTableContainer && ensureConnection(xHitEntry.get(), xConnection ) && xConnection.is())
105 Reference<XChild> xChild(xConnection,UNO_QUERY);
106 Reference<XStorable> xStore;
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;
115 return DND_ACTION_NONE;
117 sal_Int8 SbaTableQueryBrowser::executeDrop( const ExecuteDropEvent& _rEvt )
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))
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;
131 // a TransferableDataHelper for accessing the dropped data
132 TransferableDataHelper aDroppedData(_rEvt.maDropEvent.Transferable);
134 // reset the data of the previous async drop (if any)
135 if ( m_nAsyncDrop )
136 Application::RemoveUserEvent(m_nAsyncDrop);
138 m_nAsyncDrop = nullptr;
139 m_aAsyncDrop.aDroppedData.clear();
140 m_aAsyncDrop.nType = E_TABLE;
141 m_aAsyncDrop.nAction = _rEvt.mnAction;
142 m_aAsyncDrop.bError = false;
143 m_aAsyncDrop.bHtml = false;
144 m_aAsyncDrop.xDroppedAt.reset();
145 m_aAsyncDrop.aUrl.clear();
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()) )
151 m_aAsyncDrop.aDroppedData = ODataAccessObjectTransferable::extractObjectDescriptor(aDroppedData);
152 m_aAsyncDrop.xDroppedAt = std::move(xHitEntry);
154 // asynchron because we some dialogs and we aren't allowed to show them while in D&D
155 m_nAsyncDrop = Application::PostUserEvent(LINK(this, SbaTableQueryBrowser, OnAsyncDrop));
156 return DND_ACTION_COPY;
158 else
160 SharedConnection xDestConnection;
161 if ( ensureConnection( xHitEntry.get(), xDestConnection )
162 && xDestConnection.is()
163 && m_aTableCopyHelper.copyTagTable( aDroppedData, m_aAsyncDrop, xDestConnection )
166 m_aAsyncDrop.xDroppedAt = std::move(xHitEntry);
168 // asynchron because we some dialogs and we aren't allowed to show them while in D&D
169 m_nAsyncDrop = Application::PostUserEvent(LINK(this, SbaTableQueryBrowser, OnAsyncDrop));
170 return DND_ACTION_COPY;
174 return DND_ACTION_NONE;
177 bool SbaTableQueryBrowser::requestDrag(const weld::TreeIter& rEntry)
179 // it must be a query/table
180 EntryType eEntryType = getEntryType(rEntry);
181 if (!isObject(eEntryType))
182 return false;
184 ODataClipboard& rExchange = static_cast<ODataClipboard&>(m_pTreeView->GetDataTransfer());
185 return implCopyObject(rExchange, rEntry, (etTableOrView == eEntryType) ? CommandType::TABLE : CommandType::QUERY);
188 IMPL_LINK_NOARG(SbaTableQueryBrowser, OnCopyEntry, LinkParamNone*, void)
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);
196 bool SbaTableQueryBrowser::isEntryCopyAllowed(const weld::TreeIter& rEntry) const
198 EntryType eType = getEntryType(rEntry);
199 return ( eType == etTableOrView || eType == etQuery );
202 void SbaTableQueryBrowser::copyEntry(const weld::TreeIter& rEntry)
204 EntryType eType = getEntryType(rEntry);
205 rtl::Reference<ODataClipboard> xTransfer(new ODataClipboard);
206 if (implCopyObject(*xTransfer, rEntry, eType == etQuery ? CommandType::QUERY : CommandType::TABLE))
207 xTransfer->CopyToClipboard(getView());
210 IMPL_LINK_NOARG( SbaTableQueryBrowser, OnAsyncDrop, void*, void )
212 m_nAsyncDrop = nullptr;
213 SolarMutexGuard aSolarGuard;
214 ::osl::MutexGuard aGuard( getMutex() );
216 if ( m_aAsyncDrop.nType == E_TABLE )
218 SharedConnection xDestConnection;
219 if ( ensureConnection(m_aAsyncDrop.xDroppedAt.get(), xDestConnection) && xDestConnection.is())
221 std::unique_ptr<weld::TreeIter> xDataSourceEntry =
222 m_pTreeView->GetRootLevelParent(m_aAsyncDrop.xDroppedAt.get());
223 m_aTableCopyHelper.asyncCopyTagTable(m_aAsyncDrop, getDataSourceAccessor(*xDataSourceEntry), xDestConnection);
227 m_aAsyncDrop.aDroppedData.clear();
230 void SbaTableQueryBrowser::clearTreeModel()
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 = reinterpret_cast<DBTreeListUserData*>(rTreeView.get_id(rEntryLoop).toUInt64());
236 if (pData)
238 rTreeView.set_id(rEntryLoop, OUString());
239 Reference<XContainer> xContainer(pData->xContainer, UNO_QUERY);
240 if (xContainer.is())
241 xContainer->removeContainerListener(this);
243 if (pData->xConnection.is())
245 // connections are to be stored *only* at the data source entries
246 impl_releaseConnection(pData->xConnection);
249 delete pData;
251 return false;
254 m_xCurrentlyDisplayed.reset();
256 } // namespace dbaui
258 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */