LCOV - code coverage report
Current view: top level - vcl/source/gdi - gfxlink.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 204 0.0 %
Date: 2014-04-14 Functions: 0 27 0.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10