Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / vcl / unx / generic / printer / jobdata.cxx
blob389f74dc8be80307b19767a643fee82bc7001f27
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 void JobData::setCollate( bool bCollate )
58 if (m_nPDFDevice > 0)
60 m_bCollate = bCollate;
61 return;
63 const PPDParser* pParser = m_aContext.getParser();
64 if( pParser )
66 const PPDKey* pKey = pParser->getKey( OUString( "Collate" ) );
67 if( pKey )
69 const PPDValue* pVal = NULL;
70 if( bCollate )
71 pVal = pKey->getValue( OUString( "True" ) );
72 else
74 pVal = pKey->getValue( OUString( "False" ) );
75 if( ! pVal )
76 pVal = pKey->getValue( OUString( "None" ) );
78 m_aContext.setValue( pKey, pVal );
83 bool JobData::setPaper( int i_nWidth, int i_nHeight )
85 bool bSuccess = false;
86 if( m_pParser )
88 OUString aPaper( m_pParser->matchPaper( i_nWidth, i_nHeight ) );
90 const PPDKey* pKey = m_pParser->getKey( OUString( "PageSize" ) );
91 const PPDValue* pValue = pKey ? pKey->getValueCaseInsensitive( aPaper ) : NULL;
93 bSuccess = pKey && pValue && m_aContext.setValue( pKey, pValue, false );
95 return bSuccess;
98 bool JobData::setPaperBin( int i_nPaperBin )
100 bool bSuccess = false;
101 if( m_pParser )
103 const PPDKey* pKey = m_pParser->getKey( OUString( "InputSlot" ) );
104 const PPDValue* pValue = pKey ? pKey->getValue( i_nPaperBin ) : NULL;
106 bSuccess = pKey && pValue && m_aContext.setValue( pKey, pValue, false );
108 return bSuccess;
111 bool JobData::getStreamBuffer( void*& pData, int& bytes )
113 // consistency checks
114 if( ! m_pParser )
115 m_pParser = m_aContext.getParser();
116 if( m_pParser != m_aContext.getParser() ||
117 ! m_pParser )
118 return false;
120 SvMemoryStream aStream;
122 // write header job data
123 aStream.WriteLine(OString("JobData 1"));
125 OStringBuffer aLine;
127 aLine.append("printer=");
128 aLine.append(OUStringToOString(m_aPrinterName, RTL_TEXTENCODING_UTF8));
129 aStream.WriteLine(aLine.makeStringAndClear());
131 aLine.append("orientation=");
132 if (m_eOrientation == orientation::Landscape)
133 aLine.append("Landscape");
134 else
135 aLine.append("Portrait");
136 aStream.WriteLine(aLine.makeStringAndClear());
138 aLine.append("copies=");
139 aLine.append(static_cast<sal_Int32>(m_nCopies));
140 aStream.WriteLine(aLine.makeStringAndClear());
142 if (m_nPDFDevice > 0)
144 aLine.append("collate=");
145 aLine.append(OString::boolean(m_bCollate));
146 aStream.WriteLine(aLine.makeStringAndClear());
149 aLine.append("margindajustment=");
150 aLine.append(static_cast<sal_Int32>(m_nLeftMarginAdjust));
151 aLine.append(',');
152 aLine.append(static_cast<sal_Int32>(m_nRightMarginAdjust));
153 aLine.append(',');
154 aLine.append(static_cast<sal_Int32>(m_nTopMarginAdjust));
155 aLine.append(',');
156 aLine.append(static_cast<sal_Int32>(m_nBottomMarginAdjust));
157 aStream.WriteLine(aLine.makeStringAndClear());
159 aLine.append("colordepth=");
160 aLine.append(static_cast<sal_Int32>(m_nColorDepth));
161 aStream.WriteLine(aLine.makeStringAndClear());
163 aLine.append("pslevel=");
164 aLine.append(static_cast<sal_Int32>(m_nPSLevel));
165 aStream.WriteLine(aLine.makeStringAndClear());
167 aLine.append("pdfdevice=");
168 aLine.append(static_cast<sal_Int32>(m_nPDFDevice));
169 aStream.WriteLine(aLine.makeStringAndClear());
171 aLine.append("colordevice=");
172 aLine.append(static_cast<sal_Int32>(m_nColorDevice));
173 aStream.WriteLine(aLine.makeStringAndClear());
175 // now append the PPDContext stream buffer
176 aStream.WriteLine( "PPDContexData" );
177 sal_uLong nBytes;
178 boost::scoped_array<char> pContextBuffer(m_aContext.getStreamableBuffer( nBytes ));
179 if( nBytes )
180 aStream.Write( pContextBuffer.get(), nBytes );
181 pContextBuffer.reset();
183 // success
184 pData = rtl_allocateMemory( bytes = aStream.Tell() );
185 memcpy( pData, aStream.GetData(), bytes );
186 return true;
189 bool JobData::constructFromStreamBuffer( void* pData, int bytes, JobData& rJobData )
191 SvMemoryStream aStream( pData, bytes, STREAM_READ );
192 OString aLine;
193 bool bVersion = false;
194 bool bPrinter = false;
195 bool bOrientation = false;
196 bool bCopies = false;
197 bool bContext = false;
198 bool bMargin = false;
199 bool bColorDepth = false;
200 bool bColorDevice = false;
201 bool bPSLevel = false;
202 bool bPDFDevice = false;
204 const char printerEquals[] = "printer=";
205 const char orientatationEquals[] = "orientation=";
206 const char copiesEquals[] = "copies=";
207 const char collateEquals[] = "collate=";
208 const char margindajustmentEquals[] = "margindajustment=";
209 const char colordepthEquals[] = "colordepth=";
210 const char colordeviceEquals[] = "colordevice=";
211 const char pslevelEquals[] = "pslevel=";
212 const char pdfdeviceEquals[] = "pdfdevice=";
214 while( ! aStream.IsEof() )
216 aStream.ReadLine( aLine );
217 if (aLine.startsWith("JobData"))
218 bVersion = true;
219 else if (aLine.startsWith(printerEquals))
221 bPrinter = true;
222 rJobData.m_aPrinterName = OStringToOUString(aLine.copy(RTL_CONSTASCII_LENGTH(printerEquals)), RTL_TEXTENCODING_UTF8);
224 else if (aLine.startsWith(orientatationEquals))
226 bOrientation = true;
227 rJobData.m_eOrientation = aLine.copy(RTL_CONSTASCII_LENGTH(orientatationEquals)).equalsIgnoreAsciiCase("landscape") ? orientation::Landscape : orientation::Portrait;
229 else if (aLine.startsWith(copiesEquals))
231 bCopies = true;
232 rJobData.m_nCopies = aLine.copy(RTL_CONSTASCII_LENGTH(copiesEquals)).toInt32();
234 else if (aLine.startsWith(collateEquals))
236 rJobData.m_bCollate = aLine.copy(RTL_CONSTASCII_LENGTH(collateEquals)).toInt32();
238 else if (aLine.startsWith(margindajustmentEquals))
240 bMargin = true;
241 OString aValues(aLine.copy(RTL_CONSTASCII_LENGTH(margindajustmentEquals)));
242 rJobData.m_nLeftMarginAdjust = aValues.getToken(0, ',').toInt32();
243 rJobData.m_nRightMarginAdjust = aValues.getToken(1, ',').toInt32();
244 rJobData.m_nTopMarginAdjust = aValues.getToken(2, ',').toInt32();
245 rJobData.m_nBottomMarginAdjust = aValues.getToken(3, ',').toInt32();
247 else if (aLine.startsWith(colordepthEquals))
249 bColorDepth = true;
250 rJobData.m_nColorDepth = aLine.copy(RTL_CONSTASCII_LENGTH(colordepthEquals)).toInt32();
252 else if (aLine.startsWith(colordeviceEquals))
254 bColorDevice = true;
255 rJobData.m_nColorDevice = aLine.copy(RTL_CONSTASCII_LENGTH(colordeviceEquals)).toInt32();
257 else if (aLine.startsWith(pslevelEquals))
259 bPSLevel = true;
260 rJobData.m_nPSLevel = aLine.copy(RTL_CONSTASCII_LENGTH(pslevelEquals)).toInt32();
262 else if (aLine.startsWith(pdfdeviceEquals))
264 bPDFDevice = true;
265 rJobData.m_nPDFDevice = aLine.copy(RTL_CONSTASCII_LENGTH(pdfdeviceEquals)).toInt32();
267 else if (aLine == "PPDContexData")
269 if( bPrinter )
271 PrinterInfoManager& rManager = PrinterInfoManager::get();
272 const PrinterInfo& rInfo = rManager.getPrinterInfo( rJobData.m_aPrinterName );
273 rJobData.m_pParser = PPDParser::getParser( rInfo.m_aDriverName );
274 if( rJobData.m_pParser )
276 rJobData.m_aContext.setParser( rJobData.m_pParser );
277 int nBytes = bytes - aStream.Tell();
278 char* pRemain = (char*)alloca( bytes - aStream.Tell() );
279 aStream.Read( pRemain, nBytes );
280 rJobData.m_aContext.rebuildFromStreamBuffer( pRemain, nBytes );
281 bContext = true;
287 return bVersion && bPrinter && bOrientation && bCopies && bContext && bMargin && bPSLevel && bPDFDevice && bColorDevice && bColorDepth;
290 void JobData::resolveDefaultBackend()
292 if (m_nPSLevel == 0 && m_nPDFDevice == 0)
293 setDefaultBackend(officecfg::Office::Common::Print::Option::Printer::PDFAsStandardPrintJobFormat::get());
296 void JobData::setDefaultBackend(bool bUsePDF)
298 if (bUsePDF && m_nPSLevel == 0 && m_nPDFDevice == 0)
299 m_nPDFDevice = 1;
302 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */