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 <vcl/toolbox.hxx>
31 : #include <toolkit/helper/vclunohelper.hxx>
32 : #include <osl/thread.hxx>
33 : #include <osl/mutex.hxx>
34 : #include <rtl/ustring.hxx>
35 :
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>
41 :
42 : #include <unotools/localfilehelper.hxx>
43 : #include <comphelper/processfactory.hxx>
44 :
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"
51 :
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;
57 :
58 :
59 : // SfxURLToolBoxControl_Impl
60 :
61 :
62 208 : SFX_IMPL_TOOLBOX_CONTROL(SfxURLToolBoxControl_Impl,SfxStringItem)
63 :
64 0 : SfxURLToolBoxControl_Impl::SfxURLToolBoxControl_Impl( sal_uInt16 nSlotId, sal_uInt16 nId, ToolBox& rBox )
65 0 : : SfxToolBoxControl( nSlotId, nId, rBox )
66 : {
67 0 : addStatusListener( OUString( ".uno:CurrentURL" ));
68 0 : }
69 :
70 0 : SfxURLToolBoxControl_Impl::~SfxURLToolBoxControl_Impl()
71 : {
72 0 : }
73 :
74 0 : SvtURLBox* SfxURLToolBoxControl_Impl::GetURLBox() const
75 : {
76 0 : return static_cast<SvtURLBox*>(GetToolBox().GetItemWindow( GetId() ));
77 : }
78 :
79 :
80 :
81 0 : void SfxURLToolBoxControl_Impl::OpenURL( const OUString& rName, bool /*bNew*/ ) const
82 : {
83 0 : OUString aName;
84 0 : OUString aFilter;
85 0 : OUString aOptions;
86 :
87 0 : INetURLObject aObj( rName );
88 0 : if ( aObj.GetProtocol() == INetProtocol::NotValid )
89 : {
90 0 : OUString aBaseURL = GetURLBox()->GetBaseURL();
91 0 : aName = SvtURLBox::ParseSmart( rName, aBaseURL, SvtPathOptions().GetWorkPath() );
92 : }
93 : else
94 0 : aName = rName;
95 :
96 0 : if ( aName.isEmpty() )
97 0 : return;
98 :
99 0 : Reference< XDispatchProvider > xDispatchProvider( getFrameInterface(), UNO_QUERY );
100 0 : if ( xDispatchProvider.is() )
101 : {
102 0 : URL aTargetURL;
103 0 : OUString aTarget( "_default" );
104 :
105 0 : aTargetURL.Complete = aName;
106 :
107 0 : getURLTransformer()->parseStrict( aTargetURL );
108 0 : Reference< XDispatch > xDispatch = xDispatchProvider->queryDispatch( aTargetURL, aTarget, 0 );
109 0 : if ( xDispatch.is() )
110 : {
111 0 : Sequence< PropertyValue > aArgs( 2 );
112 0 : aArgs[0].Name = "Referer";
113 0 : aArgs[0].Value = makeAny( OUString( "private:user" ));
114 0 : aArgs[1].Name = "FileName";
115 0 : aArgs[1].Value = makeAny( OUString( aName ));
116 :
117 0 : if ( !aFilter.isEmpty() )
118 : {
119 0 : aArgs.realloc( 4 );
120 0 : aArgs[2].Name = "FilterOptions";
121 0 : aArgs[2].Value = makeAny( OUString( aOptions ));
122 0 : aArgs[3].Name = "FilterName";
123 0 : aArgs[3].Value = makeAny( OUString( aFilter ));
124 : }
125 :
126 0 : SfxURLToolBoxControl_Impl::ExecuteInfo* pExecuteInfo = new SfxURLToolBoxControl_Impl::ExecuteInfo;
127 0 : pExecuteInfo->xDispatch = xDispatch;
128 0 : pExecuteInfo->aTargetURL = aTargetURL;
129 0 : pExecuteInfo->aArgs = aArgs;
130 0 : Application::PostUserEvent( LINK( 0, SfxURLToolBoxControl_Impl, ExecuteHdl_Impl), pExecuteInfo );
131 0 : }
132 0 : }
133 : }
134 :
135 :
136 :
137 0 : IMPL_STATIC_LINK( SfxURLToolBoxControl_Impl, ExecuteHdl_Impl, ExecuteInfo*, pExecuteInfo )
138 : {
139 : try
140 : {
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 0 : pExecuteInfo->xDispatch->dispatch( pExecuteInfo->aTargetURL, pExecuteInfo->aArgs );
145 : }
146 0 : catch ( Exception& )
147 : {
148 : }
149 :
150 0 : delete pExecuteInfo;
151 0 : return 0;
152 : }
153 :
154 :
155 0 : VclPtr<vcl::Window> SfxURLToolBoxControl_Impl::CreateItemWindow( vcl::Window* pParent )
156 : {
157 0 : VclPtrInstance<SvtURLBox> pURLBox( pParent );
158 0 : pURLBox->SetOpenHdl( LINK( this, SfxURLToolBoxControl_Impl, OpenHdl ) );
159 0 : pURLBox->SetSelectHdl( LINK( this, SfxURLToolBoxControl_Impl, SelectHdl ) );
160 0 : return pURLBox.get();
161 : }
162 :
163 0 : IMPL_LINK_NOARG(SfxURLToolBoxControl_Impl, SelectHdl)
164 : {
165 0 : SvtURLBox* pURLBox = GetURLBox();
166 0 : OUString aName( pURLBox->GetURL() );
167 :
168 0 : if ( !pURLBox->IsTravelSelect() && !aName.isEmpty() )
169 0 : OpenURL( aName, false );
170 :
171 0 : return 1L;
172 : }
173 :
174 0 : IMPL_LINK_NOARG(SfxURLToolBoxControl_Impl, OpenHdl)
175 : {
176 0 : SvtURLBox* pURLBox = GetURLBox();
177 0 : OpenURL( pURLBox->GetURL(), pURLBox->IsCtrlOpen() );
178 :
179 0 : Reference< XDesktop2 > xDesktop = Desktop::create( m_xContext );
180 0 : Reference< XFrame > xFrame( xDesktop->getActiveFrame(), UNO_QUERY );
181 0 : if ( xFrame.is() )
182 : {
183 0 : vcl::Window* pWin = VCLUnoHelper::GetWindow( xFrame->getContainerWindow() );
184 0 : if ( pWin )
185 : {
186 0 : pWin->GrabFocus();
187 0 : pWin->ToTop( ToTopFlags::RestoreWhenMin );
188 : }
189 : }
190 :
191 0 : return 1L;
192 : }
193 :
194 :
195 :
196 0 : void SfxURLToolBoxControl_Impl::StateChanged
197 : (
198 : sal_uInt16 nSID,
199 : SfxItemState eState,
200 : const SfxPoolItem* pState
201 : )
202 : {
203 0 : if ( nSID == SID_OPENURL )
204 : {
205 : // Disable URL box if command is disabled
206 0 : GetURLBox()->Enable( SfxItemState::DISABLED != eState );
207 : }
208 :
209 0 : if ( GetURLBox()->IsEnabled() )
210 : {
211 0 : if( nSID == SID_FOCUSURLBOX )
212 : {
213 0 : if ( GetURLBox()->IsVisible() )
214 0 : GetURLBox()->GrabFocus();
215 : }
216 0 : else if ( !GetURLBox()->IsModified() && SfxItemState::DEFAULT == eState )
217 : {
218 0 : SvtURLBox* pURLBox = GetURLBox();
219 0 : pURLBox->Clear();
220 :
221 0 : ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue > > lList = SvtHistoryOptions().GetList(ePICKLIST);
222 0 : for (sal_Int32 i=0; i<lList.getLength(); ++i)
223 : {
224 0 : ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue > lProps = lList[i];
225 0 : for (sal_Int32 p=0; p<lProps.getLength(); ++p)
226 : {
227 0 : if (lProps[p].Name != HISTORY_PROPERTYNAME_URL)
228 0 : continue;
229 :
230 0 : OUString sURL;
231 0 : if (!(lProps[p].Value>>=sURL) || sURL.isEmpty())
232 0 : continue;
233 :
234 0 : INetURLObject aURL ( sURL );
235 0 : OUString sMainURL( aURL.GetMainURL( INetURLObject::DECODE_WITH_CHARSET ) );
236 0 : OUString sFile;
237 :
238 0 : if (::utl::LocalFileHelper::ConvertURLToSystemPath(sMainURL,sFile))
239 0 : pURLBox->InsertEntry(sFile);
240 : else
241 0 : pURLBox->InsertEntry(sMainURL);
242 0 : }
243 0 : }
244 :
245 0 : const SfxStringItem *pURL = PTR_CAST(SfxStringItem,pState);
246 0 : OUString aRep( pURL->GetValue() );
247 0 : INetURLObject aURL( aRep );
248 0 : INetProtocol eProt = aURL.GetProtocol();
249 0 : if ( eProt == INetProtocol::File )
250 : {
251 0 : pURLBox->SetText( aURL.PathToFileName() );
252 : }
253 : else
254 0 : pURLBox->SetText( aURL.GetURLNoPass() );
255 : }
256 : }
257 648 : }
258 :
259 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|