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