Coverage Report

Created: 2026-06-30 11:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/sfx2/source/inet/inettbc.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 <inettbc.hxx>
22
23
#include <com/sun/star/awt/XTopWindow.hpp>
24
#include <com/sun/star/frame/Desktop.hpp>
25
#include <com/sun/star/task/XInteractionHandler.hpp>
26
#include <com/sun/star/util/XURLTransformer.hpp>
27
28
#include <comphelper/propertyvalue.hxx>
29
#include <svl/stritem.hxx>
30
#include <tools/urlobj.hxx>
31
#include <unotools/historyoptions.hxx>
32
#include <vcl/toolbox.hxx>
33
#include <vcl/svapp.hxx>
34
#include <osl/file.hxx>
35
#include <rtl/ustring.hxx>
36
37
#include <svtools/inettbc.hxx>
38
39
#include <vcl/InterimItemWindow.hxx>
40
#include <vcl/weld/Builder.hxx>
41
#include <sfx2/sfxsids.hrc>
42
43
using namespace ::com::sun::star::uno;
44
using namespace ::com::sun::star::beans;
45
using namespace ::com::sun::star::util;
46
using namespace ::com::sun::star::frame;
47
48
49
// SfxURLToolBoxControl_Impl
50
51
52
SFX_IMPL_TOOLBOX_CONTROL(SfxURLToolBoxControl_Impl,SfxStringItem)
53
54
SfxURLToolBoxControl_Impl::SfxURLToolBoxControl_Impl( sal_uInt16 nSlotId, ToolBoxItemId nId, ToolBox& rBox )
55
0
    : SfxToolBoxControl( nSlotId, nId, rBox )
56
0
    , m_bModified(false)
57
0
{
58
0
    addStatusListener( u".uno:CurrentURL"_ustr);
59
0
}
Unexecuted instantiation: SfxURLToolBoxControl_Impl::SfxURLToolBoxControl_Impl(unsigned short, o3tl::strong_int<unsigned short, ToolBoxItemIdTag>, ToolBox&)
Unexecuted instantiation: SfxURLToolBoxControl_Impl::SfxURLToolBoxControl_Impl(unsigned short, o3tl::strong_int<unsigned short, ToolBoxItemIdTag>, ToolBox&)
60
61
SfxURLToolBoxControl_Impl::~SfxURLToolBoxControl_Impl()
62
0
{
63
0
}
64
65
class URLBoxItemWindow final : public InterimItemWindow
66
{
67
private:
68
    std::unique_ptr<SvtURLBox> m_xWidget;
69
70
    DECL_LINK(KeyInputHdl, const KeyEvent&, bool);
71
public:
72
    URLBoxItemWindow(vcl::Window* pParent)
73
0
        : InterimItemWindow(pParent, u"sfx/ui/urlbox.ui"_ustr, u"URLBox"_ustr)
74
0
        , m_xWidget(new SvtURLBox(m_xBuilder->weld_combo_box(u"urlbox"_ustr)))
75
0
    {
76
0
        InitControlBase(m_xWidget->getWidget());
77
78
0
        m_xWidget->connect_key_press(LINK(this, URLBoxItemWindow, KeyInputHdl));
79
80
0
        int nWidth = GetDesktopRectPixel().GetWidth() > 800 ? 300 : 225;
81
0
        SetSizePixel(Size(nWidth, m_xWidget->get_preferred_size().Height()));
82
0
    }
83
84
    SvtURLBox* GetURLBox()
85
0
    {
86
0
        return m_xWidget.get();
87
0
    }
88
89
    virtual void dispose() override
90
0
    {
91
0
        m_xWidget.reset();
92
0
        InterimItemWindow::dispose();
93
0
    }
94
95
    void set_sensitive(bool bSensitive)
96
0
    {
97
0
        Enable(bSensitive);
98
0
        m_xWidget->set_sensitive(bSensitive);
99
0
    }
100
101
    virtual ~URLBoxItemWindow() override
102
0
    {
103
0
        disposeOnce();
104
0
    }
105
};
106
107
IMPL_LINK(URLBoxItemWindow, KeyInputHdl, const KeyEvent&, rKEvt, bool)
108
0
{
109
0
    return ChildKeyInput(rKEvt);
110
0
}
111
112
URLBoxItemWindow* SfxURLToolBoxControl_Impl::GetURLBoxItemWindow() const
113
0
{
114
0
    return static_cast<URLBoxItemWindow*>(GetToolBox().GetItemWindow(GetId()));
115
0
}
116
117
SvtURLBox* SfxURLToolBoxControl_Impl::GetURLBox() const
118
0
{
119
0
    return GetURLBoxItemWindow()->GetURLBox();
120
0
}
121
122
void SfxURLToolBoxControl_Impl::OpenURL( const OUString& rName ) const
123
0
{
124
0
    OUString aName;
125
126
0
    INetURLObject aObj( rName );
127
0
    if ( aObj.GetProtocol() == INetProtocol::NotValid )
128
0
    {
129
0
        aName = SvtURLBox::ParseSmart( rName, u""_ustr );
130
0
    }
131
0
    else
132
0
        aName = rName;
133
134
0
    if ( aName.isEmpty() )
135
0
        return;
136
137
0
    Reference< XDispatchProvider > xDispatchProvider( getFrameInterface(), UNO_QUERY );
138
0
    if ( !xDispatchProvider.is() )
139
0
        return;
140
141
0
    URL             aTargetURL;
142
0
    aTargetURL.Complete = aName;
143
144
0
    getURLTransformer()->parseStrict( aTargetURL );
145
0
    Reference< XDispatch > xDispatch = xDispatchProvider->queryDispatch( aTargetURL, u"_default"_ustr, 0 );
146
0
    if ( !xDispatch.is() )
147
0
        return;
148
149
0
    SfxURLToolBoxControl_Impl::ExecuteInfo* pExecuteInfo = new SfxURLToolBoxControl_Impl::ExecuteInfo;
150
0
    pExecuteInfo->xDispatch     = std::move(xDispatch);
151
0
    pExecuteInfo->aTargetURL    = std::move(aTargetURL);
152
0
    pExecuteInfo->aArgs         = {
153
0
        comphelper::makePropertyValue(u"Referer"_ustr, u"private:user"_ustr),
154
0
        comphelper::makePropertyValue(u"FileName"_ustr, aName)
155
0
    };
156
157
0
    Application::PostUserEvent( LINK( nullptr, SfxURLToolBoxControl_Impl, ExecuteHdl_Impl), pExecuteInfo );
158
0
}
159
160
161
IMPL_STATIC_LINK( SfxURLToolBoxControl_Impl, ExecuteHdl_Impl, void*, p, void )
162
0
{
163
0
    ExecuteInfo* pExecuteInfo = static_cast<ExecuteInfo*>(p);
164
0
    try
165
0
    {
166
        // Asynchronous execution as this can lead to our own destruction!
167
        // Framework can recycle our current frame and the layout manager disposes all user interface
168
        // elements if a component gets detached from its frame!
169
0
        pExecuteInfo->xDispatch->dispatch( pExecuteInfo->aTargetURL, pExecuteInfo->aArgs );
170
0
    }
171
0
    catch ( Exception& )
172
0
    {
173
0
    }
174
175
0
    delete pExecuteInfo;
176
0
}
177
178
VclPtr<InterimItemWindow> SfxURLToolBoxControl_Impl::CreateItemWindow( vcl::Window* pParent )
179
0
{
180
0
    VclPtrInstance<URLBoxItemWindow> xURLBox(pParent);
181
0
    SvtURLBox* pURLBox = xURLBox->GetURLBox();
182
0
    pURLBox->connect_changed(LINK(this, SfxURLToolBoxControl_Impl, SelectHdl));
183
0
    pURLBox->connect_entry_activate(LINK(this, SfxURLToolBoxControl_Impl, OpenHdl));
184
0
    xURLBox->Show();
185
0
    return xURLBox;
186
0
}
187
188
IMPL_LINK(SfxURLToolBoxControl_Impl, SelectHdl, weld::ComboBox&, rComboBox, void)
189
0
{
190
0
    m_bModified = true;
191
192
0
    SvtURLBox* pURLBox = GetURLBox();
193
0
    OUString aName( pURLBox->GetURL() );
194
195
0
    if (rComboBox.changed_by_direct_pick() && !aName.isEmpty())
196
0
        OpenURL( aName );
197
0
}
198
199
IMPL_LINK_NOARG(SfxURLToolBoxControl_Impl, OpenHdl, weld::ComboBox&, bool)
200
0
{
201
0
    SvtURLBox* pURLBox = GetURLBox();
202
0
    OpenURL( pURLBox->GetURL() );
203
204
0
    Reference< XDesktop2 > xDesktop = Desktop::create( m_xContext );
205
0
    Reference< XFrame > xFrame = xDesktop->getActiveFrame();
206
0
    if (!xFrame.is())
207
0
        return true;
208
209
0
    auto xWin = xFrame->getContainerWindow();
210
0
    if (!xWin)
211
0
        return true;
212
0
    xWin->setFocus();
213
0
    Reference<css::awt::XTopWindow> xTop(xWin, UNO_QUERY);
214
0
    if (!xTop)
215
0
        return true;
216
0
    xTop->toFront();
217
0
    return true;
218
0
}
219
220
void SfxURLToolBoxControl_Impl::StateChangedAtToolBoxControl
221
(
222
    sal_uInt16          nSID,
223
    SfxItemState        eState,
224
    const SfxPoolItem*  pState
225
)
226
0
{
227
0
    if ( nSID == SID_OPENURL )
228
0
    {
229
        // Disable URL box if command is disabled
230
0
        GetURLBoxItemWindow()->set_sensitive( SfxItemState::DISABLED != eState );
231
0
    }
232
233
0
    if ( !GetURLBoxItemWindow()->IsEnabled() )
234
0
        return;
235
236
0
    if( nSID == SID_FOCUSURLBOX )
237
0
    {
238
0
        if ( GetURLBoxItemWindow()->IsVisible() )
239
0
            GetURLBoxItemWindow()->GrabFocus();
240
0
    }
241
0
    else if ( !m_bModified && SfxItemState::DEFAULT == eState )
242
0
    {
243
0
        SvtURLBox* pURLBox = GetURLBox();
244
0
        pURLBox->clear();
245
246
0
        const std::vector< SvtHistoryOptions::HistoryItem > lList = SvtHistoryOptions::GetList(EHistoryType::PickList);
247
0
        for (const SvtHistoryOptions::HistoryItem& lProps : lList)
248
0
        {
249
0
            if (!lProps.sURL.isEmpty())
250
0
            {
251
0
                INetURLObject aURL    ( lProps.sURL );
252
0
                OUString      sMainURL( aURL.GetMainURL( INetURLObject::DecodeMechanism::WithCharset ) );
253
0
                OUString      sFile;
254
255
0
                if (osl::FileBase::getSystemPathFromFileURL(sMainURL, sFile) == osl::FileBase::E_None)
256
0
                    pURLBox->append_text(sFile);
257
0
                else
258
0
                    pURLBox->append_text(sMainURL);
259
0
            }
260
0
        }
261
262
0
        const SfxStringItem *pURL = dynamic_cast< const SfxStringItem* >(pState);
263
0
        assert(pURL);
264
0
        INetURLObject aURL( pURL->GetValue() );
265
0
        INetProtocol eProt = aURL.GetProtocol();
266
0
        if ( eProt == INetProtocol::File )
267
0
        {
268
0
            pURLBox->set_entry_text( aURL.PathToFileName() );
269
0
        }
270
0
        else
271
0
            pURLBox->set_entry_text( aURL.GetURLNoPass() );
272
0
    }
273
0
}
274
275
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */