LCOV - code coverage report
Current view: top level - libreoffice/vcl/source/gdi - jobset.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 78 199 39.2 %
Date: 2012-12-27 Functions: 7 16 43.8 %
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             : 
      21             : #include <tools/debug.hxx>
      22             : #include <tools/stream.hxx>
      23             : #include <rtl/alloc.h>
      24             : #include <vcl/jobset.hxx>
      25             : 
      26             : #include <jobset.h>
      27             : 
      28             : // =======================================================================
      29             : 
      30             : DBG_NAME( JobSetup )
      31             : 
      32             : #define JOBSET_FILEFORMAT2      3780
      33             : #define JOBSET_FILE364_SYSTEM   ((sal_uInt16)0xFFFF)
      34             : #define JOBSET_FILE605_SYSTEM   ((sal_uInt16)0xFFFE)
      35             : 
      36             : struct ImplOldJobSetupData
      37             : {
      38             :     char    cPrinterName[64];
      39             :     char    cDeviceName[32];
      40             :     char    cPortName[32];
      41             :     char    cDriverName[32];
      42             : };
      43             : 
      44             : struct Impl364JobSetupData
      45             : {
      46             :     SVBT16  nSize;
      47             :     SVBT16  nSystem;
      48             :     SVBT32  nDriverDataLen;
      49             :     SVBT16  nOrientation;
      50             :     SVBT16  nPaperBin;
      51             :     SVBT16  nPaperFormat;
      52             :     SVBT32  nPaperWidth;
      53             :     SVBT32  nPaperHeight;
      54             : };
      55             : 
      56             : // =======================================================================
      57             : 
      58           6 : ImplJobSetup::ImplJobSetup()
      59             : {
      60           6 :     mnRefCount          = 1;
      61           6 :     mnSystem            = 0;
      62           6 :     meOrientation       = ORIENTATION_PORTRAIT;
      63           6 :     meDuplexMode        = DUPLEX_UNKNOWN;
      64           6 :     mnPaperBin          = 0;
      65           6 :     mePaperFormat       = PAPER_USER;
      66           6 :     mnPaperWidth        = 0;
      67           6 :     mnPaperHeight       = 0;
      68           6 :     mnDriverDataLen     = 0;
      69           6 :     mpDriverData        = NULL;
      70           6 : }
      71             : 
      72             : // -----------------------------------------------------------------------
      73             : 
      74           0 : ImplJobSetup::ImplJobSetup( const ImplJobSetup& rJobSetup ) :
      75             :     maPrinterName( rJobSetup.maPrinterName ),
      76           0 :     maDriver( rJobSetup.maDriver )
      77             : {
      78           0 :     mnRefCount          = 1;
      79           0 :     mnSystem            = rJobSetup.mnSystem;
      80           0 :     meOrientation       = rJobSetup.meOrientation;
      81           0 :     meDuplexMode        = rJobSetup.meDuplexMode;
      82           0 :     mnPaperBin          = rJobSetup.mnPaperBin;
      83           0 :     mePaperFormat       = rJobSetup.mePaperFormat;
      84           0 :     mnPaperWidth        = rJobSetup.mnPaperWidth;
      85           0 :     mnPaperHeight       = rJobSetup.mnPaperHeight;
      86           0 :     mnDriverDataLen     = rJobSetup.mnDriverDataLen;
      87           0 :     if ( rJobSetup.mpDriverData )
      88             :     {
      89           0 :         mpDriverData = (sal_uInt8*)rtl_allocateMemory( mnDriverDataLen );
      90           0 :         memcpy( mpDriverData, rJobSetup.mpDriverData, mnDriverDataLen );
      91             :     }
      92             :     else
      93           0 :         mpDriverData = NULL;
      94           0 :     maValueMap          = rJobSetup.maValueMap;
      95           0 : }
      96             : 
      97             : // -----------------------------------------------------------------------
      98             : 
      99          12 : ImplJobSetup::~ImplJobSetup()
     100             : {
     101           6 :     rtl_freeMemory( mpDriverData );
     102           6 : }
     103             : 
     104             : // =======================================================================
     105             : 
     106           0 : ImplJobSetup* JobSetup::ImplGetData()
     107             : {
     108           0 :     if ( !mpData )
     109           0 :         mpData = new ImplJobSetup;
     110           0 :     else if ( mpData->mnRefCount != 1 )
     111             :     {
     112           0 :         mpData->mnRefCount--;
     113           0 :         mpData = new ImplJobSetup( *mpData );
     114             :     }
     115             : 
     116           0 :     return mpData;
     117             : }
     118             : 
     119             : // -----------------------------------------------------------------------
     120             : 
     121           0 : ImplJobSetup* JobSetup::ImplGetConstData()
     122             : {
     123           0 :     if ( !mpData )
     124           0 :         mpData = new ImplJobSetup;
     125           0 :     return mpData;
     126             : }
     127             : 
     128             : // -----------------------------------------------------------------------
     129             : 
     130           1 : const ImplJobSetup* JobSetup::ImplGetConstData() const
     131             : {
     132           1 :     if ( !mpData )
     133           1 :         ((JobSetup*)this)->mpData = new ImplJobSetup;
     134           1 :     return mpData;
     135             : }
     136             : 
     137             : // =======================================================================
     138             : 
     139        2555 : JobSetup::JobSetup()
     140             : {
     141             :     DBG_CTOR( JobSetup, NULL );
     142             : 
     143        2555 :     mpData = NULL;
     144        2555 : }
     145             : 
     146             : // -----------------------------------------------------------------------
     147             : 
     148           0 : JobSetup::JobSetup( const JobSetup& rJobSetup )
     149             : {
     150             :     DBG_CTOR( JobSetup, NULL );
     151             :     DBG_CHKOBJ( &rJobSetup, JobSetup, NULL );
     152             :     DBG_ASSERT( !rJobSetup.mpData || (rJobSetup.mpData->mnRefCount < 0xFFFE), "JobSetup: RefCount overflow" );
     153             : 
     154           0 :     mpData = rJobSetup.mpData;
     155           0 :     if ( mpData )
     156           0 :         mpData->mnRefCount++;
     157           0 : }
     158             : 
     159             : // -----------------------------------------------------------------------
     160             : 
     161        2535 : JobSetup::~JobSetup()
     162             : {
     163             :     DBG_DTOR( JobSetup, NULL );
     164             : 
     165        2535 :     if ( mpData )
     166             :     {
     167           6 :         if ( mpData->mnRefCount == 1 )
     168           6 :             delete mpData;
     169             :         else
     170           0 :             mpData->mnRefCount--;
     171             :     }
     172        2535 : }
     173             : 
     174             : // -----------------------------------------------------------------------
     175             : 
     176          12 : rtl::OUString JobSetup::GetPrinterName() const
     177             : {
     178          12 :     if ( mpData )
     179          10 :         return mpData->maPrinterName;
     180             :     else
     181           2 :         return rtl::OUString();
     182             : }
     183             : 
     184             : // -----------------------------------------------------------------------
     185             : 
     186           0 : rtl::OUString JobSetup::GetDriverName() const
     187             : {
     188           0 :     if ( mpData )
     189           0 :         return mpData->maDriver;
     190             :     else
     191           0 :         return rtl::OUString();
     192             : }
     193             : 
     194             : // -----------------------------------------------------------------------
     195             : 
     196           0 : void JobSetup::SetValue( const rtl::OUString& rKey, const rtl::OUString& rValue )
     197             : {
     198           0 :     if( ! mpData )
     199           0 :         mpData = new ImplJobSetup();
     200             : 
     201           0 :     mpData->maValueMap[ rKey ] = rValue;
     202           0 : }
     203             : 
     204             : // -----------------------------------------------------------------------
     205             : 
     206           0 : JobSetup& JobSetup::operator=( const JobSetup& rJobSetup )
     207             : {
     208             :     DBG_CHKTHIS( JobSetup, NULL );
     209             :     DBG_CHKOBJ( &rJobSetup, JobSetup, NULL );
     210             :     DBG_ASSERT( !rJobSetup.mpData || (rJobSetup.mpData->mnRefCount) < 0xFFFE, "JobSetup: RefCount overflow" );
     211             : 
     212             :     // Zuerst Referenzcounter erhoehen, damit man sich selbst zuweisen kann
     213           0 :     if ( rJobSetup.mpData )
     214           0 :         rJobSetup.mpData->mnRefCount++;
     215             : 
     216             :     // Wenn es keine statischen ImpDaten sind, dann loeschen, wenn es
     217             :     // die letzte Referenz ist, sonst Referenzcounter decrementieren
     218           0 :     if ( mpData )
     219             :     {
     220           0 :         if ( mpData->mnRefCount == 1 )
     221           0 :             delete mpData;
     222             :         else
     223           0 :             mpData->mnRefCount--;
     224             :     }
     225             : 
     226           0 :     mpData = rJobSetup.mpData;
     227             : 
     228           0 :     return *this;
     229             : }
     230             : 
     231             : // -----------------------------------------------------------------------
     232             : 
     233           0 : sal_Bool JobSetup::operator==( const JobSetup& rJobSetup ) const
     234             : {
     235             :     DBG_CHKTHIS( JobSetup, NULL );
     236             :     DBG_CHKOBJ( &rJobSetup, JobSetup, NULL );
     237             : 
     238           0 :     if ( mpData == rJobSetup.mpData )
     239           0 :         return sal_True;
     240             : 
     241           0 :     if ( !mpData || !rJobSetup.mpData )
     242           0 :         return sal_False;
     243             : 
     244           0 :     ImplJobSetup* pData1 = mpData;
     245           0 :     ImplJobSetup* pData2 = rJobSetup.mpData;
     246           0 :     if ( (pData1->mnSystem          == pData2->mnSystem)                &&
     247           0 :          (pData1->maPrinterName     == pData2->maPrinterName)           &&
     248           0 :          (pData1->maDriver          == pData2->maDriver)                &&
     249             :          (pData1->meOrientation     == pData2->meOrientation)           &&
     250             :          (pData1->meDuplexMode      == pData2->meDuplexMode)            &&
     251             :          (pData1->mnPaperBin        == pData2->mnPaperBin)              &&
     252             :          (pData1->mePaperFormat     == pData2->mePaperFormat)           &&
     253             :          (pData1->mnPaperWidth      == pData2->mnPaperWidth)            &&
     254             :          (pData1->mnPaperHeight     == pData2->mnPaperHeight)           &&
     255             :          (pData1->mnDriverDataLen   == pData2->mnDriverDataLen)         &&
     256           0 :          (memcmp( pData1->mpDriverData, pData2->mpDriverData, pData1->mnDriverDataLen ) == 0)                                                           &&
     257           0 :          (pData1->maValueMap        == pData2->maValueMap)
     258             :          )
     259           0 :         return sal_True;
     260             : 
     261           0 :     return sal_False;
     262             : }
     263             : 
     264             : // -----------------------------------------------------------------------
     265             : 
     266           5 : SvStream& operator>>( SvStream& rIStream, JobSetup& rJobSetup )
     267             : {
     268             :     DBG_ASSERTWARNING( rIStream.GetVersion(), "JobSetup::>> - Solar-Version not set on rOStream" );
     269             : 
     270             :     {
     271           5 :         sal_Size nFirstPos = rIStream.Tell();
     272             : 
     273           5 :         sal_uInt16 nLen = 0;
     274           5 :         rIStream >> nLen;
     275           5 :         if ( !nLen )
     276           0 :             return rIStream;
     277             : 
     278           5 :         sal_uInt16 nSystem = 0;
     279           5 :         rIStream >> nSystem;
     280             : 
     281           5 :         char* pTempBuf = new char[nLen];
     282           5 :         rIStream.Read( pTempBuf,  nLen - sizeof( nLen ) - sizeof( nSystem ) );
     283           5 :         if ( nLen >= sizeof(ImplOldJobSetupData)+4 )
     284             :         {
     285           5 :             ImplOldJobSetupData* pData = (ImplOldJobSetupData*)pTempBuf;
     286           5 :             if ( rJobSetup.mpData )
     287             :             {
     288           0 :                 if ( rJobSetup.mpData->mnRefCount == 1 )
     289           0 :                     delete rJobSetup.mpData;
     290             :                 else
     291           0 :                     rJobSetup.mpData->mnRefCount--;
     292             :             }
     293             : 
     294           5 :             rtl_TextEncoding aStreamEncoding = RTL_TEXTENCODING_UTF8;
     295           5 :             if( nSystem == JOBSET_FILE364_SYSTEM )
     296           0 :                 aStreamEncoding = rIStream.GetStreamCharSet();
     297             : 
     298           5 :             rJobSetup.mpData = new ImplJobSetup;
     299           5 :             ImplJobSetup* pJobData = rJobSetup.mpData;
     300           5 :             pJobData->maPrinterName = UniString(pData->cPrinterName, aStreamEncoding);
     301           5 :             pJobData->maDriver = UniString(pData->cDriverName, aStreamEncoding);
     302             : 
     303             :             // Sind es unsere neuen JobSetup-Daten?
     304           5 :             if ( nSystem == JOBSET_FILE364_SYSTEM ||
     305             :                  nSystem == JOBSET_FILE605_SYSTEM )
     306             :             {
     307           5 :                 Impl364JobSetupData* pOldJobData    = (Impl364JobSetupData*)(pTempBuf + sizeof( ImplOldJobSetupData ));
     308           5 :                 sal_uInt16 nOldJobDataSize              = SVBT16ToShort( pOldJobData->nSize );
     309           5 :                 pJobData->mnSystem                  = SVBT16ToShort( pOldJobData->nSystem );
     310           5 :                 pJobData->mnDriverDataLen           = SVBT32ToUInt32( pOldJobData->nDriverDataLen );
     311           5 :                 pJobData->meOrientation             = (Orientation)SVBT16ToShort( pOldJobData->nOrientation );
     312           5 :                 pJobData->meDuplexMode              = DUPLEX_UNKNOWN;
     313           5 :                 pJobData->mnPaperBin                = SVBT16ToShort( pOldJobData->nPaperBin );
     314           5 :                 pJobData->mePaperFormat             = (Paper)SVBT16ToShort( pOldJobData->nPaperFormat );
     315           5 :                 pJobData->mnPaperWidth              = (long)SVBT32ToUInt32( pOldJobData->nPaperWidth );
     316           5 :                 pJobData->mnPaperHeight             = (long)SVBT32ToUInt32( pOldJobData->nPaperHeight );
     317           5 :                 if ( pJobData->mnDriverDataLen )
     318             :                 {
     319           5 :                     sal_uInt8* pDriverData = ((sal_uInt8*)pOldJobData) + nOldJobDataSize;
     320           5 :                     pJobData->mpDriverData = (sal_uInt8*)rtl_allocateMemory( pJobData->mnDriverDataLen );
     321           5 :                     memcpy( pJobData->mpDriverData, pDriverData, pJobData->mnDriverDataLen );
     322             :                 }
     323           5 :                 if( nSystem == JOBSET_FILE605_SYSTEM )
     324             :                 {
     325           5 :                     rIStream.Seek( nFirstPos + sizeof( ImplOldJobSetupData ) + 4 + sizeof( Impl364JobSetupData ) + pJobData->mnDriverDataLen );
     326          14 :                     while( rIStream.Tell() < nFirstPos + nLen )
     327             :                     {
     328           4 :                         String aKey = read_lenPrefixed_uInt8s_ToOUString<sal_uInt16>(rIStream, RTL_TEXTENCODING_UTF8);
     329           4 :                         String aValue = read_lenPrefixed_uInt8s_ToOUString<sal_uInt16>(rIStream, RTL_TEXTENCODING_UTF8);
     330           4 :                         if( aKey.EqualsAscii( "COMPAT_DUPLEX_MODE" ) )
     331             :                         {
     332           4 :                             if( aValue.EqualsAscii( "DUPLEX_UNKNOWN" ) )
     333           3 :                                 pJobData->meDuplexMode = DUPLEX_UNKNOWN;
     334           1 :                             else if( aValue.EqualsAscii( "DUPLEX_OFF" ) )
     335           1 :                                 pJobData->meDuplexMode = DUPLEX_OFF;
     336           0 :                             else if( aValue.EqualsAscii( "DUPLEX_SHORTEDGE" ) )
     337           0 :                                 pJobData->meDuplexMode = DUPLEX_SHORTEDGE;
     338           0 :                             else if( aValue.EqualsAscii( "DUPLEX_LONGEDGE" ) )
     339           0 :                                 pJobData->meDuplexMode = DUPLEX_LONGEDGE;
     340             :                         }
     341             :                         else
     342           0 :                             pJobData->maValueMap[ aKey ] = aValue;
     343           4 :                     }
     344             :                     DBG_ASSERT( rIStream.Tell() == nFirstPos+nLen, "corrupted job setup" );
     345             :                     // ensure correct stream position
     346           5 :                     rIStream.Seek( nFirstPos + nLen );
     347             :                 }
     348             :             }
     349             :         }
     350           5 :         delete[] pTempBuf;
     351             :     }
     352             : 
     353           5 :     return rIStream;
     354             : }
     355             : 
     356             : // -----------------------------------------------------------------------
     357             : 
     358           0 : SvStream& operator<<( SvStream& rOStream, const JobSetup& rJobSetup )
     359             : {
     360             :     DBG_ASSERTWARNING( rOStream.GetVersion(), "JobSetup::<< - Solar-Version not set on rOStream" );
     361             : 
     362             :     // Zur Zeit haben wir noch kein neues FileFormat
     363             : //    if ( rOStream.GetVersion() < JOBSET_FILEFORMAT2 )
     364             :     {
     365           0 :         sal_uInt16 nLen = 0;
     366           0 :         if ( !rJobSetup.mpData )
     367           0 :             rOStream << nLen;
     368             :         else
     369             :         {
     370           0 :             sal_uInt16 nSystem = JOBSET_FILE605_SYSTEM;
     371             : 
     372           0 :             const ImplJobSetup* pJobData = rJobSetup.ImplGetConstData();
     373             :             Impl364JobSetupData aOldJobData;
     374           0 :             sal_uInt16              nOldJobDataSize = sizeof( aOldJobData );
     375           0 :             ShortToSVBT16( nOldJobDataSize, aOldJobData.nSize );
     376           0 :             ShortToSVBT16( pJobData->mnSystem, aOldJobData.nSystem );
     377           0 :             UInt32ToSVBT32( pJobData->mnDriverDataLen, aOldJobData.nDriverDataLen );
     378           0 :             ShortToSVBT16( (sal_uInt16)(pJobData->meOrientation), aOldJobData.nOrientation );
     379           0 :             ShortToSVBT16( pJobData->mnPaperBin, aOldJobData.nPaperBin );
     380           0 :             ShortToSVBT16( (sal_uInt16)(pJobData->mePaperFormat), aOldJobData.nPaperFormat );
     381           0 :             UInt32ToSVBT32( (sal_uLong)(pJobData->mnPaperWidth), aOldJobData.nPaperWidth );
     382           0 :             UInt32ToSVBT32( (sal_uLong)(pJobData->mnPaperHeight), aOldJobData.nPaperHeight );
     383             : 
     384             :             ImplOldJobSetupData aOldData;
     385           0 :             memset( &aOldData, 0, sizeof( aOldData ) );
     386           0 :             rtl::OString aPrnByteName(rtl::OUStringToOString(rJobSetup.GetPrinterName(), RTL_TEXTENCODING_UTF8));
     387           0 :             strncpy( aOldData.cPrinterName, aPrnByteName.getStr(), 63 );
     388           0 :             rtl::OString aDriverByteName(rtl::OUStringToOString(rJobSetup.GetDriverName(), RTL_TEXTENCODING_UTF8));
     389           0 :             strncpy( aOldData.cDriverName, aDriverByteName.getStr(), 31 );
     390             : //          nLen = sizeof( aOldData ) + 4 + nOldJobDataSize + pJobData->mnDriverDataLen;
     391           0 :             int nPos = rOStream.Tell();
     392           0 :             rOStream << nLen;
     393           0 :             rOStream << nSystem;
     394           0 :             rOStream.Write( (char*)&aOldData, sizeof( aOldData ) );
     395           0 :             rOStream.Write( (char*)&aOldJobData, nOldJobDataSize );
     396           0 :             rOStream.Write( (char*)pJobData->mpDriverData, pJobData->mnDriverDataLen );
     397           0 :             ::boost::unordered_map< ::rtl::OUString, ::rtl::OUString, ::rtl::OUStringHash >::const_iterator it;
     398           0 :             for( it = pJobData->maValueMap.begin(); it != pJobData->maValueMap.end(); ++it )
     399             :             {
     400           0 :                 write_lenPrefixed_uInt8s_FromOUString<sal_uInt16>(rOStream, it->first, RTL_TEXTENCODING_UTF8);
     401           0 :                 write_lenPrefixed_uInt8s_FromOUString<sal_uInt16>(rOStream, it->second, RTL_TEXTENCODING_UTF8);
     402             :             }
     403           0 :             write_lenPrefixed_uInt8s_FromOString<sal_uInt16>(rOStream, "COMPAT_DUPLEX_MODE");
     404           0 :             switch( pJobData->meDuplexMode )
     405             :             {
     406             :                 case DUPLEX_UNKNOWN:
     407           0 :                     write_lenPrefixed_uInt8s_FromOString<sal_uInt16>(rOStream, "DUPLEX_UNKNOWN");
     408           0 :                     break;
     409             :                 case DUPLEX_OFF:
     410           0 :                     write_lenPrefixed_uInt8s_FromOString<sal_uInt16>(rOStream, "DUPLEX_OFF");
     411           0 :                     break;
     412             :                 case DUPLEX_SHORTEDGE:
     413           0 :                     write_lenPrefixed_uInt8s_FromOString<sal_uInt16>(rOStream, "DUPLEX_SHORTEDGE");
     414           0 :                     break;
     415             :                 case DUPLEX_LONGEDGE:
     416           0 :                     write_lenPrefixed_uInt8s_FromOString<sal_uInt16>(rOStream, "DUPLEX_LONGEDGE");
     417           0 :                     break;
     418             :             }
     419           0 :             nLen = sal::static_int_cast<sal_uInt16>(rOStream.Tell() - nPos);
     420           0 :             rOStream.Seek( nPos );
     421           0 :             rOStream << nLen;
     422           0 :             rOStream.Seek( nPos + nLen );
     423             :         }
     424             :     }
     425             : /*
     426             :     else
     427             :     {
     428             :     }
     429             : */
     430             : 
     431           0 :     return rOStream;
     432             : }
     433             : 
     434             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10