Line data Source code
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 "iodlg.hxx"
23 : #include "svtools/fileview.hxx"
24 : #include <tools/debug.hxx>
25 :
26 : #include <memory>
27 :
28 : //........................................................................
29 : namespace svt
30 : {
31 : //........................................................................
32 :
33 : //====================================================================
34 : //= AsyncPickerAction
35 : //====================================================================
36 : DBG_NAME( AsyncPickerAction )
37 : //--------------------------------------------------------------------
38 0 : AsyncPickerAction::AsyncPickerAction( SvtFileDialog* _pDialog, SvtFileView* _pView, const Action _eAction )
39 : :m_refCount ( 0 )
40 : ,m_eAction ( _eAction )
41 : ,m_pView ( _pView )
42 : ,m_pDialog ( _pDialog )
43 0 : ,m_bRunning ( false )
44 : {
45 : DBG_CTOR( AsyncPickerAction, NULL );
46 : DBG_ASSERT( m_pDialog, "AsyncPickerAction::AsyncPickerAction: invalid dialog!" );
47 : DBG_ASSERT( m_pView, "AsyncPickerAction::AsyncPickerAction: invalid view!" );
48 0 : }
49 :
50 : //--------------------------------------------------------------------
51 0 : AsyncPickerAction::~AsyncPickerAction()
52 : {
53 : DBG_DTOR( AsyncPickerAction, NULL );
54 0 : }
55 :
56 : //--------------------------------------------------------------------
57 0 : oslInterlockedCount SAL_CALL AsyncPickerAction::acquire()
58 : {
59 0 : return osl_atomic_increment( &m_refCount );
60 : }
61 :
62 : //--------------------------------------------------------------------
63 0 : oslInterlockedCount SAL_CALL AsyncPickerAction::release()
64 : {
65 0 : if ( 0 == osl_atomic_decrement( &m_refCount ) )
66 : {
67 0 : delete this;
68 0 : return 0;
69 : }
70 0 : return m_refCount;
71 : }
72 :
73 : //--------------------------------------------------------------------
74 0 : void AsyncPickerAction::cancel()
75 : {
76 : DBG_TESTSOLARMUTEX();
77 : // if this asserts, we'd need to have an own mutex per instance
78 :
79 : OSL_ENSURE( m_bRunning, "AsyncPickerAction::cancel: not running" );
80 0 : if ( m_pView )
81 0 : m_pView->CancelRunningAsyncAction();
82 0 : }
83 :
84 : //--------------------------------------------------------------------
85 0 : void AsyncPickerAction::execute(
86 : const String& _rURL,
87 : const String& _rFilter,
88 : sal_Int32 _nMinTimeout,
89 : sal_Int32 _nMaxTimeout,
90 : const OUStringList& rBlackList )
91 : {
92 : DBG_TESTSOLARMUTEX();
93 : // if this asserts, we'd need to have an own mutex per instance
94 :
95 0 : sal_Int32 nMinTimeout = _nMinTimeout;
96 0 : sal_Int32 nMaxTimeout = _nMaxTimeout;
97 : // normalizations
98 0 : if ( nMinTimeout < 0 )
99 : // if negative, this is considered as "do it synchronously"
100 0 : nMinTimeout = 0;
101 0 : else if ( nMinTimeout < 1000 )
102 0 : nMinTimeout = 1000;
103 0 : if ( nMaxTimeout <= nMinTimeout )
104 0 : nMaxTimeout = nMinTimeout + 30000;
105 :
106 0 : ::std::auto_ptr< FileViewAsyncAction > pActionDescriptor;
107 0 : if ( nMinTimeout )
108 : {
109 0 : pActionDescriptor.reset( new FileViewAsyncAction );
110 0 : pActionDescriptor->nMinTimeout = nMinTimeout;
111 0 : pActionDescriptor->nMaxTimeout = nMaxTimeout;
112 0 : pActionDescriptor->aFinishHandler = LINK( this, AsyncPickerAction, OnActionDone );
113 : }
114 :
115 0 : FileViewResult eResult = eFailure;
116 0 : m_sURL = _rURL;
117 0 : switch ( m_eAction )
118 : {
119 : case ePrevLevel:
120 0 : eResult = m_pView->PreviousLevel( pActionDescriptor.get() );
121 0 : break;
122 :
123 : case eOpenURL:
124 0 : eResult = m_pView->Initialize( _rURL, _rFilter, pActionDescriptor.get(), rBlackList );
125 0 : break;
126 :
127 : case eExecuteFilter:
128 : // preserve the filename (FS: why?)
129 0 : m_sFileName = m_pDialog->getCurrentFileText();
130 : // execute the new filter
131 0 : eResult = m_pView->ExecuteFilter( _rFilter, pActionDescriptor.get() );
132 0 : break;
133 :
134 : default:
135 : OSL_FAIL( "AsyncPickerAction::execute: unknown action!" );
136 0 : break;
137 : }
138 :
139 0 : acquire();
140 0 : if ( ( eResult == eSuccess ) || ( eResult == eFailure ) )
141 : {
142 : // the handler is only called if the action could not be finished within
143 : // the given minimum time period. In case of success, we need to call it
144 : // explicitly
145 0 : OnActionDone( reinterpret_cast< void* >( eResult ) );
146 : }
147 0 : else if ( eResult == eStillRunning )
148 : {
149 0 : m_bRunning = true;
150 0 : m_pDialog->onAsyncOperationStarted();
151 0 : }
152 0 : }
153 :
154 : //--------------------------------------------------------------------
155 0 : IMPL_LINK( AsyncPickerAction, OnActionDone, void*, pEmptyArg )
156 : {
157 : DBG_TESTSOLARMUTEX();
158 : // if this asserts, we'd need to have an own mutex per instance
159 :
160 0 : FileViewResult eResult = static_cast< FileViewResult >( reinterpret_cast< sal_IntPtr >( pEmptyArg ) );
161 : OSL_ENSURE( eStillRunning != eResult, "AsyncPickerAction::OnActionDone: invalid result!" );
162 :
163 : // release once (since we acquired in |execute|), but keep alive until the
164 : // end of the method
165 0 : ::rtl::Reference< AsyncPickerAction > xKeepAlive( this );
166 0 : release();
167 :
168 0 : m_pDialog->onAsyncOperationFinished();
169 0 : m_bRunning = true;
170 :
171 0 : if ( eFailure == eResult )
172 : // TODO: do we need some kind of cleanup here?
173 0 : return 0L;
174 :
175 0 : if ( eTimeout == eResult )
176 : {
177 0 : m_pDialog->displayIOException( m_sURL, ::com::sun::star::ucb::IOErrorCode_CANT_READ );
178 0 : return 0L;
179 : }
180 :
181 : OSL_ENSURE( eSuccess == eResult, "AsyncPickerAction::OnActionDone: what else valid results are there?" );
182 :
183 0 : switch ( m_eAction )
184 : {
185 : case ePrevLevel:
186 : case eOpenURL:
187 0 : m_pDialog->UpdateControls( m_pView->GetViewURL() );
188 0 : break;
189 :
190 : case eExecuteFilter:
191 : // restore the filename
192 0 : m_pView->SetNoSelection();
193 0 : m_pDialog->setCurrentFileText( m_sFileName, true );
194 :
195 : // notify listeners
196 0 : m_pDialog->FilterSelect();
197 0 : break;
198 :
199 : default:
200 : OSL_FAIL( "AsyncPickerAction::OnActionDone: unknown action!" );
201 0 : break;
202 : }
203 :
204 0 : return 1L;
205 : }
206 :
207 : //........................................................................
208 : } // namespace svt
209 : //........................................................................
210 :
211 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|