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

Generated by: LCOV version 1.10