Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / sfx2 / source / inet / inettbc.cxx
blobe1671d51ff8e93ff2808ada2a4b22dc108ba5a01
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include "inettbc.hxx"
32 #include <com/sun/star/uno/Any.h>
33 #include <com/sun/star/frame/XFramesSupplier.hpp>
34 #include <com/sun/star/task/XInteractionHandler.hpp>
35 #include <svl/eitem.hxx>
36 #include <svl/stritem.hxx>
37 #include <unotools/historyoptions.hxx>
38 #include <svl/folderrestriction.hxx>
39 #include <vcl/toolbox.hxx>
40 #include <toolkit/unohlp.hxx>
41 #include <osl/thread.hxx>
42 #include <osl/mutex.hxx>
43 #include <rtl/ustring.hxx>
45 #include <svl/itemset.hxx>
46 #include <svl/urihelper.hxx>
47 #include <unotools/pathoptions.hxx>
48 #include <svtools/asynclink.hxx>
49 #include <svtools/inettbc.hxx>
51 #include <unotools/localfilehelper.hxx>
52 #include <comphelper/processfactory.hxx>
54 #include <sfx2/sfx.hrc>
55 #include <sfx2/dispatch.hxx>
56 #include <sfx2/viewfrm.hxx>
57 #include <sfx2/objsh.hxx>
58 #include "referers.hxx"
59 #include "sfxtypes.hxx"
60 #include "helper.hxx"
62 using namespace ::com::sun::star::uno;
63 using namespace ::com::sun::star::beans;
64 using namespace ::com::sun::star::util;
65 using namespace ::com::sun::star::frame;
66 using namespace ::com::sun::star::task;
68 //***************************************************************************
69 // SfxURLToolBoxControl_Impl
70 //***************************************************************************
72 SFX_IMPL_TOOLBOX_CONTROL(SfxURLToolBoxControl_Impl,SfxStringItem)
74 SfxURLToolBoxControl_Impl::SfxURLToolBoxControl_Impl( sal_uInt16 nSlotId, sal_uInt16 nId, ToolBox& rBox )
75 : SfxToolBoxControl( nSlotId, nId, rBox ),
76 pAccExec( 0 )
78 addStatusListener( rtl::OUString( ".uno:CurrentURL" ));
81 SfxURLToolBoxControl_Impl::~SfxURLToolBoxControl_Impl()
83 delete pAccExec;
86 SvtURLBox* SfxURLToolBoxControl_Impl::GetURLBox() const
88 return (SvtURLBox*)GetToolBox().GetItemWindow( GetId() );
91 //***************************************************************************
93 void SfxURLToolBoxControl_Impl::OpenURL( const String& rName, sal_Bool /*bNew*/ ) const
95 String aName;
96 String aFilter;
97 String aOptions;
99 INetURLObject aObj( rName );
100 if ( aObj.GetProtocol() == INET_PROT_NOT_VALID )
102 String aBaseURL = GetURLBox()->GetBaseURL();
103 aName = SvtURLBox::ParseSmart( rName, aBaseURL, SvtPathOptions().GetWorkPath() );
105 else
106 aName = rName;
108 if ( !aName.Len() )
109 return;
111 Reference< XDispatchProvider > xDispatchProvider( getFrameInterface(), UNO_QUERY );
112 if ( xDispatchProvider.is() && m_xServiceManager.is() )
114 URL aTargetURL;
115 ::rtl::OUString aTarget( ::rtl::OUString("_default"));
117 aTargetURL.Complete = aName;
119 getURLTransformer()->parseStrict( aTargetURL );
120 Reference< XDispatch > xDispatch = xDispatchProvider->queryDispatch( aTargetURL, aTarget, 0 );
121 if ( xDispatch.is() )
123 Sequence< PropertyValue > aArgs( 2 );
124 aArgs[0].Name = ::rtl::OUString("Referer");
125 aArgs[0].Value = makeAny( ::rtl::OUString(SFX_REFERER_USER ));
126 aArgs[1].Name = ::rtl::OUString( "FileName" );
127 aArgs[1].Value = makeAny( ::rtl::OUString( aName ));
129 if ( aFilter.Len() )
131 aArgs.realloc( 4 );
132 aArgs[2].Name = ::rtl::OUString("FilterOptions");
133 aArgs[2].Value = makeAny( ::rtl::OUString( aOptions ));
134 aArgs[3].Name = ::rtl::OUString("FilterName");
135 aArgs[3].Value = makeAny( ::rtl::OUString( aFilter ));
138 SfxURLToolBoxControl_Impl::ExecuteInfo* pExecuteInfo = new SfxURLToolBoxControl_Impl::ExecuteInfo;
139 pExecuteInfo->xDispatch = xDispatch;
140 pExecuteInfo->aTargetURL = aTargetURL;
141 pExecuteInfo->aArgs = aArgs;
142 Application::PostUserEvent( STATIC_LINK( 0, SfxURLToolBoxControl_Impl, ExecuteHdl_Impl), pExecuteInfo );
147 //--------------------------------------------------------------------
149 IMPL_STATIC_LINK_NOINSTANCE( SfxURLToolBoxControl_Impl, ExecuteHdl_Impl, ExecuteInfo*, pExecuteInfo )
153 // Asynchronous execution as this can lead to our own destruction!
154 // Framework can recycle our current frame and the layout manager disposes all user interface
155 // elements if a component gets detached from its frame!
156 pExecuteInfo->xDispatch->dispatch( pExecuteInfo->aTargetURL, pExecuteInfo->aArgs );
158 catch ( Exception& )
162 delete pExecuteInfo;
163 return 0;
167 Window* SfxURLToolBoxControl_Impl::CreateItemWindow( Window* pParent )
169 SvtURLBox* pURLBox = new SvtURLBox( pParent );
170 pURLBox->SetOpenHdl( LINK( this, SfxURLToolBoxControl_Impl, OpenHdl ) );
171 pURLBox->SetSelectHdl( LINK( this, SfxURLToolBoxControl_Impl, SelectHdl ) );
173 return pURLBox;
176 IMPL_LINK_NOARG(SfxURLToolBoxControl_Impl, SelectHdl)
178 SvtURLBox* pURLBox = GetURLBox();
179 String aName( pURLBox->GetURL() );
181 if ( !pURLBox->IsTravelSelect() && aName.Len() )
182 OpenURL( aName, sal_False );
184 return 1L;
187 IMPL_LINK_NOARG(SfxURLToolBoxControl_Impl, OpenHdl)
189 SvtURLBox* pURLBox = GetURLBox();
190 OpenURL( pURLBox->GetURL(), pURLBox->IsCtrlOpen() );
192 if ( m_xServiceManager.is() )
194 Reference< XFramesSupplier > xDesktop( m_xServiceManager->createInstance(
195 ::rtl::OUString("com.sun.star.frame.Desktop")),
196 UNO_QUERY );
197 Reference< XFrame > xFrame( xDesktop->getActiveFrame(), UNO_QUERY );
198 if ( xFrame.is() )
200 Window* pWin = VCLUnoHelper::GetWindow( xFrame->getContainerWindow() );
201 if ( pWin )
203 pWin->GrabFocus();
204 pWin->ToTop( TOTOP_RESTOREWHENMIN );
209 return 1L;
212 //***************************************************************************
214 void SfxURLToolBoxControl_Impl::StateChanged
216 sal_uInt16 nSID,
217 SfxItemState eState,
218 const SfxPoolItem* pState
221 if ( nSID == SID_OPENURL )
223 // Disable URL box if command is disabled
224 GetURLBox()->Enable( SFX_ITEM_DISABLED != eState );
227 if ( GetURLBox()->IsEnabled() )
229 if( nSID == SID_FOCUSURLBOX )
231 if ( GetURLBox()->IsVisible() )
232 GetURLBox()->GrabFocus();
234 else if ( !GetURLBox()->IsModified() && SFX_ITEM_AVAILABLE == eState )
236 SvtURLBox* pURLBox = GetURLBox();
237 pURLBox->Clear();
239 ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue > > lList = SvtHistoryOptions().GetList(eHISTORY);
240 for (sal_Int32 i=0; i<lList.getLength(); ++i)
242 ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue > lProps = lList[i];
243 for (sal_Int32 p=0; p<lProps.getLength(); ++p)
245 if (lProps[p].Name != HISTORY_PROPERTYNAME_URL)
246 continue;
248 ::rtl::OUString sURL;
249 if (!(lProps[p].Value>>=sURL) || sURL.isEmpty())
250 continue;
252 INetURLObject aURL ( sURL );
253 String sMainURL( aURL.GetMainURL( INetURLObject::DECODE_WITH_CHARSET ) );
254 String sFile;
256 if (::utl::LocalFileHelper::ConvertURLToSystemPath(sMainURL,sFile))
257 pURLBox->InsertEntry(sFile);
258 else
259 pURLBox->InsertEntry(sMainURL);
263 const SfxStringItem *pURL = PTR_CAST(SfxStringItem,pState);
264 String aRep( pURL->GetValue() );
265 INetURLObject aURL( aRep );
266 INetProtocol eProt = aURL.GetProtocol();
267 if ( eProt == INET_PROT_FILE )
269 pURLBox->SetText( aURL.PathToFileName() );
271 else
272 pURLBox->SetText( aURL.GetURLNoPass() );
277 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */