LCOV - code coverage report
Current view: top level - vcl/unx/generic/printer - jobdata.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 179 0.0 %
Date: 2014-04-14 Functions: 0 8 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 <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           0 : JobData& JobData::operator=(const JobData& rRight)
      32             : {
      33           0 :     m_nCopies               = rRight.m_nCopies;
      34           0 :     m_bCollate              = rRight.m_bCollate;
      35           0 :     m_nLeftMarginAdjust     = rRight.m_nLeftMarginAdjust;
      36           0 :     m_nRightMarginAdjust    = rRight.m_nRightMarginAdjust;
      37           0 :     m_nTopMarginAdjust      = rRight.m_nTopMarginAdjust;
      38           0 :     m_nBottomMarginAdjust   = rRight.m_nBottomMarginAdjust;
      39           0 :     m_nColorDepth           = rRight.m_nColorDepth;
      40           0 :     m_eOrientation          = rRight.m_eOrientation;
      41           0 :     m_aPrinterName          = rRight.m_aPrinterName;
      42           0 :     m_pParser               = rRight.m_pParser;
      43           0 :     m_aContext              = rRight.m_aContext;
      44           0 :     m_nPSLevel              = rRight.m_nPSLevel;
      45           0 :     m_nPDFDevice            = rRight.m_nPDFDevice;
      46           0 :     m_nColorDevice          = rRight.m_nColorDevice;
      47             : 
      48           0 :     if( !m_pParser && !m_aPrinterName.isEmpty() )
      49             :     {
      50           0 :         PrinterInfoManager& rMgr = PrinterInfoManager::get();
      51           0 :         rMgr.setupJobContextData( *this );
      52             :     }
      53           0 :     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           0 : bool JobData::getStreamBuffer( void*& pData, int& bytes )
     112             : {
     113             :     // consistency checks
     114           0 :     if( ! m_pParser )
     115           0 :         m_pParser = m_aContext.getParser();
     116           0 :     if( m_pParser != m_aContext.getParser() ||
     117           0 :         ! m_pParser )
     118           0 :         return false;
     119             : 
     120           0 :     SvMemoryStream aStream;
     121             : 
     122             :     // write header job data
     123           0 :     aStream.WriteLine(OString("JobData 1"));
     124             : 
     125           0 :     OStringBuffer aLine;
     126             : 
     127           0 :     aLine.append("printer=");
     128           0 :     aLine.append(OUStringToOString(m_aPrinterName, RTL_TEXTENCODING_UTF8));
     129           0 :     aStream.WriteLine(aLine.makeStringAndClear());
     130             : 
     131           0 :     aLine.append("orientation=");
     132           0 :     if (m_eOrientation == orientation::Landscape)
     133           0 :         aLine.append("Landscape");
     134             :     else
     135           0 :         aLine.append("Portrait");
     136           0 :     aStream.WriteLine(aLine.makeStringAndClear());
     137             : 
     138           0 :     aLine.append("copies=");
     139           0 :     aLine.append(static_cast<sal_Int32>(m_nCopies));
     140           0 :     aStream.WriteLine(aLine.makeStringAndClear());
     141             : 
     142           0 :     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           0 :     aLine.append("margindajustment=");
     150           0 :     aLine.append(static_cast<sal_Int32>(m_nLeftMarginAdjust));
     151           0 :     aLine.append(',');
     152           0 :     aLine.append(static_cast<sal_Int32>(m_nRightMarginAdjust));
     153           0 :     aLine.append(',');
     154           0 :     aLine.append(static_cast<sal_Int32>(m_nTopMarginAdjust));
     155           0 :     aLine.append(',');
     156           0 :     aLine.append(static_cast<sal_Int32>(m_nBottomMarginAdjust));
     157           0 :     aStream.WriteLine(aLine.makeStringAndClear());
     158             : 
     159           0 :     aLine.append("colordepth=");
     160           0 :     aLine.append(static_cast<sal_Int32>(m_nColorDepth));
     161           0 :     aStream.WriteLine(aLine.makeStringAndClear());
     162             : 
     163           0 :     aLine.append("pslevel=");
     164           0 :     aLine.append(static_cast<sal_Int32>(m_nPSLevel));
     165           0 :     aStream.WriteLine(aLine.makeStringAndClear());
     166             : 
     167           0 :     aLine.append("pdfdevice=");
     168           0 :     aLine.append(static_cast<sal_Int32>(m_nPDFDevice));
     169           0 :     aStream.WriteLine(aLine.makeStringAndClear());
     170             : 
     171           0 :     aLine.append("colordevice=");
     172           0 :     aLine.append(static_cast<sal_Int32>(m_nColorDevice));
     173           0 :     aStream.WriteLine(aLine.makeStringAndClear());
     174             : 
     175             :     // now append the PPDContext stream buffer
     176           0 :     aStream.WriteLine( "PPDContexData" );
     177             :     sal_uLong nBytes;
     178           0 :     boost::scoped_array<char> pContextBuffer(m_aContext.getStreamableBuffer( nBytes ));
     179           0 :     if( nBytes )
     180           0 :         aStream.Write( pContextBuffer.get(), nBytes );
     181           0 :     pContextBuffer.reset();
     182             : 
     183             :     // success
     184           0 :     pData = rtl_allocateMemory( bytes = aStream.Tell() );
     185           0 :     memcpy( pData, aStream.GetData(), bytes );
     186           0 :     return true;
     187             : }
     188             : 
     189           0 : bool JobData::constructFromStreamBuffer( void* pData, int bytes, JobData& rJobData )
     190             : {
     191           0 :     SvMemoryStream aStream( pData, bytes, STREAM_READ );
     192           0 :     OString aLine;
     193           0 :     bool bVersion       = false;
     194           0 :     bool bPrinter       = false;
     195           0 :     bool bOrientation   = false;
     196           0 :     bool bCopies        = false;
     197           0 :     bool bContext       = false;
     198           0 :     bool bMargin        = false;
     199           0 :     bool bColorDepth    = false;
     200           0 :     bool bColorDevice   = false;
     201           0 :     bool bPSLevel       = false;
     202           0 :     bool bPDFDevice     = false;
     203             : 
     204           0 :     const char printerEquals[] = "printer=";
     205           0 :     const char orientatationEquals[] = "orientation=";
     206           0 :     const char copiesEquals[] = "copies=";
     207           0 :     const char collateEquals[] = "collate=";
     208           0 :     const char margindajustmentEquals[] = "margindajustment=";
     209           0 :     const char colordepthEquals[] = "colordepth=";
     210           0 :     const char colordeviceEquals[] = "colordevice=";
     211           0 :     const char pslevelEquals[] = "pslevel=";
     212           0 :     const char pdfdeviceEquals[] = "pdfdevice=";
     213             : 
     214           0 :     while( ! aStream.IsEof() )
     215             :     {
     216           0 :         aStream.ReadLine( aLine );
     217           0 :         if (aLine.startsWith("JobData"))
     218           0 :             bVersion = true;
     219           0 :         else if (aLine.startsWith(printerEquals))
     220             :         {
     221           0 :             bPrinter = true;
     222           0 :             rJobData.m_aPrinterName = OStringToOUString(aLine.copy(RTL_CONSTASCII_LENGTH(printerEquals)), RTL_TEXTENCODING_UTF8);
     223             :         }
     224           0 :         else if (aLine.startsWith(orientatationEquals))
     225             :         {
     226           0 :             bOrientation = true;
     227           0 :             rJobData.m_eOrientation = aLine.copy(RTL_CONSTASCII_LENGTH(orientatationEquals)).equalsIgnoreAsciiCase("landscape") ? orientation::Landscape : orientation::Portrait;
     228             :         }
     229           0 :         else if (aLine.startsWith(copiesEquals))
     230             :         {
     231           0 :             bCopies = true;
     232           0 :             rJobData.m_nCopies = aLine.copy(RTL_CONSTASCII_LENGTH(copiesEquals)).toInt32();
     233             :         }
     234           0 :         else if (aLine.startsWith(collateEquals))
     235             :         {
     236           0 :             rJobData.m_bCollate = aLine.copy(RTL_CONSTASCII_LENGTH(collateEquals)).toInt32();
     237             :         }
     238           0 :         else if (aLine.startsWith(margindajustmentEquals))
     239             :         {
     240           0 :             bMargin = true;
     241           0 :             OString aValues(aLine.copy(RTL_CONSTASCII_LENGTH(margindajustmentEquals)));
     242           0 :             rJobData.m_nLeftMarginAdjust = aValues.getToken(0, ',').toInt32();
     243           0 :             rJobData.m_nRightMarginAdjust = aValues.getToken(1, ',').toInt32();
     244           0 :             rJobData.m_nTopMarginAdjust = aValues.getToken(2, ',').toInt32();
     245           0 :             rJobData.m_nBottomMarginAdjust = aValues.getToken(3, ',').toInt32();
     246             :         }
     247           0 :         else if (aLine.startsWith(colordepthEquals))
     248             :         {
     249           0 :             bColorDepth = true;
     250           0 :             rJobData.m_nColorDepth = aLine.copy(RTL_CONSTASCII_LENGTH(colordepthEquals)).toInt32();
     251             :         }
     252           0 :         else if (aLine.startsWith(colordeviceEquals))
     253             :         {
     254           0 :             bColorDevice = true;
     255           0 :             rJobData.m_nColorDevice = aLine.copy(RTL_CONSTASCII_LENGTH(colordeviceEquals)).toInt32();
     256             :         }
     257           0 :         else if (aLine.startsWith(pslevelEquals))
     258             :         {
     259           0 :             bPSLevel = true;
     260           0 :             rJobData.m_nPSLevel = aLine.copy(RTL_CONSTASCII_LENGTH(pslevelEquals)).toInt32();
     261             :         }
     262           0 :         else if (aLine.startsWith(pdfdeviceEquals))
     263             :         {
     264           0 :             bPDFDevice = true;
     265           0 :             rJobData.m_nPDFDevice = aLine.copy(RTL_CONSTASCII_LENGTH(pdfdeviceEquals)).toInt32();
     266             :         }
     267           0 :         else if (aLine == "PPDContexData")
     268             :         {
     269           0 :             if( bPrinter )
     270             :             {
     271           0 :                 PrinterInfoManager& rManager = PrinterInfoManager::get();
     272           0 :                 const PrinterInfo& rInfo = rManager.getPrinterInfo( rJobData.m_aPrinterName );
     273           0 :                 rJobData.m_pParser = PPDParser::getParser( rInfo.m_aDriverName );
     274           0 :                 if( rJobData.m_pParser )
     275             :                 {
     276           0 :                     rJobData.m_aContext.setParser( rJobData.m_pParser );
     277           0 :                     int nBytes = bytes - aStream.Tell();
     278           0 :                     char* pRemain = (char*)alloca( bytes - aStream.Tell() );
     279           0 :                     aStream.Read( pRemain, nBytes );
     280           0 :                     rJobData.m_aContext.rebuildFromStreamBuffer( pRemain, nBytes );
     281           0 :                     bContext = true;
     282             :                 }
     283             :             }
     284             :         }
     285             :     }
     286             : 
     287           0 :     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