xref: /trunk/main/fpicker/source/office/asyncfilepicker.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_fpicker.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "asyncfilepicker.hxx"
32*cdf0e10cSrcweir #include "iodlg.hxx"
33*cdf0e10cSrcweir #include "svtools/fileview.hxx"
34*cdf0e10cSrcweir #include <tools/debug.hxx>
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir #include <memory>
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir //........................................................................
39*cdf0e10cSrcweir namespace svt
40*cdf0e10cSrcweir {
41*cdf0e10cSrcweir //........................................................................
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir     //====================================================================
44*cdf0e10cSrcweir     //= AsyncPickerAction
45*cdf0e10cSrcweir     //====================================================================
46*cdf0e10cSrcweir     DBG_NAME( AsyncPickerAction )
47*cdf0e10cSrcweir     //--------------------------------------------------------------------
48*cdf0e10cSrcweir     AsyncPickerAction::AsyncPickerAction( SvtFileDialog* _pDialog, SvtFileView* _pView, const Action _eAction )
49*cdf0e10cSrcweir         :m_refCount ( 0        )
50*cdf0e10cSrcweir         ,m_eAction  ( _eAction )
51*cdf0e10cSrcweir         ,m_pView    ( _pView   )
52*cdf0e10cSrcweir         ,m_pDialog  ( _pDialog )
53*cdf0e10cSrcweir         ,m_bRunning ( false    )
54*cdf0e10cSrcweir     {
55*cdf0e10cSrcweir         DBG_CTOR( AsyncPickerAction, NULL );
56*cdf0e10cSrcweir         DBG_ASSERT( m_pDialog, "AsyncPickerAction::AsyncPickerAction: invalid dialog!" );
57*cdf0e10cSrcweir         DBG_ASSERT( m_pView, "AsyncPickerAction::AsyncPickerAction: invalid view!" );
58*cdf0e10cSrcweir     }
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir     //--------------------------------------------------------------------
61*cdf0e10cSrcweir     AsyncPickerAction::~AsyncPickerAction()
62*cdf0e10cSrcweir     {
63*cdf0e10cSrcweir         DBG_DTOR( AsyncPickerAction, NULL );
64*cdf0e10cSrcweir     }
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir     //--------------------------------------------------------------------
67*cdf0e10cSrcweir     oslInterlockedCount SAL_CALL AsyncPickerAction::acquire()
68*cdf0e10cSrcweir     {
69*cdf0e10cSrcweir         return osl_incrementInterlockedCount( &m_refCount );
70*cdf0e10cSrcweir     }
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir     //--------------------------------------------------------------------
73*cdf0e10cSrcweir     oslInterlockedCount SAL_CALL AsyncPickerAction::release()
74*cdf0e10cSrcweir     {
75*cdf0e10cSrcweir         if ( 0 == osl_decrementInterlockedCount( &m_refCount ) )
76*cdf0e10cSrcweir         {
77*cdf0e10cSrcweir             delete this;
78*cdf0e10cSrcweir             return 0;
79*cdf0e10cSrcweir         }
80*cdf0e10cSrcweir         return m_refCount;
81*cdf0e10cSrcweir     }
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir     //--------------------------------------------------------------------
84*cdf0e10cSrcweir     void AsyncPickerAction::cancel()
85*cdf0e10cSrcweir     {
86*cdf0e10cSrcweir         DBG_TESTSOLARMUTEX();
87*cdf0e10cSrcweir             // if this asserts, we'd need to have an own mutex per instance
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir         OSL_ENSURE( m_bRunning, "AsyncPickerAction::cancel: not running" );
90*cdf0e10cSrcweir         if ( m_pView )
91*cdf0e10cSrcweir             m_pView->CancelRunningAsyncAction();
92*cdf0e10cSrcweir     }
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir     //--------------------------------------------------------------------
95*cdf0e10cSrcweir     void AsyncPickerAction::execute(
96*cdf0e10cSrcweir         const String& _rURL,
97*cdf0e10cSrcweir         const String& _rFilter,
98*cdf0e10cSrcweir         sal_Int32 _nMinTimeout,
99*cdf0e10cSrcweir         sal_Int32 _nMaxTimeout,
100*cdf0e10cSrcweir         const OUStringList& rBlackList )
101*cdf0e10cSrcweir     {
102*cdf0e10cSrcweir         DBG_TESTSOLARMUTEX();
103*cdf0e10cSrcweir             // if this asserts, we'd need to have an own mutex per instance
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir         sal_Int32 nMinTimeout = _nMinTimeout;
106*cdf0e10cSrcweir         sal_Int32 nMaxTimeout = _nMaxTimeout;
107*cdf0e10cSrcweir         // normalizations
108*cdf0e10cSrcweir         if ( nMinTimeout < 0 )
109*cdf0e10cSrcweir             // if negative, this is considered as "do it synchronously"
110*cdf0e10cSrcweir             nMinTimeout = 0;
111*cdf0e10cSrcweir         else if ( nMinTimeout < 1000 )
112*cdf0e10cSrcweir             nMinTimeout = 1000;
113*cdf0e10cSrcweir         if ( nMaxTimeout <= nMinTimeout )
114*cdf0e10cSrcweir             nMaxTimeout = nMinTimeout + 30000;
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir         ::std::auto_ptr< FileViewAsyncAction > pActionDescriptor;
117*cdf0e10cSrcweir         if ( nMinTimeout )
118*cdf0e10cSrcweir         {
119*cdf0e10cSrcweir             pActionDescriptor.reset( new FileViewAsyncAction );
120*cdf0e10cSrcweir             pActionDescriptor->nMinTimeout = nMinTimeout;
121*cdf0e10cSrcweir             pActionDescriptor->nMaxTimeout = nMaxTimeout;
122*cdf0e10cSrcweir             pActionDescriptor->aFinishHandler = LINK( this, AsyncPickerAction, OnActionDone );
123*cdf0e10cSrcweir         }
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir         FileViewResult eResult = eFailure;
126*cdf0e10cSrcweir         m_sURL = _rURL;
127*cdf0e10cSrcweir         switch ( m_eAction )
128*cdf0e10cSrcweir         {
129*cdf0e10cSrcweir         case ePrevLevel:
130*cdf0e10cSrcweir             eResult = m_pView->PreviousLevel( pActionDescriptor.get() );
131*cdf0e10cSrcweir             break;
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir         case eOpenURL:
134*cdf0e10cSrcweir             eResult = m_pView->Initialize( _rURL, _rFilter, pActionDescriptor.get(), rBlackList );
135*cdf0e10cSrcweir             break;
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir         case eExecuteFilter:
138*cdf0e10cSrcweir             // preserve the filename (FS: why?)
139*cdf0e10cSrcweir             m_sFileName = m_pDialog->getCurrentFileText();
140*cdf0e10cSrcweir             // execute the new filter
141*cdf0e10cSrcweir             eResult = m_pView->ExecuteFilter( _rFilter, pActionDescriptor.get() );
142*cdf0e10cSrcweir             break;
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir         default:
145*cdf0e10cSrcweir             DBG_ERROR( "AsyncPickerAction::execute: unknown action!" );
146*cdf0e10cSrcweir             break;
147*cdf0e10cSrcweir         }
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir         acquire();
150*cdf0e10cSrcweir         if ( ( eResult == eSuccess ) || ( eResult == eFailure ) )
151*cdf0e10cSrcweir         {
152*cdf0e10cSrcweir             // the handler is only called if the action could not be finished within
153*cdf0e10cSrcweir             // the given minimum time period. In case of success, we need to call it
154*cdf0e10cSrcweir             // explicitly
155*cdf0e10cSrcweir             OnActionDone( reinterpret_cast< void* >( eResult ) );
156*cdf0e10cSrcweir         }
157*cdf0e10cSrcweir         else if ( eResult == eStillRunning )
158*cdf0e10cSrcweir         {
159*cdf0e10cSrcweir             m_bRunning = true;
160*cdf0e10cSrcweir             m_pDialog->onAsyncOperationStarted();
161*cdf0e10cSrcweir         }
162*cdf0e10cSrcweir     }
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir     //--------------------------------------------------------------------
165*cdf0e10cSrcweir     IMPL_LINK( AsyncPickerAction, OnActionDone, void*, pEmptyArg )
166*cdf0e10cSrcweir     {
167*cdf0e10cSrcweir         DBG_TESTSOLARMUTEX();
168*cdf0e10cSrcweir             // if this asserts, we'd need to have an own mutex per instance
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir         FileViewResult eResult = static_cast< FileViewResult >( reinterpret_cast< sal_IntPtr >( pEmptyArg ) );
171*cdf0e10cSrcweir         OSL_ENSURE( eStillRunning != eResult, "AsyncPickerAction::OnActionDone: invalid result!" );
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir         // release once (since we acquired in |execute|), but keep alive until the
174*cdf0e10cSrcweir         // end of the method
175*cdf0e10cSrcweir         ::rtl::Reference< AsyncPickerAction > xKeepAlive( this );
176*cdf0e10cSrcweir         release();
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir         m_pDialog->onAsyncOperationFinished();
179*cdf0e10cSrcweir         m_bRunning = true;
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir         if ( eFailure == eResult )
182*cdf0e10cSrcweir             // TODO: do we need some kind of cleanup here?
183*cdf0e10cSrcweir             return 0L;
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir         if ( eTimeout == eResult )
186*cdf0e10cSrcweir         {
187*cdf0e10cSrcweir             m_pDialog->displayIOException( m_sURL, ::com::sun::star::ucb::IOErrorCode_CANT_READ );
188*cdf0e10cSrcweir             return 0L;
189*cdf0e10cSrcweir         }
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir         OSL_ENSURE( eSuccess == eResult, "AsyncPickerAction::OnActionDone: what else valid results are there?" );
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir         switch ( m_eAction )
194*cdf0e10cSrcweir         {
195*cdf0e10cSrcweir         case ePrevLevel:
196*cdf0e10cSrcweir         case eOpenURL:
197*cdf0e10cSrcweir             m_pDialog->UpdateControls( m_pView->GetViewURL() );
198*cdf0e10cSrcweir             break;
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir         case eExecuteFilter:
201*cdf0e10cSrcweir             // restore the filename
202*cdf0e10cSrcweir             m_pView->SetNoSelection();
203*cdf0e10cSrcweir             m_pDialog->setCurrentFileText( m_sFileName, true );
204*cdf0e10cSrcweir 
205*cdf0e10cSrcweir             // notify listeners
206*cdf0e10cSrcweir             m_pDialog->FilterSelect();
207*cdf0e10cSrcweir             break;
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir         default:
210*cdf0e10cSrcweir             DBG_ERROR( "AsyncPickerAction::OnActionDone: unknown action!" );
211*cdf0e10cSrcweir             break;
212*cdf0e10cSrcweir         }
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir         return 1L;
215*cdf0e10cSrcweir     }
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir //........................................................................
218*cdf0e10cSrcweir } // namespace svt
219*cdf0e10cSrcweir //........................................................................
220*cdf0e10cSrcweir 
221