Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
21 : #include <osl/file.h>
22 : #include <tools/vcompat.hxx>
23 : #include <tools/debug.hxx>
24 : #include <unotools/ucbstreamhelper.hxx>
25 : #include <unotools/tempfile.hxx>
26 : #include <ucbhelper/content.hxx>
27 : #include <vcl/graph.hxx>
28 : #include <vcl/gfxlink.hxx>
29 : #include <vcl/cvtgrf.hxx>
30 : #include <com/sun/star/ucb/CommandAbortedException.hpp>
31 :
32 7050 : GfxLink::GfxLink() :
33 : meType ( GFX_LINK_TYPE_NONE ),
34 : mpBuf ( NULL ),
35 : mpSwap ( NULL ),
36 : mnBufSize ( 0 ),
37 : mnUserId ( 0UL ),
38 7050 : mpImpData ( new ImpGfxLink )
39 : {
40 7050 : }
41 :
42 2114 : GfxLink::GfxLink( const GfxLink& rGfxLink ) :
43 2114 : mpImpData( new ImpGfxLink )
44 : {
45 2114 : ImplCopy( rGfxLink );
46 2114 : }
47 :
48 926 : GfxLink::GfxLink( sal_uInt8* pBuf, sal_uInt32 nSize, GfxLinkType nType, sal_Bool bOwns ) :
49 926 : mpImpData( new ImpGfxLink )
50 : {
51 : DBG_ASSERT( (pBuf != NULL && nSize) || (!bOwns && nSize == 0),
52 : "GfxLink::GfxLink(): empty/NULL buffer given" );
53 :
54 926 : meType = nType;
55 926 : mnBufSize = nSize;
56 926 : mpSwap = NULL;
57 926 : mnUserId = 0UL;
58 :
59 926 : if( bOwns )
60 926 : mpBuf = new ImpBuffer( pBuf );
61 0 : else if( nSize )
62 : {
63 0 : mpBuf = new ImpBuffer( nSize );
64 0 : memcpy( mpBuf->mpBuffer, pBuf, nSize );
65 : }
66 : else
67 0 : mpBuf = NULL;
68 926 : }
69 :
70 10014 : GfxLink::~GfxLink()
71 : {
72 10014 : if( mpBuf && !( --mpBuf->mnRefCount ) )
73 927 : delete mpBuf;
74 :
75 10014 : if( mpSwap && !( --mpSwap->mnRefCount ) )
76 899 : delete mpSwap;
77 :
78 10014 : delete mpImpData;
79 10014 : }
80 :
81 3641 : GfxLink& GfxLink::operator=( const GfxLink& rGfxLink )
82 : {
83 3641 : if( &rGfxLink != this )
84 : {
85 3641 : if ( mpBuf && !( --mpBuf->mnRefCount ) )
86 0 : delete mpBuf;
87 :
88 3641 : if( mpSwap && !( --mpSwap->mnRefCount ) )
89 6 : delete mpSwap;
90 :
91 3641 : ImplCopy( rGfxLink );
92 : }
93 :
94 3641 : return *this;
95 : }
96 :
97 0 : sal_Bool GfxLink::IsEqual( const GfxLink& rGfxLink ) const
98 : {
99 0 : sal_Bool bIsEqual = sal_False;
100 :
101 0 : if ( ( mnBufSize == rGfxLink.mnBufSize ) && ( meType == rGfxLink.meType ) )
102 : {
103 0 : const sal_uInt8* pSource = GetData();
104 0 : const sal_uInt8* pDest = rGfxLink.GetData();
105 0 : sal_uInt32 nSourceSize = GetDataSize();
106 0 : sal_uInt32 nDestSize = rGfxLink.GetDataSize();
107 0 : if ( pSource && pDest && ( nSourceSize == nDestSize ) )
108 : {
109 0 : bIsEqual = memcmp( pSource, pDest, nSourceSize ) == 0;
110 : }
111 0 : else if ( ( pSource == 0 ) && ( pDest == 0 ) )
112 0 : bIsEqual = sal_True;
113 : }
114 0 : return bIsEqual;
115 : }
116 :
117 5755 : void GfxLink::ImplCopy( const GfxLink& rGfxLink )
118 : {
119 5755 : mnBufSize = rGfxLink.mnBufSize;
120 5755 : meType = rGfxLink.meType;
121 5755 : mpBuf = rGfxLink.mpBuf;
122 5755 : mpSwap = rGfxLink.mpSwap;
123 5755 : mnUserId = rGfxLink.mnUserId;
124 5755 : *mpImpData = *rGfxLink.mpImpData;
125 :
126 5755 : if( mpBuf )
127 932 : mpBuf->mnRefCount++;
128 :
129 5755 : if( mpSwap )
130 1411 : mpSwap->mnRefCount++;
131 5755 : }
132 :
133 1857 : GfxLinkType GfxLink::GetType() const
134 : {
135 1857 : return meType;
136 : }
137 :
138 1654 : sal_Bool GfxLink::IsNative() const
139 : {
140 1654 : return( meType >= GFX_LINK_FIRST_NATIVE_ID && meType <= GFX_LINK_LAST_NATIVE_ID );
141 : }
142 :
143 28 : sal_uInt32 GfxLink::GetDataSize() const
144 : {
145 28 : return mnBufSize;
146 : }
147 :
148 9 : const sal_uInt8* GfxLink::GetData() const
149 : {
150 9 : if( IsSwappedOut() )
151 1 : ( (GfxLink*) this )->SwapIn();
152 :
153 9 : return( mpBuf ? mpBuf->mpBuffer : NULL );
154 : }
155 :
156 17 : const Size& GfxLink::GetPrefSize() const
157 : {
158 17 : return mpImpData->maPrefSize;
159 : }
160 :
161 17 : void GfxLink::SetPrefSize( const Size& rPrefSize )
162 : {
163 17 : mpImpData->maPrefSize = rPrefSize;
164 17 : mpImpData->mbPrefSizeValid = true;
165 17 : }
166 :
167 6 : bool GfxLink::IsPrefSizeValid()
168 : {
169 6 : return mpImpData->mbPrefSizeValid;
170 : }
171 :
172 17 : const MapMode& GfxLink::GetPrefMapMode() const
173 : {
174 17 : return mpImpData->maPrefMapMode;
175 : }
176 :
177 17 : void GfxLink::SetPrefMapMode( const MapMode& rPrefMapMode )
178 : {
179 17 : mpImpData->maPrefMapMode = rPrefMapMode;
180 17 : mpImpData->mbPrefMapModeValid = true;
181 17 : }
182 :
183 6 : bool GfxLink::IsPrefMapModeValid()
184 : {
185 6 : return mpImpData->mbPrefMapModeValid;
186 : }
187 :
188 6 : sal_Bool GfxLink::LoadNative( Graphic& rGraphic )
189 : {
190 6 : sal_Bool bRet = sal_False;
191 :
192 6 : if( IsNative() && mnBufSize )
193 : {
194 6 : const sal_uInt8* pData = GetData();
195 :
196 6 : if( pData )
197 : {
198 6 : SvMemoryStream aMemStm;
199 : sal_uLong nCvtType;
200 :
201 6 : aMemStm.SetBuffer( (char*) pData, mnBufSize, sal_False, mnBufSize );
202 :
203 6 : switch( meType )
204 : {
205 0 : case( GFX_LINK_TYPE_NATIVE_GIF ): nCvtType = CVT_GIF; break;
206 4 : case( GFX_LINK_TYPE_NATIVE_JPG ): nCvtType = CVT_JPG; break;
207 2 : case( GFX_LINK_TYPE_NATIVE_PNG ): nCvtType = CVT_PNG; break;
208 0 : case( GFX_LINK_TYPE_NATIVE_TIF ): nCvtType = CVT_TIF; break;
209 0 : case( GFX_LINK_TYPE_NATIVE_WMF ): nCvtType = CVT_WMF; break;
210 0 : case( GFX_LINK_TYPE_NATIVE_MET ): nCvtType = CVT_MET; break;
211 0 : case( GFX_LINK_TYPE_NATIVE_PCT ): nCvtType = CVT_PCT; break;
212 0 : case( GFX_LINK_TYPE_NATIVE_SVG ): nCvtType = CVT_SVG; break;
213 :
214 0 : default: nCvtType = CVT_UNKNOWN; break;
215 : }
216 :
217 6 : if( nCvtType && ( GraphicConverter::Import( aMemStm, rGraphic, nCvtType ) == ERRCODE_NONE ) )
218 6 : bRet = sal_True;
219 : }
220 : }
221 :
222 6 : return bRet;
223 : }
224 :
225 1620 : void GfxLink::SwapOut()
226 : {
227 1620 : if( !IsSwappedOut() && mpBuf )
228 : {
229 926 : mpSwap = new ImpSwap( mpBuf->mpBuffer, mnBufSize );
230 :
231 926 : if( !mpSwap->IsSwapped() )
232 : {
233 20 : delete mpSwap;
234 20 : mpSwap = NULL;
235 : }
236 : else
237 : {
238 906 : if( !( --mpBuf->mnRefCount ) )
239 0 : delete mpBuf;
240 :
241 906 : mpBuf = NULL;
242 : }
243 : }
244 1620 : }
245 :
246 1 : void GfxLink::SwapIn()
247 : {
248 1 : if( IsSwappedOut() )
249 : {
250 1 : mpBuf = new ImpBuffer( mpSwap->GetData() );
251 :
252 1 : if( !( --mpSwap->mnRefCount ) )
253 0 : delete mpSwap;
254 :
255 1 : mpSwap = NULL;
256 : }
257 1 : }
258 :
259 3 : sal_Bool GfxLink::ExportNative( SvStream& rOStream ) const
260 : {
261 3 : if( GetDataSize() )
262 : {
263 3 : if( IsSwappedOut() )
264 3 : mpSwap->WriteTo( rOStream );
265 0 : else if( GetData() )
266 0 : rOStream.Write( GetData(), GetDataSize() );
267 : }
268 :
269 3 : return ( rOStream.GetError() == ERRCODE_NONE );
270 : }
271 :
272 11 : SvStream& operator<<( SvStream& rOStream, const GfxLink& rGfxLink )
273 : {
274 11 : VersionCompat* pCompat = new VersionCompat( rOStream, STREAM_WRITE, 2 );
275 :
276 : // Version 1
277 11 : rOStream << (sal_uInt16) rGfxLink.GetType() << rGfxLink.GetDataSize() << rGfxLink.GetUserId();
278 :
279 : // Version 2
280 11 : rOStream << rGfxLink.GetPrefSize() << rGfxLink.GetPrefMapMode();
281 :
282 11 : delete pCompat;
283 :
284 11 : if( rGfxLink.GetDataSize() )
285 : {
286 11 : if( rGfxLink.IsSwappedOut() )
287 11 : rGfxLink.mpSwap->WriteTo( rOStream );
288 0 : else if( rGfxLink.GetData() )
289 0 : rOStream.Write( rGfxLink.GetData(), rGfxLink.GetDataSize() );
290 : }
291 :
292 11 : return rOStream;
293 : }
294 :
295 6 : SvStream& operator>>( SvStream& rIStream, GfxLink& rGfxLink)
296 : {
297 6 : Size aSize;
298 6 : MapMode aMapMode;
299 : sal_uInt32 nSize;
300 : sal_uInt32 nUserId;
301 : sal_uInt16 nType;
302 : sal_uInt8* pBuf;
303 6 : bool bMapAndSizeValid( false );
304 6 : VersionCompat* pCompat = new VersionCompat( rIStream, STREAM_READ );
305 :
306 : // Version 1
307 6 : rIStream >> nType >> nSize >> nUserId;
308 :
309 6 : if( pCompat->GetVersion() >= 2 )
310 : {
311 6 : rIStream >> aSize >> aMapMode;
312 6 : bMapAndSizeValid = true;
313 : }
314 :
315 6 : delete pCompat;
316 :
317 6 : pBuf = new sal_uInt8[ nSize ];
318 6 : rIStream.Read( pBuf, nSize );
319 :
320 6 : rGfxLink = GfxLink( pBuf, nSize, (GfxLinkType) nType, sal_True );
321 6 : rGfxLink.SetUserId( nUserId );
322 :
323 6 : if( bMapAndSizeValid )
324 : {
325 6 : rGfxLink.SetPrefSize( aSize );
326 6 : rGfxLink.SetPrefMapMode( aMapMode );
327 : }
328 :
329 6 : return rIStream;
330 : }
331 :
332 926 : ImpSwap::ImpSwap( sal_uInt8* pData, sal_uLong nDataSize ) :
333 : mnDataSize( nDataSize ),
334 926 : mnRefCount( 1UL )
335 : {
336 926 : if( pData && mnDataSize )
337 : {
338 926 : ::utl::TempFile aTempFile;
339 :
340 926 : maURL = aTempFile.GetURL();
341 926 : if( !maURL.isEmpty() )
342 : {
343 906 : SvStream* pOStm = ::utl::UcbStreamHelper::CreateStream( maURL, STREAM_READWRITE | STREAM_SHARE_DENYWRITE );
344 906 : if( pOStm )
345 : {
346 906 : pOStm->Write( pData, mnDataSize );
347 906 : bool bError = ( ERRCODE_NONE != pOStm->GetError() );
348 906 : delete pOStm;
349 :
350 906 : if( bError )
351 : {
352 0 : osl_removeFile( maURL.pData );
353 0 : maURL = String();
354 : }
355 : }
356 926 : }
357 : }
358 926 : }
359 :
360 1850 : ImpSwap::~ImpSwap()
361 : {
362 925 : if( IsSwapped() )
363 905 : osl_removeFile( maURL.pData );
364 925 : }
365 :
366 15 : sal_uInt8* ImpSwap::GetData() const
367 : {
368 : sal_uInt8* pData;
369 :
370 15 : if( IsSwapped() )
371 : {
372 15 : SvStream* pIStm = ::utl::UcbStreamHelper::CreateStream( maURL, STREAM_READWRITE );
373 15 : if( pIStm )
374 : {
375 15 : pData = new sal_uInt8[ mnDataSize ];
376 15 : pIStm->Read( pData, mnDataSize );
377 15 : bool bError = ( ERRCODE_NONE != pIStm->GetError() );
378 15 : sal_Size nActReadSize = pIStm->Tell();
379 15 : if (nActReadSize != mnDataSize)
380 : {
381 0 : bError = true;
382 : }
383 15 : delete pIStm;
384 :
385 15 : if( bError )
386 0 : delete[] pData, pData = NULL;
387 : }
388 : else
389 0 : pData = NULL;
390 : }
391 : else
392 0 : pData = NULL;
393 :
394 15 : return pData;
395 : }
396 :
397 14 : void ImpSwap::WriteTo( SvStream& rOStm ) const
398 : {
399 14 : sal_uInt8* pData = GetData();
400 :
401 14 : if( pData )
402 : {
403 14 : rOStm.Write( pData, mnDataSize );
404 14 : delete[] pData;
405 : }
406 479 : }
407 :
408 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|