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