update dev300-m58
[ooovba.git] / dtrans / source / win32 / dtobj / DTransHelper.cxx
blob41c357c376690b8c43064ee539e0833043a48a60
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: DTransHelper.cxx,v $
10 * $Revision: 1.14 $
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 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_dtrans.hxx"
34 //------------------------------------------------------------------------
35 // includes
36 //------------------------------------------------------------------------
37 #include <rtl/ustring.h>
38 #include <osl/diagnose.h>
39 #include "DTransHelper.hxx"
41 //------------------------------------------------------------------------
42 // implementation
43 //------------------------------------------------------------------------
45 CStgTransferHelper::CStgTransferHelper( sal_Bool bAutoInit,
46 HGLOBAL hGlob,
47 sal_Bool bDelStgOnRelease ) :
48 m_lpStream( NULL ),
49 m_bDelStgOnRelease( bDelStgOnRelease )
51 if ( bAutoInit )
52 init( hGlob, m_bDelStgOnRelease );
55 //------------------------------------------------------------------------
56 // dtor
57 //------------------------------------------------------------------------
60 CStgTransferHelper::~CStgTransferHelper( )
62 if ( m_lpStream )
63 m_lpStream->Release( );
66 //------------------------------------------------------------------------
67 // TransferData into the
68 //------------------------------------------------------------------------
70 void SAL_CALL CStgTransferHelper::write( const void* lpData, ULONG cb, ULONG* cbWritten )
72 HRESULT hr = E_FAIL;
74 if ( m_lpStream )
75 hr = m_lpStream->Write( lpData, cb, cbWritten );
77 if ( FAILED( hr ) )
78 throw CStgTransferException( hr );
80 #if OSL_DEBUG_LEVEL > 0
81 HGLOBAL hGlob;
82 hr = GetHGlobalFromStream( m_lpStream, &hGlob );
83 OSL_ASSERT( SUCCEEDED( hr ) );
85 /*DWORD dwSize =*/ GlobalSize( hGlob );
86 /*LPVOID lpdbgData =*/ GlobalLock( hGlob );
87 GlobalUnlock( hGlob );
88 #endif
91 //------------------------------------------------------------------------
92 // read
93 //------------------------------------------------------------------------
95 void SAL_CALL CStgTransferHelper::read( LPVOID pv, ULONG cb, ULONG* pcbRead )
97 HRESULT hr = E_FAIL;
99 if ( m_lpStream )
100 hr = m_lpStream->Read( pv, cb , pcbRead );
102 if ( FAILED( hr ) )
103 throw CStgTransferException( hr );
106 //------------------------------------------------------------------------
107 // GetHGlobal
108 //------------------------------------------------------------------------
110 HGLOBAL SAL_CALL CStgTransferHelper::getHGlobal( ) const
112 OSL_ASSERT( m_lpStream );
114 HGLOBAL hGlob = NULL;
116 if ( m_lpStream )
118 HRESULT hr = GetHGlobalFromStream( m_lpStream, &hGlob );
119 if ( FAILED( hr ) )
120 hGlob = NULL;
123 return hGlob;
126 //------------------------------------------------------------------------
127 // getIStream
128 //------------------------------------------------------------------------
130 void SAL_CALL CStgTransferHelper::getIStream( LPSTREAM* ppStream )
132 OSL_ASSERT( ppStream );
133 *ppStream = m_lpStream;
134 if ( *ppStream )
135 static_cast< LPUNKNOWN >( *ppStream )->AddRef( );
138 //------------------------------------------------------------------------
139 // Init
140 //------------------------------------------------------------------------
142 void SAL_CALL CStgTransferHelper::init( SIZE_T newSize,
143 sal_uInt32 uiFlags,
144 sal_Bool bDelStgOnRelease )
146 cleanup( );
148 m_bDelStgOnRelease = bDelStgOnRelease;
150 HGLOBAL hGlob = GlobalAlloc( uiFlags, newSize );
151 if ( NULL == hGlob )
152 throw CStgTransferException( STG_E_MEDIUMFULL );
154 HRESULT hr = CreateStreamOnHGlobal( hGlob, m_bDelStgOnRelease, &m_lpStream );
155 if ( FAILED( hr ) )
157 GlobalFree( hGlob );
158 m_lpStream = NULL;
159 throw CStgTransferException( hr );
162 #if OSL_DEBUG_LEVEL > 0
163 STATSTG statstg;
164 hr = m_lpStream->Stat( &statstg, STATFLAG_DEFAULT );
165 OSL_ASSERT( SUCCEEDED( hr ) );
166 #endif
169 //------------------------------------------------------------------------
170 // Init
171 //------------------------------------------------------------------------
173 void SAL_CALL CStgTransferHelper::init( HGLOBAL hGlob,
174 sal_Bool bDelStgOnRelease )
176 cleanup( );
178 m_bDelStgOnRelease = bDelStgOnRelease;
180 HRESULT hr = CreateStreamOnHGlobal( hGlob, m_bDelStgOnRelease, &m_lpStream );
181 if ( FAILED( hr ) )
182 throw CStgTransferException( hr );
185 //------------------------------------------------------------------------
186 // free the global memory and invalidate the stream pointer
187 //------------------------------------------------------------------------
189 void SAL_CALL CStgTransferHelper::cleanup( )
191 if ( m_lpStream && !m_bDelStgOnRelease )
193 HGLOBAL hGlob;
194 GetHGlobalFromStream( m_lpStream, &hGlob );
195 GlobalFree( hGlob );
198 if ( m_lpStream )
200 m_lpStream->Release( );
201 m_lpStream = NULL;
205 //------------------------------------------------------------------------
206 // return the size of memory we point to
207 //------------------------------------------------------------------------
209 sal_uInt32 SAL_CALL CStgTransferHelper::memSize( CLIPFORMAT cf ) const
211 DWORD dwSize = 0;
213 if ( NULL != m_lpStream )
215 HGLOBAL hGlob;
216 GetHGlobalFromStream( m_lpStream, &hGlob );
218 if ( CF_TEXT == cf || RegisterClipboardFormat( "HTML Format" ) == cf )
220 sal_Char* pText = static_cast< sal_Char* >( GlobalLock( hGlob ) );
221 if ( pText )
223 dwSize = strlen(pText) + 1; // strlen + trailing '\0'
224 GlobalUnlock( hGlob );
227 else if ( CF_UNICODETEXT == cf )
229 sal_Unicode* pText = static_cast< sal_Unicode* >( GlobalLock( hGlob ) );
230 if ( pText )
232 dwSize = rtl_ustr_getLength( pText ) * sizeof( sal_Unicode );
233 GlobalUnlock( hGlob );
236 else
237 dwSize = GlobalSize( hGlob );
240 return dwSize;