bump product version to 5.0.4.1
[LibreOffice.git] / sfx2 / source / inet / inettbc.cxx
blob04564cdd8b0f661ec6c1fd880077137a16122af4
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/frame/Desktop.hpp>
25 #include <com/sun/star/frame/XFramesSupplier.hpp>
26 #include <com/sun/star/task/XInteractionHandler.hpp>
27 #include <svl/eitem.hxx>
28 #include <svl/stritem.hxx>
29 #include <unotools/historyoptions.hxx>
30 #include <vcl/toolbox.hxx>
31 #include <toolkit/helper/vclunohelper.hxx>
32 #include <osl/thread.hxx>
33 #include <osl/mutex.hxx>
34 #include <rtl/ustring.hxx>
36 #include <svl/itemset.hxx>
37 #include <svl/urihelper.hxx>
38 #include <unotools/pathoptions.hxx>
39 #include <svtools/asynclink.hxx>
40 #include <svtools/inettbc.hxx>
42 #include <unotools/localfilehelper.hxx>
43 #include <comphelper/processfactory.hxx>
45 #include <sfx2/sfx.hrc>
46 #include <sfx2/dispatch.hxx>
47 #include <sfx2/viewfrm.hxx>
48 #include <sfx2/objsh.hxx>
49 #include "sfxtypes.hxx"
50 #include "helper.hxx"
52 using namespace ::com::sun::star::uno;
53 using namespace ::com::sun::star::beans;
54 using namespace ::com::sun::star::util;
55 using namespace ::com::sun::star::frame;
56 using namespace ::com::sun::star::task;
59 // SfxURLToolBoxControl_Impl
62 SFX_IMPL_TOOLBOX_CONTROL(SfxURLToolBoxControl_Impl,SfxStringItem)
64 SfxURLToolBoxControl_Impl::SfxURLToolBoxControl_Impl( sal_uInt16 nSlotId, sal_uInt16 nId, ToolBox& rBox )
65 : SfxToolBoxControl( nSlotId, nId, rBox )
67 addStatusListener( OUString( ".uno:CurrentURL" ));
70 SfxURLToolBoxControl_Impl::~SfxURLToolBoxControl_Impl()
74 SvtURLBox* SfxURLToolBoxControl_Impl::GetURLBox() const
76 return static_cast<SvtURLBox*>(GetToolBox().GetItemWindow( GetId() ));
81 void SfxURLToolBoxControl_Impl::OpenURL( const OUString& rName, bool /*bNew*/ ) const
83 OUString aName;
84 OUString aFilter;
85 OUString aOptions;
87 INetURLObject aObj( rName );
88 if ( aObj.GetProtocol() == INetProtocol::NotValid )
90 OUString aBaseURL = GetURLBox()->GetBaseURL();
91 aName = SvtURLBox::ParseSmart( rName, aBaseURL, SvtPathOptions().GetWorkPath() );
93 else
94 aName = rName;
96 if ( aName.isEmpty() )
97 return;
99 Reference< XDispatchProvider > xDispatchProvider( getFrameInterface(), UNO_QUERY );
100 if ( xDispatchProvider.is() )
102 URL aTargetURL;
103 OUString aTarget( "_default" );
105 aTargetURL.Complete = aName;
107 getURLTransformer()->parseStrict( aTargetURL );
108 Reference< XDispatch > xDispatch = xDispatchProvider->queryDispatch( aTargetURL, aTarget, 0 );
109 if ( xDispatch.is() )
111 Sequence< PropertyValue > aArgs( 2 );
112 aArgs[0].Name = "Referer";
113 aArgs[0].Value = makeAny( OUString( "private:user" ));
114 aArgs[1].Name = "FileName";
115 aArgs[1].Value = makeAny( OUString( aName ));
117 if ( !aFilter.isEmpty() )
119 aArgs.realloc( 4 );
120 aArgs[2].Name = "FilterOptions";
121 aArgs[2].Value = makeAny( OUString( aOptions ));
122 aArgs[3].Name = "FilterName";
123 aArgs[3].Value = makeAny( OUString( aFilter ));
126 SfxURLToolBoxControl_Impl::ExecuteInfo* pExecuteInfo = new SfxURLToolBoxControl_Impl::ExecuteInfo;
127 pExecuteInfo->xDispatch = xDispatch;
128 pExecuteInfo->aTargetURL = aTargetURL;
129 pExecuteInfo->aArgs = aArgs;
130 Application::PostUserEvent( LINK( 0, SfxURLToolBoxControl_Impl, ExecuteHdl_Impl), pExecuteInfo );
137 IMPL_STATIC_LINK( SfxURLToolBoxControl_Impl, ExecuteHdl_Impl, ExecuteInfo*, pExecuteInfo )
141 // Asynchronous execution as this can lead to our own destruction!
142 // Framework can recycle our current frame and the layout manager disposes all user interface
143 // elements if a component gets detached from its frame!
144 pExecuteInfo->xDispatch->dispatch( pExecuteInfo->aTargetURL, pExecuteInfo->aArgs );
146 catch ( Exception& )
150 delete pExecuteInfo;
151 return 0;
155 VclPtr<vcl::Window> SfxURLToolBoxControl_Impl::CreateItemWindow( vcl::Window* pParent )
157 VclPtrInstance<SvtURLBox> pURLBox( pParent );
158 pURLBox->SetOpenHdl( LINK( this, SfxURLToolBoxControl_Impl, OpenHdl ) );
159 pURLBox->SetSelectHdl( LINK( this, SfxURLToolBoxControl_Impl, SelectHdl ) );
160 return pURLBox.get();
163 IMPL_LINK_NOARG(SfxURLToolBoxControl_Impl, SelectHdl)
165 SvtURLBox* pURLBox = GetURLBox();
166 OUString aName( pURLBox->GetURL() );
168 if ( !pURLBox->IsTravelSelect() && !aName.isEmpty() )
169 OpenURL( aName, false );
171 return 1L;
174 IMPL_LINK_NOARG(SfxURLToolBoxControl_Impl, OpenHdl)
176 SvtURLBox* pURLBox = GetURLBox();
177 OpenURL( pURLBox->GetURL(), pURLBox->IsCtrlOpen() );
179 Reference< XDesktop2 > xDesktop = Desktop::create( m_xContext );
180 Reference< XFrame > xFrame( xDesktop->getActiveFrame(), UNO_QUERY );
181 if ( xFrame.is() )
183 vcl::Window* pWin = VCLUnoHelper::GetWindow( xFrame->getContainerWindow() );
184 if ( pWin )
186 pWin->GrabFocus();
187 pWin->ToTop( TOTOP_RESTOREWHENMIN );
191 return 1L;
196 void SfxURLToolBoxControl_Impl::StateChanged
198 sal_uInt16 nSID,
199 SfxItemState eState,
200 const SfxPoolItem* pState
203 if ( nSID == SID_OPENURL )
205 // Disable URL box if command is disabled
206 GetURLBox()->Enable( SfxItemState::DISABLED != eState );
209 if ( GetURLBox()->IsEnabled() )
211 if( nSID == SID_FOCUSURLBOX )
213 if ( GetURLBox()->IsVisible() )
214 GetURLBox()->GrabFocus();
216 else if ( !GetURLBox()->IsModified() && SfxItemState::DEFAULT == eState )
218 SvtURLBox* pURLBox = GetURLBox();
219 pURLBox->Clear();
221 ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue > > lList = SvtHistoryOptions().GetList(ePICKLIST);
222 for (sal_Int32 i=0; i<lList.getLength(); ++i)
224 ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue > lProps = lList[i];
225 for (sal_Int32 p=0; p<lProps.getLength(); ++p)
227 if (lProps[p].Name != HISTORY_PROPERTYNAME_URL)
228 continue;
230 OUString sURL;
231 if (!(lProps[p].Value>>=sURL) || sURL.isEmpty())
232 continue;
234 INetURLObject aURL ( sURL );
235 OUString sMainURL( aURL.GetMainURL( INetURLObject::DECODE_WITH_CHARSET ) );
236 OUString sFile;
238 if (::utl::LocalFileHelper::ConvertURLToSystemPath(sMainURL,sFile))
239 pURLBox->InsertEntry(sFile);
240 else
241 pURLBox->InsertEntry(sMainURL);
245 const SfxStringItem *pURL = PTR_CAST(SfxStringItem,pState);
246 OUString aRep( pURL->GetValue() );
247 INetURLObject aURL( aRep );
248 INetProtocol eProt = aURL.GetProtocol();
249 if ( eProt == INetProtocol::File )
251 pURLBox->SetText( aURL.PathToFileName() );
253 else
254 pURLBox->SetText( aURL.GetURLNoPass() );
259 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */