Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / fpicker / source / office / asyncfilepicker.cxx
blob4dc64af3d4ebd64a1eeaf403106eda06ad770725
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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"
23 #include "iodlg.hxx"
24 #include <tools/debug.hxx>
25 #include <osl/diagnose.h>
27 #include <memory>
30 namespace svt
32 AsyncPickerAction::AsyncPickerAction( SvtFileDialog_Base* _pDialog, SvtFileView* _pView, const Action _eAction )
33 :m_eAction ( _eAction )
34 ,m_pView ( _pView )
35 ,m_pDialog ( _pDialog )
36 ,m_bRunning ( false )
38 assert( m_pDialog && "AsyncPickerAction::AsyncPickerAction: invalid dialog!" );
39 assert( m_pView && "AsyncPickerAction::AsyncPickerAction: invalid view!" );
43 AsyncPickerAction::~AsyncPickerAction()
48 void AsyncPickerAction::cancel()
50 DBG_TESTSOLARMUTEX();
51 // if this asserts, we'd need to have an own mutex per instance
53 OSL_ENSURE( m_bRunning, "AsyncPickerAction::cancel: not running" );
54 if ( m_pView )
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 )
66 DBG_TESTSOLARMUTEX();
67 // if this asserts, we'd need to have an own mutex per instance
69 sal_Int32 nMinTimeout = _nMinTimeout;
70 sal_Int32 nMaxTimeout = _nMaxTimeout;
71 // normalizations
72 if ( nMinTimeout < 0 )
73 // if negative, this is considered as "do it synchronously"
74 nMinTimeout = 0;
75 else if ( nMinTimeout < 1000 )
76 nMinTimeout = 1000;
77 if ( nMaxTimeout <= nMinTimeout )
78 nMaxTimeout = nMinTimeout + 30000;
80 std::unique_ptr< FileViewAsyncAction > pActionDescriptor;
81 if ( nMinTimeout )
83 pActionDescriptor.reset( new FileViewAsyncAction );
84 pActionDescriptor->nMinTimeout = nMinTimeout;
85 pActionDescriptor->nMaxTimeout = nMaxTimeout;
86 pActionDescriptor->aFinishHandler = LINK( this, AsyncPickerAction, OnActionDone );
89 FileViewResult eResult = eFailure;
90 m_sURL = _rURL;
91 switch ( m_eAction )
93 case ePrevLevel:
94 eResult = m_pView->PreviousLevel( pActionDescriptor.get() );
95 break;
97 case eOpenURL:
98 eResult = m_pView->Initialize( _rURL, _rFilter, pActionDescriptor.get(), rBlackList );
99 break;
101 case eExecuteFilter:
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() );
106 break;
108 default:
109 OSL_FAIL( "AsyncPickerAction::execute: unknown action!" );
110 break;
113 acquire();
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
118 // explicitly
119 OnActionDone( reinterpret_cast< void* >( eResult ) );
121 else if ( eResult == eStillRunning )
123 m_bRunning = true;
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
138 // end of the method
139 ::rtl::Reference< AsyncPickerAction > xKeepAlive( this );
140 release();
142 m_pDialog->onAsyncOperationFinished();
143 m_bRunning = true;
145 if ( eFailure == eResult )
146 // TODO: do we need some kind of cleanup here?
147 return;
149 if ( eTimeout == eResult )
151 SvtFileDialog::displayIOException( m_sURL, css::ucb::IOErrorCode_CANT_READ );
152 return;
155 OSL_ENSURE( eSuccess == eResult, "AsyncPickerAction::OnActionDone: what else valid results are there?" );
157 switch ( m_eAction )
159 case ePrevLevel:
160 case eOpenURL:
161 m_pDialog->UpdateControls( m_pView->GetViewURL() );
162 break;
164 case eExecuteFilter:
165 // restore the filename
166 m_pView->SetNoSelection();
167 m_pDialog->setCurrentFileText( m_sFileName, true );
169 // notify listeners
170 m_pDialog->FilterSelect();
171 break;
173 default:
174 OSL_FAIL( "AsyncPickerAction::OnActionDone: unknown action!" );
175 break;
180 } // namespace svt
183 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */