1*48123e16SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*48123e16SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*48123e16SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*48123e16SAndrew Rist  * distributed with this work for additional information
6*48123e16SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*48123e16SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*48123e16SAndrew Rist  * "License"); you may not use this file except in compliance
9*48123e16SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*48123e16SAndrew Rist  *
11*48123e16SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*48123e16SAndrew Rist  *
13*48123e16SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*48123e16SAndrew Rist  * software distributed under the License is distributed on an
15*48123e16SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*48123e16SAndrew Rist  * KIND, either express or implied.  See the License for the
17*48123e16SAndrew Rist  * specific language governing permissions and limitations
18*48123e16SAndrew Rist  * under the License.
19*48123e16SAndrew Rist  *
20*48123e16SAndrew Rist  *************************************************************/
21*48123e16SAndrew Rist 
22*48123e16SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_dtrans.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir //------------------------------------------------------------------------
28cdf0e10cSrcweir // includes
29cdf0e10cSrcweir //------------------------------------------------------------------------
30cdf0e10cSrcweir #include <rtl/ustring.h>
31cdf0e10cSrcweir #include <osl/diagnose.h>
32cdf0e10cSrcweir #include "DTransHelper.hxx"
33cdf0e10cSrcweir 
34cdf0e10cSrcweir //------------------------------------------------------------------------
35cdf0e10cSrcweir // implementation
36cdf0e10cSrcweir //------------------------------------------------------------------------
37cdf0e10cSrcweir 
CStgTransferHelper(sal_Bool bAutoInit,HGLOBAL hGlob,sal_Bool bDelStgOnRelease)38cdf0e10cSrcweir CStgTransferHelper::CStgTransferHelper( sal_Bool bAutoInit,
39cdf0e10cSrcweir 										HGLOBAL hGlob,
40cdf0e10cSrcweir 										sal_Bool bDelStgOnRelease ) :
41cdf0e10cSrcweir 	m_lpStream( NULL ),
42cdf0e10cSrcweir 	m_bDelStgOnRelease( bDelStgOnRelease )
43cdf0e10cSrcweir {
44cdf0e10cSrcweir 	if ( bAutoInit )
45cdf0e10cSrcweir 		init( hGlob, m_bDelStgOnRelease );
46cdf0e10cSrcweir }
47cdf0e10cSrcweir 
48cdf0e10cSrcweir //------------------------------------------------------------------------
49cdf0e10cSrcweir // dtor
50cdf0e10cSrcweir //------------------------------------------------------------------------
51cdf0e10cSrcweir 
52cdf0e10cSrcweir 
~CStgTransferHelper()53cdf0e10cSrcweir CStgTransferHelper::~CStgTransferHelper( )
54cdf0e10cSrcweir {
55cdf0e10cSrcweir 	if ( m_lpStream )
56cdf0e10cSrcweir 		m_lpStream->Release( );
57cdf0e10cSrcweir }
58cdf0e10cSrcweir 
59cdf0e10cSrcweir //------------------------------------------------------------------------
60cdf0e10cSrcweir // TransferData into the
61cdf0e10cSrcweir //------------------------------------------------------------------------
62cdf0e10cSrcweir 
write(const void * lpData,ULONG cb,ULONG * cbWritten)63cdf0e10cSrcweir void SAL_CALL CStgTransferHelper::write( const void* lpData, ULONG cb, ULONG* cbWritten )
64cdf0e10cSrcweir {
65cdf0e10cSrcweir 	HRESULT hr = E_FAIL;
66cdf0e10cSrcweir 
67cdf0e10cSrcweir 	if ( m_lpStream )
68cdf0e10cSrcweir 		hr = m_lpStream->Write( lpData, cb, cbWritten );
69cdf0e10cSrcweir 
70cdf0e10cSrcweir 	if ( FAILED( hr ) )
71cdf0e10cSrcweir 		throw CStgTransferException( hr );
72cdf0e10cSrcweir 
73cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0
74cdf0e10cSrcweir 	HGLOBAL hGlob;
75cdf0e10cSrcweir 	hr = GetHGlobalFromStream( m_lpStream, &hGlob );
76cdf0e10cSrcweir 	OSL_ASSERT( SUCCEEDED( hr ) );
77cdf0e10cSrcweir 
78cdf0e10cSrcweir 	/*DWORD dwSize =*/ GlobalSize( hGlob );
79cdf0e10cSrcweir 	/*LPVOID lpdbgData =*/ GlobalLock( hGlob );
80cdf0e10cSrcweir 	GlobalUnlock( hGlob );
81cdf0e10cSrcweir #endif
82cdf0e10cSrcweir }
83cdf0e10cSrcweir 
84cdf0e10cSrcweir //------------------------------------------------------------------------
85cdf0e10cSrcweir // read
86cdf0e10cSrcweir //------------------------------------------------------------------------
87cdf0e10cSrcweir 
read(LPVOID pv,ULONG cb,ULONG * pcbRead)88cdf0e10cSrcweir void SAL_CALL CStgTransferHelper::read( LPVOID pv, ULONG cb, ULONG* pcbRead )
89cdf0e10cSrcweir {
90cdf0e10cSrcweir 	HRESULT hr = E_FAIL;
91cdf0e10cSrcweir 
92cdf0e10cSrcweir 	if ( m_lpStream )
93cdf0e10cSrcweir 		hr = m_lpStream->Read( pv, cb , pcbRead );
94cdf0e10cSrcweir 
95cdf0e10cSrcweir 	if ( FAILED( hr ) )
96cdf0e10cSrcweir 		throw CStgTransferException( hr );
97cdf0e10cSrcweir }
98cdf0e10cSrcweir 
99cdf0e10cSrcweir //------------------------------------------------------------------------
100cdf0e10cSrcweir // GetHGlobal
101cdf0e10cSrcweir //------------------------------------------------------------------------
102cdf0e10cSrcweir 
getHGlobal() const103cdf0e10cSrcweir HGLOBAL SAL_CALL CStgTransferHelper::getHGlobal( ) const
104cdf0e10cSrcweir {
105cdf0e10cSrcweir 	OSL_ASSERT( m_lpStream );
106cdf0e10cSrcweir 
107cdf0e10cSrcweir 	HGLOBAL hGlob = NULL;
108cdf0e10cSrcweir 
109cdf0e10cSrcweir 	if ( m_lpStream )
110cdf0e10cSrcweir 	{
111cdf0e10cSrcweir 		HRESULT hr = GetHGlobalFromStream( m_lpStream, &hGlob );
112cdf0e10cSrcweir 		if ( FAILED( hr ) )
113cdf0e10cSrcweir 			hGlob = NULL;
114cdf0e10cSrcweir 	}
115cdf0e10cSrcweir 
116cdf0e10cSrcweir 	return hGlob;
117cdf0e10cSrcweir }
118cdf0e10cSrcweir 
119cdf0e10cSrcweir //------------------------------------------------------------------------
120cdf0e10cSrcweir // getIStream
121cdf0e10cSrcweir //------------------------------------------------------------------------
122cdf0e10cSrcweir 
getIStream(LPSTREAM * ppStream)123cdf0e10cSrcweir void SAL_CALL CStgTransferHelper::getIStream( LPSTREAM* ppStream )
124cdf0e10cSrcweir {
125cdf0e10cSrcweir 	OSL_ASSERT( ppStream );
126cdf0e10cSrcweir 	*ppStream = m_lpStream;
127cdf0e10cSrcweir 	if ( *ppStream )
128cdf0e10cSrcweir 		static_cast< LPUNKNOWN >( *ppStream )->AddRef( );
129cdf0e10cSrcweir }
130cdf0e10cSrcweir 
131cdf0e10cSrcweir //------------------------------------------------------------------------
132cdf0e10cSrcweir // Init
133cdf0e10cSrcweir //------------------------------------------------------------------------
134cdf0e10cSrcweir 
init(SIZE_T newSize,sal_uInt32 uiFlags,sal_Bool bDelStgOnRelease)135cdf0e10cSrcweir void SAL_CALL CStgTransferHelper::init( SIZE_T newSize,
136cdf0e10cSrcweir 									    sal_uInt32 uiFlags,
137cdf0e10cSrcweir 										sal_Bool bDelStgOnRelease )
138cdf0e10cSrcweir {
139cdf0e10cSrcweir 	cleanup( );
140cdf0e10cSrcweir 
141cdf0e10cSrcweir 	m_bDelStgOnRelease      = bDelStgOnRelease;
142cdf0e10cSrcweir 
143cdf0e10cSrcweir 	HGLOBAL hGlob = GlobalAlloc( uiFlags, newSize );
144cdf0e10cSrcweir 	if ( NULL == hGlob )
145cdf0e10cSrcweir 		throw CStgTransferException( STG_E_MEDIUMFULL );
146cdf0e10cSrcweir 
147cdf0e10cSrcweir 	HRESULT hr = CreateStreamOnHGlobal( hGlob, m_bDelStgOnRelease, &m_lpStream );
148cdf0e10cSrcweir 	if ( FAILED( hr ) )
149cdf0e10cSrcweir 	{
150cdf0e10cSrcweir 		GlobalFree( hGlob );
151cdf0e10cSrcweir 		m_lpStream = NULL;
152cdf0e10cSrcweir 		throw CStgTransferException( hr );
153cdf0e10cSrcweir 	}
154cdf0e10cSrcweir 
155cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0
156cdf0e10cSrcweir 	STATSTG statstg;
157cdf0e10cSrcweir 	hr = m_lpStream->Stat( &statstg, STATFLAG_DEFAULT );
158cdf0e10cSrcweir 	OSL_ASSERT( SUCCEEDED( hr ) );
159cdf0e10cSrcweir #endif
160cdf0e10cSrcweir }
161cdf0e10cSrcweir 
162cdf0e10cSrcweir //------------------------------------------------------------------------
163cdf0e10cSrcweir // Init
164cdf0e10cSrcweir //------------------------------------------------------------------------
165cdf0e10cSrcweir 
init(HGLOBAL hGlob,sal_Bool bDelStgOnRelease)166cdf0e10cSrcweir void SAL_CALL CStgTransferHelper::init( HGLOBAL hGlob,
167cdf0e10cSrcweir 						 			    sal_Bool bDelStgOnRelease )
168cdf0e10cSrcweir {
169cdf0e10cSrcweir 	cleanup( );
170cdf0e10cSrcweir 
171cdf0e10cSrcweir 	m_bDelStgOnRelease      = bDelStgOnRelease;
172cdf0e10cSrcweir 
173cdf0e10cSrcweir 	HRESULT hr = CreateStreamOnHGlobal( hGlob, m_bDelStgOnRelease, &m_lpStream );
174cdf0e10cSrcweir 	if ( FAILED( hr ) )
175cdf0e10cSrcweir 		throw CStgTransferException( hr );
176cdf0e10cSrcweir }
177cdf0e10cSrcweir 
178cdf0e10cSrcweir //------------------------------------------------------------------------
179cdf0e10cSrcweir // free the global memory and invalidate the stream pointer
180cdf0e10cSrcweir //------------------------------------------------------------------------
181cdf0e10cSrcweir 
cleanup()182cdf0e10cSrcweir void SAL_CALL CStgTransferHelper::cleanup( )
183cdf0e10cSrcweir {
184cdf0e10cSrcweir 	if ( m_lpStream && !m_bDelStgOnRelease )
185cdf0e10cSrcweir 	{
186cdf0e10cSrcweir 		HGLOBAL hGlob;
187cdf0e10cSrcweir 		GetHGlobalFromStream( m_lpStream, &hGlob );
188cdf0e10cSrcweir 		GlobalFree( hGlob );
189cdf0e10cSrcweir 	}
190cdf0e10cSrcweir 
191cdf0e10cSrcweir 	if ( m_lpStream )
192cdf0e10cSrcweir 	{
193cdf0e10cSrcweir 		m_lpStream->Release( );
194cdf0e10cSrcweir 		m_lpStream = NULL;
195cdf0e10cSrcweir 	}
196cdf0e10cSrcweir }
197cdf0e10cSrcweir 
198cdf0e10cSrcweir //------------------------------------------------------------------------
199cdf0e10cSrcweir // return the size of memory we point to
200cdf0e10cSrcweir //------------------------------------------------------------------------
201cdf0e10cSrcweir 
memSize(CLIPFORMAT cf) const202cdf0e10cSrcweir sal_uInt32 SAL_CALL CStgTransferHelper::memSize( CLIPFORMAT cf ) const
203cdf0e10cSrcweir {
204cdf0e10cSrcweir 	DWORD dwSize = 0;
205cdf0e10cSrcweir 
206cdf0e10cSrcweir 	if ( NULL != m_lpStream )
207cdf0e10cSrcweir 	{
208cdf0e10cSrcweir 		HGLOBAL hGlob;
209cdf0e10cSrcweir 		GetHGlobalFromStream( m_lpStream, &hGlob );
210cdf0e10cSrcweir 
211cdf0e10cSrcweir 		if ( CF_TEXT == cf || RegisterClipboardFormat( "HTML Format" ) == cf )
212cdf0e10cSrcweir 		{
213cdf0e10cSrcweir 			sal_Char* pText = static_cast< sal_Char* >( GlobalLock( hGlob ) );
214cdf0e10cSrcweir 			if ( pText )
215cdf0e10cSrcweir 			{
216cdf0e10cSrcweir 				dwSize = strlen(pText) + 1; // strlen + trailing '\0'
217cdf0e10cSrcweir 				GlobalUnlock( hGlob );
218cdf0e10cSrcweir 			}
219cdf0e10cSrcweir 		}
220cdf0e10cSrcweir 		else if ( CF_UNICODETEXT == cf )
221cdf0e10cSrcweir 		{
222cdf0e10cSrcweir 			sal_Unicode* pText = static_cast< sal_Unicode* >( GlobalLock( hGlob ) );
223cdf0e10cSrcweir 			if ( pText )
224cdf0e10cSrcweir 			{
225cdf0e10cSrcweir 				dwSize = rtl_ustr_getLength( pText ) * sizeof( sal_Unicode );
226cdf0e10cSrcweir 				GlobalUnlock( hGlob );
227cdf0e10cSrcweir 			}
228cdf0e10cSrcweir 		}
229cdf0e10cSrcweir 		else
230cdf0e10cSrcweir 			dwSize = GlobalSize( hGlob );
231cdf0e10cSrcweir 	}
232cdf0e10cSrcweir 
233cdf0e10cSrcweir 	return dwSize;
234cdf0e10cSrcweir }
235cdf0e10cSrcweir 
236