1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: DTransHelper.cxx,v $
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 //------------------------------------------------------------------------
36 //------------------------------------------------------------------------
37 #include <rtl/ustring.h>
38 #include <osl/diagnose.h>
39 #include "DTransHelper.hxx"
41 //------------------------------------------------------------------------
43 //------------------------------------------------------------------------
45 CStgTransferHelper::CStgTransferHelper( sal_Bool bAutoInit
,
47 sal_Bool bDelStgOnRelease
) :
49 m_bDelStgOnRelease( bDelStgOnRelease
)
52 init( hGlob
, m_bDelStgOnRelease
);
55 //------------------------------------------------------------------------
57 //------------------------------------------------------------------------
60 CStgTransferHelper::~CStgTransferHelper( )
63 m_lpStream
->Release( );
66 //------------------------------------------------------------------------
67 // TransferData into the
68 //------------------------------------------------------------------------
70 void SAL_CALL
CStgTransferHelper::write( const void* lpData
, ULONG cb
, ULONG
* cbWritten
)
75 hr
= m_lpStream
->Write( lpData
, cb
, cbWritten
);
78 throw CStgTransferException( hr
);
80 #if OSL_DEBUG_LEVEL > 0
82 hr
= GetHGlobalFromStream( m_lpStream
, &hGlob
);
83 OSL_ASSERT( SUCCEEDED( hr
) );
85 /*DWORD dwSize =*/ GlobalSize( hGlob
);
86 /*LPVOID lpdbgData =*/ GlobalLock( hGlob
);
87 GlobalUnlock( hGlob
);
91 //------------------------------------------------------------------------
93 //------------------------------------------------------------------------
95 void SAL_CALL
CStgTransferHelper::read( LPVOID pv
, ULONG cb
, ULONG
* pcbRead
)
100 hr
= m_lpStream
->Read( pv
, cb
, pcbRead
);
103 throw CStgTransferException( hr
);
106 //------------------------------------------------------------------------
108 //------------------------------------------------------------------------
110 HGLOBAL SAL_CALL
CStgTransferHelper::getHGlobal( ) const
112 OSL_ASSERT( m_lpStream
);
114 HGLOBAL hGlob
= NULL
;
118 HRESULT hr
= GetHGlobalFromStream( m_lpStream
, &hGlob
);
126 //------------------------------------------------------------------------
128 //------------------------------------------------------------------------
130 void SAL_CALL
CStgTransferHelper::getIStream( LPSTREAM
* ppStream
)
132 OSL_ASSERT( ppStream
);
133 *ppStream
= m_lpStream
;
135 static_cast< LPUNKNOWN
>( *ppStream
)->AddRef( );
138 //------------------------------------------------------------------------
140 //------------------------------------------------------------------------
142 void SAL_CALL
CStgTransferHelper::init( SIZE_T newSize
,
144 sal_Bool bDelStgOnRelease
)
148 m_bDelStgOnRelease
= bDelStgOnRelease
;
150 HGLOBAL hGlob
= GlobalAlloc( uiFlags
, newSize
);
152 throw CStgTransferException( STG_E_MEDIUMFULL
);
154 HRESULT hr
= CreateStreamOnHGlobal( hGlob
, m_bDelStgOnRelease
, &m_lpStream
);
159 throw CStgTransferException( hr
);
162 #if OSL_DEBUG_LEVEL > 0
164 hr
= m_lpStream
->Stat( &statstg
, STATFLAG_DEFAULT
);
165 OSL_ASSERT( SUCCEEDED( hr
) );
169 //------------------------------------------------------------------------
171 //------------------------------------------------------------------------
173 void SAL_CALL
CStgTransferHelper::init( HGLOBAL hGlob
,
174 sal_Bool bDelStgOnRelease
)
178 m_bDelStgOnRelease
= bDelStgOnRelease
;
180 HRESULT hr
= CreateStreamOnHGlobal( hGlob
, m_bDelStgOnRelease
, &m_lpStream
);
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
)
194 GetHGlobalFromStream( m_lpStream
, &hGlob
);
200 m_lpStream
->Release( );
205 //------------------------------------------------------------------------
206 // return the size of memory we point to
207 //------------------------------------------------------------------------
209 sal_uInt32 SAL_CALL
CStgTransferHelper::memSize( CLIPFORMAT cf
) const
213 if ( NULL
!= m_lpStream
)
216 GetHGlobalFromStream( m_lpStream
, &hGlob
);
218 if ( CF_TEXT
== cf
|| RegisterClipboardFormat( "HTML Format" ) == cf
)
220 sal_Char
* pText
= static_cast< sal_Char
* >( GlobalLock( hGlob
) );
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
) );
232 dwSize
= rtl_ustr_getLength( pText
) * sizeof( sal_Unicode
);
233 GlobalUnlock( hGlob
);
237 dwSize
= GlobalSize( hGlob
);