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 : #include <retrievedinputstreamdata.hxx>
21 : #include <retrieveinputstreamconsumer.hxx>
22 : #include <vcl/svapp.hxx>
23 :
24 : // #i73788#
25 :
26 : SwRetrievedInputStreamDataManager::tDataKey SwRetrievedInputStreamDataManager::mnNextKeyValue = 1;
27 :
28 : namespace
29 : {
30 : class theSwRetrievedInputStreamDataManager :
31 : public rtl::Static< SwRetrievedInputStreamDataManager, theSwRetrievedInputStreamDataManager>
32 : {
33 : };
34 : }
35 :
36 12 : SwRetrievedInputStreamDataManager& SwRetrievedInputStreamDataManager::GetManager()
37 : {
38 12 : return theSwRetrievedInputStreamDataManager::get();
39 : }
40 :
41 4 : SwRetrievedInputStreamDataManager::tDataKey SwRetrievedInputStreamDataManager::ReserveData(
42 : boost::weak_ptr< SwAsyncRetrieveInputStreamThreadConsumer > pThreadConsumer )
43 : {
44 4 : osl::MutexGuard aGuard(maMutex);
45 :
46 : // create empty data container for given thread Consumer
47 4 : tDataKey nDataKey( mnNextKeyValue );
48 8 : tData aNewEntry( pThreadConsumer );
49 4 : maInputStreamData[ nDataKey ] = aNewEntry;
50 :
51 : // prepare next data key value
52 4 : if ( mnNextKeyValue < SAL_MAX_UINT64 )
53 : {
54 4 : ++mnNextKeyValue;
55 : }
56 : else
57 : {
58 0 : mnNextKeyValue = 1;
59 : }
60 :
61 8 : return nDataKey;
62 : }
63 :
64 4 : void SwRetrievedInputStreamDataManager::PushData(
65 : const tDataKey nDataKey,
66 : com::sun::star::uno::Reference<com::sun::star::io::XInputStream> xInputStream,
67 : const bool bIsStreamReadOnly )
68 : {
69 4 : osl::MutexGuard aGuard(maMutex);
70 :
71 4 : std::map< tDataKey, tData >::iterator aIter = maInputStreamData.find( nDataKey );
72 :
73 4 : if ( aIter != maInputStreamData.end() )
74 : {
75 : // Fill data container.
76 4 : (*aIter).second.mxInputStream = xInputStream;
77 4 : (*aIter).second.mbIsStreamReadOnly = bIsStreamReadOnly;
78 :
79 : // post user event to process the retrieved input stream data
80 4 : if ( GetpApp() )
81 : {
82 :
83 4 : tDataKey* pDataKey = new tDataKey;
84 4 : *pDataKey = nDataKey;
85 4 : Application::PostUserEvent( LINK( this, SwRetrievedInputStreamDataManager, LinkedInputStreamReady ), pDataKey );
86 : }
87 : else
88 : {
89 : // no application available -> discard data
90 0 : maInputStreamData.erase( aIter );
91 : }
92 4 : }
93 4 : }
94 :
95 4 : bool SwRetrievedInputStreamDataManager::PopData( const tDataKey nDataKey,
96 : tData& rData )
97 : {
98 4 : osl::MutexGuard aGuard(maMutex);
99 :
100 4 : bool bDataProvided( false );
101 :
102 4 : std::map< tDataKey, tData >::iterator aIter = maInputStreamData.find( nDataKey );
103 :
104 4 : if ( aIter != maInputStreamData.end() )
105 : {
106 4 : rData.mpThreadConsumer = (*aIter).second.mpThreadConsumer;
107 4 : rData.mxInputStream = (*aIter).second.mxInputStream;
108 4 : rData.mbIsStreamReadOnly = (*aIter).second.mbIsStreamReadOnly;
109 :
110 4 : maInputStreamData.erase( aIter );
111 :
112 4 : bDataProvided = true;
113 : }
114 :
115 4 : return bDataProvided;
116 : }
117 :
118 : /** callback function, which is triggered by input stream data manager on
119 : filling of the data container to provide retrieved input stream to the
120 : thread Consumer using <Application::PostUserEvent(..)>
121 :
122 : #i73788#
123 : Note: This method has to be run in the main thread.
124 : */
125 8 : IMPL_LINK( SwRetrievedInputStreamDataManager,
126 : LinkedInputStreamReady,
127 : SwRetrievedInputStreamDataManager::tDataKey*,
128 : pDataKey )
129 : {
130 4 : if ( !pDataKey )
131 : {
132 0 : return 0;
133 : }
134 :
135 4 : osl::MutexGuard aGuard(maMutex);
136 :
137 : SwRetrievedInputStreamDataManager& rDataManager =
138 4 : SwRetrievedInputStreamDataManager::GetManager();
139 8 : SwRetrievedInputStreamDataManager::tData aInputStreamData;
140 4 : if ( rDataManager.PopData( *pDataKey, aInputStreamData ) )
141 : {
142 : boost::shared_ptr< SwAsyncRetrieveInputStreamThreadConsumer > pThreadConsumer =
143 4 : aInputStreamData.mpThreadConsumer.lock();
144 4 : if ( pThreadConsumer )
145 : {
146 : pThreadConsumer->ApplyInputStream( aInputStreamData.mxInputStream,
147 0 : aInputStreamData.mbIsStreamReadOnly );
148 4 : }
149 : }
150 4 : delete pDataKey;
151 :
152 8 : return 0;
153 : }
154 :
155 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|