xref: /AOO41X/main/sfx2/source/inet/inettbc.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sfx2.hxx"
30 
31 #include "inettbc.hxx"
32 
33 #ifndef GCC
34 #endif
35 #include <com/sun/star/uno/Any.h>
36 #ifndef _COM_SUN_STAR_FRAME_XFRAMESSUPLLIER_HPP_
37 #include <com/sun/star/frame/XFramesSupplier.hpp>
38 #endif
39 #include <com/sun/star/task/XInteractionHandler.hpp>
40 #include <svl/eitem.hxx>
41 #include <svl/stritem.hxx>
42 #include <unotools/historyoptions.hxx>
43 #include <svl/folderrestriction.hxx>
44 #include <vcl/toolbox.hxx>
45 #ifndef _TOOLKIT_HELPER_VCLUNOHELPER_HXX_
46 #include <toolkit/unohlp.hxx>
47 #endif
48 #ifndef _VOS_THREAD_HXX //autogen
49 #include <vos/thread.hxx>
50 #endif
51 #ifndef _VOS_MUTEX_HXX //autogen
52 #include <vos/mutex.hxx>
53 #endif
54 #include <rtl/ustring.hxx>
55 
56 #include <svl/itemset.hxx>
57 #include <svl/urihelper.hxx>
58 #include <unotools/pathoptions.hxx>
59 #include <svtools/asynclink.hxx>
60 #include <svtools/inettbc.hxx>
61 
62 #include <unotools/localfilehelper.hxx>
63 #include <comphelper/processfactory.hxx>
64 
65 #include <sfx2/sfx.hrc>
66 #include <sfx2/dispatch.hxx>
67 #include <sfx2/viewfrm.hxx>
68 #include <sfx2/objsh.hxx>
69 #include "referers.hxx"
70 #include "sfxtypes.hxx"
71 #include "helper.hxx"
72 
73 using namespace ::com::sun::star::uno;
74 using namespace ::com::sun::star::beans;
75 using namespace ::com::sun::star::util;
76 using namespace ::com::sun::star::frame;
77 using namespace ::com::sun::star::task;
78 
79 //***************************************************************************
80 // SfxURLToolBoxControl_Impl
81 //***************************************************************************
82 
83 SFX_IMPL_TOOLBOX_CONTROL(SfxURLToolBoxControl_Impl,SfxStringItem)
84 
85 SfxURLToolBoxControl_Impl::SfxURLToolBoxControl_Impl( sal_uInt16 nSlotId, sal_uInt16 nId, ToolBox& rBox )
86     : SfxToolBoxControl( nSlotId, nId, rBox ),
87     pAccExec( 0 )
88 {
89     addStatusListener( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".uno:CurrentURL" )));
90 }
91 
92 SfxURLToolBoxControl_Impl::~SfxURLToolBoxControl_Impl()
93 {
94     delete pAccExec;
95 }
96 
97 SvtURLBox* SfxURLToolBoxControl_Impl::GetURLBox() const
98 {
99 	return (SvtURLBox*)GetToolBox().GetItemWindow( GetId() );
100 }
101 
102 //***************************************************************************
103 
104 void SfxURLToolBoxControl_Impl::OpenURL( const String& rName, sal_Bool /*bNew*/ ) const
105 {
106     String aName;
107     String aFilter;
108     String aOptions;
109 
110     INetURLObject aObj( rName );
111     if ( aObj.GetProtocol() == INET_PROT_NOT_VALID )
112     {
113         String aBaseURL = GetURLBox()->GetBaseURL();
114         aName = SvtURLBox::ParseSmart( rName, aBaseURL, SvtPathOptions().GetWorkPath() );
115     }
116     else
117         aName = rName;
118 
119 	if ( !aName.Len() )
120 		return;
121 
122     Reference< XDispatchProvider > xDispatchProvider( getFrameInterface(), UNO_QUERY );
123     if ( xDispatchProvider.is() && m_xServiceManager.is() )
124     {
125         URL             aTargetURL;
126         ::rtl::OUString	aTarget( ::rtl::OUString::createFromAscii( "_default" ));
127 
128         aTargetURL.Complete = aName;
129 
130         getURLTransformer()->parseStrict( aTargetURL );
131         Reference< XDispatch > xDispatch = xDispatchProvider->queryDispatch( aTargetURL, aTarget, 0 );
132         if ( xDispatch.is() )
133         {
134             Sequence< PropertyValue > aArgs( 2 );
135             aArgs[0].Name = ::rtl::OUString::createFromAscii( "Referer" );
136             aArgs[0].Value = makeAny( ::rtl::OUString::createFromAscii( SFX_REFERER_USER ));
137 	        aArgs[1].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "FileName" ));
138 	        aArgs[1].Value = makeAny( ::rtl::OUString( aName ));
139 
140             if ( aFilter.Len() )
141             {
142                 aArgs.realloc( 4 );
143                 aArgs[2].Name = ::rtl::OUString::createFromAscii( "FilterOptions" );
144                 aArgs[2].Value = makeAny( ::rtl::OUString( aOptions ));
145                 aArgs[3].Name = ::rtl::OUString::createFromAscii( "FilterName" );
146                 aArgs[3].Value = makeAny( ::rtl::OUString( aFilter ));
147             }
148 
149             SfxURLToolBoxControl_Impl::ExecuteInfo* pExecuteInfo = new SfxURLToolBoxControl_Impl::ExecuteInfo;
150             pExecuteInfo->xDispatch     = xDispatch;
151             pExecuteInfo->aTargetURL    = aTargetURL;
152             pExecuteInfo->aArgs         = aArgs;
153             Application::PostUserEvent( STATIC_LINK( 0, SfxURLToolBoxControl_Impl, ExecuteHdl_Impl), pExecuteInfo );
154         }
155     }
156 }
157 
158 //--------------------------------------------------------------------
159 
160 IMPL_STATIC_LINK_NOINSTANCE( SfxURLToolBoxControl_Impl, ExecuteHdl_Impl, ExecuteInfo*, pExecuteInfo )
161 {
162     try
163     {
164         // Asynchronous execution as this can lead to our own destruction!
165         // Framework can recycle our current frame and the layout manager disposes all user interface
166         // elements if a component gets detached from its frame!
167         pExecuteInfo->xDispatch->dispatch( pExecuteInfo->aTargetURL, pExecuteInfo->aArgs );
168     }
169     catch ( Exception& )
170     {
171     }
172 
173     delete pExecuteInfo;
174     return 0;
175 }
176 
177 
178 Window* SfxURLToolBoxControl_Impl::CreateItemWindow( Window* pParent )
179 {
180 	SvtURLBox* pURLBox = new SvtURLBox( pParent );
181     pURLBox->SetOpenHdl( LINK( this, SfxURLToolBoxControl_Impl, OpenHdl ) );
182     pURLBox->SetSelectHdl( LINK( this, SfxURLToolBoxControl_Impl, SelectHdl ) );
183 
184 	return pURLBox;
185 }
186 
187 IMPL_LINK( SfxURLToolBoxControl_Impl, SelectHdl, void*, EMPTYARG )
188 {
189     SvtURLBox* pURLBox = GetURLBox();
190     String aName( pURLBox->GetURL() );
191 
192     if ( !pURLBox->IsTravelSelect() && aName.Len() )
193         OpenURL( aName, sal_False );
194 
195     return 1L;
196 }
197 
198 IMPL_LINK( SfxURLToolBoxControl_Impl, OpenHdl, void*, EMPTYARG )
199 {
200     SvtURLBox* pURLBox = GetURLBox();
201     OpenURL( pURLBox->GetURL(), pURLBox->IsCtrlOpen() );
202 
203     if ( m_xServiceManager.is() )
204     {
205         Reference< XFramesSupplier > xDesktop( m_xServiceManager->createInstance(
206                                                 ::rtl::OUString::createFromAscii( "com.sun.star.frame.Desktop" )),
207                                              UNO_QUERY );
208         Reference< XFrame > xFrame( xDesktop->getActiveFrame(), UNO_QUERY );
209         if ( xFrame.is() )
210         {
211             Window* pWin = VCLUnoHelper::GetWindow( xFrame->getContainerWindow() );
212             if ( pWin )
213             {
214                 pWin->GrabFocus();
215                 pWin->ToTop( TOTOP_RESTOREWHENMIN );
216             }
217         }
218     }
219 
220     return 1L;
221 }
222 
223 IMPL_LINK( SfxURLToolBoxControl_Impl, WindowEventListener, VclSimpleEvent*, pEvent )
224 {
225     if ( pAccExec &&
226          pEvent &&
227          pEvent->ISA( VclWindowEvent ) &&
228          ( pEvent->GetId() == VCLEVENT_WINDOW_KEYINPUT ))
229     {
230         VclWindowEvent* pWinEvent = static_cast< VclWindowEvent* >( pEvent );
231         KeyEvent* pKeyEvent = static_cast< KeyEvent* >( pWinEvent->GetData() );
232 
233         pAccExec->execute( pKeyEvent->GetKeyCode() );
234     }
235 
236     return 1;
237 }
238 
239 //***************************************************************************
240 
241 void SfxURLToolBoxControl_Impl::StateChanged
242 (
243 	sal_uInt16              nSID,
244 	SfxItemState        eState,
245 	const SfxPoolItem*  pState
246 )
247 {
248     if ( nSID == SID_OPENURL )
249     {
250         // Disable URL box if command is disabled #111014#
251         GetURLBox()->Enable( SFX_ITEM_DISABLED != eState );
252     }
253 
254     if ( GetURLBox()->IsEnabled() )
255     {
256         if( nSID == SID_FOCUSURLBOX )
257 	    {
258 		    if ( GetURLBox()->IsVisible() )
259 			    GetURLBox()->GrabFocus();
260 	    }
261 	    else if ( !GetURLBox()->IsModified() && SFX_ITEM_AVAILABLE == eState )
262 	    {
263 		    SvtURLBox* pURLBox = GetURLBox();
264 		    pURLBox->Clear();
265 
266             ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue > > lList = SvtHistoryOptions().GetList(eHISTORY);
267             for (sal_Int32 i=0; i<lList.getLength(); ++i)
268             {
269                 ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue > lProps = lList[i];
270                 for (sal_Int32 p=0; p<lProps.getLength(); ++p)
271                 {
272                     if (lProps[p].Name != HISTORY_PROPERTYNAME_URL)
273                         continue;
274 
275                     ::rtl::OUString sURL;
276                     if (!(lProps[p].Value>>=sURL) || !sURL.getLength())
277                         continue;
278 
279                     INetURLObject aURL    ( sURL );
280                     String        sMainURL( aURL.GetMainURL( INetURLObject::DECODE_WITH_CHARSET ) );
281                     String        sFile;
282 
283                     if (::utl::LocalFileHelper::ConvertURLToSystemPath(sMainURL,sFile))
284                         pURLBox->InsertEntry(sFile);
285                     else
286                         pURLBox->InsertEntry(sMainURL);
287                 }
288             }
289 
290 		    const SfxStringItem *pURL = PTR_CAST(SfxStringItem,pState);
291 		    String aRep( pURL->GetValue() );
292 		    INetURLObject aURL( aRep );
293 		    INetProtocol eProt = aURL.GetProtocol();
294             if ( eProt == INET_PROT_FILE )
295             {
296                 pURLBox->SetText( aURL.PathToFileName() );
297             }
298             else
299                 pURLBox->SetText( aURL.GetURLNoPass() );
300 	    }
301     }
302 }
303 
304