Branch libreoffice-5-0-4
[LibreOffice.git] / vcl / unx / generic / printer / jobdata.cxx
blob778961faa1bb1c4ed01f8dae707122a088edd80b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <officecfg/Office/Common.hxx>
21 #include "vcl/jobdata.hxx"
22 #include "vcl/printerinfomanager.hxx"
23 #include "tools/stream.hxx"
25 #include <sal/alloca.h>
26 #include <rtl/strbuf.hxx>
27 #include <boost/scoped_array.hpp>
29 using namespace psp;
31 JobData& JobData::operator=(const JobData& rRight)
33 m_nCopies = rRight.m_nCopies;
34 m_bCollate = rRight.m_bCollate;
35 m_nLeftMarginAdjust = rRight.m_nLeftMarginAdjust;
36 m_nRightMarginAdjust = rRight.m_nRightMarginAdjust;
37 m_nTopMarginAdjust = rRight.m_nTopMarginAdjust;
38 m_nBottomMarginAdjust = rRight.m_nBottomMarginAdjust;
39 m_nColorDepth = rRight.m_nColorDepth;
40 m_eOrientation = rRight.m_eOrientation;
41 m_aPrinterName = rRight.m_aPrinterName;
42 m_pParser = rRight.m_pParser;
43 m_aContext = rRight.m_aContext;
44 m_nPSLevel = rRight.m_nPSLevel;
45 m_nPDFDevice = rRight.m_nPDFDevice;
46 m_nColorDevice = rRight.m_nColorDevice;
48 if( !m_pParser && !m_aPrinterName.isEmpty() )
50 PrinterInfoManager& rMgr = PrinterInfoManager::get();
51 rMgr.setupJobContextData( *this );
53 return *this;
56 bool psp::operator==(const psp::JobData& rLeft, const psp::JobData& rRight)
58 return rLeft.m_nCopies == rRight.m_nCopies
59 // && rLeft.m_bCollate == rRight.m_bCollate
60 && rLeft.m_nLeftMarginAdjust == rRight.m_nLeftMarginAdjust
61 && rLeft.m_nRightMarginAdjust == rRight.m_nRightMarginAdjust
62 && rLeft.m_nTopMarginAdjust == rRight.m_nTopMarginAdjust
63 && rLeft.m_nBottomMarginAdjust == rRight.m_nBottomMarginAdjust
64 && rLeft.m_nColorDepth == rRight.m_nColorDepth
65 && rLeft.m_eOrientation == rRight.m_eOrientation
66 && rLeft.m_aPrinterName == rRight.m_aPrinterName
67 && rLeft.m_pParser == rRight.m_pParser
68 // && rLeft.m_aContext == rRight.m_aContext
69 && rLeft.m_nPSLevel == rRight.m_nPSLevel
70 && rLeft.m_nPDFDevice == rRight.m_nPDFDevice
71 && rLeft.m_nColorDevice == rRight.m_nColorDevice;
74 void JobData::setCollate( bool bCollate )
76 if (m_nPDFDevice > 0)
78 m_bCollate = bCollate;
79 return;
81 const PPDParser* pParser = m_aContext.getParser();
82 if( pParser )
84 const PPDKey* pKey = pParser->getKey( OUString( "Collate" ) );
85 if( pKey )
87 const PPDValue* pVal = NULL;
88 if( bCollate )
89 pVal = pKey->getValue( OUString( "True" ) );
90 else
92 pVal = pKey->getValue( OUString( "False" ) );
93 if( ! pVal )
94 pVal = pKey->getValue( OUString( "None" ) );
96 m_aContext.setValue( pKey, pVal );
101 bool JobData::setPaper( int i_nWidth, int i_nHeight )
103 bool bSuccess = false;
104 if( m_pParser )
106 OUString aPaper( m_pParser->matchPaper( i_nWidth, i_nHeight ) );
108 const PPDKey* pKey = m_pParser->getKey( OUString( "PageSize" ) );
109 const PPDValue* pValue = pKey ? pKey->getValueCaseInsensitive( aPaper ) : NULL;
111 bSuccess = pKey && pValue && m_aContext.setValue( pKey, pValue, false );
113 return bSuccess;
116 bool JobData::setPaperBin( int i_nPaperBin )
118 bool bSuccess = false;
119 if( m_pParser )
121 const PPDKey* pKey = m_pParser->getKey( OUString( "InputSlot" ) );
122 const PPDValue* pValue = pKey ? pKey->getValue( i_nPaperBin ) : NULL;
124 bSuccess = pKey && pValue && m_aContext.setValue( pKey, pValue, false );
126 return bSuccess;
129 bool JobData::getStreamBuffer( void*& pData, int& bytes )
131 // consistency checks
132 if( ! m_pParser )
133 m_pParser = m_aContext.getParser();
134 if( m_pParser != m_aContext.getParser() ||
135 ! m_pParser )
136 return false;
138 SvMemoryStream aStream;
140 // write header job data
141 aStream.WriteLine(OString("JobData 1"));
143 OStringBuffer aLine;
145 aLine.append("printer=");
146 aLine.append(OUStringToOString(m_aPrinterName, RTL_TEXTENCODING_UTF8));
147 aStream.WriteLine(aLine.makeStringAndClear());
149 aLine.append("orientation=");
150 if (m_eOrientation == orientation::Landscape)
151 aLine.append("Landscape");
152 else
153 aLine.append("Portrait");
154 aStream.WriteLine(aLine.makeStringAndClear());
156 aLine.append("copies=");
157 aLine.append(static_cast<sal_Int32>(m_nCopies));
158 aStream.WriteLine(aLine.makeStringAndClear());
160 if (m_nPDFDevice > 0)
162 aLine.append("collate=");
163 aLine.append(OString::boolean(m_bCollate));
164 aStream.WriteLine(aLine.makeStringAndClear());
167 aLine.append("margindajustment=");
168 aLine.append(static_cast<sal_Int32>(m_nLeftMarginAdjust));
169 aLine.append(',');
170 aLine.append(static_cast<sal_Int32>(m_nRightMarginAdjust));
171 aLine.append(',');
172 aLine.append(static_cast<sal_Int32>(m_nTopMarginAdjust));
173 aLine.append(',');
174 aLine.append(static_cast<sal_Int32>(m_nBottomMarginAdjust));
175 aStream.WriteLine(aLine.makeStringAndClear());
177 aLine.append("colordepth=");
178 aLine.append(static_cast<sal_Int32>(m_nColorDepth));
179 aStream.WriteLine(aLine.makeStringAndClear());
181 aLine.append("pslevel=");
182 aLine.append(static_cast<sal_Int32>(m_nPSLevel));
183 aStream.WriteLine(aLine.makeStringAndClear());
185 aLine.append("pdfdevice=");
186 aLine.append(static_cast<sal_Int32>(m_nPDFDevice));
187 aStream.WriteLine(aLine.makeStringAndClear());
189 aLine.append("colordevice=");
190 aLine.append(static_cast<sal_Int32>(m_nColorDevice));
191 aStream.WriteLine(aLine.makeStringAndClear());
193 // now append the PPDContext stream buffer
194 aStream.WriteLine( "PPDContexData" );
195 sal_uLong nBytes;
196 boost::scoped_array<char> pContextBuffer(m_aContext.getStreamableBuffer( nBytes ));
197 if( nBytes )
198 aStream.Write( pContextBuffer.get(), nBytes );
199 pContextBuffer.reset();
201 // success
202 pData = rtl_allocateMemory( bytes = aStream.Tell() );
203 memcpy( pData, aStream.GetData(), bytes );
204 return true;
207 bool JobData::constructFromStreamBuffer( void* pData, int bytes, JobData& rJobData )
209 SvMemoryStream aStream( pData, bytes, StreamMode::READ );
210 OString aLine;
211 bool bVersion = false;
212 bool bPrinter = false;
213 bool bOrientation = false;
214 bool bCopies = false;
215 bool bContext = false;
216 bool bMargin = false;
217 bool bColorDepth = false;
218 bool bColorDevice = false;
219 bool bPSLevel = false;
220 bool bPDFDevice = false;
222 const char printerEquals[] = "printer=";
223 const char orientatationEquals[] = "orientation=";
224 const char copiesEquals[] = "copies=";
225 const char collateEquals[] = "collate=";
226 const char margindajustmentEquals[] = "margindajustment=";
227 const char colordepthEquals[] = "colordepth=";
228 const char colordeviceEquals[] = "colordevice=";
229 const char pslevelEquals[] = "pslevel=";
230 const char pdfdeviceEquals[] = "pdfdevice=";
232 while( ! aStream.IsEof() )
234 aStream.ReadLine( aLine );
235 if (aLine.startsWith("JobData"))
236 bVersion = true;
237 else if (aLine.startsWith(printerEquals))
239 bPrinter = true;
240 rJobData.m_aPrinterName = OStringToOUString(aLine.copy(RTL_CONSTASCII_LENGTH(printerEquals)), RTL_TEXTENCODING_UTF8);
242 else if (aLine.startsWith(orientatationEquals))
244 bOrientation = true;
245 rJobData.m_eOrientation = aLine.copy(RTL_CONSTASCII_LENGTH(orientatationEquals)).equalsIgnoreAsciiCase("landscape") ? orientation::Landscape : orientation::Portrait;
247 else if (aLine.startsWith(copiesEquals))
249 bCopies = true;
250 rJobData.m_nCopies = aLine.copy(RTL_CONSTASCII_LENGTH(copiesEquals)).toInt32();
252 else if (aLine.startsWith(collateEquals))
254 rJobData.m_bCollate = aLine.copy(RTL_CONSTASCII_LENGTH(collateEquals)).toInt32();
256 else if (aLine.startsWith(margindajustmentEquals))
258 bMargin = true;
259 OString aValues(aLine.copy(RTL_CONSTASCII_LENGTH(margindajustmentEquals)));
260 rJobData.m_nLeftMarginAdjust = aValues.getToken(0, ',').toInt32();
261 rJobData.m_nRightMarginAdjust = aValues.getToken(1, ',').toInt32();
262 rJobData.m_nTopMarginAdjust = aValues.getToken(2, ',').toInt32();
263 rJobData.m_nBottomMarginAdjust = aValues.getToken(3, ',').toInt32();
265 else if (aLine.startsWith(colordepthEquals))
267 bColorDepth = true;
268 rJobData.m_nColorDepth = aLine.copy(RTL_CONSTASCII_LENGTH(colordepthEquals)).toInt32();
270 else if (aLine.startsWith(colordeviceEquals))
272 bColorDevice = true;
273 rJobData.m_nColorDevice = aLine.copy(RTL_CONSTASCII_LENGTH(colordeviceEquals)).toInt32();
275 else if (aLine.startsWith(pslevelEquals))
277 bPSLevel = true;
278 rJobData.m_nPSLevel = aLine.copy(RTL_CONSTASCII_LENGTH(pslevelEquals)).toInt32();
280 else if (aLine.startsWith(pdfdeviceEquals))
282 bPDFDevice = true;
283 rJobData.m_nPDFDevice = aLine.copy(RTL_CONSTASCII_LENGTH(pdfdeviceEquals)).toInt32();
285 else if (aLine == "PPDContexData")
287 if( bPrinter )
289 PrinterInfoManager& rManager = PrinterInfoManager::get();
290 const PrinterInfo& rInfo = rManager.getPrinterInfo( rJobData.m_aPrinterName );
291 rJobData.m_pParser = PPDParser::getParser( rInfo.m_aDriverName );
292 if( rJobData.m_pParser )
294 rJobData.m_aContext.setParser( rJobData.m_pParser );
295 int nBytes = bytes - aStream.Tell();
296 char* pRemain = static_cast<char*>(alloca( bytes - aStream.Tell() ));
297 aStream.Read( pRemain, nBytes );
298 rJobData.m_aContext.rebuildFromStreamBuffer( pRemain, nBytes );
299 bContext = true;
305 return bVersion && bPrinter && bOrientation && bCopies && bContext && bMargin && bPSLevel && bPDFDevice && bColorDevice && bColorDepth;
308 void JobData::resolveDefaultBackend()
310 if (m_nPSLevel == 0 && m_nPDFDevice == 0)
311 setDefaultBackend(officecfg::Office::Common::Print::Option::Printer::PDFAsStandardPrintJobFormat::get());
314 void JobData::setDefaultBackend(bool bUsePDF)
316 if (bUsePDF && m_nPSLevel == 0 && m_nPDFDevice == 0)
317 m_nPDFDevice = 1;
320 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */