xref: /trunk/main/svl/source/items/lckbitem.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_svl.hxx"
30 
31 #define _LCKBITEM_CXX
32 #include <svl/lckbitem.hxx>
33 #include <svl/poolitem.hxx>
34 #include <com/sun/star/uno/Any.hxx>
35 #include <com/sun/star/uno/Sequence.hxx>
36 #include <tools/cachestr.hxx>
37 
38 // STATIC DATA -----------------------------------------------------------
39 
40 
41 // -----------------------------------------------------------------------
42 
43 TYPEINIT1_AUTOFACTORY(SfxLockBytesItem, SfxPoolItem);
44 
45 // -----------------------------------------------------------------------
46 
47 SfxLockBytesItem::SfxLockBytesItem()
48 {
49 }
50 
51 // -----------------------------------------------------------------------
52 
53 SfxLockBytesItem::SfxLockBytesItem( sal_uInt16 nW, SvLockBytes *pLockBytes )
54 :	SfxPoolItem( nW ),
55 	_xVal( pLockBytes )
56 {
57 }
58 
59 // -----------------------------------------------------------------------
60 
61 SfxLockBytesItem::SfxLockBytesItem( sal_uInt16 nW, SvStream &rStream )
62 :	SfxPoolItem( nW )
63 {
64 	rStream.Seek( 0L );
65 	_xVal = new SvLockBytes( new SvCacheStream(), sal_True );
66 
67 	SvStream aLockBytesStream( _xVal );
68 	rStream >> aLockBytesStream;
69 }
70 
71 // -----------------------------------------------------------------------
72 
73 SfxLockBytesItem::SfxLockBytesItem( const SfxLockBytesItem& rItem )
74 :	SfxPoolItem( rItem ),
75 	_xVal( rItem._xVal )
76 {
77 }
78 
79 // -----------------------------------------------------------------------
80 
81 SfxLockBytesItem::~SfxLockBytesItem()
82 {
83 }
84 
85 // -----------------------------------------------------------------------
86 
87 int SfxLockBytesItem::operator==( const SfxPoolItem& rItem ) const
88 {
89 	return ((SfxLockBytesItem&)rItem)._xVal == _xVal;
90 }
91 
92 // -----------------------------------------------------------------------
93 
94 SfxPoolItem* SfxLockBytesItem::Clone(SfxItemPool *) const
95 {
96 	return new SfxLockBytesItem( *this );
97 }
98 
99 // -----------------------------------------------------------------------
100 
101 #define MAX_BUF	32000
102 
103 SfxPoolItem* SfxLockBytesItem::Create( SvStream &rStream, sal_uInt16 ) const
104 {
105 	sal_uInt32 nSize = 0;
106 	sal_uLong nActRead = 0;
107 	sal_Char cTmpBuf[MAX_BUF];
108 	SvMemoryStream aNewStream;
109 	rStream >> nSize;
110 
111 	do {
112 		sal_uLong nToRead;
113 		if( (nSize - nActRead) > MAX_BUF )
114 			nToRead = MAX_BUF;
115 		else
116 			nToRead = nSize - nActRead;
117 		nActRead += rStream.Read( cTmpBuf, nToRead );
118 		aNewStream.Write( cTmpBuf, nToRead );
119 	} while( nSize > nActRead );
120 
121 	return new SfxLockBytesItem( Which(), aNewStream );
122 }
123 
124 // -----------------------------------------------------------------------
125 
126 SvStream& SfxLockBytesItem::Store(SvStream &rStream, sal_uInt16 ) const
127 {
128 	SvStream aLockBytesStream( _xVal );
129 	sal_uInt32 nSize = aLockBytesStream.Seek( STREAM_SEEK_TO_END );
130 	aLockBytesStream.Seek( 0L );
131 
132 	rStream << nSize;
133 	rStream << aLockBytesStream;
134 
135 	return rStream;
136 }
137 
138 //----------------------------------------------------------------------------
139 // virtual
140 sal_Bool SfxLockBytesItem::PutValue( const com::sun::star::uno::Any& rVal, sal_uInt8 )
141 {
142 	com::sun::star::uno::Sequence< sal_Int8 > aSeq;
143 	if ( rVal >>= aSeq )
144 	{
145 		if ( aSeq.getLength() )
146 		{
147 			SvCacheStream* pStream = new SvCacheStream;
148 			pStream->Write( (void*)aSeq.getConstArray(), aSeq.getLength() );
149 			pStream->Seek(0);
150 
151 			_xVal = new SvLockBytes( pStream, sal_True );
152 		}
153 		else
154 			_xVal = NULL;
155 
156 		return sal_True;
157 	}
158 
159 	DBG_ERROR( "SfxLockBytesItem::PutValue - Wrong type!" );
160 	return sal_False;
161 }
162 
163 //----------------------------------------------------------------------------
164 // virtual
165 sal_Bool SfxLockBytesItem::QueryValue( com::sun::star::uno::Any& rVal,sal_uInt8 ) const
166 {
167 	if ( _xVal.Is() )
168 	{
169 		sal_uInt32 nLen;
170 		SvLockBytesStat aStat;
171 
172 		if ( _xVal->Stat( &aStat, SVSTATFLAG_DEFAULT ) == ERRCODE_NONE )
173 			nLen = aStat.nSize;
174 		else
175 			return sal_False;
176 
177 		sal_uLong nRead = 0;
178 		com::sun::star::uno::Sequence< sal_Int8 > aSeq( nLen );
179 
180 		_xVal->ReadAt( 0, aSeq.getArray(), nLen, &nRead );
181 		rVal <<= aSeq;
182 	}
183 	else
184 	{
185 		com::sun::star::uno::Sequence< sal_Int8 > aSeq( 0 );
186 		rVal <<= aSeq;
187 	}
188 
189 	return sal_True;
190 }
191 
192