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 24 : SwRetrievedInputStreamDataManager& SwRetrievedInputStreamDataManager::GetManager()
37 : {
38 24 : return theSwRetrievedInputStreamDataManager::get();
39 : }
40 :
41 8 : SwRetrievedInputStreamDataManager::tDataKey SwRetrievedInputStreamDataManager::ReserveData(
42 : boost::weak_ptr< SwAsyncRetrieveInputStreamThreadConsumer > pThreadConsumer )
43 : {
44 8 : osl::MutexGuard aGuard(maMutex);
45 :
46 : // create empty data container for given thread Consumer
47 8 : tDataKey nDataKey( mnNextKeyValue );
48 16 : tData aNewEntry( pThreadConsumer );
49 8 : maInputStreamData[ nDataKey ] = aNewEntry;
50 :
51 : // prepare next data key value
52 8 : if ( mnNextKeyValue < SAL_MAX_UINT64 )
53 : {
54 8 : ++mnNextKeyValue;
55 : }
56 : else
57 : {
58 0 : mnNextKeyValue = 1;
59 : }
60 :
61 16 : return nDataKey;
62 : }
63 :
64 8 : 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 8 : osl::MutexGuard aGuard(maMutex);
70 :
71 8 : std::map< tDataKey, tData >::iterator aIter = maInputStreamData.find( nDataKey );
72 :
73 8 : if ( aIter != maInputStreamData.end() )
74 : {
75 : // Fill data container.
76 8 : (*aIter).second.mxInputStream = xInputStream;
77 8 : (*aIter).second.mbIsStreamReadOnly = bIsStreamReadOnly;
78 :
79 : // post user event to process the retrieved input stream data
80 8 : if ( GetpApp() )
81 : {
82 :
83 8 : tDataKey* pDataKey = new tDataKey;
84 8 : *pDataKey = nDataKey;
85 8 : Application::PostUserEvent( LINK( this, SwRetrievedInputStreamDataManager, LinkedInputStreamReady ), pDataKey );
86 : }
87 : else
88 : {
89 : // no application available -> discard data
90 0 : maInputStreamData.erase( aIter );
91 : }
92 8 : }
93 8 : }
94 :
95 8 : bool SwRetrievedInputStreamDataManager::PopData( const tDataKey nDataKey,
96 : tData& rData )
97 : {
98 8 : osl::MutexGuard aGuard(maMutex);
99 :
100 8 : bool bDataProvided( false );
101 :
102 8 : std::map< tDataKey, tData >::iterator aIter = maInputStreamData.find( nDataKey );
103 :
104 8 : if ( aIter != maInputStreamData.end() )
105 : {
106 8 : rData.mpThreadConsumer = (*aIter).second.mpThreadConsumer;
107 8 : rData.mxInputStream = (*aIter).second.mxInputStream;
108 8 : rData.mbIsStreamReadOnly = (*aIter).second.mbIsStreamReadOnly;
109 :
110 8 : maInputStreamData.erase( aIter );
111 :
112 8 : bDataProvided = true;
113 : }
114 :
115 8 : 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 16 : IMPL_LINK( SwRetrievedInputStreamDataManager,
126 : LinkedInputStreamReady,
127 : SwRetrievedInputStreamDataManager::tDataKey*,
128 : pDataKey )
129 : {
130 8 : if ( !pDataKey )
131 : {
132 0 : return 0;
133 : }
134 :
135 8 : osl::MutexGuard aGuard(maMutex);
136 :
137 : SwRetrievedInputStreamDataManager& rDataManager =
138 8 : SwRetrievedInputStreamDataManager::GetManager();
139 16 : SwRetrievedInputStreamDataManager::tData aInputStreamData;
140 8 : if ( rDataManager.PopData( *pDataKey, aInputStreamData ) )
141 : {
142 : boost::shared_ptr< SwAsyncRetrieveInputStreamThreadConsumer > pThreadConsumer =
143 8 : aInputStreamData.mpThreadConsumer.lock();
144 8 : if ( pThreadConsumer )
145 : {
146 : pThreadConsumer->ApplyInputStream( aInputStreamData.mxInputStream,
147 8 : aInputStreamData.mbIsStreamReadOnly );
148 8 : }
149 : }
150 8 : delete pDataKey;
151 :
152 16 : return 0;
153 270 : }
154 :
155 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|