Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : : #include <retrievedinputstreamdata.hxx>
29 : : #include <retrieveinputstreamconsumer.hxx>
30 : : #include <vcl/svapp.hxx>
31 : :
32 : : /** implementation of class <SwRetrievedInputStreamDataManager>
33 : :
34 : : #i73788#
35 : : */
36 : : SwRetrievedInputStreamDataManager::tDataKey SwRetrievedInputStreamDataManager::mnNextKeyValue = 1;
37 : :
38 : : namespace
39 : : {
40 : : class theSwRetrievedInputStreamDataManager :
41 : : public rtl::Static< SwRetrievedInputStreamDataManager, theSwRetrievedInputStreamDataManager>
42 : : {
43 : : };
44 : : }
45 : :
46 : 24 : SwRetrievedInputStreamDataManager& SwRetrievedInputStreamDataManager::GetManager()
47 : : {
48 : 24 : return theSwRetrievedInputStreamDataManager::get();
49 : : }
50 : :
51 : 8 : SwRetrievedInputStreamDataManager::tDataKey SwRetrievedInputStreamDataManager::ReserveData(
52 : : boost::weak_ptr< SwAsyncRetrieveInputStreamThreadConsumer > pThreadConsumer )
53 : : {
54 [ + - ]: 8 : osl::MutexGuard aGuard(maMutex);
55 : :
56 : : // create empty data container for given thread Consumer
57 : 8 : tDataKey nDataKey( mnNextKeyValue );
58 [ + - ][ + - ]: 8 : tData aNewEntry( pThreadConsumer );
[ + - ]
59 [ + - ][ + - ]: 8 : maInputStreamData[ nDataKey ] = aNewEntry;
60 : :
61 : : // prepare next data key value
62 [ + - ]: 8 : if ( mnNextKeyValue < SAL_MAX_UINT64 )
63 : : {
64 : 8 : ++mnNextKeyValue;
65 : : }
66 : : else
67 : : {
68 : 0 : mnNextKeyValue = 1;
69 : : }
70 : :
71 [ + - ][ + - ]: 8 : return nDataKey;
72 : : }
73 : :
74 : 8 : void SwRetrievedInputStreamDataManager::PushData(
75 : : const tDataKey nDataKey,
76 : : com::sun::star::uno::Reference<com::sun::star::io::XInputStream> xInputStream,
77 : : const sal_Bool bIsStreamReadOnly )
78 : : {
79 [ + - ]: 8 : osl::MutexGuard aGuard(maMutex);
80 : :
81 [ + - ]: 8 : std::map< tDataKey, tData >::iterator aIter = maInputStreamData.find( nDataKey );
82 : :
83 [ + - ]: 8 : if ( aIter != maInputStreamData.end() )
84 : : {
85 : : // Fill data container.
86 [ + - ]: 8 : (*aIter).second.mxInputStream = xInputStream;
87 : 8 : (*aIter).second.mbIsStreamReadOnly = bIsStreamReadOnly;
88 : :
89 : : // post user event to process the retrieved input stream data
90 [ + - ][ + - ]: 8 : if ( GetpApp() )
91 : : {
92 : :
93 [ + - ]: 8 : tDataKey* pDataKey = new tDataKey;
94 : 8 : *pDataKey = nDataKey;
95 [ + - ][ + - ]: 8 : GetpApp()->PostUserEvent( LINK( this, SwRetrievedInputStreamDataManager, LinkedInputStreamReady ), pDataKey );
[ + - ]
96 : : }
97 : : else
98 : : {
99 : : // no application available -> discard data
100 [ # # ]: 0 : maInputStreamData.erase( aIter );
101 : : }
102 [ + - ]: 8 : }
103 : 8 : }
104 : :
105 : 8 : bool SwRetrievedInputStreamDataManager::PopData( const tDataKey nDataKey,
106 : : tData& rData )
107 : : {
108 [ + - ]: 8 : osl::MutexGuard aGuard(maMutex);
109 : :
110 : 8 : bool bDataProvided( false );
111 : :
112 [ + - ]: 8 : std::map< tDataKey, tData >::iterator aIter = maInputStreamData.find( nDataKey );
113 : :
114 [ + - ]: 8 : if ( aIter != maInputStreamData.end() )
115 : : {
116 [ + - ]: 8 : rData.mpThreadConsumer = (*aIter).second.mpThreadConsumer;
117 [ + - ]: 8 : rData.mxInputStream = (*aIter).second.mxInputStream;
118 : 8 : rData.mbIsStreamReadOnly = (*aIter).second.mbIsStreamReadOnly;
119 : :
120 [ + - ]: 8 : maInputStreamData.erase( aIter );
121 : :
122 : 8 : bDataProvided = true;
123 : : }
124 : :
125 [ + - ]: 8 : return bDataProvided;
126 : : }
127 : :
128 : : /** callback function, which is triggered by input stream data manager on
129 : : filling of the data container to provide retrieved input stream to the
130 : : thread Consumer using <Application::PostUserEvent(..)>
131 : :
132 : : #i73788#
133 : : Note: This method has to be run in the main thread.
134 : :
135 : : @author OD
136 : : */
137 : 8 : IMPL_LINK( SwRetrievedInputStreamDataManager,
138 : : LinkedInputStreamReady,
139 : : SwRetrievedInputStreamDataManager::tDataKey*,
140 : : pDataKey )
141 : : {
142 [ - + ]: 8 : if ( !pDataKey )
143 : : {
144 : 0 : return 0;
145 : : }
146 : :
147 [ + - ]: 8 : osl::MutexGuard aGuard(maMutex);
148 : :
149 : : SwRetrievedInputStreamDataManager& rDataManager =
150 [ + - ]: 8 : SwRetrievedInputStreamDataManager::GetManager();
151 [ + - ]: 8 : SwRetrievedInputStreamDataManager::tData aInputStreamData;
152 [ + - ][ + - ]: 8 : if ( rDataManager.PopData( *pDataKey, aInputStreamData ) )
153 : : {
154 : : boost::shared_ptr< SwAsyncRetrieveInputStreamThreadConsumer > pThreadConsumer =
155 [ + - ]: 8 : aInputStreamData.mpThreadConsumer.lock();
156 [ + - ]: 8 : if ( pThreadConsumer )
157 : : {
158 : : pThreadConsumer->ApplyInputStream( aInputStreamData.mxInputStream,
159 [ + - ]: 8 : aInputStreamData.mbIsStreamReadOnly );
160 [ + - ]: 8 : }
161 : : }
162 : 8 : delete pDataKey;
163 : :
164 [ + - ][ + - ]: 8 : return 0;
165 : : }
166 : :
167 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|