Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sfx2 / source / inet / inettbc.cxx
blob165dd2eeb5962d404b71d59fb077071d3c494dcb
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/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>
28 #include <comphelper/propertyvalue.hxx>
29 #include <svl/stritem.hxx>
30 #include <unotools/historyoptions.hxx>
31 #include <vcl/toolbox.hxx>
32 #include <vcl/svapp.hxx>
33 #include <osl/file.hxx>
34 #include <rtl/ustring.hxx>
36 #include <svtools/inettbc.hxx>
38 #include <vcl/InterimItemWindow.hxx>
39 #include <sfx2/sfxsids.hrc>
41 using namespace ::com::sun::star::uno;
42 using namespace ::com::sun::star::beans;
43 using namespace ::com::sun::star::util;
44 using namespace ::com::sun::star::frame;
45 using namespace ::com::sun::star::task;
48 // SfxURLToolBoxControl_Impl
51 SFX_IMPL_TOOLBOX_CONTROL(SfxURLToolBoxControl_Impl,SfxStringItem)
53 SfxURLToolBoxControl_Impl::SfxURLToolBoxControl_Impl( sal_uInt16 nSlotId, ToolBoxItemId nId, ToolBox& rBox )
54 : SfxToolBoxControl( nSlotId, nId, rBox )
55 , m_bModified(false)
57 addStatusListener( ".uno:CurrentURL");
60 SfxURLToolBoxControl_Impl::~SfxURLToolBoxControl_Impl()
64 class URLBoxItemWindow final : public InterimItemWindow
66 private:
67 std::unique_ptr<SvtURLBox> m_xWidget;
69 DECL_LINK(KeyInputHdl, const KeyEvent&, bool);
70 public:
71 URLBoxItemWindow(vcl::Window* pParent)
72 : InterimItemWindow(pParent, "sfx/ui/urlbox.ui", "URLBox")
73 , m_xWidget(new SvtURLBox(m_xBuilder->weld_combo_box("urlbox")))
75 InitControlBase(m_xWidget->getWidget());
77 m_xWidget->connect_key_press(LINK(this, URLBoxItemWindow, KeyInputHdl));
79 int nWidth = GetDesktopRectPixel().GetWidth() > 800 ? 300 : 225;
80 SetSizePixel(Size(nWidth, m_xWidget->get_preferred_size().Height()));
83 SvtURLBox* GetURLBox()
85 return m_xWidget.get();
88 virtual void dispose() override
90 m_xWidget.reset();
91 InterimItemWindow::dispose();
94 void set_sensitive(bool bSensitive)
96 Enable(bSensitive);
97 m_xWidget->set_sensitive(bSensitive);
100 virtual ~URLBoxItemWindow() override
102 disposeOnce();
106 IMPL_LINK(URLBoxItemWindow, KeyInputHdl, const KeyEvent&, rKEvt, bool)
108 return ChildKeyInput(rKEvt);
111 URLBoxItemWindow* SfxURLToolBoxControl_Impl::GetURLBoxItemWindow() const
113 return static_cast<URLBoxItemWindow*>(GetToolBox().GetItemWindow(GetId()));
116 SvtURLBox* SfxURLToolBoxControl_Impl::GetURLBox() const
118 return GetURLBoxItemWindow()->GetURLBox();
121 void SfxURLToolBoxControl_Impl::OpenURL( const OUString& rName ) const
123 OUString aName;
125 INetURLObject aObj( rName );
126 if ( aObj.GetProtocol() == INetProtocol::NotValid )
128 aName = SvtURLBox::ParseSmart( rName, "" );
130 else
131 aName = rName;
133 if ( aName.isEmpty() )
134 return;
136 Reference< XDispatchProvider > xDispatchProvider( getFrameInterface(), UNO_QUERY );
137 if ( !xDispatchProvider.is() )
138 return;
140 URL aTargetURL;
141 aTargetURL.Complete = aName;
143 getURLTransformer()->parseStrict( aTargetURL );
144 Reference< XDispatch > xDispatch = xDispatchProvider->queryDispatch( aTargetURL, "_default", 0 );
145 if ( !xDispatch.is() )
146 return;
148 Sequence< PropertyValue > aArgs{
149 comphelper::makePropertyValue("Referer", OUString( "private:user" )),
150 comphelper::makePropertyValue("FileName", 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 );
171 catch ( Exception& )
175 delete pExecuteInfo;
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));
184 xURLBox->Show();
185 return xURLBox;
188 IMPL_LINK(SfxURLToolBoxControl_Impl, SelectHdl, weld::ComboBox&, rComboBox, void)
190 m_bModified = true;
192 SvtURLBox* pURLBox = GetURLBox();
193 OUString aName( pURLBox->GetURL() );
195 if (rComboBox.changed_by_direct_pick() && !aName.isEmpty())
196 OpenURL( aName );
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();
206 if (!xFrame.is())
207 return true;
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;
220 void SfxURLToolBoxControl_Impl::StateChangedAtToolBoxControl
222 sal_uInt16 nSID,
223 SfxItemState eState,
224 const SfxPoolItem* pState
227 if ( nSID == SID_OPENURL )
229 // Disable URL box if command is disabled
230 GetURLBoxItemWindow()->set_sensitive( SfxItemState::DISABLED != eState );
233 if ( !GetURLBoxItemWindow()->IsEnabled() )
234 return;
236 if( nSID == SID_FOCUSURLBOX )
238 if ( GetURLBoxItemWindow()->IsVisible() )
239 GetURLBoxItemWindow()->GrabFocus();
241 else if ( !m_bModified && SfxItemState::DEFAULT == eState )
243 SvtURLBox* pURLBox = GetURLBox();
244 pURLBox->clear();
246 const std::vector< SvtHistoryOptions::HistoryItem > lList = SvtHistoryOptions::GetList(EHistoryType::PickList);
247 for (const SvtHistoryOptions::HistoryItem& lProps : lList)
249 if (!lProps.sURL.isEmpty())
251 INetURLObject aURL ( lProps.sURL );
252 OUString sMainURL( aURL.GetMainURL( INetURLObject::DecodeMechanism::WithCharset ) );
253 OUString sFile;
255 if (osl::FileBase::getSystemPathFromFileURL(sMainURL, sFile) == osl::FileBase::E_None)
256 pURLBox->append_text(sFile);
257 else
258 pURLBox->append_text(sMainURL);
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 )
268 pURLBox->set_entry_text( aURL.PathToFileName() );
270 else
271 pURLBox->set_entry_text( aURL.GetURLNoPass() );
275 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */