Coverage Report

Created: 2026-10-14 09:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/svx/source/form/tabwin.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
21
#include <tabwin.hxx>
22
#include <fmservs.hxx>
23
24
#include <svl/itemset.hxx>
25
#include <svx/strings.hrc>
26
#include <svx/svxids.hrc>
27
#include <com/sun/star/beans/XPropertySet.hpp>
28
#include <com/sun/star/sdb/CommandType.hpp>
29
#include <com/sun/star/sdbc/XConnection.hpp>
30
#include <comphelper/processfactory.hxx>
31
#include <comphelper/types.hxx>
32
33
#include <helpids.h>
34
#include <svx/fmshell.hxx>
35
#include <fmshimp.hxx>
36
37
#include <fmprop.hxx>
38
39
#include <svx/dialmgr.hxx>
40
#include <sfx2/bindings.hxx>
41
#include <sfx2/objitem.hxx>
42
#include <sfx2/frame.hxx>
43
#include <svx/dataaccessdescriptor.hxx>
44
#include <vcl/weld/Builder.hxx>
45
#include <vcl/weld/Dialog.hxx>
46
#include <vcl/weld/weldutils.hxx>
47
#include <comphelper/diagnose_ex.hxx>
48
#include <tabwin.hrc>
49
#include <utility>
50
51
const tools::Long STD_WIN_SIZE_X = 120;
52
const tools::Long STD_WIN_SIZE_Y = 150;
53
54
using namespace ::com::sun::star::sdbc;
55
using namespace ::com::sun::star::sdb;
56
using namespace ::com::sun::star::uno;
57
using namespace ::com::sun::star::datatransfer;
58
using namespace ::com::sun::star::beans;
59
using namespace ::com::sun::star::lang;
60
using namespace ::com::sun::star::form;
61
using namespace ::com::sun::star::container;
62
using namespace ::com::sun::star;
63
using namespace ::svxform;
64
using namespace ::svx;
65
using namespace ::dbtools;
66
67
struct ColumnInfo
68
{
69
    OUString sColumnName;
70
    explicit ColumnInfo(OUString i_sColumnName)
71
0
        : sColumnName(std::move(i_sColumnName))
72
0
    {
73
0
    }
74
};
75
76
void FmFieldWin::addToList(const uno::Reference< container::XNameAccess>& i_xColumns )
77
0
{
78
0
    const uno::Sequence< OUString > aEntries = i_xColumns->getElementNames();
79
0
    for ( const OUString& rEntry : aEntries )
80
0
    {
81
0
        uno::Reference< beans::XPropertySet> xColumn(i_xColumns->getByName(rEntry),UNO_QUERY_THROW);
82
0
        OUString sLabel;
83
0
        if ( xColumn->getPropertySetInfo()->hasPropertyByName(FM_PROP_LABEL) )
84
0
            xColumn->getPropertyValue(FM_PROP_LABEL) >>= sLabel;
85
0
        m_aListBoxData.emplace_back(new ColumnInfo(rEntry));
86
0
        OUString sId(weld::toId(m_aListBoxData.back().get()));
87
0
        if ( !sLabel.isEmpty() )
88
0
            m_xListBox->append(sId, sLabel);
89
0
        else
90
0
            m_xListBox->append(sId, rEntry);
91
0
    }
92
0
}
93
94
IMPL_LINK(FmFieldWin, DragBeginHdl, bool&, rUnsetDragIcon, bool)
95
0
{
96
0
    rUnsetDragIcon = false;
97
98
0
    ColumnInfo* pSelected = weld::fromId<ColumnInfo*>(m_xListBox->get_selected_id());
99
0
    if (!pSelected)
100
0
    {
101
        // no drag without a field
102
0
        return true;
103
0
    }
104
105
0
    svx::ODataAccessDescriptor aDescriptor;
106
0
    aDescriptor[ DataAccessDescriptorProperty::DataSource ] <<= GetDatabaseName();
107
0
    aDescriptor[ DataAccessDescriptorProperty::Connection ] <<= GetConnection().getTyped();
108
0
    aDescriptor[ DataAccessDescriptorProperty::Command ]    <<= GetObjectName();
109
0
    aDescriptor[ DataAccessDescriptorProperty::CommandType ]<<= GetObjectType();
110
0
    aDescriptor[ DataAccessDescriptorProperty::ColumnName ] <<= pSelected->sColumnName;
111
112
0
    m_xHelper->setDescriptor(aDescriptor);
113
114
0
    return false;
115
0
}
116
117
FmFieldWin::FmFieldWin(SfxBindings* _pBindings, SfxChildWindow* _pMgr, weld::Window* _pParent)
118
0
    : SfxModelessDialogController(_pBindings, _pMgr, _pParent, u"svx/ui/formfielddialog.ui"_ustr, u"FormFieldDialog"_ustr)
119
0
    , SfxControllerItem(SID_FM_FIELDS_CONTROL, *_pBindings)
120
0
    , comphelper::OPropertyChangeListener2()
121
0
    , m_xListBox(m_xBuilder->weld_tree_view(u"treeview"_ustr))
122
0
    , m_nObjectType(0)
123
0
{
124
0
    m_xDialog->set_help_id(HID_FIELD_SEL_WIN);
125
0
    m_xListBox->set_help_id(HID_FIELD_SEL);
126
127
0
    m_xListBox->connect_item_activated(LINK(this, FmFieldWin, RowActivatedHdl));
128
0
    m_xHelper.set(new OColumnTransferable(
129
0
        ColumnTransferFormatFlags::FIELD_DESCRIPTOR | ColumnTransferFormatFlags::CONTROL_EXCHANGE | ColumnTransferFormatFlags::COLUMN_DESCRIPTOR
130
0
    ));
131
0
    rtl::Reference<TransferDataContainer> xHelper(m_xHelper);
132
0
    m_xListBox->enable_drag_source(xHelper, DND_ACTION_COPY);
133
0
    m_xListBox->connect_drag_begin(LINK(this, FmFieldWin, DragBeginHdl));
134
135
0
    UpdateContent(nullptr);
136
0
    m_xDialog->set_size_request(STD_WIN_SIZE_X, STD_WIN_SIZE_Y);
137
0
}
138
139
FmFieldWin::~FmFieldWin()
140
0
{
141
0
    {
142
0
        std::unique_lock g(m_aMutex);
143
0
        if (m_xChangeListener.is())
144
0
        {
145
0
            m_xChangeListener->dispose(g);
146
0
            m_xChangeListener.clear();
147
0
        }
148
0
    }
149
0
    ::SfxControllerItem::dispose();
150
0
}
151
152
IMPL_LINK_NOARG(FmFieldWin, RowActivatedHdl, const weld::TreeIter&, bool)
153
0
{
154
0
    return createSelectionControls();
155
0
}
156
157
bool FmFieldWin::createSelectionControls()
158
0
{
159
0
    ColumnInfo* pSelected = weld::fromId<ColumnInfo*>(m_xListBox->get_selected_id());
160
0
    if (pSelected)
161
0
    {
162
        // build a descriptor for the currently selected field
163
0
        ODataAccessDescriptor aDescr;
164
0
        aDescr.setDataSource(GetDatabaseName());
165
166
0
        aDescr[ DataAccessDescriptorProperty::Connection ]  <<= GetConnection().getTyped();
167
168
0
        aDescr[ DataAccessDescriptorProperty::Command ]     <<= GetObjectName();
169
0
        aDescr[ DataAccessDescriptorProperty::CommandType ] <<= GetObjectType();
170
0
        aDescr[ DataAccessDescriptorProperty::ColumnName ]  <<= pSelected->sColumnName;
171
172
        // transfer this to the SFX world
173
0
        SfxUnoAnyItem aDescriptorItem( SID_FM_DATACCESS_DESCRIPTOR, Any( aDescr.createPropertyValueSequence() ) );
174
0
        const SfxPoolItem* pArgs[] =
175
0
        {
176
0
            &aDescriptorItem, nullptr
177
0
        };
178
179
        // execute the create slot
180
0
        GetBindings().Execute( SID_FM_CREATE_FIELDCONTROL, pArgs );
181
0
    }
182
183
0
    return nullptr != pSelected;
184
0
}
185
186
void FmFieldWin::_propertyChanged(const css::beans::PropertyChangeEvent& evt)
187
0
{
188
0
    css::uno::Reference< css::form::XForm >  xForm(evt.Source, css::uno::UNO_QUERY);
189
0
    UpdateContent(xForm);
190
0
}
191
192
void FmFieldWin::StateChangedAtToolBoxControl(sal_uInt16 nSID, SfxItemState eState, const SfxPoolItem* pState)
193
0
{
194
0
    if (!pState  || SID_FM_FIELDS_CONTROL != nSID)
195
0
        return;
196
197
0
    if (eState >= SfxItemState::DEFAULT)
198
0
    {
199
0
        FmFormShell* pShell = dynamic_cast<FmFormShell*>( static_cast<const SfxObjectItem*>(pState)->GetShell() );
200
0
        UpdateContent(pShell);
201
0
    }
202
0
    else
203
0
        UpdateContent(nullptr);
204
0
}
205
206
void FmFieldWin::UpdateContent(FmFormShell const * pShell)
207
0
{
208
0
    m_xListBox->clear();
209
0
    m_aListBoxData.clear();
210
0
    OUString aTitle(SvxResId(RID_STR_FIELDSELECTION));
211
0
    m_xDialog->set_title(aTitle);
212
213
0
    if (!pShell || !pShell->GetImpl())
214
0
        return;
215
216
0
    Reference<XForm> const xForm = pShell->GetImpl()->getCurrentForm_Lock();
217
0
    if ( xForm.is() )
218
0
        UpdateContent( xForm );
219
0
}
220
221
void FmFieldWin::UpdateContent(const css::uno::Reference< css::form::XForm > & xForm)
222
0
{
223
0
    try
224
0
    {
225
        // delete ListBox
226
0
        m_xListBox->clear();
227
0
        m_aListBoxData.clear();
228
0
        OUString aTitle(SvxResId(RID_STR_FIELDSELECTION));
229
0
        m_xDialog->set_title(aTitle);
230
231
0
        if (!xForm.is())
232
0
            return;
233
234
0
        Reference< XPropertySet >  xSet(xForm, UNO_QUERY);
235
236
0
        m_aObjectName   = ::comphelper::getString(xSet->getPropertyValue(FM_PROP_COMMAND));
237
0
        m_aDatabaseName = ::comphelper::getString(xSet->getPropertyValue(FM_PROP_DATASOURCE));
238
0
        m_nObjectType   = ::comphelper::getINT32(xSet->getPropertyValue(FM_PROP_COMMANDTYPE));
239
240
        // get the connection of the form
241
0
        m_aConnection.reset(
242
0
            connectRowset( Reference< XRowSet >( xForm, UNO_QUERY ), ::comphelper::getProcessComponentContext(), nullptr ),
243
0
            SharedConnection::NoTakeOwnership
244
0
        );
245
        // TODO: When incompatible changes (such as extending the "virtualdbtools" interface by ensureRowSetConnection)
246
        // are allowed, again, we should change this: dbtools should consistently use SharedConnection all over
247
        // the place, and connectRowset should be replaced with ensureRowSetConnection
248
249
        // get the fields of the object
250
251
0
        if ( m_aConnection.is() && !m_aObjectName.isEmpty() )
252
0
        {
253
0
            Reference< XComponent > xKeepFieldsAlive;
254
0
            Reference< XNameAccess > xColumns = getFieldsByCommandDescriptor( m_aConnection, m_nObjectType, m_aObjectName,xKeepFieldsAlive );
255
0
            if ( xColumns.is() )
256
0
                addToList(xColumns);
257
0
        }
258
259
        // set prefix
260
0
        OUString  aPrefix;
261
262
0
        switch (m_nObjectType)
263
0
        {
264
0
            case CommandType::TABLE:
265
0
                aPrefix = SvxResId(RID_RSC_TABWIN_PREFIX[0]);
266
0
                break;
267
0
            case CommandType::QUERY:
268
0
                aPrefix = SvxResId(RID_RSC_TABWIN_PREFIX[1]);
269
0
                break;
270
0
            default:
271
0
                aPrefix = SvxResId(RID_RSC_TABWIN_PREFIX[2]);
272
0
                break;
273
0
        }
274
275
        // listen for changes at ControlSource in PropertySet
276
0
        std::unique_lock g(m_aMutex);
277
0
        if (m_xChangeListener.is())
278
0
        {
279
0
            m_xChangeListener->dispose(g);
280
0
            m_xChangeListener.clear();
281
0
        }
282
0
        m_xChangeListener = new ::comphelper::OPropertyChangeMultiplexer2(m_aMutex, g, this, xSet);
283
0
        m_xChangeListener->addProperty(FM_PROP_DATASOURCE);
284
0
        m_xChangeListener->addProperty(FM_PROP_COMMAND);
285
0
        m_xChangeListener->addProperty(FM_PROP_COMMANDTYPE);
286
287
        // set title
288
0
        aTitle += " " + aPrefix + " " + m_aObjectName;
289
0
        m_xDialog->set_title(aTitle);
290
0
    }
291
0
    catch( const Exception& )
292
0
    {
293
0
        TOOLS_WARN_EXCEPTION( "svx", "FmTabWin::UpdateContent" );
294
0
    }
295
0
}
296
297
void FmFieldWin::FillInfo( SfxChildWinInfo& rInfo ) const
298
0
{
299
0
    rInfo.bVisible = false;
300
0
}
301
302
SFX_IMPL_MODELESSDIALOGCONTOLLER(FmFieldWinMgr, SID_FM_ADD_FIELD)
303
304
FmFieldWinMgr::FmFieldWinMgr(vcl::Window* _pParent, sal_uInt16 _nId, SfxBindings& rBindings,
305
                             const SfxChildWinInfo& rInfo)
306
0
    : SfxChildWindow(_pParent, _nId)
307
0
{
308
0
    auto xDlg = std::make_shared<FmFieldWin>(&rBindings, this, _pParent->GetFrameWeld());
309
0
    SetController(xDlg);
310
0
    SetHideNotDelete(true);
311
0
    xDlg->Initialize(rInfo);
312
0
}
313
314
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */