Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
21 : #include "inettbc.hxx"
22 :
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 <svl/folderrestriction.hxx>
31 : #include <vcl/toolbox.hxx>
32 : #include <toolkit/unohlp.hxx>
33 : #include <osl/thread.hxx>
34 : #include <osl/mutex.hxx>
35 : #include <rtl/ustring.hxx>
36 :
37 : #include <svl/itemset.hxx>
38 : #include <svl/urihelper.hxx>
39 : #include <unotools/pathoptions.hxx>
40 : #include <svtools/asynclink.hxx>
41 : #include <svtools/inettbc.hxx>
42 :
43 : #include <unotools/localfilehelper.hxx>
44 : #include <comphelper/processfactory.hxx>
45 :
46 : #include <sfx2/sfx.hrc>
47 : #include <sfx2/dispatch.hxx>
48 : #include <sfx2/viewfrm.hxx>
49 : #include <sfx2/objsh.hxx>
50 : #include "referers.hxx"
51 : #include "sfxtypes.hxx"
52 : #include "helper.hxx"
53 :
54 : using namespace ::com::sun::star::uno;
55 : using namespace ::com::sun::star::beans;
56 : using namespace ::com::sun::star::util;
57 : using namespace ::com::sun::star::frame;
58 : using namespace ::com::sun::star::task;
59 :
60 : //***************************************************************************
61 : // SfxURLToolBoxControl_Impl
62 : //***************************************************************************
63 :
64 19 : SFX_IMPL_TOOLBOX_CONTROL(SfxURLToolBoxControl_Impl,SfxStringItem)
65 :
66 0 : SfxURLToolBoxControl_Impl::SfxURLToolBoxControl_Impl( sal_uInt16 nSlotId, sal_uInt16 nId, ToolBox& rBox )
67 : : SfxToolBoxControl( nSlotId, nId, rBox ),
68 0 : pAccExec( 0 )
69 : {
70 0 : addStatusListener( rtl::OUString( ".uno:CurrentURL" ));
71 0 : }
72 :
73 0 : SfxURLToolBoxControl_Impl::~SfxURLToolBoxControl_Impl()
74 : {
75 0 : delete pAccExec;
76 0 : }
77 :
78 0 : SvtURLBox* SfxURLToolBoxControl_Impl::GetURLBox() const
79 : {
80 0 : return (SvtURLBox*)GetToolBox().GetItemWindow( GetId() );
81 : }
82 :
83 : //***************************************************************************
84 :
85 0 : void SfxURLToolBoxControl_Impl::OpenURL( const String& rName, sal_Bool /*bNew*/ ) const
86 : {
87 0 : String aName;
88 0 : String aFilter;
89 0 : String aOptions;
90 :
91 0 : INetURLObject aObj( rName );
92 0 : if ( aObj.GetProtocol() == INET_PROT_NOT_VALID )
93 : {
94 0 : String aBaseURL = GetURLBox()->GetBaseURL();
95 0 : aName = SvtURLBox::ParseSmart( rName, aBaseURL, SvtPathOptions().GetWorkPath() );
96 : }
97 : else
98 0 : aName = rName;
99 :
100 0 : if ( !aName.Len() )
101 0 : return;
102 :
103 0 : Reference< XDispatchProvider > xDispatchProvider( getFrameInterface(), UNO_QUERY );
104 0 : if ( xDispatchProvider.is() && m_xServiceManager.is() )
105 : {
106 0 : URL aTargetURL;
107 0 : ::rtl::OUString aTarget( ::rtl::OUString("_default"));
108 :
109 0 : aTargetURL.Complete = aName;
110 :
111 0 : getURLTransformer()->parseStrict( aTargetURL );
112 0 : Reference< XDispatch > xDispatch = xDispatchProvider->queryDispatch( aTargetURL, aTarget, 0 );
113 0 : if ( xDispatch.is() )
114 : {
115 0 : Sequence< PropertyValue > aArgs( 2 );
116 0 : aArgs[0].Name = ::rtl::OUString("Referer");
117 0 : aArgs[0].Value = makeAny( ::rtl::OUString(SFX_REFERER_USER ));
118 0 : aArgs[1].Name = ::rtl::OUString( "FileName" );
119 0 : aArgs[1].Value = makeAny( ::rtl::OUString( aName ));
120 :
121 0 : if ( aFilter.Len() )
122 : {
123 0 : aArgs.realloc( 4 );
124 0 : aArgs[2].Name = ::rtl::OUString("FilterOptions");
125 0 : aArgs[2].Value = makeAny( ::rtl::OUString( aOptions ));
126 0 : aArgs[3].Name = ::rtl::OUString("FilterName");
127 0 : aArgs[3].Value = makeAny( ::rtl::OUString( aFilter ));
128 : }
129 :
130 0 : SfxURLToolBoxControl_Impl::ExecuteInfo* pExecuteInfo = new SfxURLToolBoxControl_Impl::ExecuteInfo;
131 0 : pExecuteInfo->xDispatch = xDispatch;
132 0 : pExecuteInfo->aTargetURL = aTargetURL;
133 0 : pExecuteInfo->aArgs = aArgs;
134 0 : Application::PostUserEvent( STATIC_LINK( 0, SfxURLToolBoxControl_Impl, ExecuteHdl_Impl), pExecuteInfo );
135 0 : }
136 0 : }
137 : }
138 :
139 : //--------------------------------------------------------------------
140 :
141 0 : IMPL_STATIC_LINK_NOINSTANCE( SfxURLToolBoxControl_Impl, ExecuteHdl_Impl, ExecuteInfo*, pExecuteInfo )
142 : {
143 : try
144 : {
145 : // Asynchronous execution as this can lead to our own destruction!
146 : // Framework can recycle our current frame and the layout manager disposes all user interface
147 : // elements if a component gets detached from its frame!
148 0 : pExecuteInfo->xDispatch->dispatch( pExecuteInfo->aTargetURL, pExecuteInfo->aArgs );
149 : }
150 0 : catch ( Exception& )
151 : {
152 : }
153 :
154 0 : delete pExecuteInfo;
155 0 : return 0;
156 : }
157 :
158 :
159 0 : Window* SfxURLToolBoxControl_Impl::CreateItemWindow( Window* pParent )
160 : {
161 0 : SvtURLBox* pURLBox = new SvtURLBox( pParent );
162 0 : pURLBox->SetOpenHdl( LINK( this, SfxURLToolBoxControl_Impl, OpenHdl ) );
163 0 : pURLBox->SetSelectHdl( LINK( this, SfxURLToolBoxControl_Impl, SelectHdl ) );
164 :
165 0 : return pURLBox;
166 : }
167 :
168 0 : IMPL_LINK_NOARG(SfxURLToolBoxControl_Impl, SelectHdl)
169 : {
170 0 : SvtURLBox* pURLBox = GetURLBox();
171 0 : String aName( pURLBox->GetURL() );
172 :
173 0 : if ( !pURLBox->IsTravelSelect() && aName.Len() )
174 0 : OpenURL( aName, sal_False );
175 :
176 0 : return 1L;
177 : }
178 :
179 0 : IMPL_LINK_NOARG(SfxURLToolBoxControl_Impl, OpenHdl)
180 : {
181 0 : SvtURLBox* pURLBox = GetURLBox();
182 0 : OpenURL( pURLBox->GetURL(), pURLBox->IsCtrlOpen() );
183 :
184 0 : if ( m_xServiceManager.is() )
185 : {
186 0 : Reference< XDesktop2 > xDesktop = Desktop::create( comphelper::getComponentContext(m_xServiceManager) );
187 0 : Reference< XFrame > xFrame( xDesktop->getActiveFrame(), UNO_QUERY );
188 0 : if ( xFrame.is() )
189 : {
190 0 : Window* pWin = VCLUnoHelper::GetWindow( xFrame->getContainerWindow() );
191 0 : if ( pWin )
192 : {
193 0 : pWin->GrabFocus();
194 0 : pWin->ToTop( TOTOP_RESTOREWHENMIN );
195 : }
196 0 : }
197 : }
198 :
199 0 : return 1L;
200 : }
201 :
202 : //***************************************************************************
203 :
204 0 : void SfxURLToolBoxControl_Impl::StateChanged
205 : (
206 : sal_uInt16 nSID,
207 : SfxItemState eState,
208 : const SfxPoolItem* pState
209 : )
210 : {
211 0 : if ( nSID == SID_OPENURL )
212 : {
213 : // Disable URL box if command is disabled
214 0 : GetURLBox()->Enable( SFX_ITEM_DISABLED != eState );
215 : }
216 :
217 0 : if ( GetURLBox()->IsEnabled() )
218 : {
219 0 : if( nSID == SID_FOCUSURLBOX )
220 : {
221 0 : if ( GetURLBox()->IsVisible() )
222 0 : GetURLBox()->GrabFocus();
223 : }
224 0 : else if ( !GetURLBox()->IsModified() && SFX_ITEM_AVAILABLE == eState )
225 : {
226 0 : SvtURLBox* pURLBox = GetURLBox();
227 0 : pURLBox->Clear();
228 :
229 0 : ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue > > lList = SvtHistoryOptions().GetList(eHISTORY);
230 0 : for (sal_Int32 i=0; i<lList.getLength(); ++i)
231 : {
232 0 : ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue > lProps = lList[i];
233 0 : for (sal_Int32 p=0; p<lProps.getLength(); ++p)
234 : {
235 0 : if (lProps[p].Name != HISTORY_PROPERTYNAME_URL)
236 0 : continue;
237 :
238 0 : ::rtl::OUString sURL;
239 0 : if (!(lProps[p].Value>>=sURL) || sURL.isEmpty())
240 0 : continue;
241 :
242 0 : INetURLObject aURL ( sURL );
243 0 : String sMainURL( aURL.GetMainURL( INetURLObject::DECODE_WITH_CHARSET ) );
244 0 : String sFile;
245 :
246 0 : if (::utl::LocalFileHelper::ConvertURLToSystemPath(sMainURL,sFile))
247 0 : pURLBox->InsertEntry(sFile);
248 : else
249 0 : pURLBox->InsertEntry(sMainURL);
250 0 : }
251 0 : }
252 :
253 0 : const SfxStringItem *pURL = PTR_CAST(SfxStringItem,pState);
254 0 : String aRep( pURL->GetValue() );
255 0 : INetURLObject aURL( aRep );
256 0 : INetProtocol eProt = aURL.GetProtocol();
257 0 : if ( eProt == INET_PROT_FILE )
258 : {
259 0 : pURLBox->SetText( aURL.PathToFileName() );
260 : }
261 : else
262 0 : pURLBox->SetText( aURL.GetURLNoPass() );
263 : }
264 : }
265 0 : }
266 :
267 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|