Coverage Report

Created: 2026-04-09 11:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/svx/source/form/fmexch.cxx
Line
Count
Source
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 <fmexch.hxx>
21
22
#include <sot/formats.hxx>
23
#include <tools/debug.hxx>
24
#include <comphelper/diagnose_ex.hxx>
25
26
namespace svxform
27
{
28
    using namespace ::com::sun::star::uno;
29
    using namespace ::com::sun::star::datatransfer;
30
31
    OLocalExchange::OLocalExchange( )
32
0
        :m_bDragging( false )
33
0
        ,m_bClipboardOwner( false )
34
0
    {
35
0
    }
36
37
    void OLocalExchange::copyToClipboard(const weld::Widget& rWidget, const GrantAccess&)
38
0
    {
39
0
        if ( m_bClipboardOwner )
40
0
        {   // simulate a lostOwnership to notify parties interested in
41
0
            m_aClipboardListener.Call( *this );
42
0
        }
43
44
0
        m_bClipboardOwner = true;
45
0
        CopyToClipboard(rWidget.get_clipboard());
46
0
    }
47
48
    void OLocalExchange::clear()
49
0
    {
50
0
        if ( !isClipboardOwner() )
51
0
            return;
52
53
0
        try
54
0
        {
55
0
            Reference< clipboard::XClipboard > xClipBoard( getOwnClipboard() );
56
0
            if ( xClipBoard.is() )
57
0
                xClipBoard->setContents( nullptr, nullptr );
58
0
        }
59
0
        catch( const Exception& )
60
0
        {
61
0
            DBG_UNHANDLED_EXCEPTION("svx");
62
0
        }
63
0
        m_bClipboardOwner = false;
64
0
    }
65
66
    void SAL_CALL OLocalExchange::lostOwnership( const Reference< clipboard::XClipboard >& _rxClipboard, const Reference< XTransferable >& _rxTrans )
67
0
    {
68
0
        TransferDataContainer::implCallOwnLostOwnership( _rxClipboard, _rxTrans );
69
0
        m_bClipboardOwner = false;
70
71
0
        m_aClipboardListener.Call( *this );
72
0
    }
73
74
    void OLocalExchange::setDragging(bool bDragging)
75
0
    {
76
0
        m_bDragging = bDragging;
77
0
    }
78
79
    void OLocalExchange::DragFinished( sal_Int8 nDropAction )
80
0
    {
81
0
        TransferDataContainer::DragFinished( nDropAction );
82
0
        setDragging(false);
83
0
    }
84
85
    bool OLocalExchange::hasFormat( const DataFlavorExVector& _rFormats, SotClipboardFormatId _nFormatId )
86
0
    {
87
0
        return std::any_of(_rFormats.begin(), _rFormats.end(),
88
0
            [&_nFormatId](const DataFlavorEx& rFormat) { return rFormat.mnSotId == _nFormatId; });
89
0
    }
90
91
    bool OLocalExchange::GetData( const css::datatransfer::DataFlavor& /*_rFlavor*/, const OUString& /*rDestDoc*/ )
92
0
    {
93
0
        return false;   // do not have any formats by default
94
0
    }
95
96
    OControlTransferData::OControlTransferData( )
97
0
        : m_bFocusEntry(false)
98
0
    {
99
0
    }
100
101
    OControlTransferData::OControlTransferData( const Reference< XTransferable >& _rxTransferable )
102
0
        : m_bFocusEntry(false)
103
0
    {
104
0
        TransferableDataHelper aExchangedData( _rxTransferable );
105
106
        // try the formats we know
107
0
        if ( OControlExchange::hasControlPathFormat( aExchangedData.GetDataFlavorExVector() ) )
108
0
        {   // paths to the controls, relative to a root
109
0
            Sequence< Any > aControlPathData;
110
0
            if ( aExchangedData.GetAny(OControlExchange::getControlPathFormatId(), OUString()) >>= aControlPathData )
111
0
            {
112
0
                DBG_ASSERT( aControlPathData.getLength() >= 2, "OControlTransferData::OControlTransferData: invalid data for the control path format!" );
113
0
                if ( aControlPathData.getLength() >= 2 )
114
0
                {
115
0
                    aControlPathData[0] >>= m_xFormsRoot;
116
0
                    aControlPathData[1] >>= m_aControlPaths;
117
0
                }
118
0
            }
119
0
            else
120
0
            {
121
0
                OSL_FAIL( "OControlTransferData::OControlTransferData: invalid data for the control path format (2)!" );
122
0
            }
123
0
        }
124
0
        if ( OControlExchange::hasHiddenControlModelsFormat( aExchangedData.GetDataFlavorExVector() ) )
125
0
        {   // sequence of models of hidden controls
126
0
            aExchangedData.GetAny(OControlExchange::getHiddenControlModelsFormatId(), OUString()) >>= m_aHiddenControlModels;
127
0
        }
128
129
0
        updateFormats( );
130
0
    }
131
132
133
    static bool lcl_fillDataFlavorEx( SotClipboardFormatId nId, DataFlavorEx& _rFlavor )
134
0
    {
135
0
        _rFlavor.mnSotId = nId;
136
0
        return SotExchange::GetFormatDataFlavor( _rFlavor.mnSotId, _rFlavor );
137
0
    }
138
139
140
    void OControlTransferData::updateFormats( )
141
0
    {
142
0
        m_aCurrentFormats.clear();
143
0
        m_aCurrentFormats.reserve( 3 );
144
145
0
        DataFlavorEx aFlavor;
146
147
0
        if ( m_aHiddenControlModels.hasElements() )
148
0
        {
149
0
            if ( lcl_fillDataFlavorEx( OControlExchange::getHiddenControlModelsFormatId(), aFlavor ) )
150
0
                m_aCurrentFormats.push_back( aFlavor );
151
0
        }
152
153
0
        if ( m_xFormsRoot.is() && m_aControlPaths.hasElements() )
154
0
        {
155
0
            if ( lcl_fillDataFlavorEx( OControlExchange::getControlPathFormatId(), aFlavor ) )
156
0
                m_aCurrentFormats.push_back( aFlavor );
157
0
        }
158
159
0
        if ( !m_aSelectedEntries.empty() )
160
0
        {
161
0
            if ( lcl_fillDataFlavorEx( OControlExchange::getFieldExchangeFormatId(), aFlavor ) )
162
0
                m_aCurrentFormats.push_back( aFlavor );
163
0
        }
164
0
    }
165
166
    size_t OControlTransferData::onEntryRemoved(const weld::TreeIter* _pEntry)
167
0
    {
168
0
        auto aIter = std::find_if(m_aSelectedEntries.begin(), m_aSelectedEntries.end(),
169
0
                                  [_pEntry](const auto& rElem) { return rElem->equal(*_pEntry); });
170
0
        if (aIter != m_aSelectedEntries.end())
171
0
            m_aSelectedEntries.erase(aIter);
172
173
0
        return m_aSelectedEntries.size();
174
0
    }
175
176
    void OControlTransferData::addSelectedEntry(std::unique_ptr<weld::TreeIter> xEntry)
177
0
    {
178
0
        m_aSelectedEntries.emplace(std::move(xEntry));
179
0
    }
180
181
    void OControlTransferData::setFocusEntry(bool _bFocusEntry)
182
0
    {
183
0
        m_bFocusEntry = _bFocusEntry;
184
0
    }
185
186
    void OControlTransferData::addHiddenControlsFormat(const css::uno::Sequence< css::uno::Reference< css::uno::XInterface > >& seqInterfaces)
187
0
    {
188
0
        m_aHiddenControlModels = seqInterfaces;
189
0
    }
190
191
    void OControlTransferData::buildPathFormat(const weld::TreeView* pTreeBox, const weld::TreeIter* pRoot)
192
0
    {
193
0
        m_aControlPaths.realloc(0);
194
195
0
        sal_Int32 nEntryCount = m_aSelectedEntries.size();
196
0
        if (nEntryCount == 0)
197
0
            return;
198
199
0
        m_aControlPaths.realloc(nEntryCount);
200
0
        css::uno::Sequence<sal_uInt32>* pAllPaths = m_aControlPaths.getArray();
201
0
        for (const auto& rCurrentEntry : m_aSelectedEntries)
202
0
        {
203
            // first we collect the path in an array
204
0
            ::std::vector< sal_uInt32 > aCurrentPath;
205
206
0
            std::unique_ptr<weld::TreeIter> xLoop(pTreeBox->make_iterator(rCurrentEntry.get()));
207
0
            while (!xLoop->equal(*pRoot))
208
0
            {
209
0
                aCurrentPath.push_back(pTreeBox->get_iter_index_in_parent(*xLoop));
210
0
                bool bLoop = pTreeBox->iter_parent(*xLoop);
211
0
                assert(bLoop && "OControlTransferData::buildPathFormat: invalid root or entry !"); (void)bLoop;
212
0
            }
213
214
            // then we can transfer it into css::uno::Sequence
215
0
            Sequence<sal_uInt32>& rCurrentPath = *pAllPaths;
216
0
            sal_Int32 nDepth = aCurrentPath.size();
217
218
0
            rCurrentPath.realloc(nDepth);
219
0
            sal_uInt32* pSeq = rCurrentPath.getArray();
220
0
            sal_Int32 j,k;
221
0
            for (j = nDepth - 1, k = 0; k<nDepth; --j, ++k)
222
0
                pSeq[j] = aCurrentPath[k];
223
0
            ++pAllPaths;
224
0
        }
225
0
    }
226
227
    void OControlTransferData::buildListFromPath(const weld::TreeView* pTreeBox, const weld::TreeIter* pRoot)
228
0
    {
229
0
        ListBoxEntrySet().swap(m_aSelectedEntries);
230
231
0
        for (const css::uno::Sequence<sal_uInt32>& rPaths : m_aControlPaths)
232
0
        {
233
0
            std::unique_ptr<weld::TreeIter> xSearch(pTreeBox->make_iterator(pRoot));
234
0
            for (const sal_uInt32 nThisPath : rPaths)
235
0
                pTreeBox->iter_nth_child(*xSearch, nThisPath);
236
0
            m_aSelectedEntries.emplace(std::move(xSearch));
237
0
        }
238
0
    }
239
240
    OControlExchange::OControlExchange( )
241
0
    {
242
0
    }
243
244
    bool OControlExchange::GetData( const DataFlavor& _rFlavor, const OUString& rDestDoc )
245
0
    {
246
0
        const SotClipboardFormatId nFormatId = SotExchange::GetFormat( _rFlavor );
247
248
0
        if ( getControlPathFormatId( ) == nFormatId )
249
0
        {
250
            // ugly. We have to pack all the info into one object
251
0
            Sequence< Any > aCompleteInfo( 2 );
252
0
            OSL_ENSURE( m_xFormsRoot.is(), "OLocalExchange::GetData: invalid forms root for this format!" );
253
0
            aCompleteInfo.getArray()[ 0 ] <<= m_xFormsRoot;
254
0
            aCompleteInfo.getArray()[ 1 ] <<= m_aControlPaths;
255
256
0
            SetAny( Any( aCompleteInfo ) );
257
0
        }
258
0
        else if ( getHiddenControlModelsFormatId() == nFormatId )
259
0
        {
260
            // just need to transfer the models
261
0
            SetAny( Any( m_aHiddenControlModels ) );
262
0
        }
263
0
        else
264
0
            return OLocalExchange::GetData(_rFlavor, rDestDoc);
265
266
0
        return true;
267
0
    }
268
269
    void OControlExchange::AddSupportedFormats()
270
0
    {
271
0
        if (m_bFocusEntry && !m_aSelectedEntries.empty())
272
0
            AddFormat(getFieldExchangeFormatId());
273
274
0
        if (m_aControlPaths.hasElements())
275
0
            AddFormat(getControlPathFormatId());
276
277
0
        if (m_aHiddenControlModels.hasElements())
278
0
            AddFormat(getHiddenControlModelsFormatId());
279
0
    }
280
281
    SotClipboardFormatId OControlExchange::getControlPathFormatId()
282
0
    {
283
0
        static SotClipboardFormatId s_nFormat =
284
0
            SotExchange::RegisterFormatName(u"application/x-openoffice;windows_formatname=\"svxform.ControlPathExchange\""_ustr);
285
0
        DBG_ASSERT(static_cast<SotClipboardFormatId>(-1) != s_nFormat, "OControlExchange::getControlPathFormatId: bad exchange id!");
286
0
        return s_nFormat;
287
0
    }
288
289
    SotClipboardFormatId OControlExchange::getHiddenControlModelsFormatId()
290
0
    {
291
0
        static SotClipboardFormatId s_nFormat =
292
0
            SotExchange::RegisterFormatName(u"application/x-openoffice;windows_formatname=\"svxform.HiddenControlModelsExchange\""_ustr);
293
0
        DBG_ASSERT(static_cast<SotClipboardFormatId>(-1) != s_nFormat, "OControlExchange::getHiddenControlModelsFormatId: bad exchange id!");
294
0
        return s_nFormat;
295
0
    }
296
297
298
    SotClipboardFormatId OControlExchange::getFieldExchangeFormatId()
299
0
    {
300
0
        static SotClipboardFormatId s_nFormat =
301
0
            SotExchange::RegisterFormatName(u"application/x-openoffice;windows_formatname=\"svxform.FieldNameExchange\""_ustr);
302
0
        DBG_ASSERT(static_cast<SotClipboardFormatId>(-1) != s_nFormat, "OControlExchange::getFieldExchangeFormatId: bad exchange id!");
303
0
        return s_nFormat;
304
0
    }
305
306
    //= OControlExchangeHelper
307
    rtl::Reference<OLocalExchange> OControlExchangeHelper::createExchange() const
308
0
    {
309
0
        return new OControlExchange;
310
0
    }
311
312
    OLocalExchangeHelper::OLocalExchangeHelper()
313
0
    {
314
0
    }
315
316
    OLocalExchangeHelper::~OLocalExchangeHelper()
317
0
    {
318
0
        implReset();
319
0
    }
320
321
    void OLocalExchangeHelper::copyToClipboard(const weld::Widget& rWidget) const
322
0
    {
323
0
        DBG_ASSERT( m_xTransferable.is(), "OLocalExchangeHelper::copyToClipboard: not prepared!" );
324
0
        m_xTransferable->copyToClipboard(rWidget, OLocalExchange::GrantAccess());
325
0
    }
326
327
    void OLocalExchangeHelper::implReset()
328
0
    {
329
0
        if (m_xTransferable.is())
330
0
        {
331
0
            m_xTransferable->setClipboardListener( Link<OLocalExchange&,void>() );
332
0
            m_xTransferable.clear();
333
0
        }
334
0
    }
335
336
    void OLocalExchangeHelper::prepareDrag( )
337
0
    {
338
0
        DBG_ASSERT(!m_xTransferable.is() || !m_xTransferable->isDragging(), "OLocalExchangeHelper::prepareDrag: recursive DnD?");
339
340
0
        implReset();
341
0
        m_xTransferable = createExchange();
342
0
    }
343
}
344
345
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */