Update ooo320-m1
[ooovba.git] / fpicker / source / office / asyncfilepicker.cxx
blob467f7d99ac7b177a7d55e5caa7eb55b7b46af3f6
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: asyncfilepicker.cxx,v $
10 * $Revision: 1.8 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_fpicker.hxx"
34 #include "asyncfilepicker.hxx"
35 #include "iodlg.hxx"
36 #include "svtools/fileview.hxx"
37 #include <tools/debug.hxx>
39 #include <memory>
41 //........................................................................
42 namespace svt
44 //........................................................................
46 //====================================================================
47 //= AsyncPickerAction
48 //====================================================================
49 DBG_NAME( AsyncPickerAction )
50 //--------------------------------------------------------------------
51 AsyncPickerAction::AsyncPickerAction( SvtFileDialog* _pDialog, SvtFileView* _pView, const Action _eAction )
52 :m_refCount ( 0 )
53 ,m_eAction ( _eAction )
54 ,m_pView ( _pView )
55 ,m_pDialog ( _pDialog )
56 ,m_bRunning ( false )
58 DBG_CTOR( AsyncPickerAction, NULL );
59 DBG_ASSERT( m_pDialog, "AsyncPickerAction::AsyncPickerAction: invalid dialog!" );
60 DBG_ASSERT( m_pView, "AsyncPickerAction::AsyncPickerAction: invalid view!" );
63 //--------------------------------------------------------------------
64 AsyncPickerAction::~AsyncPickerAction()
66 DBG_DTOR( AsyncPickerAction, NULL );
69 //--------------------------------------------------------------------
70 oslInterlockedCount SAL_CALL AsyncPickerAction::acquire()
72 return osl_incrementInterlockedCount( &m_refCount );
75 //--------------------------------------------------------------------
76 oslInterlockedCount SAL_CALL AsyncPickerAction::release()
78 if ( 0 == osl_decrementInterlockedCount( &m_refCount ) )
80 delete this;
81 return 0;
83 return m_refCount;
86 //--------------------------------------------------------------------
87 void AsyncPickerAction::cancel()
89 DBG_TESTSOLARMUTEX();
90 // if this asserts, we'd need to have an own mutex per instance
92 OSL_ENSURE( m_bRunning, "AsyncPickerAction::cancel: not running" );
93 if ( m_pView )
94 m_pView->CancelRunningAsyncAction();
97 //--------------------------------------------------------------------
98 void AsyncPickerAction::execute(
99 const String& _rURL,
100 const String& _rFilter,
101 sal_Int32 _nMinTimeout,
102 sal_Int32 _nMaxTimeout,
103 const OUStringList& rBlackList )
105 DBG_TESTSOLARMUTEX();
106 // if this asserts, we'd need to have an own mutex per instance
108 sal_Int32 nMinTimeout = _nMinTimeout;
109 sal_Int32 nMaxTimeout = _nMaxTimeout;
110 // normalizations
111 if ( nMinTimeout < 0 )
112 // if negative, this is considered as "do it synchronously"
113 nMinTimeout = 0;
114 else if ( nMinTimeout < 1000 )
115 nMinTimeout = 1000;
116 if ( nMaxTimeout <= nMinTimeout )
117 nMaxTimeout = nMinTimeout + 30000;
119 ::std::auto_ptr< FileViewAsyncAction > pActionDescriptor;
120 if ( nMinTimeout )
122 pActionDescriptor.reset( new FileViewAsyncAction );
123 pActionDescriptor->nMinTimeout = nMinTimeout;
124 pActionDescriptor->nMaxTimeout = nMaxTimeout;
125 pActionDescriptor->aFinishHandler = LINK( this, AsyncPickerAction, OnActionDone );
128 FileViewResult eResult = eFailure;
129 m_sURL = _rURL;
130 switch ( m_eAction )
132 case ePrevLevel:
133 eResult = m_pView->PreviousLevel( pActionDescriptor.get() );
134 break;
136 case eOpenURL:
137 eResult = m_pView->Initialize( _rURL, _rFilter, pActionDescriptor.get(), rBlackList );
138 break;
140 case eExecuteFilter:
141 // preserve the filename (FS: why?)
142 m_sFileName = m_pDialog->getCurrentFileText();
143 // execute the new filter
144 eResult = m_pView->ExecuteFilter( _rFilter, pActionDescriptor.get() );
145 break;
147 default:
148 DBG_ERROR( "AsyncPickerAction::execute: unknown action!" );
149 break;
152 acquire();
153 if ( ( eResult == eSuccess ) || ( eResult == eFailure ) )
155 // the handler is only called if the action could not be finished within
156 // the given minimum time period. In case of success, we need to call it
157 // explicitly
158 OnActionDone( reinterpret_cast< void* >( eResult ) );
160 else if ( eResult == eStillRunning )
162 m_bRunning = true;
163 m_pDialog->onAsyncOperationStarted();
167 //--------------------------------------------------------------------
168 IMPL_LINK( AsyncPickerAction, OnActionDone, void*, pEmptyArg )
170 DBG_TESTSOLARMUTEX();
171 // if this asserts, we'd need to have an own mutex per instance
173 FileViewResult eResult = static_cast< FileViewResult >( reinterpret_cast< sal_IntPtr >( pEmptyArg ) );
174 OSL_ENSURE( eStillRunning != eResult, "AsyncPickerAction::OnActionDone: invalid result!" );
176 // release once (since we acquired in |execute|), but keep alive until the
177 // end of the method
178 ::rtl::Reference< AsyncPickerAction > xKeepAlive( this );
179 release();
181 m_pDialog->onAsyncOperationFinished();
182 m_bRunning = true;
184 if ( eFailure == eResult )
185 // TODO: do we need some kind of cleanup here?
186 return 0L;
188 if ( eTimeout == eResult )
190 m_pDialog->displayIOException( m_sURL, ::com::sun::star::ucb::IOErrorCode_CANT_READ );
191 return 0L;
194 OSL_ENSURE( eSuccess == eResult, "AsyncPickerAction::OnActionDone: what else valid results are there?" );
196 switch ( m_eAction )
198 case ePrevLevel:
199 case eOpenURL:
200 m_pDialog->UpdateControls( m_pView->GetViewURL() );
201 break;
203 case eExecuteFilter:
204 // restore the filename
205 m_pView->SetNoSelection();
206 m_pDialog->setCurrentFileText( m_sFileName, true );
208 // notify listeners
209 m_pDialog->FilterSelect();
210 break;
212 default:
213 DBG_ERROR( "AsyncPickerAction::OnActionDone: unknown action!" );
214 break;
217 return 1L;
220 //........................................................................
221 } // namespace svt
222 //........................................................................