Update ooo320-m1
[ooovba.git] / sw / source / core / docnode / retrievedinputstreamdata.cxx
blob520a5c9c09a536c2a60b3765e7e04749dde7d2dc
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: retrievedinputstreamdata.cxx,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #include "precompiled_sw.hxx"
31 #include <retrievedinputstreamdata.hxx>
32 #include <retrieveinputstreamconsumer.hxx>
33 #include <vcl/svapp.hxx>
35 /** implementation of class <SwRetrievedInputStreamDataManager>
37 OD 2007-01-30 #i73788#
39 SwRetrievedInputStreamDataManager* SwRetrievedInputStreamDataManager::mpManager = 0;
40 SwRetrievedInputStreamDataManager::tDataKey SwRetrievedInputStreamDataManager::mnNextKeyValue = 1;
41 osl::Mutex SwRetrievedInputStreamDataManager::maGetManagerMutex;
43 SwRetrievedInputStreamDataManager& SwRetrievedInputStreamDataManager::GetManager()
45 osl::MutexGuard aGuard(maGetManagerMutex);
47 if ( mpManager == 0 )
49 mpManager = new SwRetrievedInputStreamDataManager();
52 return *mpManager;
55 SwRetrievedInputStreamDataManager::tDataKey SwRetrievedInputStreamDataManager::ReserveData(
56 boost::weak_ptr< SwAsyncRetrieveInputStreamThreadConsumer > pThreadConsumer )
58 osl::MutexGuard aGuard(maMutex);
60 // create empty data container for given thread Consumer
61 tDataKey nDataKey( mnNextKeyValue );
62 tData aNewEntry( pThreadConsumer );
63 maInputStreamData[ nDataKey ] = aNewEntry;
65 // prepare next data key value
66 if ( mnNextKeyValue < SAL_MAX_UINT64 )
68 ++mnNextKeyValue;
70 else
72 mnNextKeyValue = 1;
75 return nDataKey;
78 void SwRetrievedInputStreamDataManager::PushData(
79 const tDataKey nDataKey,
80 com::sun::star::uno::Reference<com::sun::star::io::XInputStream> xInputStream,
81 const sal_Bool bIsStreamReadOnly )
83 osl::MutexGuard aGuard(maMutex);
85 std::map< tDataKey, tData >::iterator aIter = maInputStreamData.find( nDataKey );
87 if ( aIter != maInputStreamData.end() )
89 // Fill data container.
90 (*aIter).second.mxInputStream = xInputStream;
91 (*aIter).second.mbIsStreamReadOnly = bIsStreamReadOnly;
93 // post user event to process the retrieved input stream data
94 if ( GetpApp() )
97 tDataKey* pDataKey = new tDataKey;
98 *pDataKey = nDataKey;
99 GetpApp()->PostUserEvent( LINK( this, SwRetrievedInputStreamDataManager, LinkedInputStreamReady ), pDataKey );
101 else
103 // no application available -> discard data
104 maInputStreamData.erase( aIter );
109 bool SwRetrievedInputStreamDataManager::PopData( const tDataKey nDataKey,
110 tData& rData )
112 osl::MutexGuard aGuard(maMutex);
114 bool bDataProvided( false );
116 std::map< tDataKey, tData >::iterator aIter = maInputStreamData.find( nDataKey );
118 if ( aIter != maInputStreamData.end() )
120 rData.mpThreadConsumer = (*aIter).second.mpThreadConsumer;
121 rData.mxInputStream = (*aIter).second.mxInputStream;
122 rData.mbIsStreamReadOnly = (*aIter).second.mbIsStreamReadOnly;
124 maInputStreamData.erase( aIter );
126 bDataProvided = true;
129 return bDataProvided;
132 /** callback function, which is triggered by input stream data manager on
133 filling of the data container to provide retrieved input stream to the
134 thread Consumer using <Application::PostUserEvent(..)>
136 OD 2007-01-29 #i73788#
137 Note: This method has to be run in the main thread.
139 @author OD
141 IMPL_LINK( SwRetrievedInputStreamDataManager,
142 LinkedInputStreamReady,
143 SwRetrievedInputStreamDataManager::tDataKey*,
144 pDataKey )
146 if ( !pDataKey )
148 return 0;
151 osl::MutexGuard aGuard(maMutex);
153 SwRetrievedInputStreamDataManager& rDataManager =
154 SwRetrievedInputStreamDataManager::GetManager();
155 SwRetrievedInputStreamDataManager::tData aInputStreamData;
156 if ( rDataManager.PopData( *pDataKey, aInputStreamData ) )
158 boost::shared_ptr< SwAsyncRetrieveInputStreamThreadConsumer > pThreadConsumer =
159 aInputStreamData.mpThreadConsumer.lock();
160 if ( pThreadConsumer )
162 pThreadConsumer->ApplyInputStream( aInputStreamData.mxInputStream,
163 aInputStreamData.mbIsStreamReadOnly );
166 delete pDataKey;
168 return 0;