1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: asyncfilepicker.cxx,v $
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"
36 #include "svtools/fileview.hxx"
37 #include <tools/debug.hxx>
41 //........................................................................
44 //........................................................................
46 //====================================================================
48 //====================================================================
49 DBG_NAME( AsyncPickerAction
)
50 //--------------------------------------------------------------------
51 AsyncPickerAction::AsyncPickerAction( SvtFileDialog
* _pDialog
, SvtFileView
* _pView
, const Action _eAction
)
53 ,m_eAction ( _eAction
)
55 ,m_pDialog ( _pDialog
)
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
) )
86 //--------------------------------------------------------------------
87 void AsyncPickerAction::cancel()
90 // if this asserts, we'd need to have an own mutex per instance
92 OSL_ENSURE( m_bRunning
, "AsyncPickerAction::cancel: not running" );
94 m_pView
->CancelRunningAsyncAction();
97 //--------------------------------------------------------------------
98 void AsyncPickerAction::execute(
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
;
111 if ( nMinTimeout
< 0 )
112 // if negative, this is considered as "do it synchronously"
114 else if ( nMinTimeout
< 1000 )
116 if ( nMaxTimeout
<= nMinTimeout
)
117 nMaxTimeout
= nMinTimeout
+ 30000;
119 ::std::auto_ptr
< FileViewAsyncAction
> pActionDescriptor
;
122 pActionDescriptor
.reset( new FileViewAsyncAction
);
123 pActionDescriptor
->nMinTimeout
= nMinTimeout
;
124 pActionDescriptor
->nMaxTimeout
= nMaxTimeout
;
125 pActionDescriptor
->aFinishHandler
= LINK( this, AsyncPickerAction
, OnActionDone
);
128 FileViewResult eResult
= eFailure
;
133 eResult
= m_pView
->PreviousLevel( pActionDescriptor
.get() );
137 eResult
= m_pView
->Initialize( _rURL
, _rFilter
, pActionDescriptor
.get(), rBlackList
);
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() );
148 DBG_ERROR( "AsyncPickerAction::execute: unknown action!" );
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
158 OnActionDone( reinterpret_cast< void* >( eResult
) );
160 else if ( eResult
== eStillRunning
)
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
178 ::rtl::Reference
< AsyncPickerAction
> xKeepAlive( this );
181 m_pDialog
->onAsyncOperationFinished();
184 if ( eFailure
== eResult
)
185 // TODO: do we need some kind of cleanup here?
188 if ( eTimeout
== eResult
)
190 m_pDialog
->displayIOException( m_sURL
, ::com::sun::star::ucb::IOErrorCode_CANT_READ
);
194 OSL_ENSURE( eSuccess
== eResult
, "AsyncPickerAction::OnActionDone: what else valid results are there?" );
200 m_pDialog
->UpdateControls( m_pView
->GetViewURL() );
204 // restore the filename
205 m_pView
->SetNoSelection();
206 m_pDialog
->setCurrentFileText( m_sFileName
, true );
209 m_pDialog
->FilterSelect();
213 DBG_ERROR( "AsyncPickerAction::OnActionDone: unknown action!" );
220 //........................................................................
222 //........................................................................