1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <inettbc.hxx>
23 #include <com/sun/star/uno/Any.h>
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 #include <svl/stritem.hxx>
28 #include <unotools/historyoptions.hxx>
29 #include <vcl/toolbox.hxx>
30 #include <vcl/svapp.hxx>
31 #include <toolkit/helper/vclunohelper.hxx>
32 #include <osl/file.hxx>
33 #include <rtl/ustring.hxx>
35 #include <svtools/inettbc.hxx>
37 #include <vcl/InterimItemWindow.hxx>
38 #include <sfx2/sfxsids.hrc>
40 using namespace ::com::sun::star::uno
;
41 using namespace ::com::sun::star::beans
;
42 using namespace ::com::sun::star::util
;
43 using namespace ::com::sun::star::frame
;
44 using namespace ::com::sun::star::task
;
47 // SfxURLToolBoxControl_Impl
50 SFX_IMPL_TOOLBOX_CONTROL(SfxURLToolBoxControl_Impl
,SfxStringItem
)
52 SfxURLToolBoxControl_Impl::SfxURLToolBoxControl_Impl( sal_uInt16 nSlotId
, sal_uInt16 nId
, ToolBox
& rBox
)
53 : SfxToolBoxControl( nSlotId
, nId
, rBox
)
56 addStatusListener( ".uno:CurrentURL");
59 SfxURLToolBoxControl_Impl::~SfxURLToolBoxControl_Impl()
63 class URLBoxItemWindow final
: public InterimItemWindow
66 std::unique_ptr
<SvtURLBox
> m_xWidget
;
68 DECL_LINK(KeyInputHdl
, const KeyEvent
&, bool);
70 URLBoxItemWindow(vcl::Window
* pParent
)
71 : InterimItemWindow(pParent
, "sfx/ui/urlbox.ui", "URLBox")
72 , m_xWidget(new SvtURLBox(m_xBuilder
->weld_combo_box("urlbox")))
74 InitControlBase(m_xWidget
->getWidget());
76 m_xWidget
->connect_key_press(LINK(this, URLBoxItemWindow
, KeyInputHdl
));
78 int nWidth
= GetDesktopRectPixel().GetWidth() > 800 ? 300 : 225;
79 SetSizePixel(Size(nWidth
, m_xWidget
->get_preferred_size().Height()));
82 SvtURLBox
* GetURLBox()
84 return m_xWidget
.get();
87 virtual void dispose() override
90 InterimItemWindow::dispose();
93 void set_sensitive(bool bSensitive
)
96 m_xWidget
->set_sensitive(bSensitive
);
99 virtual ~URLBoxItemWindow() override
105 IMPL_LINK(URLBoxItemWindow
, KeyInputHdl
, const KeyEvent
&, rKEvt
, bool)
107 return ChildKeyInput(rKEvt
);
110 URLBoxItemWindow
* SfxURLToolBoxControl_Impl::GetURLBoxItemWindow() const
112 return static_cast<URLBoxItemWindow
*>(GetToolBox().GetItemWindow(GetId()));
115 SvtURLBox
* SfxURLToolBoxControl_Impl::GetURLBox() const
117 return GetURLBoxItemWindow()->GetURLBox();
120 void SfxURLToolBoxControl_Impl::OpenURL( const OUString
& rName
) const
124 INetURLObject
aObj( rName
);
125 if ( aObj
.GetProtocol() == INetProtocol::NotValid
)
127 aName
= SvtURLBox::ParseSmart( rName
, "" );
132 if ( aName
.isEmpty() )
135 Reference
< XDispatchProvider
> xDispatchProvider( getFrameInterface(), UNO_QUERY
);
136 if ( !xDispatchProvider
.is() )
140 aTargetURL
.Complete
= aName
;
142 getURLTransformer()->parseStrict( aTargetURL
);
143 Reference
< XDispatch
> xDispatch
= xDispatchProvider
->queryDispatch( aTargetURL
, "_default", 0 );
144 if ( !xDispatch
.is() )
147 Sequence
< PropertyValue
> aArgs( 2 );
148 aArgs
[0].Name
= "Referer";
149 aArgs
[0].Value
<<= OUString( "private:user" );
150 aArgs
[1].Name
= "FileName";
151 aArgs
[1].Value
<<= aName
;
153 SfxURLToolBoxControl_Impl::ExecuteInfo
* pExecuteInfo
= new SfxURLToolBoxControl_Impl::ExecuteInfo
;
154 pExecuteInfo
->xDispatch
= xDispatch
;
155 pExecuteInfo
->aTargetURL
= aTargetURL
;
156 pExecuteInfo
->aArgs
= aArgs
;
157 Application::PostUserEvent( LINK( nullptr, SfxURLToolBoxControl_Impl
, ExecuteHdl_Impl
), pExecuteInfo
);
161 IMPL_STATIC_LINK( SfxURLToolBoxControl_Impl
, ExecuteHdl_Impl
, void*, p
, void )
163 ExecuteInfo
* pExecuteInfo
= static_cast<ExecuteInfo
*>(p
);
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
);
178 VclPtr
<InterimItemWindow
> SfxURLToolBoxControl_Impl::CreateItemWindow( vcl::Window
* pParent
)
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
));
188 IMPL_LINK(SfxURLToolBoxControl_Impl
, SelectHdl
, weld::ComboBox
&, rComboBox
, void)
192 SvtURLBox
* pURLBox
= GetURLBox();
193 OUString
aName( pURLBox
->GetURL() );
195 if (rComboBox
.changed_by_direct_pick() && !aName
.isEmpty())
199 IMPL_LINK_NOARG(SfxURLToolBoxControl_Impl
, OpenHdl
, weld::ComboBox
&, bool)
201 SvtURLBox
* pURLBox
= GetURLBox();
202 OpenURL( pURLBox
->GetURL() );
204 Reference
< XDesktop2
> xDesktop
= Desktop::create( m_xContext
);
205 Reference
< XFrame
> xFrame
= xDesktop
->getActiveFrame();
208 VclPtr
<vcl::Window
> pWin
= VCLUnoHelper::GetWindow( xFrame
->getContainerWindow() );
212 pWin
->ToTop( ToTopFlags::RestoreWhenMin
);
219 void SfxURLToolBoxControl_Impl::StateChanged
223 const SfxPoolItem
* pState
226 if ( nSID
== SID_OPENURL
)
228 // Disable URL box if command is disabled
229 GetURLBoxItemWindow()->set_sensitive( SfxItemState::DISABLED
!= eState
);
232 if ( !GetURLBoxItemWindow()->IsEnabled() )
235 if( nSID
== SID_FOCUSURLBOX
)
237 if ( GetURLBoxItemWindow()->IsVisible() )
238 GetURLBoxItemWindow()->GrabFocus();
240 else if ( !m_bModified
&& SfxItemState::DEFAULT
== eState
)
242 SvtURLBox
* pURLBox
= GetURLBox();
245 const css::uno::Sequence
< css::uno::Sequence
< css::beans::PropertyValue
> > lList
= SvtHistoryOptions().GetList(ePICKLIST
);
246 for (const css::uno::Sequence
< css::beans::PropertyValue
>& lProps
: lList
)
248 for (const auto& rProp
: lProps
)
250 if (rProp
.Name
!= HISTORY_PROPERTYNAME_URL
)
254 if (!(rProp
.Value
>>=sURL
) || sURL
.isEmpty())
257 INetURLObject
aURL ( sURL
);
258 OUString
sMainURL( aURL
.GetMainURL( INetURLObject::DecodeMechanism::WithCharset
) );
261 if (osl::FileBase::getSystemPathFromFileURL(sMainURL
, sFile
) == osl::FileBase::E_None
)
262 pURLBox
->append_text(sFile
);
264 pURLBox
->append_text(sMainURL
);
268 const SfxStringItem
*pURL
= dynamic_cast< const SfxStringItem
* >(pState
);
270 INetURLObject
aURL( pURL
->GetValue() );
271 INetProtocol eProt
= aURL
.GetProtocol();
272 if ( eProt
== INetProtocol::File
)
274 pURLBox
->set_entry_text( aURL
.PathToFileName() );
277 pURLBox
->set_entry_text( aURL
.GetURLNoPass() );
281 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */