LibreOffice Module fpicker (master) 1
asyncfilepicker.cxx
Go to the documentation of this file.
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 "asyncfilepicker.hxx"
22#include "fileview.hxx"
23#include "iodlg.hxx"
24#include <tools/debug.hxx>
25#include <osl/diagnose.h>
26
27#include <memory>
28
29
30namespace svt
31{
33 :m_eAction ( _eAction )
34 ,m_pView ( _pView )
35 ,m_pDialog ( _pDialog )
36 ,m_bRunning ( false )
37 {
38 assert( m_pDialog && "AsyncPickerAction::AsyncPickerAction: invalid dialog!" );
39 assert( m_pView && "AsyncPickerAction::AsyncPickerAction: invalid view!" );
40 }
41
42
44 {
45 }
46
47
49 {
51 // if this asserts, we'd need to have an own mutex per instance
52
53 OSL_ENSURE( m_bRunning, "AsyncPickerAction::cancel: not running" );
54 if ( m_pView )
56 }
57
58
60 const OUString& _rURL,
61 const OUString& _rFilter,
62 sal_Int32 _nMinTimeout,
63 sal_Int32 _nMaxTimeout,
64 const css::uno::Sequence< OUString >& rDenyList )
65 {
67 // if this asserts, we'd need to have an own mutex per instance
68
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;
79
80 std::unique_ptr< FileViewAsyncAction > pActionDescriptor;
81 if ( nMinTimeout )
82 {
83 pActionDescriptor.reset( new FileViewAsyncAction );
84 pActionDescriptor->nMinTimeout = nMinTimeout;
85 pActionDescriptor->nMaxTimeout = nMaxTimeout;
86 pActionDescriptor->aFinishHandler = LINK( this, AsyncPickerAction, OnActionDone );
87 }
88
89 FileViewResult eResult = eFailure;
90 m_sURL = _rURL;
91 switch ( m_eAction )
92 {
93 case ePrevLevel:
94 eResult = m_pView->PreviousLevel( pActionDescriptor.get() );
95 break;
96
97 case eOpenURL:
98 eResult = m_pView->Initialize( _rURL, _rFilter, pActionDescriptor.get(), rDenyList );
99 break;
100
101 case eExecuteFilter:
102 // preserve the filename (FS: why?)
104 // execute the new filter
105 eResult = m_pView->ExecuteFilter( _rFilter, pActionDescriptor.get() );
106 break;
107
108 default:
109 OSL_FAIL( "AsyncPickerAction::execute: unknown action!" );
110 break;
111 }
112
113 acquire();
114 if ( ( eResult == eSuccess ) || ( eResult == eFailure ) )
115 {
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 ) );
120 }
121 else if ( eResult == eStillRunning )
122 {
123 m_bRunning = true;
125 }
126 }
127
128
129 IMPL_LINK( AsyncPickerAction, OnActionDone, void*, pEmptyArg, void )
130 {
132 // if this asserts, we'd need to have an own mutex per instance
133
134 FileViewResult eResult = static_cast< FileViewResult >( reinterpret_cast< sal_IntPtr >( pEmptyArg ) );
135 OSL_ENSURE( eStillRunning != eResult, "AsyncPickerAction::OnActionDone: invalid result!" );
136
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();
141
142 m_pDialog->onAsyncOperationFinished();
143 m_bRunning = true;
144
145 if ( eFailure == eResult )
146 // TODO: do we need some kind of cleanup here?
147 return;
148
149 if ( eTimeout == eResult )
150 {
151 SvtFileDialog::displayIOException( m_sURL, css::ucb::IOErrorCode_CANT_READ );
152 return;
153 }
154
155 OSL_ENSURE( eSuccess == eResult, "AsyncPickerAction::OnActionDone: what else valid results are there?" );
156
157 switch ( m_eAction )
158 {
159 case ePrevLevel:
160 case eOpenURL:
161 m_pDialog->UpdateControls( m_pView->GetViewURL() );
162 break;
163
164 case eExecuteFilter:
165 // restore the filename
166 m_pView->SetNoSelection();
167 m_pDialog->setCurrentFileText( m_sFileName, true );
168
169 // notify listeners
170 m_pDialog->FilterSelect();
171 break;
172
173 default:
174 OSL_FAIL( "AsyncPickerAction::OnActionDone: unknown action!" );
175 break;
176 }
177 }
178
179
180} // namespace svt
181
182
183/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SectionViewAction m_eAction
virtual void onAsyncOperationStarted()=0
virtual OUString getCurrentFileText() const =0
static void displayIOException(const OUString &_rURL, css::ucb::IOErrorCode _eCode)
Definition: iodlg.cxx:1379
FileViewResult PreviousLevel(const FileViewAsyncAction *pAsyncDescriptor)
initializes the view with the parent folder of the current folder
Definition: fileview.cxx:881
void CancelRunningAsyncAction()
cancels a running async action (if any)
Definition: fileview.cxx:976
FileViewResult Initialize(const OUString &rFolderURL, const OUString &rFilter, const FileViewAsyncAction *pAsyncDescriptor, const css::uno::Sequence< OUString > &rDenyList)
initialize the view with the content of a folder given by URL, and apply an immediate filter
Definition: fileview.cxx:935
FileViewResult ExecuteFilter(const OUString &rFilter, const FileViewAsyncAction *pAsyncDescriptor)
reads the current content of the current folder again, and applies the given filter to it
Definition: fileview.cxx:966
void cancel()
cancels the running action
virtual ~AsyncPickerAction() override
SvtFileDialog_Base * m_pDialog
void execute(const OUString &_rURL, const OUString &_rFilter, sal_Int32 _nMinTimeout, sal_Int32 _nMaxTimeout, const css::uno::Sequence< OUString > &rDenyList)
executes the action
AsyncPickerAction(SvtFileDialog_Base *_pDialog, SvtFileView *_pView, const Action _eAction)
OUString m_sURL
#define DBG_TESTSOLARMUTEX()
FileViewResult
the result of an action in the FileView
Definition: fileview.hxx:36
@ eSuccess
Definition: fileview.hxx:37
@ eTimeout
Definition: fileview.hxx:39
@ eFailure
Definition: fileview.hxx:38
@ eStillRunning
Definition: fileview.hxx:40
IMPL_LINK(ControlBase, KeyInputHdl, const KeyEvent &, rKEvt, bool)
describes parameters for doing an action on the FileView asynchronously
Definition: fileview.hxx:51