bump product version to 4.1.6.2
[LibreOffice.git] / dtrans / source / win32 / dtobj / DTransHelper.cxx
blobee7352c83f0f460d878122601c0a0024c880b13f
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <rtl/ustring.h>
21 #include <osl/diagnose.h>
22 #include "DTransHelper.hxx"
24 //------------------------------------------------------------------------
25 // implementation
26 //------------------------------------------------------------------------
28 CStgTransferHelper::CStgTransferHelper( sal_Bool bAutoInit,
29 HGLOBAL hGlob,
30 sal_Bool bDelStgOnRelease ) :
31 m_lpStream( NULL ),
32 m_bDelStgOnRelease( bDelStgOnRelease )
34 if ( bAutoInit )
35 init( hGlob, m_bDelStgOnRelease );
38 //------------------------------------------------------------------------
39 // dtor
40 //------------------------------------------------------------------------
43 CStgTransferHelper::~CStgTransferHelper( )
45 if ( m_lpStream )
46 m_lpStream->Release( );
49 //------------------------------------------------------------------------
50 // TransferData into the
51 //------------------------------------------------------------------------
53 void SAL_CALL CStgTransferHelper::write( const void* lpData, ULONG cb, ULONG* cbWritten )
55 HRESULT hr = E_FAIL;
57 if ( m_lpStream )
58 hr = m_lpStream->Write( lpData, cb, cbWritten );
60 if ( FAILED( hr ) )
61 throw CStgTransferException( hr );
63 #if OSL_DEBUG_LEVEL > 0
64 HGLOBAL hGlob;
65 hr = GetHGlobalFromStream( m_lpStream, &hGlob );
66 OSL_ASSERT( SUCCEEDED( hr ) );
68 /*DWORD dwSize =*/ GlobalSize( hGlob );
69 /*LPVOID lpdbgData =*/ GlobalLock( hGlob );
70 GlobalUnlock( hGlob );
71 #endif
74 //------------------------------------------------------------------------
75 // read
76 //------------------------------------------------------------------------
78 void SAL_CALL CStgTransferHelper::read( LPVOID pv, ULONG cb, ULONG* pcbRead )
80 HRESULT hr = E_FAIL;
82 if ( m_lpStream )
83 hr = m_lpStream->Read( pv, cb , pcbRead );
85 if ( FAILED( hr ) )
86 throw CStgTransferException( hr );
89 //------------------------------------------------------------------------
90 // GetHGlobal
91 //------------------------------------------------------------------------
93 HGLOBAL SAL_CALL CStgTransferHelper::getHGlobal( ) const
95 OSL_ASSERT( m_lpStream );
97 HGLOBAL hGlob = NULL;
99 if ( m_lpStream )
101 HRESULT hr = GetHGlobalFromStream( m_lpStream, &hGlob );
102 if ( FAILED( hr ) )
103 hGlob = NULL;
106 return hGlob;
109 //------------------------------------------------------------------------
110 // getIStream
111 //------------------------------------------------------------------------
113 void SAL_CALL CStgTransferHelper::getIStream( LPSTREAM* ppStream )
115 OSL_ASSERT( ppStream );
116 *ppStream = m_lpStream;
117 if ( *ppStream )
118 static_cast< LPUNKNOWN >( *ppStream )->AddRef( );
121 //------------------------------------------------------------------------
122 // Init
123 //------------------------------------------------------------------------
125 void SAL_CALL CStgTransferHelper::init( SIZE_T newSize,
126 sal_uInt32 uiFlags,
127 sal_Bool bDelStgOnRelease )
129 cleanup( );
131 m_bDelStgOnRelease = bDelStgOnRelease;
133 HGLOBAL hGlob = GlobalAlloc( uiFlags, newSize );
134 if ( NULL == hGlob )
135 throw CStgTransferException( STG_E_MEDIUMFULL );
137 HRESULT hr = CreateStreamOnHGlobal( hGlob, m_bDelStgOnRelease, &m_lpStream );
138 if ( FAILED( hr ) )
140 GlobalFree( hGlob );
141 m_lpStream = NULL;
142 throw CStgTransferException( hr );
145 #if OSL_DEBUG_LEVEL > 0
146 STATSTG statstg;
147 hr = m_lpStream->Stat( &statstg, STATFLAG_DEFAULT );
148 OSL_ASSERT( SUCCEEDED( hr ) );
149 #endif
152 //------------------------------------------------------------------------
153 // Init
154 //------------------------------------------------------------------------
156 void SAL_CALL CStgTransferHelper::init( HGLOBAL hGlob,
157 sal_Bool bDelStgOnRelease )
159 cleanup( );
161 m_bDelStgOnRelease = bDelStgOnRelease;
163 HRESULT hr = CreateStreamOnHGlobal( hGlob, m_bDelStgOnRelease, &m_lpStream );
164 if ( FAILED( hr ) )
165 throw CStgTransferException( hr );
168 //------------------------------------------------------------------------
169 // free the global memory and invalidate the stream pointer
170 //------------------------------------------------------------------------
172 void SAL_CALL CStgTransferHelper::cleanup( )
174 if ( m_lpStream && !m_bDelStgOnRelease )
176 HGLOBAL hGlob;
177 GetHGlobalFromStream( m_lpStream, &hGlob );
178 GlobalFree( hGlob );
181 if ( m_lpStream )
183 m_lpStream->Release( );
184 m_lpStream = NULL;
188 //------------------------------------------------------------------------
189 // return the size of memory we point to
190 //------------------------------------------------------------------------
192 sal_uInt32 SAL_CALL CStgTransferHelper::memSize( CLIPFORMAT cf ) const
194 DWORD dwSize = 0;
196 if ( NULL != m_lpStream )
198 HGLOBAL hGlob;
199 GetHGlobalFromStream( m_lpStream, &hGlob );
201 if ( CF_TEXT == cf || RegisterClipboardFormat( "HTML Format" ) == cf )
203 sal_Char* pText = static_cast< sal_Char* >( GlobalLock( hGlob ) );
204 if ( pText )
206 dwSize = strlen(pText) + 1; // strlen + trailing '\0'
207 GlobalUnlock( hGlob );
210 else if ( CF_UNICODETEXT == cf )
212 sal_Unicode* pText = static_cast< sal_Unicode* >( GlobalLock( hGlob ) );
213 if ( pText )
215 dwSize = rtl_ustr_getLength( pText ) * sizeof( sal_Unicode );
216 GlobalUnlock( hGlob );
219 else
220 dwSize = GlobalSize( hGlob );
223 return dwSize;
226 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */