1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include "asyncfilepicker.hxx"
22 #include "fileview.hxx"
24 #include <tools/debug.hxx>
25 #include <osl/diagnose.h>
32 AsyncPickerAction::AsyncPickerAction( SvtFileDialog_Base
* _pDialog
, SvtFileView
* _pView
, const Action _eAction
)
33 :m_eAction ( _eAction
)
35 ,m_pDialog ( _pDialog
)
38 assert( m_pDialog
&& "AsyncPickerAction::AsyncPickerAction: invalid dialog!" );
39 assert( m_pView
&& "AsyncPickerAction::AsyncPickerAction: invalid view!" );
43 AsyncPickerAction::~AsyncPickerAction()
48 void AsyncPickerAction::cancel()
51 // if this asserts, we'd need to have an own mutex per instance
53 OSL_ENSURE( m_bRunning
, "AsyncPickerAction::cancel: not running" );
55 m_pView
->CancelRunningAsyncAction();
59 void AsyncPickerAction::execute(
60 const OUString
& _rURL
,
61 const OUString
& _rFilter
,
62 sal_Int32 _nMinTimeout
,
63 sal_Int32 _nMaxTimeout
,
64 const css::uno::Sequence
< OUString
>& rBlackList
)
67 // if this asserts, we'd need to have an own mutex per instance
69 sal_Int32 nMinTimeout
= _nMinTimeout
;
70 sal_Int32 nMaxTimeout
= _nMaxTimeout
;
72 if ( nMinTimeout
< 0 )
73 // if negative, this is considered as "do it synchronously"
75 else if ( nMinTimeout
< 1000 )
77 if ( nMaxTimeout
<= nMinTimeout
)
78 nMaxTimeout
= nMinTimeout
+ 30000;
80 std::unique_ptr
< FileViewAsyncAction
> pActionDescriptor
;
83 pActionDescriptor
.reset( new FileViewAsyncAction
);
84 pActionDescriptor
->nMinTimeout
= nMinTimeout
;
85 pActionDescriptor
->nMaxTimeout
= nMaxTimeout
;
86 pActionDescriptor
->aFinishHandler
= LINK( this, AsyncPickerAction
, OnActionDone
);
89 FileViewResult eResult
= eFailure
;
94 eResult
= m_pView
->PreviousLevel( pActionDescriptor
.get() );
98 eResult
= m_pView
->Initialize( _rURL
, _rFilter
, pActionDescriptor
.get(), rBlackList
);
102 // preserve the filename (FS: why?)
103 m_sFileName
= m_pDialog
->getCurrentFileText();
104 // execute the new filter
105 eResult
= m_pView
->ExecuteFilter( _rFilter
, pActionDescriptor
.get() );
109 OSL_FAIL( "AsyncPickerAction::execute: unknown action!" );
114 if ( ( eResult
== eSuccess
) || ( eResult
== eFailure
) )
116 // the handler is only called if the action could not be finished within
117 // the given minimum time period. In case of success, we need to call it
119 OnActionDone( reinterpret_cast< void* >( eResult
) );
121 else if ( eResult
== eStillRunning
)
124 m_pDialog
->onAsyncOperationStarted();
129 IMPL_LINK( AsyncPickerAction
, OnActionDone
, void*, pEmptyArg
, void )
131 DBG_TESTSOLARMUTEX();
132 // if this asserts, we'd need to have an own mutex per instance
134 FileViewResult eResult
= static_cast< FileViewResult
>( reinterpret_cast< sal_IntPtr
>( pEmptyArg
) );
135 OSL_ENSURE( eStillRunning
!= eResult
, "AsyncPickerAction::OnActionDone: invalid result!" );
137 // release once (since we acquired in |execute|), but keep alive until the
139 ::rtl::Reference
< AsyncPickerAction
> xKeepAlive( this );
142 m_pDialog
->onAsyncOperationFinished();
145 if ( eFailure
== eResult
)
146 // TODO: do we need some kind of cleanup here?
149 if ( eTimeout
== eResult
)
151 SvtFileDialog::displayIOException( m_sURL
, css::ucb::IOErrorCode_CANT_READ
);
155 OSL_ENSURE( eSuccess
== eResult
, "AsyncPickerAction::OnActionDone: what else valid results are there?" );
161 m_pDialog
->UpdateControls( m_pView
->GetViewURL() );
165 // restore the filename
166 m_pView
->SetNoSelection();
167 m_pDialog
->setCurrentFileText( m_sFileName
, true );
170 m_pDialog
->FilterSelect();
174 OSL_FAIL( "AsyncPickerAction::OnActionDone: unknown action!" );
183 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */