LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / sfx2 / source / inet / inettbc.cxx
blob0966d1c5a6ec1c91890774179fdd64eb0375555c
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/uno/Any.h>
24 #include <com/sun/star/awt/XTopWindow.hpp>
25 #include <com/sun/star/frame/Desktop.hpp>
26 #include <com/sun/star/task/XInteractionHandler.hpp>
27 #include <com/sun/star/util/XURLTransformer.hpp>
29 #include <comphelper/propertyvalue.hxx>
30 #include <svl/stritem.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>
37 #include <svtools/inettbc.hxx>
39 #include <vcl/InterimItemWindow.hxx>
40 #include <sfx2/sfxsids.hrc>
42 using namespace ::com::sun::star::uno;
43 using namespace ::com::sun::star::beans;
44 using namespace ::com::sun::star::util;
45 using namespace ::com::sun::star::frame;
46 using namespace ::com::sun::star::task;
49 // SfxURLToolBoxControl_Impl
52 SFX_IMPL_TOOLBOX_CONTROL(SfxURLToolBoxControl_Impl,SfxStringItem)
54 SfxURLToolBoxControl_Impl::SfxURLToolBoxControl_Impl( sal_uInt16 nSlotId, ToolBoxItemId nId, ToolBox& rBox )
55 : SfxToolBoxControl( nSlotId, nId, rBox )
56 , m_bModified(false)
58 addStatusListener( ".uno:CurrentURL");
61 SfxURLToolBoxControl_Impl::~SfxURLToolBoxControl_Impl()
65 class URLBoxItemWindow final : public InterimItemWindow
67 private:
68 std::unique_ptr<SvtURLBox> m_xWidget;
70 DECL_LINK(KeyInputHdl, const KeyEvent&, bool);
71 public:
72 URLBoxItemWindow(vcl::Window* pParent)
73 : InterimItemWindow(pParent, "sfx/ui/urlbox.ui", "URLBox")
74 , m_xWidget(new SvtURLBox(m_xBuilder->weld_combo_box("urlbox")))
76 InitControlBase(m_xWidget->getWidget());
78 m_xWidget->connect_key_press(LINK(this, URLBoxItemWindow, KeyInputHdl));
80 int nWidth = GetDesktopRectPixel().GetWidth() > 800 ? 300 : 225;
81 SetSizePixel(Size(nWidth, m_xWidget->get_preferred_size().Height()));
84 SvtURLBox* GetURLBox()
86 return m_xWidget.get();
89 virtual void dispose() override
91 m_xWidget.reset();
92 InterimItemWindow::dispose();
95 void set_sensitive(bool bSensitive)
97 Enable(bSensitive);
98 m_xWidget->set_sensitive(bSensitive);
101 virtual ~URLBoxItemWindow() override
103 disposeOnce();
107 IMPL_LINK(URLBoxItemWindow, KeyInputHdl, const KeyEvent&, rKEvt, bool)
109 return ChildKeyInput(rKEvt);
112 URLBoxItemWindow* SfxURLToolBoxControl_Impl::GetURLBoxItemWindow() const
114 return static_cast<URLBoxItemWindow*>(GetToolBox().GetItemWindow(GetId()));
117 SvtURLBox* SfxURLToolBoxControl_Impl::GetURLBox() const
119 return GetURLBoxItemWindow()->GetURLBox();
122 void SfxURLToolBoxControl_Impl::OpenURL( const OUString& rName ) const
124 OUString aName;
126 INetURLObject aObj( rName );
127 if ( aObj.GetProtocol() == INetProtocol::NotValid )
129 aName = SvtURLBox::ParseSmart( rName, "" );
131 else
132 aName = rName;
134 if ( aName.isEmpty() )
135 return;
137 Reference< XDispatchProvider > xDispatchProvider( getFrameInterface(), UNO_QUERY );
138 if ( !xDispatchProvider.is() )
139 return;
141 URL aTargetURL;
142 aTargetURL.Complete = aName;
144 getURLTransformer()->parseStrict( aTargetURL );
145 Reference< XDispatch > xDispatch = xDispatchProvider->queryDispatch( aTargetURL, "_default", 0 );
146 if ( !xDispatch.is() )
147 return;
149 Sequence< PropertyValue > aArgs{
150 comphelper::makePropertyValue("Referer", OUString( "private:user" )),
151 comphelper::makePropertyValue("FileName", aName)
154 SfxURLToolBoxControl_Impl::ExecuteInfo* pExecuteInfo = new SfxURLToolBoxControl_Impl::ExecuteInfo;
155 pExecuteInfo->xDispatch = xDispatch;
156 pExecuteInfo->aTargetURL = aTargetURL;
157 pExecuteInfo->aArgs = aArgs;
158 Application::PostUserEvent( LINK( nullptr, SfxURLToolBoxControl_Impl, ExecuteHdl_Impl), pExecuteInfo );
162 IMPL_STATIC_LINK( SfxURLToolBoxControl_Impl, ExecuteHdl_Impl, void*, p, void )
164 ExecuteInfo* pExecuteInfo = static_cast<ExecuteInfo*>(p);
167 // Asynchronous execution as this can lead to our own destruction!
168 // Framework can recycle our current frame and the layout manager disposes all user interface
169 // elements if a component gets detached from its frame!
170 pExecuteInfo->xDispatch->dispatch( pExecuteInfo->aTargetURL, pExecuteInfo->aArgs );
172 catch ( Exception& )
176 delete pExecuteInfo;
179 VclPtr<InterimItemWindow> SfxURLToolBoxControl_Impl::CreateItemWindow( vcl::Window* pParent )
181 VclPtrInstance<URLBoxItemWindow> xURLBox(pParent);
182 SvtURLBox* pURLBox = xURLBox->GetURLBox();
183 pURLBox->connect_changed(LINK(this, SfxURLToolBoxControl_Impl, SelectHdl));
184 pURLBox->connect_entry_activate(LINK(this, SfxURLToolBoxControl_Impl, OpenHdl));
185 xURLBox->Show();
186 return xURLBox;
189 IMPL_LINK(SfxURLToolBoxControl_Impl, SelectHdl, weld::ComboBox&, rComboBox, void)
191 m_bModified = true;
193 SvtURLBox* pURLBox = GetURLBox();
194 OUString aName( pURLBox->GetURL() );
196 if (rComboBox.changed_by_direct_pick() && !aName.isEmpty())
197 OpenURL( aName );
200 IMPL_LINK_NOARG(SfxURLToolBoxControl_Impl, OpenHdl, weld::ComboBox&, bool)
202 SvtURLBox* pURLBox = GetURLBox();
203 OpenURL( pURLBox->GetURL() );
205 Reference< XDesktop2 > xDesktop = Desktop::create( m_xContext );
206 Reference< XFrame > xFrame = xDesktop->getActiveFrame();
207 if (!xFrame.is())
208 return true;
210 auto xWin = xFrame->getContainerWindow();
211 if (!xWin)
212 return true;
213 xWin->setFocus();
214 Reference<css::awt::XTopWindow> xTop(xWin, UNO_QUERY);
215 if (!xTop)
216 return true;
217 xTop->toFront();
218 return true;
221 void SfxURLToolBoxControl_Impl::StateChangedAtToolBoxControl
223 sal_uInt16 nSID,
224 SfxItemState eState,
225 const SfxPoolItem* pState
228 if ( nSID == SID_OPENURL )
230 // Disable URL box if command is disabled
231 GetURLBoxItemWindow()->set_sensitive( SfxItemState::DISABLED != eState );
234 if ( !GetURLBoxItemWindow()->IsEnabled() )
235 return;
237 if( nSID == SID_FOCUSURLBOX )
239 if ( GetURLBoxItemWindow()->IsVisible() )
240 GetURLBoxItemWindow()->GrabFocus();
242 else if ( !m_bModified && SfxItemState::DEFAULT == eState )
244 SvtURLBox* pURLBox = GetURLBox();
245 pURLBox->clear();
247 const std::vector< SvtHistoryOptions::HistoryItem > lList = SvtHistoryOptions::GetList(EHistoryType::PickList);
248 for (const SvtHistoryOptions::HistoryItem& lProps : lList)
250 if (!lProps.sURL.isEmpty())
252 INetURLObject aURL ( lProps.sURL );
253 OUString sMainURL( aURL.GetMainURL( INetURLObject::DecodeMechanism::WithCharset ) );
254 OUString sFile;
256 if (osl::FileBase::getSystemPathFromFileURL(sMainURL, sFile) == osl::FileBase::E_None)
257 pURLBox->append_text(sFile);
258 else
259 pURLBox->append_text(sMainURL);
263 const SfxStringItem *pURL = dynamic_cast< const SfxStringItem* >(pState);
264 assert(pURL);
265 INetURLObject aURL( pURL->GetValue() );
266 INetProtocol eProt = aURL.GetProtocol();
267 if ( eProt == INetProtocol::File )
269 pURLBox->set_entry_text( aURL.PathToFileName() );
271 else
272 pURLBox->set_entry_text( aURL.GetURLNoPass() );
276 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */