LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/vcl/unx/generic/printer - jobdata.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 128 169 75.7 %
Date: 2013-07-09 Functions: 3 8 37.5 %
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 <officecfg/Office/Common.hxx>
      22             : #include "vcl/jobdata.hxx"
      23             : #include "vcl/printerinfomanager.hxx"
      24             : #include "tools/stream.hxx"
      25             : 
      26             : #include <sal/alloca.h>
      27             : #include <rtl/strbuf.hxx>
      28             : 
      29             : using namespace psp;
      30             : 
      31         319 : JobData& JobData::operator=(const JobData& rRight)
      32             : {
      33         319 :     m_nCopies               = rRight.m_nCopies;
      34         319 :     m_nLeftMarginAdjust     = rRight.m_nLeftMarginAdjust;
      35         319 :     m_nRightMarginAdjust    = rRight.m_nRightMarginAdjust;
      36         319 :     m_nTopMarginAdjust      = rRight.m_nTopMarginAdjust;
      37         319 :     m_nBottomMarginAdjust   = rRight.m_nBottomMarginAdjust;
      38         319 :     m_nColorDepth           = rRight.m_nColorDepth;
      39         319 :     m_eOrientation          = rRight.m_eOrientation;
      40         319 :     m_aPrinterName          = rRight.m_aPrinterName;
      41         319 :     m_pParser               = rRight.m_pParser;
      42         319 :     m_aContext              = rRight.m_aContext;
      43         319 :     m_nPSLevel              = rRight.m_nPSLevel;
      44         319 :     m_nPDFDevice            = rRight.m_nPDFDevice;
      45         319 :     m_nColorDevice          = rRight.m_nColorDevice;
      46             : 
      47         319 :     if( !m_pParser && !m_aPrinterName.isEmpty() )
      48             :     {
      49           0 :         PrinterInfoManager& rMgr = PrinterInfoManager::get();
      50           0 :         rMgr.setupJobContextData( *this );
      51             :     }
      52         319 :     return *this;
      53             : }
      54             : 
      55           0 : void JobData::setCollate( bool bCollate )
      56             : {
      57           0 :     const PPDParser* pParser = m_aContext.getParser();
      58           0 :     if( pParser )
      59             :     {
      60           0 :         const PPDKey* pKey = pParser->getKey( String( RTL_CONSTASCII_USTRINGPARAM( "Collate" ) ) );
      61           0 :         if( pKey )
      62             :         {
      63           0 :             const PPDValue* pVal = NULL;
      64           0 :             if( bCollate )
      65           0 :                 pVal = pKey->getValue( String( RTL_CONSTASCII_USTRINGPARAM( "True" ) ) );
      66             :             else
      67             :             {
      68           0 :                 pVal = pKey->getValue( String( RTL_CONSTASCII_USTRINGPARAM( "False" ) ) );
      69           0 :                 if( ! pVal )
      70           0 :                     pVal = pKey->getValue( String( RTL_CONSTASCII_USTRINGPARAM( "None" ) ) );
      71             :             }
      72           0 :             m_aContext.setValue( pKey, pVal );
      73             :         }
      74             :     }
      75           0 : }
      76             : 
      77           0 : bool JobData::setPaper( int i_nWidth, int i_nHeight )
      78             : {
      79           0 :     bool bSuccess = false;
      80           0 :     if( m_pParser )
      81             :     {
      82           0 :         OUString aPaper( m_pParser->matchPaper( i_nWidth, i_nHeight ) );
      83             : 
      84           0 :         const PPDKey*   pKey = m_pParser->getKey( String( RTL_CONSTASCII_USTRINGPARAM( "PageSize" ) ) );
      85           0 :         const PPDValue* pValue = pKey ? pKey->getValueCaseInsensitive( aPaper ) : NULL;
      86             : 
      87           0 :         bSuccess = pKey && pValue && m_aContext.setValue( pKey, pValue, false );
      88             :     }
      89           0 :     return bSuccess;
      90             : }
      91             : 
      92           0 : bool JobData::setPaperBin( int i_nPaperBin )
      93             : {
      94           0 :     bool bSuccess = false;
      95           0 :     if( m_pParser )
      96             :     {
      97           0 :         const PPDKey*   pKey = m_pParser->getKey( String( RTL_CONSTASCII_USTRINGPARAM( "InputSlot" ) ) );
      98           0 :         const PPDValue* pValue = pKey ? pKey->getValue( i_nPaperBin ) : NULL;
      99             : 
     100           0 :         bSuccess = pKey && pValue && m_aContext.setValue( pKey, pValue, false );
     101             :     }
     102           0 :     return bSuccess;
     103             : }
     104             : 
     105          80 : bool JobData::getStreamBuffer( void*& pData, int& bytes )
     106             : {
     107             :     // consistency checks
     108          80 :     if( ! m_pParser )
     109           0 :         m_pParser = m_aContext.getParser();
     110         160 :     if( m_pParser != m_aContext.getParser() ||
     111          80 :         ! m_pParser )
     112           0 :         return false;
     113             : 
     114          80 :     SvMemoryStream aStream;
     115             : 
     116             :     // write header job data
     117          80 :     aStream.WriteLine(OString(RTL_CONSTASCII_STRINGPARAM("JobData 1")));
     118             : 
     119         160 :     OStringBuffer aLine;
     120             : 
     121          80 :     aLine.append(RTL_CONSTASCII_STRINGPARAM("printer="));
     122          80 :     aLine.append(OUStringToOString(m_aPrinterName, RTL_TEXTENCODING_UTF8));
     123          80 :     aStream.WriteLine(aLine.makeStringAndClear());
     124             : 
     125          80 :     aLine.append(RTL_CONSTASCII_STRINGPARAM("orientation="));
     126          80 :     if (m_eOrientation == orientation::Landscape)
     127           0 :         aLine.append(RTL_CONSTASCII_STRINGPARAM("Landscape"));
     128             :     else
     129          80 :         aLine.append(RTL_CONSTASCII_STRINGPARAM("Portrait"));
     130          80 :     aStream.WriteLine(aLine.makeStringAndClear());
     131             : 
     132          80 :     aLine.append(RTL_CONSTASCII_STRINGPARAM("copies="));
     133          80 :     aLine.append(static_cast<sal_Int32>(m_nCopies));
     134          80 :     aStream.WriteLine(aLine.makeStringAndClear());
     135             : 
     136          80 :     aLine.append(RTL_CONSTASCII_STRINGPARAM("margindajustment="));
     137          80 :     aLine.append(static_cast<sal_Int32>(m_nLeftMarginAdjust));
     138          80 :     aLine.append(',');
     139          80 :     aLine.append(static_cast<sal_Int32>(m_nRightMarginAdjust));
     140          80 :     aLine.append(',');
     141          80 :     aLine.append(static_cast<sal_Int32>(m_nTopMarginAdjust));
     142          80 :     aLine.append(',');
     143          80 :     aLine.append(static_cast<sal_Int32>(m_nBottomMarginAdjust));
     144          80 :     aStream.WriteLine(aLine.makeStringAndClear());
     145             : 
     146          80 :     aLine.append(RTL_CONSTASCII_STRINGPARAM("colordepth="));
     147          80 :     aLine.append(static_cast<sal_Int32>(m_nColorDepth));
     148          80 :     aStream.WriteLine(aLine.makeStringAndClear());
     149             : 
     150          80 :     aLine.append(RTL_CONSTASCII_STRINGPARAM("pslevel="));
     151          80 :     aLine.append(static_cast<sal_Int32>(m_nPSLevel));
     152          80 :     aStream.WriteLine(aLine.makeStringAndClear());
     153             : 
     154          80 :     aLine.append(RTL_CONSTASCII_STRINGPARAM("pdfdevice="));
     155          80 :     aLine.append(static_cast<sal_Int32>(m_nPDFDevice));
     156          80 :     aStream.WriteLine(aLine.makeStringAndClear());
     157             : 
     158          80 :     aLine.append(RTL_CONSTASCII_STRINGPARAM("colordevice="));
     159          80 :     aLine.append(static_cast<sal_Int32>(m_nColorDevice));
     160          80 :     aStream.WriteLine(aLine.makeStringAndClear());
     161             : 
     162             :     // now append the PPDContext stream buffer
     163          80 :     aStream.WriteLine( "PPDContexData" );
     164             :     sal_uLong nBytes;
     165          80 :     char* pContextBuffer = m_aContext.getStreamableBuffer( nBytes );
     166          80 :     if( nBytes )
     167          80 :         aStream.Write( pContextBuffer, nBytes );
     168          80 :     delete [] pContextBuffer;
     169             : 
     170             :     // success
     171          80 :     pData = rtl_allocateMemory( bytes = aStream.Tell() );
     172          80 :     memcpy( pData, aStream.GetData(), bytes );
     173         160 :     return true;
     174             : }
     175             : 
     176          83 : bool JobData::constructFromStreamBuffer( void* pData, int bytes, JobData& rJobData )
     177             : {
     178          83 :     SvMemoryStream aStream( pData, bytes, STREAM_READ );
     179         166 :     OString aLine;
     180          83 :     bool bVersion       = false;
     181          83 :     bool bPrinter       = false;
     182          83 :     bool bOrientation   = false;
     183          83 :     bool bCopies        = false;
     184          83 :     bool bContext       = false;
     185          83 :     bool bMargin        = false;
     186          83 :     bool bColorDepth    = false;
     187          83 :     bool bColorDevice   = false;
     188          83 :     bool bPSLevel       = false;
     189          83 :     bool bPDFDevice     = false;
     190             : 
     191          83 :     const char printerEquals[] = "printer=";
     192          83 :     const char orientatationEquals[] = "orientation=";
     193          83 :     const char copiesEquals[] = "copies=";
     194          83 :     const char margindajustmentEquals[] = "margindajustment=";
     195          83 :     const char colordepthEquals[] = "colordepth=";
     196          83 :     const char colordeviceEquals[] = "colordevice=";
     197          83 :     const char pslevelEquals[] = "pslevel=";
     198          83 :     const char pdfdeviceEquals[] = "pdfdevice=";
     199             : 
     200        1079 :     while( ! aStream.IsEof() )
     201             :     {
     202         913 :         aStream.ReadLine( aLine );
     203         913 :         if (aLine.matchL(RTL_CONSTASCII_STRINGPARAM("JobData")))
     204          83 :             bVersion = true;
     205         830 :         else if (aLine.matchL(RTL_CONSTASCII_STRINGPARAM(printerEquals)))
     206             :         {
     207          83 :             bPrinter = true;
     208          83 :             rJobData.m_aPrinterName = OStringToOUString(aLine.copy(RTL_CONSTASCII_LENGTH(printerEquals)), RTL_TEXTENCODING_UTF8);
     209             :         }
     210         747 :         else if (aLine.matchL(RTL_CONSTASCII_STRINGPARAM(orientatationEquals)))
     211             :         {
     212          83 :             bOrientation = true;
     213          83 :             rJobData.m_eOrientation = aLine.copy(RTL_CONSTASCII_LENGTH(orientatationEquals)).equalsIgnoreAsciiCase("landscape") ? orientation::Landscape : orientation::Portrait;
     214             :         }
     215         664 :         else if (aLine.matchL(RTL_CONSTASCII_STRINGPARAM(copiesEquals)))
     216             :         {
     217          83 :             bCopies = true;
     218          83 :             rJobData.m_nCopies = aLine.copy(RTL_CONSTASCII_LENGTH(copiesEquals)).toInt32();
     219             :         }
     220         581 :         else if (aLine.matchL(RTL_CONSTASCII_STRINGPARAM(margindajustmentEquals)))
     221             :         {
     222          83 :             bMargin = true;
     223          83 :             OString aValues(aLine.copy(RTL_CONSTASCII_LENGTH(margindajustmentEquals)));
     224          83 :             rJobData.m_nLeftMarginAdjust = aValues.getToken(0, ',').toInt32();
     225          83 :             rJobData.m_nRightMarginAdjust = aValues.getToken(1, ',').toInt32();
     226          83 :             rJobData.m_nTopMarginAdjust = aValues.getToken(2, ',').toInt32();
     227          83 :             rJobData.m_nBottomMarginAdjust = aValues.getToken(3, ',').toInt32();
     228             :         }
     229         498 :         else if (aLine.matchL(RTL_CONSTASCII_STRINGPARAM(colordepthEquals)))
     230             :         {
     231          83 :             bColorDepth = true;
     232          83 :             rJobData.m_nColorDepth = aLine.copy(RTL_CONSTASCII_LENGTH(colordepthEquals)).toInt32();
     233             :         }
     234         415 :         else if (aLine.matchL(RTL_CONSTASCII_STRINGPARAM(colordeviceEquals)))
     235             :         {
     236          83 :             bColorDevice = true;
     237          83 :             rJobData.m_nColorDevice = aLine.copy(RTL_CONSTASCII_LENGTH(colordeviceEquals)).toInt32();
     238             :         }
     239         332 :         else if (aLine.matchL(RTL_CONSTASCII_STRINGPARAM(pslevelEquals)))
     240             :         {
     241          83 :             bPSLevel = true;
     242          83 :             rJobData.m_nPSLevel = aLine.copy(RTL_CONSTASCII_LENGTH(pslevelEquals)).toInt32();
     243             :         }
     244         249 :         else if (aLine.matchL(RTL_CONSTASCII_STRINGPARAM(pdfdeviceEquals)))
     245             :         {
     246          83 :             bPDFDevice = true;
     247          83 :             rJobData.m_nPDFDevice = aLine.copy(RTL_CONSTASCII_LENGTH(pdfdeviceEquals)).toInt32();
     248             :         }
     249         166 :         else if (aLine.equalsL(RTL_CONSTASCII_STRINGPARAM("PPDContexData")))
     250             :         {
     251          83 :             if( bPrinter )
     252             :             {
     253          83 :                 PrinterInfoManager& rManager = PrinterInfoManager::get();
     254          83 :                 const PrinterInfo& rInfo = rManager.getPrinterInfo( rJobData.m_aPrinterName );
     255          83 :                 rJobData.m_pParser = PPDParser::getParser( rInfo.m_aDriverName );
     256          83 :                 if( rJobData.m_pParser )
     257             :                 {
     258          83 :                     rJobData.m_aContext.setParser( rJobData.m_pParser );
     259          83 :                     int nBytes = bytes - aStream.Tell();
     260          83 :                     char* pRemain = (char*)alloca( bytes - aStream.Tell() );
     261          83 :                     aStream.Read( pRemain, nBytes );
     262          83 :                     rJobData.m_aContext.rebuildFromStreamBuffer( pRemain, nBytes );
     263          83 :                     bContext = true;
     264             :                 }
     265             :             }
     266             :         }
     267             :     }
     268             : 
     269         166 :     return bVersion && bPrinter && bOrientation && bCopies && bContext && bMargin && bPSLevel && bPDFDevice && bColorDevice && bColorDepth;
     270             : }
     271             : 
     272           0 : void JobData::resolveDefaultBackend()
     273             : {
     274           0 :     if (m_nPSLevel == 0 && m_nPDFDevice == 0)
     275           0 :         setDefaultBackend(officecfg::Office::Common::Print::Option::Printer::PDFAsStandardPrintJobFormat::get());
     276           0 : }
     277             : 
     278           0 : void JobData::setDefaultBackend(bool bUsePDF)
     279             : {
     280           0 :     if (bUsePDF && m_nPSLevel == 0 && m_nPDFDevice == 0)
     281           0 :         m_nPDFDevice = 1;
     282           0 : }
     283             : 
     284             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10