update dev300-m58
[ooovba.git] / sot / source / sdstor / stgole.cxx
blobd04049cddda91918c868cc3227b1df0c94af9f47
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: stgole.cxx,v $
10 * $Revision: 1.11 $
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_sot.hxx"
34 #include "rtl/string.h"
35 #include "rtl/string.h"
36 #include "stgole.hxx"
37 #include "storinfo.hxx" // Read/WriteClipboardFormat()
39 #include <tools/debug.hxx>
40 #if defined(_MSC_VER) && (_MSC_VER>=1400)
41 #pragma warning(disable: 4342)
42 #endif
43 ///////////////////////// class StgInternalStream ////////////////////////
45 StgInternalStream::StgInternalStream
46 ( BaseStorage& rStg, const String& rName, BOOL bWr )
48 bIsWritable = TRUE;
49 USHORT nMode = bWr
50 ? STREAM_WRITE | STREAM_SHARE_DENYALL
51 : STREAM_READ | STREAM_SHARE_DENYWRITE | STREAM_NOCREATE;
52 pStrm = rStg.OpenStream( rName, nMode );
54 // set the error code right here in the stream
55 SetError( rStg.GetError() );
56 SetBufferSize( 1024 );
59 StgInternalStream::~StgInternalStream()
61 delete pStrm;
64 ULONG StgInternalStream::GetData( void* pData, ULONG nSize )
66 if( pStrm )
68 nSize = pStrm->Read( pData, nSize );
69 SetError( pStrm->GetError() );
70 return nSize;
72 else
73 return 0;
76 ULONG StgInternalStream::PutData( const void* pData, ULONG nSize )
78 if( pStrm )
80 nSize = pStrm->Write( pData, nSize );
81 SetError( pStrm->GetError() );
82 return nSize;
84 else
85 return 0;
88 ULONG StgInternalStream::SeekPos( ULONG nPos )
90 return pStrm ? pStrm->Seek( nPos ) : 0;
93 void StgInternalStream::FlushData()
95 if( pStrm )
97 pStrm->Flush();
98 SetError( pStrm->GetError() );
102 void StgInternalStream::Commit()
104 Flush();
105 pStrm->Commit();
108 ///////////////////////// class StgCompObjStream /////////////////////////
110 StgCompObjStream::StgCompObjStream( BaseStorage& rStg, BOOL bWr )
111 : StgInternalStream( rStg, String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "\1CompObj" ) ), bWr )
113 memset( &aClsId, 0, sizeof( ClsId ) );
114 nCbFormat = 0;
117 BOOL StgCompObjStream::Load()
119 memset( &aClsId, 0, sizeof( ClsId ) );
120 nCbFormat = 0;
121 aUserName.Erase();
122 if( GetError() != SVSTREAM_OK )
123 return FALSE;
124 Seek( 8L ); // skip the first part
125 INT32 nMarker = 0;
126 *this >> nMarker;
127 if( nMarker == -1L )
129 *this >> aClsId;
130 INT32 nLen1 = 0;
131 *this >> nLen1;
132 // higher bits are ignored
133 nLen1 &= 0xFFFF;
134 sal_Char* p = new sal_Char[ (USHORT) nLen1 ];
135 if( Read( p, nLen1 ) == (ULONG) nLen1 )
137 aUserName = nLen1 ? String( p, gsl_getSystemTextEncoding() ) : String();
138 /* // Now we can read the CB format
139 INT32 nLen2 = 0;
140 *this >> nLen2;
141 if( nLen2 > 0 )
143 // get a string name
144 if( nLen2 > nLen1 )
145 delete p, p = new char[ nLen2 ];
146 if( Read( p, nLen2 ) == (ULONG) nLen2 && nLen2 )
147 nCbFormat = Exchange::RegisterFormatName( String( p ) );
148 else
149 SetError( SVSTREAM_GENERALERROR );
151 else if( nLen2 == -1L )
152 // Windows clipboard format
153 *this >> nCbFormat;
154 else
155 // unknown identifier
156 SetError( SVSTREAM_GENERALERROR );
158 nCbFormat = ReadClipboardFormat( *this );
160 else
161 SetError( SVSTREAM_GENERALERROR );
162 delete [] p;
164 return BOOL( GetError() == SVSTREAM_OK );
167 BOOL StgCompObjStream::Store()
169 if( GetError() != SVSTREAM_OK )
170 return FALSE;
171 Seek( 0L );
172 ByteString aAsciiUserName( aUserName, RTL_TEXTENCODING_ASCII_US );
173 *this << (INT16) 1 // Version?
174 << (INT16) -2 // 0xFFFE = Byte Order Indicator
175 << (INT32) 0x0A03 // Windows 3.10
176 << (INT32) -1L
177 << aClsId // Class ID
178 << (INT32) (aAsciiUserName.Len() + 1)
179 << (const char *)aAsciiUserName.GetBuffer()
180 << (UINT8) 0; // string terminator
181 /* // determine the clipboard format string
182 String aCbFmt;
183 if( nCbFormat > FORMAT_GDIMETAFILE )
184 aCbFmt = Exchange::GetFormatName( nCbFormat );
185 if( aCbFmt.Len() )
186 *this << (INT32) aCbFmt.Len() + 1
187 << (const char*) aCbFmt
188 << (UINT8) 0;
189 else if( nCbFormat )
190 *this << (INT32) -1 // for Windows
191 << (INT32) nCbFormat;
192 else
193 *this << (INT32) 0; // no clipboard format
195 WriteClipboardFormat( *this, nCbFormat );
196 *this << (INT32) 0; // terminator
197 Commit();
198 return BOOL( GetError() == SVSTREAM_OK );
201 /////////////////////////// class StgOleStream ///////////////////////////
203 StgOleStream::StgOleStream( BaseStorage& rStg, BOOL bWr )
204 : StgInternalStream( rStg, String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "\1Ole" ) ), bWr )
206 nFlags = 0;
209 BOOL StgOleStream::Load()
211 nFlags = 0;
212 if( GetError() != SVSTREAM_OK )
213 return FALSE;
214 INT32 version = 0;
215 Seek( 0L );
216 *this >> version >> nFlags;
217 return BOOL( GetError() == SVSTREAM_OK );
220 BOOL StgOleStream::Store()
222 if( GetError() != SVSTREAM_OK )
223 return FALSE;
224 Seek( 0L );
225 *this << (INT32) 0x02000001 // OLE version, format
226 << (INT32) nFlags // Object flags
227 << (INT32) 0 // Update Options
228 << (INT32) 0 // reserved
229 << (INT32) 0; // Moniker 1
230 Commit();
231 return BOOL( GetError() == SVSTREAM_OK );