Coverage Report

Created: 2025-12-08 09:28

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