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